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