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