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