]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Merge remote-tracking branch 'origin/branch-netatalk-3-0' into develop
[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 /* ----------------- 
341    if dsi->in_write is set attention, tickle (and close?) msg
342    aren't sent. We don't care about tickle 
343 */
344 static void pending_request(DSI *dsi)
345 {
346     /* send pending attention */
347
348     /* read msg if any, it could be done in afp_getsrvrmesg */
349     if (dsi->msg_request) {
350         if (dsi->msg_request == 2) {
351             /* didn't send it in signal handler */
352             dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
353         }
354         dsi->msg_request = 0;
355         readmessage(AFPobj);
356     }
357     if (dsi->down_request) {
358         dsi->down_request = 0;
359         dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
360                   AFPATTN_MESG | AFPATTN_TIME(5));
361     }
362 }
363
364 void afp_over_dsi_sighandlers(AFPObj *obj)
365 {
366     DSI *dsi = (DSI *) obj->dsi;
367     struct sigaction action;
368
369     memset(&action, 0, sizeof(action));
370     sigfillset(&action.sa_mask);
371     action.sa_flags = SA_RESTART;
372
373     /* install SIGHUP */
374     action.sa_handler = afp_dsi_reload;
375     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
376         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
377         afp_dsi_die(EXITERR_SYS);
378     }
379
380     /* install SIGURG */
381     action.sa_handler = afp_dsi_transfer_session;
382     if ( sigaction( SIGURG, &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 SIGTERM */
388     action.sa_handler = afp_dsi_die;
389     if ( sigaction( SIGTERM, &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 SIGQUIT */
395     action.sa_handler = afp_dsi_die;
396     if ( sigaction(SIGQUIT, &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     /* SIGUSR2 - server message support */
402     action.sa_handler = afp_dsi_getmesg;
403     if ( sigaction( SIGUSR2, &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     /*  SIGUSR1 - set down in 5 minutes  */
409     action.sa_handler = afp_dsi_timedown;
410     action.sa_flags = SA_RESTART;
411     if ( sigaction( SIGUSR1, &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     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
417     action.sa_handler = afp_dsi_debug;
418     if ( sigaction( SIGINT, &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 #ifndef DEBUGGING
424     /* SIGALRM - tickle handler */
425     action.sa_handler = alarm_handler;
426     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
427             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
428         afp_dsi_die(EXITERR_SYS);
429     }
430 #endif /* DEBUGGING */
431 }
432
433 /* -------------------------------------------
434  afp over dsi. this never returns. 
435 */
436 void afp_over_dsi(AFPObj *obj)
437 {
438     DSI *dsi = (DSI *) obj->dsi;
439     int rc_idx;
440     uint32_t err, cmd;
441     uint8_t function;
442
443     AFPobj = obj;
444     obj->exit = afp_dsi_die;
445     obj->reply = (int (*)()) dsi_cmdreply;
446     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
447     dsi->tickle = 0;
448
449     afp_over_dsi_sighandlers(obj);
450
451     if (dircache_init(obj->options.dircachesize) != 0)
452         afp_dsi_die(EXITERR_SYS);
453
454     /* set TCP snd/rcv buf */
455     if (obj->options.tcp_rcvbuf) {
456         if (setsockopt(dsi->socket,
457                        SOL_SOCKET,
458                        SO_RCVBUF,
459                        &obj->options.tcp_rcvbuf,
460                        sizeof(obj->options.tcp_rcvbuf)) != 0) {
461             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
462         }
463     }
464     if (obj->options.tcp_sndbuf) {
465         if (setsockopt(dsi->socket,
466                        SOL_SOCKET,
467                        SO_SNDBUF,
468                        &obj->options.tcp_sndbuf,
469                        sizeof(obj->options.tcp_sndbuf)) != 0) {
470             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
471         }
472     }
473
474     /* set TCP_NODELAY */
475     int flag = 1;
476     setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
477
478     /* Initialize Spotlight */
479     if ((obj->options.flags & OPTION_SPOTLIGHT) && (obj->options.slmod_path))
480         sl_mod_load(obj->options.slmod_path);
481
482     ipc_child_state(obj, DSI_RUNNING);
483
484     /* get stuck here until the end */
485     while (1) {
486         if (sigsetjmp(recon_jmp, 1) != 0)
487             /* returning from SIGALARM handler for a primary reconnect */
488             continue;
489
490         /* Blocking read on the network socket */
491         cmd = dsi_stream_receive(dsi);
492
493         if (cmd == 0) {
494             /* cmd == 0 is the error condition */
495             if (dsi->flags & DSI_RECONSOCKET) {
496                 /* we just got a reconnect so we immediately try again to receive on the new fd */
497                 dsi->flags &= ~DSI_RECONSOCKET;
498                 continue;
499             }
500
501             /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
502             if (dsi->flags & DSI_AFP_LOGGED_OUT) {
503                 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
504                 afp_dsi_close(obj);
505                 exit(0);
506             }
507
508             if (dsi->flags & DSI_RECONINPROG) {
509                 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
510                 afp_dsi_close(obj);
511                 exit(0);
512             }
513
514             /* Some error on the client connection, enter disconnected state */
515             if (dsi_disconnect(dsi) != 0)
516                 afp_dsi_die(EXITERR_CLNT);
517
518             ipc_child_state(obj, DSI_DISCONNECTED);
519
520             while (dsi->flags & DSI_DISCONNECTED)
521                 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
522             ipc_child_state(obj, DSI_RUNNING);
523             continue; /* continue receiving until disconnect timer expires
524                        * or a primary reconnect succeeds  */
525         }
526
527         if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
528             LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
529             dsi->flags &= ~DSI_SLEEPING;
530             dsi->tickle = 0;
531             ipc_child_state(obj, DSI_RUNNING);
532         }
533
534         if (reload_request) {
535             reload_request = 0;
536             load_volumes(AFPobj);
537         }
538
539         /* The first SIGINT enables debugging, the next restores the config */
540         if (debug_request) {
541             static int debugging = 0;
542             debug_request = 0;
543
544             dircache_dump();
545             uuidcache_dump();
546
547             if (debugging) {
548                 if (obj->options.logconfig)
549                     setuplog(obj->options.logconfig, obj->options.logfile);
550                 else
551                     setuplog("default:note", NULL);
552                 debugging = 0;
553             } else {
554                 char logstr[50];
555                 debugging = 1;
556                 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
557                 setuplog("default:maxdebug", logstr);
558             }
559         }
560
561
562         dsi->flags |= DSI_DATA;
563         dsi->tickle = 0;
564
565         switch(cmd) {
566
567         case DSIFUNC_CLOSE:
568             LOG(log_debug, logtype_afpd, "DSI: close session request");
569             afp_dsi_close(obj);
570             LOG(log_note, logtype_afpd, "done");
571             exit(0);
572
573         case DSIFUNC_TICKLE:
574             dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
575             LOG(log_debug, logtype_afpd, "DSI: client tickle");
576             /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
577             if ((dsi->flags & DSI_DIE))
578                 dsi_tickle(dsi);
579             break;
580
581         case DSIFUNC_CMD:
582 #ifdef AFS
583             if ( writtenfork ) {
584                 if ( flushfork( writtenfork ) < 0 ) {
585                     LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
586                 }
587                 writtenfork = NULL;
588             }
589 #endif /* AFS */
590
591             function = (u_char) dsi->commands[0];
592
593             /* AFP replay cache */
594             rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
595             LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
596
597             if (replaycache[rc_idx].DSIreqID == dsi->clientID
598                 && replaycache[rc_idx].AFPcommand == function) {
599                 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
600                     dsi->clientID, AfpNum2name(function));
601                 err = replaycache[rc_idx].result;
602             /* AFP replay cache end */
603             } else {
604                 /* send off an afp command. in a couple cases, we take advantage
605                  * of the fact that we're a stream-based protocol. */
606                 if (afp_switch[function]) {
607                     dsi->datalen = DSI_DATASIZ;
608                     dsi->flags |= DSI_RUNNING;
609
610                     LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
611
612                     AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
613                     err = (*afp_switch[function])(obj,
614                                                   (char *)dsi->commands, dsi->cmdlen,
615                                                   (char *)&dsi->data, &dsi->datalen);
616
617                     AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
618                     LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
619                         AfpNum2name(function), AfpErr2name(err));
620
621                     dir_free_invalid_q();
622
623                     dsi->flags &= ~DSI_RUNNING;
624
625                     /* Add result to the AFP replay cache */
626                     replaycache[rc_idx].DSIreqID = dsi->clientID;
627                     replaycache[rc_idx].AFPcommand = function;
628                     replaycache[rc_idx].result = err;
629                 } else {
630                     LOG(log_error, logtype_afpd, "bad function %X", function);
631                     dsi->datalen = 0;
632                     err = AFPERR_NOOP;
633                 }
634             }
635
636             /* single shot toggle that gets set by dsi_readinit. */
637             if (dsi->flags & DSI_NOREPLY) {
638                 dsi->flags &= ~DSI_NOREPLY;
639                 break;
640             } else if (!dsi_cmdreply(dsi, err)) {
641                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
642                 if (dsi_disconnect(dsi) != 0)
643                     afp_dsi_die(EXITERR_CLNT);
644             }
645             break;
646
647         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
648             function = (u_char) dsi->commands[0];
649             if ( afp_switch[ function ] != NULL ) {
650                 dsi->datalen = DSI_DATASIZ;
651                 dsi->flags |= DSI_RUNNING;
652
653                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
654
655                 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
656
657                 err = (*afp_switch[function])(obj,
658                                               (char *)dsi->commands, dsi->cmdlen,
659                                               (char *)&dsi->data, &dsi->datalen);
660
661                 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
662
663                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
664                     AfpNum2name(function), AfpErr2name(err));
665
666                 dsi->flags &= ~DSI_RUNNING;
667             } else {
668                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
669                 dsi->datalen = 0;
670                 err = AFPERR_NOOP;
671             }
672
673             if (!dsi_wrtreply(dsi, err)) {
674                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
675                 if (dsi_disconnect(dsi) != 0)
676                     afp_dsi_die(EXITERR_CLNT);
677             }
678             break;
679
680         case DSIFUNC_ATTN: /* attention replies */
681             break;
682
683             /* error. this usually implies a mismatch of some kind
684              * between server and client. if things are correct,
685              * we need to flush the rest of the packet if necessary. */
686         default:
687             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
688             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
689             dsi_writeflush(dsi);
690             break;
691         }
692         pending_request(dsi);
693
694         fce_pending_events(obj);
695     }
696
697     /* error */
698     afp_dsi_die(EXITERR_CLNT);
699 }