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