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