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