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