]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/nscd.c
Update.
[thirdparty/glibc.git] / nscd / nscd.c
CommitLineData
d67281a7
UD
1/* Copyright (c) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20/* nscd - Name Service Cache Daemon. Caches passwd and group. */
21
4d06461a 22#include <argp.h>
d67281a7 23#include <errno.h>
4d06461a 24#include <error.h>
d67281a7
UD
25#include <libintl.h>
26#include <locale.h>
27#include <pthread.h>
28#include <pwd.h>
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <syslog.h>
4c223a7c 34#include <unistd.h>
d67281a7
UD
35#include <sys/socket.h>
36#include <sys/un.h>
37
38#include "dbg_log.h"
39#include "nscd.h"
40
41/* Get libc version number. */
42#include <version.h>
43
44#define PACKAGE _libc_intl_domainname
45
46/* Structure used by main() thread to keep track of the number of
47 active threads. Used to limit how many threads it will create
48 and under a shutdown condition to wait till all in-progress
49 requests have finished before "turning off the lights". */
50
51typedef struct
52{
53 int num_active;
54 pthread_cond_t thread_exit_cv;
55 pthread_mutex_t mutex;
56} thread_info_t;
57
58thread_info_t thread_info;
59
60int do_shutdown = 0;
61int disabled_passwd = 0;
62int disabled_group = 0;
4d06461a
UD
63int go_background = 1;
64const char *conffile = _PATH_NSCDCONF;
d67281a7
UD
65
66static void termination_handler (int signum);
67static int check_pid (const char *file);
68static int write_pid (const char *file);
d67281a7
UD
69static void handle_requests (void);
70
4d06461a
UD
71/* Name and version of program. */
72static void print_version (FILE *stream, struct argp_state *state);
73void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
74
75/* Definitions of arguments for argp functions. */
76static const struct argp_option options[] =
77{
78 { "config-file", 'f', N_("NAME"), 0,
79 N_("Read configuration data from NAME") },
80 { "debug", 'd', NULL, 0,
81 N_("Do not fork and display messages on the current tty") },
82 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
3dd90163 83 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
4d06461a
UD
84 { NULL, 0, NULL, 0, NULL }
85};
86
87/* Short description of program. */
88static const char doc[] = N_("Name Switch Cache Daemon.");
89
90/* Prototype for option handler. */
91static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
92
93/* Data structure to communicate with argp functions. */
94static struct argp argp =
95{
96 options, parse_opt, NULL, doc,
97};
98
d67281a7
UD
99int
100main (int argc, char **argv)
101{
4d06461a 102 int remaining;
d67281a7
UD
103
104 /* Set locale via LC_ALL. */
105 setlocale (LC_ALL, "");
106 /* Set the text message domain. */
107 textdomain (PACKAGE);
108
4d06461a
UD
109 /* Parse and process arguments. */
110 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
111
14e9dd67 112 if (remaining != argc)
d67281a7 113 {
4d06461a
UD
114 error (0, 0, gettext ("wrong number of arguments"));
115 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
116 exit (EXIT_FAILURE);
d67281a7
UD
117 }
118
119 signal (SIGINT, termination_handler);
120 signal (SIGQUIT, termination_handler);
121 signal (SIGTERM, termination_handler);
eb13b9a0 122 signal (SIGPIPE, SIG_IGN);
d67281a7
UD
123
124 /* Check if we are already running. */
125 if (check_pid (_PATH_NSCDPID))
126 {
127 fputs (_("already running"), stderr);
128 exit (EXIT_FAILURE);
129 }
130
131 /* Behave like a daemon. */
132 if (go_background)
133 {
134 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
135
136 if (daemon (0, 0) < 0)
137 {
138 fprintf (stderr, _("connot auto-background: %s\n"),
139 strerror (errno));
140 exit (EXIT_FAILURE);
141 }
142 if (write_pid (_PATH_NSCDPID) < 0)
143 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
144
145 /* Ignore job control signals */
146 signal (SIGTTOU, SIG_IGN);
147 signal (SIGTTIN, SIG_IGN);
148 signal (SIGTSTP, SIG_IGN);
149 }
150 /* Cleanup files created by a previous `bind' */
151 unlink (_PATH_NSCDSOCKET);
152
153 nscd_parse_file (conffile);
154
155 /* Create first sockets */
156 init_sockets ();
157 /* Init databases */
14e9dd67
UD
158 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
159 {
160 fputs (_("Not enough memory\n"), stderr);
161 return 1;
162 }
d67281a7
UD
163 /* Handle incoming requests */
164 handle_requests ();
165
166 return 0;
167}
168
4d06461a
UD
169
170/* Handle program arguments. */
171static error_t
172parse_opt (int key, char *arg, struct argp_state *state)
173{
174 switch (key)
175 {
176 case 'd':
177 debug_flag = 1;
178 go_background = 0;
179 break;
180 case 'f':
181 conffile = arg;
182 break;
183 case 'K':
184 if (getuid () != 0)
185 {
186 printf (_("Only root is allowed to use this option!\n\n"));
187 exit (EXIT_FAILURE);
188 }
189 {
190 int sock = __nscd_open_socket ();
191 request_header req;
192 ssize_t nbytes;
193
194 if (sock == -1)
195 exit (EXIT_FAILURE);
196
197 req.version = NSCD_VERSION;
198 req.type = SHUTDOWN;
199 req.key_len = 0;
200 nbytes = write (sock, &req, sizeof (request_header));
201 close (sock);
202 if (nbytes != req.key_len)
203 exit (EXIT_FAILURE);
204 else
205 exit (EXIT_SUCCESS);
206 }
207 case 'g':
208 print_stat ();
209 exit (EXIT_SUCCESS);
210 default:
211 return ARGP_ERR_UNKNOWN;
212 }
213 return 0;
214}
215
216/* Print the version information. */
217static void
218print_version (FILE *stream, struct argp_state *state)
219{
220 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
221 fprintf (stream, gettext ("\
222Copyright (C) %s Free Software Foundation, Inc.\n\
223This is free software; see the source for copying conditions. There is NO\n\
224warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
225"), "1998");
226 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
227}
228
229
d67281a7
UD
230/* Create a socket connected to a name. */
231int
232__nscd_open_socket (void)
233{
234 struct sockaddr_un addr;
235 int sock;
236
237 sock = socket (PF_UNIX, SOCK_STREAM, 0);
238 if (sock < 0)
239 return -1;
240
241 addr.sun_family = AF_UNIX;
242 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
243 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
244 {
245 close (sock);
246 return -1;
247 }
248
249 return sock;
250}
251
252/* Cleanup. */
253static void
254termination_handler (int signum)
255{
256 close_sockets ();
257
258 /* Clean up the files created by `bind'. */
259 unlink (_PATH_NSCDSOCKET);
260
261 /* Clean up pid file. */
262 unlink (_PATH_NSCDPID);
263
264 exit (EXIT_SUCCESS);
265}
266
d67281a7
UD
267/* Returns 1 if the process in pid file FILE is running, 0 if not. */
268static int
269check_pid (const char *file)
270{
271 FILE *fp;
272
273 fp = fopen (file, "r");
274 if (fp)
275 {
276 pid_t pid;
277
278 fscanf (fp, "%d", &pid);
279 fclose (fp);
280
281 if (kill (pid, 0) == 0)
282 return 1;
283 }
284
285 return 0;
286}
287
288/* Write the current process id to the file FILE.
289 Returns 0 if successful, -1 if not. */
290static int
291write_pid (const char *file)
292{
293 FILE *fp;
294
295 fp = fopen (file, "w");
296 if (fp == NULL)
297 return -1;
298
299 fprintf (fp, "%d\n", getpid ());
300 if (ferror (fp))
301 return -1;
302
303 fclose (fp);
304
305 return 0;
306}
307
308/* Type of the lookup function for netname2user. */
309typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
310 char *buffer, size_t buflen);
311
ba9234d9 312/* Handle incoming requests. */
d67281a7
UD
313static
314void handle_requests (void)
315{
316 request_header req;
317 int conn; /* Handle on which connection (client) the request came from. */
318 int done = 0;
319 char *key;
ba9234d9
UD
320 pthread_attr_t th_attr;
321
322 /* We will create all threads detached. Therefore prepare an attribute
323 now. */
324 pthread_attr_init (&th_attr);
325 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
d67281a7
UD
326
327 while (!done)
328 {
329 key = NULL;
330 get_request (&conn, &req, &key);
331 if (debug_flag)
332 dbg_log (_("handle_requests: request received (Version = %d)"),
333 req.version);
334 switch (req.type)
335 {
336 case GETPWBYNAME:
337 {
338 param_t *param = malloc (sizeof (param_t));
339 pthread_t thread;
ba9234d9 340 int status;
d67281a7
UD
341
342 if (debug_flag)
343 dbg_log ("\tGETPWBYNAME (%s)", key);
344 param->key = key;
345 param->conn = conn;
346 if (disabled_passwd)
ba9234d9
UD
347 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
348 (void *)param);
d67281a7 349 else
ba9234d9
UD
350 status = pthread_create (&thread, &th_attr, cache_getpwnam,
351 (void *)param);
352 if (status != 0)
353 {
354 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
355 close_socket (conn);
356 }
d67281a7
UD
357 pthread_detach (thread);
358 }
359 break;
360 case GETPWBYUID:
361 {
362 param_t *param = malloc (sizeof (param_t));
363 pthread_t thread;
ba9234d9 364 int status;
d67281a7
UD
365
366 if (debug_flag)
367 dbg_log ("\tGETPWBYUID (%s)", key);
368 param->key = key;
369 param->conn = conn;
370 if (disabled_passwd)
ba9234d9
UD
371 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
372 (void *)param);
d67281a7 373 else
ba9234d9
UD
374 status = pthread_create (&thread, &th_attr, cache_getpwuid,
375 (void *)param);
376 if (status != 0)
377 {
378 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
379 close_socket (conn);
380 }
d67281a7
UD
381 }
382 break;
383 case GETGRBYNAME:
384 {
385 param_t *param = malloc (sizeof (param_t));
386 pthread_t thread;
ba9234d9 387 int status;
d67281a7
UD
388
389 if (debug_flag)
390 dbg_log ("\tGETGRBYNAME (%s)", key);
391 param->key = key;
392 param->conn = conn;
393 if (disabled_group)
ba9234d9
UD
394 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
395 (void *)param);
d67281a7 396 else
ba9234d9
UD
397 status = pthread_create (&thread, &th_attr, cache_getgrnam,
398 (void *)param);
399 if (status != 0)
400 {
401 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
402 close_socket (conn);
403 }
d67281a7
UD
404 }
405 break;
406 case GETGRBYGID:
407 {
408 param_t *param = malloc (sizeof (param_t));
409 pthread_t thread;
ba9234d9 410 int status;
d67281a7
UD
411
412 if (debug_flag)
413 dbg_log ("\tGETGRBYGID (%s)", key);
414 param->key = key;
415 param->conn = conn;
416 if (disabled_group)
ba9234d9
UD
417 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
418 (void *)param);
d67281a7 419 else
ba9234d9
UD
420 status = pthread_create (&thread, &th_attr, cache_getgrgid,
421 (void *)param);
422 if (status != 0)
423 {
424 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
425 close_socket (conn);
426 }
d67281a7
UD
427 }
428 break;
429 case GETHOSTBYNAME:
430 /* Not yetimplemented. */
431 close_socket (conn);
432 break;
433 case GETHOSTBYADDR:
434 /* Not yet implemented. */
435 close_socket (conn);
436 break;
437 case SHUTDOWN:
438 do_shutdown = 1;
439 close_socket (0);
440 close_socket (conn);
441 /* Clean up the files created by `bind'. */
442 unlink (_PATH_NSCDSOCKET);
443 /* Clean up pid file. */
444 unlink (_PATH_NSCDPID);
445 done = 1;
446 break;
447 case GETSTAT:
448 {
449 stat_response_header resp;
450
451 if (debug_flag)
452 dbg_log ("\tGETSTAT");
453
454 get_pw_stat (&resp);
455 get_gr_stat (&resp);
456 resp.debug_level = debug_flag;
457 resp.pw_enabled = !disabled_passwd;
458 resp.gr_enabled = !disabled_group;
459
460 stat_send (conn, &resp);
461
462 close_socket (conn);
463 }
464 break;
465 default:
466 dbg_log (_("Unknown request (%d)"), req.type);
467 break;
468 }
469 }
ba9234d9
UD
470
471 pthread_attr_destroy (&th_attr);
d67281a7 472}