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