]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
bugfix: close the right fork in renamefile, copy and paste error...
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.26 2002-12-04 10:59:36 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
248         if (cmd == DSIFUNC_TICKLE) {
249             /* so we don't get killed on the client side. */
250             if (child.flags & CHILD_DIE)
251                 dsi_tickle(dsi);
252             continue;
253         } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
254             setitimer(ITIMER_REAL, &dsi->timer, NULL);
255         }
256         switch(cmd) {
257         case DSIFUNC_CLOSE:
258             afp_dsi_close(obj);
259             LOG(log_info, logtype_afpd, "done");
260             if (obj->options.flags & OPTION_DEBUG )
261                 printf("done\n");
262             return;
263             break;
264
265         case DSIFUNC_CMD:
266 #ifdef AFS
267             if ( writtenfork ) {
268                 if ( flushfork( writtenfork ) < 0 ) {
269                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
270                 }
271                 writtenfork = NULL;
272             }
273 #endif /* AFS */
274
275             function = (u_char) dsi->commands[0];
276             if (obj->options.flags & OPTION_DEBUG ) {
277                 printf("command: %d (%s)\n", function, AfpNum2name(function));
278                 bprint((char *) dsi->commands, dsi->cmdlen);
279             }
280
281             /* send off an afp command. in a couple cases, we take advantage
282              * of the fact that we're a stream-based protocol. */
283             if (afp_switch[function]) {
284                 dsi->datalen = DSI_DATASIZ;
285                 child.flags |= CHILD_RUNNING;
286
287                 err = (*afp_switch[function])(obj,
288                                               dsi->commands, dsi->cmdlen,
289                                               dsi->data, &dsi->datalen);
290 #ifdef FORCE_UIDGID
291                 /* bring everything back to old euid, egid */
292                 if (obj->force_uid)
293                     restore_uidgid ( &obj->uidgid );
294 #endif /* FORCE_UIDGID */
295                 child.flags &= ~CHILD_RUNNING;
296             } else {
297                 LOG(log_error, logtype_afpd, "bad function %X", function);
298                 dsi->datalen = 0;
299                 err = AFPERR_NOOP;
300             }
301
302             /* single shot toggle that gets set by dsi_readinit. */
303             if (dsi->noreply) {
304                 dsi->noreply = 0;
305                 break;
306             }
307
308             if (obj->options.flags & OPTION_DEBUG ) {
309                 printf( "reply: %d, %d\n", err, dsi->clientID);
310                 bprint((char *) dsi->data, dsi->datalen);
311             }
312
313             if (!dsi_cmdreply(dsi, err)) {
314                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
315                 afp_dsi_die(1);
316             }
317             break;
318
319         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
320             function = (u_char) dsi->commands[0];
321             if ( obj->options.flags & OPTION_DEBUG ) {
322                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
323                 bprint((char *) dsi->commands, dsi->cmdlen);
324             }
325
326             if ( afp_switch[ function ] != NULL ) {
327                 dsi->datalen = DSI_DATASIZ;
328                 child.flags |= CHILD_RUNNING;
329                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
330                                               dsi->data, &dsi->datalen);
331                 child.flags &= ~CHILD_RUNNING;
332 #ifdef FORCE_UIDGID
333                 /* bring everything back to old euid, egid */
334                 if (obj->force_uid)
335                     restore_uidgid ( &obj->uidgid );
336 #endif /* FORCE_UIDGID */
337             } else {
338                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
339                 dsi->datalen = 0;
340                 err = AFPERR_NOOP;
341             }
342
343             if (obj->options.flags & OPTION_DEBUG ) {
344                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
345                 bprint((char *) dsi->data, dsi->datalen);
346             }
347
348             if (!dsi_wrtreply(dsi, err)) {
349                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
350                 afp_dsi_die(1);
351             }
352             break;
353
354         case DSIFUNC_ATTN: /* attention replies */
355             continue;
356             break;
357
358             /* error. this usually implies a mismatch of some kind
359              * between server and client. if things are correct,
360              * we need to flush the rest of the packet if necessary. */
361         default:
362             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
363             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
364             dsi_writeflush(dsi);
365             break;
366         }
367
368         if ( obj->options.flags & OPTION_DEBUG ) {
369 #ifdef notdef
370             pdesc( stdout );
371 #endif /* notdef */
372             of_pforkdesc( stdout );
373             fflush( stdout );
374         }
375     }
376
377     /* error */
378     afp_dsi_die(1);
379 }