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