]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss-default.c
Clean up internal fopen uses
[thirdparty/glibc.git] / nis / nss-default.c
CommitLineData
312be3f9
UD
1/* Copyright (C) 1996,2001,2004,2006,2007,2010,2011
2 Free Software Foundation, Inc.
433f49c4
UD
3 This file is part of the GNU C Library.
4
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.
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <ctype.h>
57c9179c 21#include <errno.h>
433f49c4
UD
22#include <stdio.h>
23#include <stdio_ext.h>
24#include <stdlib.h>
25#include <string.h>
26#include <bits/libc-lock.h>
27
28#include <libnsl.h>
29
30
31/* Path of the file. */
32static const char default_nss[] = "/etc/default/nss";
33
34/* Flags once read from the file. */
35static int default_nss_flags;
36
37/* Code to make sure we call 'init' once. */
38__libc_once_define (static, once);
39
ee821689
UD
40/* Table of the recognized variables. */
41static const struct
42{
43 char name[23];
44 unsigned int len;
45 int flag;
46} vars[] =
47 {
48#define STRNLEN(s) s, sizeof (s) - 1
49 { STRNLEN ("NETID_AUTHORITATIVE"), NSS_FLAG_NETID_AUTHORITATIVE },
4eaa9bb4 50 { STRNLEN ("SERVICES_AUTHORITATIVE"), NSS_FLAG_SERVICES_AUTHORITATIVE },
71170aa0
UD
51 { STRNLEN ("SETENT_BATCH_READ"), NSS_FLAG_SETENT_BATCH_READ },
52 { STRNLEN ("ADJUNCT_AS_SHADOW"), NSS_FLAG_ADJUNCT_AS_SHADOW },
ee821689
UD
53 };
54#define nvars (sizeof (vars) / sizeof (vars[0]))
55
433f49c4
UD
56
57static void
58init (void)
59{
57c9179c 60 int saved_errno = errno;
312be3f9 61 FILE *fp = fopen (default_nss, "rce");
433f49c4
UD
62 if (fp != NULL)
63 {
64 char *line = NULL;
65 size_t linelen = 0;
66
67 __fsetlocking (fp, FSETLOCKING_BYCALLER);
68
69 while (!feof_unlocked (fp))
70 {
71 ssize_t n = getline (&line, &linelen, fp);
72 if (n <= 0)
73 break;
74
ee821689 75 /* Recognize only
433f49c4 76
ee821689 77 <THE-VARIABLE> = TRUE
433f49c4
UD
78
79 with arbitrary white spaces. */
80 char *cp = line;
81 while (isspace (*cp))
82 ++cp;
83
84 /* Recognize comment lines. */
85 if (*cp == '#')
86 continue;
87
ee821689
UD
88 int idx;
89 for (idx = 0; idx < nvars; ++idx)
90 if (strncmp (cp, vars[idx].name, vars[idx].len) == 0)
91 break;
92 if (idx == nvars)
433f49c4
UD
93 continue;
94
ee821689 95 cp += vars[idx].len;
433f49c4
UD
96 while (isspace (*cp))
97 ++cp;
98 if (*cp++ != '=')
99 continue;
100 while (isspace (*cp))
101 ++cp;
102
103 if (strncmp (cp, "TRUE", 4) != 0)
104 continue;
105 cp += 4;
106
107 while (isspace (*cp))
108 ++cp;
109
110 if (*cp == '\0')
ee821689 111 default_nss_flags |= vars[idx].flag;
433f49c4
UD
112 }
113
114 free (line);
115
116 fclose (fp);
117 }
57c9179c 118 __set_errno (saved_errno);
433f49c4
UD
119}
120
121
122int
123_nsl_default_nss (void)
124{
125 /* If we have not yet read the file yet do it now. */
126 __libc_once (once, init);
127
128 return default_nss_flags;
129}