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