]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
don't reset timer each time afpd get a request
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.41 2009-10-21 07:03:08 didg Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5  * Copyright (c) 1990,1993 Regents of The University of Michigan.
6  * All Rights Reserved.  See COPYRIGHT.
7  *
8  * modified from main.c. this handles afp over tcp.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <errno.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <atalk/logger.h>
31
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
35
36 #include "globals.h"
37 #include "switch.h"
38 #include "auth.h"
39 #include "fork.h"
40
41 #ifdef FORCE_UIDGID
42 #warning UIDGID
43 #include "uid.h"
44 #endif /* FORCE_UIDGID */
45
46 #define CHILD_DIE         (1 << 0)
47 #define CHILD_RUNNING     (1 << 1)
48 #define CHILD_SLEEPING    (1 << 2)
49 #define CHILD_DATA        (1 << 3)
50
51 static struct {
52     AFPObj *obj;
53     unsigned char flags;
54     int tickle;
55 } child;
56
57
58 static void afp_dsi_close(AFPObj *obj)
59 {
60     DSI *dsi = obj->handle;
61
62     close_all_vol();
63     if (obj->logout)
64         (*obj->logout)();
65
66     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
67         dsi->read_count/1024.0, dsi->write_count/1024.0);
68
69     dsi_close(dsi);
70 }
71
72 /* -------------------------------
73  * SIGTERM
74  * a little bit of code duplication. 
75  */
76 static void afp_dsi_die(int sig)
77 {
78 static volatile int in_handler;
79     
80     if (in_handler) {
81         return;
82     }
83     /* it's not atomic but we don't care because it's an exit function
84      * ie if a signal is received here, between the test and the affectation,
85      * it will not return.
86     */
87     in_handler = 1;
88
89     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
90     afp_dsi_close(child.obj);
91     if (sig) /* if no signal, assume dieing because logins are disabled &
92                 don't log it (maintenance mode)*/
93         LOG(log_info, logtype_afpd, "Connection terminated");
94     if (sig == SIGTERM || sig == SIGALRM) {
95         exit( 0 );
96     }
97     else {
98         exit(sig);
99     }
100 }
101
102 /* */
103 static void afp_dsi_sleep(void)
104 {
105     child.flags |= CHILD_SLEEPING;
106     dsi_sleep(child.obj->handle, 1);
107 }
108
109 /* ------------------- */
110 static void afp_dsi_timedown(int sig _U_)
111 {
112     struct sigaction    sv;
113     struct itimerval    it;
114
115     child.flags |= CHILD_DIE;
116     /* shutdown and don't reconnect. server going down in 5 minutes. */
117     setmessage("The server is going down for maintenance.");
118     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
119                   AFPATTN_MESG | AFPATTN_TIME(5));
120
121     it.it_interval.tv_sec = 0;
122     it.it_interval.tv_usec = 0;
123     it.it_value.tv_sec = 300;
124     it.it_value.tv_usec = 0;
125
126     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
127         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
128         afp_dsi_die(EXITERR_SYS);
129     }
130     memset(&sv, 0, sizeof(sv));
131     sv.sa_handler = afp_dsi_die;
132     sigemptyset( &sv.sa_mask );
133     sigaddset(&sv.sa_mask, SIGHUP);
134     sigaddset(&sv.sa_mask, SIGTERM);
135     sv.sa_flags = SA_RESTART;
136     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
137         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
138         afp_dsi_die(EXITERR_SYS);
139     }
140
141     /* ignore myself */
142     sv.sa_handler = SIG_IGN;
143     sigemptyset( &sv.sa_mask );
144     sv.sa_flags = SA_RESTART;
145     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
146         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
147         afp_dsi_die(EXITERR_SYS);
148     }
149
150 }
151
152 /* ---------------------------------
153  * SIGHUP reload configuration file
154  * FIXME here or we wait ?
155 */
156 volatile int reload_request = 0;
157
158 static void afp_dsi_reload(int sig _U_)
159 {
160     reload_request = 1;
161 }
162
163 /* ---------------------- */
164 #ifdef SERVERTEXT
165 static void afp_dsi_getmesg (int sig _U_)
166 {
167     readmessage(child.obj);
168     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
169 }
170 #endif /* SERVERTEXT */
171
172 static void alarm_handler(int sig _U_)
173 {
174     int err;
175     DSI *dsi = (DSI *) child.obj->handle;
176
177     /* we have to restart the timer because some libraries 
178      * may use alarm() */
179     setitimer(ITIMER_REAL, &dsi->timer, NULL);
180
181     /* we got some traffic from the client since the previous timer 
182      * tick. */
183     if ((child.flags & CHILD_DATA)) {
184         child.flags &= ~CHILD_DATA;
185         return;
186     }
187
188     /* if we're in the midst of processing something,
189        don't die. */
190     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
191         return;
192     } 
193         
194     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
195         if (!(err = pollvoltime(child.obj)))
196             err = dsi_tickle(child.obj->handle);
197         if (err <= 0) 
198             afp_dsi_die(EXITERR_CLNT);
199         
200     } else { /* didn't receive a tickle. close connection */
201         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
202         afp_dsi_die(EXITERR_CLNT);
203     }
204 }
205
206
207 #ifdef DEBUG1
208 /*  ---------------------------------
209  *  old signal handler for SIGUSR1 - set the debug flag and 
210  *  redirect stdout to <tmpdir>/afpd-debug-<pid>.
211  */
212 void afp_set_debug (int sig)
213 {
214     char        fname[MAXPATHLEN];
215
216     snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
217     freopen(fname, "w", stdout);
218     child.obj->options.flags |= OPTION_DEBUG;
219
220     return;
221 }
222 #endif
223
224 /* -------------------------------------------
225  afp over dsi. this never returns. 
226 */
227 void afp_over_dsi(AFPObj *obj)
228 {
229     DSI *dsi = (DSI *) obj->handle;
230     u_int32_t err, cmd;
231     u_int8_t function;
232     struct sigaction action;
233     const char *afpcmpstr;
234
235     obj->exit = afp_dsi_die;
236     obj->reply = (int (*)()) dsi_cmdreply;
237     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
238
239     obj->sleep = afp_dsi_sleep;
240     child.obj = obj;
241     child.tickle = child.flags = 0;
242
243     memset(&action, 0, sizeof(action));
244
245     /* install SIGHUP */
246     action.sa_handler = afp_dsi_reload;
247     sigemptyset( &action.sa_mask );
248     sigaddset(&action.sa_mask, SIGALRM);
249     sigaddset(&action.sa_mask, SIGTERM);
250     sigaddset(&action.sa_mask, SIGUSR1);
251 #ifdef SERVERTEXT
252     sigaddset(&action.sa_mask, SIGUSR2);
253 #endif    
254     action.sa_flags = SA_RESTART;
255     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
256         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
257         afp_dsi_die(EXITERR_SYS);
258     }
259
260     /* install SIGTERM */
261     action.sa_handler = afp_dsi_die;
262     sigemptyset( &action.sa_mask );
263     sigaddset(&action.sa_mask, SIGALRM);
264     sigaddset(&action.sa_mask, SIGHUP);
265     sigaddset(&action.sa_mask, SIGUSR1);
266 #ifdef SERVERTEXT
267     sigaddset(&action.sa_mask, SIGUSR2);
268 #endif    
269     action.sa_flags = SA_RESTART;
270     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
271         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
272         afp_dsi_die(EXITERR_SYS);
273     }
274
275 #ifdef SERVERTEXT
276     /* Added for server message support */
277     action.sa_handler = afp_dsi_getmesg;
278     sigemptyset( &action.sa_mask );
279     sigaddset(&action.sa_mask, SIGALRM);
280     sigaddset(&action.sa_mask, SIGTERM);
281     sigaddset(&action.sa_mask, SIGUSR1);
282     sigaddset(&action.sa_mask, SIGHUP);
283     action.sa_flags = SA_RESTART;
284     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
285         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
286         afp_dsi_die(EXITERR_SYS);
287     }
288 #endif /* SERVERTEXT */
289
290     /*  SIGUSR1 - set down in 5 minutes  */
291     action.sa_handler = afp_dsi_timedown;
292     sigemptyset( &action.sa_mask );
293     sigaddset(&action.sa_mask, SIGALRM);
294     sigaddset(&action.sa_mask, SIGHUP);
295     sigaddset(&action.sa_mask, SIGTERM);
296 #ifdef SERVERTEXT
297     sigaddset(&action.sa_mask, SIGUSR2);
298 #endif    
299     action.sa_flags = SA_RESTART;
300     if ( sigaction( SIGUSR1, &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 #ifndef DEBUGGING
306     /* tickle handler */
307     action.sa_handler = alarm_handler;
308     sigemptyset(&action.sa_mask);
309     sigaddset(&action.sa_mask, SIGHUP);
310     sigaddset(&action.sa_mask, SIGTERM);
311     sigaddset(&action.sa_mask, SIGUSR1);
312 #ifdef SERVERTEXT
313     sigaddset(&action.sa_mask, SIGUSR2);
314 #endif    
315     action.sa_flags = SA_RESTART;
316     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
317             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
318         afp_dsi_die(EXITERR_SYS);
319     }
320 #endif /* DEBUGGING */
321
322 #ifdef DEBUG1
323     fault_setup((void (*)(void *))afp_dsi_die);
324 #endif
325
326     /* get stuck here until the end */
327     while ((cmd = dsi_receive(dsi))) {
328         child.tickle = 0;
329         child.flags &= ~CHILD_SLEEPING;
330         dsi_sleep(dsi, 0); /* wake up */
331         if (reload_request) {
332             reload_request = 0;
333             load_volumes(child.obj);
334         }
335
336         if (cmd == DSIFUNC_TICKLE) {
337             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
338             if ((child.flags & CHILD_DIE))
339                 dsi_tickle(dsi);
340             continue;
341         } 
342         child.flags |= CHILD_DATA;
343         switch(cmd) {
344         case DSIFUNC_CLOSE:
345             afp_dsi_close(obj);
346             LOG(log_info, logtype_afpd, "done");
347 #ifdef DEBUG1
348             if (obj->options.flags & OPTION_DEBUG )
349                 printf("done\n");
350 #endif                
351             return;
352             break;
353
354         case DSIFUNC_CMD:
355 #ifdef AFS
356             if ( writtenfork ) {
357                 if ( flushfork( writtenfork ) < 0 ) {
358                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
359                 }
360                 writtenfork = NULL;
361             }
362 #endif /* AFS */
363
364             function = (u_char) dsi->commands[0];
365 #ifdef DEBUG1
366             if (obj->options.flags & OPTION_DEBUG ) {
367                 printf("command: %d (%s)\n", function, AfpNum2name(function));
368                 bprint((char *) dsi->commands, dsi->cmdlen);
369             }
370 #endif            
371
372             /* send off an afp command. in a couple cases, we take advantage
373              * of the fact that we're a stream-based protocol. */
374             if (afp_switch[function]) {
375                 dsi->datalen = DSI_DATASIZ;
376                 child.flags |= CHILD_RUNNING;
377
378                 afpcmpstr = AfpNum2name(function);
379                 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
380
381                 err = (*afp_switch[function])(obj,
382                                               dsi->commands, dsi->cmdlen,
383                                               dsi->data, &dsi->datalen);
384
385                 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
386 #ifdef FORCE_UIDGID
387                 /* bring everything back to old euid, egid */
388                 if (obj->force_uid)
389                     restore_uidgid ( &obj->uidgid );
390 #endif /* FORCE_UIDGID */
391                 child.flags &= ~CHILD_RUNNING;
392             } else {
393                 LOG(log_error, logtype_afpd, "bad function %X", function);
394                 dsi->datalen = 0;
395                 err = AFPERR_NOOP;
396             }
397
398             /* single shot toggle that gets set by dsi_readinit. */
399             if (dsi->noreply) {
400                 dsi->noreply = 0;
401                 break;
402             }
403
404 #ifdef DEBUG1
405             if (obj->options.flags & OPTION_DEBUG ) {
406                 printf( "reply: %d, %d\n", err, dsi->clientID);
407                 bprint((char *) dsi->data, dsi->datalen);
408             }
409 #endif
410             if (!dsi_cmdreply(dsi, err)) {
411                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
412                 afp_dsi_die(EXITERR_CLNT);
413             }
414             break;
415
416         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
417             function = (u_char) dsi->commands[0];
418 #ifdef DEBUG1
419             if ( obj->options.flags & OPTION_DEBUG ) {
420                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
421                 bprint((char *) dsi->commands, dsi->cmdlen);
422             }
423 #endif
424             if ( afp_switch[ function ] != NULL ) {
425                 dsi->datalen = DSI_DATASIZ;
426                 child.flags |= CHILD_RUNNING;
427                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
428                                               dsi->data, &dsi->datalen);
429                 child.flags &= ~CHILD_RUNNING;
430 #ifdef FORCE_UIDGID
431                 /* bring everything back to old euid, egid */
432                 if (obj->force_uid)
433                     restore_uidgid ( &obj->uidgid );
434 #endif /* FORCE_UIDGID */
435             } else {
436                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
437                 dsi->datalen = 0;
438                 err = AFPERR_NOOP;
439             }
440
441 #ifdef DEBUG1
442             if (obj->options.flags & OPTION_DEBUG ) {
443                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
444                 bprint((char *) dsi->data, dsi->datalen);
445             }
446 #endif
447             if (!dsi_wrtreply(dsi, err)) {
448                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
449                 afp_dsi_die(EXITERR_CLNT);
450             }
451             break;
452
453         case DSIFUNC_ATTN: /* attention replies */
454             continue;
455             break;
456
457             /* error. this usually implies a mismatch of some kind
458              * between server and client. if things are correct,
459              * we need to flush the rest of the packet if necessary. */
460         default:
461             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
462             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
463             dsi_writeflush(dsi);
464             break;
465         }
466 #ifdef DEBUG1
467         if ( obj->options.flags & OPTION_DEBUG ) {
468 #ifdef notdef
469             pdesc( stdout );
470 #endif /* notdef */
471             of_pforkdesc( stdout );
472             fflush( stdout );
473         }
474 #endif
475     }
476
477     /* error */
478     afp_dsi_die(EXITERR_CLNT);
479 }