]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_ipc.c
Merge pull request #15 from xrmx/coverity1
[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_t *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                                          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_t *children)
70 {
71     uint32_t boottime;
72     uint32_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                                 ipc->child_pid,
99                                 ipc->uid,
100                                 idlen,
101                                 clientid,
102                                 boottime);
103
104     return 0;
105 }
106
107 /***********************************************************************************
108  * Public functions
109  ***********************************************************************************/
110
111 /* ----------------- 
112  * Ipc format
113  * command
114  * pid
115  * uid
116  * 
117  */
118
119 /*!
120  * Read a IPC message from a child
121  *
122  * This is using an fd with non-blocking IO, so EAGAIN is not an error
123  *
124  * @args children  (rw) pointer to our structure with all childs
125  * @args fd        (r)  IPC socket with child
126  *
127  * @returns -1 on error, 0 on success
128  */
129 int ipc_server_read(server_child_t *children, int fd)
130 {
131     int       ret;
132     struct ipc_header ipc;
133     char      buf[IPC_MAXMSGSIZE], *p;
134
135     if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
136         if (ret != 0) {
137             if (errno == EAGAIN)
138                 return 0;
139             LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
140                 ret, IPC_HEADERLEN, strerror(errno));
141         }
142         return -1;
143     }
144
145     p = buf;
146
147     memcpy(&ipc.command, p, sizeof(ipc.command));
148     p += sizeof(ipc.command);
149
150     memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
151     p += sizeof(ipc.child_pid);
152
153     memcpy(&ipc.uid, p, sizeof(ipc.uid));
154     p += sizeof(ipc.uid);
155
156     memcpy(&ipc.len, p, sizeof(ipc.len));
157
158     /* This should never happen */
159     if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
160         LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
161         return -1;
162     }
163
164     memset (buf, 0, IPC_MAXMSGSIZE);
165     if ( ipc.len != 0) {
166             if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
167             LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u  bytes read): %s",
168                 ret, ipc.len, strerror(errno));
169             return -1;
170         }        
171     }
172     ipc.msg = buf;
173
174     LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
175         ipc_cmd_str[ipc.command], ipc.child_pid); 
176
177     switch (ipc.command) {
178
179         case IPC_DISCOLDSESSION:
180         if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
181             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
182                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
183             return -1;
184         }
185         if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
186             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
187                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
188             return -1;
189         }
190                 if (ipc_kill_token(&ipc, children) == 1) {
191             /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
192             LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
193                 ipc.child_pid);
194             kill(ipc.child_pid, SIGTERM);
195         }        
196         close(ipc.afp_socket);
197         break;
198
199         case IPC_GETSESSION:
200                 if (ipc_get_session(&ipc, children) != 0)
201             return -1;
202         break;
203
204         default:
205                 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
206                 return -1;
207     }
208
209     return 0;
210 }
211
212 /* ----------------- */
213 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
214 {
215    char block[IPC_MAXMSGSIZE], *p;
216    pid_t pid;
217    uid_t uid;
218    ssize_t ret;
219
220    p = block;
221
222    memset ( p, 0 , IPC_MAXMSGSIZE);
223    if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
224        return -1;
225
226    memcpy(p, &command, sizeof(command));
227    p   += sizeof(command);
228
229    pid = getpid();
230    memcpy(p, &pid, sizeof(pid_t));
231    p += sizeof(pid_t);
232    
233    /* FIXME 
234     * using uid is wrong. It will not disconnect if the new connection
235     * is with a different user. 
236     * But we really don't want a remote kill command.
237    */
238    uid = geteuid();
239    memcpy(p, &uid, sizeof(uid_t));
240    p += sizeof(uid_t);
241
242    memcpy(p, &len, 4);
243    p += 4;
244
245    memcpy(p, msg, len);
246
247    LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
248
249    if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
250        return -1;
251    }
252
253    return 0;
254 }