]>
Commit | Line | Data |
---|---|---|
51702635 UD |
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. | |
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 | #include <rpcsvc/nislib.h> | |
27 | ||
28 | #define DEFAULT_TTL 43200 | |
29 | ||
30 | /* | |
31 | ** Some functions for parsing the -D param and NIS_DEFAULTS Environ | |
32 | */ | |
33 | static nis_name | |
34 | searchgroup (char *str) | |
35 | { | |
36 | static char default_group[NIS_MAXNAMELEN]; | |
37 | char *cptr; | |
38 | int i; | |
39 | ||
40 | cptr = strstr (str, "group="); | |
41 | if (cptr == NULL) | |
42 | return NULL; | |
43 | ||
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)""; | |
50 | ||
51 | strncpy (default_group, cptr, i); | |
52 | ||
53 | return default_group; | |
54 | } | |
55 | ||
56 | static nis_name | |
57 | searchowner (char *str) | |
58 | { | |
59 | static char default_owner[NIS_MAXNAMELEN]; | |
60 | char *cptr; | |
61 | int i; | |
62 | ||
63 | cptr = strstr (str, "owner="); | |
64 | if (cptr == NULL) | |
65 | return NULL; | |
66 | ||
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)""; | |
73 | ||
74 | strncpy (default_owner, cptr, i); | |
75 | ||
76 | return default_owner; | |
77 | } | |
78 | ||
79 | static u_long | |
80 | searchttl (char *str) | |
81 | { | |
82 | char buf[1024]; | |
83 | char *cptr, *dptr; | |
84 | u_long time; | |
85 | int i; | |
86 | ||
87 | dptr = strstr (str, "ttl="); | |
88 | if (dptr == NULL) /* should (could) not happen */ | |
89 | return DEFAULT_TTL;; | |
90 | ||
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; | |
97 | ||
98 | strncpy (buf, dptr, i); | |
99 | time = 0; | |
100 | ||
101 | dptr = buf; | |
102 | cptr = strchr (dptr, 'd'); | |
103 | if (cptr != NULL) | |
104 | { | |
105 | *cptr = '\0'; | |
106 | cptr++; | |
107 | time += atoi (dptr) * 60 * 60 * 24; | |
108 | dptr = cptr; | |
109 | } | |
110 | ||
111 | cptr = strchr (dptr, 'h'); | |
112 | if (cptr != NULL) | |
113 | { | |
114 | *cptr = '\0'; | |
115 | cptr++; | |
116 | time += atoi (dptr) * 60 * 60; | |
117 | dptr = cptr; | |
118 | } | |
119 | ||
120 | cptr = strchr (dptr, 'm'); | |
121 | if (cptr != NULL) | |
122 | { | |
123 | *cptr = '\0'; | |
124 | cptr++; | |
125 | time += atoi (dptr) * 60; | |
126 | dptr = cptr; | |
127 | } | |
128 | ||
129 | cptr = strchr (dptr, 's'); | |
130 | if (cptr != NULL) | |
131 | *cptr = '\0'; | |
132 | ||
133 | time += atoi (dptr); | |
134 | ||
135 | return time; | |
136 | } | |
137 | ||
138 | static u_long | |
139 | searchaccess (char *str, u_long access) | |
140 | { | |
141 | static char buf[NIS_MAXNAMELEN]; | |
142 | char *cptr; | |
143 | u_long result; | |
144 | int i; | |
145 | int n, o, g, w; | |
146 | ||
147 | cptr = strstr (str, "access="); | |
148 | if (cptr == NULL) | |
149 | return 0; | |
150 | ||
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; | |
157 | ||
158 | strncpy (buf, cptr, i); | |
159 | ||
160 | result = n = o = g = w = 0; | |
161 | cptr = buf; | |
162 | while (*cptr != '\0') | |
163 | { | |
164 | switch (*cptr) | |
165 | { | |
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 != ',') | |
184 | { | |
185 | switch (*cptr) | |
186 | { | |
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; | |
230 | } | |
231 | cptr++; | |
232 | } | |
233 | break; | |
234 | case '+': | |
235 | cptr++; /* Remove "=" from beginning */ | |
236 | while (*cptr != '\0' && *cptr != ',') | |
237 | { | |
238 | switch (*cptr) | |
239 | { | |
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; | |
283 | } | |
284 | cptr++; | |
285 | } | |
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); | |
293 | ||
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 != ',') | |
304 | { | |
305 | switch (*cptr) | |
306 | { | |
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; | |
350 | } | |
351 | cptr++; | |
352 | } | |
353 | break; | |
354 | default: | |
355 | fprintf (stderr, "Parse error in \"%s\"\n", buf); | |
356 | return 0; | |
357 | } | |
358 | cptr++; | |
359 | } | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | nis_name | |
365 | __nis_default_owner (char *defaults) | |
366 | { | |
367 | static char default_owner[NIS_MAXNAMELEN]; | |
368 | char *cptr, *dptr; | |
369 | ||
370 | strcpy (default_owner, nis_local_principal ()); | |
371 | ||
372 | if (defaults != NULL) | |
373 | { | |
374 | dptr = strstr (defaults, "owner="); | |
375 | if (dptr != NULL) | |
376 | strcpy (default_owner, searchowner (defaults)); | |
377 | } | |
378 | else | |
379 | { | |
380 | cptr = getenv ("NIS_DEFAULTS"); | |
381 | if (cptr != NULL) | |
382 | { | |
383 | dptr = strstr (cptr, "owner="); | |
384 | if (dptr != NULL) | |
385 | strcpy (default_owner, searchowner (cptr)); | |
386 | } | |
387 | } | |
388 | ||
389 | return default_owner; | |
390 | } | |
391 | ||
392 | nis_name | |
393 | __nis_default_group (char *defaults) | |
394 | { | |
395 | static char default_group[NIS_MAXNAMELEN]; | |
396 | char *cptr, *dptr; | |
397 | ||
398 | strcpy (default_group, nis_local_group ()); | |
399 | ||
400 | if (defaults != NULL) | |
401 | { | |
402 | dptr = strstr (defaults, "group="); | |
403 | if (dptr != NULL) | |
404 | strcpy (default_group, searchgroup (defaults)); | |
405 | } | |
406 | else | |
407 | { | |
408 | cptr = getenv ("NIS_DEFAULTS"); | |
409 | if (cptr != NULL) | |
410 | { | |
411 | dptr = strstr (cptr, "group="); | |
412 | if (dptr != NULL) | |
413 | strcpy (default_group, searchgroup (cptr)); | |
414 | } | |
415 | } | |
416 | ||
417 | return default_group; | |
418 | } | |
419 | ||
420 | u_long | |
421 | __nis_default_ttl (char *defaults) | |
422 | { | |
423 | char *cptr, *dptr; | |
424 | ||
425 | if (defaults != NULL) | |
426 | { | |
427 | dptr = strstr (defaults, "ttl="); | |
428 | if (dptr != NULL) | |
429 | return searchttl (defaults); | |
430 | } | |
431 | ||
432 | cptr = getenv ("NIS_DEFAULTS"); | |
433 | if (cptr == NULL) | |
434 | return DEFAULT_TTL; | |
435 | ||
436 | dptr = strstr (cptr, "ttl="); | |
437 | if (dptr == NULL) | |
438 | return DEFAULT_TTL; | |
439 | ||
440 | return searchttl (cptr); | |
441 | } | |
442 | ||
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) | |
447 | { | |
448 | u_long result; | |
449 | char *cptr; | |
450 | ||
451 | if (defaults == 0) | |
452 | result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT; | |
453 | else | |
454 | result = defaults; | |
455 | ||
456 | if (param != NULL && strstr (param, "access=") != NULL) | |
457 | result = searchaccess (param, result); | |
458 | else | |
459 | { | |
460 | cptr = getenv ("NIS_DEFAULTS"); | |
461 | if (cptr != NULL && strstr (cptr, "access=") != NULL) | |
462 | result = searchaccess (getenv ("NIS_DEFAULTS"), result); | |
463 | } | |
464 | ||
465 | return result; | |
466 | } |