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