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 */
45 #define CHILD_DIE (1 << 0)
46 #define CHILD_RUNNING (1 << 1)
47 #define CHILD_SLEEPING (1 << 2)
48 #define CHILD_DATA (1 << 3)
49 #define CHILD_DISCONNECTED (1 << 4)
52 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
53 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
54 * but are deeply nested in the function chain with the caller already without acces to it.
55 * Changing this would require adding a reference to the caller which itself might be
56 * called in many places (eg acltoownermode is called from accessmode).
57 * The only sane way out is providing a copy of it here:
59 AFPObj *AFPobj = NULL;
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(child.obj->handle, AFPATTN_SHUTDOWN);
116 afp_dsi_close(child.obj);
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 *)child.obj->handle;
134 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session fd");
136 if (readt(child.obj->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(child.obj);
142 if ((socket = recv_fd(child.obj->ipc_fd, 1)) == -1) {
143 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
144 afp_dsi_close(child.obj);
149 dsi->socket = socket;
150 dsi->header.dsi_requestID = dsiID;
151 dsi->header.dsi_len = 0;
152 dsi->header.dsi_code = AFP_OK;
153 dsi->header.dsi_command = DSIFUNC_CMD;
154 dsi->header.dsi_flags = DSIFL_REPLY;
156 if (!dsi_cmdreply(dsi, AFP_OK)) {
157 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
158 afp_dsi_close(child.obj);
163 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
165 * Now returning from this signal handler return to dsi_receive which should start
166 * reading/continuing from the connected socket that was passed via the parent from
167 * another session. The parent will terminate that session.
172 static void afp_dsi_sleep(void)
174 child.flags |= CHILD_SLEEPING;
175 dsi_sleep(child.obj->handle, 1);
178 /* ------------------- */
179 static void afp_dsi_timedown(int sig _U_)
184 child.flags |= CHILD_DIE;
185 /* shutdown and don't reconnect. server going down in 5 minutes. */
186 setmessage("The server is going down for maintenance.");
187 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
188 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
189 DSI *dsi = (DSI *) child.obj->handle;
190 dsi->down_request = 1;
193 it.it_interval.tv_sec = 0;
194 it.it_interval.tv_usec = 0;
195 it.it_value.tv_sec = 300;
196 it.it_value.tv_usec = 0;
198 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
199 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
200 afp_dsi_die(EXITERR_SYS);
202 memset(&sv, 0, sizeof(sv));
203 sv.sa_handler = afp_dsi_die;
204 sigemptyset( &sv.sa_mask );
205 sigaddset(&sv.sa_mask, SIGHUP);
206 sigaddset(&sv.sa_mask, SIGTERM);
207 sv.sa_flags = SA_RESTART;
208 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
209 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
210 afp_dsi_die(EXITERR_SYS);
214 sv.sa_handler = SIG_IGN;
215 sigemptyset( &sv.sa_mask );
216 sv.sa_flags = SA_RESTART;
217 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
218 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
219 afp_dsi_die(EXITERR_SYS);
223 /* ---------------------------------
224 * SIGHUP reload configuration file
226 volatile int reload_request = 0;
228 static void afp_dsi_reload(int sig _U_)
233 /* ---------------------------------
234 * SIGINT: enable max_debug LOGging
236 static volatile sig_atomic_t debug_request = 0;
238 static void afp_dsi_debug(int sig _U_)
243 /* ---------------------- */
245 static void afp_dsi_getmesg (int sig _U_)
247 DSI *dsi = (DSI *) child.obj->handle;
249 dsi->msg_request = 1;
250 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
251 dsi->msg_request = 2;
253 #endif /* SERVERTEXT */
255 static void alarm_handler(int sig _U_)
258 DSI *dsi = (DSI *) child.obj->handle;
260 if (child.flags & CHILD_DISCONNECTED) {
261 LOG(log_note, logtype_afpd, "afp_alarm: no reconnect within 10 hours, goodbye");
262 afp_dsi_die(EXITERR_CLNT);
265 /* we have to restart the timer because some libraries may use alarm() */
266 setitimer(ITIMER_REAL, &dsi->timer, NULL);
268 /* we got some traffic from the client since the previous timer
270 if ((child.flags & CHILD_DATA)) {
271 child.flags &= ~CHILD_DATA;
272 child.flags &= ~CHILD_DISCONNECTED;
276 /* if we're in the midst of processing something,
278 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
282 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
283 if (!(err = pollvoltime(child.obj)))
284 err = dsi_tickle(child.obj->handle);
286 afp_dsi_die(EXITERR_CLNT);
287 } else { /* didn't receive a tickle, enter disconnected state */
288 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
289 struct itimerval t = {{0, 60 * 60 * 10}, {0, 0}}; /* 10 hours */
290 setitimer(ITIMER_REAL, &t, NULL);
291 child.flags |= CHILD_DISCONNECTED;
296 if dsi->in_write is set attention, tickle (and close?) msg
297 aren't sent. We don't care about tickle
299 static void pending_request(DSI *dsi)
301 /* send pending attention */
303 /* read msg if any, it could be done in afp_getsrvrmesg */
304 if (dsi->msg_request) {
305 if (dsi->msg_request == 2) {
306 /* didn't send it in signal handler */
307 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
309 dsi->msg_request = 0;
310 readmessage(child.obj);
312 if (dsi->down_request) {
313 dsi->down_request = 0;
314 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
315 AFPATTN_MESG | AFPATTN_TIME(5));
319 /* -------------------------------------------
320 afp over dsi. this never returns.
322 void afp_over_dsi(AFPObj *obj)
324 DSI *dsi = (DSI *) obj->handle;
327 struct sigaction action;
330 obj->exit = afp_dsi_die;
331 obj->reply = (int (*)()) dsi_cmdreply;
332 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
334 obj->sleep = afp_dsi_sleep;
336 child.tickle = child.flags = 0;
338 memset(&action, 0, sizeof(action));
341 action.sa_handler = afp_dsi_reload;
342 sigemptyset( &action.sa_mask );
343 sigaddset(&action.sa_mask, SIGALRM);
344 sigaddset(&action.sa_mask, SIGTERM);
345 sigaddset(&action.sa_mask, SIGUSR1);
346 sigaddset(&action.sa_mask, SIGINT);
348 sigaddset(&action.sa_mask, SIGUSR2);
350 action.sa_flags = SA_RESTART;
351 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
352 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
353 afp_dsi_die(EXITERR_SYS);
357 action.sa_handler = afp_dsi_transfer_session;
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);
364 sigaddset(&action.sa_mask, SIGUSR2);
366 action.sa_flags = SA_RESTART;
367 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
368 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
369 afp_dsi_die(EXITERR_SYS);
372 /* install SIGTERM */
373 action.sa_handler = afp_dsi_die;
374 sigemptyset( &action.sa_mask );
375 sigaddset(&action.sa_mask, SIGALRM);
376 sigaddset(&action.sa_mask, SIGHUP);
377 sigaddset(&action.sa_mask, SIGUSR1);
378 sigaddset(&action.sa_mask, SIGINT);
380 sigaddset(&action.sa_mask, SIGUSR2);
382 action.sa_flags = SA_RESTART;
383 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
384 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
385 afp_dsi_die(EXITERR_SYS);
389 /* Added for server message support */
390 action.sa_handler = afp_dsi_getmesg;
391 sigemptyset( &action.sa_mask );
392 sigaddset(&action.sa_mask, SIGALRM);
393 sigaddset(&action.sa_mask, SIGTERM);
394 sigaddset(&action.sa_mask, SIGUSR1);
395 sigaddset(&action.sa_mask, SIGHUP);
396 sigaddset(&action.sa_mask, SIGINT);
397 action.sa_flags = SA_RESTART;
398 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
399 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
400 afp_dsi_die(EXITERR_SYS);
402 #endif /* SERVERTEXT */
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);
412 sigaddset(&action.sa_mask, SIGUSR2);
414 action.sa_flags = SA_RESTART;
415 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
416 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
417 afp_dsi_die(EXITERR_SYS);
420 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
421 action.sa_handler = afp_dsi_debug;
422 sigfillset( &action.sa_mask );
423 action.sa_flags = SA_RESTART;
424 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
425 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
426 afp_dsi_die(EXITERR_SYS);
431 action.sa_handler = alarm_handler;
432 sigemptyset(&action.sa_mask);
433 sigaddset(&action.sa_mask, SIGHUP);
434 sigaddset(&action.sa_mask, SIGTERM);
435 sigaddset(&action.sa_mask, SIGUSR1);
436 sigaddset(&action.sa_mask, SIGINT);
438 sigaddset(&action.sa_mask, SIGUSR2);
440 action.sa_flags = SA_RESTART;
441 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
442 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
443 afp_dsi_die(EXITERR_SYS);
445 #endif /* DEBUGGING */
447 if (dircache_init(obj->options.dircachesize) != 0)
448 afp_dsi_die(EXITERR_SYS);
450 /* get stuck here until the end */
451 while ((cmd = dsi_receive(dsi))) {
453 child.flags &= ~CHILD_SLEEPING;
454 dsi_sleep(dsi, 0); /* wake up */
456 if (reload_request) {
458 load_volumes(child.obj);
463 /* The first SIGINT enables debugging, the next restores the config */
465 static int debugging = 0;
469 if (obj->options.logconfig)
470 setuplog(obj->options.logconfig);
472 setuplog("default log_note");
477 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
482 if (cmd == DSIFUNC_TICKLE) {
483 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
484 if ((child.flags & CHILD_DIE))
486 pending_request(dsi);
490 child.flags |= CHILD_DATA;
494 LOG(log_info, logtype_afpd, "done");
501 if ( flushfork( writtenfork ) < 0 ) {
502 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
508 function = (u_char) dsi->commands[0];
510 /* send off an afp command. in a couple cases, we take advantage
511 * of the fact that we're a stream-based protocol. */
512 if (afp_switch[function]) {
513 dsi->datalen = DSI_DATASIZ;
514 child.flags |= CHILD_RUNNING;
516 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
518 err = (*afp_switch[function])(obj,
519 (char *)&dsi->commands, dsi->cmdlen,
520 (char *)&dsi->data, &dsi->datalen);
522 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
523 AfpNum2name(function), AfpErr2name(err));
525 dir_free_invalid_q();
528 /* bring everything back to old euid, egid */
530 restore_uidgid ( &obj->uidgid );
531 #endif /* FORCE_UIDGID */
532 child.flags &= ~CHILD_RUNNING;
534 LOG(log_error, logtype_afpd, "bad function %X", function);
539 /* single shot toggle that gets set by dsi_readinit. */
545 if (!dsi_cmdreply(dsi, err)) {
546 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
547 afp_dsi_die(EXITERR_CLNT);
551 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
552 function = (u_char) dsi->commands[0];
553 if ( afp_switch[ function ] != NULL ) {
554 dsi->datalen = DSI_DATASIZ;
555 child.flags |= CHILD_RUNNING;
557 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
559 err = (*afp_switch[function])(obj,
560 (char *)&dsi->commands, dsi->cmdlen,
561 (char *)&dsi->data, &dsi->datalen);
563 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
564 AfpNum2name(function), AfpErr2name(err));
566 child.flags &= ~CHILD_RUNNING;
568 /* bring everything back to old euid, egid */
570 restore_uidgid ( &obj->uidgid );
571 #endif /* FORCE_UIDGID */
573 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
578 if (!dsi_wrtreply(dsi, err)) {
579 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
580 afp_dsi_die(EXITERR_CLNT);
584 case DSIFUNC_ATTN: /* attention replies */
587 /* error. this usually implies a mismatch of some kind
588 * between server and client. if things are correct,
589 * we need to flush the rest of the packet if necessary. */
591 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
592 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
596 pending_request(dsi);
600 afp_dsi_die(EXITERR_CLNT);