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