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 */
46 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
47 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
48 * but are deeply nested in the function chain with the caller already without acces to it.
49 * Changing this would require adding a reference to the caller which itself might be
50 * called in many places (eg acltoownermode is called from accessmode).
51 * The only sane way out is providing a copy of it here:
53 AFPObj *AFPobj = NULL;
70 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
72 rc_elem_t replaycache[REPLAYCACHE_SIZE];
74 static void afp_dsi_close(AFPObj *obj)
76 DSI *dsi = obj->handle;
81 /* we may have been called from a signal handler caught when afpd was running
82 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
83 * restore our login user
85 if (geteuid() != obj->uid) {
86 if (seteuid( obj->uid ) < 0) {
87 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
88 obj->uid, strerror(errno), getuid(), geteuid());
97 LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
98 dsi->read_count/1024.0, dsi->write_count/1024.0);
104 /* -------------------------------
106 * a little bit of code duplication.
108 static void afp_dsi_die(int sig)
110 static volatile int in_handler;
115 /* it's not atomic but we don't care because it's an exit function
116 * ie if a signal is received here, between the test and the affectation,
117 * it will not return.
121 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
122 afp_dsi_close(child.obj);
123 if (sig) /* if no signal, assume dieing because logins are disabled &
124 don't log it (maintenance mode)*/
125 LOG(log_info, logtype_afpd, "Connection terminated");
126 if (sig == SIGTERM || sig == SIGALRM) {
134 static void afp_dsi_transfer_session(int sig _U_)
138 DSI *dsi = (DSI *)child.obj->handle;
140 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
142 if (readt(child.obj->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(child.obj);
148 if ((socket = recv_fd(child.obj->ipc_fd, 1)) == -1) {
149 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
150 afp_dsi_close(child.obj);
154 if (dsi->socket != -1)
156 dsi->socket = socket;
157 dsi->header.dsi_requestID = dsiID;
158 dsi->header.dsi_len = 0;
159 dsi->header.dsi_code = AFP_OK;
160 dsi->header.dsi_command = DSIFUNC_CMD;
161 dsi->header.dsi_flags = DSIFL_REPLY;
164 * The session transfer happens in the middle of FPDisconnect old session, thus we
165 * have to send the reply now.
167 if (!dsi_cmdreply(dsi, AFP_OK)) {
168 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
169 afp_dsi_close(child.obj);
173 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
175 * Now returning from this signal handler return to dsi_receive which should start
176 * reading/continuing from the connected socket that was passed via the parent from
177 * another session. The parent will terminate that session.
182 static void afp_dsi_sleep(void)
184 dsi_sleep(child.obj->handle, 1);
187 /* ------------------- */
188 static void afp_dsi_timedown(int sig _U_)
192 DSI *dsi = (DSI *)child.obj->handle;
193 dsi->flags |= DSI_DIE;
194 /* shutdown and don't reconnect. server going down in 5 minutes. */
195 setmessage("The server is going down for maintenance.");
196 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
197 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
198 DSI *dsi = (DSI *) child.obj->handle;
199 dsi->down_request = 1;
202 it.it_interval.tv_sec = 0;
203 it.it_interval.tv_usec = 0;
204 it.it_value.tv_sec = 300;
205 it.it_value.tv_usec = 0;
207 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
208 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
209 afp_dsi_die(EXITERR_SYS);
211 memset(&sv, 0, sizeof(sv));
212 sv.sa_handler = afp_dsi_die;
213 sigemptyset( &sv.sa_mask );
214 sigaddset(&sv.sa_mask, SIGHUP);
215 sigaddset(&sv.sa_mask, SIGTERM);
216 sv.sa_flags = SA_RESTART;
217 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
218 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
219 afp_dsi_die(EXITERR_SYS);
223 sv.sa_handler = SIG_IGN;
224 sigemptyset( &sv.sa_mask );
225 sv.sa_flags = SA_RESTART;
226 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
227 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
228 afp_dsi_die(EXITERR_SYS);
232 /* ---------------------------------
233 * SIGHUP reload configuration file
235 volatile int reload_request = 0;
237 static void afp_dsi_reload(int sig _U_)
242 /* ---------------------------------
243 * SIGINT: enable max_debug LOGging
245 static volatile sig_atomic_t debug_request = 0;
247 static void afp_dsi_debug(int sig _U_)
252 /* ---------------------- */
254 static void afp_dsi_getmesg (int sig _U_)
256 DSI *dsi = (DSI *) child.obj->handle;
258 dsi->msg_request = 1;
259 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
260 dsi->msg_request = 2;
262 #endif /* SERVERTEXT */
264 static void alarm_handler(int sig _U_)
267 DSI *dsi = (DSI *) child.obj->handle;
269 /* we have to restart the timer because some libraries may use alarm() */
270 setitimer(ITIMER_REAL, &dsi->timer, NULL);
272 /* we got some traffic from the client since the previous timer tick. */
273 if ((dsi->flags & DSI_DATA)) {
274 dsi->flags &= ~DSI_DATA;
275 dsi->flags &= ~DSI_DISCONNECTED;
281 if (dsi->flags & DSI_SLEEPING) {
282 if (child.tickle < child.obj->options.sleep) {
283 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
284 afp_dsi_die(EXITERR_CLNT);
289 if (dsi->flags & DSI_DISCONNECTED) {
290 if (child.tickle > child.obj->options.disconnected) {
291 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
292 afp_dsi_die(EXITERR_CLNT);
297 /* if we're in the midst of processing something, don't die. */
298 if ( !(dsi->flags & DSI_RUNNING) && (child.tickle > child.obj->options.timeout)) {
299 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
300 dsi->flags |= DSI_DISCONNECTED;
304 if ((err = pollvoltime(child.obj)) == 0)
305 err = dsi_tickle(child.obj->handle);
307 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
308 dsi->flags |= DSI_DISCONNECTED;
313 if dsi->in_write is set attention, tickle (and close?) msg
314 aren't sent. We don't care about tickle
316 static void pending_request(DSI *dsi)
318 /* send pending attention */
320 /* read msg if any, it could be done in afp_getsrvrmesg */
321 if (dsi->msg_request) {
322 if (dsi->msg_request == 2) {
323 /* didn't send it in signal handler */
324 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
326 dsi->msg_request = 0;
327 readmessage(child.obj);
329 if (dsi->down_request) {
330 dsi->down_request = 0;
331 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
332 AFPATTN_MESG | AFPATTN_TIME(5));
336 /* -------------------------------------------
337 afp over dsi. this never returns.
339 void afp_over_dsi(AFPObj *obj)
341 DSI *dsi = (DSI *) obj->handle;
345 struct sigaction action;
348 obj->exit = afp_dsi_die;
349 obj->reply = (int (*)()) dsi_cmdreply;
350 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
352 obj->sleep = afp_dsi_sleep;
354 child.tickle = child.flags = 0;
356 memset(&action, 0, sizeof(action));
359 action.sa_handler = afp_dsi_reload;
360 sigemptyset( &action.sa_mask );
361 sigaddset(&action.sa_mask, SIGALRM);
362 sigaddset(&action.sa_mask, SIGTERM);
363 sigaddset(&action.sa_mask, SIGUSR1);
364 sigaddset(&action.sa_mask, SIGINT);
366 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);
382 sigaddset(&action.sa_mask, SIGUSR2);
384 action.sa_flags = SA_RESTART;
385 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
386 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
387 afp_dsi_die(EXITERR_SYS);
390 /* install SIGTERM */
391 action.sa_handler = afp_dsi_die;
392 sigemptyset( &action.sa_mask );
393 sigaddset(&action.sa_mask, SIGALRM);
394 sigaddset(&action.sa_mask, SIGHUP);
395 sigaddset(&action.sa_mask, SIGUSR1);
396 sigaddset(&action.sa_mask, SIGINT);
398 sigaddset(&action.sa_mask, SIGUSR2);
400 action.sa_flags = SA_RESTART;
401 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
402 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
403 afp_dsi_die(EXITERR_SYS);
407 /* Added for server message support */
408 action.sa_handler = afp_dsi_getmesg;
409 sigemptyset( &action.sa_mask );
410 sigaddset(&action.sa_mask, SIGALRM);
411 sigaddset(&action.sa_mask, SIGTERM);
412 sigaddset(&action.sa_mask, SIGUSR1);
413 sigaddset(&action.sa_mask, SIGHUP);
414 sigaddset(&action.sa_mask, SIGINT);
415 action.sa_flags = SA_RESTART;
416 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
417 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
418 afp_dsi_die(EXITERR_SYS);
420 #endif /* SERVERTEXT */
422 /* SIGUSR1 - set down in 5 minutes */
423 action.sa_handler = afp_dsi_timedown;
424 sigemptyset( &action.sa_mask );
425 sigaddset(&action.sa_mask, SIGALRM);
426 sigaddset(&action.sa_mask, SIGHUP);
427 sigaddset(&action.sa_mask, SIGTERM);
428 sigaddset(&action.sa_mask, SIGINT);
430 sigaddset(&action.sa_mask, SIGUSR2);
432 action.sa_flags = SA_RESTART;
433 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
434 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
435 afp_dsi_die(EXITERR_SYS);
438 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
439 action.sa_handler = afp_dsi_debug;
440 sigfillset( &action.sa_mask );
441 action.sa_flags = SA_RESTART;
442 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
443 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
444 afp_dsi_die(EXITERR_SYS);
449 action.sa_handler = alarm_handler;
450 sigemptyset(&action.sa_mask);
451 sigaddset(&action.sa_mask, SIGHUP);
452 sigaddset(&action.sa_mask, SIGTERM);
453 sigaddset(&action.sa_mask, SIGUSR1);
454 sigaddset(&action.sa_mask, SIGINT);
456 sigaddset(&action.sa_mask, SIGUSR2);
458 action.sa_flags = SA_RESTART; /* dont restart, otherwi */
459 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
460 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
461 afp_dsi_die(EXITERR_SYS);
463 #endif /* DEBUGGING */
465 if (dircache_init(obj->options.dircachesize) != 0)
466 afp_dsi_die(EXITERR_SYS);
468 /* get stuck here until the end */
470 cmd = dsi_receive(dsi);
472 /* Some error on the client connection, enter disconnected state */
473 dsi->flags |= DSI_DISCONNECTED;
474 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
475 continue; /* continue receiving until disconnect timer expires
476 * or a primary reconnect succeeds */
480 dsi_sleep(dsi, 0); /* wake up */
482 if (reload_request) {
484 load_volumes(child.obj);
489 /* The first SIGINT enables debugging, the next restores the config */
491 static int debugging = 0;
495 if (obj->options.logconfig)
496 setuplog(obj->options.logconfig);
498 setuplog("default log_note");
503 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
508 if (cmd == DSIFUNC_TICKLE) {
509 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
510 if ((dsi->flags & DSI_DIE))
512 pending_request(dsi);
516 dsi->flags |= DSI_DATA;
520 LOG(log_info, logtype_afpd, "done");
527 if ( flushfork( writtenfork ) < 0 ) {
528 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
534 function = (u_char) dsi->commands[0];
536 /* AFP replay cache */
537 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
538 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
540 if (replaycache[rc_idx].DSIreqID == dsi->clientID
541 && replaycache[rc_idx].AFPcommand == function) {
542 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
543 dsi->clientID, AfpNum2name(function));
544 err = replaycache[rc_idx].result;
545 /* AFP replay cache end */
547 /* send off an afp command. in a couple cases, we take advantage
548 * of the fact that we're a stream-based protocol. */
549 if (afp_switch[function]) {
550 dsi->datalen = DSI_DATASIZ;
551 dsi->flags |= DSI_RUNNING;
553 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
555 err = (*afp_switch[function])(obj,
556 (char *)&dsi->commands, dsi->cmdlen,
557 (char *)&dsi->data, &dsi->datalen);
559 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
560 AfpNum2name(function), AfpErr2name(err));
562 dir_free_invalid_q();
565 /* bring everything back to old euid, egid */
567 restore_uidgid ( &obj->uidgid );
568 #endif /* FORCE_UIDGID */
569 dsi->flags &= ~DSI_RUNNING;
571 /* Add result to the AFP replay cache */
572 replaycache[rc_idx].DSIreqID = dsi->clientID;
573 replaycache[rc_idx].AFPcommand = function;
574 replaycache[rc_idx].result = err;
576 LOG(log_error, logtype_afpd, "bad function %X", function);
582 /* single shot toggle that gets set by dsi_readinit. */
588 if (!dsi_cmdreply(dsi, err)) {
589 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
590 afp_dsi_die(EXITERR_CLNT);
594 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
595 function = (u_char) dsi->commands[0];
596 if ( afp_switch[ function ] != NULL ) {
597 dsi->datalen = DSI_DATASIZ;
598 dsi->flags |= DSI_RUNNING;
600 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
602 err = (*afp_switch[function])(obj,
603 (char *)&dsi->commands, dsi->cmdlen,
604 (char *)&dsi->data, &dsi->datalen);
606 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
607 AfpNum2name(function), AfpErr2name(err));
609 dsi->flags &= ~DSI_RUNNING;
611 /* bring everything back to old euid, egid */
613 restore_uidgid ( &obj->uidgid );
614 #endif /* FORCE_UIDGID */
616 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
621 if (!dsi_wrtreply(dsi, err)) {
622 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
623 afp_dsi_die(EXITERR_CLNT);
627 case DSIFUNC_ATTN: /* attention replies */
630 /* error. this usually implies a mismatch of some kind
631 * between server and client. if things are correct,
632 * we need to flush the rest of the packet if necessary. */
634 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
635 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
639 pending_request(dsi);
643 afp_dsi_die(EXITERR_CLNT);