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