]> 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 <errno.h>
18 #include <signal.h>
19
20 #include <atalk/server_child.h>
21 #include <atalk/server_ipc.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
24
25 typedef struct ipc_header {
26         uint16_t command;
27     pid_t        child_pid;
28     uid_t    uid;
29     uint32_t len;
30         char     *msg;
31     int      afp_socket;
32     uint16_t DSI_requestID;
33 } ipc_header_t;
34
35 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
36                                "IPC_GETSESSION"};
37
38 /*
39  * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
40  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
41  */
42 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
43 {
44     pid_t pid;
45
46     if (ipc->len != sizeof(pid_t)) {
47         return -1;
48     }
49     /* assume signals SA_RESTART set */
50     memcpy (&pid, ipc->msg, sizeof(pid_t));
51
52     return server_child_transfer_session(children,
53                                          CHILD_DSIFORK,
54                                          pid,
55                                          ipc->uid,
56                                          ipc->afp_socket,
57                                          ipc->DSI_requestID);
58 }
59
60 /* ----------------- */
61 static int ipc_get_session(struct ipc_header *ipc, server_child *children)
62 {
63     u_int32_t boottime;
64     u_int32_t idlen;
65     char     *clientid, *p;
66
67     
68     if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
69         return -1;
70
71     p = ipc->msg;
72     memcpy (&idlen, p, sizeof(idlen));
73     idlen = ntohl (idlen);
74     p += sizeof(idlen); 
75
76     memcpy (&boottime, p, sizeof(boottime));
77     p += sizeof(boottime);
78     
79     if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
80         return -1;
81
82     if (NULL == (clientid = (char*) malloc(idlen)) )
83         return -1;
84     memcpy (clientid, p, idlen);
85   
86     LOG (log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time %x)",
87          ipc->child_pid, ipc->uid, boottime); 
88
89     server_child_kill_one_by_id(children,
90                                 CHILD_DSIFORK,
91                                 ipc->child_pid,
92                                 ipc->uid,
93                                 idlen,
94                                 clientid,
95                                 boottime);
96
97     return 0;
98 }
99
100 #define IPC_HEADERLEN 14
101 #define IPC_MAXMSGSIZE 90
102
103 /* ----------------- 
104  * Ipc format
105  * command
106  * pid
107  * uid
108  * 
109 */
110
111 /*!
112  * Read a IPC message from a child
113  *
114  * @args children  (rw) pointer to our structure with all childs
115  * @args fd        (r)  IPC socket with child
116  *
117  * @returns number of bytes transfered, -1 on error, 0 on EOF
118  */
119 int ipc_server_read(server_child *children, int fd)
120 {
121     int       ret = 0;
122     struct ipc_header ipc;
123     char      buf[IPC_MAXMSGSIZE], *p;
124
125     if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
126         LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
127             ret, IPC_HEADERLEN, strerror(errno));
128         return ret;
129     }
130
131     p = buf;
132
133     memcpy(&ipc.command, p, sizeof(ipc.command));
134     p += sizeof(ipc.command);
135
136     memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
137     p += sizeof(ipc.child_pid);
138
139     memcpy(&ipc.uid, p, sizeof(ipc.uid));
140     p += sizeof(ipc.uid);
141
142     memcpy(&ipc.len, p, sizeof(ipc.len));
143
144     /* This should never happen */
145     if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
146         LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
147         return -1;
148     }
149
150     memset (buf, 0, IPC_MAXMSGSIZE);
151     if ( ipc.len != 0) {
152             if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
153             LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u  bytes read): %s",
154                 ret, ipc.len, strerror(errno));
155             return ret;
156         }        
157     }
158     ipc.msg = buf;
159
160     LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
161         ipc_cmd_str[ipc.command], ipc.child_pid); 
162
163     int afp_socket;
164
165     switch (ipc.command) {
166
167         case IPC_DISCOLDSESSION:
168         if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
169             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
170                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
171         }
172         if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
173             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
174                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
175             return -1;
176         }
177                 if (ipc_kill_token(&ipc, children) == 1) {
178             /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
179             LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
180                 ipc.child_pid);
181             kill(ipc.child_pid, SIGKILL);
182         }        
183         break;
184
185         case IPC_GETSESSION:
186                 if (ipc_get_session(&ipc, children) != 0)
187             return -1;
188         break;
189
190         default:
191                 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
192                 return -1;
193     }
194
195     return ret;
196 }
197
198 /* ----------------- */
199 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
200 {
201    char block[IPC_MAXMSGSIZE], *p;
202    pid_t pid;
203    uid_t uid;
204    p = block;
205
206    memset ( p, 0 , IPC_MAXMSGSIZE);
207    if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
208        return -1;
209
210    memcpy(p, &command, sizeof(command));
211    p   += sizeof(command);
212
213    pid = getpid();
214    memcpy(p, &pid, sizeof(pid_t));
215    p += sizeof(pid_t);
216    
217    /* FIXME 
218     * using uid is wrong. It will not disconnect if the new connection
219     * is with a different user. 
220     * But we really don't want a remote kill command.
221    */
222    uid = geteuid();
223    memcpy(p, &uid, sizeof(uid_t));
224    p += sizeof(uid_t);
225
226    memcpy(p, &len, 4);
227    p += 4;
228
229    memcpy(p, msg, len);
230
231    LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
232
233    return write(fd, block, len+IPC_HEADERLEN );
234 }