]> sourceware.org Git - glibc.git/blob - nscd/nscd_conf.c
* nis/nss_nis/nis-service.c (_nss_nis_getservbyname_r): Correct
[glibc.git] / nscd / nscd_conf.c
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.
4
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.
8
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.
13
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. */
17
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>
31
32 #include "dbg_log.h"
33 #include "nscd.h"
34
35 /* Wrapper functions with error checking for standard functions. */
36 extern char *xstrdup (const char *s);
37
38
39 /* Names of the databases. */
40 const char *const dbnames[lastdb] =
41 {
42 [pwddb] = "passwd",
43 [grpdb] = "group",
44 [hstdb] = "hosts",
45 [servdb] = "services"
46 };
47
48
49 static int
50 find_db (const char *name)
51 {
52 for (int cnt = 0; cnt < lastdb; ++cnt)
53 if (strcmp (name, dbnames[cnt]) == 0)
54 return cnt;
55
56 error (0, 0, _("database %s is not supported"), name);
57 return -1;
58 }
59
60 int
61 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
62 {
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;
68
69 /* Open the configuration file. */
70 fp = fopen (fname, "r");
71 if (fp == NULL)
72 return -1;
73
74 /* The stream is not used by more than one thread. */
75 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
76
77 line = NULL;
78 len = 0;
79
80 do
81 {
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';
87
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';
92
93 /* If the line is blank it is ignored. */
94 if (line[0] == '\0')
95 continue;
96
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)
117 {
118 while (isspace (*arg2) && *arg2 != '\0')
119 ++arg2;
120 cp = arg2;
121 while (!isspace (*cp) && *cp != '\0')
122 ++cp;
123 *cp = '\0';
124 }
125
126 if (strcmp (entry, "positive-time-to-live") == 0)
127 {
128 int idx = find_db (arg1);
129 if (idx >= 0)
130 dbs[idx].postimeout = atol (arg2);
131 }
132 else if (strcmp (entry, "negative-time-to-live") == 0)
133 {
134 int idx = find_db (arg1);
135 if (idx >= 0)
136 dbs[idx].negtimeout = atol (arg2);
137 }
138 else if (strcmp (entry, "suggested-size") == 0)
139 {
140 int idx = find_db (arg1);
141 if (idx >= 0)
142 dbs[idx].suggested_module = atol (arg2);
143 }
144 else if (strcmp (entry, "enable-cache") == 0)
145 {
146 int idx = find_db (arg1);
147 if (idx >= 0)
148 {
149 if (strcmp (arg2, "no") == 0)
150 dbs[idx].enabled = 0;
151 else if (strcmp (arg2, "yes") == 0)
152 dbs[idx].enabled = 1;
153 }
154 }
155 else if (strcmp (entry, "check-files") == 0)
156 {
157 int idx = find_db (arg1);
158 if (idx >= 0)
159 {
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;
164 }
165 }
166 else if (strcmp (entry, "max-db-size") == 0)
167 {
168 int idx = find_db (arg1);
169 if (idx >= 0)
170 dbs[idx].max_db_size = atol (arg2);
171 }
172 else if (strcmp (entry, "logfile") == 0)
173 set_logfile (arg1);
174 else if (strcmp (entry, "debug-level") == 0)
175 {
176 int level = atoi (arg1);
177 if (level > 0)
178 debug_level = level;
179 }
180 else if (strcmp (entry, "threads") == 0)
181 {
182 if (nthreads == -1)
183 nthreads = MAX (atol (arg1), lastdb);
184 }
185 else if (strcmp (entry, "max-threads") == 0)
186 {
187 max_nthreads = MAX (atol (arg1), lastdb);
188 }
189 else if (strcmp (entry, "server-user") == 0)
190 {
191 if (!arg1)
192 error (0, 0, _("Must specify user name for server-user option"));
193 else
194 server_user = xstrdup (arg1);
195 }
196 else if (strcmp (entry, "stat-user") == 0)
197 {
198 if (arg1 == NULL)
199 error (0, 0, _("Must specify user name for stat-user option"));
200 else
201 {
202 stat_user = xstrdup (arg1);
203
204 struct passwd *pw = getpwnam (stat_user);
205 if (pw != NULL)
206 stat_uid = pw->pw_uid;
207 }
208 }
209 else if (strcmp (entry, "persistent") == 0)
210 {
211 int idx = find_db (arg1);
212 if (idx >= 0)
213 {
214 if (strcmp (arg2, "no") == 0)
215 dbs[idx].persistent = 0;
216 else if (strcmp (arg2, "yes") == 0)
217 dbs[idx].persistent = 1;
218 }
219 }
220 else if (strcmp (entry, "shared") == 0)
221 {
222 int idx = find_db (arg1);
223 if (idx >= 0)
224 {
225 if (strcmp (arg2, "no") == 0)
226 dbs[idx].shared = 0;
227 else if (strcmp (arg2, "yes") == 0)
228 dbs[idx].shared = 1;
229 }
230 }
231 else if (strcmp (entry, "reload-count") == 0)
232 {
233 if (strcasecmp (arg1, "unlimited") == 0)
234 reload_count = UINT_MAX;
235 else
236 {
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);
244 }
245 }
246 else if (strcmp (entry, "paranoia") == 0)
247 {
248 if (strcmp (arg1, "no") == 0)
249 paranoia = 0;
250 else if (strcmp (arg1, "yes") == 0)
251 paranoia = 1;
252 }
253 else if (strcmp (entry, "restart-interval") == 0)
254 {
255 if (arg1 != NULL)
256 restart_interval = atol (arg1);
257 else
258 error (0, 0, _("Must specify value for restart-interval option"));
259 }
260 else if (strcmp (entry, "auto-propagate") == 0)
261 {
262 int idx = find_db (arg1);
263 if (idx >= 0)
264 {
265 if (strcmp (arg2, "no") == 0)
266 dbs[idx].propagate = 0;
267 else if (strcmp (arg2, "yes") == 0)
268 dbs[idx].propagate = 1;
269 }
270 }
271 else
272 error (0, 0, _("Unknown option: %s %s %s"), entry, arg1, arg2);
273 }
274 while (!feof_unlocked (fp));
275
276 if (paranoia)
277 {
278 restart_time = time (NULL) + restart_interval;
279
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)
284 {
285 error (0, 0, _("\
286 cannot get current working directory: %s; disabling paranoia mode"),
287 strerror (errno));
288 paranoia = 0;
289 }
290 }
291
292 /* Enforce sanity. */
293 if (max_nthreads < nthreads)
294 max_nthreads = nthreads;
295
296 for (cnt = 0; cnt < lastdb; ++cnt)
297 {
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)
304 {
305 error (0, 0, _("maximum file size for %s database too small"),
306 dbnames[cnt]);
307 dbs[cnt].max_db_size = datasize;
308 }
309
310 }
311
312 /* Free the buffer. */
313 free (line);
314 /* Close configuration file. */
315 fclose (fp);
316
317 return error_message_count != initial_error_message_count;
318 }
This page took 0.050598 seconds and 5 git commands to generate.