]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_defaults.c
Update.
[thirdparty/glibc.git] / nis / nis_defaults.c
1 /* Copyright (c) 1997, 1998 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.
4
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.
9
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.
14
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. */
19
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
27 #define DEFAULT_TTL 43200
28
29 /*
30 ** Some functions for parsing the -D param and NIS_DEFAULTS Environ
31 */
32 static nis_name
33 searchgroup (char *str)
34 {
35 char *cptr;
36 int i;
37
38 cptr = strstr (str, "group=");
39 if (cptr == NULL)
40 return NULL;
41
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) "";
48
49 return strndup (cptr, i);
50 }
51
52 static nis_name
53 searchowner (char *str)
54 {
55 char *cptr;
56 int i;
57
58 cptr = strstr (str, "owner=");
59 if (cptr == NULL)
60 return NULL;
61
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 ("");
68
69 return strndup (cptr, i);
70 }
71
72 static uint32_t
73 searchttl (char *str)
74 {
75 char buf[strlen (str) + 1];
76 char *cptr, *dptr;
77 uint32_t time;
78 int i;
79
80 dptr = strstr (str, "ttl=");
81 if (dptr == NULL) /* should (could) not happen */
82 return DEFAULT_TTL;;
83
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;
90
91 strncpy (buf, dptr, i);
92 buf[i] = '\0';
93 time = 0;
94
95 dptr = buf;
96 cptr = strchr (dptr, 'd');
97 if (cptr != NULL)
98 {
99 *cptr = '\0';
100 cptr++;
101 time += atoi (dptr) * 60 * 60 * 24;
102 dptr = cptr;
103 }
104
105 cptr = strchr (dptr, 'h');
106 if (cptr != NULL)
107 {
108 *cptr = '\0';
109 cptr++;
110 time += atoi (dptr) * 60 * 60;
111 dptr = cptr;
112 }
113
114 cptr = strchr (dptr, 'm');
115 if (cptr != NULL)
116 {
117 *cptr = '\0';
118 cptr++;
119 time += atoi (dptr) * 60;
120 dptr = cptr;
121 }
122
123 cptr = strchr (dptr, 's');
124 if (cptr != NULL)
125 *cptr = '\0';
126
127 time += atoi (dptr);
128
129 return time;
130 }
131
132 static unsigned int
133 searchaccess (char *str, unsigned int access)
134 {
135 char buf[strlen (str) + 1];
136 char *cptr;
137 unsigned int result = access;
138 int i;
139 int n, o, g, w;
140
141 cptr = strstr (str, "access=");
142 if (cptr == NULL)
143 return 0;
144
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;
151
152 strncpy (buf, cptr, i);
153 buf[i] = '\0';
154
155 n = o = g = w = 0;
156 cptr = buf;
157 if (*cptr == ',') /* Fix for stupid Solaris scripts */
158 ++cptr;
159 while (*cptr != '\0')
160 {
161 switch (*cptr)
162 {
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 != ',')
181 {
182 switch (*cptr)
183 {
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);
226 }
227 cptr++;
228 }
229 n = o = g = w = 0;
230 break;
231 case '+':
232 cptr++; /* Remove "+" from beginning */
233 while (*cptr != '\0' && *cptr != ',')
234 {
235 switch (*cptr)
236 {
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);
279 }
280 cptr++;
281 }
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);
290
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 != ',')
301 {
302 switch (*cptr)
303 {
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);
346 }
347 cptr++;
348 }
349 n = o = g = w = 0;
350 break;
351 default:
352 return result = (~0U);
353 }
354 if (*cptr != '\0')
355 cptr++;
356 }
357
358 return result;
359 }
360
361 nis_name
362 __nis_default_owner (char *defaults)
363 {
364 char default_owner[NIS_MAXNAMELEN + 1];
365 char *cptr, *dptr;
366
367 strcpy (default_owner, nis_local_principal ());
368
369 if (defaults != NULL)
370 {
371 dptr = strstr (defaults, "owner=");
372 if (dptr != NULL)
373 {
374 char *p = searchowner (defaults);
375 if (strlen (p) <= NIS_MAXNAMELEN)
376 strcpy (default_owner, p);
377 free (p);
378 }
379 }
380 else
381 {
382 cptr = getenv ("NIS_DEFAULTS");
383 if (cptr != NULL)
384 {
385 dptr = strstr (cptr, "owner=");
386 if (dptr != NULL)
387 {
388 char *p = searchowner (cptr);
389 if (strlen (p) <= NIS_MAXNAMELEN)
390 strcpy (default_owner, p);
391 free (p);
392 }
393 }
394 }
395
396 return strdup (default_owner);
397 }
398
399 nis_name
400 __nis_default_group (char *defaults)
401 {
402 char default_group[NIS_MAXNAMELEN + 1];
403 char *cptr, *dptr;
404
405 strcpy (default_group, nis_local_group ());
406
407 if (defaults != NULL)
408 {
409 dptr = strstr (defaults, "group=");
410 if (dptr != NULL)
411 {
412 char *p = searchgroup (defaults);
413
414 if (strlen (p) <= NIS_MAXNAMELEN)
415 strcpy (default_group, p);
416 free (p);
417 }
418 }
419 else
420 {
421 cptr = getenv ("NIS_DEFAULTS");
422 if (cptr != NULL)
423 {
424 dptr = strstr (cptr, "group=");
425 if (dptr != NULL)
426 {
427 char *p = searchgroup (cptr);
428
429 if (strlen (p) <= NIS_MAXNAMELEN)
430 strcpy (default_group, p);
431 free (p);
432 }
433 }
434 }
435
436 return strdup (default_group);
437 }
438
439 uint32_t
440 __nis_default_ttl (char *defaults)
441 {
442 char *cptr, *dptr;
443
444 if (defaults != NULL)
445 {
446 dptr = strstr (defaults, "ttl=");
447 if (dptr != NULL)
448 return searchttl (defaults);
449 }
450
451 cptr = getenv ("NIS_DEFAULTS");
452 if (cptr == NULL)
453 return DEFAULT_TTL;
454
455 dptr = strstr (cptr, "ttl=");
456 if (dptr == NULL)
457 return DEFAULT_TTL;
458
459 return searchttl (cptr);
460 }
461
462 /* Default access rights are ----rmcdr---r---, but we could change
463 this with the NIS_DEFAULTS variable. */
464 unsigned int
465 __nis_default_access (char *param, unsigned int defaults)
466 {
467 unsigned int result;
468 char *cptr;
469
470 if (defaults == 0)
471 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
472 else
473 result = defaults;
474
475 if (param != NULL && strstr (param, "access=") != NULL)
476 result = searchaccess (param, result);
477 else
478 {
479 cptr = getenv ("NIS_DEFAULTS");
480 if (cptr != NULL && strstr (cptr, "access=") != NULL)
481 result = searchaccess (getenv ("NIS_DEFAULTS"), result);
482 }
483
484 return result;
485 }