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