* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nscd / nscd_conf.c
blobe724c6bd59dc82051f8e2477ffc5e9bad540f2ac
1 /* Copyright (c) 1998, 2000, 2003-2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <error.h>
21 #include <libintl.h>
22 #include <malloc.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdio_ext.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
32 #include "dbg_log.h"
33 #include "nscd.h"
35 /* Wrapper functions with error checking for standard functions. */
36 extern char *xstrdup (const char *s);
39 /* Names of the databases. */
40 const char *const dbnames[lastdb] =
42 [pwddb] = "passwd",
43 [grpdb] = "group",
44 [hstdb] = "hosts",
45 [servdb] = "services"
49 static int
50 find_db (const char *name)
52 for (int cnt = 0; cnt < lastdb; ++cnt)
53 if (strcmp (name, dbnames[cnt]) == 0)
54 return cnt;
56 error (0, 0, _("database %s is not supported"), name);
57 return -1;
60 int
61 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
63 FILE *fp;
64 char *line, *cp, *entry, *arg1, *arg2;
65 size_t len;
66 int cnt;
67 const unsigned int initial_error_message_count = error_message_count;
69 /* Open the configuration file. */
70 fp = fopen (fname, "r");
71 if (fp == NULL)
72 return -1;
74 /* The stream is not used by more than one thread. */
75 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
77 line = NULL;
78 len = 0;
82 ssize_t n = getline (&line, &len, fp);
83 if (n < 0)
84 break;
85 if (line[n - 1] == '\n')
86 line[n - 1] = '\0';
88 /* Because the file format does not know any form of quoting we
89 can search forward for the next '#' character and if found
90 make it terminating the line. */
91 *strchrnul (line, '#') = '\0';
93 /* If the line is blank it is ignored. */
94 if (line[0] == '\0')
95 continue;
97 entry = line;
98 while (isspace (*entry) && *entry != '\0')
99 ++entry;
100 cp = entry;
101 while (!isspace (*cp) && *cp != '\0')
102 ++cp;
103 arg1 = cp;
104 ++arg1;
105 *cp = '\0';
106 if (strlen (entry) == 0)
107 error (0, 0, _("Parse error: %s"), line);
108 while (isspace (*arg1) && *arg1 != '\0')
109 ++arg1;
110 cp = arg1;
111 while (!isspace (*cp) && *cp != '\0')
112 ++cp;
113 arg2 = cp;
114 ++arg2;
115 *cp = '\0';
116 if (strlen (arg2) > 0)
118 while (isspace (*arg2) && *arg2 != '\0')
119 ++arg2;
120 cp = arg2;
121 while (!isspace (*cp) && *cp != '\0')
122 ++cp;
123 *cp = '\0';
126 if (strcmp (entry, "positive-time-to-live") == 0)
128 int idx = find_db (arg1);
129 if (idx >= 0)
130 dbs[idx].postimeout = atol (arg2);
132 else if (strcmp (entry, "negative-time-to-live") == 0)
134 int idx = find_db (arg1);
135 if (idx >= 0)
136 dbs[idx].negtimeout = atol (arg2);
138 else if (strcmp (entry, "suggested-size") == 0)
140 int idx = find_db (arg1);
141 if (idx >= 0)
142 dbs[idx].suggested_module = atol (arg2);
144 else if (strcmp (entry, "enable-cache") == 0)
146 int idx = find_db (arg1);
147 if (idx >= 0)
149 if (strcmp (arg2, "no") == 0)
150 dbs[idx].enabled = 0;
151 else if (strcmp (arg2, "yes") == 0)
152 dbs[idx].enabled = 1;
155 else if (strcmp (entry, "check-files") == 0)
157 int idx = find_db (arg1);
158 if (idx >= 0)
160 if (strcmp (arg2, "no") == 0)
161 dbs[idx].check_file = 0;
162 else if (strcmp (arg2, "yes") == 0)
163 dbs[idx].check_file = 1;
166 else if (strcmp (entry, "max-db-size") == 0)
168 int idx = find_db (arg1);
169 if (idx >= 0)
170 dbs[idx].max_db_size = atol (arg2);
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, "max-threads") == 0)
187 max_nthreads = MAX (atol (arg1), lastdb);
189 else if (strcmp (entry, "server-user") == 0)
191 if (!arg1)
192 error (0, 0, _("Must specify user name for server-user option"));
193 else
194 server_user = xstrdup (arg1);
196 else if (strcmp (entry, "stat-user") == 0)
198 if (arg1 == NULL)
199 error (0, 0, _("Must specify user name for stat-user option"));
200 else
202 stat_user = xstrdup (arg1);
204 struct passwd *pw = getpwnam (stat_user);
205 if (pw != NULL)
206 stat_uid = pw->pw_uid;
209 else if (strcmp (entry, "persistent") == 0)
211 int idx = find_db (arg1);
212 if (idx >= 0)
214 if (strcmp (arg2, "no") == 0)
215 dbs[idx].persistent = 0;
216 else if (strcmp (arg2, "yes") == 0)
217 dbs[idx].persistent = 1;
220 else if (strcmp (entry, "shared") == 0)
222 int idx = find_db (arg1);
223 if (idx >= 0)
225 if (strcmp (arg2, "no") == 0)
226 dbs[idx].shared = 0;
227 else if (strcmp (arg2, "yes") == 0)
228 dbs[idx].shared = 1;
231 else if (strcmp (entry, "reload-count") == 0)
233 if (strcasecmp (arg1, "unlimited") == 0)
234 reload_count = UINT_MAX;
235 else
237 unsigned int count = strtoul (arg1, NULL, 0);
238 if (count > UINT8_MAX - 1)
239 reload_count = UINT_MAX;
240 else if (count >= 0)
241 reload_count = count;
242 else
243 error (0, 0, _("invalid value for 'reload-count': %u"), count);
246 else if (strcmp (entry, "paranoia") == 0)
248 if (strcmp (arg1, "no") == 0)
249 paranoia = 0;
250 else if (strcmp (arg1, "yes") == 0)
251 paranoia = 1;
253 else if (strcmp (entry, "restart-interval") == 0)
255 if (arg1 != NULL)
256 restart_interval = atol (arg1);
257 else
258 error (0, 0, _("Must specify value for restart-interval option"));
260 else if (strcmp (entry, "auto-propagate") == 0)
262 int idx = find_db (arg1);
263 if (idx >= 0)
265 if (strcmp (arg2, "no") == 0)
266 dbs[idx].propagate = 0;
267 else if (strcmp (arg2, "yes") == 0)
268 dbs[idx].propagate = 1;
271 else
272 error (0, 0, _("Unknown option: %s %s %s"), entry, arg1, arg2);
274 while (!feof_unlocked (fp));
276 if (paranoia)
278 restart_time = time (NULL) + restart_interval;
280 /* Save the old current workding directory if we are in paranoia
281 mode. We have to change back to it. */
282 oldcwd = get_current_dir_name ();
283 if (oldcwd == NULL)
285 error (0, 0, _("\
286 cannot get current working directory: %s; disabling paranoia mode"),
287 strerror (errno));
288 paranoia = 0;
292 /* Enforce sanity. */
293 if (max_nthreads < nthreads)
294 max_nthreads = nthreads;
296 for (cnt = 0; cnt < lastdb; ++cnt)
298 size_t datasize = (sizeof (struct database_pers_head)
299 + roundup (dbs[cnt].suggested_module
300 * sizeof (ref_t), ALIGN)
301 + (dbs[cnt].suggested_module
302 * DEFAULT_DATASIZE_PER_BUCKET));
303 if (datasize > dbs[cnt].max_db_size)
305 error (0, 0, _("maximum file size for %s database too small"),
306 dbnames[cnt]);
307 dbs[cnt].max_db_size = datasize;
312 /* Free the buffer. */
313 free (line);
314 /* Close configuration file. */
315 fclose (fp);
317 return error_message_count != initial_error_message_count;