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