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