]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Start locking using Berkeley db
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3  * Copyright (c) 1990,1993 Regents of The University of Michigan.
4  * All Rights Reserved.  See COPYRIGHT.
5  *
6  * modified from main.c. this handles afp over tcp.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <errno.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/socket.h>
22 #include <sys/time.h>
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif /* HAVE_SYS_STAT_H */
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <atalk/logger.h>
29
30 #include <atalk/dsi.h>
31 #include <atalk/compat.h>
32 #include <atalk/util.h>
33 #include <atalk/locking.h>
34
35 #include "globals.h"
36 #include "switch.h"
37 #include "auth.h"
38 #include "fork.h"
39 #include "dircache.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, "AFP statistics: %.2f KB read, %.2f KB written",
89         dsi->read_count/1024.0, dsi->write_count/1024.0);
90     log_dircache_stat();
91
92     dsi_close(dsi);
93 }
94
95 /* -------------------------------
96  * SIGTERM
97  * a little bit of code duplication. 
98  */
99 static void afp_dsi_die(int sig)
100 {
101 static volatile int in_handler;
102     
103     if (in_handler) {
104         return;
105     }
106     /* it's not atomic but we don't care because it's an exit function
107      * ie if a signal is received here, between the test and the affectation,
108      * it will not return.
109     */
110     in_handler = 1;
111
112     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
113     afp_dsi_close(child.obj);
114     if (sig) /* if no signal, assume dieing because logins are disabled &
115                 don't log it (maintenance mode)*/
116         LOG(log_info, logtype_afpd, "Connection terminated");
117     if (sig == SIGTERM || sig == SIGALRM) {
118         exit( 0 );
119     }
120     else {
121         exit(sig);
122     }
123 }
124
125 /* */
126 static void afp_dsi_sleep(void)
127 {
128     child.flags |= CHILD_SLEEPING;
129     dsi_sleep(child.obj->handle, 1);
130 }
131
132 /* ------------------- */
133 static void afp_dsi_timedown(int sig _U_)
134 {
135     struct sigaction    sv;
136     struct itimerval    it;
137
138     child.flags |= CHILD_DIE;
139     /* shutdown and don't reconnect. server going down in 5 minutes. */
140     setmessage("The server is going down for maintenance.");
141     if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
142                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
143         DSI *dsi = (DSI *) child.obj->handle;
144         dsi->down_request = 1;
145     }                  
146
147     it.it_interval.tv_sec = 0;
148     it.it_interval.tv_usec = 0;
149     it.it_value.tv_sec = 300;
150     it.it_value.tv_usec = 0;
151
152     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
153         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
154         afp_dsi_die(EXITERR_SYS);
155     }
156     memset(&sv, 0, sizeof(sv));
157     sv.sa_handler = afp_dsi_die;
158     sigemptyset( &sv.sa_mask );
159     sigaddset(&sv.sa_mask, SIGHUP);
160     sigaddset(&sv.sa_mask, SIGTERM);
161     sv.sa_flags = SA_RESTART;
162     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
163         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
164         afp_dsi_die(EXITERR_SYS);
165     }
166
167     /* ignore myself */
168     sv.sa_handler = SIG_IGN;
169     sigemptyset( &sv.sa_mask );
170     sv.sa_flags = SA_RESTART;
171     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
172         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
173         afp_dsi_die(EXITERR_SYS);
174     }
175 }
176
177 /* ---------------------------------
178  * SIGHUP reload configuration file
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     if (dircache_init(obj->options.dircachesize) != 0)
380         afp_dsi_die(EXITERR_SYS);
381
382     /* get stuck here until the end */
383     while ((cmd = dsi_receive(dsi))) {
384         child.tickle = 0;
385         child.flags &= ~CHILD_SLEEPING;
386         dsi_sleep(dsi, 0); /* wake up */
387
388         if (reload_request) {
389             reload_request = 0;
390             load_volumes(child.obj);
391             dircache_dump();
392             log_dircache_stat();
393         }
394
395         /* The first SIGINT enables debugging, the next restores the config */
396         if (debug_request) {
397             static int debugging = 0;
398             debug_request = 0;
399
400             if (debugging) {
401                 if (obj->options.logconfig)
402                     setuplog(obj->options.logconfig);
403                 else
404                     setuplog("default log_note");
405                 debugging = 0;
406             } else {
407                 char logstr[50];
408                 debugging = 1;
409                 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
410                 setuplog(logstr);
411             }
412         }
413
414         if (cmd == DSIFUNC_TICKLE) {
415             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
416             if ((child.flags & CHILD_DIE))
417                 dsi_tickle(dsi);
418             pending_request(dsi);
419             continue;
420         } 
421
422         child.flags |= CHILD_DATA;
423         switch(cmd) {
424         case DSIFUNC_CLOSE:
425             afp_dsi_close(obj);
426             LOG(log_info, logtype_afpd, "done");
427             return;
428             break;
429
430         case DSIFUNC_CMD:
431 #ifdef AFS
432             if ( writtenfork ) {
433                 if ( flushfork( writtenfork ) < 0 ) {
434                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
435                 }
436                 writtenfork = NULL;
437             }
438 #endif /* AFS */
439
440             function = (u_char) dsi->commands[0];
441
442             /* send off an afp command. in a couple cases, we take advantage
443              * of the fact that we're a stream-based protocol. */
444             if (afp_switch[function]) {
445                 dsi->datalen = DSI_DATASIZ;
446                 child.flags |= CHILD_RUNNING;
447
448                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
449
450                 err = (*afp_switch[function])(obj,
451                                               (char *)&dsi->commands, dsi->cmdlen,
452                                               (char *)&dsi->data, &dsi->datalen);
453
454                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
455                     AfpNum2name(function), AfpErr2name(err));
456
457                 dir_free_invalid_q();
458
459 #ifdef FORCE_UIDGID
460                 /* bring everything back to old euid, egid */
461                 if (obj->force_uid)
462                     restore_uidgid ( &obj->uidgid );
463 #endif /* FORCE_UIDGID */
464                 child.flags &= ~CHILD_RUNNING;
465             } else {
466                 LOG(log_error, logtype_afpd, "bad function %X", function);
467                 dsi->datalen = 0;
468                 err = AFPERR_NOOP;
469             }
470
471             /* single shot toggle that gets set by dsi_readinit. */
472             if (dsi->noreply) {
473                 dsi->noreply = 0;
474                 break;
475             }
476
477             if (!dsi_cmdreply(dsi, err)) {
478                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
479                 afp_dsi_die(EXITERR_CLNT);
480             }
481             break;
482
483         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
484             function = (u_char) dsi->commands[0];
485             if ( afp_switch[ function ] != NULL ) {
486                 dsi->datalen = DSI_DATASIZ;
487                 child.flags |= CHILD_RUNNING;
488
489                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
490
491                 err = (*afp_switch[function])(obj,
492                                               (char *)&dsi->commands, dsi->cmdlen,
493                                               (char *)&dsi->data, &dsi->datalen);
494
495                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
496                     AfpNum2name(function), AfpErr2name(err));
497
498                 child.flags &= ~CHILD_RUNNING;
499 #ifdef FORCE_UIDGID
500                 /* bring everything back to old euid, egid */
501                 if (obj->force_uid)
502                     restore_uidgid ( &obj->uidgid );
503 #endif /* FORCE_UIDGID */
504             } else {
505                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
506                 dsi->datalen = 0;
507                 err = AFPERR_NOOP;
508             }
509
510             if (!dsi_wrtreply(dsi, err)) {
511                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
512                 afp_dsi_die(EXITERR_CLNT);
513             }
514             break;
515
516         case DSIFUNC_ATTN: /* attention replies */
517             break;
518
519             /* error. this usually implies a mismatch of some kind
520              * between server and client. if things are correct,
521              * we need to flush the rest of the packet if necessary. */
522         default:
523             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
524             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
525             dsi_writeflush(dsi);
526             break;
527         }
528         pending_request(dsi);
529     }
530
531     /* error */
532     afp_dsi_die(EXITERR_CLNT);
533 }