Updated to fedora-glibc-20050708T0811
[glibc.git] / nscd / nscd_conf.c
blobd21f2fc501135dbe305c9ec879935aaf0779b7fb
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 <errno.h>
22 #include <libintl.h>
23 #include <malloc.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdio_ext.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include "dbg_log.h"
34 #include "nscd.h"
36 /* Wrapper functions with error checking for standard functions. */
37 extern char *xstrdup (const char *s);
40 /* Names of the databases. */
41 const char *dbnames[lastdb] =
43 [pwddb] = "passwd",
44 [grpdb] = "group",
45 [hstdb] = "hosts"
48 int
49 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
51 FILE *fp;
52 char *line, *cp, *entry, *arg1, *arg2;
53 size_t len;
54 int cnt;
56 /* Open the configuration file. */
57 fp = fopen (fname, "r");
58 if (fp == NULL)
59 return -1;
61 /* The stream is not used by more than one thread. */
62 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
64 line = NULL;
65 len = 0;
69 ssize_t n = getline (&line, &len, fp);
70 if (n < 0)
71 break;
72 if (line[n - 1] == '\n')
73 line[n - 1] = '\0';
75 /* Because the file format does not know any form of quoting we
76 can search forward for the next '#' character and if found
77 make it terminating the line. */
78 *strchrnul (line, '#') = '\0';
80 /* If the line is blank it is ignored. */
81 if (line[0] == '\0')
82 continue;
84 entry = line;
85 while (isspace (*entry) && *entry != '\0')
86 ++entry;
87 cp = entry;
88 while (!isspace (*cp) && *cp != '\0')
89 ++cp;
90 arg1 = cp;
91 ++arg1;
92 *cp = '\0';
93 if (strlen (entry) == 0)
94 dbg_log (_("Parse error: %s"), line);
95 while (isspace (*arg1) && *arg1 != '\0')
96 ++arg1;
97 cp = arg1;
98 while (!isspace (*cp) && *cp != '\0')
99 ++cp;
100 arg2 = cp;
101 ++arg2;
102 *cp = '\0';
103 if (strlen (arg2) > 0)
105 while (isspace (*arg2) && *arg2 != '\0')
106 ++arg2;
107 cp = arg2;
108 while (!isspace (*cp) && *cp != '\0')
109 ++cp;
110 *cp = '\0';
113 if (strcmp (entry, "positive-time-to-live") == 0)
115 for (cnt = 0; cnt < lastdb; ++cnt)
116 if (strcmp (arg1, dbnames[cnt]) == 0)
118 dbs[cnt].postimeout = atol (arg2);
119 break;
121 if (cnt == lastdb)
122 dbg_log ("database %s is not supported\n", arg1);
124 else if (strcmp (entry, "negative-time-to-live") == 0)
126 for (cnt = 0; cnt < lastdb; ++cnt)
127 if (strcmp (arg1, dbnames[cnt]) == 0)
129 dbs[cnt].negtimeout = atol (arg2);
130 break;
132 if (cnt == lastdb)
133 dbg_log ("database %s is not supported\n", arg1);
135 else if (strcmp (entry, "suggested-size") == 0)
137 for (cnt = 0; cnt < lastdb; ++cnt)
138 if (strcmp (arg1, dbnames[cnt]) == 0)
140 dbs[cnt].suggested_module = atol (arg2);
141 break;
143 if (cnt == lastdb)
144 dbg_log ("database %s is not supported\n", arg1);
146 else if (strcmp (entry, "enable-cache") == 0)
148 for (cnt = 0; cnt < lastdb; ++cnt)
149 if (strcmp (arg1, dbnames[cnt]) == 0)
151 if (strcmp (arg2, "no") == 0)
152 dbs[cnt].enabled = 0;
153 else if (strcmp (arg2, "yes") == 0)
154 dbs[cnt].enabled = 1;
155 break;
157 if (cnt == lastdb)
158 dbg_log ("database %s is not supported\n", arg1);
160 else if (strcmp (entry, "check-files") == 0)
162 for (cnt = 0; cnt < lastdb; ++cnt)
163 if (strcmp (arg1, dbnames[cnt]) == 0)
165 if (strcmp (arg2, "no") == 0)
166 dbs[cnt].check_file = 0;
167 else if (strcmp (arg2, "yes") == 0)
168 dbs[cnt].check_file = 1;
169 break;
171 if (cnt == lastdb)
172 dbg_log ("database %s is not supported\n", arg1);
174 else if (strcmp (entry, "logfile") == 0)
175 set_logfile (arg1);
176 else if (strcmp (entry, "debug-level") == 0)
178 int level = atoi (arg1);
179 if (level > 0)
180 debug_level = level;
182 else if (strcmp (entry, "threads") == 0)
184 if (nthreads == -1)
185 nthreads = MAX (atol (arg1), lastdb);
187 else if (strcmp (entry, "max-threads") == 0)
189 max_nthreads = MAX (atol (arg1), lastdb);
191 else if (strcmp (entry, "server-user") == 0)
193 if (!arg1)
194 dbg_log (_("Must specify user name for server-user option"));
195 else
196 server_user = xstrdup (arg1);
198 else if (strcmp (entry, "stat-user") == 0)
200 if (arg1 == NULL)
201 dbg_log (_("Must specify user name for stat-user option"));
202 else
204 stat_user = xstrdup (arg1);
206 struct passwd *pw = getpwnam (stat_user);
207 if (pw != NULL)
208 stat_uid = pw->pw_uid;
211 else if (strcmp (entry, "persistent") == 0)
213 for (cnt = 0; cnt < lastdb; ++cnt)
214 if (strcmp (arg1, dbnames[cnt]) == 0)
216 if (strcmp (arg2, "no") == 0)
217 dbs[cnt].persistent = 0;
218 else if (strcmp (arg2, "yes") == 0)
219 dbs[cnt].persistent = 1;
220 break;
222 if (cnt == lastdb)
223 dbg_log ("database %s is not supported\n", arg1);
225 else if (strcmp (entry, "shared") == 0)
227 for (cnt = 0; cnt < lastdb; ++cnt)
228 if (strcmp (arg1, dbnames[cnt]) == 0)
230 if (strcmp (arg2, "no") == 0)
231 dbs[cnt].shared = 0;
232 else if (strcmp (arg2, "yes") == 0)
233 dbs[cnt].shared = 1;
234 break;
236 if (cnt == lastdb)
237 dbg_log ("database %s is not supported\n", arg1);
239 else if (strcmp (entry, "reload-count") == 0)
241 if (strcasecmp (arg1, "unlimited") == 0)
242 reload_count = UINT_MAX;
243 else
245 unsigned int count = strtoul (arg1, NULL, 0);
246 if (count > UINT8_MAX - 1)
247 reload_count = UINT_MAX;
248 else if (count >= 0)
249 reload_count = count;
250 else
251 dbg_log (_("invalid value for 'reload-count': %u"), count);
254 else if (strcmp (entry, "paranoia") == 0)
256 if (strcmp (arg1, "no") == 0)
257 paranoia = 0;
258 else if (strcmp (arg1, "yes") == 0)
259 paranoia = 1;
261 else if (strcmp (entry, "restart-interval") == 0)
263 if (arg1 != NULL)
264 restart_interval = atol (arg1);
265 else
266 dbg_log (_("Must specify value for restart-interval option"));
268 else
269 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
271 while (!feof_unlocked (fp));
273 if (paranoia)
275 restart_time = time (NULL) + restart_interval;
277 /* Save the old current workding directory if we are in paranoia
278 mode. We have to change back to it. */
279 oldcwd = get_current_dir_name ();
280 if (oldcwd == NULL)
282 dbg_log (_("\
283 cannot get current working directory: %s; disabling paranoia mode"),
284 strerror (errno));
285 paranoia = 0;
289 /* Enforce sanity. */
290 if (max_nthreads < nthreads)
291 max_nthreads = nthreads;
293 /* Free the buffer. */
294 free (line);
295 /* Close configuration file. */
296 fclose (fp);
298 return 0;