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