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