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