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