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>
34 #include <atalk/locking.h>
43 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
44 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
45 * but are deeply nested in the function chain with the caller already without acces to it.
46 * Changing this would require adding a reference to the caller which itself might be
47 * called in many places (eg acltoownermode is called from accessmode).
48 * The only sane way out is providing a copy of it here:
50 AFPObj *AFPobj = NULL;
61 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
63 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
65 static sigjmp_buf recon_jmp;
66 static void afp_dsi_close(AFPObj *obj)
68 DSI *dsi = obj->handle;
73 /* we may have been called from a signal handler caught when afpd was running
74 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
75 * restore our login user
77 if (geteuid() != obj->uid) {
78 if (seteuid( obj->uid ) < 0) {
79 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
80 obj->uid, strerror(errno), getuid(), geteuid());
89 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
90 dsi->read_count/1024.0, dsi->write_count/1024.0);
96 /* -------------------------------
98 * a little bit of code duplication.
100 static void afp_dsi_die(int sig)
102 DSI *dsi = (DSI *)AFPobj->handle;
104 if (dsi->flags & DSI_RECONINPROG) {
105 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
106 dsi->flags &= ~DSI_RECONINPROG;
107 return; /* this returns to afp_disconnect */
110 if (dsi->flags & DSI_DISCONNECTED) {
111 LOG(log_note, logtype_afpd, "Disconnected session terminating");
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 /* SIGURG handler (primary reconnect) */
129 static void afp_dsi_transfer_session(int sig _U_)
133 DSI *dsi = (DSI *)AFPobj->handle;
135 LOG(log_debug, 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 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
151 dsi->proto_close(dsi);
152 dsi->socket = socket;
153 dsi->flags = DSI_RECONSOCKET;
155 dsi->eof = dsi->start = dsi->buffer;
157 dsi->header.dsi_requestID = dsiID;
158 dsi->header.dsi_command = DSIFUNC_CMD;
161 * The session transfer happens in the middle of FPDisconnect old session, thus we
162 * have to send the reply now.
164 if (!dsi_cmdreply(dsi, AFP_OK)) {
165 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
166 afp_dsi_close(AFPobj);
170 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
172 * Now returning from this signal handler return to dsi_receive which should start
173 * reading/continuing from the connected socket that was passed via the parent from
174 * another session. The parent will terminate that session.
176 siglongjmp(recon_jmp, 1);
179 /* ------------------- */
180 static void afp_dsi_timedown(int sig _U_)
184 DSI *dsi = (DSI *)AFPobj->handle;
185 dsi->flags |= DSI_DIE;
186 /* shutdown and don't reconnect. server going down in 5 minutes. */
187 setmessage("The server is going down for maintenance.");
188 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
189 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
190 DSI *dsi = (DSI *)AFPobj->handle;
191 dsi->down_request = 1;
194 it.it_interval.tv_sec = 0;
195 it.it_interval.tv_usec = 0;
196 it.it_value.tv_sec = 300;
197 it.it_value.tv_usec = 0;
199 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
200 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
201 afp_dsi_die(EXITERR_SYS);
203 memset(&sv, 0, sizeof(sv));
204 sv.sa_handler = afp_dsi_die;
205 sigemptyset( &sv.sa_mask );
206 sigaddset(&sv.sa_mask, SIGHUP);
207 sigaddset(&sv.sa_mask, SIGTERM);
208 sv.sa_flags = SA_RESTART;
209 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
210 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
211 afp_dsi_die(EXITERR_SYS);
215 sv.sa_handler = SIG_IGN;
216 sigemptyset( &sv.sa_mask );
217 sv.sa_flags = SA_RESTART;
218 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
219 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
220 afp_dsi_die(EXITERR_SYS);
224 /* ---------------------------------
225 * SIGHUP reload configuration file
227 volatile int reload_request = 0;
229 static void afp_dsi_reload(int sig _U_)
234 /* ---------------------------------
235 * SIGINT: enable max_debug LOGging
237 static volatile sig_atomic_t debug_request = 0;
239 static void afp_dsi_debug(int sig _U_)
244 /* ---------------------- */
245 static void afp_dsi_getmesg (int sig _U_)
247 DSI *dsi = (DSI *)AFPobj->handle;
249 dsi->msg_request = 1;
250 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
251 dsi->msg_request = 2;
254 static void alarm_handler(int sig _U_)
257 DSI *dsi = (DSI *)AFPobj->handle;
259 /* we have to restart the timer because some libraries may use alarm() */
260 setitimer(ITIMER_REAL, &dsi->timer, NULL);
262 /* we got some traffic from the client since the previous timer tick. */
263 if ((dsi->flags & DSI_DATA)) {
264 dsi->flags &= ~DSI_DATA;
269 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
271 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
272 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
273 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
274 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
275 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
276 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
277 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
278 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
279 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
281 if (dsi->flags & DSI_SLEEPING) {
282 if (dsi->tickle > AFPobj->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 (dsi->tickle > AFPobj->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) && (dsi->tickle >= AFPobj->options.timeout)) {
299 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
300 dsi->proto_close(dsi);
301 dsi->flags |= DSI_DISCONNECTED;
305 if ((err = pollvoltime(AFPobj)) == 0)
306 err = dsi_tickle(AFPobj->handle);
308 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
309 dsi->flags |= DSI_DISCONNECTED;
314 if dsi->in_write is set attention, tickle (and close?) msg
315 aren't sent. We don't care about tickle
317 static void pending_request(DSI *dsi)
319 /* send pending attention */
321 /* read msg if any, it could be done in afp_getsrvrmesg */
322 if (dsi->msg_request) {
323 if (dsi->msg_request == 2) {
324 /* didn't send it in signal handler */
325 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
327 dsi->msg_request = 0;
330 if (dsi->down_request) {
331 dsi->down_request = 0;
332 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
333 AFPATTN_MESG | AFPATTN_TIME(5));
337 /* -------------------------------------------
338 afp over dsi. this never returns.
340 void afp_over_dsi(AFPObj *obj)
342 DSI *dsi = (DSI *) obj->handle;
346 struct sigaction action;
349 obj->exit = afp_dsi_die;
350 obj->reply = (int (*)()) dsi_cmdreply;
351 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
354 memset(&action, 0, sizeof(action));
357 action.sa_handler = afp_dsi_reload;
358 sigemptyset( &action.sa_mask );
359 sigaddset(&action.sa_mask, SIGALRM);
360 sigaddset(&action.sa_mask, SIGTERM);
361 sigaddset(&action.sa_mask, SIGUSR1);
362 sigaddset(&action.sa_mask, SIGINT);
363 sigaddset(&action.sa_mask, SIGUSR2);
364 action.sa_flags = SA_RESTART;
365 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
366 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
367 afp_dsi_die(EXITERR_SYS);
371 action.sa_handler = afp_dsi_transfer_session;
372 sigemptyset( &action.sa_mask );
373 sigaddset(&action.sa_mask, SIGALRM);
374 sigaddset(&action.sa_mask, SIGTERM);
375 sigaddset(&action.sa_mask, SIGUSR1);
376 sigaddset(&action.sa_mask, SIGINT);
377 sigaddset(&action.sa_mask, SIGUSR2);
378 action.sa_flags = SA_RESTART;
379 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
380 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
381 afp_dsi_die(EXITERR_SYS);
384 /* install SIGTERM */
385 action.sa_handler = afp_dsi_die;
386 sigemptyset( &action.sa_mask );
387 sigaddset(&action.sa_mask, SIGALRM);
388 sigaddset(&action.sa_mask, SIGHUP);
389 sigaddset(&action.sa_mask, SIGUSR1);
390 sigaddset(&action.sa_mask, SIGINT);
391 sigaddset(&action.sa_mask, SIGUSR2);
392 action.sa_flags = SA_RESTART;
393 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
394 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
395 afp_dsi_die(EXITERR_SYS);
398 /* Added for server message support */
399 action.sa_handler = afp_dsi_getmesg;
400 sigemptyset( &action.sa_mask );
401 sigaddset(&action.sa_mask, SIGALRM);
402 sigaddset(&action.sa_mask, SIGTERM);
403 sigaddset(&action.sa_mask, SIGUSR1);
404 sigaddset(&action.sa_mask, SIGHUP);
405 sigaddset(&action.sa_mask, SIGINT);
406 action.sa_flags = SA_RESTART;
407 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
408 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
409 afp_dsi_die(EXITERR_SYS);
412 /* SIGUSR1 - set down in 5 minutes */
413 action.sa_handler = afp_dsi_timedown;
414 sigemptyset( &action.sa_mask );
415 sigaddset(&action.sa_mask, SIGALRM);
416 sigaddset(&action.sa_mask, SIGHUP);
417 sigaddset(&action.sa_mask, SIGTERM);
418 sigaddset(&action.sa_mask, SIGINT);
419 sigaddset(&action.sa_mask, SIGUSR2);
420 action.sa_flags = SA_RESTART;
421 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
422 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
423 afp_dsi_die(EXITERR_SYS);
426 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
427 action.sa_handler = afp_dsi_debug;
428 sigfillset( &action.sa_mask );
429 action.sa_flags = SA_RESTART;
430 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
431 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
432 afp_dsi_die(EXITERR_SYS);
437 action.sa_handler = alarm_handler;
438 sigemptyset(&action.sa_mask);
439 sigaddset(&action.sa_mask, SIGHUP);
440 sigaddset(&action.sa_mask, SIGTERM);
441 sigaddset(&action.sa_mask, SIGUSR1);
442 sigaddset(&action.sa_mask, SIGINT);
443 sigaddset(&action.sa_mask, SIGUSR2);
444 action.sa_flags = SA_RESTART;
445 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
446 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
447 afp_dsi_die(EXITERR_SYS);
449 #endif /* DEBUGGING */
451 if (dircache_init(obj->options.dircachesize) != 0)
452 afp_dsi_die(EXITERR_SYS);
454 /* get stuck here until the end */
456 if (sigsetjmp(recon_jmp, 1) != 0)
457 /* returning from SIGALARM handler for a primary reconnect */
460 /* Blocking read on the network socket */
461 cmd = dsi_receive(dsi);
464 /* cmd == 0 is the error condition */
465 if (dsi->flags & DSI_RECONSOCKET) {
466 /* we just got a reconnect so we immediately try again to receive on the new fd */
467 dsi->flags &= ~DSI_RECONSOCKET;
470 /* Some error on the client connection, enter disconnected state */
471 dsi->flags |= DSI_DISCONNECTED;
472 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
473 continue; /* continue receiving until disconnect timer expires
474 * or a primary reconnect succeeds */
477 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
478 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
479 dsi->flags &= ~DSI_SLEEPING;
483 if (reload_request) {
485 load_volumes(AFPobj);
490 /* The first SIGINT enables debugging, the next restores the config */
492 static int debugging = 0;
496 if (obj->options.logconfig)
497 setuplog(obj->options.logconfig);
499 setuplog("default log_note");
504 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
509 if (cmd == DSIFUNC_TICKLE) {
510 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
511 if ((dsi->flags & DSI_DIE))
513 pending_request(dsi);
517 dsi->flags |= DSI_DATA;
521 LOG(log_note, logtype_afpd, "done");
528 if ( flushfork( writtenfork ) < 0 ) {
529 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
535 function = (u_char) dsi->commands[0];
537 /* AFP replay cache */
538 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
539 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
541 if (replaycache[rc_idx].DSIreqID == dsi->clientID
542 && replaycache[rc_idx].AFPcommand == function) {
543 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
544 dsi->clientID, AfpNum2name(function));
545 err = replaycache[rc_idx].result;
546 /* AFP replay cache end */
548 /* send off an afp command. in a couple cases, we take advantage
549 * of the fact that we're a stream-based protocol. */
550 if (afp_switch[function]) {
551 dsi->datalen = DSI_DATASIZ;
552 dsi->flags |= DSI_RUNNING;
554 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
556 err = (*afp_switch[function])(obj,
557 (char *)&dsi->commands, dsi->cmdlen,
558 (char *)&dsi->data, &dsi->datalen);
560 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
561 AfpNum2name(function), AfpErr2name(err));
563 dir_free_invalid_q();
565 dsi->flags &= ~DSI_RUNNING;
567 /* Add result to the AFP replay cache */
568 replaycache[rc_idx].DSIreqID = dsi->clientID;
569 replaycache[rc_idx].AFPcommand = function;
570 replaycache[rc_idx].result = err;
572 LOG(log_error, logtype_afpd, "bad function %X", function);
578 /* single shot toggle that gets set by dsi_readinit. */
579 if (dsi->flags & DSI_NOREPLY) {
580 dsi->flags &= ~DSI_NOREPLY;
584 if (!dsi_cmdreply(dsi, err)) {
585 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
586 dsi->flags |= DSI_DISCONNECTED;
590 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
591 function = (u_char) dsi->commands[0];
592 if ( afp_switch[ function ] != NULL ) {
593 dsi->datalen = DSI_DATASIZ;
594 dsi->flags |= DSI_RUNNING;
596 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
598 err = (*afp_switch[function])(obj,
599 (char *)&dsi->commands, dsi->cmdlen,
600 (char *)&dsi->data, &dsi->datalen);
602 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
603 AfpNum2name(function), AfpErr2name(err));
605 dsi->flags &= ~DSI_RUNNING;
607 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
612 if (!dsi_wrtreply(dsi, err)) {
613 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
614 dsi->flags |= DSI_DISCONNECTED;
618 case DSIFUNC_ATTN: /* attention replies */
621 /* error. this usually implies a mismatch of some kind
622 * between server and client. if things are correct,
623 * we need to flush the rest of the packet if necessary. */
625 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
626 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
630 pending_request(dsi);
634 afp_dsi_die(EXITERR_CLNT);