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