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