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