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 #include <pthread.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 #include <atalk/globals.h>
30 #include <atalk/dsi.h>
31
32 #define IPC_HEADERLEN 14
33 #define IPC_MAXMSGSIZE 90
34
35 typedef struct ipc_header {
36 uint16_t command;
37 pid_t child_pid;
38 uid_t uid;
39 uint32_t len;
40 char *msg;
41 int afp_socket;
42 uint16_t DSI_requestID;
43 } ipc_header_t;
44
45 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
46 "IPC_GETSESSION",
47 "IPC_STATE",
48 "IPC_VOLUMES"};
49
50 /*
51 * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
52 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
53 */
ipc_kill_token(struct ipc_header * ipc,server_child_t * children)54 static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
55 {
56 pid_t pid;
57
58 if (ipc->len != sizeof(pid_t)) {
59 return -1;
60 }
61 /* assume signals SA_RESTART set */
62 memcpy (&pid, ipc->msg, sizeof(pid_t));
63
64 return server_child_transfer_session(children,
65 pid,
66 ipc->uid,
67 ipc->afp_socket,
68 ipc->DSI_requestID);
69 }
70
71 /* ----------------- */
ipc_get_session(struct ipc_header * ipc,server_child_t * children)72 static int ipc_get_session(struct ipc_header *ipc, server_child_t *children)
73 {
74 uint32_t boottime;
75 uint32_t idlen;
76 char *clientid, *p;
77
78
79 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
80 return -1;
81
82 p = ipc->msg;
83 memcpy (&idlen, p, sizeof(idlen));
84 idlen = ntohl (idlen);
85 p += sizeof(idlen);
86
87 memcpy (&boottime, p, sizeof(boottime));
88 p += sizeof(boottime);
89
90 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
91 return -1;
92
93 if (NULL == (clientid = (char*) malloc(idlen)) )
94 return -1;
95 memcpy (clientid, p, idlen);
96
97 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
98 ipc->child_pid, ipc->uid, boottime);
99
100 server_child_kill_one_by_id(children,
101 ipc->child_pid,
102 ipc->uid,
103 idlen,
104 clientid,
105 boottime);
106
107 return 0;
108 }
109
ipc_set_state(struct ipc_header * ipc,server_child_t * children)110 static int ipc_set_state(struct ipc_header *ipc, server_child_t *children)
111 {
112 EC_INIT;
113 afp_child_t *child;
114
115 pthread_mutex_lock(&children->servch_lock);
116
117 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
118 EC_FAIL;
119
120 memcpy(&child->afpch_state, ipc->msg, sizeof(uint16_t));
121
122 EC_CLEANUP:
123 pthread_mutex_unlock(&children->servch_lock);
124 EC_EXIT;
125 }
126
ipc_set_volumes(struct ipc_header * ipc,server_child_t * children)127 static int ipc_set_volumes(struct ipc_header *ipc, server_child_t *children)
128 {
129 EC_INIT;
130 afp_child_t *child;
131
132 pthread_mutex_lock(&children->servch_lock);
133
134 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
135 EC_FAIL;
136
137 if (child->afpch_volumes) {
138 free(child->afpch_volumes);
139 child->afpch_volumes = NULL;
140 }
141 if (ipc->len)
142 child->afpch_volumes = strdup(ipc->msg);
143
144 EC_CLEANUP:
145 pthread_mutex_unlock(&children->servch_lock);
146 EC_EXIT;
147 }
148
149 /***********************************************************************************
150 * Public functions
151 ***********************************************************************************/
152
153 /* -----------------
154 * Ipc format
155 * command
156 * pid
157 * uid
158 *
159 */
160
161 /*!
162 * Read a IPC message from a child
163 *
164 * This is using an fd with non-blocking IO, so EAGAIN is not an error
165 *
166 * @args children (rw) pointer to our structure with all childs
167 * @args fd (r) IPC socket with child
168 *
169 * @returns -1 on error, 0 on success
170 */
ipc_server_read(server_child_t * children,int fd)171 int ipc_server_read(server_child_t *children, int fd)
172 {
173 int ret;
174 struct ipc_header ipc;
175 char buf[IPC_MAXMSGSIZE], *p;
176
177 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
178 if (ret != 0) {
179 if (errno == EAGAIN)
180 return 0;
181 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
182 ret, IPC_HEADERLEN, strerror(errno));
183 }
184 return -1;
185 }
186
187 p = buf;
188
189 memcpy(&ipc.command, p, sizeof(ipc.command));
190 p += sizeof(ipc.command);
191
192 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
193 p += sizeof(ipc.child_pid);
194
195 memcpy(&ipc.uid, p, sizeof(ipc.uid));
196 p += sizeof(ipc.uid);
197
198 memcpy(&ipc.len, p, sizeof(ipc.len));
199
200 /* This should never happen */
201 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
202 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
203 return -1;
204 }
205
206 memset (buf, 0, IPC_MAXMSGSIZE);
207 if ( ipc.len != 0) {
208 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
209 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
210 ret, ipc.len, strerror(errno));
211 return -1;
212 }
213 }
214 ipc.msg = buf;
215
216 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
217 ipc_cmd_str[ipc.command], ipc.child_pid);
218
219 switch (ipc.command) {
220
221 case IPC_DISCOLDSESSION:
222 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
223 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
224 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
225 return -1;
226 }
227 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
228 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
229 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
230 return -1;
231 }
232 if (ipc_kill_token(&ipc, children) == 1) {
233 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
234 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
235 ipc.child_pid);
236 kill(ipc.child_pid, SIGTERM);
237 }
238 close(ipc.afp_socket);
239 break;
240
241 case IPC_GETSESSION:
242 if (ipc_get_session(&ipc, children) != 0)
243 return -1;
244 break;
245
246 case IPC_STATE:
247 if (ipc_set_state(&ipc, children) != 0)
248 return -1;
249 break;
250
251 case IPC_VOLUMES:
252 if (ipc_set_volumes(&ipc, children) != 0)
253 return -1;
254 break;
255
256 default:
257 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
258 return -1;
259 }
260
261 return 0;
262 }
263
264 /* ----------------- */
ipc_child_write(int fd,uint16_t command,int len,void * msg)265 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
266 {
267 char block[IPC_MAXMSGSIZE], *p;
268 pid_t pid;
269 uid_t uid;
270 ssize_t ret;
271
272 p = block;
273
274 memset ( p, 0 , IPC_MAXMSGSIZE);
275 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
276 return -1;
277
278 memcpy(p, &command, sizeof(command));
279 p += sizeof(command);
280
281 pid = getpid();
282 memcpy(p, &pid, sizeof(pid_t));
283 p += sizeof(pid_t);
284
285 /* FIXME
286 * using uid is wrong. It will not disconnect if the new connection
287 * is with a different user.
288 * But we really don't want a remote kill command.
289 */
290 uid = geteuid();
291 memcpy(p, &uid, sizeof(uid_t));
292 p += sizeof(uid_t);
293
294 memcpy(p, &len, 4);
295 p += 4;
296
297 memcpy(p, msg, len);
298
299 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
300
301 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
302 return -1;
303 }
304
305 return 0;
306 }
307
ipc_child_state(AFPObj * obj,uint16_t state)308 int ipc_child_state(AFPObj *obj, uint16_t state)
309 {
310 return ipc_child_write(obj->ipc_fd, IPC_STATE, sizeof(uint16_t), &state);
311 }
312