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