]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Add another check to prevent running as root in disconnected state
[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
36 #include "globals.h"
37 #include "switch.h"
38 #include "auth.h"
39 #include "fork.h"
40 #include "dircache.h"
41
42 #ifdef FORCE_UIDGID
43 #warning UIDGID
44 #include "uid.h"
45 #endif /* FORCE_UIDGID */
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->handle;
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->handle;
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->handle, 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 /* SIGURG handler (primary reconnect) */
134 static void afp_dsi_transfer_session(int sig _U_)
135 {
136     uint16_t dsiID;
137     int socket;
138     DSI *dsi = (DSI *)AFPobj->handle;
139
140     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
141
142     if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
143         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
144         afp_dsi_close(AFPobj);
145         exit(EXITERR_SYS);
146     }
147
148     if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
149         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
150         afp_dsi_close(AFPobj);
151         exit(EXITERR_SYS);
152     }
153
154     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
155
156     dsi->proto_close(dsi);
157     dsi->socket = socket;
158     dsi->flags = DSI_RECONSOCKET;
159     dsi->datalen = 0;
160     dsi->eof = dsi->start = dsi->buffer;
161     dsi->in_write = 0;
162     dsi->header.dsi_requestID = dsiID;
163     dsi->header.dsi_command = DSIFUNC_CMD;
164
165     /*
166      * The session transfer happens in the middle of FPDisconnect old session, thus we
167      * have to send the reply now.
168      */
169     if (!dsi_cmdreply(dsi, AFP_OK)) {
170         LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
171         afp_dsi_close(AFPobj);
172         exit(EXITERR_CLNT);
173     }
174
175     LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
176     /* 
177      * Now returning from this signal handler return to dsi_receive which should start
178      * reading/continuing from the connected socket that was passed via the parent from
179      * another session. The parent will terminate that session.
180      */
181     siglongjmp(recon_jmp, 1);
182 }
183
184 /* ------------------- */
185 static void afp_dsi_timedown(int sig _U_)
186 {
187     struct sigaction    sv;
188     struct itimerval    it;
189     DSI                 *dsi = (DSI *)AFPobj->handle;
190     dsi->flags |= DSI_DIE;
191     /* shutdown and don't reconnect. server going down in 5 minutes. */
192     setmessage("The server is going down for maintenance.");
193     if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
194                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
195         DSI *dsi = (DSI *)AFPobj->handle;
196         dsi->down_request = 1;
197     }                  
198
199     it.it_interval.tv_sec = 0;
200     it.it_interval.tv_usec = 0;
201     it.it_value.tv_sec = 300;
202     it.it_value.tv_usec = 0;
203
204     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
205         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
206         afp_dsi_die(EXITERR_SYS);
207     }
208     memset(&sv, 0, sizeof(sv));
209     sv.sa_handler = afp_dsi_die;
210     sigemptyset( &sv.sa_mask );
211     sigaddset(&sv.sa_mask, SIGHUP);
212     sigaddset(&sv.sa_mask, SIGTERM);
213     sv.sa_flags = SA_RESTART;
214     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
215         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
216         afp_dsi_die(EXITERR_SYS);
217     }
218
219     /* ignore myself */
220     sv.sa_handler = SIG_IGN;
221     sigemptyset( &sv.sa_mask );
222     sv.sa_flags = SA_RESTART;
223     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
224         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
225         afp_dsi_die(EXITERR_SYS);
226     }
227 }
228
229 /* ---------------------------------
230  * SIGHUP reload configuration file
231  */
232 volatile int reload_request = 0;
233
234 static void afp_dsi_reload(int sig _U_)
235 {
236     reload_request = 1;
237 }
238
239 /* ---------------------------------
240  * SIGINT: enable max_debug LOGging
241  */
242 static volatile sig_atomic_t debug_request = 0;
243
244 static void afp_dsi_debug(int sig _U_)
245 {
246     debug_request = 1;
247 }
248
249 /* ---------------------- */
250 static void afp_dsi_getmesg (int sig _U_)
251 {
252     DSI *dsi = (DSI *)AFPobj->handle;
253
254     dsi->msg_request = 1;
255     if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
256         dsi->msg_request = 2;
257 }
258
259 static void alarm_handler(int sig _U_)
260 {
261     int err;
262     DSI *dsi = (DSI *)AFPobj->handle;
263
264     /* we have to restart the timer because some libraries may use alarm() */
265     setitimer(ITIMER_REAL, &dsi->timer, NULL);
266
267     /* we got some traffic from the client since the previous timer tick. */
268     if ((dsi->flags & DSI_DATA)) {
269         dsi->flags &= ~DSI_DATA;
270         return;
271     }
272
273     dsi->tickle++;
274     LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
275         dsi->tickle,
276         (dsi->flags & DSI_DATA) ?         "DSI_DATA" : "-",
277         (dsi->flags & DSI_RUNNING) ?      "DSI_RUNNING" : "-",
278         (dsi->flags & DSI_SLEEPING) ?     "DSI_SLEEPING" : "-",
279         (dsi->flags & DSI_EXTSLEEP) ?     "DSI_EXTSLEEP" : "-",
280         (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
281         (dsi->flags & DSI_DIE) ?          "DSI_DIE" : "-",
282         (dsi->flags & DSI_NOREPLY) ?      "DSI_NOREPLY" : "-",
283         (dsi->flags & DSI_RECONSOCKET) ?  "DSI_RECONSOCKET" : "-",
284         (dsi->flags & DSI_RECONINPROG) ?  "DSI_RECONINPROG" : "-");
285
286     if (dsi->flags & DSI_SLEEPING) {
287         if (dsi->tickle > AFPobj->options.sleep) {
288             LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
289             afp_dsi_die(EXITERR_CLNT);
290         }
291         return;
292     } 
293
294     if (dsi->flags & DSI_DISCONNECTED) {
295         if (geteuid() == 0) {
296             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
297             afp_dsi_die(EXITERR_CLNT);
298         }
299         if (dsi->tickle > AFPobj->options.disconnected) {
300             LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
301             afp_dsi_die(EXITERR_CLNT);
302         }
303         return;
304     }
305
306     /* if we're in the midst of processing something, don't die. */        
307     if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
308         LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
309         if (dsi_disconnect(dsi) != 0)
310             afp_dsi_die(EXITERR_CLNT);
311         return;
312     }
313
314     if ((err = pollvoltime(AFPobj)) == 0)
315         LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
316         err = dsi_tickle(AFPobj->handle);
317     if (err <= 0) {
318         if (geteuid() == 0) {
319             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
320             afp_dsi_die(EXITERR_CLNT);
321         }
322         LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
323         if (dsi_disconnect(dsi) != 0)
324             afp_dsi_die(EXITERR_CLNT);
325     }
326 }
327
328 /* ----------------- 
329    if dsi->in_write is set attention, tickle (and close?) msg
330    aren't sent. We don't care about tickle 
331 */
332 static void pending_request(DSI *dsi)
333 {
334     /* send pending attention */
335
336     /* read msg if any, it could be done in afp_getsrvrmesg */
337     if (dsi->msg_request) {
338         if (dsi->msg_request == 2) {
339             /* didn't send it in signal handler */
340             dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
341         }
342         dsi->msg_request = 0;
343         readmessage(AFPobj);
344     }
345     if (dsi->down_request) {
346         dsi->down_request = 0;
347         dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
348                   AFPATTN_MESG | AFPATTN_TIME(5));
349     }
350 }
351
352 /* -------------------------------------------
353  afp over dsi. this never returns. 
354 */
355 void afp_over_dsi(AFPObj *obj)
356 {
357     DSI *dsi = (DSI *) obj->handle;
358     int rc_idx;
359     u_int32_t err, cmd;
360     u_int8_t function;
361     struct sigaction action;
362
363     AFPobj = obj;
364     obj->exit = afp_dsi_die;
365     obj->reply = (int (*)()) dsi_cmdreply;
366     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
367     dsi->tickle = 0;
368
369     memset(&action, 0, sizeof(action));
370
371     /* install SIGHUP */
372     action.sa_handler = afp_dsi_reload;
373     sigemptyset( &action.sa_mask );
374     sigaddset(&action.sa_mask, SIGALRM);
375     sigaddset(&action.sa_mask, SIGTERM);
376     sigaddset(&action.sa_mask, SIGUSR1);
377     sigaddset(&action.sa_mask, SIGINT);
378     sigaddset(&action.sa_mask, SIGUSR2);
379     action.sa_flags = SA_RESTART;
380     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
381         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
382         afp_dsi_die(EXITERR_SYS);
383     }
384
385     /* install SIGURG */
386     action.sa_handler = afp_dsi_transfer_session;
387     sigemptyset( &action.sa_mask );
388     sigaddset(&action.sa_mask, SIGALRM);
389     sigaddset(&action.sa_mask, SIGTERM);
390     sigaddset(&action.sa_mask, SIGUSR1);
391     sigaddset(&action.sa_mask, SIGINT);
392     sigaddset(&action.sa_mask, SIGUSR2);
393     action.sa_flags = SA_RESTART;
394     if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
395         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
396         afp_dsi_die(EXITERR_SYS);
397     }
398
399     /* install SIGTERM */
400     action.sa_handler = afp_dsi_die;
401     sigemptyset( &action.sa_mask );
402     sigaddset(&action.sa_mask, SIGALRM);
403     sigaddset(&action.sa_mask, SIGHUP);
404     sigaddset(&action.sa_mask, SIGUSR1);
405     sigaddset(&action.sa_mask, SIGINT);
406     sigaddset(&action.sa_mask, SIGUSR2);
407     action.sa_flags = SA_RESTART;
408     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
409         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
410         afp_dsi_die(EXITERR_SYS);
411     }
412
413     /* Added for server message support */
414     action.sa_handler = afp_dsi_getmesg;
415     sigemptyset( &action.sa_mask );
416     sigaddset(&action.sa_mask, SIGALRM);
417     sigaddset(&action.sa_mask, SIGTERM);
418     sigaddset(&action.sa_mask, SIGUSR1);
419     sigaddset(&action.sa_mask, SIGHUP);
420     sigaddset(&action.sa_mask, SIGINT);
421     action.sa_flags = SA_RESTART;
422     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
423         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
424         afp_dsi_die(EXITERR_SYS);
425     }
426
427     /*  SIGUSR1 - set down in 5 minutes  */
428     action.sa_handler = afp_dsi_timedown;
429     sigemptyset( &action.sa_mask );
430     sigaddset(&action.sa_mask, SIGALRM);
431     sigaddset(&action.sa_mask, SIGHUP);
432     sigaddset(&action.sa_mask, SIGTERM);
433     sigaddset(&action.sa_mask, SIGINT);
434     sigaddset(&action.sa_mask, SIGUSR2);
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     sigfillset( &action.sa_mask );
444     action.sa_flags = SA_RESTART;
445     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
446         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
447         afp_dsi_die(EXITERR_SYS);
448     }
449
450 #ifndef DEBUGGING
451     /* tickle handler */
452     action.sa_handler = alarm_handler;
453     sigemptyset(&action.sa_mask);
454     sigaddset(&action.sa_mask, SIGHUP);
455     sigaddset(&action.sa_mask, SIGTERM);
456     sigaddset(&action.sa_mask, SIGUSR1);
457     sigaddset(&action.sa_mask, SIGINT);
458     sigaddset(&action.sa_mask, SIGUSR2);
459     action.sa_flags = SA_RESTART;
460     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
461             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
462         afp_dsi_die(EXITERR_SYS);
463     }
464 #endif /* DEBUGGING */
465
466     if (dircache_init(obj->options.dircachesize) != 0)
467         afp_dsi_die(EXITERR_SYS);
468
469     /* set TCP snd/rcv buf */
470     if (obj->options.tcp_rcvbuf) {
471         if (setsockopt(dsi->socket,
472                        SOL_SOCKET,
473                        SO_RCVBUF,
474                        &obj->options.tcp_rcvbuf,
475                        sizeof(obj->options.tcp_rcvbuf)) != 0) {
476             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
477         }
478     }
479     if (obj->options.tcp_sndbuf) {
480         if (setsockopt(dsi->socket,
481                        SOL_SOCKET,
482                        SO_SNDBUF,
483                        &obj->options.tcp_sndbuf,
484                        sizeof(obj->options.tcp_sndbuf)) != 0) {
485             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
486         }
487     }
488
489     /* get stuck here until the end */
490     while (1) {
491         if (sigsetjmp(recon_jmp, 1) != 0)
492             /* returning from SIGALARM handler for a primary reconnect */
493             continue;
494
495         /* Blocking read on the network socket */
496         cmd = dsi_receive(dsi);
497
498         if (cmd == 0) {
499             /* cmd == 0 is the error condition */
500             if (dsi->flags & DSI_RECONSOCKET) {
501                 /* we just got a reconnect so we immediately try again to receive on the new fd */
502                 dsi->flags &= ~DSI_RECONSOCKET;
503                 continue;
504             }
505             /* Some error on the client connection, enter disconnected state */
506             if (dsi_disconnect(dsi) != 0)
507                 afp_dsi_die(EXITERR_CLNT);
508
509             /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
510             if (dsi->flags & DSI_AFP_LOGGED_OUT) {
511                 afp_dsi_close(obj);
512                 exit(0);
513             }
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 }