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