Remove x32/iofopen.c and x32/iofopen64.c
[glibc.git] / nis / nis_defaults.c
blob2c8241833a98176540a9a9a1c0247effb158824a
1 /* Copyright (c) 1997, 1998, 2004, 2006 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
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 searchXYX (char *str, const char *what)
35 assert (strlen (what) == 6);
36 assert (strncmp (str, what, 6) == 0);
37 str += 6; /* Points to the begin of the parameters. */
39 int i = 0;
40 while (str[i] != '\0' && str[i] != ':')
41 ++i;
42 if (i == 0) /* only "<WHAT>=" ? */
43 return strdup ("");
45 return strndup (str, i);
49 static nis_name
50 searchgroup (char *str)
52 return searchXYX (str, "group=");
56 static nis_name
57 searchowner (char *str)
59 return searchXYX (str, "owner=");
63 static uint32_t
64 searchttl (char *str)
66 char buf[strlen (str) + 1];
67 char *cptr, *dptr;
68 uint32_t time;
69 int i;
71 dptr = strstr (str, "ttl=");
72 if (dptr == NULL) /* should (could) not happen */
73 return DEFAULT_TTL;;
75 dptr += 4; /* points to the begin of the new ttl */
76 i = 0;
77 while (dptr[i] != '\0' && dptr[i] != ':')
78 i++;
79 if (i == 0) /* only "ttl=" ? */
80 return DEFAULT_TTL;
82 strncpy (buf, dptr, i);
83 buf[i] = '\0';
84 time = 0;
86 dptr = buf;
87 cptr = strchr (dptr, 'd');
88 if (cptr != NULL)
90 *cptr = '\0';
91 cptr++;
92 time += atoi (dptr) * 60 * 60 * 24;
93 dptr = cptr;
96 cptr = strchr (dptr, 'h');
97 if (cptr != NULL)
99 *cptr = '\0';
100 cptr++;
101 time += atoi (dptr) * 60 * 60;
102 dptr = cptr;
105 cptr = strchr (dptr, 'm');
106 if (cptr != NULL)
108 *cptr = '\0';
109 cptr++;
110 time += atoi (dptr) * 60;
111 dptr = cptr;
114 cptr = strchr (dptr, 's');
115 if (cptr != NULL)
116 *cptr = '\0';
118 time += atoi (dptr);
120 return time;
123 static unsigned int
124 searchaccess (char *str, unsigned int access)
126 char buf[strlen (str) + 1];
127 char *cptr;
128 unsigned int result = access;
129 int i;
130 int n, o, g, w;
132 cptr = strstr (str, "access=");
133 if (cptr == NULL)
134 return 0;
136 cptr += 7; /* points to the begin of the access string */
137 i = 0;
138 while (cptr[i] != '\0' && cptr[i] != ':')
139 i++;
140 if (i == 0) /* only "access=" ? */
141 return 0;
143 strncpy (buf, cptr, i);
144 buf[i] = '\0';
146 n = o = g = w = 0;
147 cptr = buf;
148 if (*cptr == ',') /* Fix for stupid Solaris scripts */
149 ++cptr;
150 while (*cptr != '\0')
152 switch (*cptr)
154 case 'n':
155 n = 1;
156 break;
157 case 'o':
158 o = 1;
159 break;
160 case 'g':
161 g = 1;
162 break;
163 case 'w':
164 w = 1;
165 break;
166 case 'a':
167 o = g = w = 1;
168 break;
169 case '-':
170 cptr++; /* Remove "-" from beginning */
171 while (*cptr != '\0' && *cptr != ',')
173 switch (*cptr)
175 case 'r':
176 if (n)
177 result = result & ~(NIS_READ_ACC << 24);
178 if (o)
179 result = result & ~(NIS_READ_ACC << 16);
180 if (g)
181 result = result & ~(NIS_READ_ACC << 8);
182 if (w)
183 result = result & ~(NIS_READ_ACC);
184 break;
185 case 'm':
186 if (n)
187 result = result & ~(NIS_MODIFY_ACC << 24);
188 if (o)
189 result = result & ~(NIS_MODIFY_ACC << 16);
190 if (g)
191 result = result & ~(NIS_MODIFY_ACC << 8);
192 if (w)
193 result = result & ~(NIS_MODIFY_ACC);
194 break;
195 case 'c':
196 if (n)
197 result = result & ~(NIS_CREATE_ACC << 24);
198 if (o)
199 result = result & ~(NIS_CREATE_ACC << 16);
200 if (g)
201 result = result & ~(NIS_CREATE_ACC << 8);
202 if (w)
203 result = result & ~(NIS_CREATE_ACC);
204 break;
205 case 'd':
206 if (n)
207 result = result & ~(NIS_DESTROY_ACC << 24);
208 if (o)
209 result = result & ~(NIS_DESTROY_ACC << 16);
210 if (g)
211 result = result & ~(NIS_DESTROY_ACC << 8);
212 if (w)
213 result = result & ~(NIS_DESTROY_ACC);
214 break;
215 default:
216 return (~0U);
218 cptr++;
220 n = o = g = w = 0;
221 break;
222 case '+':
223 cptr++; /* Remove "+" from beginning */
224 while (*cptr != '\0' && *cptr != ',')
226 switch (*cptr)
228 case 'r':
229 if (n)
230 result = result | (NIS_READ_ACC << 24);
231 if (o)
232 result = result | (NIS_READ_ACC << 16);
233 if (g)
234 result = result | (NIS_READ_ACC << 8);
235 if (w)
236 result = result | (NIS_READ_ACC);
237 break;
238 case 'm':
239 if (n)
240 result = result | (NIS_MODIFY_ACC << 24);
241 if (o)
242 result = result | (NIS_MODIFY_ACC << 16);
243 if (g)
244 result = result | (NIS_MODIFY_ACC << 8);
245 if (w)
246 result = result | (NIS_MODIFY_ACC);
247 break;
248 case 'c':
249 if (n)
250 result = result | (NIS_CREATE_ACC << 24);
251 if (o)
252 result = result | (NIS_CREATE_ACC << 16);
253 if (g)
254 result = result | (NIS_CREATE_ACC << 8);
255 if (w)
256 result = result | (NIS_CREATE_ACC);
257 break;
258 case 'd':
259 if (n)
260 result = result | (NIS_DESTROY_ACC << 24);
261 if (o)
262 result = result | (NIS_DESTROY_ACC << 16);
263 if (g)
264 result = result | (NIS_DESTROY_ACC << 8);
265 if (w)
266 result = result | (NIS_DESTROY_ACC);
267 break;
268 default:
269 return (~0U);
271 cptr++;
273 n = o = g = w = 0;
274 break;
275 case '=':
276 cptr++; /* Remove "=" from beginning */
277 /* Clear */
278 if (n)
279 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
280 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
282 if (o)
283 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
284 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
285 if (g)
286 result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
287 NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
288 if (w)
289 result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
290 NIS_CREATE_ACC + NIS_DESTROY_ACC);
291 while (*cptr != '\0' && *cptr != ',')
293 switch (*cptr)
295 case 'r':
296 if (n)
297 result = result | (NIS_READ_ACC << 24);
298 if (o)
299 result = result | (NIS_READ_ACC << 16);
300 if (g)
301 result = result | (NIS_READ_ACC << 8);
302 if (w)
303 result = result | (NIS_READ_ACC);
304 break;
305 case 'm':
306 if (n)
307 result = result | (NIS_MODIFY_ACC << 24);
308 if (o)
309 result = result | (NIS_MODIFY_ACC << 16);
310 if (g)
311 result = result | (NIS_MODIFY_ACC << 8);
312 if (w)
313 result = result | (NIS_MODIFY_ACC);
314 break;
315 case 'c':
316 if (n)
317 result = result | (NIS_CREATE_ACC << 24);
318 if (o)
319 result = result | (NIS_CREATE_ACC << 16);
320 if (g)
321 result = result | (NIS_CREATE_ACC << 8);
322 if (w)
323 result = result | (NIS_CREATE_ACC);
324 break;
325 case 'd':
326 if (n)
327 result = result | (NIS_DESTROY_ACC << 24);
328 if (o)
329 result = result | (NIS_DESTROY_ACC << 16);
330 if (g)
331 result = result | (NIS_DESTROY_ACC << 8);
332 if (w)
333 result = result | (NIS_DESTROY_ACC);
334 break;
335 default:
336 return result = (~0U);
338 cptr++;
340 n = o = g = w = 0;
341 break;
342 default:
343 return result = (~0U);
345 if (*cptr != '\0')
346 cptr++;
349 return result;
353 nis_name
354 __nis_default_owner (char *defaults)
356 char *default_owner = NULL;
358 char *cptr = defaults;
359 if (cptr == NULL)
360 cptr = getenv ("NIS_DEFAULTS");
362 if (cptr != NULL)
364 char *dptr = strstr (cptr, "owner=");
365 if (dptr != NULL)
367 char *p = searchowner (dptr);
368 if (p == NULL)
369 return NULL;
370 default_owner = strdupa (p);
371 free (p);
375 return strdup (default_owner ?: nis_local_principal ());
377 libnsl_hidden_def (__nis_default_owner)
380 nis_name
381 __nis_default_group (char *defaults)
383 char *default_group = NULL;
385 char *cptr = defaults;
386 if (cptr == NULL)
387 cptr = getenv ("NIS_DEFAULTS");
389 if (cptr != NULL)
391 char *dptr = strstr (cptr, "group=");
392 if (dptr != NULL)
394 char *p = searchgroup (dptr);
395 if (p == NULL)
396 return NULL;
397 default_group = strdupa (p);
398 free (p);
402 return strdup (default_group ?: nis_local_group ());
404 libnsl_hidden_def (__nis_default_group)
407 uint32_t
408 __nis_default_ttl (char *defaults)
410 char *cptr, *dptr;
412 if (defaults != NULL)
414 dptr = strstr (defaults, "ttl=");
415 if (dptr != NULL)
416 return searchttl (defaults);
419 cptr = getenv ("NIS_DEFAULTS");
420 if (cptr == NULL)
421 return DEFAULT_TTL;
423 dptr = strstr (cptr, "ttl=");
424 if (dptr == NULL)
425 return DEFAULT_TTL;
427 return searchttl (cptr);
430 /* Default access rights are ----rmcdr---r---, but we could change
431 this with the NIS_DEFAULTS variable. */
432 unsigned int
433 __nis_default_access (char *param, unsigned int defaults)
435 unsigned int result;
436 char *cptr;
438 if (defaults == 0)
439 result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
440 else
441 result = defaults;
443 if (param != NULL && strstr (param, "access=") != NULL)
444 result = searchaccess (param, result);
445 else
447 cptr = getenv ("NIS_DEFAULTS");
448 if (cptr != NULL && strstr (cptr, "access=") != NULL)
449 result = searchaccess (cptr, result);
452 return result;
454 libnsl_hidden_def (__nis_default_access)