]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
remove gcc warnings and cleanup inline mess
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.27.2.3.2.4.2.3 2008-11-25 15:16:32 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     /* UAM had syslog control; afpd needs to reassert itself */
68     set_processname("afpd");
69     syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
70     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
71         dsi->read_count/1024.0, dsi->write_count/1024.0);
72
73     dsi_close(dsi);
74 }
75
76 /* -------------------------------
77  * SIGTERM
78  * a little bit of code duplication. 
79  */
80 static void afp_dsi_die(int sig)
81 {
82 static volatile int in_handler;
83     
84     if (in_handler) {
85         return;
86     }
87     /* it's not atomic but we don't care because it's an exit function
88      * ie if a signal is received here, between the test and the affectation,
89      * it will not return.
90     */
91     in_handler = 1;
92
93     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
94     afp_dsi_close(child.obj);
95     if (sig) /* if no signal, assume dieing because logins are disabled &
96                 don't log it (maintenance mode)*/
97         LOG(log_info, logtype_afpd, "Connection terminated");
98     if (sig == SIGTERM || sig == SIGALRM) {
99         exit( 0 );
100     }
101     else {
102         exit(sig);
103     }
104 }
105
106 /* */
107 static void afp_dsi_sleep(void)
108 {
109     child.flags |= CHILD_SLEEPING;
110     dsi_sleep(child.obj->handle, 1);
111 }
112
113 /* ------------------- */
114 static void afp_dsi_timedown()
115 {
116     struct sigaction    sv;
117     struct itimerval    it;
118
119     child.flags |= CHILD_DIE;
120     /* shutdown and don't reconnect. server going down in 5 minutes. */
121     setmessage("The server is going down for maintenance.");
122     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
123                   AFPATTN_MESG | AFPATTN_TIME(5));
124
125     it.it_interval.tv_sec = 0;
126     it.it_interval.tv_usec = 0;
127     it.it_value.tv_sec = 300;
128     it.it_value.tv_usec = 0;
129
130     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
131         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
132         afp_dsi_die(EXITERR_SYS);
133     }
134     memset(&sv, 0, sizeof(sv));
135     sv.sa_handler = afp_dsi_die;
136     sigemptyset( &sv.sa_mask );
137     sigaddset(&sv.sa_mask, SIGHUP);
138     sigaddset(&sv.sa_mask, SIGTERM);
139     sv.sa_flags = SA_RESTART;
140     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
141         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
142         afp_dsi_die(EXITERR_SYS);
143     }
144
145     /* ignore myself */
146     sv.sa_handler = SIG_IGN;
147     sigemptyset( &sv.sa_mask );
148     sv.sa_flags = SA_RESTART;
149     if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
150         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
151         afp_dsi_die(EXITERR_SYS);
152     }
153
154 }
155
156 /* ---------------------------------
157  * SIGHUP reload configuration file
158  * FIXME here or we wait ?
159 */
160 volatile int reload_request = 0;
161
162 static void afp_dsi_reload()
163 {
164     reload_request = 1;
165 }
166
167 /* ---------------------- */
168 #ifdef SERVERTEXT
169 static void afp_dsi_getmesg (int sig _U_)
170 {
171     readmessage(child.obj);
172     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
173 }
174 #endif /* SERVERTEXT */
175
176 static void alarm_handler()
177 {
178     int err;
179
180     /* if we're in the midst of processing something,
181        don't die. */
182     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
183         return;
184     } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
185         if (!(err = pollvoltime(child.obj)))
186             err = dsi_tickle(child.obj->handle);
187         if (err <= 0) 
188             afp_dsi_die(EXITERR_CLNT);
189         
190     } else { /* didn't receive a tickle. close connection */
191         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
192         afp_dsi_die(EXITERR_CLNT);
193     }
194 }
195
196
197 #ifdef DEBUG1
198 /*  ---------------------------------
199  *  old signal handler for SIGUSR1 - set the debug flag and 
200  *  redirect stdout to <tmpdir>/afpd-debug-<pid>.
201  */
202 void afp_set_debug (int sig)
203 {
204     char        fname[MAXPATHLEN];
205
206     snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
207     freopen(fname, "w", stdout);
208     child.obj->options.flags |= OPTION_DEBUG;
209
210     return;
211 }
212 #endif
213
214 /* -------------------------------------------
215  afp over dsi. this never returns. 
216 */
217 void afp_over_dsi(AFPObj *obj)
218 {
219     DSI *dsi = (DSI *) obj->handle;
220     u_int32_t err, cmd;
221     u_int8_t function;
222     struct sigaction action;
223
224     obj->exit = afp_dsi_die;
225     obj->reply = (int (*)()) dsi_cmdreply;
226     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
227
228     obj->sleep = afp_dsi_sleep;
229     child.obj = obj;
230     child.tickle = child.flags = 0;
231
232     memset(&action, 0, sizeof(action));
233
234     /* install SIGHUP */
235     action.sa_handler = afp_dsi_reload;
236     sigemptyset( &action.sa_mask );
237     sigaddset(&action.sa_mask, SIGALRM);
238     sigaddset(&action.sa_mask, SIGTERM);
239     sigaddset(&action.sa_mask, SIGUSR1);
240 #ifdef SERVERTEXT
241     sigaddset(&action.sa_mask, SIGUSR2);
242 #endif    
243     action.sa_flags = SA_RESTART;
244     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
245         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
246         afp_dsi_die(EXITERR_SYS);
247     }
248
249     /* install SIGTERM */
250     action.sa_handler = afp_dsi_die;
251     sigemptyset( &action.sa_mask );
252     sigaddset(&action.sa_mask, SIGALRM);
253     sigaddset(&action.sa_mask, SIGHUP);
254     sigaddset(&action.sa_mask, SIGUSR1);
255 #ifdef SERVERTEXT
256     sigaddset(&action.sa_mask, SIGUSR2);
257 #endif    
258     action.sa_flags = SA_RESTART;
259     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
260         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
261         afp_dsi_die(EXITERR_SYS);
262     }
263
264 #ifdef SERVERTEXT
265     /* Added for server message support */
266     action.sa_handler = afp_dsi_getmesg;
267     sigemptyset( &action.sa_mask );
268     sigaddset(&action.sa_mask, SIGALRM);
269     sigaddset(&action.sa_mask, SIGTERM);
270     sigaddset(&action.sa_mask, SIGUSR1);
271     sigaddset(&action.sa_mask, SIGHUP);
272     action.sa_flags = SA_RESTART;
273     if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
274         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
275         afp_dsi_die(EXITERR_SYS);
276     }
277 #endif /* SERVERTEXT */
278
279     /*  SIGUSR1 - set down in 5 minutes  */
280     action.sa_handler = afp_dsi_timedown;
281     sigemptyset( &action.sa_mask );
282     sigaddset(&action.sa_mask, SIGALRM);
283     sigaddset(&action.sa_mask, SIGHUP);
284     sigaddset(&action.sa_mask, SIGTERM);
285 #ifdef SERVERTEXT
286     sigaddset(&action.sa_mask, SIGUSR2);
287 #endif    
288     action.sa_flags = SA_RESTART;
289     if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
290         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
291         afp_dsi_die(EXITERR_SYS);
292     }
293
294     /* tickle handler */
295     action.sa_handler = alarm_handler;
296     sigemptyset(&action.sa_mask);
297     sigaddset(&action.sa_mask, SIGHUP);
298     sigaddset(&action.sa_mask, SIGTERM);
299     sigaddset(&action.sa_mask, SIGUSR1);
300 #ifdef SERVERTEXT
301     sigaddset(&action.sa_mask, SIGUSR2);
302 #endif    
303     action.sa_flags = SA_RESTART;
304     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
305             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
306         afp_dsi_die(EXITERR_SYS);
307     }
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             setitimer(ITIMER_REAL, &dsi->timer, NULL);
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                 err = (*afp_switch[function])(obj,
367                                               dsi->commands, dsi->cmdlen,
368                                               dsi->data, &dsi->datalen);
369 #ifdef FORCE_UIDGID
370                 /* bring everything back to old euid, egid */
371                 if (obj->force_uid)
372                     restore_uidgid ( &obj->uidgid );
373 #endif /* FORCE_UIDGID */
374                 child.flags &= ~CHILD_RUNNING;
375             } else {
376                 LOG(log_error, logtype_afpd, "bad function %X", function);
377                 dsi->datalen = 0;
378                 err = AFPERR_NOOP;
379             }
380
381             /* single shot toggle that gets set by dsi_readinit. */
382             if (dsi->noreply) {
383                 dsi->noreply = 0;
384                 break;
385             }
386
387 #ifdef DEBUG1
388             if (obj->options.flags & OPTION_DEBUG ) {
389                 printf( "reply: %d, %d\n", err, dsi->clientID);
390                 bprint((char *) dsi->data, dsi->datalen);
391             }
392 #endif
393             if (!dsi_cmdreply(dsi, err)) {
394                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
395                 afp_dsi_die(EXITERR_CLNT);
396             }
397             break;
398
399         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
400             function = (u_char) dsi->commands[0];
401 #ifdef DEBUG1
402             if ( obj->options.flags & OPTION_DEBUG ) {
403                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
404                 bprint((char *) dsi->commands, dsi->cmdlen);
405             }
406 #endif
407             if ( afp_switch[ function ] != NULL ) {
408                 dsi->datalen = DSI_DATASIZ;
409                 child.flags |= CHILD_RUNNING;
410                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
411                                               dsi->data, &dsi->datalen);
412                 child.flags &= ~CHILD_RUNNING;
413 #ifdef FORCE_UIDGID
414                 /* bring everything back to old euid, egid */
415                 if (obj->force_uid)
416                     restore_uidgid ( &obj->uidgid );
417 #endif /* FORCE_UIDGID */
418             } else {
419                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
420                 dsi->datalen = 0;
421                 err = AFPERR_NOOP;
422             }
423
424 #ifdef DEBUG1
425             if (obj->options.flags & OPTION_DEBUG ) {
426                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
427                 bprint((char *) dsi->data, dsi->datalen);
428             }
429 #endif
430             if (!dsi_wrtreply(dsi, err)) {
431                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
432                 afp_dsi_die(EXITERR_CLNT);
433             }
434             break;
435
436         case DSIFUNC_ATTN: /* attention replies */
437             continue;
438             break;
439
440             /* error. this usually implies a mismatch of some kind
441              * between server and client. if things are correct,
442              * we need to flush the rest of the packet if necessary. */
443         default:
444             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
445             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
446             dsi_writeflush(dsi);
447             break;
448         }
449 #ifdef DEBUG1
450         if ( obj->options.flags & OPTION_DEBUG ) {
451 #ifdef notdef
452             pdesc( stdout );
453 #endif /* notdef */
454             of_pforkdesc( stdout );
455             fflush( stdout );
456         }
457 #endif
458     }
459
460     /* error */
461     afp_dsi_die(EXITERR_CLNT);
462 }