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