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