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