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