[BZ #3273]
[platform/upstream/glibc.git] / nscd / nscd_conf.c
1 /* Copyright (c) 1998,2000,2003,2004,2005,2006 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 *dbnames[lastdb] =
41 {
42   [pwddb] = "passwd",
43   [grpdb] = "group",
44   [hstdb] = "hosts"
45 };
46
47
48 static int
49 find_db (const char *name)
50 {
51   for (int cnt = 0; cnt < lastdb; ++cnt)
52     if (strcmp (name, dbnames[cnt]) == 0)
53       return cnt;
54
55   error (0, 0, _("database %s is not supported\n"), name);
56   return -1;
57 }
58
59 int
60 nscd_parse_file (const char *fname, struct database_dyn dbs[lastdb])
61 {
62   FILE *fp;
63   char *line, *cp, *entry, *arg1, *arg2;
64   size_t len;
65   int cnt;
66   const unsigned int initial_error_message_count = error_message_count;
67
68   /* Open the configuration file.  */
69   fp = fopen (fname, "r");
70   if (fp == NULL)
71     return -1;
72
73   /* The stream is not used by more than one thread.  */
74   (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
75
76   line = NULL;
77   len = 0;
78
79   do
80     {
81       ssize_t n = getline (&line, &len, fp);
82       if (n < 0)
83         break;
84       if (line[n - 1] == '\n')
85         line[n - 1] = '\0';
86
87       /* Because the file format does not know any form of quoting we
88          can search forward for the next '#' character and if found
89          make it terminating the line.  */
90       *strchrnul (line, '#') = '\0';
91
92       /* If the line is blank it is ignored.  */
93       if (line[0] == '\0')
94         continue;
95
96       entry = line;
97       while (isspace (*entry) && *entry != '\0')
98         ++entry;
99       cp = entry;
100       while (!isspace (*cp) && *cp != '\0')
101         ++cp;
102       arg1 = cp;
103       ++arg1;
104       *cp = '\0';
105       if (strlen (entry) == 0)
106         error (0, 0, _("Parse error: %s"), line);
107       while (isspace (*arg1) && *arg1 != '\0')
108         ++arg1;
109       cp = arg1;
110       while (!isspace (*cp) && *cp != '\0')
111         ++cp;
112       arg2 = cp;
113       ++arg2;
114       *cp = '\0';
115       if (strlen (arg2) > 0)
116         {
117           while (isspace (*arg2) && *arg2 != '\0')
118             ++arg2;
119           cp = arg2;
120           while (!isspace (*cp) && *cp != '\0')
121             ++cp;
122           *cp = '\0';
123         }
124
125       if (strcmp (entry, "positive-time-to-live") == 0)
126         {
127           int idx = find_db (arg1);
128           if (idx >= 0)
129             dbs[idx].postimeout = atol (arg2);
130         }
131       else if (strcmp (entry, "negative-time-to-live") == 0)
132         {
133           int idx = find_db (arg1);
134           if (idx >= 0)
135             dbs[idx].negtimeout = atol (arg2);
136         }
137       else if (strcmp (entry, "suggested-size") == 0)
138         {
139           int idx = find_db (arg1);
140           if (idx >= 0)
141             dbs[idx].suggested_module = atol (arg2);
142         }
143       else if (strcmp (entry, "enable-cache") == 0)
144         {
145           int idx = find_db (arg1);
146           if (idx >= 0)
147             {
148               if (strcmp (arg2, "no") == 0)
149                 dbs[idx].enabled = 0;
150               else if (strcmp (arg2, "yes") == 0)
151                 dbs[idx].enabled = 1;
152             }
153         }
154       else if (strcmp (entry, "check-files") == 0)
155         {
156           int idx = find_db (arg1);
157           if (idx >= 0)
158             {
159               if (strcmp (arg2, "no") == 0)
160                 dbs[idx].check_file = 0;
161               else if (strcmp (arg2, "yes") == 0)
162                 dbs[idx].check_file = 1;
163             }
164         }
165       else if (strcmp (entry, "max-db-size") == 0)
166         {
167           int idx = find_db (arg1);
168           if (idx >= 0)
169             dbs[idx].max_db_size = atol (arg2);
170         }
171       else if (strcmp (entry, "logfile") == 0)
172         set_logfile (arg1);
173       else if (strcmp (entry, "debug-level") == 0)
174         {
175           int level = atoi (arg1);
176           if (level > 0)
177             debug_level = level;
178         }
179       else if (strcmp (entry, "threads") == 0)
180         {
181           if (nthreads == -1)
182             nthreads = MAX (atol (arg1), lastdb);
183         }
184       else if (strcmp (entry, "max-threads") == 0)
185         {
186           max_nthreads = MAX (atol (arg1), lastdb);
187         }
188       else if (strcmp (entry, "server-user") == 0)
189         {
190           if (!arg1)
191             error (0, 0, _("Must specify user name for server-user option"));
192           else
193             server_user = xstrdup (arg1);
194         }
195       else if (strcmp (entry, "stat-user") == 0)
196         {
197           if (arg1 == NULL)
198             error (0, 0, _("Must specify user name for stat-user option"));
199           else
200             {
201               stat_user = xstrdup (arg1);
202
203               struct passwd *pw = getpwnam (stat_user);
204               if (pw != NULL)
205                 stat_uid = pw->pw_uid;
206             }
207         }
208       else if (strcmp (entry, "persistent") == 0)
209         {
210           int idx = find_db (arg1);
211           if (idx >= 0)
212             {
213               if (strcmp (arg2, "no") == 0)
214                 dbs[idx].persistent = 0;
215               else if (strcmp (arg2, "yes") == 0)
216                 dbs[idx].persistent = 1;
217             }
218         }
219       else if (strcmp (entry, "shared") == 0)
220         {
221           int idx = find_db (arg1);
222           if (idx >= 0)
223             {
224               if (strcmp (arg2, "no") == 0)
225                 dbs[idx].shared = 0;
226               else if (strcmp (arg2, "yes") == 0)
227                 dbs[idx].shared = 1;
228             }
229         }
230       else if (strcmp (entry, "reload-count") == 0)
231         {
232           if (strcasecmp (arg1, "unlimited") == 0)
233             reload_count = UINT_MAX;
234           else
235             {
236               unsigned int count = strtoul (arg1, NULL, 0);
237               if (count > UINT8_MAX - 1)
238                 reload_count = UINT_MAX;
239               else if (count >= 0)
240             reload_count = count;
241               else
242                 error (0, 0, _("invalid value for 'reload-count': %u"), count);
243             }
244         }
245       else if (strcmp (entry, "paranoia") == 0)
246         {
247           if (strcmp (arg1, "no") == 0)
248             paranoia = 0;
249           else if (strcmp (arg1, "yes") == 0)
250             paranoia = 1;
251         }
252       else if (strcmp (entry, "restart-interval") == 0)
253         {
254           if (arg1 != NULL)
255             restart_interval = atol (arg1);
256           else
257             error (0, 0, _("Must specify value for restart-interval option"));
258         }
259       else if (strcmp (entry, "auto-propagate") == 0)
260         {
261           int idx = find_db (arg1);
262           if (idx >= 0)
263             {
264               if (strcmp (arg2, "no") == 0)
265                 dbs[idx].propagate = 0;
266               else if (strcmp (arg2, "yes") == 0)
267                 dbs[idx].propagate = 1;
268             }
269         }
270       else
271         error (0, 0, _("Unknown option: %s %s %s"), entry, arg1, arg2);
272     }
273   while (!feof_unlocked (fp));
274
275   if (paranoia)
276     {
277       restart_time = time (NULL) + restart_interval;
278
279       /* Save the old current workding directory if we are in paranoia
280          mode.  We have to change back to it.  */
281       oldcwd = get_current_dir_name ();
282       if (oldcwd == NULL)
283         {
284           error (0, 0, _("\
285 cannot get current working directory: %s; disabling paranoia mode"),
286                    strerror (errno));
287           paranoia = 0;
288         }
289     }
290
291   /* Enforce sanity.  */
292   if (max_nthreads < nthreads)
293     max_nthreads = nthreads;
294
295   for (cnt = 0; cnt < lastdb; ++cnt)
296     {
297       size_t datasize = (sizeof (struct database_pers_head)
298                          + roundup (dbs[cnt].suggested_module
299                                     * sizeof (ref_t), ALIGN)
300                          + (dbs[cnt].suggested_module
301                             * DEFAULT_DATASIZE_PER_BUCKET));
302       if (datasize > dbs[cnt].max_db_size)
303         {
304           error (0, 0, _("maximum file size for %s database too small"),
305                    dbnames[cnt]);
306           dbs[cnt].max_db_size = datasize;
307         }
308
309     }
310
311   /* Free the buffer.  */
312   free (line);
313   /* Close configuration file.  */
314   fclose (fp);
315
316   return error_message_count != initial_error_message_count;
317 }