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;
109 if (dsi->flags & DSI_RECONINPROG) {
110 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
111 dsi->flags &= ~DSI_RECONINPROG;
112 return; /* this returns to afp_disconnect */
115 if (dsi->flags & DSI_DISCONNECTED) {
116 LOG(log_note, logtype_afpd, "Disconnected session terminating");
120 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
121 afp_dsi_close(AFPobj);
122 if (sig) /* if no signal, assume dieing because logins are disabled &
123 don't log it (maintenance mode)*/
124 LOG(log_info, logtype_afpd, "Connection terminated");
125 if (sig == SIGTERM || sig == SIGALRM) {
133 /* SIGURG handler (primary reconnect) */
134 static void afp_dsi_transfer_session(int sig _U_)
138 DSI *dsi = (DSI *)AFPobj->handle;
140 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
142 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
143 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
144 afp_dsi_close(AFPobj);
148 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
149 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
150 afp_dsi_close(AFPobj);
154 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
156 dsi->socket = socket;
157 dsi->flags = DSI_RECONSOCKET;
159 dsi->eof = dsi->start = dsi->buffer;
161 dsi->header.dsi_requestID = dsiID;
162 dsi->header.dsi_command = DSIFUNC_CMD;
165 * The session transfer happens in the middle of FPDisconnect old session, thus we
166 * have to send the reply now.
168 if (!dsi_cmdreply(dsi, AFP_OK)) {
169 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
170 afp_dsi_close(AFPobj);
174 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
176 * Now returning from this signal handler return to dsi_receive which should start
177 * reading/continuing from the connected socket that was passed via the parent from
178 * another session. The parent will terminate that session.
180 siglongjmp(recon_jmp, 1);
183 /* ------------------- */
184 static void afp_dsi_timedown(int sig _U_)
188 DSI *dsi = (DSI *)AFPobj->handle;
189 dsi->flags |= DSI_DIE;
190 /* shutdown and don't reconnect. server going down in 5 minutes. */
191 setmessage("The server is going down for maintenance.");
192 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
193 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
194 DSI *dsi = (DSI *)AFPobj->handle;
195 dsi->down_request = 1;
198 it.it_interval.tv_sec = 0;
199 it.it_interval.tv_usec = 0;
200 it.it_value.tv_sec = 300;
201 it.it_value.tv_usec = 0;
203 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
204 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
205 afp_dsi_die(EXITERR_SYS);
207 memset(&sv, 0, sizeof(sv));
208 sv.sa_handler = afp_dsi_die;
209 sigemptyset( &sv.sa_mask );
210 sigaddset(&sv.sa_mask, SIGHUP);
211 sigaddset(&sv.sa_mask, SIGTERM);
212 sv.sa_flags = SA_RESTART;
213 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
214 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
215 afp_dsi_die(EXITERR_SYS);
219 sv.sa_handler = SIG_IGN;
220 sigemptyset( &sv.sa_mask );
221 sv.sa_flags = SA_RESTART;
222 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
223 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
224 afp_dsi_die(EXITERR_SYS);
228 /* ---------------------------------
229 * SIGHUP reload configuration file
231 volatile int reload_request = 0;
233 static void afp_dsi_reload(int sig _U_)
238 /* ---------------------------------
239 * SIGINT: enable max_debug LOGging
241 static volatile sig_atomic_t debug_request = 0;
243 static void afp_dsi_debug(int sig _U_)
248 /* ---------------------- */
249 static void afp_dsi_getmesg (int sig _U_)
251 DSI *dsi = (DSI *)AFPobj->handle;
253 dsi->msg_request = 1;
254 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
255 dsi->msg_request = 2;
258 static void alarm_handler(int sig _U_)
261 DSI *dsi = (DSI *)AFPobj->handle;
263 /* we have to restart the timer because some libraries may use alarm() */
264 setitimer(ITIMER_REAL, &dsi->timer, NULL);
266 /* we got some traffic from the client since the previous timer tick. */
267 if ((dsi->flags & DSI_DATA)) {
268 dsi->flags &= ~DSI_DATA;
273 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
275 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
276 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
277 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
278 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
279 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
280 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
281 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
282 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
283 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
285 if (dsi->flags & DSI_SLEEPING) {
286 if (dsi->tickle > AFPobj->options.sleep) {
287 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
288 afp_dsi_die(EXITERR_CLNT);
293 if (dsi->flags & DSI_DISCONNECTED) {
294 if (dsi->tickle > AFPobj->options.disconnected) {
295 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
296 afp_dsi_die(EXITERR_CLNT);
301 /* if we're in the midst of processing something, don't die. */
302 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
303 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
304 dsi->proto_close(dsi);
305 dsi->flags |= DSI_DISCONNECTED;
309 if ((err = pollvoltime(AFPobj)) == 0)
310 err = dsi_tickle(AFPobj->handle);
312 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
313 dsi->flags |= DSI_DISCONNECTED;
318 if dsi->in_write is set attention, tickle (and close?) msg
319 aren't sent. We don't care about tickle
321 static void pending_request(DSI *dsi)
323 /* send pending attention */
325 /* read msg if any, it could be done in afp_getsrvrmesg */
326 if (dsi->msg_request) {
327 if (dsi->msg_request == 2) {
328 /* didn't send it in signal handler */
329 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
331 dsi->msg_request = 0;
334 if (dsi->down_request) {
335 dsi->down_request = 0;
336 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
337 AFPATTN_MESG | AFPATTN_TIME(5));
341 /* -------------------------------------------
342 afp over dsi. this never returns.
344 void afp_over_dsi(AFPObj *obj)
346 DSI *dsi = (DSI *) obj->handle;
350 struct sigaction action;
353 obj->exit = afp_dsi_die;
354 obj->reply = (int (*)()) dsi_cmdreply;
355 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
358 memset(&action, 0, sizeof(action));
361 action.sa_handler = afp_dsi_reload;
362 sigemptyset( &action.sa_mask );
363 sigaddset(&action.sa_mask, SIGALRM);
364 sigaddset(&action.sa_mask, SIGTERM);
365 sigaddset(&action.sa_mask, SIGUSR1);
366 sigaddset(&action.sa_mask, SIGINT);
367 sigaddset(&action.sa_mask, SIGUSR2);
368 action.sa_flags = SA_RESTART;
369 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
370 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
371 afp_dsi_die(EXITERR_SYS);
375 action.sa_handler = afp_dsi_transfer_session;
376 sigemptyset( &action.sa_mask );
377 sigaddset(&action.sa_mask, SIGALRM);
378 sigaddset(&action.sa_mask, SIGTERM);
379 sigaddset(&action.sa_mask, SIGUSR1);
380 sigaddset(&action.sa_mask, SIGINT);
381 sigaddset(&action.sa_mask, SIGUSR2);
382 action.sa_flags = SA_RESTART;
383 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
384 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
385 afp_dsi_die(EXITERR_SYS);
388 /* install SIGTERM */
389 action.sa_handler = afp_dsi_die;
390 sigemptyset( &action.sa_mask );
391 sigaddset(&action.sa_mask, SIGALRM);
392 sigaddset(&action.sa_mask, SIGHUP);
393 sigaddset(&action.sa_mask, SIGUSR1);
394 sigaddset(&action.sa_mask, SIGINT);
395 sigaddset(&action.sa_mask, SIGUSR2);
396 action.sa_flags = SA_RESTART;
397 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
398 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
399 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);
416 /* SIGUSR1 - set down in 5 minutes */
417 action.sa_handler = afp_dsi_timedown;
418 sigemptyset( &action.sa_mask );
419 sigaddset(&action.sa_mask, SIGALRM);
420 sigaddset(&action.sa_mask, SIGHUP);
421 sigaddset(&action.sa_mask, SIGTERM);
422 sigaddset(&action.sa_mask, SIGINT);
423 sigaddset(&action.sa_mask, SIGUSR2);
424 action.sa_flags = SA_RESTART;
425 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
426 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
427 afp_dsi_die(EXITERR_SYS);
430 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
431 action.sa_handler = afp_dsi_debug;
432 sigfillset( &action.sa_mask );
433 action.sa_flags = SA_RESTART;
434 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
435 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
436 afp_dsi_die(EXITERR_SYS);
441 action.sa_handler = alarm_handler;
442 sigemptyset(&action.sa_mask);
443 sigaddset(&action.sa_mask, SIGHUP);
444 sigaddset(&action.sa_mask, SIGTERM);
445 sigaddset(&action.sa_mask, SIGUSR1);
446 sigaddset(&action.sa_mask, SIGINT);
447 sigaddset(&action.sa_mask, SIGUSR2);
448 action.sa_flags = SA_RESTART;
449 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
450 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
451 afp_dsi_die(EXITERR_SYS);
453 #endif /* DEBUGGING */
455 if (dircache_init(obj->options.dircachesize) != 0)
456 afp_dsi_die(EXITERR_SYS);
458 /* get stuck here until the end */
460 if (sigsetjmp(recon_jmp, 1) != 0)
461 /* returning from SIGALARM handler for a primary reconnect */
464 /* Blocking read on the network socket */
465 cmd = dsi_receive(dsi);
468 /* cmd == 0 is the error condition */
469 if (dsi->flags & DSI_RECONSOCKET) {
470 /* we just got a reconnect so we immediately try again to receive on the new fd */
471 dsi->flags &= ~DSI_RECONSOCKET;
474 /* Some error on the client connection, enter disconnected state */
475 dsi->flags |= DSI_DISCONNECTED;
476 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
477 continue; /* continue receiving until disconnect timer expires
478 * or a primary reconnect succeeds */
481 /* Now, after successfully reading from the new socket after a reconnect *
482 * we can close the old socket without taking the risk of confusing the client */
488 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
489 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
490 dsi->flags &= ~DSI_SLEEPING;
494 if (reload_request) {
496 load_volumes(AFPobj);
501 /* The first SIGINT enables debugging, the next restores the config */
503 static int debugging = 0;
507 if (obj->options.logconfig)
508 setuplog(obj->options.logconfig);
510 setuplog("default log_note");
515 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
520 if (cmd == DSIFUNC_TICKLE) {
521 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
522 if ((dsi->flags & DSI_DIE))
524 pending_request(dsi);
528 dsi->flags |= DSI_DATA;
532 LOG(log_note, logtype_afpd, "done");
539 if ( flushfork( writtenfork ) < 0 ) {
540 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
546 function = (u_char) dsi->commands[0];
548 /* AFP replay cache */
549 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
550 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
552 if (replaycache[rc_idx].DSIreqID == dsi->clientID
553 && replaycache[rc_idx].AFPcommand == function) {
554 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
555 dsi->clientID, AfpNum2name(function));
556 err = replaycache[rc_idx].result;
557 /* AFP replay cache end */
559 /* send off an afp command. in a couple cases, we take advantage
560 * of the fact that we're a stream-based protocol. */
561 if (afp_switch[function]) {
562 dsi->datalen = DSI_DATASIZ;
563 dsi->flags |= DSI_RUNNING;
565 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
567 err = (*afp_switch[function])(obj,
568 (char *)&dsi->commands, dsi->cmdlen,
569 (char *)&dsi->data, &dsi->datalen);
571 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
572 AfpNum2name(function), AfpErr2name(err));
574 dir_free_invalid_q();
577 /* bring everything back to old euid, egid */
579 restore_uidgid ( &obj->uidgid );
580 #endif /* FORCE_UIDGID */
581 dsi->flags &= ~DSI_RUNNING;
583 /* Add result to the AFP replay cache */
584 replaycache[rc_idx].DSIreqID = dsi->clientID;
585 replaycache[rc_idx].AFPcommand = function;
586 replaycache[rc_idx].result = err;
588 LOG(log_error, logtype_afpd, "bad function %X", function);
594 /* single shot toggle that gets set by dsi_readinit. */
595 if (dsi->flags & DSI_NOREPLY) {
596 dsi->flags &= ~DSI_NOREPLY;
600 if (!dsi_cmdreply(dsi, err)) {
601 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
602 dsi->flags |= DSI_DISCONNECTED;
606 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
607 function = (u_char) dsi->commands[0];
608 if ( afp_switch[ function ] != NULL ) {
609 dsi->datalen = DSI_DATASIZ;
610 dsi->flags |= DSI_RUNNING;
612 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
614 err = (*afp_switch[function])(obj,
615 (char *)&dsi->commands, dsi->cmdlen,
616 (char *)&dsi->data, &dsi->datalen);
618 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
619 AfpNum2name(function), AfpErr2name(err));
621 dsi->flags &= ~DSI_RUNNING;
623 /* bring everything back to old euid, egid */
625 restore_uidgid ( &obj->uidgid );
626 #endif /* FORCE_UIDGID */
628 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
633 if (!dsi_wrtreply(dsi, err)) {
634 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
635 dsi->flags |= DSI_DISCONNECTED;
639 case DSIFUNC_ATTN: /* attention replies */
642 /* error. this usually implies a mismatch of some kind
643 * between server and client. if things are correct,
644 * we need to flush the rest of the packet if necessary. */
646 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
647 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
651 pending_request(dsi);
655 afp_dsi_die(EXITERR_CLNT);