1 /* Copyright (c) 1998-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
25 #include <stdio_ext.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
36 /* Names of the databases. */
37 const char *const dbnames
[lastdb
] =
42 [servdb
] = "services",
43 [netgrdb
] = "netgroup"
48 find_db (const char *name
)
50 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
51 if (strcmp (name
, dbnames
[cnt
]) == 0)
54 error (0, 0, _("database %s is not supported"), name
);
59 nscd_parse_file (const char *fname
, struct database_dyn dbs
[lastdb
])
62 char *line
, *cp
, *entry
, *arg1
, *arg2
;
65 const unsigned int initial_error_message_count
= error_message_count
;
67 /* Open the configuration file. */
68 fp
= fopen (fname
, "r");
72 /* The stream is not used by more than one thread. */
73 (void) __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
80 ssize_t n
= getline (&line
, &len
, fp
);
83 if (line
[n
- 1] == '\n')
86 /* Because the file format does not know any form of quoting we
87 can search forward for the next '#' character and if found
88 make it terminating the line. */
89 *strchrnul (line
, '#') = '\0';
91 /* If the line is blank it is ignored. */
96 while (isspace (*entry
) && *entry
!= '\0')
99 while (!isspace (*cp
) && *cp
!= '\0')
104 if (strlen (entry
) == 0)
105 error (0, 0, _("Parse error: %s"), line
);
106 while (isspace (*arg1
) && *arg1
!= '\0')
109 while (!isspace (*cp
) && *cp
!= '\0')
114 if (strlen (arg2
) > 0)
116 while (isspace (*arg2
) && *arg2
!= '\0')
119 while (!isspace (*cp
) && *cp
!= '\0')
124 if (strcmp (entry
, "positive-time-to-live") == 0)
126 int idx
= find_db (arg1
);
128 dbs
[idx
].postimeout
= atol (arg2
);
130 else if (strcmp (entry
, "negative-time-to-live") == 0)
132 int idx
= find_db (arg1
);
134 dbs
[idx
].negtimeout
= atol (arg2
);
136 else if (strcmp (entry
, "suggested-size") == 0)
138 int idx
= find_db (arg1
);
140 dbs
[idx
].suggested_module
141 = atol (arg2
) ?: DEFAULT_SUGGESTED_MODULE
;
143 else if (strcmp (entry
, "enable-cache") == 0)
145 int idx
= find_db (arg1
);
148 if (strcmp (arg2
, "no") == 0)
149 dbs
[idx
].enabled
= 0;
150 else if (strcmp (arg2
, "yes") == 0)
151 dbs
[idx
].enabled
= 1;
154 else if (strcmp (entry
, "check-files") == 0)
156 int idx
= find_db (arg1
);
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;
165 else if (strcmp (entry
, "max-db-size") == 0)
167 int idx
= find_db (arg1
);
169 dbs
[idx
].max_db_size
= atol (arg2
) ?: DEFAULT_MAX_DB_SIZE
;
171 else if (strcmp (entry
, "logfile") == 0)
173 else if (strcmp (entry
, "debug-level") == 0)
175 int level
= atoi (arg1
);
179 else if (strcmp (entry
, "threads") == 0)
182 nthreads
= MAX (atol (arg1
), lastdb
);
184 else if (strcmp (entry
, "max-threads") == 0)
186 max_nthreads
= MAX (atol (arg1
), lastdb
);
188 else if (strcmp (entry
, "server-user") == 0)
191 error (0, 0, _("Must specify user name for server-user option"));
193 server_user
= xstrdup (arg1
);
195 else if (strcmp (entry
, "stat-user") == 0)
198 error (0, 0, _("Must specify user name for stat-user option"));
201 stat_user
= xstrdup (arg1
);
203 struct passwd
*pw
= getpwnam (stat_user
);
205 stat_uid
= pw
->pw_uid
;
208 else if (strcmp (entry
, "persistent") == 0)
210 int idx
= find_db (arg1
);
213 if (strcmp (arg2
, "no") == 0)
214 dbs
[idx
].persistent
= 0;
215 else if (strcmp (arg2
, "yes") == 0)
216 dbs
[idx
].persistent
= 1;
219 else if (strcmp (entry
, "shared") == 0)
221 int idx
= find_db (arg1
);
224 if (strcmp (arg2
, "no") == 0)
226 else if (strcmp (arg2
, "yes") == 0)
230 else if (strcmp (entry
, "reload-count") == 0)
232 if (strcasecmp (arg1
, "unlimited") == 0)
233 reload_count
= UINT_MAX
;
236 unsigned long int count
= strtoul (arg1
, NULL
, 0);
237 if (count
> UINT8_MAX
- 1)
238 reload_count
= UINT_MAX
;
240 reload_count
= count
;
243 else if (strcmp (entry
, "paranoia") == 0)
245 if (strcmp (arg1
, "no") == 0)
247 else if (strcmp (arg1
, "yes") == 0)
250 else if (strcmp (entry
, "restart-interval") == 0)
253 restart_interval
= atol (arg1
);
255 error (0, 0, _("Must specify value for restart-interval option"));
257 else if (strcmp (entry
, "auto-propagate") == 0)
259 int idx
= find_db (arg1
);
262 if (strcmp (arg2
, "no") == 0)
263 dbs
[idx
].propagate
= 0;
264 else if (strcmp (arg2
, "yes") == 0)
265 dbs
[idx
].propagate
= 1;
269 error (0, 0, _("Unknown option: %s %s %s"), entry
, arg1
, arg2
);
271 while (!feof_unlocked (fp
));
275 restart_time
= time (NULL
) + restart_interval
;
277 /* Save the old current workding directory if we are in paranoia
278 mode. We have to change back to it. */
279 oldcwd
= get_current_dir_name ();
283 cannot get current working directory: %s; disabling paranoia mode"),
289 /* Enforce sanity. */
290 if (max_nthreads
< nthreads
)
291 max_nthreads
= nthreads
;
293 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
295 size_t datasize
= (sizeof (struct database_pers_head
)
296 + roundup (dbs
[cnt
].suggested_module
297 * sizeof (ref_t
), ALIGN
)
298 + (dbs
[cnt
].suggested_module
299 * DEFAULT_DATASIZE_PER_BUCKET
));
300 if (datasize
> dbs
[cnt
].max_db_size
)
302 error (0, 0, _("maximum file size for %s database too small"),
304 dbs
[cnt
].max_db_size
= datasize
;
309 /* Free the buffer. */
311 /* Close configuration file. */
314 return error_message_count
!= initial_error_message_count
;