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>
33 #include <atalk/locking.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 rc_elem_t replaycache[REPLAYCACHE_SIZE];
69 static void afp_dsi_close(AFPObj *obj)
71 DSI *dsi = obj->handle;
76 /* we may have been called from a signal handler caught when afpd was running
77 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
78 * restore our login user
80 if (geteuid() != obj->uid) {
81 if (seteuid( obj->uid ) < 0) {
82 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
83 obj->uid, strerror(errno), getuid(), geteuid());
92 LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
93 dsi->read_count/1024.0, dsi->write_count/1024.0);
99 /* -------------------------------
101 * a little bit of code duplication.
103 static void afp_dsi_die(int sig)
105 static volatile int in_handler;
110 /* it's not atomic but we don't care because it's an exit function
111 * ie if a signal is received here, between the test and the affectation,
112 * it will not return.
116 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
117 afp_dsi_close(AFPobj);
118 if (sig) /* if no signal, assume dieing because logins are disabled &
119 don't log it (maintenance mode)*/
120 LOG(log_info, logtype_afpd, "Connection terminated");
121 if (sig == SIGTERM || sig == SIGALRM) {
129 static void afp_dsi_transfer_session(int sig _U_)
133 DSI *dsi = (DSI *)AFPobj->handle;
135 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
137 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
138 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
139 afp_dsi_close(AFPobj);
143 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
144 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
145 afp_dsi_close(AFPobj);
149 if (dsi->socket != -1)
151 dsi->socket = socket;
152 dsi->header.dsi_requestID = dsiID;
153 dsi->header.dsi_len = 0;
154 dsi->header.dsi_code = AFP_OK;
155 dsi->header.dsi_command = DSIFUNC_CMD;
156 dsi->header.dsi_flags = DSIFL_REPLY;
159 * The session transfer happens in the middle of FPDisconnect old session, thus we
160 * have to send the reply now.
162 if (!dsi_cmdreply(dsi, AFP_OK)) {
163 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
164 afp_dsi_close(AFPobj);
168 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
170 * Now returning from this signal handler return to dsi_receive which should start
171 * reading/continuing from the connected socket that was passed via the parent from
172 * another session. The parent will terminate that session.
177 static void afp_dsi_sleep(void)
179 dsi_sleep(AFPobj->handle, 1);
182 /* ------------------- */
183 static void afp_dsi_timedown(int sig _U_)
187 DSI *dsi = (DSI *)AFPobj->handle;
188 dsi->flags |= DSI_DIE;
189 /* shutdown and don't reconnect. server going down in 5 minutes. */
190 setmessage("The server is going down for maintenance.");
191 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
192 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
193 DSI *dsi = (DSI *)AFPobj->handle;
194 dsi->down_request = 1;
197 it.it_interval.tv_sec = 0;
198 it.it_interval.tv_usec = 0;
199 it.it_value.tv_sec = 300;
200 it.it_value.tv_usec = 0;
202 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
203 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
204 afp_dsi_die(EXITERR_SYS);
206 memset(&sv, 0, sizeof(sv));
207 sv.sa_handler = afp_dsi_die;
208 sigemptyset( &sv.sa_mask );
209 sigaddset(&sv.sa_mask, SIGHUP);
210 sigaddset(&sv.sa_mask, SIGTERM);
211 sv.sa_flags = SA_RESTART;
212 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
213 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
214 afp_dsi_die(EXITERR_SYS);
218 sv.sa_handler = SIG_IGN;
219 sigemptyset( &sv.sa_mask );
220 sv.sa_flags = SA_RESTART;
221 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
222 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
223 afp_dsi_die(EXITERR_SYS);
227 /* ---------------------------------
228 * SIGHUP reload configuration file
230 volatile int reload_request = 0;
232 static void afp_dsi_reload(int sig _U_)
237 /* ---------------------------------
238 * SIGINT: enable max_debug LOGging
240 static volatile sig_atomic_t debug_request = 0;
242 static void afp_dsi_debug(int sig _U_)
247 /* ---------------------- */
248 static void afp_dsi_getmesg (int sig _U_)
250 DSI *dsi = (DSI *)AFPobj->handle;
252 dsi->msg_request = 1;
253 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
254 dsi->msg_request = 2;
257 static void alarm_handler(int sig _U_)
260 DSI *dsi = (DSI *)AFPobj->handle;
262 /* we have to restart the timer because some libraries may use alarm() */
263 setitimer(ITIMER_REAL, &dsi->timer, NULL);
265 /* we got some traffic from the client since the previous timer tick. */
266 if ((dsi->flags & DSI_DATA)) {
267 dsi->flags &= ~DSI_DATA;
268 dsi->flags &= ~DSI_DISCONNECTED;
274 if (dsi->flags & DSI_SLEEPING) {
275 if (dsi->tickle < AFPobj->options.sleep) {
276 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
277 afp_dsi_die(EXITERR_CLNT);
282 if (dsi->flags & DSI_DISCONNECTED) {
283 if (dsi->tickle > AFPobj->options.disconnected) {
284 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
285 afp_dsi_die(EXITERR_CLNT);
290 /* if we're in the midst of processing something, don't die. */
291 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle > AFPobj->options.timeout)) {
292 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
293 dsi->flags |= DSI_DISCONNECTED;
297 if ((err = pollvoltime(AFPobj)) == 0)
298 err = dsi_tickle(AFPobj->handle);
300 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
301 dsi->flags |= DSI_DISCONNECTED;
306 if dsi->in_write is set attention, tickle (and close?) msg
307 aren't sent. We don't care about tickle
309 static void pending_request(DSI *dsi)
311 /* send pending attention */
313 /* read msg if any, it could be done in afp_getsrvrmesg */
314 if (dsi->msg_request) {
315 if (dsi->msg_request == 2) {
316 /* didn't send it in signal handler */
317 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
319 dsi->msg_request = 0;
322 if (dsi->down_request) {
323 dsi->down_request = 0;
324 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
325 AFPATTN_MESG | AFPATTN_TIME(5));
329 /* -------------------------------------------
330 afp over dsi. this never returns.
332 void afp_over_dsi(AFPObj *obj)
334 DSI *dsi = (DSI *) obj->handle;
338 struct sigaction action;
341 obj->exit = afp_dsi_die;
342 obj->reply = (int (*)()) dsi_cmdreply;
343 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
344 obj->sleep = afp_dsi_sleep;
347 memset(&action, 0, sizeof(action));
350 action.sa_handler = afp_dsi_reload;
351 sigemptyset( &action.sa_mask );
352 sigaddset(&action.sa_mask, SIGALRM);
353 sigaddset(&action.sa_mask, SIGTERM);
354 sigaddset(&action.sa_mask, SIGUSR1);
355 sigaddset(&action.sa_mask, SIGINT);
356 sigaddset(&action.sa_mask, SIGUSR2);
357 action.sa_flags = SA_RESTART;
358 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
359 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
360 afp_dsi_die(EXITERR_SYS);
364 action.sa_handler = afp_dsi_transfer_session;
365 sigemptyset( &action.sa_mask );
366 sigaddset(&action.sa_mask, SIGALRM);
367 sigaddset(&action.sa_mask, SIGTERM);
368 sigaddset(&action.sa_mask, SIGUSR1);
369 sigaddset(&action.sa_mask, SIGINT);
370 sigaddset(&action.sa_mask, SIGUSR2);
371 action.sa_flags = SA_RESTART;
372 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
373 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
374 afp_dsi_die(EXITERR_SYS);
377 /* install SIGTERM */
378 action.sa_handler = afp_dsi_die;
379 sigemptyset( &action.sa_mask );
380 sigaddset(&action.sa_mask, SIGALRM);
381 sigaddset(&action.sa_mask, SIGHUP);
382 sigaddset(&action.sa_mask, SIGUSR1);
383 sigaddset(&action.sa_mask, SIGINT);
384 sigaddset(&action.sa_mask, SIGUSR2);
385 action.sa_flags = SA_RESTART;
386 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
387 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
388 afp_dsi_die(EXITERR_SYS);
391 /* Added for server message support */
392 action.sa_handler = afp_dsi_getmesg;
393 sigemptyset( &action.sa_mask );
394 sigaddset(&action.sa_mask, SIGALRM);
395 sigaddset(&action.sa_mask, SIGTERM);
396 sigaddset(&action.sa_mask, SIGUSR1);
397 sigaddset(&action.sa_mask, SIGHUP);
398 sigaddset(&action.sa_mask, SIGINT);
399 action.sa_flags = SA_RESTART;
400 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
401 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
402 afp_dsi_die(EXITERR_SYS);
405 /* SIGUSR1 - set down in 5 minutes */
406 action.sa_handler = afp_dsi_timedown;
407 sigemptyset( &action.sa_mask );
408 sigaddset(&action.sa_mask, SIGALRM);
409 sigaddset(&action.sa_mask, SIGHUP);
410 sigaddset(&action.sa_mask, SIGTERM);
411 sigaddset(&action.sa_mask, SIGINT);
412 sigaddset(&action.sa_mask, SIGUSR2);
413 action.sa_flags = SA_RESTART;
414 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
415 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
416 afp_dsi_die(EXITERR_SYS);
419 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
420 action.sa_handler = afp_dsi_debug;
421 sigfillset( &action.sa_mask );
422 action.sa_flags = SA_RESTART;
423 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
424 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
425 afp_dsi_die(EXITERR_SYS);
430 action.sa_handler = alarm_handler;
431 sigemptyset(&action.sa_mask);
432 sigaddset(&action.sa_mask, SIGHUP);
433 sigaddset(&action.sa_mask, SIGTERM);
434 sigaddset(&action.sa_mask, SIGUSR1);
435 sigaddset(&action.sa_mask, SIGINT);
436 sigaddset(&action.sa_mask, SIGUSR2);
437 action.sa_flags = SA_RESTART; /* dont restart, otherwi */
438 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
439 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
440 afp_dsi_die(EXITERR_SYS);
442 #endif /* DEBUGGING */
444 if (dircache_init(obj->options.dircachesize) != 0)
445 afp_dsi_die(EXITERR_SYS);
447 /* get stuck here until the end */
449 cmd = dsi_receive(dsi);
451 /* Some error on the client connection, enter disconnected state */
452 dsi->flags |= DSI_DISCONNECTED;
453 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
454 continue; /* continue receiving until disconnect timer expires
455 * or a primary reconnect succeeds */
459 dsi_sleep(dsi, 0); /* wake up */
461 if (reload_request) {
463 load_volumes(AFPobj);
468 /* The first SIGINT enables debugging, the next restores the config */
470 static int debugging = 0;
474 if (obj->options.logconfig)
475 setuplog(obj->options.logconfig);
477 setuplog("default log_note");
482 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
487 if (cmd == DSIFUNC_TICKLE) {
488 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
489 if ((dsi->flags & DSI_DIE))
491 pending_request(dsi);
495 dsi->flags |= DSI_DATA;
499 LOG(log_note, logtype_afpd, "done");
506 if ( flushfork( writtenfork ) < 0 ) {
507 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
513 function = (u_char) dsi->commands[0];
515 /* AFP replay cache */
516 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
517 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
519 if (replaycache[rc_idx].DSIreqID == dsi->clientID
520 && replaycache[rc_idx].AFPcommand == function) {
521 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
522 dsi->clientID, AfpNum2name(function));
523 err = replaycache[rc_idx].result;
524 /* AFP replay cache end */
526 /* send off an afp command. in a couple cases, we take advantage
527 * of the fact that we're a stream-based protocol. */
528 if (afp_switch[function]) {
529 dsi->datalen = DSI_DATASIZ;
530 dsi->flags |= DSI_RUNNING;
532 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
534 err = (*afp_switch[function])(obj,
535 (char *)&dsi->commands, dsi->cmdlen,
536 (char *)&dsi->data, &dsi->datalen);
538 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
539 AfpNum2name(function), AfpErr2name(err));
541 dir_free_invalid_q();
544 /* bring everything back to old euid, egid */
546 restore_uidgid ( &obj->uidgid );
547 #endif /* FORCE_UIDGID */
548 dsi->flags &= ~DSI_RUNNING;
550 /* Add result to the AFP replay cache */
551 replaycache[rc_idx].DSIreqID = dsi->clientID;
552 replaycache[rc_idx].AFPcommand = function;
553 replaycache[rc_idx].result = err;
555 LOG(log_error, logtype_afpd, "bad function %X", function);
561 /* single shot toggle that gets set by dsi_readinit. */
562 if (dsi->flags & DSI_NOREPLY) {
563 dsi->flags &= ~DSI_NOREPLY;
567 if (!dsi_cmdreply(dsi, err)) {
568 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
569 dsi->flags |= DSI_DISCONNECTED;
573 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
574 function = (u_char) dsi->commands[0];
575 if ( afp_switch[ function ] != NULL ) {
576 dsi->datalen = DSI_DATASIZ;
577 dsi->flags |= DSI_RUNNING;
579 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
581 err = (*afp_switch[function])(obj,
582 (char *)&dsi->commands, dsi->cmdlen,
583 (char *)&dsi->data, &dsi->datalen);
585 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
586 AfpNum2name(function), AfpErr2name(err));
588 dsi->flags &= ~DSI_RUNNING;
590 /* bring everything back to old euid, egid */
592 restore_uidgid ( &obj->uidgid );
593 #endif /* FORCE_UIDGID */
595 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
600 if (!dsi_wrtreply(dsi, err)) {
601 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
602 dsi->flags |= DSI_DISCONNECTED;
606 case DSIFUNC_ATTN: /* attention replies */
609 /* error. this usually implies a mismatch of some kind
610 * between server and client. if things are correct,
611 * we need to flush the rest of the packet if necessary. */
613 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
614 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
618 pending_request(dsi);
622 afp_dsi_die(EXITERR_CLNT);