]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_conf.c
* nscd/connection.c (DEFAULT_DATASIZE_PER_BUCKET): Move to nscd.h.
[thirdparty/glibc.git] / nscd / nscd_conf.c
1 /* Copyright (c) 1998, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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>
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>
32
33 #include "dbg_log.h"
34 #include "nscd.h"
35
36 /* Wrapper functions with error checking for standard functions. */
37 extern char *xstrdup (const char *s);
38
39
40 /* Names of the databases. */
41 const char *dbnames[lastdb] =
42 {
43 [pwddb] = "passwd",
44 [grpdb] = "group",
45 [hstdb] = "hosts"
46 };
47
48 int
49 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
50 {
51 FILE *fp;
52 char *line, *cp, *entry, *arg1, *arg2;
53 size_t len;
54 int cnt;
55
56 /* Open the configuration file. */
57 fp = fopen (fname, "r");
58 if (fp == NULL)
59 return -1;
60
61 /* The stream is not used by more than one thread. */
62 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
63
64 line = NULL;
65 len = 0;
66
67 do
68 {
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';
74
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';
79
80 /* If the line is blank it is ignored. */
81 if (line[0] == '\0')
82 continue;
83
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)
104 {
105 while (isspace (*arg2) && *arg2 != '\0')
106 ++arg2;
107 cp = arg2;
108 while (!isspace (*cp) && *cp != '\0')
109 ++cp;
110 *cp = '\0';
111 }
112
113 if (strcmp (entry, "positive-time-to-live") == 0)
114 {
115 for (cnt = 0; cnt < lastdb; ++cnt)
116 if (strcmp (arg1, dbnames[cnt]) == 0)
117 {
118 dbs[cnt].postimeout = atol (arg2);
119 break;
120 }
121 if (cnt == lastdb)
122 dbg_log ("database %s is not supported\n", arg1);
123 }
124 else if (strcmp (entry, "negative-time-to-live") == 0)
125 {
126 for (cnt = 0; cnt < lastdb; ++cnt)
127 if (strcmp (arg1, dbnames[cnt]) == 0)
128 {
129 dbs[cnt].negtimeout = atol (arg2);
130 break;
131 }
132 if (cnt == lastdb)
133 dbg_log ("database %s is not supported\n", arg1);
134 }
135 else if (strcmp (entry, "suggested-size") == 0)
136 {
137 for (cnt = 0; cnt < lastdb; ++cnt)
138 if (strcmp (arg1, dbnames[cnt]) == 0)
139 {
140 dbs[cnt].suggested_module = atol (arg2);
141 break;
142 }
143 if (cnt == lastdb)
144 dbg_log ("database %s is not supported\n", arg1);
145 }
146 else if (strcmp (entry, "enable-cache") == 0)
147 {
148 for (cnt = 0; cnt < lastdb; ++cnt)
149 if (strcmp (arg1, dbnames[cnt]) == 0)
150 {
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;
156 }
157 if (cnt == lastdb)
158 dbg_log ("database %s is not supported\n", arg1);
159 }
160 else if (strcmp (entry, "check-files") == 0)
161 {
162 for (cnt = 0; cnt < lastdb; ++cnt)
163 if (strcmp (arg1, dbnames[cnt]) == 0)
164 {
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;
170 }
171 if (cnt == lastdb)
172 dbg_log ("database %s is not supported\n", arg1);
173 }
174 else if (strcmp (entry, "max-db-size") == 0)
175 {
176 for (cnt = 0; cnt < lastdb; ++cnt)
177 if (strcmp (arg1, dbnames[cnt]) == 0)
178 {
179 dbs[cnt].max_db_size = atol (arg2);
180 break;
181 }
182 if (cnt == lastdb)
183 dbg_log ("database %s is not supported\n", arg1);
184 }
185 else if (strcmp (entry, "logfile") == 0)
186 set_logfile (arg1);
187 else if (strcmp (entry, "debug-level") == 0)
188 {
189 int level = atoi (arg1);
190 if (level > 0)
191 debug_level = level;
192 }
193 else if (strcmp (entry, "threads") == 0)
194 {
195 if (nthreads == -1)
196 nthreads = MAX (atol (arg1), lastdb);
197 }
198 else if (strcmp (entry, "max-threads") == 0)
199 {
200 max_nthreads = MAX (atol (arg1), lastdb);
201 }
202 else if (strcmp (entry, "server-user") == 0)
203 {
204 if (!arg1)
205 dbg_log (_("Must specify user name for server-user option"));
206 else
207 server_user = xstrdup (arg1);
208 }
209 else if (strcmp (entry, "stat-user") == 0)
210 {
211 if (arg1 == NULL)
212 dbg_log (_("Must specify user name for stat-user option"));
213 else
214 {
215 stat_user = xstrdup (arg1);
216
217 struct passwd *pw = getpwnam (stat_user);
218 if (pw != NULL)
219 stat_uid = pw->pw_uid;
220 }
221 }
222 else if (strcmp (entry, "persistent") == 0)
223 {
224 for (cnt = 0; cnt < lastdb; ++cnt)
225 if (strcmp (arg1, dbnames[cnt]) == 0)
226 {
227 if (strcmp (arg2, "no") == 0)
228 dbs[cnt].persistent = 0;
229 else if (strcmp (arg2, "yes") == 0)
230 dbs[cnt].persistent = 1;
231 break;
232 }
233 if (cnt == lastdb)
234 dbg_log ("database %s is not supported\n", arg1);
235 }
236 else if (strcmp (entry, "shared") == 0)
237 {
238 for (cnt = 0; cnt < lastdb; ++cnt)
239 if (strcmp (arg1, dbnames[cnt]) == 0)
240 {
241 if (strcmp (arg2, "no") == 0)
242 dbs[cnt].shared = 0;
243 else if (strcmp (arg2, "yes") == 0)
244 dbs[cnt].shared = 1;
245 break;
246 }
247 if (cnt == lastdb)
248 dbg_log ("database %s is not supported\n", arg1);
249 }
250 else if (strcmp (entry, "reload-count") == 0)
251 {
252 if (strcasecmp (arg1, "unlimited") == 0)
253 reload_count = UINT_MAX;
254 else
255 {
256 unsigned int count = strtoul (arg1, NULL, 0);
257 if (count > UINT8_MAX - 1)
258 reload_count = UINT_MAX;
259 else if (count >= 0)
260 reload_count = count;
261 else
262 dbg_log (_("invalid value for 'reload-count': %u"), count);
263 }
264 }
265 else if (strcmp (entry, "paranoia") == 0)
266 {
267 if (strcmp (arg1, "no") == 0)
268 paranoia = 0;
269 else if (strcmp (arg1, "yes") == 0)
270 paranoia = 1;
271 }
272 else if (strcmp (entry, "restart-interval") == 0)
273 {
274 if (arg1 != NULL)
275 restart_interval = atol (arg1);
276 else
277 dbg_log (_("Must specify value for restart-interval option"));
278 }
279 else
280 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
281 }
282 while (!feof_unlocked (fp));
283
284 if (paranoia)
285 {
286 restart_time = time (NULL) + restart_interval;
287
288 /* Save the old current workding directory if we are in paranoia
289 mode. We have to change back to it. */
290 oldcwd = get_current_dir_name ();
291 if (oldcwd == NULL)
292 {
293 dbg_log (_("\
294 cannot get current working directory: %s; disabling paranoia mode"),
295 strerror (errno));
296 paranoia = 0;
297 }
298 }
299
300 /* Enforce sanity. */
301 if (max_nthreads < nthreads)
302 max_nthreads = nthreads;
303
304 for (cnt = 0; cnt < lastdb; ++cnt)
305 {
306 size_t datasize = (sizeof (struct database_pers_head)
307 + roundup (dbs[cnt].suggested_module
308 * sizeof (ref_t), ALIGN)
309 + (dbs[cnt].suggested_module
310 * DEFAULT_DATASIZE_PER_BUCKET));
311 if (datasize > dbs[cnt].max_db_size)
312 {
313 dbg_log (_("maximum file size for %s database too small"),
314 dbnames[cnt]);
315 dbs[cnt].max_db_size = datasize;
316 }
317
318 }
319
320 /* Free the buffer. */
321 free (line);
322 /* Close configuration file. */
323 fclose (fp);
324
325 return 0;
326 }