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