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