[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nis / nss-default.c
blob577f7c2d47fb145da02eae00f5ef49e93b28d332
1 /* Copyright (C) 1996, 2001, 2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdio_ext.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
26 #include <libnsl.h>
29 /* Path of the file. */
30 static const char default_nss[] = "/etc/default/nss";
32 /* Flags once read from the file. */
33 static int default_nss_flags;
35 /* Code to make sure we call 'init' once. */
36 __libc_once_define (static, once);
38 /* Table of the recognized variables. */
39 static const struct
41 char name[23];
42 unsigned int len;
43 int flag;
44 } vars[] =
46 #define STRNLEN(s) s, sizeof (s) - 1
47 { STRNLEN ("NETID_AUTHORITATIVE"), NSS_FLAG_NETID_AUTHORITATIVE },
48 { STRNLEN ("SERVICES_AUTHORITATIVE"), NSS_FLAG_SERVICES_AUTHORITATIVE },
49 { STRNLEN ("SETENT_BATCH_READ"), NSS_FLAG_SETENT_BATCH_READ }
51 #define nvars (sizeof (vars) / sizeof (vars[0]))
54 static void
55 init (void)
57 FILE *fp = fopen (default_nss, "rc");
58 if (fp != NULL)
60 char *line = NULL;
61 size_t linelen = 0;
63 __fsetlocking (fp, FSETLOCKING_BYCALLER);
65 while (!feof_unlocked (fp))
67 ssize_t n = getline (&line, &linelen, fp);
68 if (n <= 0)
69 break;
71 /* Recognize only
73 <THE-VARIABLE> = TRUE
75 with arbitrary white spaces. */
76 char *cp = line;
77 while (isspace (*cp))
78 ++cp;
80 /* Recognize comment lines. */
81 if (*cp == '#')
82 continue;
84 int idx;
85 for (idx = 0; idx < nvars; ++idx)
86 if (strncmp (cp, vars[idx].name, vars[idx].len) == 0)
87 break;
88 if (idx == nvars)
89 continue;
91 cp += vars[idx].len;
92 while (isspace (*cp))
93 ++cp;
94 if (*cp++ != '=')
95 continue;
96 while (isspace (*cp))
97 ++cp;
99 if (strncmp (cp, "TRUE", 4) != 0)
100 continue;
101 cp += 4;
103 while (isspace (*cp))
104 ++cp;
106 if (*cp == '\0')
107 default_nss_flags |= vars[idx].flag;
110 free (line);
112 fclose (fp);
118 _nsl_default_nss (void)
120 /* If we have not yet read the file yet do it now. */
121 __libc_once (once, init);
123 return default_nss_flags;