Update.
[glibc.git] / nis / nis_defaults.c
blob9d152d1f11f771073fc004338ba89996b5b01351
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 strncpy (default_group, cptr, i);
53 return default_group;
56 static nis_name
57 searchowner (char *str)
59 static char default_owner[NIS_MAXNAMELEN];
60 char *cptr;
61 int i;
63 cptr = strstr (str, "owner=");
64 if (cptr == NULL)
65 return NULL;
67 cptr += 6; /* points to the begin of the owner string */
68 i = 0;
69 while (cptr[i] != '\0' && cptr[i] != ':')
70 i++;
71 if (i == 0) /* only "owner=" ? */
72 return (nis_name)"";
74 strncpy (default_owner, cptr, i);
76 return default_owner;
79 static u_long
80 searchttl (char *str)
82 char buf[1024];
83 char *cptr, *dptr;
84 u_long time;
85 int i;
87 dptr = strstr (str, "ttl=");
88 if (dptr == NULL) /* should (could) not happen */
89 return DEFAULT_TTL;;
91 dptr += 4; /* points to the begin of the new ttl */
92 i = 0;
93 while (dptr[i] != '\0' && dptr[i] != ':')
94 i++;
95 if (i == 0) /* only "ttl=" ? */
96 return DEFAULT_TTL;
98 strncpy (buf, dptr, i);
99 time = 0;
101 dptr = buf;
102 cptr = strchr (dptr, 'd');
103 if (cptr != NULL)
105 *cptr = '\0';
106 cptr++;
107 time += atoi (dptr) * 60 * 60 * 24;
108 dptr = cptr;
111 cptr = strchr (dptr, 'h');
112 if (cptr != NULL)
114 *cptr = '\0';
115 cptr++;
116 time += atoi (dptr) * 60 * 60;
117 dptr = cptr;
120 cptr = strchr (dptr, 'm');
121 if (cptr != NULL)
123 *cptr = '\0';
124 cptr++;
125 time += atoi (dptr) * 60;
126 dptr = cptr;
129 cptr = strchr (dptr, 's');
130 if (cptr != NULL)
131 *cptr = '\0';
133 time += atoi (dptr);
135 return time;
138 static u_long
139 searchaccess (char *str, u_long access)
141 static char buf[NIS_MAXNAMELEN];
142 char *cptr;
143 u_long result;
144 int i;
145 int n, o, g, w;
147 cptr = strstr (str, "access=");
148 if (cptr == NULL)
149 return 0;
151 cptr += 7; /* points to the begin of the access string */
152 i = 0;
153 while (cptr[i] != '\0' && cptr[i] != ':')
154 i++;
155 if (i == 0) /* only "access=" ? */
156 return 0;
158 strncpy (buf, cptr, i);
160 result = n = o = g = w = 0;
161 cptr = buf;
162 while (*cptr != '\0')
164 switch (*cptr)
166 case 'n':
167 n = 1;
168 break;
169 case 'o':
170 o = 1;
171 break;
172 case 'g':
173 g = 1;
174 break;
175 case 'w':
176 w = 1;
177 break;
178 case 'a':
179 o = g = w = 1;
180 break;
181 case '-':
182 cptr++; /* Remove "=" from beginning */
183 while (*cptr != '\0' && *cptr != ',')
185 switch (*cptr)
187 case 'r':
188 if (n)
189 result = result & ~(NIS_READ_ACC << 24);
190 if (o)
191 result = result & ~(NIS_READ_ACC << 16);
192 if (g)
193 result = result & ~(NIS_READ_ACC << 8);
194 if (w)
195 result = result & ~(NIS_READ_ACC);
196 break;
197 case 'm':
198 if (n)
199 result = result & ~(NIS_MODIFY_ACC << 24);
200 if (o)
201 result = result & ~(NIS_MODIFY_ACC << 16);
202 if (g)
203 result = result & ~(NIS_MODIFY_ACC << 8);
204 if (w)
205 result = result & ~(NIS_MODIFY_ACC);
206 break;
207 case 'c':
208 if (n)
209 result = result & ~(NIS_CREATE_ACC << 24);
210 if (o)
211 result = result & ~(NIS_CREATE_ACC << 16);
212 if (g)
213 result = result & ~(NIS_CREATE_ACC << 8);
214 if (w)
215 result = result & ~(NIS_CREATE_ACC);
216 break;
217 case 'd':
218 if (n)
219 result = result & ~(NIS_DESTROY_ACC << 24);
220 if (o)
221 result = result & ~(NIS_DESTROY_ACC << 16);
222 if (g)
223 result = result & ~(NIS_DESTROY_ACC << 8);
224 if (w)
225 result = result & ~(NIS_DESTROY_ACC);
226 break;
227 default:
228 fprintf (stderr, "Parse error in \"%s\"\n", buf);
229 return 0;
231 cptr++;
233 break;
234 case '+':
235 cptr++; /* Remove "=" from beginning */
236 while (*cptr != '\0' && *cptr != ',')
238 switch (*cptr)
240 case 'r':
241 if (n)
242 result = result | (NIS_READ_ACC << 24);
243 if (o)
244 result = result | (NIS_READ_ACC << 16);
245 if (g)
246 result = result | (NIS_READ_ACC << 8);
247 if (w)
248 result = result | (NIS_READ_ACC);
249 break;
250 case 'm':
251 if (n)
252 result = result | (NIS_MODIFY_ACC << 24);
253 if (o)
254 result = result | (NIS_MODIFY_ACC << 16);
255 if (g)
256 result = result | (NIS_MODIFY_ACC << 8);
257 if (w)
258 result = result | (NIS_MODIFY_ACC);
259 break;
260 case 'c':
261 if (n)
262 result = result | (NIS_CREATE_ACC << 24);
263 if (o)
264 result = result | (NIS_CREATE_ACC << 16);
265 if (g)
266 result = result | (NIS_CREATE_ACC << 8);
267 if (w)
268 result = result | (NIS_CREATE_ACC);
269 break;
270 case 'd':
271 if (n)
272 result = result | (NIS_DESTROY_ACC << 24);
273 if (o)
274 result = result | (NIS_DESTROY_ACC << 16);
275 if (g)
276 result = result | (NIS_DESTROY_ACC << 8);
277 if (w)
278 result = result | (NIS_DESTROY_ACC);
279 break;
280 default:
281 fprintf (stderr, "Parse error in \"%s\"\n", buf);
282 return 0;
284 cptr++;
286 break;
287 case '=':
288 cptr++; /* Remove "=" from beginning */
289 /* Clear */
290 if (n)
291 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
292 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
294 if (o)
295 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
296 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
297 if (g)
298 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
299 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
300 if (w)
301 result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
302 NIS_CREATE_ACC + NIS_DESTROY_ACC);
303 while (*cptr != '\0' && *cptr != ',')
305 switch (*cptr)
307 case 'r':
308 if (n)
309 result = result | (NIS_READ_ACC << 24);
310 if (o)
311 result = result | (NIS_READ_ACC << 16);
312 if (g)
313 result = result | (NIS_READ_ACC << 8);
314 if (w)
315 result = result | (NIS_READ_ACC);
316 break;
317 case 'm':
318 if (n)
319 result = result | (NIS_MODIFY_ACC << 24);
320 if (o)
321 result = result | (NIS_MODIFY_ACC << 16);
322 if (g)
323 result = result | (NIS_MODIFY_ACC << 8);
324 if (w)
325 result = result | (NIS_MODIFY_ACC);
326 break;
327 case 'c':
328 if (n)
329 result = result | (NIS_CREATE_ACC << 24);
330 if (o)
331 result = result | (NIS_CREATE_ACC << 16);
332 if (g)
333 result = result | (NIS_CREATE_ACC << 8);
334 if (w)
335 result = result | (NIS_CREATE_ACC);
336 break;
337 case 'd':
338 if (n)
339 result = result | (NIS_DESTROY_ACC << 24);
340 if (o)
341 result = result | (NIS_DESTROY_ACC << 16);
342 if (g)
343 result = result | (NIS_DESTROY_ACC << 8);
344 if (w)
345 result = result | (NIS_DESTROY_ACC);
346 break;
347 default:
348 fprintf (stderr, "Parse error in \"%s\"\n", buf);
349 return 0;
351 cptr++;
353 break;
354 default:
355 fprintf (stderr, "Parse error in \"%s\"\n", buf);
356 return 0;
358 cptr++;
361 return 0;
364 nis_name
365 __nis_default_owner (char *defaults)
367 static char default_owner[NIS_MAXNAMELEN];
368 char *cptr, *dptr;
370 strcpy (default_owner, nis_local_principal ());
372 if (defaults != NULL)
374 dptr = strstr (defaults, "owner=");
375 if (dptr != NULL)
376 strcpy (default_owner, searchowner (defaults));
378 else
380 cptr = getenv ("NIS_DEFAULTS");
381 if (cptr != NULL)
383 dptr = strstr (cptr, "owner=");
384 if (dptr != NULL)
385 strcpy (default_owner, searchowner (cptr));
389 return default_owner;
392 nis_name
393 __nis_default_group (char *defaults)
395 static 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)
404 strcpy (default_group, searchgroup (defaults));
406 else
408 cptr = getenv ("NIS_DEFAULTS");
409 if (cptr != NULL)
411 dptr = strstr (cptr, "group=");
412 if (dptr != NULL)
413 strcpy (default_group, searchgroup (cptr));
417 return default_group;
420 u_long
421 __nis_default_ttl (char *defaults)
423 char *cptr, *dptr;
425 if (defaults != NULL)
427 dptr = strstr (defaults, "ttl=");
428 if (dptr != NULL)
429 return searchttl (defaults);
432 cptr = getenv ("NIS_DEFAULTS");
433 if (cptr == NULL)
434 return DEFAULT_TTL;
436 dptr = strstr (cptr, "ttl=");
437 if (dptr == NULL)
438 return DEFAULT_TTL;
440 return searchttl (cptr);
443 /* Default access rights are ----rmcdr---r---, but we could change
444 this with the NIS_DEFAULTS variable. */
445 u_long
446 __nis_default_access (char *param, u_long defaults)
448 u_long result;
449 char *cptr;
451 if (defaults == 0)
452 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
453 else
454 result = defaults;
456 if (param != NULL && strstr (param, "access=") != NULL)
457 result = searchaccess (param, result);
458 else
460 cptr = getenv ("NIS_DEFAULTS");
461 if (cptr != NULL && strstr (cptr, "access=") != NULL)
462 result = searchaccess (getenv ("NIS_DEFAULTS"), result);
465 return result;