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