2 * $Id: afp_dsi.c,v 1.53 2010-03-30 12:55:26 franklahm Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
8 * modified from main.c. this handles afp over tcp.
13 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
25 #ifdef HAVE_SYS_STAT_H
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <atalk/logger.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
44 #endif /* FORCE_UIDGID */
46 #define CHILD_DIE (1 << 0)
47 #define CHILD_RUNNING (1 << 1)
48 #define CHILD_SLEEPING (1 << 2)
49 #define CHILD_DATA (1 << 3)
52 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
53 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
54 * but are deeply nested in the function chain with the caller already without acces to it.
55 * Changing this would require adding a reference to the caller which itself might be
56 * called in many places (eg acltoownermode is called from accessmode).
57 * The only sane way out is providing a copy of it here:
59 AFPObj *AFPobj = NULL;
68 static void afp_dsi_close(AFPObj *obj)
70 DSI *dsi = obj->handle;
72 /* we may have been called from a signal handler caught when afpd was running
73 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
74 * restore our login user
76 if (geteuid() != obj->uid) {
77 if (seteuid( obj->uid ) < 0) {
78 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
79 obj->uid, strerror(errno), getuid(), geteuid());
88 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
89 dsi->read_count/1024.0, dsi->write_count/1024.0);
94 /* -------------------------------
96 * a little bit of code duplication.
98 static void afp_dsi_die(int sig)
100 static volatile int in_handler;
105 /* it's not atomic but we don't care because it's an exit function
106 * ie if a signal is received here, between the test and the affectation,
107 * it will not return.
111 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
112 afp_dsi_close(child.obj);
113 if (sig) /* if no signal, assume dieing because logins are disabled &
114 don't log it (maintenance mode)*/
115 LOG(log_info, logtype_afpd, "Connection terminated");
116 if (sig == SIGTERM || sig == SIGALRM) {
125 static void afp_dsi_sleep(void)
127 child.flags |= CHILD_SLEEPING;
128 dsi_sleep(child.obj->handle, 1);
131 /* ------------------- */
132 static void afp_dsi_timedown(int sig _U_)
137 child.flags |= CHILD_DIE;
138 /* shutdown and don't reconnect. server going down in 5 minutes. */
139 setmessage("The server is going down for maintenance.");
140 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
141 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
142 DSI *dsi = (DSI *) child.obj->handle;
143 dsi->down_request = 1;
146 it.it_interval.tv_sec = 0;
147 it.it_interval.tv_usec = 0;
148 it.it_value.tv_sec = 300;
149 it.it_value.tv_usec = 0;
151 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
152 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
153 afp_dsi_die(EXITERR_SYS);
155 memset(&sv, 0, sizeof(sv));
156 sv.sa_handler = afp_dsi_die;
157 sigemptyset( &sv.sa_mask );
158 sigaddset(&sv.sa_mask, SIGHUP);
159 sigaddset(&sv.sa_mask, SIGTERM);
160 sv.sa_flags = SA_RESTART;
161 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
162 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
163 afp_dsi_die(EXITERR_SYS);
167 sv.sa_handler = SIG_IGN;
168 sigemptyset( &sv.sa_mask );
169 sv.sa_flags = SA_RESTART;
170 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
171 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
172 afp_dsi_die(EXITERR_SYS);
176 /* ---------------------------------
177 * SIGHUP reload configuration file
178 * FIXME here or we wait ?
180 volatile int reload_request = 0;
182 static void afp_dsi_reload(int sig _U_)
187 /* ---------------------------------
188 * SIGINT: enable max_debug LOGging
190 static volatile sig_atomic_t debug_request = 0;
192 static void afp_dsi_debug(int sig _U_)
197 /* ---------------------- */
199 static void afp_dsi_getmesg (int sig _U_)
201 DSI *dsi = (DSI *) child.obj->handle;
203 dsi->msg_request = 1;
204 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
205 dsi->msg_request = 2;
207 #endif /* SERVERTEXT */
209 static void alarm_handler(int sig _U_)
212 DSI *dsi = (DSI *) child.obj->handle;
214 /* we have to restart the timer because some libraries
216 setitimer(ITIMER_REAL, &dsi->timer, NULL);
218 /* we got some traffic from the client since the previous timer
220 if ((child.flags & CHILD_DATA)) {
221 child.flags &= ~CHILD_DATA;
225 /* if we're in the midst of processing something,
227 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
231 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
232 if (!(err = pollvoltime(child.obj)))
233 err = dsi_tickle(child.obj->handle);
235 afp_dsi_die(EXITERR_CLNT);
237 } else { /* didn't receive a tickle. close connection */
238 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
239 afp_dsi_die(EXITERR_CLNT);
244 if dsi->in_write is set attention, tickle (and close?) msg
245 aren't sent. We don't care about tickle
247 static void pending_request(DSI *dsi)
249 /* send pending attention */
251 /* read msg if any, it could be done in afp_getsrvrmesg */
252 if (dsi->msg_request) {
253 if (dsi->msg_request == 2) {
254 /* didn't send it in signal handler */
255 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
257 dsi->msg_request = 0;
258 readmessage(child.obj);
260 if (dsi->down_request) {
261 dsi->down_request = 0;
262 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
263 AFPATTN_MESG | AFPATTN_TIME(5));
267 /* -------------------------------------------
268 afp over dsi. this never returns.
270 void afp_over_dsi(AFPObj *obj)
272 DSI *dsi = (DSI *) obj->handle;
275 struct sigaction action;
278 obj->exit = afp_dsi_die;
279 obj->reply = (int (*)()) dsi_cmdreply;
280 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
282 obj->sleep = afp_dsi_sleep;
284 child.tickle = child.flags = 0;
286 memset(&action, 0, sizeof(action));
289 action.sa_handler = afp_dsi_reload;
290 sigemptyset( &action.sa_mask );
291 sigaddset(&action.sa_mask, SIGALRM);
292 sigaddset(&action.sa_mask, SIGTERM);
293 sigaddset(&action.sa_mask, SIGUSR1);
294 sigaddset(&action.sa_mask, SIGINT);
296 sigaddset(&action.sa_mask, SIGUSR2);
298 action.sa_flags = SA_RESTART;
299 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
300 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
301 afp_dsi_die(EXITERR_SYS);
304 /* install SIGTERM */
305 action.sa_handler = afp_dsi_die;
306 sigemptyset( &action.sa_mask );
307 sigaddset(&action.sa_mask, SIGALRM);
308 sigaddset(&action.sa_mask, SIGHUP);
309 sigaddset(&action.sa_mask, SIGUSR1);
310 sigaddset(&action.sa_mask, SIGINT);
312 sigaddset(&action.sa_mask, SIGUSR2);
314 action.sa_flags = SA_RESTART;
315 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
316 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
317 afp_dsi_die(EXITERR_SYS);
321 /* Added for server message support */
322 action.sa_handler = afp_dsi_getmesg;
323 sigemptyset( &action.sa_mask );
324 sigaddset(&action.sa_mask, SIGALRM);
325 sigaddset(&action.sa_mask, SIGTERM);
326 sigaddset(&action.sa_mask, SIGUSR1);
327 sigaddset(&action.sa_mask, SIGHUP);
328 sigaddset(&action.sa_mask, SIGINT);
329 action.sa_flags = SA_RESTART;
330 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
331 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
332 afp_dsi_die(EXITERR_SYS);
334 #endif /* SERVERTEXT */
336 /* SIGUSR1 - set down in 5 minutes */
337 action.sa_handler = afp_dsi_timedown;
338 sigemptyset( &action.sa_mask );
339 sigaddset(&action.sa_mask, SIGALRM);
340 sigaddset(&action.sa_mask, SIGHUP);
341 sigaddset(&action.sa_mask, SIGTERM);
342 sigaddset(&action.sa_mask, SIGINT);
344 sigaddset(&action.sa_mask, SIGUSR2);
346 action.sa_flags = SA_RESTART;
347 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
348 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
349 afp_dsi_die(EXITERR_SYS);
352 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
353 action.sa_handler = afp_dsi_debug;
354 sigfillset( &action.sa_mask );
355 action.sa_flags = SA_RESTART;
356 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
357 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
358 afp_dsi_die(EXITERR_SYS);
363 action.sa_handler = alarm_handler;
364 sigemptyset(&action.sa_mask);
365 sigaddset(&action.sa_mask, SIGHUP);
366 sigaddset(&action.sa_mask, SIGTERM);
367 sigaddset(&action.sa_mask, SIGUSR1);
368 sigaddset(&action.sa_mask, SIGINT);
370 sigaddset(&action.sa_mask, SIGUSR2);
372 action.sa_flags = SA_RESTART;
373 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
374 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
375 afp_dsi_die(EXITERR_SYS);
377 #endif /* DEBUGGING */
379 /* get stuck here until the end */
380 while ((cmd = dsi_receive(dsi))) {
382 child.flags &= ~CHILD_SLEEPING;
383 dsi_sleep(dsi, 0); /* wake up */
385 if (reload_request) {
387 load_volumes(child.obj);
394 /* The first SIGINT enables debugging, the second one kills us */
395 action.sa_handler = afp_dsi_die;
396 sigfillset( &action.sa_mask );
397 action.sa_flags = SA_RESTART;
398 if ( sigaction( SIGINT, &action, NULL ) < 0 ) {
399 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
400 afp_dsi_die(EXITERR_SYS);
403 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
407 if (cmd == DSIFUNC_TICKLE) {
408 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
409 if ((child.flags & CHILD_DIE))
411 pending_request(dsi);
415 child.flags |= CHILD_DATA;
419 LOG(log_info, logtype_afpd, "done");
426 if ( flushfork( writtenfork ) < 0 ) {
427 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
433 function = (u_char) dsi->commands[0];
435 /* send off an afp command. in a couple cases, we take advantage
436 * of the fact that we're a stream-based protocol. */
437 if (afp_switch[function]) {
438 dsi->datalen = DSI_DATASIZ;
439 child.flags |= CHILD_RUNNING;
441 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
443 err = (*afp_switch[function])(obj,
444 (char *)&dsi->commands, dsi->cmdlen,
445 (char *)&dsi->data, &dsi->datalen);
447 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
448 AfpNum2name(function), AfpErr2name(err));
450 /* bring everything back to old euid, egid */
452 restore_uidgid ( &obj->uidgid );
453 #endif /* FORCE_UIDGID */
454 child.flags &= ~CHILD_RUNNING;
456 LOG(log_error, logtype_afpd, "bad function %X", function);
461 /* single shot toggle that gets set by dsi_readinit. */
467 if (!dsi_cmdreply(dsi, err)) {
468 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
469 afp_dsi_die(EXITERR_CLNT);
473 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
474 function = (u_char) dsi->commands[0];
475 if ( afp_switch[ function ] != NULL ) {
476 dsi->datalen = DSI_DATASIZ;
477 child.flags |= CHILD_RUNNING;
479 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
481 err = (*afp_switch[function])(obj,
482 (char *)&dsi->commands, dsi->cmdlen,
483 (char *)&dsi->data, &dsi->datalen);
485 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
486 AfpNum2name(function), AfpErr2name(err));
488 child.flags &= ~CHILD_RUNNING;
490 /* bring everything back to old euid, egid */
492 restore_uidgid ( &obj->uidgid );
493 #endif /* FORCE_UIDGID */
495 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
500 if (!dsi_wrtreply(dsi, err)) {
501 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
502 afp_dsi_die(EXITERR_CLNT);
506 case DSIFUNC_ATTN: /* attention replies */
509 /* error. this usually implies a mismatch of some kind
510 * between server and client. if things are correct,
511 * we need to flush the rest of the packet if necessary. */
513 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
514 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
518 pending_request(dsi);
522 afp_dsi_die(EXITERR_CLNT);