]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Enhance dircache robustness, dircache statistics logged in SIGALARM
[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
50 /* 
51  * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
52  * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
53  * but are deeply nested in the function chain with the caller already without acces to it.
54  * Changing this would require adding a reference to the caller which itself might be
55  * called in many places (eg acltoownermode is called from accessmode).
56  * The only sane way out is providing a copy of it here:
57  */
58 AFPObj *AFPobj = NULL;
59
60 static struct {
61     AFPObj *obj;
62     unsigned char flags;
63     int tickle;
64 } child;
65
66
67 static void afp_dsi_close(AFPObj *obj)
68 {
69     DSI *dsi = obj->handle;
70
71     /* we may have been called from a signal handler caught when afpd was running
72      * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
73      * restore our login user
74      */
75     if (geteuid() != obj->uid) {
76         if (seteuid( obj->uid ) < 0) {
77             LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
78                 obj->uid, strerror(errno), getuid(), geteuid());
79             exit(EXITERR_SYS);
80         }
81     }
82
83     close_all_vol();
84     if (obj->logout)
85         (*obj->logout)();
86
87     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
88         dsi->read_count/1024.0, dsi->write_count/1024.0);
89
90     dsi_close(dsi);
91 }
92
93 /* -------------------------------
94  * SIGTERM
95  * a little bit of code duplication. 
96  */
97 static void afp_dsi_die(int sig)
98 {
99 static volatile int in_handler;
100     
101     if (in_handler) {
102         return;
103     }
104     /* it's not atomic but we don't care because it's an exit function
105      * ie if a signal is received here, between the test and the affectation,
106      * it will not return.
107     */
108     in_handler = 1;
109
110     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
111     afp_dsi_close(child.obj);
112     if (sig) /* if no signal, assume dieing because logins are disabled &
113                 don't log it (maintenance mode)*/
114         LOG(log_info, logtype_afpd, "Connection terminated");
115     if (sig == SIGTERM || sig == SIGALRM) {
116         exit( 0 );
117     }
118     else {
119         exit(sig);
120     }
121 }
122
123 /* */
124 static void afp_dsi_sleep(void)
125 {
126     child.flags |= CHILD_SLEEPING;
127     dsi_sleep(child.obj->handle, 1);
128 }
129
130 /* ------------------- */
131 static void afp_dsi_timedown(int sig _U_)
132 {
133     struct sigaction    sv;
134     struct itimerval    it;
135
136     child.flags |= CHILD_DIE;
137     /* shutdown and don't reconnect. server going down in 5 minutes. */
138     setmessage("The server is going down for maintenance.");
139     if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
140                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
141         DSI *dsi = (DSI *) child.obj->handle;
142         dsi->down_request = 1;
143     }                  
144
145     it.it_interval.tv_sec = 0;
146     it.it_interval.tv_usec = 0;
147     it.it_value.tv_sec = 300;
148     it.it_value.tv_usec = 0;
149
150     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
151         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
152         afp_dsi_die(EXITERR_SYS);
153     }
154     memset(&sv, 0, sizeof(sv));
155     sv.sa_handler = afp_dsi_die;
156     sigemptyset( &sv.sa_mask );
157     sigaddset(&sv.sa_mask, SIGHUP);
158     sigaddset(&sv.sa_mask, SIGTERM);
159     sv.sa_flags = SA_RESTART;
160     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
161         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
162         afp_dsi_die(EXITERR_SYS);
163     }
164
165     /* ignore myself */
166     sv.sa_handler = SIG_IGN;
167     sigemptyset( &sv.sa_mask );
168     sv.sa_flags = SA_RESTART;
169     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
170         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
171         afp_dsi_die(EXITERR_SYS);
172     }
173 }
174
175 /* ---------------------------------
176  * SIGHUP reload configuration file
177  * FIXME here or we wait ?
178 */
179 volatile int reload_request = 0;
180
181 static void afp_dsi_reload(int sig _U_)
182 {
183     reload_request = 1;
184 }
185
186 /* ---------------------------------
187  * SIGINT: enable max_debug LOGging
188  */
189 static volatile sig_atomic_t debug_request = 0;
190
191 static void afp_dsi_debug(int sig _U_)
192 {
193     debug_request = 1;
194 }
195
196 /* ---------------------- */
197 #ifdef SERVERTEXT
198 static void afp_dsi_getmesg (int sig _U_)
199 {
200     DSI *dsi = (DSI *) child.obj->handle;
201
202     dsi->msg_request = 1;
203     if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
204         dsi->msg_request = 2;
205 }
206 #endif /* SERVERTEXT */
207
208 static void alarm_handler(int sig _U_)
209 {
210     int err;
211     DSI *dsi = (DSI *) child.obj->handle;
212
213     /* we have to restart the timer because some libraries 
214      * may use alarm() */
215     setitimer(ITIMER_REAL, &dsi->timer, NULL);
216
217     log_dircache_stat();
218
219     /* we got some traffic from the client since the previous timer 
220      * tick. */
221     if ((child.flags & CHILD_DATA)) {
222         child.flags &= ~CHILD_DATA;
223         return;
224     }
225
226     /* if we're in the midst of processing something,
227        don't die. */
228     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
229         return;
230     } 
231         
232     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
233         if (!(err = pollvoltime(child.obj)))
234             err = dsi_tickle(child.obj->handle);
235         if (err <= 0) 
236             afp_dsi_die(EXITERR_CLNT);
237         
238     } else { /* didn't receive a tickle. close connection */
239         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
240         afp_dsi_die(EXITERR_CLNT);
241     }
242 }
243
244 /* ----------------- 
245    if dsi->in_write is set attention, tickle (and close?) msg
246    aren't sent. We don't care about tickle 
247 */
248 static void pending_request(DSI *dsi)
249 {
250     /* send pending attention */
251
252     /* read msg if any, it could be done in afp_getsrvrmesg */
253     if (dsi->msg_request) {
254         if (dsi->msg_request == 2) {
255             /* didn't send it in signal handler */
256             dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
257         }
258         dsi->msg_request = 0;
259         readmessage(child.obj);
260     }
261     if (dsi->down_request) {
262         dsi->down_request = 0;
263         dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
264                   AFPATTN_MESG | AFPATTN_TIME(5));
265     }
266 }
267
268 /* -------------------------------------------
269  afp over dsi. this never returns. 
270 */
271 void afp_over_dsi(AFPObj *obj)
272 {
273     DSI *dsi = (DSI *) obj->handle;
274     u_int32_t err, cmd;
275     u_int8_t function;
276     struct sigaction action;
277
278     AFPobj = obj;
279     obj->exit = afp_dsi_die;
280     obj->reply = (int (*)()) dsi_cmdreply;
281     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
282
283     obj->sleep = afp_dsi_sleep;
284     child.obj = obj;
285     child.tickle = child.flags = 0;
286
287     memset(&action, 0, sizeof(action));
288
289     /* install SIGHUP */
290     action.sa_handler = afp_dsi_reload;
291     sigemptyset( &action.sa_mask );
292     sigaddset(&action.sa_mask, SIGALRM);
293     sigaddset(&action.sa_mask, SIGTERM);
294     sigaddset(&action.sa_mask, SIGUSR1);
295     sigaddset(&action.sa_mask, SIGINT);
296 #ifdef SERVERTEXT
297     sigaddset(&action.sa_mask, SIGUSR2);
298 #endif    
299     action.sa_flags = SA_RESTART;
300     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
301         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
302         afp_dsi_die(EXITERR_SYS);
303     }
304
305     /* install SIGTERM */
306     action.sa_handler = afp_dsi_die;
307     sigemptyset( &action.sa_mask );
308     sigaddset(&action.sa_mask, SIGALRM);
309     sigaddset(&action.sa_mask, SIGHUP);
310     sigaddset(&action.sa_mask, SIGUSR1);
311     sigaddset(&action.sa_mask, SIGINT);
312 #ifdef SERVERTEXT
313     sigaddset(&action.sa_mask, SIGUSR2);
314 #endif    
315     action.sa_flags = SA_RESTART;
316     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
317         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
318         afp_dsi_die(EXITERR_SYS);
319     }
320
321 #ifdef SERVERTEXT
322     /* Added for server message support */
323     action.sa_handler = afp_dsi_getmesg;
324     sigemptyset( &action.sa_mask );
325     sigaddset(&action.sa_mask, SIGALRM);
326     sigaddset(&action.sa_mask, SIGTERM);
327     sigaddset(&action.sa_mask, SIGUSR1);
328     sigaddset(&action.sa_mask, SIGHUP);
329     sigaddset(&action.sa_mask, SIGINT);
330     action.sa_flags = SA_RESTART;
331     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
332         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
333         afp_dsi_die(EXITERR_SYS);
334     }
335 #endif /* SERVERTEXT */
336
337     /*  SIGUSR1 - set down in 5 minutes  */
338     action.sa_handler = afp_dsi_timedown;
339     sigemptyset( &action.sa_mask );
340     sigaddset(&action.sa_mask, SIGALRM);
341     sigaddset(&action.sa_mask, SIGHUP);
342     sigaddset(&action.sa_mask, SIGTERM);
343     sigaddset(&action.sa_mask, SIGINT);
344 #ifdef SERVERTEXT
345     sigaddset(&action.sa_mask, SIGUSR2);
346 #endif    
347     action.sa_flags = SA_RESTART;
348     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
349         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
350         afp_dsi_die(EXITERR_SYS);
351     }
352
353     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
354     action.sa_handler = afp_dsi_debug;
355     sigfillset( &action.sa_mask );
356     action.sa_flags = SA_RESTART;
357     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
358         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
359         afp_dsi_die(EXITERR_SYS);
360     }
361
362 #ifndef DEBUGGING
363     /* tickle handler */
364     action.sa_handler = alarm_handler;
365     sigemptyset(&action.sa_mask);
366     sigaddset(&action.sa_mask, SIGHUP);
367     sigaddset(&action.sa_mask, SIGTERM);
368     sigaddset(&action.sa_mask, SIGUSR1);
369     sigaddset(&action.sa_mask, SIGINT);
370 #ifdef SERVERTEXT
371     sigaddset(&action.sa_mask, SIGUSR2);
372 #endif    
373     action.sa_flags = SA_RESTART;
374     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
375             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
376         afp_dsi_die(EXITERR_SYS);
377     }
378 #endif /* DEBUGGING */
379
380     if (dircache_init(obj->options.dircachesize) != 0)
381         afp_dsi_die(EXITERR_SYS);
382
383     /* get stuck here until the end */
384     while ((cmd = dsi_receive(dsi))) {
385         child.tickle = 0;
386         child.flags &= ~CHILD_SLEEPING;
387         dsi_sleep(dsi, 0); /* wake up */
388
389         if (reload_request) {
390             reload_request = 0;
391             load_volumes(child.obj);
392             dircache_dump();
393         }
394
395         /* The first SIGINT enables debugging, the next restores the config */
396         if (debug_request) {
397             static int debugging = 0;
398             debug_request = 0;
399
400             if (debugging) {
401                 if (obj->options.logconfig)
402                     setuplog(obj->options.logconfig);
403                 else
404                     setuplog("default log_note");
405                 debugging = 0;
406             } else {
407                 char logstr[50];
408                 debugging = 1;
409                 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
410                 setuplog(logstr);
411             }
412         }
413
414         if (cmd == DSIFUNC_TICKLE) {
415             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
416             if ((child.flags & CHILD_DIE))
417                 dsi_tickle(dsi);
418             pending_request(dsi);
419             continue;
420         } 
421
422         child.flags |= CHILD_DATA;
423         switch(cmd) {
424         case DSIFUNC_CLOSE:
425             afp_dsi_close(obj);
426             LOG(log_info, logtype_afpd, "done");
427             return;
428             break;
429
430         case DSIFUNC_CMD:
431 #ifdef AFS
432             if ( writtenfork ) {
433                 if ( flushfork( writtenfork ) < 0 ) {
434                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
435                 }
436                 writtenfork = NULL;
437             }
438 #endif /* AFS */
439
440             function = (u_char) dsi->commands[0];
441
442             /* send off an afp command. in a couple cases, we take advantage
443              * of the fact that we're a stream-based protocol. */
444             if (afp_switch[function]) {
445                 dsi->datalen = DSI_DATASIZ;
446                 child.flags |= CHILD_RUNNING;
447
448                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
449
450                 err = (*afp_switch[function])(obj,
451                                               (char *)&dsi->commands, dsi->cmdlen,
452                                               (char *)&dsi->data, &dsi->datalen);
453
454                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
455                     AfpNum2name(function), AfpErr2name(err));
456 #ifdef FORCE_UIDGID
457                 /* bring everything back to old euid, egid */
458                 if (obj->force_uid)
459                     restore_uidgid ( &obj->uidgid );
460 #endif /* FORCE_UIDGID */
461                 child.flags &= ~CHILD_RUNNING;
462             } else {
463                 LOG(log_error, logtype_afpd, "bad function %X", function);
464                 dsi->datalen = 0;
465                 err = AFPERR_NOOP;
466             }
467
468             /* single shot toggle that gets set by dsi_readinit. */
469             if (dsi->noreply) {
470                 dsi->noreply = 0;
471                 break;
472             }
473
474             if (!dsi_cmdreply(dsi, err)) {
475                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
476                 afp_dsi_die(EXITERR_CLNT);
477             }
478             break;
479
480         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
481             function = (u_char) dsi->commands[0];
482             if ( afp_switch[ function ] != NULL ) {
483                 dsi->datalen = DSI_DATASIZ;
484                 child.flags |= CHILD_RUNNING;
485
486                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
487
488                 err = (*afp_switch[function])(obj,
489                                               (char *)&dsi->commands, dsi->cmdlen,
490                                               (char *)&dsi->data, &dsi->datalen);
491
492                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
493                     AfpNum2name(function), AfpErr2name(err));
494
495                 child.flags &= ~CHILD_RUNNING;
496 #ifdef FORCE_UIDGID
497                 /* bring everything back to old euid, egid */
498                 if (obj->force_uid)
499                     restore_uidgid ( &obj->uidgid );
500 #endif /* FORCE_UIDGID */
501             } else {
502                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
503                 dsi->datalen = 0;
504                 err = AFPERR_NOOP;
505             }
506
507             if (!dsi_wrtreply(dsi, err)) {
508                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
509                 afp_dsi_die(EXITERR_CLNT);
510             }
511             break;
512
513         case DSIFUNC_ATTN: /* attention replies */
514             break;
515
516             /* error. this usually implies a mismatch of some kind
517              * between server and client. if things are correct,
518              * we need to flush the rest of the packet if necessary. */
519         default:
520             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
521             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
522             dsi_writeflush(dsi);
523             break;
524         }
525         pending_request(dsi);
526     }
527
528     /* error */
529     afp_dsi_die(EXITERR_CLNT);
530 }