Update.
[glibc.git] / nis / nis_defaults.c
blob21a80506ffedb6d4c60de1c69e81b3ac543f202a
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>
27 #define DEFAULT_TTL 43200
30 ** Some functions for parsing the -D param and NIS_DEFAULTS Environ
32 static nis_name
33 searchgroup (char *str)
35 char *cptr;
36 int i;
38 cptr = strstr (str, "group=");
39 if (cptr == NULL)
40 return NULL;
42 cptr += 6; /* points to the begin of the group string */
43 i = 0;
44 while (cptr[i] != '\0' && cptr[i] != ':')
45 i++;
46 if (i == 0) /* only "group=" ? */
47 return (nis_name) "";
49 return strndup (cptr, i);
52 static nis_name
53 searchowner (char *str)
55 char *cptr;
56 int i;
58 cptr = strstr (str, "owner=");
59 if (cptr == NULL)
60 return NULL;
62 cptr += 6; /* points to the begin of the owner string */
63 i = 0;
64 while (cptr[i] != '\0' && cptr[i] != ':')
65 i++;
66 if (i == 0) /* only "owner=" ? */
67 return strdup ("");
69 return strndup (cptr, i);
72 static u_long
73 searchttl (char *str)
75 char buf[1024];
76 char *cptr, *dptr;
77 u_long time;
78 int i;
80 dptr = strstr (str, "ttl=");
81 if (dptr == NULL) /* should (could) not happen */
82 return DEFAULT_TTL;;
84 dptr += 4; /* points to the begin of the new ttl */
85 i = 0;
86 while (dptr[i] != '\0' && dptr[i] != ':')
87 i++;
88 if (i == 0) /* only "ttl=" ? */
89 return DEFAULT_TTL;
91 strncpy (buf, dptr, i);
92 time = 0;
94 dptr = buf;
95 cptr = strchr (dptr, 'd');
96 if (cptr != NULL)
98 *cptr = '\0';
99 cptr++;
100 time += atoi (dptr) * 60 * 60 * 24;
101 dptr = cptr;
104 cptr = strchr (dptr, 'h');
105 if (cptr != NULL)
107 *cptr = '\0';
108 cptr++;
109 time += atoi (dptr) * 60 * 60;
110 dptr = cptr;
113 cptr = strchr (dptr, 'm');
114 if (cptr != NULL)
116 *cptr = '\0';
117 cptr++;
118 time += atoi (dptr) * 60;
119 dptr = cptr;
122 cptr = strchr (dptr, 's');
123 if (cptr != NULL)
124 *cptr = '\0';
126 time += atoi (dptr);
128 return time;
131 static u_long
132 searchaccess (char *str, u_long access)
134 char buf[NIS_MAXNAMELEN];
135 char *cptr;
136 u_long result = access;
137 int i;
138 int n, o, g, w;
140 cptr = strstr (str, "access=");
141 if (cptr == NULL)
142 return 0;
144 cptr += 7; /* points to the begin of the access string */
145 i = 0;
146 while (cptr[i] != '\0' && cptr[i] != ':')
147 i++;
148 if (i == 0) /* only "access=" ? */
149 return 0;
151 strncpy (buf, cptr, i);
153 n = o = g = w = 0;
154 cptr = buf;
155 while (*cptr != '\0')
157 switch (*cptr)
159 case 'n':
160 n = 1;
161 break;
162 case 'o':
163 o = 1;
164 break;
165 case 'g':
166 g = 1;
167 break;
168 case 'w':
169 w = 1;
170 break;
171 case 'a':
172 o = g = w = 1;
173 break;
174 case '-':
175 cptr++; /* Remove "=" from beginning */
176 while (*cptr != '\0' && *cptr != ',')
178 switch (*cptr)
180 case 'r':
181 if (n)
182 result = result & ~(NIS_READ_ACC << 24);
183 if (o)
184 result = result & ~(NIS_READ_ACC << 16);
185 if (g)
186 result = result & ~(NIS_READ_ACC << 8);
187 if (w)
188 result = result & ~(NIS_READ_ACC);
189 break;
190 case 'm':
191 if (n)
192 result = result & ~(NIS_MODIFY_ACC << 24);
193 if (o)
194 result = result & ~(NIS_MODIFY_ACC << 16);
195 if (g)
196 result = result & ~(NIS_MODIFY_ACC << 8);
197 if (w)
198 result = result & ~(NIS_MODIFY_ACC);
199 break;
200 case 'c':
201 if (n)
202 result = result & ~(NIS_CREATE_ACC << 24);
203 if (o)
204 result = result & ~(NIS_CREATE_ACC << 16);
205 if (g)
206 result = result & ~(NIS_CREATE_ACC << 8);
207 if (w)
208 result = result & ~(NIS_CREATE_ACC);
209 break;
210 case 'd':
211 if (n)
212 result = result & ~(NIS_DESTROY_ACC << 24);
213 if (o)
214 result = result & ~(NIS_DESTROY_ACC << 16);
215 if (g)
216 result = result & ~(NIS_DESTROY_ACC << 8);
217 if (w)
218 result = result & ~(NIS_DESTROY_ACC);
219 break;
220 default:
221 return ULONG_MAX;
223 cptr++;
225 n = o = g = w = 0;
226 break;
227 case '+':
228 cptr++; /* Remove "=" from beginning */
229 while (*cptr != '\0' && *cptr != ',')
231 switch (*cptr)
233 case 'r':
234 if (n)
235 result = result | (NIS_READ_ACC << 24);
236 if (o)
237 result = result | (NIS_READ_ACC << 16);
238 if (g)
239 result = result | (NIS_READ_ACC << 8);
240 if (w)
241 result = result | (NIS_READ_ACC);
242 break;
243 case 'm':
244 if (n)
245 result = result | (NIS_MODIFY_ACC << 24);
246 if (o)
247 result = result | (NIS_MODIFY_ACC << 16);
248 if (g)
249 result = result | (NIS_MODIFY_ACC << 8);
250 if (w)
251 result = result | (NIS_MODIFY_ACC);
252 break;
253 case 'c':
254 if (n)
255 result = result | (NIS_CREATE_ACC << 24);
256 if (o)
257 result = result | (NIS_CREATE_ACC << 16);
258 if (g)
259 result = result | (NIS_CREATE_ACC << 8);
260 if (w)
261 result = result | (NIS_CREATE_ACC);
262 break;
263 case 'd':
264 if (n)
265 result = result | (NIS_DESTROY_ACC << 24);
266 if (o)
267 result = result | (NIS_DESTROY_ACC << 16);
268 if (g)
269 result = result | (NIS_DESTROY_ACC << 8);
270 if (w)
271 result = result | (NIS_DESTROY_ACC);
272 break;
273 default:
274 return ULONG_MAX;
276 cptr++;
278 n = o = g = w = 0;
279 break;
280 case '=':
281 cptr++; /* Remove "=" from beginning */
282 /* Clear */
283 if (n)
284 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
285 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
287 if (o)
288 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
289 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
290 if (g)
291 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
292 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
293 if (w)
294 result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
295 NIS_CREATE_ACC + NIS_DESTROY_ACC);
296 while (*cptr != '\0' && *cptr != ',')
298 switch (*cptr)
300 case 'r':
301 if (n)
302 result = result | (NIS_READ_ACC << 24);
303 if (o)
304 result = result | (NIS_READ_ACC << 16);
305 if (g)
306 result = result | (NIS_READ_ACC << 8);
307 if (w)
308 result = result | (NIS_READ_ACC);
309 break;
310 case 'm':
311 if (n)
312 result = result | (NIS_MODIFY_ACC << 24);
313 if (o)
314 result = result | (NIS_MODIFY_ACC << 16);
315 if (g)
316 result = result | (NIS_MODIFY_ACC << 8);
317 if (w)
318 result = result | (NIS_MODIFY_ACC);
319 break;
320 case 'c':
321 if (n)
322 result = result | (NIS_CREATE_ACC << 24);
323 if (o)
324 result = result | (NIS_CREATE_ACC << 16);
325 if (g)
326 result = result | (NIS_CREATE_ACC << 8);
327 if (w)
328 result = result | (NIS_CREATE_ACC);
329 break;
330 case 'd':
331 if (n)
332 result = result | (NIS_DESTROY_ACC << 24);
333 if (o)
334 result = result | (NIS_DESTROY_ACC << 16);
335 if (g)
336 result = result | (NIS_DESTROY_ACC << 8);
337 if (w)
338 result = result | (NIS_DESTROY_ACC);
339 break;
340 default:
341 return result = ULONG_MAX;
343 cptr++;
345 n = o = g = w = 0;
346 break;
347 default:
348 return result = ULONG_MAX;
350 cptr++;
353 return result;
356 nis_name
357 __nis_default_owner (char *defaults)
359 char default_owner[NIS_MAXNAMELEN];
360 char *cptr, *dptr;
362 strcpy (default_owner, nis_local_principal ());
364 if (defaults != NULL)
366 dptr = strstr (defaults, "owner=");
367 if (dptr != NULL)
369 char *p = searchowner (defaults);
370 strcpy (default_owner, p);
371 free (p);
374 else
376 cptr = getenv ("NIS_DEFAULTS");
377 if (cptr != NULL)
379 dptr = strstr (cptr, "owner=");
380 if (dptr != NULL)
382 char *p = searchowner (cptr);
383 strcpy (default_owner, p);
384 free (p);
389 return strdup (default_owner);
392 nis_name
393 __nis_default_group (char *defaults)
395 char default_group[NIS_MAXNAMELEN];
396 char *cptr, *dptr;
398 strcpy (default_group, nis_local_group ());
400 if (defaults != NULL)
402 dptr = strstr (defaults, "group=");
403 if (dptr != NULL)
405 char *p = searchgroup (defaults);
406 strcpy (default_group, p);
407 free (p);
410 else
412 cptr = getenv ("NIS_DEFAULTS");
413 if (cptr != NULL)
415 dptr = strstr (cptr, "group=");
416 if (dptr != NULL)
418 char *p = searchgroup (cptr);
419 strcpy (default_group, p);
420 free (p);
425 return strdup (default_group);
428 u_long
429 __nis_default_ttl (char *defaults)
431 char *cptr, *dptr;
433 if (defaults != NULL)
435 dptr = strstr (defaults, "ttl=");
436 if (dptr != NULL)
437 return searchttl (defaults);
440 cptr = getenv ("NIS_DEFAULTS");
441 if (cptr == NULL)
442 return DEFAULT_TTL;
444 dptr = strstr (cptr, "ttl=");
445 if (dptr == NULL)
446 return DEFAULT_TTL;
448 return searchttl (cptr);
451 /* Default access rights are ----rmcdr---r---, but we could change
452 this with the NIS_DEFAULTS variable. */
453 u_long
454 __nis_default_access (char *param, u_long defaults)
456 u_long result;
457 char *cptr;
459 if (defaults == 0)
460 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
461 else
462 result = defaults;
464 if (param != NULL && strstr (param, "access=") != NULL)
465 result = searchaccess (param, result);
466 else
468 cptr = getenv ("NIS_DEFAULTS");
469 if (cptr != NULL && strstr (cptr, "access=") != NULL)
470 result = searchaccess (getenv ("NIS_DEFAULTS"), result);
473 return result;