]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (c) 1997-2018 Free Software Foundation, Inc. |
51702635 UD |
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 | |
41bdb6e2 AJ |
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. | |
51702635 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
51702635 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
51702635 | 18 | |
51e59260 | 19 | #include <assert.h> |
51702635 UD |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
e054f494 | 23 | #include <stdint.h> |
51702635 UD |
24 | #include <sys/types.h> |
25 | #include <rpc/rpc.h> | |
26 | #include <rpcsvc/nis.h> | |
82f43dd2 | 27 | #include <shlib-compat.h> |
51702635 UD |
28 | |
29 | #define DEFAULT_TTL 43200 | |
30 | ||
31 | /* | |
32 | ** Some functions for parsing the -D param and NIS_DEFAULTS Environ | |
33 | */ | |
34 | static nis_name | |
51e59260 | 35 | searchXYX (char *str, const char *what) |
51702635 | 36 | { |
51e59260 UD |
37 | assert (strlen (what) == 6); |
38 | assert (strncmp (str, what, 6) == 0); | |
39 | str += 6; /* Points to the begin of the parameters. */ | |
40 | ||
41 | int i = 0; | |
42 | while (str[i] != '\0' && str[i] != ':') | |
43 | ++i; | |
44 | if (i == 0) /* only "<WHAT>=" ? */ | |
45 | return strdup (""); | |
51702635 | 46 | |
51e59260 UD |
47 | return strndup (str, i); |
48 | } | |
51702635 | 49 | |
51702635 | 50 | |
51e59260 UD |
51 | static nis_name |
52 | searchgroup (char *str) | |
53 | { | |
54 | return searchXYX (str, "group="); | |
51702635 UD |
55 | } |
56 | ||
51e59260 | 57 | |
51702635 UD |
58 | static nis_name |
59 | searchowner (char *str) | |
60 | { | |
51e59260 | 61 | return searchXYX (str, "owner="); |
a334319f | 62 | } |
0ecb606c | 63 | |
51e59260 | 64 | |
a1129917 | 65 | static uint32_t |
51702635 UD |
66 | searchttl (char *str) |
67 | { | |
3519948b | 68 | char buf[strlen (str) + 1]; |
51702635 | 69 | char *cptr, *dptr; |
a1129917 | 70 | uint32_t time; |
51702635 UD |
71 | int i; |
72 | ||
73 | dptr = strstr (str, "ttl="); | |
74 | if (dptr == NULL) /* should (could) not happen */ | |
75 | return DEFAULT_TTL;; | |
76 | ||
77 | dptr += 4; /* points to the begin of the new ttl */ | |
78 | i = 0; | |
79 | while (dptr[i] != '\0' && dptr[i] != ':') | |
80 | i++; | |
81 | if (i == 0) /* only "ttl=" ? */ | |
82 | return DEFAULT_TTL; | |
83 | ||
84 | strncpy (buf, dptr, i); | |
3519948b | 85 | buf[i] = '\0'; |
51702635 UD |
86 | time = 0; |
87 | ||
88 | dptr = buf; | |
89 | cptr = strchr (dptr, 'd'); | |
90 | if (cptr != NULL) | |
91 | { | |
92 | *cptr = '\0'; | |
93 | cptr++; | |
94 | time += atoi (dptr) * 60 * 60 * 24; | |
95 | dptr = cptr; | |
96 | } | |
97 | ||
98 | cptr = strchr (dptr, 'h'); | |
99 | if (cptr != NULL) | |
100 | { | |
101 | *cptr = '\0'; | |
102 | cptr++; | |
103 | time += atoi (dptr) * 60 * 60; | |
104 | dptr = cptr; | |
105 | } | |
106 | ||
107 | cptr = strchr (dptr, 'm'); | |
108 | if (cptr != NULL) | |
109 | { | |
110 | *cptr = '\0'; | |
111 | cptr++; | |
112 | time += atoi (dptr) * 60; | |
113 | dptr = cptr; | |
114 | } | |
115 | ||
116 | cptr = strchr (dptr, 's'); | |
117 | if (cptr != NULL) | |
118 | *cptr = '\0'; | |
119 | ||
120 | time += atoi (dptr); | |
121 | ||
122 | return time; | |
123 | } | |
124 | ||
a1129917 UD |
125 | static unsigned int |
126 | searchaccess (char *str, unsigned int access) | |
51702635 | 127 | { |
3519948b | 128 | char buf[strlen (str) + 1]; |
51702635 | 129 | char *cptr; |
a1129917 | 130 | unsigned int result = access; |
51702635 UD |
131 | int i; |
132 | int n, o, g, w; | |
133 | ||
134 | cptr = strstr (str, "access="); | |
135 | if (cptr == NULL) | |
136 | return 0; | |
137 | ||
138 | cptr += 7; /* points to the begin of the access string */ | |
139 | i = 0; | |
140 | while (cptr[i] != '\0' && cptr[i] != ':') | |
141 | i++; | |
142 | if (i == 0) /* only "access=" ? */ | |
143 | return 0; | |
144 | ||
145 | strncpy (buf, cptr, i); | |
3519948b | 146 | buf[i] = '\0'; |
51702635 | 147 | |
dfd2257a | 148 | n = o = g = w = 0; |
51702635 | 149 | cptr = buf; |
3519948b UD |
150 | if (*cptr == ',') /* Fix for stupid Solaris scripts */ |
151 | ++cptr; | |
51702635 UD |
152 | while (*cptr != '\0') |
153 | { | |
154 | switch (*cptr) | |
155 | { | |
156 | case 'n': | |
157 | n = 1; | |
158 | break; | |
159 | case 'o': | |
160 | o = 1; | |
161 | break; | |
162 | case 'g': | |
163 | g = 1; | |
164 | break; | |
165 | case 'w': | |
166 | w = 1; | |
167 | break; | |
168 | case 'a': | |
169 | o = g = w = 1; | |
170 | break; | |
171 | case '-': | |
3519948b | 172 | cptr++; /* Remove "-" from beginning */ |
51702635 UD |
173 | while (*cptr != '\0' && *cptr != ',') |
174 | { | |
175 | switch (*cptr) | |
176 | { | |
177 | case 'r': | |
178 | if (n) | |
179 | result = result & ~(NIS_READ_ACC << 24); | |
180 | if (o) | |
181 | result = result & ~(NIS_READ_ACC << 16); | |
182 | if (g) | |
183 | result = result & ~(NIS_READ_ACC << 8); | |
184 | if (w) | |
185 | result = result & ~(NIS_READ_ACC); | |
186 | break; | |
187 | case 'm': | |
188 | if (n) | |
189 | result = result & ~(NIS_MODIFY_ACC << 24); | |
190 | if (o) | |
191 | result = result & ~(NIS_MODIFY_ACC << 16); | |
192 | if (g) | |
193 | result = result & ~(NIS_MODIFY_ACC << 8); | |
194 | if (w) | |
195 | result = result & ~(NIS_MODIFY_ACC); | |
196 | break; | |
197 | case 'c': | |
198 | if (n) | |
199 | result = result & ~(NIS_CREATE_ACC << 24); | |
200 | if (o) | |
201 | result = result & ~(NIS_CREATE_ACC << 16); | |
202 | if (g) | |
203 | result = result & ~(NIS_CREATE_ACC << 8); | |
204 | if (w) | |
205 | result = result & ~(NIS_CREATE_ACC); | |
206 | break; | |
207 | case 'd': | |
208 | if (n) | |
209 | result = result & ~(NIS_DESTROY_ACC << 24); | |
210 | if (o) | |
211 | result = result & ~(NIS_DESTROY_ACC << 16); | |
212 | if (g) | |
213 | result = result & ~(NIS_DESTROY_ACC << 8); | |
214 | if (w) | |
215 | result = result & ~(NIS_DESTROY_ACC); | |
216 | break; | |
217 | default: | |
a1129917 | 218 | return (~0U); |
51702635 UD |
219 | } |
220 | cptr++; | |
221 | } | |
dfd2257a | 222 | n = o = g = w = 0; |
51702635 UD |
223 | break; |
224 | case '+': | |
3519948b | 225 | cptr++; /* Remove "+" from beginning */ |
51702635 UD |
226 | while (*cptr != '\0' && *cptr != ',') |
227 | { | |
228 | switch (*cptr) | |
229 | { | |
230 | case 'r': | |
231 | if (n) | |
232 | result = result | (NIS_READ_ACC << 24); | |
233 | if (o) | |
234 | result = result | (NIS_READ_ACC << 16); | |
235 | if (g) | |
236 | result = result | (NIS_READ_ACC << 8); | |
237 | if (w) | |
238 | result = result | (NIS_READ_ACC); | |
239 | break; | |
240 | case 'm': | |
241 | if (n) | |
242 | result = result | (NIS_MODIFY_ACC << 24); | |
243 | if (o) | |
244 | result = result | (NIS_MODIFY_ACC << 16); | |
245 | if (g) | |
246 | result = result | (NIS_MODIFY_ACC << 8); | |
247 | if (w) | |
248 | result = result | (NIS_MODIFY_ACC); | |
249 | break; | |
250 | case 'c': | |
251 | if (n) | |
252 | result = result | (NIS_CREATE_ACC << 24); | |
253 | if (o) | |
254 | result = result | (NIS_CREATE_ACC << 16); | |
255 | if (g) | |
256 | result = result | (NIS_CREATE_ACC << 8); | |
257 | if (w) | |
258 | result = result | (NIS_CREATE_ACC); | |
259 | break; | |
260 | case 'd': | |
261 | if (n) | |
262 | result = result | (NIS_DESTROY_ACC << 24); | |
263 | if (o) | |
264 | result = result | (NIS_DESTROY_ACC << 16); | |
265 | if (g) | |
266 | result = result | (NIS_DESTROY_ACC << 8); | |
267 | if (w) | |
268 | result = result | (NIS_DESTROY_ACC); | |
269 | break; | |
270 | default: | |
a1129917 | 271 | return (~0U); |
51702635 UD |
272 | } |
273 | cptr++; | |
274 | } | |
dfd2257a | 275 | n = o = g = w = 0; |
51702635 UD |
276 | break; |
277 | case '=': | |
278 | cptr++; /* Remove "=" from beginning */ | |
279 | /* Clear */ | |
280 | if (n) | |
281 | result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC + | |
282 | NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24); | |
283 | ||
284 | if (o) | |
285 | result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC + | |
286 | NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16); | |
287 | if (g) | |
288 | result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC + | |
289 | NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8); | |
290 | if (w) | |
291 | result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC + | |
292 | NIS_CREATE_ACC + NIS_DESTROY_ACC); | |
293 | while (*cptr != '\0' && *cptr != ',') | |
294 | { | |
295 | switch (*cptr) | |
296 | { | |
297 | case 'r': | |
298 | if (n) | |
299 | result = result | (NIS_READ_ACC << 24); | |
300 | if (o) | |
301 | result = result | (NIS_READ_ACC << 16); | |
302 | if (g) | |
303 | result = result | (NIS_READ_ACC << 8); | |
304 | if (w) | |
305 | result = result | (NIS_READ_ACC); | |
306 | break; | |
307 | case 'm': | |
308 | if (n) | |
309 | result = result | (NIS_MODIFY_ACC << 24); | |
310 | if (o) | |
311 | result = result | (NIS_MODIFY_ACC << 16); | |
312 | if (g) | |
313 | result = result | (NIS_MODIFY_ACC << 8); | |
314 | if (w) | |
315 | result = result | (NIS_MODIFY_ACC); | |
316 | break; | |
317 | case 'c': | |
318 | if (n) | |
319 | result = result | (NIS_CREATE_ACC << 24); | |
320 | if (o) | |
321 | result = result | (NIS_CREATE_ACC << 16); | |
322 | if (g) | |
323 | result = result | (NIS_CREATE_ACC << 8); | |
324 | if (w) | |
325 | result = result | (NIS_CREATE_ACC); | |
326 | break; | |
327 | case 'd': | |
328 | if (n) | |
329 | result = result | (NIS_DESTROY_ACC << 24); | |
330 | if (o) | |
331 | result = result | (NIS_DESTROY_ACC << 16); | |
332 | if (g) | |
333 | result = result | (NIS_DESTROY_ACC << 8); | |
334 | if (w) | |
335 | result = result | (NIS_DESTROY_ACC); | |
336 | break; | |
337 | default: | |
a1129917 | 338 | return result = (~0U); |
51702635 UD |
339 | } |
340 | cptr++; | |
341 | } | |
dfd2257a | 342 | n = o = g = w = 0; |
51702635 UD |
343 | break; |
344 | default: | |
a1129917 | 345 | return result = (~0U); |
51702635 | 346 | } |
3519948b UD |
347 | if (*cptr != '\0') |
348 | cptr++; | |
51702635 UD |
349 | } |
350 | ||
dfd2257a | 351 | return result; |
51702635 UD |
352 | } |
353 | ||
51e59260 | 354 | |
51702635 UD |
355 | nis_name |
356 | __nis_default_owner (char *defaults) | |
357 | { | |
51e59260 | 358 | char *default_owner = NULL; |
51702635 | 359 | |
51e59260 UD |
360 | char *cptr = defaults; |
361 | if (cptr == NULL) | |
362 | cptr = getenv ("NIS_DEFAULTS"); | |
51702635 | 363 | |
51e59260 | 364 | if (cptr != NULL) |
51702635 | 365 | { |
51e59260 | 366 | char *dptr = strstr (cptr, "owner="); |
51702635 | 367 | if (dptr != NULL) |
dfd2257a | 368 | { |
51e59260 UD |
369 | char *p = searchowner (dptr); |
370 | if (p == NULL) | |
371 | return NULL; | |
372 | default_owner = strdupa (p); | |
dfd2257a UD |
373 | free (p); |
374 | } | |
51702635 | 375 | } |
51702635 | 376 | |
51e59260 | 377 | return strdup (default_owner ?: nis_local_principal ()); |
51702635 | 378 | } |
1e4d83f6 | 379 | libnsl_hidden_nolink_def (__nis_default_owner, GLIBC_2_1) |
51702635 | 380 | |
51e59260 | 381 | |
51702635 UD |
382 | nis_name |
383 | __nis_default_group (char *defaults) | |
384 | { | |
51e59260 | 385 | char *default_group = NULL; |
51702635 | 386 | |
51e59260 UD |
387 | char *cptr = defaults; |
388 | if (cptr == NULL) | |
389 | cptr = getenv ("NIS_DEFAULTS"); | |
51702635 | 390 | |
51e59260 | 391 | if (cptr != NULL) |
51702635 | 392 | { |
51e59260 | 393 | char *dptr = strstr (cptr, "group="); |
51702635 | 394 | if (dptr != NULL) |
dfd2257a | 395 | { |
51e59260 UD |
396 | char *p = searchgroup (dptr); |
397 | if (p == NULL) | |
398 | return NULL; | |
399 | default_group = strdupa (p); | |
dfd2257a UD |
400 | free (p); |
401 | } | |
51702635 | 402 | } |
a334319f | 403 | |
51e59260 | 404 | return strdup (default_group ?: nis_local_group ()); |
51702635 | 405 | } |
1e4d83f6 | 406 | libnsl_hidden_nolink_def (__nis_default_group, GLIBC_2_1) |
51702635 | 407 | |
51e59260 | 408 | |
a1129917 | 409 | uint32_t |
51702635 UD |
410 | __nis_default_ttl (char *defaults) |
411 | { | |
412 | char *cptr, *dptr; | |
413 | ||
414 | if (defaults != NULL) | |
415 | { | |
416 | dptr = strstr (defaults, "ttl="); | |
417 | if (dptr != NULL) | |
418 | return searchttl (defaults); | |
419 | } | |
420 | ||
421 | cptr = getenv ("NIS_DEFAULTS"); | |
422 | if (cptr == NULL) | |
423 | return DEFAULT_TTL; | |
424 | ||
425 | dptr = strstr (cptr, "ttl="); | |
426 | if (dptr == NULL) | |
427 | return DEFAULT_TTL; | |
428 | ||
429 | return searchttl (cptr); | |
430 | } | |
431 | ||
432 | /* Default access rights are ----rmcdr---r---, but we could change | |
433 | this with the NIS_DEFAULTS variable. */ | |
a1129917 UD |
434 | unsigned int |
435 | __nis_default_access (char *param, unsigned int defaults) | |
51702635 | 436 | { |
a1129917 | 437 | unsigned int result; |
51702635 UD |
438 | char *cptr; |
439 | ||
440 | if (defaults == 0) | |
441 | result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT; | |
442 | else | |
443 | result = defaults; | |
444 | ||
445 | if (param != NULL && strstr (param, "access=") != NULL) | |
446 | result = searchaccess (param, result); | |
447 | else | |
448 | { | |
449 | cptr = getenv ("NIS_DEFAULTS"); | |
450 | if (cptr != NULL && strstr (cptr, "access=") != NULL) | |
6c7e1cf5 | 451 | result = searchaccess (cptr, result); |
51702635 UD |
452 | } |
453 | ||
454 | return result; | |
455 | } | |
1e4d83f6 | 456 | libnsl_hidden_nolink_def (__nis_default_access, GLIBC_2_1) |