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