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