]> granicus.if.org Git - shadow/blob - lib/nscd.c
[svn-upgrade] Integrating new upstream version, shadow (4.0.4)
[shadow] / lib / nscd.c
1 /* Copyright (c) 1999 SuSE GmbH Nuerenberg, Germany
2    Author: Thorsten Kukuk <kukuk@suse.de> */
3
4 #include <assert.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/socket.h>
11 #include <sys/un.h>
12
13 /* Version number of the daemon interface */
14 #define NSCD_VERSION 2
15 /* Path for the Unix domain socket.  */
16 #define _PATH_NSCDSOCKET "/var/run/.nscd_socket"
17
18 /* Available services.  */
19 typedef enum {
20         GETPWBYNAME,
21         GETPWBYUID,
22         GETGRBYNAME,
23         GETGRBYGID,
24         GETHOSTBYNAME,
25         GETHOSTBYNAMEv6,
26         GETHOSTBYADDR,
27         GETHOSTBYADDRv6,
28         LASTDBREQ = GETHOSTBYADDRv6,
29         SHUTDOWN,               /* Shut the server down.  */
30         GETSTAT,                /* Get the server statistic.  */
31         INVALIDATE,             /* Invalidate one special cache.  */
32         LASTREQ
33 } request_type;
34
35 /* Header common to all requests */
36 typedef struct {
37         int version;            /* Version number of the daemon interface.  */
38         request_type type;      /* Service requested.  */
39 #if defined(__alpha__)
40         int64_t key_len;        /* Key length is 64bit on Alpha.  */
41 #else
42         int32_t key_len;        /* Key length, 32bit on most plattforms.  */
43 #endif
44 } request_header;
45
46 /* Create a socket connected to a name.  */
47 static int nscd_open_socket (void)
48 {
49         struct sockaddr_un addr;
50         int sock;
51
52         sock = socket (PF_UNIX, SOCK_STREAM, 0);
53         if (sock < 0)
54                 return -1;
55
56         addr.sun_family = AF_UNIX;
57         assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
58         strcpy (addr.sun_path, _PATH_NSCDSOCKET);
59         if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
60                 close (sock);
61                 return -1;
62         }
63
64         return sock;
65 }
66
67 /*
68  * nscd_flush_cache - flush specyfied service bufor in nscd cache
69  */
70 int nscd_flush_cache (char *service)
71 {
72         int sock = nscd_open_socket ();
73         request_header req;
74         ssize_t nbytes;
75
76         if (sock == -1)
77                 return -1;
78
79         req.version = NSCD_VERSION;
80         req.type = INVALIDATE;
81         req.key_len = strlen (service) + 1;
82         nbytes = write (sock, &req, sizeof (request_header));
83         if (nbytes != sizeof (request_header)) {
84                 close (sock);
85                 return -1;
86         }
87
88         nbytes = write (sock, (void *) service, req.key_len);
89
90         close (sock);
91         return (nbytes != req.key_len ? (-1) : 0);
92 }