]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
6ae44dc6385bfa02245422a4035d51ad4694c46e
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.11 2001-12-10 20:16:53 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 <syslog.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 extern struct oforks    *writtenfork;
42
43 #define CHILD_DIE         (1 << 0)
44 #define CHILD_RUNNING     (1 << 1)
45
46 static struct {
47     AFPObj *obj;
48     unsigned char tickle, flags;
49 } child;
50
51
52 static __inline__ void afp_dsi_close(AFPObj *obj)
53 {
54     DSI *dsi = obj->handle;
55
56     if (obj->logout)
57         (*obj->logout)();
58
59     dsi_close(dsi);
60
61     /* UAM had syslog control; afpd needs to reassert itself */
62     openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
63     syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
64            dsi->read_count/1024.0, dsi->write_count/1024.0);
65 }
66
67 /* a little bit of code duplication. */
68 static void afp_dsi_die(int sig)
69 {
70     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
71     afp_dsi_close(child.obj);
72     if (sig) /* if no signal, assume dieing because logins are disabled &
73         don't log it (maintenance mode)*/
74         syslog (LOG_INFO, "Connection terminated");
75     if (sig == SIGTERM || sig == SIGALRM) {
76         exit( 0 );
77     }
78     else {
79         exit(sig);
80     }
81 }
82
83 static void afp_dsi_timedown()
84 {
85     struct sigaction    sv;
86     struct itimerval    it;
87
88     child.flags |= CHILD_DIE;
89     /* shutdown and don't reconnect. server going down in 5 minutes. */
90     setmessage("The server is going down for maintenance.");
91     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
92                   AFPATTN_MESG | AFPATTN_TIME(5));
93
94     it.it_interval.tv_sec = 0;
95     it.it_interval.tv_usec = 0;
96     it.it_value.tv_sec = 300;
97     it.it_value.tv_usec = 0;
98     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
99         syslog( LOG_ERR, "afp_timedown: setitimer: %s", strerror(errno) );
100         afp_dsi_die(1);
101     }
102
103     memset(&sv, 0, sizeof(sv));
104     sv.sa_handler = afp_dsi_die;
105     sigemptyset( &sv.sa_mask );
106     sigaddset(&sv.sa_mask, SIGHUP);
107     sigaddset(&sv.sa_mask, SIGTERM);
108     sv.sa_flags = SA_RESTART;
109     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
110         syslog( LOG_ERR, "afp_timedown: sigaction: %s", strerror(errno) );
111         afp_dsi_die(1);
112     }
113 }
114
115 #ifdef SERVERTEXT
116 static void afp_dsi_getmesg (int sig)
117 {
118     readmessage();
119     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
120 }
121 #endif /* SERVERTEXT */
122
123 static void alarm_handler()
124 {
125     /* if we're in the midst of processing something,
126        don't die. we'll allow 3 missed tickles before we die (2 minutes) */
127     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < 4)) {
128         dsi_tickle(child.obj->handle);
129     } else { /* didn't receive a tickle. close connection */
130         syslog(LOG_ERR, "afp_alarm: child timed out");
131         afp_dsi_die(1);
132     }
133 }
134
135 /* afp over dsi. this never returns. */
136 void afp_over_dsi(AFPObj *obj)
137 {
138     DSI *dsi = (DSI *) obj->handle;
139     u_int32_t err, cmd;
140     u_int8_t function;
141     struct sigaction action;
142
143     obj->exit = afp_dsi_die;
144     obj->reply = (int (*)()) dsi_cmdreply;
145     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
146
147     child.obj = obj;
148     child.tickle = child.flags = 0;
149
150     /* install SIGTERM and SIGHUP */
151     memset(&action, 0, sizeof(action));
152     action.sa_handler = afp_dsi_timedown;
153     sigemptyset( &action.sa_mask );
154     sigaddset(&action.sa_mask, SIGALRM);
155     sigaddset(&action.sa_mask, SIGTERM);
156     action.sa_flags = SA_RESTART;
157     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
158         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
159         afp_dsi_die(1);
160     }
161
162     action.sa_handler = afp_dsi_die;
163     sigemptyset( &action.sa_mask );
164     sigaddset(&action.sa_mask, SIGALRM);
165     sigaddset(&action.sa_mask, SIGHUP);
166     action.sa_flags = SA_RESTART;
167     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
168         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
169         afp_dsi_die(1);
170     }
171
172 #ifdef SERVERTEXT
173     /* Added for server message support */
174     action.sa_handler = afp_dsi_getmesg;
175     sigemptyset( &action.sa_mask );
176     sigaddset(&action.sa_mask, SIGUSR2);
177     action.sa_flags = SA_RESTART;
178     if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
179         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
180         afp_dsi_die(1);
181     }
182 #endif /* SERVERTEXT */
183
184     /* tickle handler */
185     action.sa_handler = alarm_handler;
186     sigemptyset(&action.sa_mask);
187     sigaddset(&action.sa_mask, SIGHUP);
188     sigaddset(&action.sa_mask, SIGTERM);
189     action.sa_flags = SA_RESTART;
190     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
191             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
192         afp_dsi_die(1);
193     }
194
195     /* get stuck here until the end */
196     while ((cmd = dsi_receive(dsi))) {
197         child.tickle = 0;
198
199         if (cmd == DSIFUNC_TICKLE) {
200             /* so we don't get killed on the client side. */
201             if (child.flags & CHILD_DIE)
202                 dsi_tickle(dsi);
203             continue;
204         } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
205             setitimer(ITIMER_REAL, &dsi->timer, NULL);
206
207         switch(cmd) {
208         case DSIFUNC_CLOSE:
209             afp_dsi_close(obj);
210             syslog(LOG_INFO, "done");
211             if (obj->options.flags & OPTION_DEBUG )
212                 printf("done\n");
213             return;
214             break;
215
216         case DSIFUNC_CMD:
217 #ifdef AFS
218             if ( writtenfork ) {
219                 if ( flushfork( writtenfork ) < 0 ) {
220                     syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
221                 }
222                 writtenfork = NULL;
223             }
224 #endif /* AFS */
225
226             function = (u_char) dsi->commands[0];
227             if (obj->options.flags & OPTION_DEBUG ) {
228                 printf("command: %d\n", function);
229                 bprint((char *) dsi->commands, dsi->cmdlen);
230             }
231
232             /* send off an afp command. in a couple cases, we take advantage
233              * of the fact that we're a stream-based protocol. */
234             if (afp_switch[function]) {
235                 dsi->datalen = DSI_DATASIZ;
236                 child.flags |= CHILD_RUNNING;
237                 err = (*afp_switch[function])(obj,
238                                               dsi->commands, dsi->cmdlen,
239                                               dsi->data, &dsi->datalen);
240                 child.flags &= ~CHILD_RUNNING;
241             } else {
242                 syslog(LOG_ERR, "bad function %X", function);
243                 dsi->datalen = 0;
244                 err = AFPERR_NOOP;
245             }
246
247             /* single shot toggle that gets set by dsi_readinit. */
248             if (dsi->noreply) {
249                 dsi->noreply = 0;
250                 break;
251             }
252
253             if (obj->options.flags & OPTION_DEBUG ) {
254                 printf( "reply: %d, %d\n", err, dsi->clientID);
255                 bprint((char *) dsi->data, dsi->datalen);
256             }
257
258             if (!dsi_cmdreply(dsi, err)) {
259                 syslog(LOG_ERR, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
260                 afp_dsi_die(1);
261             }
262             break;
263
264         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
265             function = (u_char) dsi->commands[0];
266             if ( obj->options.flags & OPTION_DEBUG ) {
267                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
268                 bprint((char *) dsi->commands, dsi->cmdlen);
269             }
270
271             if ( afp_switch[ function ] != NULL ) {
272                 dsi->datalen = DSI_DATASIZ;
273                 child.flags |= CHILD_RUNNING;
274                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
275                                               dsi->data, &dsi->datalen);
276                 child.flags &= ~CHILD_RUNNING;
277             } else {
278                 syslog( LOG_ERR, "(write) bad function %x", function);
279                 dsi->datalen = 0;
280                 err = AFPERR_NOOP;
281             }
282
283             if (obj->options.flags & OPTION_DEBUG ) {
284                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
285                 bprint((char *) dsi->data, dsi->datalen);
286             }
287
288             if (!dsi_wrtreply(dsi, err)) {
289                 syslog( LOG_ERR, "dsi_wrtreply: %s", strerror(errno) );
290                 afp_dsi_die(1);
291             }
292             break;
293
294         case DSIFUNC_ATTN: /* attention replies */
295             continue;
296             break;
297
298             /* error. this usually implies a mismatch of some kind
299              * between server and client. if things are correct,
300              * we need to flush the rest of the packet if necessary. */
301         default:
302             syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
303             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
304             dsi_writeflush(dsi);
305             break;
306         }
307
308         if ( obj->options.flags & OPTION_DEBUG ) {
309 #ifdef notdef
310             pdesc( stdout );
311 #endif /* notdef */
312             of_pforkdesc( stdout );
313             fflush( stdout );
314         }
315     }
316
317     /* error */
318     afp_dsi_die(1);
319 }