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