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