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