]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
remove trivial warnings
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.37 2009-07-21 13:41:16 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 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     const char *afpcmpstr;
221
222     obj->exit = afp_dsi_die;
223     obj->reply = (int (*)()) dsi_cmdreply;
224     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
225
226     obj->sleep = afp_dsi_sleep;
227     child.obj = obj;
228     child.tickle = child.flags = 0;
229
230     memset(&action, 0, sizeof(action));
231
232     /* install SIGHUP */
233     action.sa_handler = afp_dsi_reload;
234     sigemptyset( &action.sa_mask );
235     sigaddset(&action.sa_mask, SIGALRM);
236     sigaddset(&action.sa_mask, SIGTERM);
237     sigaddset(&action.sa_mask, SIGUSR1);
238 #ifdef SERVERTEXT
239     sigaddset(&action.sa_mask, SIGUSR2);
240 #endif    
241     action.sa_flags = SA_RESTART;
242     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
243         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
244         afp_dsi_die(EXITERR_SYS);
245     }
246
247     /* install SIGTERM */
248     action.sa_handler = afp_dsi_die;
249     sigemptyset( &action.sa_mask );
250     sigaddset(&action.sa_mask, SIGALRM);
251     sigaddset(&action.sa_mask, SIGHUP);
252     sigaddset(&action.sa_mask, SIGUSR1);
253 #ifdef SERVERTEXT
254     sigaddset(&action.sa_mask, SIGUSR2);
255 #endif    
256     action.sa_flags = SA_RESTART;
257     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
258         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
259         afp_dsi_die(EXITERR_SYS);
260     }
261
262 #ifdef SERVERTEXT
263     /* Added for server message support */
264     action.sa_handler = afp_dsi_getmesg;
265     sigemptyset( &action.sa_mask );
266     sigaddset(&action.sa_mask, SIGALRM);
267     sigaddset(&action.sa_mask, SIGTERM);
268     sigaddset(&action.sa_mask, SIGUSR1);
269     sigaddset(&action.sa_mask, SIGHUP);
270     action.sa_flags = SA_RESTART;
271     if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
272         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
273         afp_dsi_die(EXITERR_SYS);
274     }
275 #endif /* SERVERTEXT */
276
277     /*  SIGUSR1 - set down in 5 minutes  */
278     action.sa_handler = afp_dsi_timedown;
279     sigemptyset( &action.sa_mask );
280     sigaddset(&action.sa_mask, SIGALRM);
281     sigaddset(&action.sa_mask, SIGHUP);
282     sigaddset(&action.sa_mask, SIGTERM);
283 #ifdef SERVERTEXT
284     sigaddset(&action.sa_mask, SIGUSR2);
285 #endif    
286     action.sa_flags = SA_RESTART;
287     if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
288         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
289         afp_dsi_die(EXITERR_SYS);
290     }
291
292 #ifndef DEBUGGING
293     /* tickle handler */
294     action.sa_handler = alarm_handler;
295     sigemptyset(&action.sa_mask);
296     sigaddset(&action.sa_mask, SIGHUP);
297     sigaddset(&action.sa_mask, SIGTERM);
298     sigaddset(&action.sa_mask, SIGUSR1);
299 #ifdef SERVERTEXT
300     sigaddset(&action.sa_mask, SIGUSR2);
301 #endif    
302     action.sa_flags = SA_RESTART;
303     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
304             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
305         afp_dsi_die(EXITERR_SYS);
306     }
307 #endif /* DEBUGGING */
308
309 #ifdef DEBUG1
310     fault_setup((void (*)(void *))afp_dsi_die);
311 #endif
312
313     /* get stuck here until the end */
314     while ((cmd = dsi_receive(dsi))) {
315         child.tickle = 0;
316         child.flags &= ~CHILD_SLEEPING;
317         dsi_sleep(dsi, 0); /* wake up */
318         if (reload_request) {
319             reload_request = 0;
320             load_volumes(child.obj);
321         }
322
323         if (cmd == DSIFUNC_TICKLE) {
324             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
325             if ((child.flags & CHILD_DIE))
326                 dsi_tickle(dsi);
327             continue;
328         } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
329 #ifndef DEBUGGING
330             setitimer(ITIMER_REAL, &dsi->timer, NULL);
331 #endif
332         }
333         switch(cmd) {
334         case DSIFUNC_CLOSE:
335             afp_dsi_close(obj);
336             LOG(log_info, logtype_afpd, "done");
337 #ifdef DEBUG1
338             if (obj->options.flags & OPTION_DEBUG )
339                 printf("done\n");
340 #endif                
341             return;
342             break;
343
344         case DSIFUNC_CMD:
345 #ifdef AFS
346             if ( writtenfork ) {
347                 if ( flushfork( writtenfork ) < 0 ) {
348                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
349                 }
350                 writtenfork = NULL;
351             }
352 #endif /* AFS */
353
354             function = (u_char) dsi->commands[0];
355 #ifdef DEBUG1
356             if (obj->options.flags & OPTION_DEBUG ) {
357                 printf("command: %d (%s)\n", function, AfpNum2name(function));
358                 bprint((char *) dsi->commands, dsi->cmdlen);
359             }
360 #endif            
361
362             /* send off an afp command. in a couple cases, we take advantage
363              * of the fact that we're a stream-based protocol. */
364             if (afp_switch[function]) {
365                 dsi->datalen = DSI_DATASIZ;
366                 child.flags |= CHILD_RUNNING;
367
368                 afpcmpstr = AfpNum2name(function);
369                 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
370
371                 err = (*afp_switch[function])(obj,
372                                               dsi->commands, dsi->cmdlen,
373                                               dsi->data, &dsi->datalen);
374
375                 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
376 #ifdef FORCE_UIDGID
377                 /* bring everything back to old euid, egid */
378                 if (obj->force_uid)
379                     restore_uidgid ( &obj->uidgid );
380 #endif /* FORCE_UIDGID */
381                 child.flags &= ~CHILD_RUNNING;
382             } else {
383                 LOG(log_error, logtype_afpd, "bad function %X", function);
384                 dsi->datalen = 0;
385                 err = AFPERR_NOOP;
386             }
387
388             /* single shot toggle that gets set by dsi_readinit. */
389             if (dsi->noreply) {
390                 dsi->noreply = 0;
391                 break;
392             }
393
394 #ifdef DEBUG1
395             if (obj->options.flags & OPTION_DEBUG ) {
396                 printf( "reply: %d, %d\n", err, dsi->clientID);
397                 bprint((char *) dsi->data, dsi->datalen);
398             }
399 #endif
400             if (!dsi_cmdreply(dsi, err)) {
401                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
402                 afp_dsi_die(EXITERR_CLNT);
403             }
404             break;
405
406         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
407             function = (u_char) dsi->commands[0];
408 #ifdef DEBUG1
409             if ( obj->options.flags & OPTION_DEBUG ) {
410                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
411                 bprint((char *) dsi->commands, dsi->cmdlen);
412             }
413 #endif
414             if ( afp_switch[ function ] != NULL ) {
415                 dsi->datalen = DSI_DATASIZ;
416                 child.flags |= CHILD_RUNNING;
417                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
418                                               dsi->data, &dsi->datalen);
419                 child.flags &= ~CHILD_RUNNING;
420 #ifdef FORCE_UIDGID
421                 /* bring everything back to old euid, egid */
422                 if (obj->force_uid)
423                     restore_uidgid ( &obj->uidgid );
424 #endif /* FORCE_UIDGID */
425             } else {
426                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
427                 dsi->datalen = 0;
428                 err = AFPERR_NOOP;
429             }
430
431 #ifdef DEBUG1
432             if (obj->options.flags & OPTION_DEBUG ) {
433                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
434                 bprint((char *) dsi->data, dsi->datalen);
435             }
436 #endif
437             if (!dsi_wrtreply(dsi, err)) {
438                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
439                 afp_dsi_die(EXITERR_CLNT);
440             }
441             break;
442
443         case DSIFUNC_ATTN: /* attention replies */
444             continue;
445             break;
446
447             /* error. this usually implies a mismatch of some kind
448              * between server and client. if things are correct,
449              * we need to flush the rest of the packet if necessary. */
450         default:
451             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
452             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
453             dsi_writeflush(dsi);
454             break;
455         }
456 #ifdef DEBUG1
457         if ( obj->options.flags & OPTION_DEBUG ) {
458 #ifdef notdef
459             pdesc( stdout );
460 #endif /* notdef */
461             of_pforkdesc( stdout );
462             fflush( stdout );
463         }
464 #endif
465     }
466
467     /* error */
468     afp_dsi_die(EXITERR_CLNT);
469 }