[BZ #1978]
[glibc.git] / nis / nis_defaults.c
blobf13578635ab567c0cf9057d6c6328b091ba9b070
1 /* Copyright (c) 1997, 1998, 2004 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 uint32_t
73 searchttl (char *str)
75 char buf[strlen (str) + 1];
76 char *cptr, *dptr;
77 uint32_t 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 buf[i] = '\0';
93 time = 0;
95 dptr = buf;
96 cptr = strchr (dptr, 'd');
97 if (cptr != NULL)
99 *cptr = '\0';
100 cptr++;
101 time += atoi (dptr) * 60 * 60 * 24;
102 dptr = cptr;
105 cptr = strchr (dptr, 'h');
106 if (cptr != NULL)
108 *cptr = '\0';
109 cptr++;
110 time += atoi (dptr) * 60 * 60;
111 dptr = cptr;
114 cptr = strchr (dptr, 'm');
115 if (cptr != NULL)
117 *cptr = '\0';
118 cptr++;
119 time += atoi (dptr) * 60;
120 dptr = cptr;
123 cptr = strchr (dptr, 's');
124 if (cptr != NULL)
125 *cptr = '\0';
127 time += atoi (dptr);
129 return time;
132 static unsigned int
133 searchaccess (char *str, unsigned int access)
135 char buf[strlen (str) + 1];
136 char *cptr;
137 unsigned int result = access;
138 int i;
139 int n, o, g, w;
141 cptr = strstr (str, "access=");
142 if (cptr == NULL)
143 return 0;
145 cptr += 7; /* points to the begin of the access string */
146 i = 0;
147 while (cptr[i] != '\0' && cptr[i] != ':')
148 i++;
149 if (i == 0) /* only "access=" ? */
150 return 0;
152 strncpy (buf, cptr, i);
153 buf[i] = '\0';
155 n = o = g = w = 0;
156 cptr = buf;
157 if (*cptr == ',') /* Fix for stupid Solaris scripts */
158 ++cptr;
159 while (*cptr != '\0')
161 switch (*cptr)
163 case 'n':
164 n = 1;
165 break;
166 case 'o':
167 o = 1;
168 break;
169 case 'g':
170 g = 1;
171 break;
172 case 'w':
173 w = 1;
174 break;
175 case 'a':
176 o = g = w = 1;
177 break;
178 case '-':
179 cptr++; /* Remove "-" from beginning */
180 while (*cptr != '\0' && *cptr != ',')
182 switch (*cptr)
184 case 'r':
185 if (n)
186 result = result & ~(NIS_READ_ACC << 24);
187 if (o)
188 result = result & ~(NIS_READ_ACC << 16);
189 if (g)
190 result = result & ~(NIS_READ_ACC << 8);
191 if (w)
192 result = result & ~(NIS_READ_ACC);
193 break;
194 case 'm':
195 if (n)
196 result = result & ~(NIS_MODIFY_ACC << 24);
197 if (o)
198 result = result & ~(NIS_MODIFY_ACC << 16);
199 if (g)
200 result = result & ~(NIS_MODIFY_ACC << 8);
201 if (w)
202 result = result & ~(NIS_MODIFY_ACC);
203 break;
204 case 'c':
205 if (n)
206 result = result & ~(NIS_CREATE_ACC << 24);
207 if (o)
208 result = result & ~(NIS_CREATE_ACC << 16);
209 if (g)
210 result = result & ~(NIS_CREATE_ACC << 8);
211 if (w)
212 result = result & ~(NIS_CREATE_ACC);
213 break;
214 case 'd':
215 if (n)
216 result = result & ~(NIS_DESTROY_ACC << 24);
217 if (o)
218 result = result & ~(NIS_DESTROY_ACC << 16);
219 if (g)
220 result = result & ~(NIS_DESTROY_ACC << 8);
221 if (w)
222 result = result & ~(NIS_DESTROY_ACC);
223 break;
224 default:
225 return (~0U);
227 cptr++;
229 n = o = g = w = 0;
230 break;
231 case '+':
232 cptr++; /* Remove "+" from beginning */
233 while (*cptr != '\0' && *cptr != ',')
235 switch (*cptr)
237 case 'r':
238 if (n)
239 result = result | (NIS_READ_ACC << 24);
240 if (o)
241 result = result | (NIS_READ_ACC << 16);
242 if (g)
243 result = result | (NIS_READ_ACC << 8);
244 if (w)
245 result = result | (NIS_READ_ACC);
246 break;
247 case 'm':
248 if (n)
249 result = result | (NIS_MODIFY_ACC << 24);
250 if (o)
251 result = result | (NIS_MODIFY_ACC << 16);
252 if (g)
253 result = result | (NIS_MODIFY_ACC << 8);
254 if (w)
255 result = result | (NIS_MODIFY_ACC);
256 break;
257 case 'c':
258 if (n)
259 result = result | (NIS_CREATE_ACC << 24);
260 if (o)
261 result = result | (NIS_CREATE_ACC << 16);
262 if (g)
263 result = result | (NIS_CREATE_ACC << 8);
264 if (w)
265 result = result | (NIS_CREATE_ACC);
266 break;
267 case 'd':
268 if (n)
269 result = result | (NIS_DESTROY_ACC << 24);
270 if (o)
271 result = result | (NIS_DESTROY_ACC << 16);
272 if (g)
273 result = result | (NIS_DESTROY_ACC << 8);
274 if (w)
275 result = result | (NIS_DESTROY_ACC);
276 break;
277 default:
278 return (~0U);
280 cptr++;
282 n = o = g = w = 0;
283 break;
284 case '=':
285 cptr++; /* Remove "=" from beginning */
286 /* Clear */
287 if (n)
288 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
289 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
291 if (o)
292 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
293 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
294 if (g)
295 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
296 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
297 if (w)
298 result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
299 NIS_CREATE_ACC + NIS_DESTROY_ACC);
300 while (*cptr != '\0' && *cptr != ',')
302 switch (*cptr)
304 case 'r':
305 if (n)
306 result = result | (NIS_READ_ACC << 24);
307 if (o)
308 result = result | (NIS_READ_ACC << 16);
309 if (g)
310 result = result | (NIS_READ_ACC << 8);
311 if (w)
312 result = result | (NIS_READ_ACC);
313 break;
314 case 'm':
315 if (n)
316 result = result | (NIS_MODIFY_ACC << 24);
317 if (o)
318 result = result | (NIS_MODIFY_ACC << 16);
319 if (g)
320 result = result | (NIS_MODIFY_ACC << 8);
321 if (w)
322 result = result | (NIS_MODIFY_ACC);
323 break;
324 case 'c':
325 if (n)
326 result = result | (NIS_CREATE_ACC << 24);
327 if (o)
328 result = result | (NIS_CREATE_ACC << 16);
329 if (g)
330 result = result | (NIS_CREATE_ACC << 8);
331 if (w)
332 result = result | (NIS_CREATE_ACC);
333 break;
334 case 'd':
335 if (n)
336 result = result | (NIS_DESTROY_ACC << 24);
337 if (o)
338 result = result | (NIS_DESTROY_ACC << 16);
339 if (g)
340 result = result | (NIS_DESTROY_ACC << 8);
341 if (w)
342 result = result | (NIS_DESTROY_ACC);
343 break;
344 default:
345 return result = (~0U);
347 cptr++;
349 n = o = g = w = 0;
350 break;
351 default:
352 return result = (~0U);
354 if (*cptr != '\0')
355 cptr++;
358 return result;
361 nis_name
362 __nis_default_owner (char *defaults)
364 char default_owner[NIS_MAXNAMELEN + 1];
365 char *cptr, *dptr;
367 strcpy (default_owner, nis_local_principal ());
369 if (defaults != NULL)
371 dptr = strstr (defaults, "owner=");
372 if (dptr != NULL)
374 char *p = searchowner (defaults);
375 if (strlen (p) <= NIS_MAXNAMELEN)
376 strcpy (default_owner, p);
377 free (p);
380 else
382 cptr = getenv ("NIS_DEFAULTS");
383 if (cptr != NULL)
385 dptr = strstr (cptr, "owner=");
386 if (dptr != NULL)
388 char *p = searchowner (cptr);
389 if (strlen (p) <= NIS_MAXNAMELEN)
390 strcpy (default_owner, p);
391 free (p);
396 return strdup (default_owner);
398 libnsl_hidden_def (__nis_default_owner)
400 nis_name
401 __nis_default_group (char *defaults)
403 char default_group[NIS_MAXNAMELEN + 1];
404 char *cptr, *dptr;
406 strcpy (default_group, nis_local_group ());
408 if (defaults != NULL)
410 dptr = strstr (defaults, "group=");
411 if (dptr != NULL)
413 char *p = searchgroup (defaults);
415 if (strlen (p) <= NIS_MAXNAMELEN)
416 strcpy (default_group, p);
417 free (p);
420 else
422 cptr = getenv ("NIS_DEFAULTS");
423 if (cptr != NULL)
425 dptr = strstr (cptr, "group=");
426 if (dptr != NULL)
428 char *p = searchgroup (cptr);
430 if (strlen (p) <= NIS_MAXNAMELEN)
431 strcpy (default_group, p);
432 free (p);
437 return strdup (default_group);
439 libnsl_hidden_def (__nis_default_group)
441 uint32_t
442 __nis_default_ttl (char *defaults)
444 char *cptr, *dptr;
446 if (defaults != NULL)
448 dptr = strstr (defaults, "ttl=");
449 if (dptr != NULL)
450 return searchttl (defaults);
453 cptr = getenv ("NIS_DEFAULTS");
454 if (cptr == NULL)
455 return DEFAULT_TTL;
457 dptr = strstr (cptr, "ttl=");
458 if (dptr == NULL)
459 return DEFAULT_TTL;
461 return searchttl (cptr);
464 /* Default access rights are ----rmcdr---r---, but we could change
465 this with the NIS_DEFAULTS variable. */
466 unsigned int
467 __nis_default_access (char *param, unsigned int defaults)
469 unsigned int result;
470 char *cptr;
472 if (defaults == 0)
473 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
474 else
475 result = defaults;
477 if (param != NULL && strstr (param, "access=") != NULL)
478 result = searchaccess (param, result);
479 else
481 cptr = getenv ("NIS_DEFAULTS");
482 if (cptr != NULL && strstr (cptr, "access=") != NULL)
483 result = searchaccess (getenv ("NIS_DEFAULTS"), result);
486 return result;
488 libnsl_hidden_def (__nis_default_access)