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>
31 #include <atalk/dsi.h>
32 #include <atalk/compat.h>
33 #include <atalk/util.h>
44 #endif /* FORCE_UIDGID */
47 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
48 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
49 * but are deeply nested in the function chain with the caller already without acces to it.
50 * Changing this would require adding a reference to the caller which itself might be
51 * called in many places (eg acltoownermode is called from accessmode).
52 * The only sane way out is providing a copy of it here:
54 AFPObj *AFPobj = NULL;
65 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
67 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
69 static sigjmp_buf recon_jmp;
70 static int oldsock = -1;
71 static void afp_dsi_close(AFPObj *obj)
73 DSI *dsi = obj->handle;
78 /* we may have been called from a signal handler caught when afpd was running
79 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
80 * restore our login user
82 if (geteuid() != obj->uid) {
83 if (seteuid( obj->uid ) < 0) {
84 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
85 obj->uid, strerror(errno), getuid(), geteuid());
94 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
95 dsi->read_count/1024.0, dsi->write_count/1024.0);
101 /* -------------------------------
103 * a little bit of code duplication.
105 static void afp_dsi_die(int sig)
107 DSI *dsi = (DSI *)AFPobj->handle;
108 static volatile int in_handler;
113 /* it's not atomic but we don't care because it's an exit function
114 * ie if a signal is received here, between the test and the affectation,
115 * it will not return.
119 if (dsi->flags & DSI_RECONINPROG) {
120 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
121 dsi->flags &= ~DSI_RECONINPROG;
122 return; /* this returns to afp_disconnect */
125 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
126 afp_dsi_close(AFPobj);
127 if (sig) /* if no signal, assume dieing because logins are disabled &
128 don't log it (maintenance mode)*/
129 LOG(log_info, logtype_afpd, "Connection terminated");
130 if (sig == SIGTERM || sig == SIGALRM) {
138 static void afp_dsi_transfer_session(int sig _U_)
142 DSI *dsi = (DSI *)AFPobj->handle;
144 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
146 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
147 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
148 afp_dsi_close(AFPobj);
152 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
153 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
154 afp_dsi_close(AFPobj);
158 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
160 dsi->socket = socket;
161 dsi->flags |= DSI_RECONSOCKET;
162 dsi->flags &= ~DSI_DISCONNECTED;
164 dsi->header.dsi_requestID = dsiID;
165 dsi->header.dsi_command = DSIFUNC_CMD;
168 * The session transfer happens in the middle of FPDisconnect old session, thus we
169 * have to send the reply now.
171 if (!dsi_cmdreply(dsi, AFP_OK)) {
172 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
173 afp_dsi_close(AFPobj);
177 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
179 * Now returning from this signal handler return to dsi_receive which should start
180 * reading/continuing from the connected socket that was passed via the parent from
181 * another session. The parent will terminate that session.
183 siglongjmp(recon_jmp, 1);
187 static void afp_dsi_sleep(void)
189 dsi_sleep(AFPobj->handle, 1);
192 /* ------------------- */
193 static void afp_dsi_timedown(int sig _U_)
197 DSI *dsi = (DSI *)AFPobj->handle;
198 dsi->flags |= DSI_DIE;
199 /* shutdown and don't reconnect. server going down in 5 minutes. */
200 setmessage("The server is going down for maintenance.");
201 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
202 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
203 DSI *dsi = (DSI *)AFPobj->handle;
204 dsi->down_request = 1;
207 it.it_interval.tv_sec = 0;
208 it.it_interval.tv_usec = 0;
209 it.it_value.tv_sec = 300;
210 it.it_value.tv_usec = 0;
212 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
213 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
214 afp_dsi_die(EXITERR_SYS);
216 memset(&sv, 0, sizeof(sv));
217 sv.sa_handler = afp_dsi_die;
218 sigemptyset( &sv.sa_mask );
219 sigaddset(&sv.sa_mask, SIGHUP);
220 sigaddset(&sv.sa_mask, SIGTERM);
221 sv.sa_flags = SA_RESTART;
222 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
223 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
224 afp_dsi_die(EXITERR_SYS);
228 sv.sa_handler = SIG_IGN;
229 sigemptyset( &sv.sa_mask );
230 sv.sa_flags = SA_RESTART;
231 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
232 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
233 afp_dsi_die(EXITERR_SYS);
237 /* ---------------------------------
238 * SIGHUP reload configuration file
240 volatile int reload_request = 0;
242 static void afp_dsi_reload(int sig _U_)
247 /* ---------------------------------
248 * SIGINT: enable max_debug LOGging
250 static volatile sig_atomic_t debug_request = 0;
252 static void afp_dsi_debug(int sig _U_)
257 /* ---------------------- */
258 static void afp_dsi_getmesg (int sig _U_)
260 DSI *dsi = (DSI *)AFPobj->handle;
262 dsi->msg_request = 1;
263 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
264 dsi->msg_request = 2;
267 static void alarm_handler(int sig _U_)
270 DSI *dsi = (DSI *)AFPobj->handle;
272 /* we have to restart the timer because some libraries may use alarm() */
273 setitimer(ITIMER_REAL, &dsi->timer, NULL);
275 /* we got some traffic from the client since the previous timer tick. */
276 if ((dsi->flags & DSI_DATA)) {
277 dsi->flags &= ~DSI_DATA;
278 dsi->flags &= ~DSI_DISCONNECTED;
284 if (dsi->flags & DSI_SLEEPING) {
285 if (dsi->tickle < AFPobj->options.sleep) {
286 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
287 afp_dsi_die(EXITERR_CLNT);
292 if (dsi->flags & DSI_DISCONNECTED) {
293 if (dsi->tickle > AFPobj->options.disconnected) {
294 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
295 afp_dsi_die(EXITERR_CLNT);
300 /* if we're in the midst of processing something, don't die. */
301 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle > AFPobj->options.timeout)) {
302 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
303 dsi->flags |= DSI_DISCONNECTED;
307 if ((err = pollvoltime(AFPobj)) == 0)
308 err = dsi_tickle(AFPobj->handle);
310 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
311 dsi->flags |= DSI_DISCONNECTED;
316 if dsi->in_write is set attention, tickle (and close?) msg
317 aren't sent. We don't care about tickle
319 static void pending_request(DSI *dsi)
321 /* send pending attention */
323 /* read msg if any, it could be done in afp_getsrvrmesg */
324 if (dsi->msg_request) {
325 if (dsi->msg_request == 2) {
326 /* didn't send it in signal handler */
327 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
329 dsi->msg_request = 0;
332 if (dsi->down_request) {
333 dsi->down_request = 0;
334 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
335 AFPATTN_MESG | AFPATTN_TIME(5));
339 /* -------------------------------------------
340 afp over dsi. this never returns.
342 void afp_over_dsi(AFPObj *obj)
344 DSI *dsi = (DSI *) obj->handle;
348 struct sigaction action;
351 obj->exit = afp_dsi_die;
352 obj->reply = (int (*)()) dsi_cmdreply;
353 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
354 obj->sleep = afp_dsi_sleep;
357 memset(&action, 0, sizeof(action));
360 action.sa_handler = afp_dsi_reload;
361 sigemptyset( &action.sa_mask );
362 sigaddset(&action.sa_mask, SIGALRM);
363 sigaddset(&action.sa_mask, SIGTERM);
364 sigaddset(&action.sa_mask, SIGUSR1);
365 sigaddset(&action.sa_mask, SIGINT);
366 sigaddset(&action.sa_mask, SIGUSR2);
367 action.sa_flags = SA_RESTART;
368 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
369 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
370 afp_dsi_die(EXITERR_SYS);
374 action.sa_handler = afp_dsi_transfer_session;
375 sigemptyset( &action.sa_mask );
376 sigaddset(&action.sa_mask, SIGALRM);
377 sigaddset(&action.sa_mask, SIGTERM);
378 sigaddset(&action.sa_mask, SIGUSR1);
379 sigaddset(&action.sa_mask, SIGINT);
380 sigaddset(&action.sa_mask, SIGUSR2);
381 action.sa_flags = SA_RESTART;
382 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
383 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
384 afp_dsi_die(EXITERR_SYS);
387 /* install SIGTERM */
388 action.sa_handler = afp_dsi_die;
389 sigemptyset( &action.sa_mask );
390 sigaddset(&action.sa_mask, SIGALRM);
391 sigaddset(&action.sa_mask, SIGHUP);
392 sigaddset(&action.sa_mask, SIGUSR1);
393 sigaddset(&action.sa_mask, SIGINT);
394 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);
401 /* Added for server message support */
402 action.sa_handler = afp_dsi_getmesg;
403 sigemptyset( &action.sa_mask );
404 sigaddset(&action.sa_mask, SIGALRM);
405 sigaddset(&action.sa_mask, SIGTERM);
406 sigaddset(&action.sa_mask, SIGUSR1);
407 sigaddset(&action.sa_mask, SIGHUP);
408 sigaddset(&action.sa_mask, SIGINT);
409 action.sa_flags = SA_RESTART;
410 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
411 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
412 afp_dsi_die(EXITERR_SYS);
415 /* SIGUSR1 - set down in 5 minutes */
416 action.sa_handler = afp_dsi_timedown;
417 sigemptyset( &action.sa_mask );
418 sigaddset(&action.sa_mask, SIGALRM);
419 sigaddset(&action.sa_mask, SIGHUP);
420 sigaddset(&action.sa_mask, SIGTERM);
421 sigaddset(&action.sa_mask, SIGINT);
422 sigaddset(&action.sa_mask, SIGUSR2);
423 action.sa_flags = SA_RESTART;
424 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
425 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
426 afp_dsi_die(EXITERR_SYS);
429 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
430 action.sa_handler = afp_dsi_debug;
431 sigfillset( &action.sa_mask );
432 action.sa_flags = SA_RESTART;
433 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
434 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
435 afp_dsi_die(EXITERR_SYS);
440 action.sa_handler = alarm_handler;
441 sigemptyset(&action.sa_mask);
442 sigaddset(&action.sa_mask, SIGHUP);
443 sigaddset(&action.sa_mask, SIGTERM);
444 sigaddset(&action.sa_mask, SIGUSR1);
445 sigaddset(&action.sa_mask, SIGINT);
446 sigaddset(&action.sa_mask, SIGUSR2);
447 action.sa_flags = SA_RESTART;
448 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
449 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
450 afp_dsi_die(EXITERR_SYS);
452 #endif /* DEBUGGING */
454 if (dircache_init(obj->options.dircachesize) != 0)
455 afp_dsi_die(EXITERR_SYS);
457 /* get stuck here until the end */
459 if (sigsetjmp(recon_jmp, 1) != 0) {
460 LOG(log_note, logtype_afpd, "Resuming operation after succesfull primary reconnect");
461 dsi->flags &= ~(DSI_RUNNING | DSI_DATA);
462 dsi->eof = dsi->start = dsi->buffer;
466 cmd = dsi_receive(dsi);
468 if (dsi->flags & DSI_RECONSOCKET) {
469 /* we just got a reconnect so we immediately try again to receive on the new fd */
470 dsi->flags &= ~DSI_RECONSOCKET;
473 /* Some error on the client connection, enter disconnected state */
474 dsi->flags |= DSI_DISCONNECTED;
475 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
476 continue; /* continue receiving until disconnect timer expires
477 * or a primary reconnect succeeds */
480 /* Now, after successfully reading from the new socket after a reconnect *
481 * we can close the old socket without taking the risk of confusing the client */
488 dsi_sleep(dsi, 0); /* wake up */
490 if (reload_request) {
492 load_volumes(AFPobj);
497 /* The first SIGINT enables debugging, the next restores the config */
499 static int debugging = 0;
503 if (obj->options.logconfig)
504 setuplog(obj->options.logconfig);
506 setuplog("default log_note");
511 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
516 if (cmd == DSIFUNC_TICKLE) {
517 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
518 if ((dsi->flags & DSI_DIE))
520 pending_request(dsi);
524 dsi->flags |= DSI_DATA;
528 LOG(log_note, logtype_afpd, "done");
535 if ( flushfork( writtenfork ) < 0 ) {
536 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
542 function = (u_char) dsi->commands[0];
544 /* AFP replay cache */
545 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
546 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
548 if (replaycache[rc_idx].DSIreqID == dsi->clientID
549 && replaycache[rc_idx].AFPcommand == function) {
550 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
551 dsi->clientID, AfpNum2name(function));
552 err = replaycache[rc_idx].result;
553 /* AFP replay cache end */
555 /* send off an afp command. in a couple cases, we take advantage
556 * of the fact that we're a stream-based protocol. */
557 if (afp_switch[function]) {
558 dsi->datalen = DSI_DATASIZ;
559 dsi->flags |= DSI_RUNNING;
561 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
563 err = (*afp_switch[function])(obj,
564 (char *)&dsi->commands, dsi->cmdlen,
565 (char *)&dsi->data, &dsi->datalen);
567 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
568 AfpNum2name(function), AfpErr2name(err));
570 dir_free_invalid_q();
573 /* bring everything back to old euid, egid */
575 restore_uidgid ( &obj->uidgid );
576 #endif /* FORCE_UIDGID */
577 dsi->flags &= ~DSI_RUNNING;
579 /* Add result to the AFP replay cache */
580 replaycache[rc_idx].DSIreqID = dsi->clientID;
581 replaycache[rc_idx].AFPcommand = function;
582 replaycache[rc_idx].result = err;
584 LOG(log_error, logtype_afpd, "bad function %X", function);
590 /* single shot toggle that gets set by dsi_readinit. */
591 if (dsi->flags & DSI_NOREPLY) {
592 dsi->flags &= ~DSI_NOREPLY;
596 if (!dsi_cmdreply(dsi, err)) {
597 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
598 dsi->flags |= DSI_DISCONNECTED;
602 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
603 function = (u_char) dsi->commands[0];
604 if ( afp_switch[ function ] != NULL ) {
605 dsi->datalen = DSI_DATASIZ;
606 dsi->flags |= DSI_RUNNING;
608 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
610 err = (*afp_switch[function])(obj,
611 (char *)&dsi->commands, dsi->cmdlen,
612 (char *)&dsi->data, &dsi->datalen);
614 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
615 AfpNum2name(function), AfpErr2name(err));
617 dsi->flags &= ~DSI_RUNNING;
619 /* bring everything back to old euid, egid */
621 restore_uidgid ( &obj->uidgid );
622 #endif /* FORCE_UIDGID */
624 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
629 if (!dsi_wrtreply(dsi, err)) {
630 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
631 dsi->flags |= DSI_DISCONNECTED;
635 case DSIFUNC_ATTN: /* attention replies */
638 /* error. this usually implies a mismatch of some kind
639 * between server and client. if things are correct,
640 * we need to flush the rest of the packet if necessary. */
642 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
643 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
647 pending_request(dsi);
651 afp_dsi_die(EXITERR_CLNT);