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