]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
remove SIGUSR2 (message) from the list of blocked signals when writing to the client...
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.43 2009-10-22 05:53:20 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     DSI *dsi = (DSI *) child.obj->handle;
167
168     dsi->msg_request = 1;
169     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
170 }
171 #endif /* SERVERTEXT */
172
173 static void alarm_handler(int sig _U_)
174 {
175     int err;
176     DSI *dsi = (DSI *) child.obj->handle;
177
178     /* we have to restart the timer because some libraries 
179      * may use alarm() */
180     setitimer(ITIMER_REAL, &dsi->timer, NULL);
181
182     /* if we're in the midst of processing something,
183        don't die. */
184     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
185         return;
186     } 
187         
188     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
189         if (!(err = pollvoltime(child.obj)))
190             err = dsi_tickle(child.obj->handle);
191         if (err <= 0) 
192             afp_dsi_die(EXITERR_CLNT);
193         
194     } else { /* didn't receive a tickle. close connection */
195         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
196         afp_dsi_die(EXITERR_CLNT);
197     }
198 }
199
200 /* ----------------- 
201    if dsi->in_write is set attention, tickle (and close?) msg
202    aren't sent. We don't care about tickle 
203 */
204 static void pending_request(DSI *dsi)
205 {
206     /* send pending attention */
207
208     /* read msg if any, it could be done in afp_getsrvrmesg */
209     if (dsi->msg_request) {
210         dsi->msg_request = 0;
211         readmessage(child.obj);
212     }
213 }
214
215 /* -------------------------------------------
216  afp over dsi. this never returns. 
217 */
218 void afp_over_dsi(AFPObj *obj)
219 {
220     DSI *dsi = (DSI *) obj->handle;
221     u_int32_t err, cmd;
222     u_int8_t function;
223     struct sigaction action;
224     const char *afpcmpstr;
225
226     obj->exit = afp_dsi_die;
227     obj->reply = (int (*)()) dsi_cmdreply;
228     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
229
230     obj->sleep = afp_dsi_sleep;
231     child.obj = obj;
232     child.tickle = child.flags = 0;
233
234     memset(&action, 0, sizeof(action));
235
236     /* install SIGHUP */
237     action.sa_handler = afp_dsi_reload;
238     sigemptyset( &action.sa_mask );
239     sigaddset(&action.sa_mask, SIGALRM);
240     sigaddset(&action.sa_mask, SIGTERM);
241     sigaddset(&action.sa_mask, SIGUSR1);
242 #ifdef SERVERTEXT
243     sigaddset(&action.sa_mask, SIGUSR2);
244 #endif    
245     action.sa_flags = SA_RESTART;
246     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
247         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
248         afp_dsi_die(EXITERR_SYS);
249     }
250
251     /* install SIGTERM */
252     action.sa_handler = afp_dsi_die;
253     sigemptyset( &action.sa_mask );
254     sigaddset(&action.sa_mask, SIGALRM);
255     sigaddset(&action.sa_mask, SIGHUP);
256     sigaddset(&action.sa_mask, SIGUSR1);
257 #ifdef SERVERTEXT
258     sigaddset(&action.sa_mask, SIGUSR2);
259 #endif    
260     action.sa_flags = SA_RESTART;
261     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
262         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
263         afp_dsi_die(EXITERR_SYS);
264     }
265
266 #ifdef SERVERTEXT
267     /* Added for server message support */
268     action.sa_handler = afp_dsi_getmesg;
269     sigemptyset( &action.sa_mask );
270     sigaddset(&action.sa_mask, SIGALRM);
271     sigaddset(&action.sa_mask, SIGTERM);
272     sigaddset(&action.sa_mask, SIGUSR1);
273     sigaddset(&action.sa_mask, SIGHUP);
274     action.sa_flags = SA_RESTART;
275     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
276         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
277         afp_dsi_die(EXITERR_SYS);
278     }
279 #endif /* SERVERTEXT */
280
281     /*  SIGUSR1 - set down in 5 minutes  */
282     action.sa_handler = afp_dsi_timedown;
283     sigemptyset( &action.sa_mask );
284     sigaddset(&action.sa_mask, SIGALRM);
285     sigaddset(&action.sa_mask, SIGHUP);
286     sigaddset(&action.sa_mask, SIGTERM);
287 #ifdef SERVERTEXT
288     sigaddset(&action.sa_mask, SIGUSR2);
289 #endif    
290     action.sa_flags = SA_RESTART;
291     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
292         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
293         afp_dsi_die(EXITERR_SYS);
294     }
295
296 #ifndef DEBUGGING
297     /* tickle handler */
298     action.sa_handler = alarm_handler;
299     sigemptyset(&action.sa_mask);
300     sigaddset(&action.sa_mask, SIGHUP);
301     sigaddset(&action.sa_mask, SIGTERM);
302     sigaddset(&action.sa_mask, SIGUSR1);
303 #ifdef SERVERTEXT
304     sigaddset(&action.sa_mask, SIGUSR2);
305 #endif    
306     action.sa_flags = SA_RESTART;
307     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
308             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
309         afp_dsi_die(EXITERR_SYS);
310     }
311 #endif /* DEBUGGING */
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             pending_request(dsi);
328             continue;
329         } 
330         switch(cmd) {
331         case DSIFUNC_CLOSE:
332             afp_dsi_close(obj);
333             LOG(log_info, logtype_afpd, "done");
334             return;
335             break;
336
337         case DSIFUNC_CMD:
338 #ifdef AFS
339             if ( writtenfork ) {
340                 if ( flushfork( writtenfork ) < 0 ) {
341                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
342                 }
343                 writtenfork = NULL;
344             }
345 #endif /* AFS */
346
347             function = (u_char) dsi->commands[0];
348
349             /* send off an afp command. in a couple cases, we take advantage
350              * of the fact that we're a stream-based protocol. */
351             if (afp_switch[function]) {
352                 dsi->datalen = DSI_DATASIZ;
353                 child.flags |= CHILD_RUNNING;
354
355                 afpcmpstr = AfpNum2name(function);
356                 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
357
358                 err = (*afp_switch[function])(obj,
359                                               dsi->commands, dsi->cmdlen,
360                                               dsi->data, &dsi->datalen);
361
362                 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
363 #ifdef FORCE_UIDGID
364                 /* bring everything back to old euid, egid */
365                 if (obj->force_uid)
366                     restore_uidgid ( &obj->uidgid );
367 #endif /* FORCE_UIDGID */
368                 child.flags &= ~CHILD_RUNNING;
369             } else {
370                 LOG(log_error, logtype_afpd, "bad function %X", function);
371                 dsi->datalen = 0;
372                 err = AFPERR_NOOP;
373             }
374
375             /* single shot toggle that gets set by dsi_readinit. */
376             if (dsi->noreply) {
377                 dsi->noreply = 0;
378                 break;
379             }
380
381             if (!dsi_cmdreply(dsi, err)) {
382                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
383                 afp_dsi_die(EXITERR_CLNT);
384             }
385             break;
386
387         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
388             function = (u_char) dsi->commands[0];
389             if ( afp_switch[ function ] != NULL ) {
390                 dsi->datalen = DSI_DATASIZ;
391                 child.flags |= CHILD_RUNNING;
392                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
393                                               dsi->data, &dsi->datalen);
394                 child.flags &= ~CHILD_RUNNING;
395 #ifdef FORCE_UIDGID
396                 /* bring everything back to old euid, egid */
397                 if (obj->force_uid)
398                     restore_uidgid ( &obj->uidgid );
399 #endif /* FORCE_UIDGID */
400             } else {
401                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
402                 dsi->datalen = 0;
403                 err = AFPERR_NOOP;
404             }
405
406             if (!dsi_wrtreply(dsi, err)) {
407                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
408                 afp_dsi_die(EXITERR_CLNT);
409             }
410             break;
411
412         case DSIFUNC_ATTN: /* attention replies */
413             break;
414
415             /* error. this usually implies a mismatch of some kind
416              * between server and client. if things are correct,
417              * we need to flush the rest of the packet if necessary. */
418         default:
419             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
420             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
421             dsi_writeflush(dsi);
422             break;
423         }
424         pending_request(dsi);
425     }
426
427     /* error */
428     afp_dsi_die(EXITERR_CLNT);
429 }