]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
- added a cache for directories offspring count.
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.25 2002-10-11 14:18:23 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
122 #ifdef SERVERTEXT
123 static void afp_dsi_getmesg (int sig)
124 {
125     readmessage();
126     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
127 }
128 #endif /* SERVERTEXT */
129
130 static void alarm_handler()
131 {
132     /* if we're in the midst of processing something,
133        don't die. */
134     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
135         if (!pollvoltime(child.obj))
136             dsi_tickle(child.obj->handle);
137     } else { /* didn't receive a tickle. close connection */
138         LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
139         afp_dsi_die(1);
140     }
141 }
142
143
144 /*
145  *  Signal handler for SIGUSR1 - set the debug flag and 
146  *  redirect stdout to <tmpdir>/afpd-debug-<pid>.
147  */
148 void afp_set_debug (int sig)
149 {
150     char        fname[MAXPATHLEN];
151
152     snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
153     freopen(fname, "w", stdout);
154     child.obj->options.flags |= OPTION_DEBUG;
155
156     return;
157 }
158
159
160 /* afp over dsi. this never returns. */
161 void afp_over_dsi(AFPObj *obj)
162 {
163     DSI *dsi = (DSI *) obj->handle;
164     u_int32_t err, cmd;
165     u_int8_t function;
166     struct sigaction action;
167
168     obj->exit = afp_dsi_die;
169     obj->reply = (int (*)()) dsi_cmdreply;
170     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
171
172     child.obj = obj;
173     child.tickle = child.flags = 0;
174
175     /* install SIGTERM and SIGHUP */
176     memset(&action, 0, sizeof(action));
177     action.sa_handler = afp_dsi_timedown;
178     sigemptyset( &action.sa_mask );
179     sigaddset(&action.sa_mask, SIGALRM);
180     sigaddset(&action.sa_mask, SIGTERM);
181     action.sa_flags = SA_RESTART;
182     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
183         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
184         afp_dsi_die(1);
185     }
186
187     action.sa_handler = afp_dsi_die;
188     sigemptyset( &action.sa_mask );
189     sigaddset(&action.sa_mask, SIGALRM);
190     sigaddset(&action.sa_mask, SIGHUP);
191     action.sa_flags = SA_RESTART;
192     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
193         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
194         afp_dsi_die(1);
195     }
196
197 #ifdef SERVERTEXT
198     /* Added for server message support */
199     action.sa_handler = afp_dsi_getmesg;
200     sigemptyset( &action.sa_mask );
201     sigaddset(&action.sa_mask, SIGUSR2);
202     action.sa_flags = SA_RESTART;
203     if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
204         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
205         afp_dsi_die(1);
206     }
207 #endif /* SERVERTEXT */
208
209     /*  SIGUSR1 - set "debug" flag on this process.  */
210     action.sa_handler = afp_set_debug;
211     sigemptyset( &action.sa_mask );
212     sigaddset(&action.sa_mask, SIGUSR1);
213     action.sa_flags = SA_RESTART;
214     if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
215         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
216         afp_dsi_die(1);
217     }
218
219     /* tickle handler */
220     action.sa_handler = alarm_handler;
221     sigemptyset(&action.sa_mask);
222     sigaddset(&action.sa_mask, SIGHUP);
223     sigaddset(&action.sa_mask, SIGTERM);
224     action.sa_flags = SA_RESTART;
225     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
226             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
227         afp_dsi_die(1);
228     }
229
230     /* get stuck here until the end */
231     while ((cmd = dsi_receive(dsi))) {
232         child.tickle = 0;
233
234         if (cmd == DSIFUNC_TICKLE) {
235             /* so we don't get killed on the client side. */
236             if (child.flags & CHILD_DIE)
237                 dsi_tickle(dsi);
238             continue;
239         } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
240             setitimer(ITIMER_REAL, &dsi->timer, NULL);
241         }
242         switch(cmd) {
243         case DSIFUNC_CLOSE:
244             afp_dsi_close(obj);
245             LOG(log_info, logtype_afpd, "done");
246             if (obj->options.flags & OPTION_DEBUG )
247                 printf("done\n");
248             return;
249             break;
250
251         case DSIFUNC_CMD:
252 #ifdef AFS
253             if ( writtenfork ) {
254                 if ( flushfork( writtenfork ) < 0 ) {
255                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
256                 }
257                 writtenfork = NULL;
258             }
259 #endif /* AFS */
260
261             function = (u_char) dsi->commands[0];
262             if (obj->options.flags & OPTION_DEBUG ) {
263                 printf("command: %d (%s)\n", function, AfpNum2name(function));
264                 bprint((char *) dsi->commands, dsi->cmdlen);
265             }
266
267             /* send off an afp command. in a couple cases, we take advantage
268              * of the fact that we're a stream-based protocol. */
269             if (afp_switch[function]) {
270                 dsi->datalen = DSI_DATASIZ;
271                 child.flags |= CHILD_RUNNING;
272
273                 err = (*afp_switch[function])(obj,
274                                               dsi->commands, dsi->cmdlen,
275                                               dsi->data, &dsi->datalen);
276 #ifdef FORCE_UIDGID
277                 /* bring everything back to old euid, egid */
278                 if (obj->force_uid)
279                     restore_uidgid ( &obj->uidgid );
280 #endif /* FORCE_UIDGID */
281                 child.flags &= ~CHILD_RUNNING;
282             } else {
283                 LOG(log_error, logtype_afpd, "bad function %X", function);
284                 dsi->datalen = 0;
285                 err = AFPERR_NOOP;
286             }
287
288             /* single shot toggle that gets set by dsi_readinit. */
289             if (dsi->noreply) {
290                 dsi->noreply = 0;
291                 break;
292             }
293
294             if (obj->options.flags & OPTION_DEBUG ) {
295                 printf( "reply: %d, %d\n", err, dsi->clientID);
296                 bprint((char *) dsi->data, dsi->datalen);
297             }
298
299             if (!dsi_cmdreply(dsi, err)) {
300                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
301                 afp_dsi_die(1);
302             }
303             break;
304
305         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
306             function = (u_char) dsi->commands[0];
307             if ( obj->options.flags & OPTION_DEBUG ) {
308                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
309                 bprint((char *) dsi->commands, dsi->cmdlen);
310             }
311
312             if ( afp_switch[ function ] != NULL ) {
313                 dsi->datalen = DSI_DATASIZ;
314                 child.flags |= CHILD_RUNNING;
315                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
316                                               dsi->data, &dsi->datalen);
317                 child.flags &= ~CHILD_RUNNING;
318 #ifdef FORCE_UIDGID
319                 /* bring everything back to old euid, egid */
320                 if (obj->force_uid)
321                     restore_uidgid ( &obj->uidgid );
322 #endif /* FORCE_UIDGID */
323             } else {
324                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
325                 dsi->datalen = 0;
326                 err = AFPERR_NOOP;
327             }
328
329             if (obj->options.flags & OPTION_DEBUG ) {
330                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
331                 bprint((char *) dsi->data, dsi->datalen);
332             }
333
334             if (!dsi_wrtreply(dsi, err)) {
335                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
336                 afp_dsi_die(1);
337             }
338             break;
339
340         case DSIFUNC_ATTN: /* attention replies */
341             continue;
342             break;
343
344             /* error. this usually implies a mismatch of some kind
345              * between server and client. if things are correct,
346              * we need to flush the rest of the packet if necessary. */
347         default:
348             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
349             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
350             dsi_writeflush(dsi);
351             break;
352         }
353
354         if ( obj->options.flags & OPTION_DEBUG ) {
355 #ifdef notdef
356             pdesc( stdout );
357 #endif /* notdef */
358             of_pforkdesc( stdout );
359             fflush( stdout );
360         }
361     }
362
363     /* error */
364     afp_dsi_die(1);
365 }