]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Add support for Tracker 0.67 on Solaris
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3  * Copyright (c) 1990,1993 Regents of The University of Michigan.
4  * All Rights Reserved.  See COPYRIGHT.
5  *
6  * modified from main.c. this handles afp over tcp.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <errno.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/socket.h>
22 #include <sys/time.h>
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif /* HAVE_SYS_STAT_H */
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
28 #include <arpa/inet.h>
29 #include <setjmp.h>
30 #include <time.h>
31
32 #include <atalk/logger.h>
33 #include <atalk/dsi.h>
34 #include <atalk/compat.h>
35 #include <atalk/util.h>
36 #include <atalk/uuid.h>
37 #include <atalk/paths.h>
38 #include <atalk/server_ipc.h>
39 #include <atalk/fce_api.h>
40 #include <atalk/globals.h>
41 #include <atalk/netatalk_conf.h>
42 #include <atalk/spotlight.h>
43
44 #include "switch.h"
45 #include "auth.h"
46 #include "fork.h"
47 #include "dircache.h"
48
49 #ifndef SOL_TCP
50 #define SOL_TCP IPPROTO_TCP
51 #endif
52
53 /* 
54  * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
55  * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
56  * but are deeply nested in the function chain with the caller already without acces to it.
57  * Changing this would require adding a reference to the caller which itself might be
58  * called in many places (eg acltoownermode is called from accessmode).
59  * The only sane way out is providing a copy of it here:
60  */
61 AFPObj *AFPobj = NULL;
62
63 typedef struct {
64     uint16_t DSIreqID;
65     uint8_t  AFPcommand;
66     uint32_t result;
67 } rc_elem_t;
68
69 /*
70  * AFP replay cache:
71  * - fix sized array
72  * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
73  */
74 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
75
76 static sigjmp_buf recon_jmp;
77 static void afp_dsi_close(AFPObj *obj)
78 {
79     DSI *dsi = obj->dsi;
80     sigset_t sigs;
81     
82     close(obj->ipc_fd);
83     obj->ipc_fd = -1;
84
85     /* we may have been called from a signal handler caught when afpd was running
86      * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
87      * restore our login user
88      */
89     if (geteuid() != obj->uid) {
90         if (seteuid( obj->uid ) < 0) {
91             LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
92                 obj->uid, strerror(errno), getuid(), geteuid());
93             exit(EXITERR_SYS);
94         }
95     }
96
97     close_all_vol(obj);
98     if (obj->logout) {
99         /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
100         sigfillset(&sigs);
101         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
102         (*obj->logout)();
103     }
104
105     LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
106         dsi->read_count/1024.0, dsi->write_count/1024.0);
107     log_dircache_stat();
108
109     dsi_close(dsi);
110 }
111
112 /* -------------------------------
113  * SIGTERM
114  * a little bit of code duplication. 
115  */
116 static void afp_dsi_die(int sig)
117 {
118     DSI *dsi = (DSI *)AFPobj->dsi;
119
120     if (dsi->flags & DSI_RECONINPROG) {
121         /* Primary reconnect succeeded, got SIGTERM from afpd parent */
122         dsi->flags &= ~DSI_RECONINPROG;
123         return; /* this returns to afp_disconnect */
124     }
125
126     if (dsi->flags & DSI_DISCONNECTED) {
127         LOG(log_note, logtype_afpd, "Disconnected session terminating");
128         exit(0);
129     }
130
131     dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN);
132     afp_dsi_close(AFPobj);
133    if (sig) /* if no signal, assume dieing because logins are disabled &
134                 don't log it (maintenance mode)*/
135         LOG(log_info, logtype_afpd, "Connection terminated");
136     if (sig == SIGTERM || sig == SIGALRM) {
137         exit( 0 );
138     }
139     else {
140         exit(sig);
141     }
142 }
143
144 /* SIGQUIT handler */
145 static void ipc_reconnect_handler(int sig _U_)
146 {
147     if (reconnect_ipc(AFPobj) != 0) {
148         LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
149         afp_dsi_close(AFPobj);
150         exit(EXITERR_SYS);        
151     }
152
153     if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
154         LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
155         afp_dsi_close(AFPobj);
156         exit(EXITERR_SYS);        
157     }
158     LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
159 }
160
161 /* SIGURG handler (primary reconnect) */
162 static void afp_dsi_transfer_session(int sig _U_)
163 {
164     uint16_t dsiID;
165     int socket;
166     DSI *dsi = (DSI *)AFPobj->dsi;
167
168     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
169
170     if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
171         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
172         afp_dsi_close(AFPobj);
173         exit(EXITERR_SYS);
174     }
175
176     if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
177         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
178         afp_dsi_close(AFPobj);
179         exit(EXITERR_SYS);
180     }
181
182     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
183
184     dsi->proto_close(dsi);
185     dsi->socket = socket;
186     dsi->flags = DSI_RECONSOCKET;
187     dsi->datalen = 0;
188     dsi->eof = dsi->start = dsi->buffer;
189     dsi->in_write = 0;
190     dsi->header.dsi_requestID = dsiID;
191     dsi->header.dsi_command = DSIFUNC_CMD;
192
193     /*
194      * The session transfer happens in the middle of FPDisconnect old session, thus we
195      * have to send the reply now.
196      */
197     if (!dsi_cmdreply(dsi, AFP_OK)) {
198         LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
199         afp_dsi_close(AFPobj);
200         exit(EXITERR_CLNT);
201     }
202
203     LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
204     /* 
205      * Now returning from this signal handler return to dsi_receive which should start
206      * reading/continuing from the connected socket that was passed via the parent from
207      * another session. The parent will terminate that session.
208      */
209     siglongjmp(recon_jmp, 1);
210 }
211
212 /* ------------------- */
213 static void afp_dsi_timedown(int sig _U_)
214 {
215     struct sigaction    sv;
216     struct itimerval    it;
217     DSI                 *dsi = (DSI *)AFPobj->dsi;
218     dsi->flags |= DSI_DIE;
219     /* shutdown and don't reconnect. server going down in 5 minutes. */
220     setmessage("The server is going down for maintenance.");
221     if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
222                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
223         DSI *dsi = (DSI *)AFPobj->dsi;
224         dsi->down_request = 1;
225     }                  
226
227     it.it_interval.tv_sec = 0;
228     it.it_interval.tv_usec = 0;
229     it.it_value.tv_sec = 300;
230     it.it_value.tv_usec = 0;
231
232     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
233         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
234         afp_dsi_die(EXITERR_SYS);
235     }
236     memset(&sv, 0, sizeof(sv));
237     sv.sa_handler = afp_dsi_die;
238     sigemptyset( &sv.sa_mask );
239     sigaddset(&sv.sa_mask, SIGHUP);
240     sigaddset(&sv.sa_mask, SIGTERM);
241     sv.sa_flags = SA_RESTART;
242     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
243         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
244         afp_dsi_die(EXITERR_SYS);
245     }
246
247     /* ignore myself */
248     sv.sa_handler = SIG_IGN;
249     sigemptyset( &sv.sa_mask );
250     sv.sa_flags = SA_RESTART;
251     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
252         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
253         afp_dsi_die(EXITERR_SYS);
254     }
255 }
256
257 /* ---------------------------------
258  * SIGHUP reload configuration file
259  */
260 volatile int reload_request = 0;
261
262 static void afp_dsi_reload(int sig _U_)
263 {
264     reload_request = 1;
265 }
266
267 /* ---------------------------------
268  * SIGINT: enable max_debug LOGging
269  */
270 static volatile sig_atomic_t debug_request = 0;
271
272 static void afp_dsi_debug(int sig _U_)
273 {
274     debug_request = 1;
275 }
276
277 /* ---------------------- */
278 static void afp_dsi_getmesg (int sig _U_)
279 {
280     DSI *dsi = (DSI *)AFPobj->dsi;
281
282     dsi->msg_request = 1;
283     if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
284         dsi->msg_request = 2;
285 }
286
287 static void alarm_handler(int sig _U_)
288 {
289     int err;
290     DSI *dsi = (DSI *)AFPobj->dsi;
291
292     /* we have to restart the timer because some libraries may use alarm() */
293     setitimer(ITIMER_REAL, &dsi->timer, NULL);
294
295     /* we got some traffic from the client since the previous timer tick. */
296     if ((dsi->flags & DSI_DATA)) {
297         dsi->flags &= ~DSI_DATA;
298         return;
299     }
300
301     dsi->tickle++;
302     LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
303         dsi->tickle,
304         (dsi->flags & DSI_DATA) ?         "DSI_DATA" : "-",
305         (dsi->flags & DSI_RUNNING) ?      "DSI_RUNNING" : "-",
306         (dsi->flags & DSI_SLEEPING) ?     "DSI_SLEEPING" : "-",
307         (dsi->flags & DSI_EXTSLEEP) ?     "DSI_EXTSLEEP" : "-",
308         (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
309         (dsi->flags & DSI_DIE) ?          "DSI_DIE" : "-",
310         (dsi->flags & DSI_NOREPLY) ?      "DSI_NOREPLY" : "-",
311         (dsi->flags & DSI_RECONSOCKET) ?  "DSI_RECONSOCKET" : "-",
312         (dsi->flags & DSI_RECONINPROG) ?  "DSI_RECONINPROG" : "-");
313
314     if (dsi->flags & DSI_SLEEPING) {
315         if (dsi->tickle > AFPobj->options.sleep) {
316             LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
317             afp_dsi_die(EXITERR_CLNT);
318         }
319         return;
320     } 
321
322     if (dsi->flags & DSI_DISCONNECTED) {
323         if (geteuid() == 0) {
324             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
325             afp_dsi_die(EXITERR_CLNT);
326         }
327         if (dsi->tickle > AFPobj->options.disconnected) {
328             LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
329             afp_dsi_die(EXITERR_CLNT);
330         }
331         return;
332     }
333
334     /* if we're in the midst of processing something, don't die. */        
335     if (dsi->tickle >= AFPobj->options.timeout) {
336         LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
337         if (dsi_disconnect(dsi) != 0)
338             afp_dsi_die(EXITERR_CLNT);
339         return;
340     }
341
342     if ((err = pollvoltime(AFPobj)) == 0)
343         LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
344         err = dsi_tickle(AFPobj->dsi);
345     if (err <= 0) {
346         if (geteuid() == 0) {
347             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
348             afp_dsi_die(EXITERR_CLNT);
349         }
350         LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
351         if (dsi_disconnect(dsi) != 0)
352             afp_dsi_die(EXITERR_CLNT);
353     }
354 }
355
356 /* ----------------- 
357    if dsi->in_write is set attention, tickle (and close?) msg
358    aren't sent. We don't care about tickle 
359 */
360 static void pending_request(DSI *dsi)
361 {
362     /* send pending attention */
363
364     /* read msg if any, it could be done in afp_getsrvrmesg */
365     if (dsi->msg_request) {
366         if (dsi->msg_request == 2) {
367             /* didn't send it in signal handler */
368             dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
369         }
370         dsi->msg_request = 0;
371         readmessage(AFPobj);
372     }
373     if (dsi->down_request) {
374         dsi->down_request = 0;
375         dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
376                   AFPATTN_MESG | AFPATTN_TIME(5));
377     }
378 }
379
380 void afp_over_dsi_sighandlers(AFPObj *obj)
381 {
382     DSI *dsi = (DSI *) obj->dsi;
383     struct sigaction action;
384
385     memset(&action, 0, sizeof(action));
386     sigfillset(&action.sa_mask);
387     action.sa_flags = SA_RESTART;
388
389     /* install SIGHUP */
390     action.sa_handler = afp_dsi_reload;
391     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
392         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
393         afp_dsi_die(EXITERR_SYS);
394     }
395
396     /* install SIGURG */
397     action.sa_handler = afp_dsi_transfer_session;
398     if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
399         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
400         afp_dsi_die(EXITERR_SYS);
401     }
402
403     /* install SIGTERM */
404     action.sa_handler = afp_dsi_die;
405     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
406         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
407         afp_dsi_die(EXITERR_SYS);
408     }
409
410     /* install SIGQUIT */
411     action.sa_handler = ipc_reconnect_handler;
412     if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
413         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
414         afp_dsi_die(EXITERR_SYS);
415     }
416
417     /* SIGUSR2 - server message support */
418     action.sa_handler = afp_dsi_getmesg;
419     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
420         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
421         afp_dsi_die(EXITERR_SYS);
422     }
423
424     /*  SIGUSR1 - set down in 5 minutes  */
425     action.sa_handler = afp_dsi_timedown;
426     action.sa_flags = SA_RESTART;
427     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
428         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
429         afp_dsi_die(EXITERR_SYS);
430     }
431
432     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
433     action.sa_handler = afp_dsi_debug;
434     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
435         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
436         afp_dsi_die(EXITERR_SYS);
437     }
438
439 #ifndef DEBUGGING
440     /* SIGALRM - tickle handler */
441     action.sa_handler = alarm_handler;
442     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
443             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
444         afp_dsi_die(EXITERR_SYS);
445     }
446 #endif /* DEBUGGING */
447 }
448
449 /* -------------------------------------------
450  afp over dsi. this never returns. 
451 */
452 void afp_over_dsi(AFPObj *obj)
453 {
454     DSI *dsi = (DSI *) obj->dsi;
455     int rc_idx;
456     uint32_t err, cmd;
457     uint8_t function;
458
459     AFPobj = obj;
460     obj->exit = afp_dsi_die;
461     obj->reply = (int (*)()) dsi_cmdreply;
462     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
463     dsi->tickle = 0;
464
465     afp_over_dsi_sighandlers(obj);
466
467     if (dircache_init(obj->options.dircachesize) != 0)
468         afp_dsi_die(EXITERR_SYS);
469
470     /* set TCP snd/rcv buf */
471     if (obj->options.tcp_rcvbuf) {
472         if (setsockopt(dsi->socket,
473                        SOL_SOCKET,
474                        SO_RCVBUF,
475                        &obj->options.tcp_rcvbuf,
476                        sizeof(obj->options.tcp_rcvbuf)) != 0) {
477             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
478         }
479     }
480     if (obj->options.tcp_sndbuf) {
481         if (setsockopt(dsi->socket,
482                        SOL_SOCKET,
483                        SO_SNDBUF,
484                        &obj->options.tcp_sndbuf,
485                        sizeof(obj->options.tcp_sndbuf)) != 0) {
486             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
487         }
488     }
489
490     /* set TCP_NODELAY */
491     int flag = 1;
492     setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
493
494     /* Initialize Spotlight */
495     if ((obj->options.flags & OPTION_SPOTLIGHT) && (obj->options.slmod_path))
496         sl_mod_load(obj->options.slmod_path);
497
498     /* get stuck here until the end */
499     while (1) {
500         if (sigsetjmp(recon_jmp, 1) != 0)
501             /* returning from SIGALARM handler for a primary reconnect */
502             continue;
503
504         /* Blocking read on the network socket */
505         cmd = dsi_stream_receive(dsi);
506
507         if (cmd == 0) {
508             /* cmd == 0 is the error condition */
509             if (dsi->flags & DSI_RECONSOCKET) {
510                 /* we just got a reconnect so we immediately try again to receive on the new fd */
511                 dsi->flags &= ~DSI_RECONSOCKET;
512                 continue;
513             }
514
515             /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
516             if (dsi->flags & DSI_AFP_LOGGED_OUT) {
517                 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
518                 afp_dsi_close(obj);
519                 exit(0);
520             }
521
522 #if 0
523             /*  got ECONNRESET in read from client => exit*/
524             if (dsi->flags & DSI_GOT_ECONNRESET) {
525                 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
526                 afp_dsi_close(obj);
527                 exit(0);
528             }
529 #endif
530
531             if (dsi->flags & DSI_RECONINPROG) {
532                 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
533                 afp_dsi_close(obj);
534                 exit(0);
535             }
536
537             /* Some error on the client connection, enter disconnected state */
538             if (dsi_disconnect(dsi) != 0)
539                 afp_dsi_die(EXITERR_CLNT);
540
541             while (dsi->flags & DSI_DISCONNECTED)
542                 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
543             continue; /* continue receiving until disconnect timer expires
544                        * or a primary reconnect succeeds  */
545         }
546
547         if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
548             LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
549             dsi->flags &= ~DSI_SLEEPING;
550             dsi->tickle = 0;
551         }
552
553         if (reload_request) {
554             reload_request = 0;
555             load_volumes(AFPobj);
556         }
557
558         /* The first SIGINT enables debugging, the next restores the config */
559         if (debug_request) {
560             static int debugging = 0;
561             debug_request = 0;
562
563             dircache_dump();
564             uuidcache_dump();
565
566             if (debugging) {
567                 if (obj->options.logconfig)
568                     setuplog(obj->options.logconfig, obj->options.logfile);
569                 else
570                     setuplog("default:note", NULL);
571                 debugging = 0;
572             } else {
573                 char logstr[50];
574                 debugging = 1;
575                 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
576                 setuplog("default:maxdebug", logstr);
577             }
578         }
579
580
581         dsi->flags |= DSI_DATA;
582         dsi->tickle = 0;
583
584         switch(cmd) {
585
586         case DSIFUNC_CLOSE:
587             LOG(log_debug, logtype_afpd, "DSI: close session request");
588             afp_dsi_close(obj);
589             LOG(log_note, logtype_afpd, "done");
590             exit(0);
591
592         case DSIFUNC_TICKLE:
593             dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
594             LOG(log_debug, logtype_afpd, "DSI: client tickle");
595             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
596             if ((dsi->flags & DSI_DIE))
597                 dsi_tickle(dsi);
598             break;
599
600         case DSIFUNC_CMD:
601 #ifdef AFS
602             if ( writtenfork ) {
603                 if ( flushfork( writtenfork ) < 0 ) {
604                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
605                 }
606                 writtenfork = NULL;
607             }
608 #endif /* AFS */
609
610             function = (u_char) dsi->commands[0];
611
612             /* AFP replay cache */
613             rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
614             LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
615
616             if (replaycache[rc_idx].DSIreqID == dsi->clientID
617                 && replaycache[rc_idx].AFPcommand == function) {
618                 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
619                     dsi->clientID, AfpNum2name(function));
620                 err = replaycache[rc_idx].result;
621             /* AFP replay cache end */
622             } else {
623                 /* send off an afp command. in a couple cases, we take advantage
624                  * of the fact that we're a stream-based protocol. */
625                 if (afp_switch[function]) {
626                     dsi->datalen = DSI_DATASIZ;
627                     dsi->flags |= DSI_RUNNING;
628
629                     LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
630
631                     err = (*afp_switch[function])(obj,
632                                                   (char *)dsi->commands, dsi->cmdlen,
633                                                   (char *)&dsi->data, &dsi->datalen);
634
635                     LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
636                         AfpNum2name(function), AfpErr2name(err));
637
638                     dir_free_invalid_q();
639
640                     dsi->flags &= ~DSI_RUNNING;
641
642                     /* Add result to the AFP replay cache */
643                     replaycache[rc_idx].DSIreqID = dsi->clientID;
644                     replaycache[rc_idx].AFPcommand = function;
645                     replaycache[rc_idx].result = err;
646                 } else {
647                     LOG(log_error, logtype_afpd, "bad function %X", function);
648                     dsi->datalen = 0;
649                     err = AFPERR_NOOP;
650                 }
651             }
652
653             /* single shot toggle that gets set by dsi_readinit. */
654             if (dsi->flags & DSI_NOREPLY) {
655                 dsi->flags &= ~DSI_NOREPLY;
656                 break;
657             } else if (!dsi_cmdreply(dsi, err)) {
658                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
659                 if (dsi_disconnect(dsi) != 0)
660                     afp_dsi_die(EXITERR_CLNT);
661             }
662             break;
663
664         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
665             function = (u_char) dsi->commands[0];
666             if ( afp_switch[ function ] != NULL ) {
667                 dsi->datalen = DSI_DATASIZ;
668                 dsi->flags |= DSI_RUNNING;
669
670                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
671
672                 err = (*afp_switch[function])(obj,
673                                               (char *)dsi->commands, dsi->cmdlen,
674                                               (char *)&dsi->data, &dsi->datalen);
675
676                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
677                     AfpNum2name(function), AfpErr2name(err));
678
679                 dsi->flags &= ~DSI_RUNNING;
680             } else {
681                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
682                 dsi->datalen = 0;
683                 err = AFPERR_NOOP;
684             }
685
686             if (!dsi_wrtreply(dsi, err)) {
687                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
688                 if (dsi_disconnect(dsi) != 0)
689                     afp_dsi_die(EXITERR_CLNT);
690             }
691             break;
692
693         case DSIFUNC_ATTN: /* attention replies */
694             break;
695
696             /* error. this usually implies a mismatch of some kind
697              * between server and client. if things are correct,
698              * we need to flush the rest of the packet if necessary. */
699         default:
700             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
701             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
702             dsi_writeflush(dsi);
703             break;
704         }
705         pending_request(dsi);
706
707         fce_pending_events(obj);
708     }
709
710     /* error */
711     afp_dsi_die(EXITERR_CLNT);
712 }