2 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * All Rights Reserved. See COPYRIGHT.
6 * modified from main.c. this handles afp over tcp.
11 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/socket.h>
23 #ifdef HAVE_SYS_STAT_H
25 #endif /* HAVE_SYS_STAT_H */
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <atalk/logger.h>
30 #include <atalk/dsi.h>
31 #include <atalk/compat.h>
32 #include <atalk/util.h>
43 #endif /* FORCE_UIDGID */
45 #define CHILD_DIE (1 << 0)
46 #define CHILD_RUNNING (1 << 1)
47 #define CHILD_SLEEPING (1 << 2)
48 #define CHILD_DATA (1 << 3)
49 #define CHILD_DISCONNECTED (1 << 4)
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;
76 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
78 rc_elem_t replaycache[REPLAYCACHE_SIZE];
80 static void afp_dsi_close(AFPObj *obj)
82 DSI *dsi = obj->handle;
87 /* we may have been called from a signal handler caught when afpd was running
88 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
89 * restore our login user
91 if (geteuid() != obj->uid) {
92 if (seteuid( obj->uid ) < 0) {
93 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
94 obj->uid, strerror(errno), getuid(), geteuid());
103 LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
104 dsi->read_count/1024.0, dsi->write_count/1024.0);
110 /* -------------------------------
112 * a little bit of code duplication.
114 static void afp_dsi_die(int sig)
116 static volatile int in_handler;
121 /* it's not atomic but we don't care because it's an exit function
122 * ie if a signal is received here, between the test and the affectation,
123 * it will not return.
127 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
128 afp_dsi_close(child.obj);
129 if (sig) /* if no signal, assume dieing because logins are disabled &
130 don't log it (maintenance mode)*/
131 LOG(log_info, logtype_afpd, "Connection terminated");
132 if (sig == SIGTERM || sig == SIGALRM) {
140 static void afp_dsi_transfer_session(int sig _U_)
144 DSI *dsi = (DSI *)child.obj->handle;
146 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session fd");
148 if (readt(child.obj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
149 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
150 afp_dsi_close(child.obj);
154 if ((socket = recv_fd(child.obj->ipc_fd, 1)) == -1) {
155 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
156 afp_dsi_close(child.obj);
161 dsi->socket = socket;
162 dsi->header.dsi_requestID = dsiID;
163 dsi->header.dsi_len = 0;
164 dsi->header.dsi_code = AFP_OK;
165 dsi->header.dsi_command = DSIFUNC_CMD;
166 dsi->header.dsi_flags = DSIFL_REPLY;
168 if (!dsi_cmdreply(dsi, AFP_OK)) {
169 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
170 afp_dsi_close(child.obj);
175 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
177 * Now returning from this signal handler return to dsi_receive which should start
178 * reading/continuing from the connected socket that was passed via the parent from
179 * another session. The parent will terminate that session.
184 static void afp_dsi_sleep(void)
186 child.flags |= CHILD_SLEEPING;
187 dsi_sleep(child.obj->handle, 1);
190 /* ------------------- */
191 static void afp_dsi_timedown(int sig _U_)
196 child.flags |= CHILD_DIE;
197 /* shutdown and don't reconnect. server going down in 5 minutes. */
198 setmessage("The server is going down for maintenance.");
199 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
200 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
201 DSI *dsi = (DSI *) child.obj->handle;
202 dsi->down_request = 1;
205 it.it_interval.tv_sec = 0;
206 it.it_interval.tv_usec = 0;
207 it.it_value.tv_sec = 300;
208 it.it_value.tv_usec = 0;
210 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
211 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
212 afp_dsi_die(EXITERR_SYS);
214 memset(&sv, 0, sizeof(sv));
215 sv.sa_handler = afp_dsi_die;
216 sigemptyset( &sv.sa_mask );
217 sigaddset(&sv.sa_mask, SIGHUP);
218 sigaddset(&sv.sa_mask, SIGTERM);
219 sv.sa_flags = SA_RESTART;
220 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
221 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
222 afp_dsi_die(EXITERR_SYS);
226 sv.sa_handler = SIG_IGN;
227 sigemptyset( &sv.sa_mask );
228 sv.sa_flags = SA_RESTART;
229 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
230 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
231 afp_dsi_die(EXITERR_SYS);
235 /* ---------------------------------
236 * SIGHUP reload configuration file
238 volatile int reload_request = 0;
240 static void afp_dsi_reload(int sig _U_)
245 /* ---------------------------------
246 * SIGINT: enable max_debug LOGging
248 static volatile sig_atomic_t debug_request = 0;
250 static void afp_dsi_debug(int sig _U_)
255 /* ---------------------- */
257 static void afp_dsi_getmesg (int sig _U_)
259 DSI *dsi = (DSI *) child.obj->handle;
261 dsi->msg_request = 1;
262 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
263 dsi->msg_request = 2;
265 #endif /* SERVERTEXT */
267 static void alarm_handler(int sig _U_)
270 DSI *dsi = (DSI *) child.obj->handle;
272 if (child.flags & CHILD_DISCONNECTED) {
273 LOG(log_note, logtype_afpd, "afp_alarm: no reconnect within 10 hours, goodbye");
274 afp_dsi_die(EXITERR_CLNT);
277 /* we have to restart the timer because some libraries may use alarm() */
278 setitimer(ITIMER_REAL, &dsi->timer, NULL);
280 /* we got some traffic from the client since the previous timer
282 if ((child.flags & CHILD_DATA)) {
283 child.flags &= ~CHILD_DATA;
284 child.flags &= ~CHILD_DISCONNECTED;
288 /* if we're in the midst of processing something,
290 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
294 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
295 if (!(err = pollvoltime(child.obj)))
296 err = dsi_tickle(child.obj->handle);
298 afp_dsi_die(EXITERR_CLNT);
299 } else { /* didn't receive a tickle, enter disconnected state */
300 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
301 struct itimerval t = {{0, 60 * 60 * 10}, {0, 0}}; /* 10 hours */
302 setitimer(ITIMER_REAL, &t, NULL);
303 child.flags |= CHILD_DISCONNECTED;
308 if dsi->in_write is set attention, tickle (and close?) msg
309 aren't sent. We don't care about tickle
311 static void pending_request(DSI *dsi)
313 /* send pending attention */
315 /* read msg if any, it could be done in afp_getsrvrmesg */
316 if (dsi->msg_request) {
317 if (dsi->msg_request == 2) {
318 /* didn't send it in signal handler */
319 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
321 dsi->msg_request = 0;
322 readmessage(child.obj);
324 if (dsi->down_request) {
325 dsi->down_request = 0;
326 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
327 AFPATTN_MESG | AFPATTN_TIME(5));
331 /* -------------------------------------------
332 afp over dsi. this never returns.
334 void afp_over_dsi(AFPObj *obj)
336 DSI *dsi = (DSI *) obj->handle;
340 struct sigaction action;
343 obj->exit = afp_dsi_die;
344 obj->reply = (int (*)()) dsi_cmdreply;
345 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
347 obj->sleep = afp_dsi_sleep;
349 child.tickle = child.flags = 0;
351 memset(&action, 0, sizeof(action));
354 action.sa_handler = afp_dsi_reload;
355 sigemptyset( &action.sa_mask );
356 sigaddset(&action.sa_mask, SIGALRM);
357 sigaddset(&action.sa_mask, SIGTERM);
358 sigaddset(&action.sa_mask, SIGUSR1);
359 sigaddset(&action.sa_mask, SIGINT);
361 sigaddset(&action.sa_mask, SIGUSR2);
363 action.sa_flags = SA_RESTART;
364 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
365 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
366 afp_dsi_die(EXITERR_SYS);
370 action.sa_handler = afp_dsi_transfer_session;
371 sigemptyset( &action.sa_mask );
372 sigaddset(&action.sa_mask, SIGALRM);
373 sigaddset(&action.sa_mask, SIGTERM);
374 sigaddset(&action.sa_mask, SIGUSR1);
375 sigaddset(&action.sa_mask, SIGINT);
377 sigaddset(&action.sa_mask, SIGUSR2);
379 action.sa_flags = SA_RESTART;
380 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
381 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
382 afp_dsi_die(EXITERR_SYS);
385 /* install SIGTERM */
386 action.sa_handler = afp_dsi_die;
387 sigemptyset( &action.sa_mask );
388 sigaddset(&action.sa_mask, SIGALRM);
389 sigaddset(&action.sa_mask, SIGHUP);
390 sigaddset(&action.sa_mask, SIGUSR1);
391 sigaddset(&action.sa_mask, SIGINT);
393 sigaddset(&action.sa_mask, SIGUSR2);
395 action.sa_flags = SA_RESTART;
396 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
397 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
398 afp_dsi_die(EXITERR_SYS);
402 /* Added for server message support */
403 action.sa_handler = afp_dsi_getmesg;
404 sigemptyset( &action.sa_mask );
405 sigaddset(&action.sa_mask, SIGALRM);
406 sigaddset(&action.sa_mask, SIGTERM);
407 sigaddset(&action.sa_mask, SIGUSR1);
408 sigaddset(&action.sa_mask, SIGHUP);
409 sigaddset(&action.sa_mask, SIGINT);
410 action.sa_flags = SA_RESTART;
411 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
412 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
413 afp_dsi_die(EXITERR_SYS);
415 #endif /* SERVERTEXT */
417 /* SIGUSR1 - set down in 5 minutes */
418 action.sa_handler = afp_dsi_timedown;
419 sigemptyset( &action.sa_mask );
420 sigaddset(&action.sa_mask, SIGALRM);
421 sigaddset(&action.sa_mask, SIGHUP);
422 sigaddset(&action.sa_mask, SIGTERM);
423 sigaddset(&action.sa_mask, SIGINT);
425 sigaddset(&action.sa_mask, SIGUSR2);
427 action.sa_flags = SA_RESTART;
428 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
429 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
430 afp_dsi_die(EXITERR_SYS);
433 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
434 action.sa_handler = afp_dsi_debug;
435 sigfillset( &action.sa_mask );
436 action.sa_flags = SA_RESTART;
437 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
438 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
439 afp_dsi_die(EXITERR_SYS);
444 action.sa_handler = alarm_handler;
445 sigemptyset(&action.sa_mask);
446 sigaddset(&action.sa_mask, SIGHUP);
447 sigaddset(&action.sa_mask, SIGTERM);
448 sigaddset(&action.sa_mask, SIGUSR1);
449 sigaddset(&action.sa_mask, SIGINT);
451 sigaddset(&action.sa_mask, SIGUSR2);
453 action.sa_flags = SA_RESTART;
454 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
455 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
456 afp_dsi_die(EXITERR_SYS);
458 #endif /* DEBUGGING */
460 if (dircache_init(obj->options.dircachesize) != 0)
461 afp_dsi_die(EXITERR_SYS);
463 /* get stuck here until the end */
464 while ((cmd = dsi_receive(dsi))) {
466 child.flags &= ~CHILD_SLEEPING;
467 dsi_sleep(dsi, 0); /* wake up */
469 if (reload_request) {
471 load_volumes(child.obj);
476 /* The first SIGINT enables debugging, the next restores the config */
478 static int debugging = 0;
482 if (obj->options.logconfig)
483 setuplog(obj->options.logconfig);
485 setuplog("default log_note");
490 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
495 if (cmd == DSIFUNC_TICKLE) {
496 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
497 if ((child.flags & CHILD_DIE))
499 pending_request(dsi);
503 child.flags |= CHILD_DATA;
507 LOG(log_info, logtype_afpd, "done");
514 if ( flushfork( writtenfork ) < 0 ) {
515 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
521 function = (u_char) dsi->commands[0];
523 /* AFP replay cache */
524 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
525 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
527 if (replaycache[rc_idx].DSIreqID == dsi->clientID
528 && replaycache[rc_idx].AFPcommand == function) {
529 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
530 dsi->clientID, AfpNum2name(function));
531 err = replaycache[rc_idx].result;
532 /* AFP replay cache end */
534 /* send off an afp command. in a couple cases, we take advantage
535 * of the fact that we're a stream-based protocol. */
536 if (afp_switch[function]) {
537 dsi->datalen = DSI_DATASIZ;
538 child.flags |= CHILD_RUNNING;
540 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
542 err = (*afp_switch[function])(obj,
543 (char *)&dsi->commands, dsi->cmdlen,
544 (char *)&dsi->data, &dsi->datalen);
546 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
547 AfpNum2name(function), AfpErr2name(err));
549 dir_free_invalid_q();
552 /* bring everything back to old euid, egid */
554 restore_uidgid ( &obj->uidgid );
555 #endif /* FORCE_UIDGID */
556 child.flags &= ~CHILD_RUNNING;
558 /* Add result to the AFP replay cache */
559 replaycache[rc_idx].DSIreqID = dsi->clientID;
560 replaycache[rc_idx].AFPcommand = function;
561 replaycache[rc_idx].result = err;
563 LOG(log_error, logtype_afpd, "bad function %X", function);
569 /* single shot toggle that gets set by dsi_readinit. */
575 if (!dsi_cmdreply(dsi, err)) {
576 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
577 afp_dsi_die(EXITERR_CLNT);
581 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
582 function = (u_char) dsi->commands[0];
583 if ( afp_switch[ function ] != NULL ) {
584 dsi->datalen = DSI_DATASIZ;
585 child.flags |= CHILD_RUNNING;
587 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
589 err = (*afp_switch[function])(obj,
590 (char *)&dsi->commands, dsi->cmdlen,
591 (char *)&dsi->data, &dsi->datalen);
593 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
594 AfpNum2name(function), AfpErr2name(err));
596 child.flags &= ~CHILD_RUNNING;
598 /* bring everything back to old euid, egid */
600 restore_uidgid ( &obj->uidgid );
601 #endif /* FORCE_UIDGID */
603 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
608 if (!dsi_wrtreply(dsi, err)) {
609 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
610 afp_dsi_die(EXITERR_CLNT);
614 case DSIFUNC_ATTN: /* attention replies */
617 /* error. this usually implies a mismatch of some kind
618 * between server and client. if things are correct,
619 * we need to flush the rest of the packet if necessary. */
621 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
622 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
626 pending_request(dsi);
630 afp_dsi_die(EXITERR_CLNT);