]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Merge from branch-2-1: umask on upriv volumes
[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 (geteuid() != obj->uid) {
78         if (seteuid( obj->uid ) < 0) {
79             LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
80                 obj->uid, strerror(errno), getuid(), geteuid());
81             exit(EXITERR_SYS);
82         }
83     }
84
85     close_all_vol();
86     if (obj->logout)
87         (*obj->logout)();
88
89     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
90         dsi->read_count/1024.0, dsi->write_count/1024.0);
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  * FIXME here or we wait ?
180 */
181 volatile int reload_request = 0;
182
183 static void afp_dsi_reload(int sig _U_)
184 {
185     reload_request = 1;
186 }
187
188 /* ---------------------------------
189  * SIGINT: enable max_debug LOGging
190  */
191 static volatile sig_atomic_t debug_request = 0;
192
193 static void afp_dsi_debug(int sig _U_)
194 {
195     debug_request = 1;
196 }
197
198 /* ---------------------- */
199 #ifdef SERVERTEXT
200 static void afp_dsi_getmesg (int sig _U_)
201 {
202     DSI *dsi = (DSI *) child.obj->handle;
203
204     dsi->msg_request = 1;
205     if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
206         dsi->msg_request = 2;
207 }
208 #endif /* SERVERTEXT */
209
210 static void alarm_handler(int sig _U_)
211 {
212     int err;
213     DSI *dsi = (DSI *) child.obj->handle;
214
215     /* we have to restart the timer because some libraries 
216      * may use alarm() */
217     setitimer(ITIMER_REAL, &dsi->timer, NULL);
218
219     /* we got some traffic from the client since the previous timer 
220      * tick. */
221     if ((child.flags & CHILD_DATA)) {
222         child.flags &= ~CHILD_DATA;
223         return;
224     }
225
226     /* if we're in the midst of processing something,
227        don't die. */
228     if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
229         return;
230     } 
231         
232     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
233         if (!(err = pollvoltime(child.obj)))
234             err = dsi_tickle(child.obj->handle);
235         if (err <= 0) 
236             afp_dsi_die(EXITERR_CLNT);
237         
238     } else { /* didn't receive a tickle. close connection */
239         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
240         afp_dsi_die(EXITERR_CLNT);
241     }
242 }
243
244 /* ----------------- 
245    if dsi->in_write is set attention, tickle (and close?) msg
246    aren't sent. We don't care about tickle 
247 */
248 static void pending_request(DSI *dsi)
249 {
250     /* send pending attention */
251
252     /* read msg if any, it could be done in afp_getsrvrmesg */
253     if (dsi->msg_request) {
254         if (dsi->msg_request == 2) {
255             /* didn't send it in signal handler */
256             dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
257         }
258         dsi->msg_request = 0;
259         readmessage(child.obj);
260     }
261     if (dsi->down_request) {
262         dsi->down_request = 0;
263         dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
264                   AFPATTN_MESG | AFPATTN_TIME(5));
265     }
266 }
267
268 /* -------------------------------------------
269  afp over dsi. this never returns. 
270 */
271 void afp_over_dsi(AFPObj *obj)
272 {
273     DSI *dsi = (DSI *) obj->handle;
274     u_int32_t err, cmd;
275     u_int8_t function;
276     struct sigaction action;
277
278     AFPobj = obj;
279     obj->exit = afp_dsi_die;
280     obj->reply = (int (*)()) dsi_cmdreply;
281     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
282
283     obj->sleep = afp_dsi_sleep;
284     child.obj = obj;
285     child.tickle = child.flags = 0;
286
287     memset(&action, 0, sizeof(action));
288
289     /* install SIGHUP */
290     action.sa_handler = afp_dsi_reload;
291     sigemptyset( &action.sa_mask );
292     sigaddset(&action.sa_mask, SIGALRM);
293     sigaddset(&action.sa_mask, SIGTERM);
294     sigaddset(&action.sa_mask, SIGUSR1);
295     sigaddset(&action.sa_mask, SIGINT);
296 #ifdef SERVERTEXT
297     sigaddset(&action.sa_mask, SIGUSR2);
298 #endif    
299     action.sa_flags = SA_RESTART;
300     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
301         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
302         afp_dsi_die(EXITERR_SYS);
303     }
304
305     /* install SIGTERM */
306     action.sa_handler = afp_dsi_die;
307     sigemptyset( &action.sa_mask );
308     sigaddset(&action.sa_mask, SIGALRM);
309     sigaddset(&action.sa_mask, SIGHUP);
310     sigaddset(&action.sa_mask, SIGUSR1);
311     sigaddset(&action.sa_mask, SIGINT);
312 #ifdef SERVERTEXT
313     sigaddset(&action.sa_mask, SIGUSR2);
314 #endif    
315     action.sa_flags = SA_RESTART;
316     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
317         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
318         afp_dsi_die(EXITERR_SYS);
319     }
320
321 #ifdef SERVERTEXT
322     /* Added for server message support */
323     action.sa_handler = afp_dsi_getmesg;
324     sigemptyset( &action.sa_mask );
325     sigaddset(&action.sa_mask, SIGALRM);
326     sigaddset(&action.sa_mask, SIGTERM);
327     sigaddset(&action.sa_mask, SIGUSR1);
328     sigaddset(&action.sa_mask, SIGHUP);
329     sigaddset(&action.sa_mask, SIGINT);
330     action.sa_flags = SA_RESTART;
331     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
332         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
333         afp_dsi_die(EXITERR_SYS);
334     }
335 #endif /* SERVERTEXT */
336
337     /*  SIGUSR1 - set down in 5 minutes  */
338     action.sa_handler = afp_dsi_timedown;
339     sigemptyset( &action.sa_mask );
340     sigaddset(&action.sa_mask, SIGALRM);
341     sigaddset(&action.sa_mask, SIGHUP);
342     sigaddset(&action.sa_mask, SIGTERM);
343     sigaddset(&action.sa_mask, SIGINT);
344 #ifdef SERVERTEXT
345     sigaddset(&action.sa_mask, SIGUSR2);
346 #endif    
347     action.sa_flags = SA_RESTART;
348     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
349         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
350         afp_dsi_die(EXITERR_SYS);
351     }
352
353     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
354     action.sa_handler = afp_dsi_debug;
355     sigfillset( &action.sa_mask );
356     action.sa_flags = SA_RESTART;
357     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
358         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
359         afp_dsi_die(EXITERR_SYS);
360     }
361
362 #ifndef DEBUGGING
363     /* tickle handler */
364     action.sa_handler = alarm_handler;
365     sigemptyset(&action.sa_mask);
366     sigaddset(&action.sa_mask, SIGHUP);
367     sigaddset(&action.sa_mask, SIGTERM);
368     sigaddset(&action.sa_mask, SIGUSR1);
369     sigaddset(&action.sa_mask, SIGINT);
370 #ifdef SERVERTEXT
371     sigaddset(&action.sa_mask, SIGUSR2);
372 #endif    
373     action.sa_flags = SA_RESTART;
374     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
375             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
376         afp_dsi_die(EXITERR_SYS);
377     }
378 #endif /* DEBUGGING */
379
380     if (dircache_init(obj->options.dircachesize) != 0)
381         afp_dsi_die(EXITERR_SYS);
382
383     /* get stuck here until the end */
384     while ((cmd = dsi_receive(dsi))) {
385         child.tickle = 0;
386         child.flags &= ~CHILD_SLEEPING;
387         dsi_sleep(dsi, 0); /* wake up */
388
389         if (reload_request) {
390             reload_request = 0;
391             load_volumes(child.obj);
392             dircache_dump();
393         }
394
395         if (debug_request) {
396             char logstr[50];
397             debug_request = 0;
398
399             /* The first SIGINT enables debugging, the second one kills us */
400             action.sa_handler = afp_dsi_die;
401             sigfillset( &action.sa_mask );
402             action.sa_flags = SA_RESTART;
403             if ( sigaction( SIGINT, &action, NULL ) < 0 ) {
404                 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
405                 afp_dsi_die(EXITERR_SYS);
406             }
407
408             sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
409             setuplog(logstr);
410         }
411
412         if (cmd == DSIFUNC_TICKLE) {
413             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
414             if ((child.flags & CHILD_DIE))
415                 dsi_tickle(dsi);
416             pending_request(dsi);
417             continue;
418         } 
419
420         child.flags |= CHILD_DATA;
421         switch(cmd) {
422         case DSIFUNC_CLOSE:
423             afp_dsi_close(obj);
424             LOG(log_info, logtype_afpd, "done");
425             return;
426             break;
427
428         case DSIFUNC_CMD:
429 #ifdef AFS
430             if ( writtenfork ) {
431                 if ( flushfork( writtenfork ) < 0 ) {
432                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
433                 }
434                 writtenfork = NULL;
435             }
436 #endif /* AFS */
437
438             function = (u_char) dsi->commands[0];
439
440             /* send off an afp command. in a couple cases, we take advantage
441              * of the fact that we're a stream-based protocol. */
442             if (afp_switch[function]) {
443                 dsi->datalen = DSI_DATASIZ;
444                 child.flags |= CHILD_RUNNING;
445
446                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
447
448                 err = (*afp_switch[function])(obj,
449                                               (char *)&dsi->commands, dsi->cmdlen,
450                                               (char *)&dsi->data, &dsi->datalen);
451
452                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
453                     AfpNum2name(function), AfpErr2name(err));
454 #ifdef FORCE_UIDGID
455                 /* bring everything back to old euid, egid */
456                 if (obj->force_uid)
457                     restore_uidgid ( &obj->uidgid );
458 #endif /* FORCE_UIDGID */
459                 child.flags &= ~CHILD_RUNNING;
460             } else {
461                 LOG(log_error, logtype_afpd, "bad function %X", function);
462                 dsi->datalen = 0;
463                 err = AFPERR_NOOP;
464             }
465
466             /* single shot toggle that gets set by dsi_readinit. */
467             if (dsi->noreply) {
468                 dsi->noreply = 0;
469                 break;
470             }
471
472             if (!dsi_cmdreply(dsi, err)) {
473                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
474                 afp_dsi_die(EXITERR_CLNT);
475             }
476             break;
477
478         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
479             function = (u_char) dsi->commands[0];
480             if ( afp_switch[ function ] != NULL ) {
481                 dsi->datalen = DSI_DATASIZ;
482                 child.flags |= CHILD_RUNNING;
483
484                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
485
486                 err = (*afp_switch[function])(obj,
487                                               (char *)&dsi->commands, dsi->cmdlen,
488                                               (char *)&dsi->data, &dsi->datalen);
489
490                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
491                     AfpNum2name(function), AfpErr2name(err));
492
493                 child.flags &= ~CHILD_RUNNING;
494 #ifdef FORCE_UIDGID
495                 /* bring everything back to old euid, egid */
496                 if (obj->force_uid)
497                     restore_uidgid ( &obj->uidgid );
498 #endif /* FORCE_UIDGID */
499             } else {
500                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
501                 dsi->datalen = 0;
502                 err = AFPERR_NOOP;
503             }
504
505             if (!dsi_wrtreply(dsi, err)) {
506                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
507                 afp_dsi_die(EXITERR_CLNT);
508             }
509             break;
510
511         case DSIFUNC_ATTN: /* attention replies */
512             break;
513
514             /* error. this usually implies a mismatch of some kind
515              * between server and client. if things are correct,
516              * we need to flush the rest of the packet if necessary. */
517         default:
518             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
519             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
520             dsi_writeflush(dsi);
521             break;
522         }
523         pending_request(dsi);
524     }
525
526     /* error */
527     afp_dsi_die(EXITERR_CLNT);
528 }