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