]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Merge pull request #15 from xrmx/coverity1
[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     /* 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_stream_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
493             /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
494             if (dsi->flags & DSI_AFP_LOGGED_OUT) {
495                 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
496                 afp_dsi_close(obj);
497                 exit(0);
498             }
499
500 #if 0
501             /*  got ECONNRESET in read from client => exit*/
502             if (dsi->flags & DSI_GOT_ECONNRESET) {
503                 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
504                 afp_dsi_close(obj);
505                 exit(0);
506             }
507 #endif
508
509             if (dsi->flags & DSI_RECONINPROG) {
510                 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
511                 afp_dsi_close(obj);
512                 exit(0);
513             }
514
515             /* Some error on the client connection, enter disconnected state */
516             if (dsi_disconnect(dsi) != 0)
517                 afp_dsi_die(EXITERR_CLNT);
518
519             while (dsi->flags & DSI_DISCONNECTED)
520                 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
521             continue; /* continue receiving until disconnect timer expires
522                        * or a primary reconnect succeeds  */
523         }
524
525         if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
526             LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
527             dsi->flags &= ~DSI_SLEEPING;
528             dsi->tickle = 0;
529         }
530
531         if (reload_request) {
532             reload_request = 0;
533             load_volumes(AFPobj);
534         }
535
536         /* The first SIGINT enables debugging, the next restores the config */
537         if (debug_request) {
538             static int debugging = 0;
539             debug_request = 0;
540
541             dircache_dump();
542             uuidcache_dump();
543
544             if (debugging) {
545                 if (obj->options.logconfig)
546                     setuplog(obj->options.logconfig, obj->options.logfile);
547                 else
548                     setuplog("default:note", NULL);
549                 debugging = 0;
550             } else {
551                 char logstr[50];
552                 debugging = 1;
553                 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
554                 setuplog("default:maxdebug", logstr);
555             }
556         }
557
558
559         dsi->flags |= DSI_DATA;
560         dsi->tickle = 0;
561
562         switch(cmd) {
563
564         case DSIFUNC_CLOSE:
565             LOG(log_debug, logtype_afpd, "DSI: close session request");
566             afp_dsi_close(obj);
567             LOG(log_note, logtype_afpd, "done");
568             exit(0);
569
570         case DSIFUNC_TICKLE:
571             dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
572             LOG(log_debug, logtype_afpd, "DSI: client tickle");
573             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
574             if ((dsi->flags & DSI_DIE))
575                 dsi_tickle(dsi);
576             break;
577
578         case DSIFUNC_CMD:
579 #ifdef AFS
580             if ( writtenfork ) {
581                 if ( flushfork( writtenfork ) < 0 ) {
582                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
583                 }
584                 writtenfork = NULL;
585             }
586 #endif /* AFS */
587
588             function = (u_char) dsi->commands[0];
589
590             /* AFP replay cache */
591             rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
592             LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
593
594             if (replaycache[rc_idx].DSIreqID == dsi->clientID
595                 && replaycache[rc_idx].AFPcommand == function) {
596                 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
597                     dsi->clientID, AfpNum2name(function));
598                 err = replaycache[rc_idx].result;
599             /* AFP replay cache end */
600             } else {
601                 /* send off an afp command. in a couple cases, we take advantage
602                  * of the fact that we're a stream-based protocol. */
603                 if (afp_switch[function]) {
604                     dsi->datalen = DSI_DATASIZ;
605                     dsi->flags |= DSI_RUNNING;
606
607                     LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
608
609                     err = (*afp_switch[function])(obj,
610                                                   (char *)dsi->commands, dsi->cmdlen,
611                                                   (char *)&dsi->data, &dsi->datalen);
612
613                     LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
614                         AfpNum2name(function), AfpErr2name(err));
615
616                     dir_free_invalid_q();
617
618                     dsi->flags &= ~DSI_RUNNING;
619
620                     /* Add result to the AFP replay cache */
621                     replaycache[rc_idx].DSIreqID = dsi->clientID;
622                     replaycache[rc_idx].AFPcommand = function;
623                     replaycache[rc_idx].result = err;
624                 } else {
625                     LOG(log_error, logtype_afpd, "bad function %X", function);
626                     dsi->datalen = 0;
627                     err = AFPERR_NOOP;
628                 }
629             }
630
631             /* single shot toggle that gets set by dsi_readinit. */
632             if (dsi->flags & DSI_NOREPLY) {
633                 dsi->flags &= ~DSI_NOREPLY;
634                 break;
635             } else if (!dsi_cmdreply(dsi, err)) {
636                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
637                 if (dsi_disconnect(dsi) != 0)
638                     afp_dsi_die(EXITERR_CLNT);
639             }
640             break;
641
642         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
643             function = (u_char) dsi->commands[0];
644             if ( afp_switch[ function ] != NULL ) {
645                 dsi->datalen = DSI_DATASIZ;
646                 dsi->flags |= DSI_RUNNING;
647
648                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
649
650                 err = (*afp_switch[function])(obj,
651                                               (char *)dsi->commands, dsi->cmdlen,
652                                               (char *)&dsi->data, &dsi->datalen);
653
654                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
655                     AfpNum2name(function), AfpErr2name(err));
656
657                 dsi->flags &= ~DSI_RUNNING;
658             } else {
659                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
660                 dsi->datalen = 0;
661                 err = AFPERR_NOOP;
662             }
663
664             if (!dsi_wrtreply(dsi, err)) {
665                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
666                 if (dsi_disconnect(dsi) != 0)
667                     afp_dsi_die(EXITERR_CLNT);
668             }
669             break;
670
671         case DSIFUNC_ATTN: /* attention replies */
672             break;
673
674             /* error. this usually implies a mismatch of some kind
675              * between server and client. if things are correct,
676              * we need to flush the rest of the packet if necessary. */
677         default:
678             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
679             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
680             dsi_writeflush(dsi);
681             break;
682         }
683         pending_request(dsi);
684
685         fce_pending_events(obj);
686     }
687
688     /* error */
689     afp_dsi_die(EXITERR_CLNT);
690 }