]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Reworked logger
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.34 2009-02-27 09:14:40 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     /* tickle handler */
292     action.sa_handler = alarm_handler;
293     sigemptyset(&action.sa_mask);
294     sigaddset(&action.sa_mask, SIGHUP);
295     sigaddset(&action.sa_mask, SIGTERM);
296     sigaddset(&action.sa_mask, SIGUSR1);
297 #ifdef SERVERTEXT
298     sigaddset(&action.sa_mask, SIGUSR2);
299 #endif    
300     action.sa_flags = SA_RESTART;
301     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
302             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
303         afp_dsi_die(EXITERR_SYS);
304     }
305
306 #ifdef DEBUG1
307     fault_setup((void (*)(void *))afp_dsi_die);
308 #endif
309
310     /* get stuck here until the end */
311     while ((cmd = dsi_receive(dsi))) {
312         child.tickle = 0;
313         child.flags &= ~CHILD_SLEEPING;
314         dsi_sleep(dsi, 0); /* wake up */
315         if (reload_request) {
316             reload_request = 0;
317             load_volumes(child.obj);
318         }
319
320         if (cmd == DSIFUNC_TICKLE) {
321             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
322             if ((child.flags & CHILD_DIE))
323                 dsi_tickle(dsi);
324             continue;
325         } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
326             setitimer(ITIMER_REAL, &dsi->timer, NULL);
327         }
328         switch(cmd) {
329         case DSIFUNC_CLOSE:
330             afp_dsi_close(obj);
331             LOG(log_info, logtype_afpd, "done");
332 #ifdef DEBUG1
333             if (obj->options.flags & OPTION_DEBUG )
334                 printf("done\n");
335 #endif                
336             return;
337             break;
338
339         case DSIFUNC_CMD:
340 #ifdef AFS
341             if ( writtenfork ) {
342                 if ( flushfork( writtenfork ) < 0 ) {
343                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
344                 }
345                 writtenfork = NULL;
346             }
347 #endif /* AFS */
348
349             function = (u_char) dsi->commands[0];
350 #ifdef DEBUG1
351             if (obj->options.flags & OPTION_DEBUG ) {
352                 printf("command: %d (%s)\n", function, AfpNum2name(function));
353                 bprint((char *) dsi->commands, dsi->cmdlen);
354             }
355 #endif            
356
357             /* send off an afp command. in a couple cases, we take advantage
358              * of the fact that we're a stream-based protocol. */
359             if (afp_switch[function]) {
360                 dsi->datalen = DSI_DATASIZ;
361                 child.flags |= CHILD_RUNNING;
362
363                 err = (*afp_switch[function])(obj,
364                                               dsi->commands, dsi->cmdlen,
365                                               dsi->data, &dsi->datalen);
366 #ifdef FORCE_UIDGID
367                 /* bring everything back to old euid, egid */
368                 if (obj->force_uid)
369                     restore_uidgid ( &obj->uidgid );
370 #endif /* FORCE_UIDGID */
371                 child.flags &= ~CHILD_RUNNING;
372             } else {
373                 LOG(log_error, logtype_afpd, "bad function %X", function);
374                 dsi->datalen = 0;
375                 err = AFPERR_NOOP;
376             }
377
378             /* single shot toggle that gets set by dsi_readinit. */
379             if (dsi->noreply) {
380                 dsi->noreply = 0;
381                 break;
382             }
383
384 #ifdef DEBUG1
385             if (obj->options.flags & OPTION_DEBUG ) {
386                 printf( "reply: %d, %d\n", err, dsi->clientID);
387                 bprint((char *) dsi->data, dsi->datalen);
388             }
389 #endif
390             if (!dsi_cmdreply(dsi, err)) {
391                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
392                 afp_dsi_die(EXITERR_CLNT);
393             }
394             break;
395
396         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
397             function = (u_char) dsi->commands[0];
398 #ifdef DEBUG1
399             if ( obj->options.flags & OPTION_DEBUG ) {
400                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
401                 bprint((char *) dsi->commands, dsi->cmdlen);
402             }
403 #endif
404             if ( afp_switch[ function ] != NULL ) {
405                 dsi->datalen = DSI_DATASIZ;
406                 child.flags |= CHILD_RUNNING;
407                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
408                                               dsi->data, &dsi->datalen);
409                 child.flags &= ~CHILD_RUNNING;
410 #ifdef FORCE_UIDGID
411                 /* bring everything back to old euid, egid */
412                 if (obj->force_uid)
413                     restore_uidgid ( &obj->uidgid );
414 #endif /* FORCE_UIDGID */
415             } else {
416                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
417                 dsi->datalen = 0;
418                 err = AFPERR_NOOP;
419             }
420
421 #ifdef DEBUG1
422             if (obj->options.flags & OPTION_DEBUG ) {
423                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
424                 bprint((char *) dsi->data, dsi->datalen);
425             }
426 #endif
427             if (!dsi_wrtreply(dsi, err)) {
428                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
429                 afp_dsi_die(EXITERR_CLNT);
430             }
431             break;
432
433         case DSIFUNC_ATTN: /* attention replies */
434             continue;
435             break;
436
437             /* error. this usually implies a mismatch of some kind
438              * between server and client. if things are correct,
439              * we need to flush the rest of the packet if necessary. */
440         default:
441             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
442             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
443             dsi_writeflush(dsi);
444             break;
445         }
446 #ifdef DEBUG1
447         if ( obj->options.flags & OPTION_DEBUG ) {
448 #ifdef notdef
449             pdesc( stdout );
450 #endif /* notdef */
451             of_pforkdesc( stdout );
452             fflush( stdout );
453         }
454 #endif
455     }
456
457     /* error */
458     afp_dsi_die(EXITERR_CLNT);
459 }