]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Primary reconnect
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
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.
5  *
6  * modified from main.c. this handles afp over tcp.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <errno.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/socket.h>
22 #include <sys/time.h>
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif /* HAVE_SYS_STAT_H */
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <atalk/logger.h>
29
30 #include <atalk/dsi.h>
31 #include <atalk/compat.h>
32 #include <atalk/util.h>
33
34 #include "globals.h"
35 #include "switch.h"
36 #include "auth.h"
37 #include "fork.h"
38 #include "dircache.h"
39
40 #ifdef FORCE_UIDGID
41 #warning UIDGID
42 #include "uid.h"
43 #endif /* FORCE_UIDGID */
44
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)
50
51 /* 
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:
58  */
59 AFPObj *AFPobj = NULL;
60
61 static struct {
62     AFPObj *obj;
63     unsigned char flags;
64     int tickle;
65 } child;
66
67
68 static void afp_dsi_close(AFPObj *obj)
69 {
70     DSI *dsi = obj->handle;
71
72     close(obj->ipc_fd);
73     obj->ipc_fd = -1;
74
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
78      */
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());
83             exit(EXITERR_SYS);
84         }
85     }
86
87     close_all_vol();
88     if (obj->logout)
89         (*obj->logout)();
90
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);
93     log_dircache_stat();
94
95     dsi_close(dsi);
96 }
97
98 /* -------------------------------
99  * SIGTERM
100  * a little bit of code duplication. 
101  */
102 static void afp_dsi_die(int sig)
103 {
104 static volatile int in_handler;
105     
106     if (in_handler) {
107         return;
108     }
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.
112     */
113     in_handler = 1;
114
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) {
121         exit( 0 );
122     }
123     else {
124         exit(sig);
125     }
126 }
127
128 static void afp_dsi_transfer_session(int sig _U_)
129 {
130     uint16_t dsiID;
131     int socket;
132     DSI *dsi = (DSI *)child.obj->handle;
133
134     LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session fd");
135
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);
139         exit(EXITERR_SYS);
140     }
141
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);
145         exit(EXITERR_SYS);
146     }
147
148     close(dsi->socket);
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;
155
156     if (!dsi_cmdreply(dsi, AFP_OK)) {
157         LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
158         afp_dsi_close(child.obj);
159         exit(EXITERR_CLNT);
160     }
161
162
163     LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
164     /* 
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.
168      */
169 }
170
171 /* */
172 static void afp_dsi_sleep(void)
173 {
174     child.flags |= CHILD_SLEEPING;
175     dsi_sleep(child.obj->handle, 1);
176 }
177
178 /* ------------------- */
179 static void afp_dsi_timedown(int sig _U_)
180 {
181     struct sigaction    sv;
182     struct itimerval    it;
183
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;
191     }                  
192
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;
197
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);
201     }
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);
211     }
212
213     /* ignore myself */
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);
220     }
221 }
222
223 /* ---------------------------------
224  * SIGHUP reload configuration file
225  */
226 volatile int reload_request = 0;
227
228 static void afp_dsi_reload(int sig _U_)
229 {
230     reload_request = 1;
231 }
232
233 /* ---------------------------------
234  * SIGINT: enable max_debug LOGging
235  */
236 static volatile sig_atomic_t debug_request = 0;
237
238 static void afp_dsi_debug(int sig _U_)
239 {
240     debug_request = 1;
241 }
242
243 /* ---------------------- */
244 #ifdef SERVERTEXT
245 static void afp_dsi_getmesg (int sig _U_)
246 {
247     DSI *dsi = (DSI *) child.obj->handle;
248
249     dsi->msg_request = 1;
250     if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
251         dsi->msg_request = 2;
252 }
253 #endif /* SERVERTEXT */
254
255 static void alarm_handler(int sig _U_)
256 {
257     int err;
258     DSI *dsi = (DSI *) child.obj->handle;
259
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);
263     }
264
265     /* we have to restart the timer because some libraries may use alarm() */
266     setitimer(ITIMER_REAL, &dsi->timer, NULL);
267
268     /* we got some traffic from the client since the previous timer 
269      * tick. */
270     if ((child.flags & CHILD_DATA)) {
271         child.flags &= ~CHILD_DATA;
272         child.flags &= ~CHILD_DISCONNECTED;
273        return;
274     }
275
276     /* if we're in the midst of processing something,
277        don't die. */
278     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
279         return;
280     } 
281         
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);
285         if (err <= 0) 
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;
292     }
293 }
294
295 /* ----------------- 
296    if dsi->in_write is set attention, tickle (and close?) msg
297    aren't sent. We don't care about tickle 
298 */
299 static void pending_request(DSI *dsi)
300 {
301     /* send pending attention */
302
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));
308         }
309         dsi->msg_request = 0;
310         readmessage(child.obj);
311     }
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));
316     }
317 }
318
319 /* -------------------------------------------
320  afp over dsi. this never returns. 
321 */
322 void afp_over_dsi(AFPObj *obj)
323 {
324     DSI *dsi = (DSI *) obj->handle;
325     u_int32_t err, cmd;
326     u_int8_t function;
327     struct sigaction action;
328
329     AFPobj = obj;
330     obj->exit = afp_dsi_die;
331     obj->reply = (int (*)()) dsi_cmdreply;
332     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
333
334     obj->sleep = afp_dsi_sleep;
335     child.obj = obj;
336     child.tickle = child.flags = 0;
337
338     memset(&action, 0, sizeof(action));
339
340     /* install SIGHUP */
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);
347 #ifdef SERVERTEXT
348     sigaddset(&action.sa_mask, SIGUSR2);
349 #endif    
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);
354     }
355
356     /* install SIGURG */
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);
363 #ifdef SERVERTEXT
364     sigaddset(&action.sa_mask, SIGUSR2);
365 #endif    
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);
370     }
371
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);
379 #ifdef SERVERTEXT
380     sigaddset(&action.sa_mask, SIGUSR2);
381 #endif    
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);
386     }
387
388 #ifdef SERVERTEXT
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);
401     }
402 #endif /* SERVERTEXT */
403
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 #ifdef SERVERTEXT
412     sigaddset(&action.sa_mask, SIGUSR2);
413 #endif    
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);
418     }
419
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);
427     }
428
429 #ifndef DEBUGGING
430     /* tickle handler */
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);
437 #ifdef SERVERTEXT
438     sigaddset(&action.sa_mask, SIGUSR2);
439 #endif    
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);
444     }
445 #endif /* DEBUGGING */
446
447     if (dircache_init(obj->options.dircachesize) != 0)
448         afp_dsi_die(EXITERR_SYS);
449
450     /* get stuck here until the end */
451     while ((cmd = dsi_receive(dsi))) {
452         child.tickle = 0;
453         child.flags &= ~CHILD_SLEEPING;
454         dsi_sleep(dsi, 0); /* wake up */
455
456         if (reload_request) {
457             reload_request = 0;
458             load_volumes(child.obj);
459             dircache_dump();
460             log_dircache_stat();
461         }
462
463         /* The first SIGINT enables debugging, the next restores the config */
464         if (debug_request) {
465             static int debugging = 0;
466             debug_request = 0;
467
468             if (debugging) {
469                 if (obj->options.logconfig)
470                     setuplog(obj->options.logconfig);
471                 else
472                     setuplog("default log_note");
473                 debugging = 0;
474             } else {
475                 char logstr[50];
476                 debugging = 1;
477                 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
478                 setuplog(logstr);
479             }
480         }
481
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))
485                 dsi_tickle(dsi);
486             pending_request(dsi);
487             continue;
488         } 
489
490         child.flags |= CHILD_DATA;
491         switch(cmd) {
492         case DSIFUNC_CLOSE:
493             afp_dsi_close(obj);
494             LOG(log_info, logtype_afpd, "done");
495             return;
496             break;
497
498         case DSIFUNC_CMD:
499 #ifdef AFS
500             if ( writtenfork ) {
501                 if ( flushfork( writtenfork ) < 0 ) {
502                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
503                 }
504                 writtenfork = NULL;
505             }
506 #endif /* AFS */
507
508             function = (u_char) dsi->commands[0];
509
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;
515
516                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
517
518                 err = (*afp_switch[function])(obj,
519                                               (char *)&dsi->commands, dsi->cmdlen,
520                                               (char *)&dsi->data, &dsi->datalen);
521
522                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
523                     AfpNum2name(function), AfpErr2name(err));
524
525                 dir_free_invalid_q();
526
527 #ifdef FORCE_UIDGID
528                 /* bring everything back to old euid, egid */
529                 if (obj->force_uid)
530                     restore_uidgid ( &obj->uidgid );
531 #endif /* FORCE_UIDGID */
532                 child.flags &= ~CHILD_RUNNING;
533             } else {
534                 LOG(log_error, logtype_afpd, "bad function %X", function);
535                 dsi->datalen = 0;
536                 err = AFPERR_NOOP;
537             }
538
539             /* single shot toggle that gets set by dsi_readinit. */
540             if (dsi->noreply) {
541                 dsi->noreply = 0;
542                 break;
543             }
544
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);
548             }
549             break;
550
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;
556
557                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
558
559                 err = (*afp_switch[function])(obj,
560                                               (char *)&dsi->commands, dsi->cmdlen,
561                                               (char *)&dsi->data, &dsi->datalen);
562
563                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
564                     AfpNum2name(function), AfpErr2name(err));
565
566                 child.flags &= ~CHILD_RUNNING;
567 #ifdef FORCE_UIDGID
568                 /* bring everything back to old euid, egid */
569                 if (obj->force_uid)
570                     restore_uidgid ( &obj->uidgid );
571 #endif /* FORCE_UIDGID */
572             } else {
573                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
574                 dsi->datalen = 0;
575                 err = AFPERR_NOOP;
576             }
577
578             if (!dsi_wrtreply(dsi, err)) {
579                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
580                 afp_dsi_die(EXITERR_CLNT);
581             }
582             break;
583
584         case DSIFUNC_ATTN: /* attention replies */
585             break;
586
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. */
590         default:
591             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
592             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
593             dsi_writeflush(dsi);
594             break;
595         }
596         pending_request(dsi);
597     }
598
599     /* error */
600     afp_dsi_die(EXITERR_CLNT);
601 }