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