]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_ipc.c
659f5a704ef779410bc8ed5160e4165d3563b5f8
[netatalk.git] / libatalk / util / server_ipc.c
1 /*
2  * All rights reserved. See COPYRIGHT.
3  *
4  * IPC over socketpair between parent and children.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif 
10
11 #include <sys/types.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <errno.h>
21 #include <signal.h>
22 #include <time.h>
23
24 #include <atalk/server_child.h>
25 #include <atalk/server_ipc.h>
26 #include <atalk/logger.h>
27 #include <atalk/util.h>
28 #include <atalk/errchk.h>
29 #include <atalk/paths.h>
30 #include <atalk/globals.h>
31 #include <atalk/dsi.h>
32
33 #define IPC_HEADERLEN 14
34 #define IPC_MAXMSGSIZE 90
35
36 typedef struct ipc_header {
37         uint16_t command;
38     pid_t        child_pid;
39     uid_t    uid;
40     uint32_t len;
41         char     *msg;
42     int      afp_socket;
43     uint16_t DSI_requestID;
44 } ipc_header_t;
45
46 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
47                                "IPC_GETSESSION"};
48
49 /*
50  * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
51  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
52  */
53 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
54 {
55     pid_t pid;
56
57     if (ipc->len != sizeof(pid_t)) {
58         return -1;
59     }
60     /* assume signals SA_RESTART set */
61     memcpy (&pid, ipc->msg, sizeof(pid_t));
62
63     return server_child_transfer_session(children,
64                                          CHILD_DSIFORK,
65                                          pid,
66                                          ipc->uid,
67                                          ipc->afp_socket,
68                                          ipc->DSI_requestID);
69 }
70
71 /* ----------------- */
72 static int ipc_get_session(struct ipc_header *ipc, server_child *children)
73 {
74     u_int32_t boottime;
75     u_int32_t idlen;
76     char     *clientid, *p;
77
78     
79     if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
80         return -1;
81
82     p = ipc->msg;
83     memcpy (&idlen, p, sizeof(idlen));
84     idlen = ntohl (idlen);
85     p += sizeof(idlen); 
86
87     memcpy (&boottime, p, sizeof(boottime));
88     p += sizeof(boottime);
89     
90     if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
91         return -1;
92
93     if (NULL == (clientid = (char*) malloc(idlen)) )
94         return -1;
95     memcpy (clientid, p, idlen);
96   
97     LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
98         ipc->child_pid, ipc->uid, boottime); 
99
100     server_child_kill_one_by_id(children,
101                                 CHILD_DSIFORK,
102                                 ipc->child_pid,
103                                 ipc->uid,
104                                 idlen,
105                                 clientid,
106                                 boottime);
107
108     return 0;
109 }
110
111 /***********************************************************************************
112  * Public functions
113  ***********************************************************************************/
114
115 /*!
116  * Listen on UNIX domain socket "name" for IPC from old sesssion
117  *
118  * @args name    (r) file name to use for UNIX domain socket
119  * @returns      socket fd, -1 on error
120  */
121 int ipc_server_uds(const char *name)
122 {
123     EC_INIT;
124     struct sockaddr_un address;
125     socklen_t address_length;
126     int fd = -1;
127
128     EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
129     EC_ZERO_LOG( setnonblock(fd, 1) );
130     unlink(name);
131     address.sun_family = AF_UNIX;
132     address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
133     EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) );
134     EC_ZERO_LOG( listen(fd, 1024) );
135
136 EC_CLEANUP:
137     if (ret != 0) {
138         return -1;
139     }
140
141     return fd;
142 }
143
144 /*!
145  * Connect to UNIX domain socket "name" for IPC with new afpd master
146  *
147  * 1. Connect
148  * 2. send pid, which establishes a child structure for us in the master
149  *
150  * @args name    (r) file name to use for UNIX domain socket
151  * @returns      socket fd, -1 on error
152  */
153 int ipc_client_uds(const char *name)
154 {
155     EC_INIT;
156     struct sockaddr_un address;
157     socklen_t address_length;
158     int fd = -1;
159     pid_t pid = getpid();
160
161     EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
162     EC_ZERO_LOG( setnonblock(fd, 1) );
163     address.sun_family = AF_UNIX;
164     address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
165
166     EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */
167     LOG(log_debug, logtype_afpd, "ipc_client_uds: connected to master");
168
169     if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
170         LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno));
171         EC_FAIL;
172     }
173
174 EC_CLEANUP:
175     if (ret != 0) {
176         return -1;
177     }
178     LOG(log_debug, logtype_afpd, "ipc_client_uds: fd: %d", fd);
179     return fd;
180 }
181
182 int reconnect_ipc(AFPObj *obj)
183 {
184     int retrycount = 0;
185
186     LOG(log_debug, logtype_afpd, "reconnect_ipc: start");
187
188     close(obj->ipc_fd);
189     obj->ipc_fd = -1;
190
191     sleep((getpid() % 5) + 15);  /* give it enough time to start */
192
193     while (retrycount++ < 10) {
194         if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) {
195             LOG(log_error, logtype_afpd, "reconnect_ipc: cant reconnect to master");
196             sleep(1);
197             continue;
198         }
199         LOG(log_debug, logtype_afpd, "reconnect_ipc: succesfull IPC reconnect");
200         return 0;
201     }
202     return -1;
203 }
204
205 /* ----------------- 
206  * Ipc format
207  * command
208  * pid
209  * uid
210  * 
211  */
212
213 /*!
214  * Read a IPC message from a child
215  *
216  * This is using an fd with non-blocking IO, so EAGAIN is not an error
217  *
218  * @args children  (rw) pointer to our structure with all childs
219  * @args fd        (r)  IPC socket with child
220  *
221  * @returns -1 on error, 0 on success
222  */
223 int ipc_server_read(server_child *children, int fd)
224 {
225     int       ret;
226     struct ipc_header ipc;
227     char      buf[IPC_MAXMSGSIZE], *p;
228
229     if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
230         if (ret != 0) {
231             if (errno == EAGAIN)
232                 return 0;
233             LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
234                 ret, IPC_HEADERLEN, strerror(errno));
235         }
236         return -1;
237     }
238
239     p = buf;
240
241     memcpy(&ipc.command, p, sizeof(ipc.command));
242     p += sizeof(ipc.command);
243
244     memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
245     p += sizeof(ipc.child_pid);
246
247     memcpy(&ipc.uid, p, sizeof(ipc.uid));
248     p += sizeof(ipc.uid);
249
250     memcpy(&ipc.len, p, sizeof(ipc.len));
251
252     /* This should never happen */
253     if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
254         LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
255         return -1;
256     }
257
258     memset (buf, 0, IPC_MAXMSGSIZE);
259     if ( ipc.len != 0) {
260             if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
261             LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u  bytes read): %s",
262                 ret, ipc.len, strerror(errno));
263             return -1;
264         }        
265     }
266     ipc.msg = buf;
267
268     LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
269         ipc_cmd_str[ipc.command], ipc.child_pid); 
270
271     int afp_socket;
272
273     switch (ipc.command) {
274
275         case IPC_DISCOLDSESSION:
276         if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
277             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
278                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
279             return -1;
280         }
281         if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
282             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
283                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
284             return -1;
285         }
286                 if (ipc_kill_token(&ipc, children) == 1) {
287             /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
288             LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
289                 ipc.child_pid);
290             kill(ipc.child_pid, SIGTERM);
291         }        
292         close(ipc.afp_socket);
293         break;
294
295         case IPC_GETSESSION:
296                 if (ipc_get_session(&ipc, children) != 0)
297             return -1;
298         break;
299
300         default:
301                 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
302                 return -1;
303     }
304
305     return 0;
306 }
307
308 /* ----------------- */
309 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
310 {
311    char block[IPC_MAXMSGSIZE], *p;
312    pid_t pid;
313    uid_t uid;
314    ssize_t ret;
315
316    p = block;
317
318    memset ( p, 0 , IPC_MAXMSGSIZE);
319    if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
320        return -1;
321
322    memcpy(p, &command, sizeof(command));
323    p   += sizeof(command);
324
325    pid = getpid();
326    memcpy(p, &pid, sizeof(pid_t));
327    p += sizeof(pid_t);
328    
329    /* FIXME 
330     * using uid is wrong. It will not disconnect if the new connection
331     * is with a different user. 
332     * But we really don't want a remote kill command.
333    */
334    uid = geteuid();
335    memcpy(p, &uid, sizeof(uid_t));
336    p += sizeof(uid_t);
337
338    memcpy(p, &len, 4);
339    p += 4;
340
341    memcpy(p, msg, len);
342
343    LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
344
345    if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
346        return -1;
347    }
348
349    return 0;
350 }
351