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