update from main archive 970619
[glibc/history.git] / nis / nis_defaults.c
blob53a585b5169d7cfcd79fe74b734a75e7e9599dbf
1 /* Copyright (c) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <rpc/rpc.h>
25 #include <rpcsvc/nis.h>
26 #include <rpcsvc/nislib.h>
28 #define DEFAULT_TTL 43200
31 ** Some functions for parsing the -D param and NIS_DEFAULTS Environ
33 static nis_name
34 searchgroup (char *str)
36 static char default_group[NIS_MAXNAMELEN];
37 char *cptr;
38 int i;
40 cptr = strstr (str, "group=");
41 if (cptr == NULL)
42 return NULL;
44 cptr += 6; /* points to the begin of the group string */
45 i = 0;
46 while (cptr[i] != '\0' && cptr[i] != ':')
47 i++;
48 if (i == 0) /* only "group=" ? */
49 return (nis_name) "";
51 return strncpy (default_group, cptr, i);
54 static nis_name
55 searchowner (char *str)
57 static char default_owner[NIS_MAXNAMELEN];
58 char *cptr;
59 int i;
61 cptr = strstr (str, "owner=");
62 if (cptr == NULL)
63 return NULL;
65 cptr += 6; /* points to the begin of the owner string */
66 i = 0;
67 while (cptr[i] != '\0' && cptr[i] != ':')
68 i++;
69 if (i == 0) /* only "owner=" ? */
70 return (nis_name)"";
72 return strncpy (default_owner, cptr, i);
75 static u_long
76 searchttl (char *str)
78 char buf[1024];
79 char *cptr, *dptr;
80 u_long time;
81 int i;
83 dptr = strstr (str, "ttl=");
84 if (dptr == NULL) /* should (could) not happen */
85 return DEFAULT_TTL;;
87 dptr += 4; /* points to the begin of the new ttl */
88 i = 0;
89 while (dptr[i] != '\0' && dptr[i] != ':')
90 i++;
91 if (i == 0) /* only "ttl=" ? */
92 return DEFAULT_TTL;
94 strncpy (buf, dptr, i);
95 time = 0;
97 dptr = buf;
98 cptr = strchr (dptr, 'd');
99 if (cptr != NULL)
101 *cptr = '\0';
102 cptr++;
103 time += atoi (dptr) * 60 * 60 * 24;
104 dptr = cptr;
107 cptr = strchr (dptr, 'h');
108 if (cptr != NULL)
110 *cptr = '\0';
111 cptr++;
112 time += atoi (dptr) * 60 * 60;
113 dptr = cptr;
116 cptr = strchr (dptr, 'm');
117 if (cptr != NULL)
119 *cptr = '\0';
120 cptr++;
121 time += atoi (dptr) * 60;
122 dptr = cptr;
125 cptr = strchr (dptr, 's');
126 if (cptr != NULL)
127 *cptr = '\0';
129 time += atoi (dptr);
131 return time;
134 static u_long
135 searchaccess (char *str, u_long access)
137 static char buf[NIS_MAXNAMELEN];
138 char *cptr;
139 u_long result;
140 int i;
141 int n, o, g, w;
143 cptr = strstr (str, "access=");
144 if (cptr == NULL)
145 return 0;
147 cptr += 7; /* points to the begin of the access string */
148 i = 0;
149 while (cptr[i] != '\0' && cptr[i] != ':')
150 i++;
151 if (i == 0) /* only "access=" ? */
152 return 0;
154 strncpy (buf, cptr, i);
156 result = n = o = g = w = 0;
157 cptr = buf;
158 while (*cptr != '\0')
160 switch (*cptr)
162 case 'n':
163 n = 1;
164 break;
165 case 'o':
166 o = 1;
167 break;
168 case 'g':
169 g = 1;
170 break;
171 case 'w':
172 w = 1;
173 break;
174 case 'a':
175 o = g = w = 1;
176 break;
177 case '-':
178 cptr++; /* Remove "=" from beginning */
179 while (*cptr != '\0' && *cptr != ',')
181 switch (*cptr)
183 case 'r':
184 if (n)
185 result = result & ~(NIS_READ_ACC << 24);
186 if (o)
187 result = result & ~(NIS_READ_ACC << 16);
188 if (g)
189 result = result & ~(NIS_READ_ACC << 8);
190 if (w)
191 result = result & ~(NIS_READ_ACC);
192 break;
193 case 'm':
194 if (n)
195 result = result & ~(NIS_MODIFY_ACC << 24);
196 if (o)
197 result = result & ~(NIS_MODIFY_ACC << 16);
198 if (g)
199 result = result & ~(NIS_MODIFY_ACC << 8);
200 if (w)
201 result = result & ~(NIS_MODIFY_ACC);
202 break;
203 case 'c':
204 if (n)
205 result = result & ~(NIS_CREATE_ACC << 24);
206 if (o)
207 result = result & ~(NIS_CREATE_ACC << 16);
208 if (g)
209 result = result & ~(NIS_CREATE_ACC << 8);
210 if (w)
211 result = result & ~(NIS_CREATE_ACC);
212 break;
213 case 'd':
214 if (n)
215 result = result & ~(NIS_DESTROY_ACC << 24);
216 if (o)
217 result = result & ~(NIS_DESTROY_ACC << 16);
218 if (g)
219 result = result & ~(NIS_DESTROY_ACC << 8);
220 if (w)
221 result = result & ~(NIS_DESTROY_ACC);
222 break;
223 default:
224 fprintf (stderr, "Parse error in \"%s\"\n", buf);
225 return 0;
227 cptr++;
229 break;
230 case '+':
231 cptr++; /* Remove "=" from beginning */
232 while (*cptr != '\0' && *cptr != ',')
234 switch (*cptr)
236 case 'r':
237 if (n)
238 result = result | (NIS_READ_ACC << 24);
239 if (o)
240 result = result | (NIS_READ_ACC << 16);
241 if (g)
242 result = result | (NIS_READ_ACC << 8);
243 if (w)
244 result = result | (NIS_READ_ACC);
245 break;
246 case 'm':
247 if (n)
248 result = result | (NIS_MODIFY_ACC << 24);
249 if (o)
250 result = result | (NIS_MODIFY_ACC << 16);
251 if (g)
252 result = result | (NIS_MODIFY_ACC << 8);
253 if (w)
254 result = result | (NIS_MODIFY_ACC);
255 break;
256 case 'c':
257 if (n)
258 result = result | (NIS_CREATE_ACC << 24);
259 if (o)
260 result = result | (NIS_CREATE_ACC << 16);
261 if (g)
262 result = result | (NIS_CREATE_ACC << 8);
263 if (w)
264 result = result | (NIS_CREATE_ACC);
265 break;
266 case 'd':
267 if (n)
268 result = result | (NIS_DESTROY_ACC << 24);
269 if (o)
270 result = result | (NIS_DESTROY_ACC << 16);
271 if (g)
272 result = result | (NIS_DESTROY_ACC << 8);
273 if (w)
274 result = result | (NIS_DESTROY_ACC);
275 break;
276 default:
277 fprintf (stderr, "Parse error in \"%s\"\n", buf);
278 return 0;
280 cptr++;
282 break;
283 case '=':
284 cptr++; /* Remove "=" from beginning */
285 /* Clear */
286 if (n)
287 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
288 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
290 if (o)
291 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
292 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
293 if (g)
294 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
295 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
296 if (w)
297 result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
298 NIS_CREATE_ACC + NIS_DESTROY_ACC);
299 while (*cptr != '\0' && *cptr != ',')
301 switch (*cptr)
303 case 'r':
304 if (n)
305 result = result | (NIS_READ_ACC << 24);
306 if (o)
307 result = result | (NIS_READ_ACC << 16);
308 if (g)
309 result = result | (NIS_READ_ACC << 8);
310 if (w)
311 result = result | (NIS_READ_ACC);
312 break;
313 case 'm':
314 if (n)
315 result = result | (NIS_MODIFY_ACC << 24);
316 if (o)
317 result = result | (NIS_MODIFY_ACC << 16);
318 if (g)
319 result = result | (NIS_MODIFY_ACC << 8);
320 if (w)
321 result = result | (NIS_MODIFY_ACC);
322 break;
323 case 'c':
324 if (n)
325 result = result | (NIS_CREATE_ACC << 24);
326 if (o)
327 result = result | (NIS_CREATE_ACC << 16);
328 if (g)
329 result = result | (NIS_CREATE_ACC << 8);
330 if (w)
331 result = result | (NIS_CREATE_ACC);
332 break;
333 case 'd':
334 if (n)
335 result = result | (NIS_DESTROY_ACC << 24);
336 if (o)
337 result = result | (NIS_DESTROY_ACC << 16);
338 if (g)
339 result = result | (NIS_DESTROY_ACC << 8);
340 if (w)
341 result = result | (NIS_DESTROY_ACC);
342 break;
343 default:
344 fprintf (stderr, "Parse error in \"%s\"\n", buf);
345 return 0;
347 cptr++;
349 break;
350 default:
351 fprintf (stderr, "Parse error in \"%s\"\n", buf);
352 return 0;
354 cptr++;
357 return 0;
360 nis_name
361 __nis_default_owner (char *defaults)
363 static char default_owner[NIS_MAXNAMELEN];
364 char *cptr, *dptr;
366 strcpy (default_owner, nis_local_principal ());
368 if (defaults != NULL)
370 dptr = strstr (defaults, "owner=");
371 if (dptr != NULL)
372 strcpy (default_owner, searchowner (defaults));
374 else
376 cptr = getenv ("NIS_DEFAULTS");
377 if (cptr != NULL)
379 dptr = strstr (cptr, "owner=");
380 if (dptr != NULL)
381 strcpy (default_owner, searchowner (cptr));
385 return default_owner;
388 nis_name
389 __nis_default_group (char *defaults)
391 static char default_group[NIS_MAXNAMELEN];
392 char *cptr, *dptr;
394 strcpy (default_group, nis_local_group ());
396 if (defaults != NULL)
398 dptr = strstr (defaults, "group=");
399 if (dptr != NULL)
400 strcpy (default_group, searchgroup (defaults));
402 else
404 cptr = getenv ("NIS_DEFAULTS");
405 if (cptr != NULL)
407 dptr = strstr (cptr, "group=");
408 if (dptr != NULL)
409 strcpy (default_group, searchgroup (cptr));
413 return default_group;
416 u_long
417 __nis_default_ttl (char *defaults)
419 char *cptr, *dptr;
421 if (defaults != NULL)
423 dptr = strstr (defaults, "ttl=");
424 if (dptr != NULL)
425 return searchttl (defaults);
428 cptr = getenv ("NIS_DEFAULTS");
429 if (cptr == NULL)
430 return DEFAULT_TTL;
432 dptr = strstr (cptr, "ttl=");
433 if (dptr == NULL)
434 return DEFAULT_TTL;
436 return searchttl (cptr);
439 /* Default access rights are ----rmcdr---r---, but we could change
440 this with the NIS_DEFAULTS variable. */
441 u_long
442 __nis_default_access (char *param, u_long defaults)
444 u_long result;
445 char *cptr;
447 if (defaults == 0)
448 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
449 else
450 result = defaults;
452 if (param != NULL && strstr (param, "access=") != NULL)
453 result = searchaccess (param, result);
454 else
456 cptr = getenv ("NIS_DEFAULTS");
457 if (cptr != NULL && strstr (cptr, "access=") != NULL)
458 result = searchaccess (getenv ("NIS_DEFAULTS"), result);
461 return result;