]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Fix regression introduced by previous IPC fds fix
[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
41 #include <atalk/globals.h>
42 #include "switch.h"
43 #include "auth.h"
44 #include "fork.h"
45 #include "dircache.h"
46
47 #ifdef FORCE_UIDGID
48 #warning UIDGID
49 #include "uid.h"
50 #endif /* FORCE_UIDGID */
51
52 #ifndef SOL_TCP
53 #define SOL_TCP IPPROTO_TCP
54 #endif
55
56 /* 
57  * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
58  * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
59  * but are deeply nested in the function chain with the caller already without acces to it.
60  * Changing this would require adding a reference to the caller which itself might be
61  * called in many places (eg acltoownermode is called from accessmode).
62  * The only sane way out is providing a copy of it here:
63  */
64 AFPObj *AFPobj = NULL;
65
66 typedef struct {
67     uint16_t DSIreqID;
68     uint8_t  AFPcommand;
69     uint32_t result;
70 } rc_elem_t;
71
72 /*
73  * AFP replay cache:
74  * - fix sized array
75  * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
76  */
77 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
78
79 static sigjmp_buf recon_jmp;
80 static void afp_dsi_close(AFPObj *obj)
81 {
82     DSI *dsi = obj->handle;
83
84     close(obj->ipc_fd);
85     obj->ipc_fd = -1;
86
87     /* we may have been called from a signal handler caught when afpd was running
88      * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
89      * restore our login user
90      */
91     if (geteuid() != obj->uid) {
92         if (seteuid( obj->uid ) < 0) {
93             LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
94                 obj->uid, strerror(errno), getuid(), geteuid());
95             exit(EXITERR_SYS);
96         }
97     }
98
99     close_all_vol();
100     if (obj->logout)
101         (*obj->logout)();
102
103     LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
104         dsi->read_count/1024.0, dsi->write_count/1024.0);
105     log_dircache_stat();
106
107     dsi_close(dsi);
108 }
109
110 /* -------------------------------
111  * SIGTERM
112  * a little bit of code duplication. 
113  */
114 static void afp_dsi_die(int sig)
115 {
116     DSI *dsi = (DSI *)AFPobj->handle;
117
118     if (dsi->flags & DSI_RECONINPROG) {
119         /* Primary reconnect succeeded, got SIGTERM from afpd parent */
120         dsi->flags &= ~DSI_RECONINPROG;
121         return; /* this returns to afp_disconnect */
122     }
123
124     if (dsi->flags & DSI_DISCONNECTED) {
125         LOG(log_note, logtype_afpd, "Disconnected session terminating");
126         exit(0);
127     }
128
129     dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
130     afp_dsi_close(AFPobj);
131    if (sig) /* if no signal, assume dieing because logins are disabled &
132                 don't log it (maintenance mode)*/
133         LOG(log_info, logtype_afpd, "Connection terminated");
134     if (sig == SIGTERM || sig == SIGALRM) {
135         exit( 0 );
136     }
137     else {
138         exit(sig);
139     }
140 }
141
142 /* SIGQUIT handler */
143 static void ipc_reconnect_handler(int sig _U_)
144 {
145     DSI *dsi = (DSI *)AFPobj->handle;
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->handle;
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->handle;
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->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
222                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
223         DSI *dsi = (DSI *)AFPobj->handle;
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->handle;
281
282     dsi->msg_request = 1;
283     if (dsi_attention(AFPobj->handle, 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->handle;
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->handle);
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->handle, 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->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
376                   AFPATTN_MESG | AFPATTN_TIME(5));
377     }
378 }
379
380 /* -------------------------------------------
381  afp over dsi. this never returns. 
382 */
383 void afp_over_dsi(AFPObj *obj)
384 {
385     DSI *dsi = (DSI *) obj->handle;
386     int rc_idx;
387     u_int32_t err, cmd;
388     u_int8_t function;
389     struct sigaction action;
390
391     AFPobj = obj;
392     obj->exit = afp_dsi_die;
393     obj->reply = (int (*)()) dsi_cmdreply;
394     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
395     dsi->tickle = 0;
396
397     memset(&action, 0, sizeof(action));
398     sigfillset(&action.sa_mask);
399     action.sa_flags = SA_RESTART;
400
401     /* install SIGHUP */
402     action.sa_handler = afp_dsi_reload;
403     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
404         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
405         afp_dsi_die(EXITERR_SYS);
406     }
407
408     /* install SIGURG */
409     action.sa_handler = afp_dsi_transfer_session;
410     if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
411         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
412         afp_dsi_die(EXITERR_SYS);
413     }
414
415     /* install SIGTERM */
416     action.sa_handler = afp_dsi_die;
417     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
418         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
419         afp_dsi_die(EXITERR_SYS);
420     }
421
422     /* install SIGQUIT */
423     action.sa_handler = ipc_reconnect_handler;
424     if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
425         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
426         afp_dsi_die(EXITERR_SYS);
427     }
428
429     /* SIGUSR2 - server message support */
430     action.sa_handler = afp_dsi_getmesg;
431     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
432         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
433         afp_dsi_die(EXITERR_SYS);
434     }
435
436     /*  SIGUSR1 - set down in 5 minutes  */
437     action.sa_handler = afp_dsi_timedown;
438     action.sa_flags = SA_RESTART;
439     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
440         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
441         afp_dsi_die(EXITERR_SYS);
442     }
443
444     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
445     action.sa_handler = afp_dsi_debug;
446     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
447         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
448         afp_dsi_die(EXITERR_SYS);
449     }
450
451 #ifndef DEBUGGING
452     /* SIGALRM - tickle handler */
453     action.sa_handler = alarm_handler;
454     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
455             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
456         afp_dsi_die(EXITERR_SYS);
457     }
458 #endif /* DEBUGGING */
459
460     if (dircache_init(obj->options.dircachesize) != 0)
461         afp_dsi_die(EXITERR_SYS);
462
463     /* set TCP snd/rcv buf */
464     if (obj->options.tcp_rcvbuf) {
465         if (setsockopt(dsi->socket,
466                        SOL_SOCKET,
467                        SO_RCVBUF,
468                        &obj->options.tcp_rcvbuf,
469                        sizeof(obj->options.tcp_rcvbuf)) != 0) {
470             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
471         }
472     }
473     if (obj->options.tcp_sndbuf) {
474         if (setsockopt(dsi->socket,
475                        SOL_SOCKET,
476                        SO_SNDBUF,
477                        &obj->options.tcp_sndbuf,
478                        sizeof(obj->options.tcp_sndbuf)) != 0) {
479             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
480         }
481     }
482
483     /* set TCP_NODELAY */
484     int flag = 1;
485     setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
486
487     /* get stuck here until the end */
488     while (1) {
489         if (sigsetjmp(recon_jmp, 1) != 0)
490             /* returning from SIGALARM handler for a primary reconnect */
491             continue;
492
493         /* Blocking read on the network socket */
494         cmd = dsi_stream_receive(dsi);
495
496         if (cmd == 0) {
497             /* cmd == 0 is the error condition */
498             if (dsi->flags & DSI_RECONSOCKET) {
499                 /* we just got a reconnect so we immediately try again to receive on the new fd */
500                 dsi->flags &= ~DSI_RECONSOCKET;
501                 continue;
502             }
503
504             /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
505             if (dsi->flags & DSI_AFP_LOGGED_OUT) {
506                 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
507                 afp_dsi_close(obj);
508                 exit(0);
509             }
510
511 #if 0
512             /*  got ECONNRESET in read from client => exit*/
513             if (dsi->flags & DSI_GOT_ECONNRESET) {
514                 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
515                 afp_dsi_close(obj);
516                 exit(0);
517             }
518 #endif
519
520             if (dsi->flags & DSI_RECONINPROG) {
521                 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
522                 afp_dsi_close(obj);
523                 exit(0);
524             }
525
526             /* Some error on the client connection, enter disconnected state */
527             if (dsi_disconnect(dsi) != 0)
528                 afp_dsi_die(EXITERR_CLNT);
529
530             while (dsi->flags & DSI_DISCONNECTED)
531                 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
532             continue; /* continue receiving until disconnect timer expires
533                        * or a primary reconnect succeeds  */
534         }
535
536         if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
537             LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
538             dsi->flags &= ~DSI_SLEEPING;
539             dsi->tickle = 0;
540         }
541
542         if (reload_request) {
543             reload_request = 0;
544             load_volumes(AFPobj);
545         }
546
547         /* The first SIGINT enables debugging, the next restores the config */
548         if (debug_request) {
549             static int debugging = 0;
550             debug_request = 0;
551
552             dircache_dump();
553             uuidcache_dump();
554
555             if (debugging) {
556                 if (obj->options.logconfig)
557                     setuplog(obj->options.logconfig);
558                 else
559                     setuplog("default log_note");
560                 debugging = 0;
561             } else {
562                 char logstr[50];
563                 debugging = 1;
564                 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
565                 setuplog(logstr);
566             }
567         }
568
569
570         dsi->flags |= DSI_DATA;
571         dsi->tickle = 0;
572
573         switch(cmd) {
574
575         case DSIFUNC_CLOSE:
576             LOG(log_debug, logtype_afpd, "DSI: close session request");
577             afp_dsi_close(obj);
578             LOG(log_note, logtype_afpd, "done");
579             exit(0);
580
581         case DSIFUNC_TICKLE:
582             dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
583             LOG(log_debug, logtype_afpd, "DSI: client tickle");
584             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
585             if ((dsi->flags & DSI_DIE))
586                 dsi_tickle(dsi);
587             break;
588
589         case DSIFUNC_CMD:
590 #ifdef AFS
591             if ( writtenfork ) {
592                 if ( flushfork( writtenfork ) < 0 ) {
593                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
594                 }
595                 writtenfork = NULL;
596             }
597 #endif /* AFS */
598
599             function = (u_char) dsi->commands[0];
600
601             /* AFP replay cache */
602             rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
603             LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
604
605             if (replaycache[rc_idx].DSIreqID == dsi->clientID
606                 && replaycache[rc_idx].AFPcommand == function) {
607                 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
608                     dsi->clientID, AfpNum2name(function));
609                 err = replaycache[rc_idx].result;
610             /* AFP replay cache end */
611             } else {
612                 /* send off an afp command. in a couple cases, we take advantage
613                  * of the fact that we're a stream-based protocol. */
614                 if (afp_switch[function]) {
615                     dsi->datalen = DSI_DATASIZ;
616                     dsi->flags |= DSI_RUNNING;
617
618                     LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
619
620                     err = (*afp_switch[function])(obj,
621                                                   (char *)&dsi->commands, dsi->cmdlen,
622                                                   (char *)&dsi->data, &dsi->datalen);
623
624                     LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
625                         AfpNum2name(function), AfpErr2name(err));
626
627                     dir_free_invalid_q();
628
629 #ifdef FORCE_UIDGID
630                     /* bring everything back to old euid, egid */
631                     if (obj->force_uid)
632                         restore_uidgid ( &obj->uidgid );
633 #endif /* FORCE_UIDGID */
634                     dsi->flags &= ~DSI_RUNNING;
635
636                     /* Add result to the AFP replay cache */
637                     replaycache[rc_idx].DSIreqID = dsi->clientID;
638                     replaycache[rc_idx].AFPcommand = function;
639                     replaycache[rc_idx].result = err;
640                 } else {
641                     LOG(log_error, logtype_afpd, "bad function %X", function);
642                     dsi->datalen = 0;
643                     err = AFPERR_NOOP;
644                 }
645             }
646
647             /* single shot toggle that gets set by dsi_readinit. */
648             if (dsi->flags & DSI_NOREPLY) {
649                 dsi->flags &= ~DSI_NOREPLY;
650                 break;
651             } else if (!dsi_cmdreply(dsi, err)) {
652                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
653                 if (dsi_disconnect(dsi) != 0)
654                     afp_dsi_die(EXITERR_CLNT);
655             }
656             break;
657
658         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
659             function = (u_char) dsi->commands[0];
660             if ( afp_switch[ function ] != NULL ) {
661                 dsi->datalen = DSI_DATASIZ;
662                 dsi->flags |= DSI_RUNNING;
663
664                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
665
666                 err = (*afp_switch[function])(obj,
667                                               (char *)&dsi->commands, dsi->cmdlen,
668                                               (char *)&dsi->data, &dsi->datalen);
669
670                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
671                     AfpNum2name(function), AfpErr2name(err));
672
673                 dsi->flags &= ~DSI_RUNNING;
674 #ifdef FORCE_UIDGID
675                 /* bring everything back to old euid, egid */
676                 if (obj->force_uid)
677                     restore_uidgid ( &obj->uidgid );
678 #endif /* FORCE_UIDGID */
679             } else {
680                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
681                 dsi->datalen = 0;
682                 err = AFPERR_NOOP;
683             }
684
685             if (!dsi_wrtreply(dsi, err)) {
686                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
687                 if (dsi_disconnect(dsi) != 0)
688                     afp_dsi_die(EXITERR_CLNT);
689             }
690             break;
691
692         case DSIFUNC_ATTN: /* attention replies */
693             break;
694
695             /* error. this usually implies a mismatch of some kind
696              * between server and client. if things are correct,
697              * we need to flush the rest of the packet if necessary. */
698         default:
699             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
700             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
701             dsi_writeflush(dsi);
702             break;
703         }
704         pending_request(dsi);
705
706         fce_pending_events(obj);
707     }
708
709     /* error */
710     afp_dsi_die(EXITERR_CLNT);
711 }