[BZ #375]
[glibc.git] / nscd / nscd_conf.c
blob2e6f8127a7557baa3ca5152bf63fce5ca7306237
1 /* Copyright (c) 1998, 2000, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <malloc.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <libintl.h>
28 #include <sys/param.h>
29 #include <sys/types.h>
31 #include "dbg_log.h"
32 #include "nscd.h"
34 /* Wrapper functions with error checking for standard functions. */
35 extern char *xstrdup (const char *s);
38 /* Names of the databases. */
39 const char *dbnames[lastdb] =
41 [pwddb] = "passwd",
42 [grpdb] = "group",
43 [hstdb] = "hosts"
46 int
47 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
49 FILE *fp;
50 char *line, *cp, *entry, *arg1, *arg2;
51 size_t len;
52 int cnt;
54 /* Open the configuration file. */
55 fp = fopen (fname, "r");
56 if (fp == NULL)
57 return -1;
59 /* The stream is not used by more than one thread. */
60 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
62 line = NULL;
63 len = 0;
67 ssize_t n = getline (&line, &len, fp);
68 if (n < 0)
69 break;
70 if (line[n - 1] == '\n')
71 line[n - 1] = '\0';
73 /* Because the file format does not know any form of quoting we
74 can search forward for the next '#' character and if found
75 make it terminating the line. */
76 *strchrnul (line, '#') = '\0';
78 /* If the line is blank it is ignored. */
79 if (line[0] == '\0')
80 continue;
82 entry = line;
83 while (isspace (*entry) && *entry != '\0')
84 ++entry;
85 cp = entry;
86 while (!isspace (*cp) && *cp != '\0')
87 ++cp;
88 arg1 = cp;
89 ++arg1;
90 *cp = '\0';
91 if (strlen (entry) == 0)
92 dbg_log (_("Parse error: %s"), line);
93 while (isspace (*arg1) && *arg1 != '\0')
94 ++arg1;
95 cp = arg1;
96 while (!isspace (*cp) && *cp != '\0')
97 ++cp;
98 arg2 = cp;
99 ++arg2;
100 *cp = '\0';
101 if (strlen (arg2) > 0)
103 while (isspace (*arg2) && *arg2 != '\0')
104 ++arg2;
105 cp = arg2;
106 while (!isspace (*cp) && *cp != '\0')
107 ++cp;
108 *cp = '\0';
111 if (strcmp (entry, "positive-time-to-live") == 0)
113 for (cnt = 0; cnt < lastdb; ++cnt)
114 if (strcmp (arg1, dbnames[cnt]) == 0)
116 dbs[cnt].postimeout = atol (arg2);
117 break;
119 if (cnt == lastdb)
120 dbg_log ("database %s is not supported\n", arg1);
122 else if (strcmp (entry, "negative-time-to-live") == 0)
124 for (cnt = 0; cnt < lastdb; ++cnt)
125 if (strcmp (arg1, dbnames[cnt]) == 0)
127 dbs[cnt].negtimeout = atol (arg2);
128 break;
130 if (cnt == lastdb)
131 dbg_log ("database %s is not supported\n", arg1);
133 else if (strcmp (entry, "suggested-size") == 0)
135 for (cnt = 0; cnt < lastdb; ++cnt)
136 if (strcmp (arg1, dbnames[cnt]) == 0)
138 dbs[cnt].suggested_module = atol (arg2);
139 break;
141 if (cnt == lastdb)
142 dbg_log ("database %s is not supported\n", arg1);
144 else if (strcmp (entry, "enable-cache") == 0)
146 for (cnt = 0; cnt < lastdb; ++cnt)
147 if (strcmp (arg1, dbnames[cnt]) == 0)
149 if (strcmp (arg2, "no") == 0)
150 dbs[cnt].enabled = 0;
151 else if (strcmp (arg2, "yes") == 0)
152 dbs[cnt].enabled = 1;
153 break;
155 if (cnt == lastdb)
156 dbg_log ("database %s is not supported\n", arg1);
158 else if (strcmp (entry, "check-files") == 0)
160 for (cnt = 0; cnt < lastdb; ++cnt)
161 if (strcmp (arg1, dbnames[cnt]) == 0)
163 if (strcmp (arg2, "no") == 0)
164 dbs[cnt].check_file = 0;
165 else if (strcmp (arg2, "yes") == 0)
166 dbs[cnt].check_file = 1;
167 break;
169 if (cnt == lastdb)
170 dbg_log ("database %s is not supported\n", arg1);
172 else if (strcmp (entry, "logfile") == 0)
173 set_logfile (arg1);
174 else if (strcmp (entry, "debug-level") == 0)
176 int level = atoi (arg1);
177 if (level > 0)
178 debug_level = level;
180 else if (strcmp (entry, "threads") == 0)
182 if (nthreads == -1)
183 nthreads = MAX (atol (arg1), lastdb);
185 else if (strcmp (entry, "server-user") == 0)
187 if (!arg1)
188 dbg_log (_("Must specify user name for server-user option"));
189 else
190 server_user = xstrdup (arg1);
192 else if (strcmp (entry, "stat-user") == 0)
194 if (!arg1)
195 dbg_log (_("Must specify user name for stat-user option"));
196 else
198 stat_user = xstrdup (arg1);
200 struct passwd *pw = getpwnam (stat_user);
201 if (pw != NULL)
202 stat_uid = pw->pw_uid;
205 else if (strcmp (entry, "persistent") == 0)
207 for (cnt = 0; cnt < lastdb; ++cnt)
208 if (strcmp (arg1, dbnames[cnt]) == 0)
210 if (strcmp (arg2, "no") == 0)
211 dbs[cnt].persistent = 0;
212 else if (strcmp (arg2, "yes") == 0)
213 dbs[cnt].persistent = 1;
214 break;
216 if (cnt == lastdb)
217 dbg_log ("database %s is not supported\n", arg1);
219 else if (strcmp (entry, "shared") == 0)
221 for (cnt = 0; cnt < lastdb; ++cnt)
222 if (strcmp (arg1, dbnames[cnt]) == 0)
224 if (strcmp (arg2, "no") == 0)
225 dbs[cnt].shared = 0;
226 else if (strcmp (arg2, "yes") == 0)
227 dbs[cnt].shared = 1;
228 break;
230 if (cnt == lastdb)
231 dbg_log ("database %s is not supported\n", arg1);
233 else if (strcmp (entry, "reload-count") == 0)
235 if (strcasecmp (arg1, "unlimited") == 0)
236 reload_count = UINT_MAX;
237 else
239 unsigned int count = strtoul (arg1, NULL, 0);
240 if (count > UINT8_MAX - 1)
241 reload_count = UINT_MAX;
242 else if (count >= 0)
243 reload_count = count;
244 else
245 dbg_log (_("invalid value for 'reload-count': %u"), count);
248 else
249 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
251 while (!feof_unlocked (fp));
253 /* Free the buffer. */
254 free (line);
255 /* Close configuration file. */
256 fclose (fp);
258 return 0;