]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Merge branch 'develop'
[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 #include <atalk/globals.h>
41 #include <atalk/netatalk_conf.h>
42
43 #include "switch.h"
44 #include "auth.h"
45 #include "fork.h"
46 #include "dircache.h"
47 #include "spotlight.h"
48
49 #ifndef SOL_TCP
50 #define SOL_TCP IPPROTO_TCP
51 #endif
52
53 /* 
54  * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
55  * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
56  * but are deeply nested in the function chain with the caller already without acces to it.
57  * Changing this would require adding a reference to the caller which itself might be
58  * called in many places (eg acltoownermode is called from accessmode).
59  * The only sane way out is providing a copy of it here:
60  */
61 AFPObj *AFPobj = NULL;
62
63 typedef struct {
64     uint16_t DSIreqID;
65     uint8_t  AFPcommand;
66     uint32_t result;
67 } rc_elem_t;
68
69 /*
70  * AFP replay cache:
71  * - fix sized array
72  * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
73  */
74 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
75
76 static sigjmp_buf recon_jmp;
77 static void afp_dsi_close(AFPObj *obj)
78 {
79     DSI *dsi = obj->dsi;
80     sigset_t sigs;
81     
82     close(obj->ipc_fd);
83     obj->ipc_fd = -1;
84
85     /* we may have been called from a signal handler caught when afpd was running
86      * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
87      * restore our login user
88      */
89     if (geteuid() != obj->uid) {
90         if (seteuid( obj->uid ) < 0) {
91             LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
92                 obj->uid, strerror(errno), getuid(), geteuid());
93             exit(EXITERR_SYS);
94         }
95     }
96
97     close_all_vol(obj);
98     if (obj->logout) {
99         /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
100         sigfillset(&sigs);
101         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
102         (*obj->logout)();
103     }
104
105     LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
106         dsi->read_count/1024.0, dsi->write_count/1024.0);
107     log_dircache_stat();
108
109     dsi_close(dsi);
110 }
111
112 /* -------------------------------
113  * SIGTERM
114  * a little bit of code duplication. 
115  */
116 static void afp_dsi_die(int sig)
117 {
118     DSI *dsi = (DSI *)AFPobj->dsi;
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     if (dsi->flags & DSI_DISCONNECTED) {
127         LOG(log_note, logtype_afpd, "Disconnected session terminating");
128         exit(0);
129     }
130
131     dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN);
132     afp_dsi_close(AFPobj);
133    if (sig) /* if no signal, assume dieing because logins are disabled &
134                 don't log it (maintenance mode)*/
135         LOG(log_info, logtype_afpd, "Connection terminated");
136     if (sig == SIGTERM || sig == SIGALRM) {
137         exit( 0 );
138     }
139     else {
140         exit(sig);
141     }
142 }
143
144 /* SIGQUIT handler */
145 static void ipc_reconnect_handler(int sig _U_)
146 {
147     DSI *dsi = (DSI *)AFPobj->dsi;
148
149     if (reconnect_ipc(AFPobj) != 0) {
150         LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
151         afp_dsi_close(AFPobj);
152         exit(EXITERR_SYS);        
153     }
154
155     if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
156         LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
157         afp_dsi_close(AFPobj);
158         exit(EXITERR_SYS);        
159     }
160     LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
161 }
162
163 /* SIGURG handler (primary reconnect) */
164 static void afp_dsi_transfer_session(int sig _U_)
165 {
166     uint16_t dsiID;
167     int socket;
168     DSI *dsi = (DSI *)AFPobj->dsi;
169
170     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
171
172     if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
173         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
174         afp_dsi_close(AFPobj);
175         exit(EXITERR_SYS);
176     }
177
178     if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
179         LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
180         afp_dsi_close(AFPobj);
181         exit(EXITERR_SYS);
182     }
183
184     LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
185
186     dsi->proto_close(dsi);
187     dsi->socket = socket;
188     dsi->flags = DSI_RECONSOCKET;
189     dsi->datalen = 0;
190     dsi->eof = dsi->start = dsi->buffer;
191     dsi->in_write = 0;
192     dsi->header.dsi_requestID = dsiID;
193     dsi->header.dsi_command = DSIFUNC_CMD;
194
195     /*
196      * The session transfer happens in the middle of FPDisconnect old session, thus we
197      * have to send the reply now.
198      */
199     if (!dsi_cmdreply(dsi, AFP_OK)) {
200         LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
201         afp_dsi_close(AFPobj);
202         exit(EXITERR_CLNT);
203     }
204
205     LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
206     /* 
207      * Now returning from this signal handler return to dsi_receive which should start
208      * reading/continuing from the connected socket that was passed via the parent from
209      * another session. The parent will terminate that session.
210      */
211     siglongjmp(recon_jmp, 1);
212 }
213
214 /* ------------------- */
215 static void afp_dsi_timedown(int sig _U_)
216 {
217     struct sigaction    sv;
218     struct itimerval    it;
219     DSI                 *dsi = (DSI *)AFPobj->dsi;
220     dsi->flags |= DSI_DIE;
221     /* shutdown and don't reconnect. server going down in 5 minutes. */
222     setmessage("The server is going down for maintenance.");
223     if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
224                   AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
225         DSI *dsi = (DSI *)AFPobj->dsi;
226         dsi->down_request = 1;
227     }                  
228
229     it.it_interval.tv_sec = 0;
230     it.it_interval.tv_usec = 0;
231     it.it_value.tv_sec = 300;
232     it.it_value.tv_usec = 0;
233
234     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
235         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
236         afp_dsi_die(EXITERR_SYS);
237     }
238     memset(&sv, 0, sizeof(sv));
239     sv.sa_handler = afp_dsi_die;
240     sigemptyset( &sv.sa_mask );
241     sigaddset(&sv.sa_mask, SIGHUP);
242     sigaddset(&sv.sa_mask, SIGTERM);
243     sv.sa_flags = SA_RESTART;
244     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
245         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
246         afp_dsi_die(EXITERR_SYS);
247     }
248
249     /* ignore myself */
250     sv.sa_handler = SIG_IGN;
251     sigemptyset( &sv.sa_mask );
252     sv.sa_flags = SA_RESTART;
253     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
254         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
255         afp_dsi_die(EXITERR_SYS);
256     }
257 }
258
259 /* ---------------------------------
260  * SIGHUP reload configuration file
261  */
262 volatile int reload_request = 0;
263
264 static void afp_dsi_reload(int sig _U_)
265 {
266     reload_request = 1;
267 }
268
269 /* ---------------------------------
270  * SIGINT: enable max_debug LOGging
271  */
272 static volatile sig_atomic_t debug_request = 0;
273
274 static void afp_dsi_debug(int sig _U_)
275 {
276     debug_request = 1;
277 }
278
279 /* ---------------------- */
280 static void afp_dsi_getmesg (int sig _U_)
281 {
282     DSI *dsi = (DSI *)AFPobj->dsi;
283
284     dsi->msg_request = 1;
285     if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
286         dsi->msg_request = 2;
287 }
288
289 static void alarm_handler(int sig _U_)
290 {
291     int err;
292     DSI *dsi = (DSI *)AFPobj->dsi;
293
294     /* we have to restart the timer because some libraries may use alarm() */
295     setitimer(ITIMER_REAL, &dsi->timer, NULL);
296
297     /* we got some traffic from the client since the previous timer tick. */
298     if ((dsi->flags & DSI_DATA)) {
299         dsi->flags &= ~DSI_DATA;
300         return;
301     }
302
303     dsi->tickle++;
304     LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
305         dsi->tickle,
306         (dsi->flags & DSI_DATA) ?         "DSI_DATA" : "-",
307         (dsi->flags & DSI_RUNNING) ?      "DSI_RUNNING" : "-",
308         (dsi->flags & DSI_SLEEPING) ?     "DSI_SLEEPING" : "-",
309         (dsi->flags & DSI_EXTSLEEP) ?     "DSI_EXTSLEEP" : "-",
310         (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
311         (dsi->flags & DSI_DIE) ?          "DSI_DIE" : "-",
312         (dsi->flags & DSI_NOREPLY) ?      "DSI_NOREPLY" : "-",
313         (dsi->flags & DSI_RECONSOCKET) ?  "DSI_RECONSOCKET" : "-",
314         (dsi->flags & DSI_RECONINPROG) ?  "DSI_RECONINPROG" : "-");
315
316     if (dsi->flags & DSI_SLEEPING) {
317         if (dsi->tickle > AFPobj->options.sleep) {
318             LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
319             afp_dsi_die(EXITERR_CLNT);
320         }
321         return;
322     } 
323
324     if (dsi->flags & DSI_DISCONNECTED) {
325         if (geteuid() == 0) {
326             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
327             afp_dsi_die(EXITERR_CLNT);
328         }
329         if (dsi->tickle > AFPobj->options.disconnected) {
330             LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
331             afp_dsi_die(EXITERR_CLNT);
332         }
333         return;
334     }
335
336     /* if we're in the midst of processing something, don't die. */        
337     if (dsi->tickle >= AFPobj->options.timeout) {
338         LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
339         if (dsi_disconnect(dsi) != 0)
340             afp_dsi_die(EXITERR_CLNT);
341         return;
342     }
343
344     if ((err = pollvoltime(AFPobj)) == 0)
345         LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
346         err = dsi_tickle(AFPobj->dsi);
347     if (err <= 0) {
348         if (geteuid() == 0) {
349             LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
350             afp_dsi_die(EXITERR_CLNT);
351         }
352         LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
353         if (dsi_disconnect(dsi) != 0)
354             afp_dsi_die(EXITERR_CLNT);
355     }
356 }
357
358 /* ----------------- 
359    if dsi->in_write is set attention, tickle (and close?) msg
360    aren't sent. We don't care about tickle 
361 */
362 static void pending_request(DSI *dsi)
363 {
364     /* send pending attention */
365
366     /* read msg if any, it could be done in afp_getsrvrmesg */
367     if (dsi->msg_request) {
368         if (dsi->msg_request == 2) {
369             /* didn't send it in signal handler */
370             dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
371         }
372         dsi->msg_request = 0;
373         readmessage(AFPobj);
374     }
375     if (dsi->down_request) {
376         dsi->down_request = 0;
377         dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
378                   AFPATTN_MESG | AFPATTN_TIME(5));
379     }
380 }
381
382 void afp_over_dsi_sighandlers(AFPObj *obj)
383 {
384     DSI *dsi = (DSI *) obj->dsi;
385     struct sigaction action;
386
387     memset(&action, 0, sizeof(action));
388     sigfillset(&action.sa_mask);
389     action.sa_flags = SA_RESTART;
390
391     /* install SIGHUP */
392     action.sa_handler = afp_dsi_reload;
393     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
394         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
395         afp_dsi_die(EXITERR_SYS);
396     }
397
398     /* install SIGURG */
399     action.sa_handler = afp_dsi_transfer_session;
400     if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
401         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
402         afp_dsi_die(EXITERR_SYS);
403     }
404
405     /* install SIGTERM */
406     action.sa_handler = afp_dsi_die;
407     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
408         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
409         afp_dsi_die(EXITERR_SYS);
410     }
411
412     /* install SIGQUIT */
413     action.sa_handler = ipc_reconnect_handler;
414     if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
415         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
416         afp_dsi_die(EXITERR_SYS);
417     }
418
419     /* SIGUSR2 - server message support */
420     action.sa_handler = afp_dsi_getmesg;
421     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
422         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
423         afp_dsi_die(EXITERR_SYS);
424     }
425
426     /*  SIGUSR1 - set down in 5 minutes  */
427     action.sa_handler = afp_dsi_timedown;
428     action.sa_flags = SA_RESTART;
429     if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
430         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
431         afp_dsi_die(EXITERR_SYS);
432     }
433
434     /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
435     action.sa_handler = afp_dsi_debug;
436     if ( sigaction( SIGINT, &action, NULL) < 0 ) {
437         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
438         afp_dsi_die(EXITERR_SYS);
439     }
440
441 #ifndef DEBUGGING
442     /* SIGALRM - tickle handler */
443     action.sa_handler = alarm_handler;
444     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
445             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
446         afp_dsi_die(EXITERR_SYS);
447     }
448 #endif /* DEBUGGING */
449 }
450
451 /* -------------------------------------------
452  afp over dsi. this never returns. 
453 */
454 void afp_over_dsi(AFPObj *obj)
455 {
456     DSI *dsi = (DSI *) obj->dsi;
457     int rc_idx;
458     uint32_t err, cmd;
459     uint8_t function;
460
461     AFPobj = obj;
462     obj->exit = afp_dsi_die;
463     obj->reply = (int (*)()) dsi_cmdreply;
464     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
465     dsi->tickle = 0;
466
467     afp_over_dsi_sighandlers(obj);
468
469     if (dircache_init(obj->options.dircachesize) != 0)
470         afp_dsi_die(EXITERR_SYS);
471
472     /* set TCP snd/rcv buf */
473     if (obj->options.tcp_rcvbuf) {
474         if (setsockopt(dsi->socket,
475                        SOL_SOCKET,
476                        SO_RCVBUF,
477                        &obj->options.tcp_rcvbuf,
478                        sizeof(obj->options.tcp_rcvbuf)) != 0) {
479             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
480         }
481     }
482     if (obj->options.tcp_sndbuf) {
483         if (setsockopt(dsi->socket,
484                        SOL_SOCKET,
485                        SO_SNDBUF,
486                        &obj->options.tcp_sndbuf,
487                        sizeof(obj->options.tcp_sndbuf)) != 0) {
488             LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
489         }
490     }
491
492     /* set TCP_NODELAY */
493     int flag = 1;
494     setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
495
496     /* Initialize Spotlight */
497     if (obj->options.flags & OPTION_SPOTLIGHT)
498         sl_mod_load(_PATH_AFPDUAMPATH "mod_spotlight.so");
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, obj->options.logfile);
571                 else
572                     setuplog("default:note", NULL);
573                 debugging = 0;
574             } else {
575                 char logstr[50];
576                 debugging = 1;
577                 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
578                 setuplog("default:maxdebug", 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                                                   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                     dsi->flags &= ~DSI_RUNNING;
643
644                     /* Add result to the AFP replay cache */
645                     replaycache[rc_idx].DSIreqID = dsi->clientID;
646                     replaycache[rc_idx].AFPcommand = function;
647                     replaycache[rc_idx].result = err;
648                 } else {
649                     LOG(log_error, logtype_afpd, "bad function %X", function);
650                     dsi->datalen = 0;
651                     err = AFPERR_NOOP;
652                 }
653             }
654
655             /* single shot toggle that gets set by dsi_readinit. */
656             if (dsi->flags & DSI_NOREPLY) {
657                 dsi->flags &= ~DSI_NOREPLY;
658                 break;
659             } else if (!dsi_cmdreply(dsi, err)) {
660                 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
661                 if (dsi_disconnect(dsi) != 0)
662                     afp_dsi_die(EXITERR_CLNT);
663             }
664             break;
665
666         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
667             function = (u_char) dsi->commands[0];
668             if ( afp_switch[ function ] != NULL ) {
669                 dsi->datalen = DSI_DATASIZ;
670                 dsi->flags |= DSI_RUNNING;
671
672                 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
673
674                 err = (*afp_switch[function])(obj,
675                                               dsi->commands, dsi->cmdlen,
676                                               (char *)&dsi->data, &dsi->datalen);
677
678                 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
679                     AfpNum2name(function), AfpErr2name(err));
680
681                 dsi->flags &= ~DSI_RUNNING;
682             } else {
683                 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
684                 dsi->datalen = 0;
685                 err = AFPERR_NOOP;
686             }
687
688             if (!dsi_wrtreply(dsi, err)) {
689                 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
690                 if (dsi_disconnect(dsi) != 0)
691                     afp_dsi_die(EXITERR_CLNT);
692             }
693             break;
694
695         case DSIFUNC_ATTN: /* attention replies */
696             break;
697
698             /* error. this usually implies a mismatch of some kind
699              * between server and client. if things are correct,
700              * we need to flush the rest of the packet if necessary. */
701         default:
702             LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
703             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
704             dsi_writeflush(dsi);
705             break;
706         }
707         pending_request(dsi);
708
709         fce_pending_events(obj);
710     }
711
712     /* error */
713     afp_dsi_die(EXITERR_CLNT);
714 }