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