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