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