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