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