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