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;
64 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
66 rc_elem_t replaycache[REPLAYCACHE_SIZE];
68 static void afp_dsi_close(AFPObj *obj)
70 DSI *dsi = obj->handle;
75 /* we may have been called from a signal handler caught when afpd was running
76 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
77 * restore our login user
79 if (geteuid() != obj->uid) {
80 if (seteuid( obj->uid ) < 0) {
81 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
82 obj->uid, strerror(errno), getuid(), geteuid());
91 LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
92 dsi->read_count/1024.0, dsi->write_count/1024.0);
98 /* -------------------------------
100 * a little bit of code duplication.
102 static void afp_dsi_die(int sig)
104 static volatile int in_handler;
109 /* it's not atomic but we don't care because it's an exit function
110 * ie if a signal is received here, between the test and the affectation,
111 * it will not return.
115 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
116 afp_dsi_close(AFPobj);
117 if (sig) /* if no signal, assume dieing because logins are disabled &
118 don't log it (maintenance mode)*/
119 LOG(log_info, logtype_afpd, "Connection terminated");
120 if (sig == SIGTERM || sig == SIGALRM) {
128 static void afp_dsi_transfer_session(int sig _U_)
132 DSI *dsi = (DSI *)AFPobj->handle;
134 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
136 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
137 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
138 afp_dsi_close(AFPobj);
142 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
143 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
144 afp_dsi_close(AFPobj);
148 if (dsi->socket != -1)
150 dsi->socket = socket;
151 dsi->header.dsi_requestID = dsiID;
152 dsi->header.dsi_len = 0;
153 dsi->header.dsi_code = AFP_OK;
154 dsi->header.dsi_command = DSIFUNC_CMD;
155 dsi->header.dsi_flags = DSIFL_REPLY;
158 * The session transfer happens in the middle of FPDisconnect old session, thus we
159 * have to send the reply now.
161 if (!dsi_cmdreply(dsi, AFP_OK)) {
162 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
163 afp_dsi_close(AFPobj);
167 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
169 * Now returning from this signal handler return to dsi_receive which should start
170 * reading/continuing from the connected socket that was passed via the parent from
171 * another session. The parent will terminate that session.
176 static void afp_dsi_sleep(void)
178 dsi_sleep(AFPobj->handle, 1);
181 /* ------------------- */
182 static void afp_dsi_timedown(int sig _U_)
186 DSI *dsi = (DSI *)AFPobj->handle;
187 dsi->flags |= DSI_DIE;
188 /* shutdown and don't reconnect. server going down in 5 minutes. */
189 setmessage("The server is going down for maintenance.");
190 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
191 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
192 DSI *dsi = (DSI *)AFPobj->handle;
193 dsi->down_request = 1;
196 it.it_interval.tv_sec = 0;
197 it.it_interval.tv_usec = 0;
198 it.it_value.tv_sec = 300;
199 it.it_value.tv_usec = 0;
201 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
202 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
203 afp_dsi_die(EXITERR_SYS);
205 memset(&sv, 0, sizeof(sv));
206 sv.sa_handler = afp_dsi_die;
207 sigemptyset( &sv.sa_mask );
208 sigaddset(&sv.sa_mask, SIGHUP);
209 sigaddset(&sv.sa_mask, SIGTERM);
210 sv.sa_flags = SA_RESTART;
211 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
212 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
213 afp_dsi_die(EXITERR_SYS);
217 sv.sa_handler = SIG_IGN;
218 sigemptyset( &sv.sa_mask );
219 sv.sa_flags = SA_RESTART;
220 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
221 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
222 afp_dsi_die(EXITERR_SYS);
226 /* ---------------------------------
227 * SIGHUP reload configuration file
229 volatile int reload_request = 0;
231 static void afp_dsi_reload(int sig _U_)
236 /* ---------------------------------
237 * SIGINT: enable max_debug LOGging
239 static volatile sig_atomic_t debug_request = 0;
241 static void afp_dsi_debug(int sig _U_)
246 /* ---------------------- */
247 static void afp_dsi_getmesg (int sig _U_)
249 DSI *dsi = (DSI *)AFPobj->handle;
251 dsi->msg_request = 1;
252 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
253 dsi->msg_request = 2;
256 static void alarm_handler(int sig _U_)
259 DSI *dsi = (DSI *)AFPobj->handle;
261 /* we have to restart the timer because some libraries may use alarm() */
262 setitimer(ITIMER_REAL, &dsi->timer, NULL);
264 /* we got some traffic from the client since the previous timer tick. */
265 if ((dsi->flags & DSI_DATA)) {
266 dsi->flags &= ~DSI_DATA;
267 dsi->flags &= ~DSI_DISCONNECTED;
273 if (dsi->flags & DSI_SLEEPING) {
274 if (dsi->tickle < AFPobj->options.sleep) {
275 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
276 afp_dsi_die(EXITERR_CLNT);
281 if (dsi->flags & DSI_DISCONNECTED) {
282 if (dsi->tickle > AFPobj->options.disconnected) {
283 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
284 afp_dsi_die(EXITERR_CLNT);
289 /* if we're in the midst of processing something, don't die. */
290 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle > AFPobj->options.timeout)) {
291 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
292 dsi->flags |= DSI_DISCONNECTED;
296 if ((err = pollvoltime(AFPobj)) == 0)
297 err = dsi_tickle(AFPobj->handle);
299 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
300 dsi->flags |= DSI_DISCONNECTED;
305 if dsi->in_write is set attention, tickle (and close?) msg
306 aren't sent. We don't care about tickle
308 static void pending_request(DSI *dsi)
310 /* send pending attention */
312 /* read msg if any, it could be done in afp_getsrvrmesg */
313 if (dsi->msg_request) {
314 if (dsi->msg_request == 2) {
315 /* didn't send it in signal handler */
316 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
318 dsi->msg_request = 0;
321 if (dsi->down_request) {
322 dsi->down_request = 0;
323 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
324 AFPATTN_MESG | AFPATTN_TIME(5));
328 /* -------------------------------------------
329 afp over dsi. this never returns.
331 void afp_over_dsi(AFPObj *obj)
333 DSI *dsi = (DSI *) obj->handle;
337 struct sigaction action;
340 obj->exit = afp_dsi_die;
341 obj->reply = (int (*)()) dsi_cmdreply;
342 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
343 obj->sleep = afp_dsi_sleep;
346 memset(&action, 0, sizeof(action));
349 action.sa_handler = afp_dsi_reload;
350 sigemptyset( &action.sa_mask );
351 sigaddset(&action.sa_mask, SIGALRM);
352 sigaddset(&action.sa_mask, SIGTERM);
353 sigaddset(&action.sa_mask, SIGUSR1);
354 sigaddset(&action.sa_mask, SIGINT);
355 sigaddset(&action.sa_mask, SIGUSR2);
356 action.sa_flags = SA_RESTART;
357 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
358 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
359 afp_dsi_die(EXITERR_SYS);
363 action.sa_handler = afp_dsi_transfer_session;
364 sigemptyset( &action.sa_mask );
365 sigaddset(&action.sa_mask, SIGALRM);
366 sigaddset(&action.sa_mask, SIGTERM);
367 sigaddset(&action.sa_mask, SIGUSR1);
368 sigaddset(&action.sa_mask, SIGINT);
369 sigaddset(&action.sa_mask, SIGUSR2);
370 action.sa_flags = SA_RESTART;
371 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
372 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
373 afp_dsi_die(EXITERR_SYS);
376 /* install SIGTERM */
377 action.sa_handler = afp_dsi_die;
378 sigemptyset( &action.sa_mask );
379 sigaddset(&action.sa_mask, SIGALRM);
380 sigaddset(&action.sa_mask, SIGHUP);
381 sigaddset(&action.sa_mask, SIGUSR1);
382 sigaddset(&action.sa_mask, SIGINT);
383 sigaddset(&action.sa_mask, SIGUSR2);
384 action.sa_flags = SA_RESTART;
385 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
386 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
387 afp_dsi_die(EXITERR_SYS);
390 /* Added for server message support */
391 action.sa_handler = afp_dsi_getmesg;
392 sigemptyset( &action.sa_mask );
393 sigaddset(&action.sa_mask, SIGALRM);
394 sigaddset(&action.sa_mask, SIGTERM);
395 sigaddset(&action.sa_mask, SIGUSR1);
396 sigaddset(&action.sa_mask, SIGHUP);
397 sigaddset(&action.sa_mask, SIGINT);
398 action.sa_flags = SA_RESTART;
399 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
400 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
401 afp_dsi_die(EXITERR_SYS);
404 /* SIGUSR1 - set down in 5 minutes */
405 action.sa_handler = afp_dsi_timedown;
406 sigemptyset( &action.sa_mask );
407 sigaddset(&action.sa_mask, SIGALRM);
408 sigaddset(&action.sa_mask, SIGHUP);
409 sigaddset(&action.sa_mask, SIGTERM);
410 sigaddset(&action.sa_mask, SIGINT);
411 sigaddset(&action.sa_mask, SIGUSR2);
412 action.sa_flags = SA_RESTART;
413 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
414 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
415 afp_dsi_die(EXITERR_SYS);
418 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
419 action.sa_handler = afp_dsi_debug;
420 sigfillset( &action.sa_mask );
421 action.sa_flags = SA_RESTART;
422 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
423 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
424 afp_dsi_die(EXITERR_SYS);
429 action.sa_handler = alarm_handler;
430 sigemptyset(&action.sa_mask);
431 sigaddset(&action.sa_mask, SIGHUP);
432 sigaddset(&action.sa_mask, SIGTERM);
433 sigaddset(&action.sa_mask, SIGUSR1);
434 sigaddset(&action.sa_mask, SIGINT);
435 sigaddset(&action.sa_mask, SIGUSR2);
436 action.sa_flags = SA_RESTART; /* dont restart, otherwi */
437 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
438 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
439 afp_dsi_die(EXITERR_SYS);
441 #endif /* DEBUGGING */
443 if (dircache_init(obj->options.dircachesize) != 0)
444 afp_dsi_die(EXITERR_SYS);
446 /* get stuck here until the end */
448 cmd = dsi_receive(dsi);
450 /* Some error on the client connection, enter disconnected state */
451 dsi->flags |= DSI_DISCONNECTED;
452 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
453 continue; /* continue receiving until disconnect timer expires
454 * or a primary reconnect succeeds */
458 dsi_sleep(dsi, 0); /* wake up */
460 if (reload_request) {
462 load_volumes(AFPobj);
467 /* The first SIGINT enables debugging, the next restores the config */
469 static int debugging = 0;
473 if (obj->options.logconfig)
474 setuplog(obj->options.logconfig);
476 setuplog("default log_note");
481 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
486 if (cmd == DSIFUNC_TICKLE) {
487 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
488 if ((dsi->flags & DSI_DIE))
490 pending_request(dsi);
494 dsi->flags |= DSI_DATA;
498 LOG(log_note, logtype_afpd, "done");
505 if ( flushfork( writtenfork ) < 0 ) {
506 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
512 function = (u_char) dsi->commands[0];
514 /* AFP replay cache */
515 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
516 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
518 if (replaycache[rc_idx].DSIreqID == dsi->clientID
519 && replaycache[rc_idx].AFPcommand == function) {
520 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
521 dsi->clientID, AfpNum2name(function));
522 err = replaycache[rc_idx].result;
523 /* AFP replay cache end */
525 /* send off an afp command. in a couple cases, we take advantage
526 * of the fact that we're a stream-based protocol. */
527 if (afp_switch[function]) {
528 dsi->datalen = DSI_DATASIZ;
529 dsi->flags |= DSI_RUNNING;
531 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
533 err = (*afp_switch[function])(obj,
534 (char *)&dsi->commands, dsi->cmdlen,
535 (char *)&dsi->data, &dsi->datalen);
537 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
538 AfpNum2name(function), AfpErr2name(err));
540 dir_free_invalid_q();
543 /* bring everything back to old euid, egid */
545 restore_uidgid ( &obj->uidgid );
546 #endif /* FORCE_UIDGID */
547 dsi->flags &= ~DSI_RUNNING;
549 /* Add result to the AFP replay cache */
550 replaycache[rc_idx].DSIreqID = dsi->clientID;
551 replaycache[rc_idx].AFPcommand = function;
552 replaycache[rc_idx].result = err;
554 LOG(log_error, logtype_afpd, "bad function %X", function);
560 /* single shot toggle that gets set by dsi_readinit. */
561 if (dsi->flags & DSI_NOREPLY) {
562 dsi->flags &= ~DSI_NOREPLY;
566 if (!dsi_cmdreply(dsi, err)) {
567 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
568 dsi->flags |= DSI_DISCONNECTED;
572 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
573 function = (u_char) dsi->commands[0];
574 if ( afp_switch[ function ] != NULL ) {
575 dsi->datalen = DSI_DATASIZ;
576 dsi->flags |= DSI_RUNNING;
578 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
580 err = (*afp_switch[function])(obj,
581 (char *)&dsi->commands, dsi->cmdlen,
582 (char *)&dsi->data, &dsi->datalen);
584 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
585 AfpNum2name(function), AfpErr2name(err));
587 dsi->flags &= ~DSI_RUNNING;
589 /* bring everything back to old euid, egid */
591 restore_uidgid ( &obj->uidgid );
592 #endif /* FORCE_UIDGID */
594 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
599 if (!dsi_wrtreply(dsi, err)) {
600 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
601 dsi->flags |= DSI_DISCONNECTED;
605 case DSIFUNC_ATTN: /* attention replies */
608 /* error. this usually implies a mismatch of some kind
609 * between server and client. if things are correct,
610 * we need to flush the rest of the packet if necessary. */
612 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
613 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
617 pending_request(dsi);
621 afp_dsi_die(EXITERR_CLNT);