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