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