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