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