]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
Warning fixes.
[netatalk.git] / etc / afpd / directory.c
1 /*
2  * $Id: directory.c,v 1.21 2001-12-10 20:16:54 srittau Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  *
7  * 19 jan 2000 implemented red-black trees for directory lookups
8  * (asun@cobalt.com).
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <sys/syslog.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <errno.h>
19 #include <sys/time.h>
20 #include <sys/param.h>
21 #include <netatalk/endian.h>
22 #include <atalk/adouble.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #ifdef CNID_DB
26 #include <atalk/cnid.h>
27 #endif /* CNID_DB */
28 #include <utime.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif /* HAVE_FCNTL_H */
35 #include <grp.h>
36 #include <pwd.h>
37
38 /* STDC check */
39 #if STDC_HEADERS
40 #include <string.h>
41 #else /* STDC_HEADERS */
42 #ifndef HAVE_STRCHR
43 #define strchr index
44 #define strrchr index
45 #endif /* HAVE_STRCHR */
46 char *strchr (), *strrchr ();
47 #ifndef HAVE_MEMCPY
48 #define memcpy(d,s,n) bcopy ((s), (d), (n))
49 #define memmove(d,s,n) bcopy ((s), (d), (n))
50 #endif /* ! HAVE_MEMCPY */
51 #endif /* STDC_HEADERS */
52
53 #include "directory.h"
54 #include "desktop.h"
55 #include "volume.h"
56 #include "fork.h"
57 #include "file.h"
58 #include "filedir.h"
59 #include "globals.h"
60 #include "unix.h"
61
62 #ifdef FORCE_UIDGID
63 #include "uid.h"
64 #endif /* FORCE_UIDGID */
65
66 struct dir      *curdir;
67
68 #define SENTINEL (&sentinel)
69 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
70                                  NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
71 static struct dir       rootpar = { SENTINEL, SENTINEL, NULL, 0,
72                                 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
73
74 /* (from IM: Toolbox Essentials)
75  * dirFinderInfo (DInfo) fields:
76  * field        bytes
77  * frRect       8    folder's window rectangle
78  * frFlags      2    flags
79  * frLocation   4    folder's location in window
80  * frView       2    folder's view (default == closedView (256))
81  *
82  * extended dirFinderInfo (DXInfo) fields:
83  * frScroll     4    scroll position
84  * frOpenChain: 4    directory ID chain of open folders
85  * frScript:    1    script flag and code
86  * frXFlags:    1    reserved
87  * frComment:   2    comment ID
88  * frPutAway:   4    home directory ID
89  */
90
91 /*
92  * redid did assignment for directories. now we use red-black trees.
93  * how exciting.
94  */
95 struct dir *
96             dirsearch( vol, did )
97             const struct vol    *vol;
98 u_int32_t       did;
99 {
100     struct dir  *dir;
101
102
103     /* check for 0 did */
104     if (!did)
105         return NULL;
106
107     if ( did == DIRDID_ROOT_PARENT ) {
108         if (!rootpar.d_did)
109             rootpar.d_did = DIRDID_ROOT_PARENT;
110         rootpar.d_child = vol->v_dir;
111         return( &rootpar );
112     }
113
114     dir = vol->v_root;
115     while ( dir != SENTINEL ) {
116         if (dir->d_did == did)
117             return dir->d_name ? dir : NULL;
118         dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
119     }
120     return NULL;
121 }
122
123
124 /* rotate the tree to the left */
125 static void dir_leftrotate(vol, dir)
126 struct vol *vol;
127 struct dir *dir;
128 {
129     struct dir *right = dir->d_right;
130
131     /* whee. move the right's left tree into dir's right tree */
132     dir->d_right = right->d_left;
133     if (right->d_left != SENTINEL)
134         right->d_left->d_back = dir;
135
136     if (right != SENTINEL) {
137         right->d_back = dir->d_back;
138         right->d_left = dir;
139     }
140
141     if (!dir->d_back) /* no parent. move the right tree to the top. */
142         vol->v_root = right;
143     else if (dir == dir->d_back->d_left) /* we were on the left */
144         dir->d_back->d_left = right;
145     else
146         dir->d_back->d_right = right; /* we were on the right */
147
148     /* re-insert dir on the left tree */
149     if (dir != SENTINEL)
150         dir->d_back = right;
151 }
152
153
154
155 /* rotate the tree to the right */
156 static void dir_rightrotate(vol, dir)
157 struct vol *vol;
158 struct dir *dir;
159 {
160     struct dir *left = dir->d_left;
161
162     /* whee. move the left's right tree into dir's left tree */
163     dir->d_left = left->d_right;
164     if (left->d_right != SENTINEL)
165         left->d_right->d_back = dir;
166
167     if (left != SENTINEL) {
168         left->d_back = dir->d_back;
169         left->d_right = dir;
170     }
171
172     if (!dir->d_back) /* no parent. move the left tree to the top. */
173         vol->v_root = left;
174     else if (dir == dir->d_back->d_right) /* we were on the right */
175         dir->d_back->d_right = left;
176     else
177         dir->d_back->d_left = left; /* we were on the left */
178
179     /* re-insert dir on the right tree */
180     if (dir != SENTINEL)
181         dir->d_back = left;
182 }
183
184 #if 0
185 /* recolor after a removal */
186 static struct dir *dir_rmrecolor(vol, dir)
187             struct vol *vol;
188 struct dir *dir;
189 {
190     struct dir *leaf;
191
192     while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
193         /* are we on the left tree? */
194         if (dir == dir->d_back->d_left) {
195             leaf = dir->d_back->d_right; /* get right side */
196             if (leaf->d_color == DIRTREE_COLOR_RED) {
197                 /* we're red. we need to change to black. */
198                 leaf->d_color = DIRTREE_COLOR_BLACK;
199                 dir->d_back->d_color = DIRTREE_COLOR_RED;
200                 dir_leftrotate(vol, dir->d_back);
201                 leaf = dir->d_back->d_right;
202             }
203
204             /* right leaf has black end nodes */
205             if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
206                     (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
207                 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
208                 dir = dir->d_back; /* ascend */
209             } else {
210                 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
211                     leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
212                     leaf->d_color = DIRTREE_COLOR_RED;
213                     dir_rightrotate(vol, leaf);
214                     leaf = dir->d_back->d_right;
215                 }
216                 leaf->d_color = dir->d_back->d_color;
217                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
218                 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
219                 dir_leftrotate(vol, dir->d_back);
220                 dir = vol->v_root;
221             }
222         } else { /* right tree */
223             leaf = dir->d_back->d_left; /* left tree */
224             if (leaf->d_color == DIRTREE_COLOR_RED) {
225                 leaf->d_color = DIRTREE_COLOR_BLACK;
226                 dir->d_back->d_color = DIRTREE_COLOR_RED;
227                 dir_rightrotate(vol, dir->d_back);
228                 leaf = dir->d_back->d_left;
229             }
230
231             /* left leaf has black end nodes */
232             if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
233                     (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
234                 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
235                 dir = dir->d_back; /* ascend */
236             } else {
237                 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
238                     leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
239                     leaf->d_color = DIRTREE_COLOR_RED;
240                     dir_leftrotate(vol, leaf);
241                     leaf = dir->d_back->d_left;
242                 }
243                 leaf->d_color = dir->d_back->d_color;
244                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
245                 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
246                 dir_rightrotate(vol, dir->d_back);
247                 dir = vol->v_root;
248             }
249         }
250     }
251     dir->d_color = DIRTREE_COLOR_BLACK;
252
253     return dir;
254 }
255 #endif /* 0 */
256
257
258 /* remove the node from the tree. this is just like insertion, but
259  * different. actually, it has to worry about a bunch of things that
260  * insertion doesn't care about. */
261 static void dir_remove( vol, dir )
262 struct vol      *vol;
263 struct dir      *dir;
264 {
265 #ifdef REMOVE_NODES
266     struct ofork *of, *last;
267     struct dir *node, *leaf;
268 #endif /* REMOVE_NODES */
269
270     if (!dir || (dir == SENTINEL))
271         return;
272
273     /* i'm not sure if it really helps to delete stuff. */
274 #ifndef REMOVE_NODES 
275     free(dir->d_name);
276     dir->d_name = NULL;
277 #else /* ! REMOVE_NODES */
278
279     /* go searching for a node with at most one child */
280     if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
281         node = dir;
282     } else {
283         node = dir->d_right;
284         while (node->d_left != SENTINEL)
285             node = node->d_left;
286     }
287
288     /* get that child */
289     leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
290
291     /* detach node */
292     leaf->d_back = node->d_back;
293     if (!node->d_back) {
294         vol->v_root = leaf;
295     } else if (node == node->d_back->d_left) { /* left tree */
296         node->d_back->d_left = leaf;
297     } else {
298         node->d_back->d_right = leaf;
299     }
300
301     /* we want to free node, but we also want to free the data in dir.
302     * currently, that's d_name and the directory traversal bits.
303     * we just copy the necessary bits and then fix up all the
304     * various pointers to the directory. needless to say, there are
305     * a bunch of places that store the directory struct. */
306     if (node != dir) {
307         struct dir save, *tmp;
308
309         memcpy(&save, dir, sizeof(save));
310         memcpy(dir, node, sizeof(struct dir));
311
312         /* restore the red-black bits */
313         dir->d_left = save.d_left;
314         dir->d_right = save.d_right;
315         dir->d_back = save.d_back;
316         dir->d_color = save.d_color;
317
318         if (node == vol->v_dir) {/* we may need to fix up this pointer */
319             vol->v_dir = dir;
320             rootpar.d_child = vol->v_dir;
321         } else {
322             /* if we aren't the root directory, we have parents and
323             * siblings to worry about */
324             if (dir->d_parent->d_child == node)
325                 dir->d_parent->d_child = dir;
326             dir->d_next->d_prev = dir;
327             dir->d_prev->d_next = dir;
328         }
329
330         /* fix up children. */
331         tmp = dir->d_child;
332         while (tmp) {
333             tmp->d_parent = dir;
334             tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
335         }
336
337         if (node == curdir) /* another pointer to fixup */
338             curdir = dir;
339
340         /* we also need to fix up oforks. bleah */
341         if ((of = dir->d_ofork)) {
342             last = of->of_d_prev;
343             while (of) {
344                 of->of_dir = dir;
345                 of = (last == of) ? NULL : of->of_d_next;
346             }
347         }
348
349         /* set the node's d_name */
350         node->d_name = save.d_name;
351     }
352
353     if (node->d_color == DIRTREE_COLOR_BLACK)
354         dir_rmrecolor(vol, leaf);
355     free(node->d_name);
356     free(node);
357 #endif /* ! REMOVE_NODES */
358 }
359
360
361 static struct dir *dir_insert(vol, dir)
362             const struct vol *vol;
363 struct dir *dir;
364 {
365     struct dir  *pdir;
366
367     pdir = vol->v_root;
368     while (pdir->d_did != dir->d_did ) {
369         if ( pdir->d_did > dir->d_did ) {
370             if ( pdir->d_left == SENTINEL ) {
371                 pdir->d_left = dir;
372                 dir->d_back = pdir;
373                 return NULL;
374             }
375             pdir = pdir->d_left;
376         } else {
377             if ( pdir->d_right == SENTINEL ) {
378                 pdir->d_right = dir;
379                 dir->d_back = pdir;
380                 return NULL;
381             }
382             pdir = pdir->d_right;
383         }
384     }
385     return pdir;
386 }
387
388
389 /*
390  * attempt to extend the current dir. tree to include path
391  * as a side-effect, movecwd to that point and return the new dir
392  */
393
394 static struct dir *
395             extenddir( vol, dir, path )
396             struct vol  *vol;
397 struct dir      *dir;
398 char    *path;
399 {
400     char        *p;
401     struct stat st;
402
403     p = mtoupath(vol, path );
404     if ( stat( p, &st ) != 0 ) {
405         return( NULL );
406     }
407     if (!S_ISDIR(st.st_mode)) {
408         return( NULL );
409     }
410
411     if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
412                         &st)) == NULL ) {
413         return( NULL );
414     }
415
416     if ( movecwd( vol, dir ) < 0 ) {
417         return( NULL );
418     }
419
420     return( dir );
421 }
422
423 static int deletedir(char *dir)
424 {
425     char path[MAXPATHLEN + 1];
426     DIR *dp;
427     struct dirent       *de;
428     struct stat st;
429     int len, err;
430
431     if ((len = strlen(dir)) > sizeof(path))
432         return AFPERR_PARAM;
433
434     /* already gone */
435     if ((dp = opendir(dir)) == NULL)
436         return AFP_OK;
437
438     strcpy(path, dir);
439     strcat(path, "/");
440     len++;
441     while ((de = readdir(dp))) {
442         /* skip this and previous directory */
443         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
444             continue;
445
446         strncpy(path + len, de->d_name, sizeof(path) - len);
447         if (stat(path, &st) == 0) {
448             if (S_ISDIR(st.st_mode)) {
449                 if ((err = deletedir(path)) < 0) {
450                     closedir(dp);
451                     return err;
452                 }
453             } else if (unlink(path) < 0) {
454                 switch (errno) {
455                 case ENOENT :
456                     continue; /* somebody went and deleted it behind our backs. */
457                 case EROFS:
458                     err = AFPERR_VLOCK;
459                 case EPERM:
460                 case EACCES :
461                     err = AFPERR_ACCESS;
462                 default :
463                     err = AFPERR_PARAM;
464                 }
465                 closedir(dp);
466                 return err;
467             }
468         }
469     }
470     closedir(dp);
471
472     /* okay. the directory is empty. delete it. note: we already got rid
473        of .AppleDouble.  */
474     if (rmdir(dir) < 0) {
475         switch ( errno ) {
476         case ENOENT :
477             break;
478         case ENOTEMPTY : /* should never happen */
479             return( AFPERR_DIRNEMPT );
480         case EPERM:
481         case EACCES :
482             return( AFPERR_ACCESS );
483         case EROFS:
484             return AFPERR_VLOCK;
485         default :
486             return( AFPERR_PARAM );
487         }
488     }
489     return AFP_OK;
490 }
491
492 /* do a recursive copy. */
493 static int copydir(char *src, char *dst, int noadouble)
494 {
495     char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
496     DIR *dp;
497     struct dirent       *de;
498     struct stat st;
499     struct utimbuf      ut;
500     int slen, dlen, err;
501
502     /* doesn't exist or the path is too long. */
503     if (((slen = strlen(src)) > sizeof(spath) - 2) ||
504             ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
505             ((dp = opendir(src)) == NULL))
506         return AFPERR_PARAM;
507
508     /* try to create the destination directory */
509     if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
510         closedir(dp);
511         switch ( errno ) {
512         case ENOENT :
513             return( AFPERR_NOOBJ );
514         case EROFS :
515             return( AFPERR_VLOCK );
516         case EPERM:
517         case EACCES :
518             return( AFPERR_ACCESS );
519         case EEXIST :
520             return( AFPERR_EXIST );
521         case ENOSPC :
522         case EDQUOT :
523             return( AFPERR_DFULL );
524         default :
525             return( AFPERR_PARAM );
526         }
527     }
528
529     /* set things up to copy */
530     strcpy(spath, src);
531     strcat(spath, "/");
532     slen++;
533     strcpy(dpath, dst);
534     strcat(dpath, "/");
535     dlen++;
536     err = AFP_OK;
537     while ((de = readdir(dp))) {
538         /* skip this and previous directory */
539         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
540             continue;
541
542         strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
543         if (stat(spath, &st) == 0) {
544             strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
545
546             if (S_ISDIR(st.st_mode)) {
547                 if ((err = copydir(spath, dpath, noadouble)) < 0)
548                     goto copydir_done;
549             } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
550                 goto copydir_done;
551
552             } else {
553                 /* keep the same time stamp. */
554                 ut.actime = ut.modtime = st.st_mtime;
555                 utime(dpath, &ut);
556             }
557         }
558     }
559
560     /* keep the same time stamp. */
561     if (stat(src, &st) == 0) {
562         ut.actime = ut.modtime = st.st_mtime;
563         utime(dst, &ut);
564     }
565
566 copydir_done:
567     closedir(dp);
568     return err;
569 }
570
571
572 /* --- public functions follow --- */
573
574 /* NOTE: we start off with at least one node (the root directory). */
575 struct dir *dirinsert( vol, dir )
576             struct vol  *vol;
577 struct dir      *dir;
578 {
579     struct dir *node;
580
581     if ((node = dir_insert(vol, dir)))
582         return node;
583
584     /* recolor the tree. the current node is red. */
585     dir->d_color = DIRTREE_COLOR_RED;
586
587     /* parent of this node has to be black. if the parent node
588     * is red, then we have a grandparent. */
589     while ((dir != vol->v_root) &&
590             (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
591         /* are we on the left tree? */
592         if (dir->d_back == dir->d_back->d_back->d_left) {
593             node = dir->d_back->d_back->d_right;  /* get the right node */
594             if (node->d_color == DIRTREE_COLOR_RED) {
595                 /* we're red. we need to change to black. */
596                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
597                 node->d_color = DIRTREE_COLOR_BLACK;
598                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
599                 dir = dir->d_back->d_back; /* finished. go up. */
600             } else {
601                 if (dir == dir->d_back->d_right) {
602                     dir = dir->d_back;
603                     dir_leftrotate(vol, dir);
604                 }
605                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
606                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
607                 dir_rightrotate(vol, dir->d_back->d_back);
608             }
609         } else {
610             node = dir->d_back->d_back->d_left;
611             if (node->d_color == DIRTREE_COLOR_RED) {
612                 /* we're red. we need to change to black. */
613                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
614                 node->d_color = DIRTREE_COLOR_BLACK;
615                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
616                 dir = dir->d_back->d_back; /* finished. ascend */
617             } else {
618                 if (dir == dir->d_back->d_left) {
619                     dir = dir->d_back;
620                     dir_rightrotate(vol, dir);
621                 }
622                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
623                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
624                 dir_leftrotate(vol, dir->d_back->d_back);
625             }
626         }
627     }
628
629     vol->v_root->d_color = DIRTREE_COLOR_BLACK;
630     return NULL;
631 }
632
633 /* free everything down. we don't bother to recolor as this is only
634  * called to free the entire tree */
635 void dirfree( dir )
636 struct dir      *dir;
637 {
638     if (!dir || (dir == SENTINEL))
639         return;
640
641     if ( dir->d_left != SENTINEL ) {
642         dirfree( dir->d_left );
643     }
644     if ( dir->d_right != SENTINEL ) {
645         dirfree( dir->d_right );
646     }
647
648     if (dir != SENTINEL) {
649         free( dir->d_name );
650         free( dir );
651     }
652 }
653
654
655 struct dir *dirnew(const int len)
656 {
657     struct dir *dir;
658
659     dir = (struct dir *) calloc(1, sizeof( struct dir ));
660     if (!dir)
661         return NULL;
662
663     if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
664         free(dir);
665         return NULL;
666     }
667
668     dir->d_left = dir->d_right = SENTINEL;
669     dir->d_next = dir->d_prev = dir;
670     return dir;
671 }
672
673
674 /* XXX: this needs to be changed to handle path types */
675 char *
676 cname( vol, dir, cpath )
677 const struct vol        *vol;
678 struct dir      *dir;
679 char    **cpath;
680 {
681     struct dir          *cdir;
682     static char         path[ MAXPATHLEN + 1];
683     char                *data, *p;
684     int                 extend = 0;
685     int                 len;
686
687     data = *cpath;
688     if ( *data++ != 2 ) {                       /* path type */
689         return( NULL );
690     }
691     len = (unsigned char) *data++;
692     *cpath += len + 2;
693     *path = '\0';
694
695     for ( ;; ) {
696         if ( len == 0 ) {
697             if ( !extend && movecwd( vol, dir ) < 0 ) {
698                 return( NULL );
699             }
700             return( path );
701         }
702
703         if ( *data == '\0' ) {
704             data++;
705             len--;
706         }
707
708         while ( *data == '\0' && len > 0 ) {
709             if ( dir->d_parent == NULL ) {
710                 return( NULL );
711             }
712             dir = dir->d_parent;
713             data++;
714             len--;
715         }
716
717         /* would this be faster with strlen + strncpy? */
718         p = path;
719         while ( *data != '\0' && len > 0 ) {
720             *p++ = *data++;
721             len--;
722         }
723
724         /* short cut bits by chopping off a trailing \0. this also
725                   makes the traversal happy w/ filenames at the end of the
726                   cname. */
727         if (len == 1)
728             len--;
729
730 #ifdef notdef
731         /*
732          * Dung Nguyen <ntd@adb.fr>
733          *
734          * AFPD cannot handle paths with "::" if the "::" notation is
735          * not at the beginning of the path. The following path will not
736          * be interpreted correctly:
737          *
738          * :a:b:::c: (directory c at the same level as directory a) */
739         if ( len > 0 ) {
740             data++;
741             len--;
742         }
743 #endif /* notdef */
744         *p = '\0';
745
746         if ( p != path ) { /* we got something */
747             if ( !extend ) {
748                 cdir = dir->d_child;
749                 while (cdir) {
750                     if ( strcasecmp( cdir->d_name, path ) == 0 ) {
751                         break;
752                     }
753                     cdir = (cdir == dir->d_child->d_prev) ? NULL :
754                            cdir->d_next;
755                 }
756                 if ( cdir == NULL ) {
757                     ++extend;
758                     if ( movecwd( vol, dir ) < 0 ) {
759                         return( NULL );
760                     }
761                     cdir = extenddir( vol, dir, path );
762                 }
763
764             } else {
765                 cdir = extenddir( vol, dir, path );
766             }
767
768             if ( cdir == NULL ) {
769                 if ( len > 0 ) {
770                     return( NULL );
771                 }
772
773             } else {
774                 dir = cdir;
775                 *path = '\0';
776             }
777         }
778     }
779 }
780
781 /*
782  * Move curdir to dir, with a possible chdir()
783  */
784 int movecwd( vol, dir)
785 const struct vol        *vol;
786 struct dir      *dir;
787 {
788     char path[MAXPATHLEN + 1];
789     struct dir  *d;
790     char        *p, *u;
791     int         n;
792
793     if ( dir == curdir ) {
794         return( 0 );
795     }
796     if ( dir->d_did == DIRDID_ROOT_PARENT) {
797         return( -1 );
798     }
799
800     p = path + sizeof(path) - 1;
801     *p-- = '\0';
802     *p = '.';
803     for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
804         *--p = '/';
805         u = mtoupath(vol, d->d_name );
806         n = strlen( u );
807         p -= n;
808         strncpy( p, u, n );
809     }
810     if ( d != curdir ) {
811         *--p = '/';
812         n = strlen( vol->v_path );
813         p -= n;
814         strncpy( p, vol->v_path, n );
815     }
816     if ( chdir( p ) < 0 ) {
817         return( -1 );
818     }
819     curdir = dir;
820     return( 0 );
821 }
822
823 int getdirparams(const struct vol *vol,
824                  u_int16_t bitmap,
825                  char *upath, struct dir *dir, struct stat *st,
826                  char *buf, int *buflen )
827 {
828     struct maccess      ma;
829     struct adouble      ad;
830     char                *data, *nameoff = NULL;
831     DIR                 *dp;
832     struct dirent       *de;
833     int                 bit = 0, isad = 1;
834     u_int32_t           aint;
835     u_int16_t           ashort;
836 #ifdef FORCE_UIDGID
837     uidgidset           *uidgid;
838     memset(&uidgid, 0, sizeof(uidgid));
839 #endif /* FORCE_UIDGID */
840
841     memset(&ad, 0, sizeof(ad));
842
843 #ifdef FORCE_UIDGID
844     save_uidgid ( &uidgid );
845     set_uidgid ( vol );
846 #endif /* FORCE_UIDGID */
847
848     if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
849                   DIRBITS | 0777, &ad) < 0 ) {
850         isad = 0;
851     }
852
853     data = buf;
854     while ( bitmap != 0 ) {
855         while (( bitmap & 1 ) == 0 ) {
856             bitmap = bitmap>>1;
857             bit++;
858         }
859
860         switch ( bit ) {
861         case DIRPBIT_ATTR :
862             if ( isad ) {
863                 ad_getattr(&ad, &ashort);
864             } else if (*upath == '.' && strcmp(upath, ".") &&
865                        strcmp(upath, "..")) {
866                 ashort = htons(ATTRBIT_INVISIBLE);
867             } else
868                 ashort = 0;
869             memcpy( data, &ashort, sizeof( ashort ));
870             data += sizeof( ashort );
871             break;
872
873         case DIRPBIT_PDID :
874             if ( dir->d_did == DIRDID_ROOT) {
875                 aint = DIRDID_ROOT_PARENT;
876             } else if (dir->d_did == DIRDID_ROOT_PARENT) {
877                 aint = 0;
878             } else {
879                 aint = dir->d_parent->d_did;
880             }
881             memcpy( data, &aint, sizeof( aint ));
882             data += sizeof( aint );
883             break;
884
885         case DIRPBIT_CDATE :
886             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
887                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
888             memcpy( data, &aint, sizeof( aint ));
889             data += sizeof( aint );
890             break;
891
892         case DIRPBIT_MDATE :
893             aint = AD_DATE_FROM_UNIX(st->st_mtime);
894             memcpy( data, &aint, sizeof( aint ));
895             data += sizeof( aint );
896             break;
897
898         case DIRPBIT_BDATE :
899             if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
900                 aint = AD_DATE_START;
901             memcpy( data, &aint, sizeof( aint ));
902             data += sizeof( aint );
903             break;
904
905         case DIRPBIT_FINFO :
906             if ( isad ) {
907                 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
908             } else { /* no appledouble */
909                 memset( data, 0, 32 );
910                 /* set default view -- this also gets done in ad_open() */
911                 ashort = htons(FINDERINFO_CLOSEDVIEW);
912                 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
913
914                 /* dot files are by default invisible */
915                 if (*upath == '.' && strcmp(upath, ".") &&
916                         strcmp(upath, "..")) {
917                     ashort = htons(FINDERINFO_INVISIBLE);
918                     memcpy(data + FINDERINFO_FRFLAGOFF,
919                            &ashort, sizeof(ashort));
920                 }
921             }
922             data += 32;
923             break;
924
925         case DIRPBIT_LNAME :
926             if (dir->d_name) /* root of parent can have a null name */
927                 nameoff = data;
928             else
929                 memset(data, 0, sizeof(u_int16_t));
930             data += sizeof( u_int16_t );
931             break;
932
933         case DIRPBIT_SNAME :
934             memset(data, 0, sizeof(u_int16_t));
935             data += sizeof( u_int16_t );
936             break;
937
938         case DIRPBIT_DID :
939             memcpy( data, &dir->d_did, sizeof( aint ));
940             data += sizeof( aint );
941             break;
942
943         case DIRPBIT_OFFCNT :
944             ashort = 0;
945             /* this needs to handle current directory access rights */
946             if ((dp = opendir( upath ))) {
947                 while (( de = readdir( dp )) != NULL ) {
948                     if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
949                         continue;
950
951                     if (!validupath(vol, de->d_name))
952                         continue;
953
954                     /* check for vetoed filenames */
955                     if (veto_file(vol->v_veto, de->d_name))
956                         continue;
957
958                     /* now check against too long a filename */
959                     if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
960                         continue;
961
962                     ashort++;
963                 }
964                 closedir( dp );
965             }
966             ashort = htons( ashort );
967             memcpy( data, &ashort, sizeof( ashort ));
968             data += sizeof( ashort );
969             break;
970
971         case DIRPBIT_UID :
972             aint = htonl(st->st_uid);
973             memcpy( data, &aint, sizeof( aint ));
974             data += sizeof( aint );
975             break;
976
977         case DIRPBIT_GID :
978             aint = htonl(st->st_gid);
979             memcpy( data, &aint, sizeof( aint ));
980             data += sizeof( aint );
981             break;
982
983         case DIRPBIT_ACCESS :
984             utommode( st, &ma );
985 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
986 #ifdef HAVE_ACCESS
987             accessmode( upath, &ma, dir );
988 #endif /* HAVE_ACCESS */
989 #endif /* SENDFILE_FLAVOR_LINUX */
990 #ifdef AFS      /* If only AFS defined, access() works only for AFS filesystems */ 
991             afsmode( upath, &ma, dir );
992 #endif /* AFS */
993             *data++ = ma.ma_user;
994             *data++ = ma.ma_world;
995             *data++ = ma.ma_group;
996             *data++ = ma.ma_owner;
997             break;
998
999             /* Client has requested the ProDOS information block.
1000                Just pass back the same basic block for all
1001                directories. <shirsch@ibm.net> */
1002         case DIRPBIT_PDINFO :                     /* ProDOS Info Block */
1003             *data++ = 0x0f;
1004             *data++ = 0;
1005             ashort = htons( 0x0200 );
1006             memcpy( data, &ashort, sizeof( ashort ));
1007             data += sizeof( ashort );
1008             memset( data, 0, sizeof( ashort ));
1009             data += sizeof( ashort );
1010             break;
1011
1012         default :
1013             if ( isad ) {
1014                 ad_close( &ad, ADFLAGS_HF );
1015             }
1016 #ifdef FORCE_UIDGID
1017             restore_uidgid ( &uidgid );
1018 #endif /* FORCE_UIDGID */
1019             return( AFPERR_BITMAP );
1020         }
1021         bitmap = bitmap>>1;
1022         bit++;
1023     }
1024     if ( nameoff ) {
1025         ashort = htons( data - buf );
1026         memcpy( nameoff, &ashort, sizeof( ashort ));
1027
1028         if ((aint = strlen( dir->d_name )) > MACFILELEN)
1029             aint = MACFILELEN;
1030
1031         *data++ = aint;
1032         memcpy( data, dir->d_name, aint );
1033         data += aint;
1034     }
1035     if ( isad ) {
1036         ad_close( &ad, ADFLAGS_HF );
1037     }
1038     *buflen = data - buf;
1039     return( AFP_OK );
1040 }
1041
1042 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1043 AFPObj      *obj;
1044 char    *ibuf, *rbuf;
1045 int             ibuflen, *rbuflen;
1046 {
1047     struct vol  *vol;
1048     struct dir  *dir;
1049     char        *path;
1050     u_int16_t   vid, bitmap;
1051     u_int32_t   did;
1052     int         rc;
1053
1054     *rbuflen = 0;
1055     ibuf += 2;
1056     memcpy( &vid, ibuf, sizeof( vid ));
1057     ibuf += sizeof( vid );
1058
1059     if (( vol = getvolbyvid( vid )) == NULL ) {
1060         return( AFPERR_PARAM );
1061     }
1062
1063     if (vol->v_flags & AFPVOL_RO)
1064         return AFPERR_VLOCK;
1065
1066     memcpy( &did, ibuf, sizeof( did ));
1067     ibuf += sizeof( int );
1068
1069     if (( dir = dirsearch( vol, did )) == NULL ) {
1070         return( AFPERR_NOOBJ );
1071     }
1072
1073     memcpy( &bitmap, ibuf, sizeof( bitmap ));
1074     bitmap = ntohs( bitmap );
1075     ibuf += sizeof( bitmap );
1076
1077     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1078         return( AFPERR_NOOBJ );
1079     }
1080
1081     /*
1082      * If ibuf is odd, make it even.
1083      */
1084     if ((u_long)ibuf & 1 ) {
1085         ibuf++;
1086     }
1087
1088     if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1089         setvoltime(obj, vol );
1090     }
1091     return( rc );
1092 }
1093
1094 int setdirparams(const struct vol *vol,
1095                  char *path, u_int16_t bitmap, char *buf )
1096 {
1097     struct maccess      ma;
1098     struct adouble      ad;
1099     struct utimbuf      ut;
1100     char                *upath;
1101     int                 bit = 0, aint, isad = 1;
1102     u_int16_t           ashort, bshort;
1103     int                 err = AFP_OK;
1104 #ifdef FORCE_UIDGID
1105     uidgidset           *uidgid;
1106
1107     memset(&uidgid, 0, sizeof(uidgid));
1108 #endif /* FORCE_UIDGID */
1109
1110     upath = mtoupath(vol, path);
1111     memset(&ad, 0, sizeof(ad));
1112 #ifdef FORCE_UIDGID
1113     save_uidgid ( &uidgid );
1114 #endif /* FORCE_UIDGID */
1115     if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1116                  O_RDWR|O_CREAT, 0666, &ad) < 0) {
1117         /*
1118          * Check to see what we're trying to set.  If it's anything
1119          * but ACCESS, UID, or GID, give an error.  If it's any of those
1120          * three, we don't need the ad to be open, so just continue.
1121          *
1122          * note: we also don't need to worry about mdate. also, be quiet
1123          *       if we're using the noadouble option.
1124          */
1125         if (!vol_noadouble(vol) && (bitmap &
1126                                     ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1127                                       (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1128 #ifdef FORCE_UIDGID
1129             restore_uidgid ( &uidgid );
1130 #endif /* FORCE_UIDGID */
1131             return AFPERR_ACCESS;
1132         }
1133
1134         isad = 0;
1135     } else {
1136         /*
1137          * Check to see if a create was necessary. If it was, we'll want
1138          * to set our name, etc.
1139          */
1140         if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1141             ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1142             memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1143                     ad_getentrylen( &ad, ADEID_NAME ));
1144         }
1145     }
1146
1147     while ( bitmap != 0 ) {
1148         while (( bitmap & 1 ) == 0 ) {
1149             bitmap = bitmap>>1;
1150             bit++;
1151         }
1152
1153         switch( bit ) {
1154         case DIRPBIT_ATTR :
1155             if (isad) {
1156                 memcpy( &ashort, buf, sizeof( ashort ));
1157                 ad_getattr(&ad, &bshort);
1158                 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1159                     bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1160                 } else {
1161                     bshort &= ~ashort;
1162                 }
1163                 ad_setattr(&ad, bshort);
1164             }
1165             buf += sizeof( ashort );
1166             break;
1167
1168         case DIRPBIT_CDATE :
1169             if (isad) {
1170                 memcpy(&aint, buf, sizeof(aint));
1171                 ad_setdate(&ad, AD_DATE_CREATE, aint);
1172             }
1173             buf += sizeof( aint );
1174             break;
1175
1176         case DIRPBIT_MDATE :
1177             memcpy(&aint, buf, sizeof(aint));
1178             if (isad)
1179                 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1180             ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1181             utime(upath, &ut);
1182             buf += sizeof( aint );
1183             break;
1184
1185         case DIRPBIT_BDATE :
1186             if (isad) {
1187                 memcpy(&aint, buf, sizeof(aint));
1188                 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1189             }
1190             buf += sizeof( aint );
1191             break;
1192
1193         case DIRPBIT_FINFO :
1194             /*
1195              * Alright, we admit it, this is *really* sick!
1196              * The 4 bytes that we don't copy, when we're dealing
1197              * with the root of a volume, are the directory's
1198              * location information. This eliminates that annoying
1199              * behavior one sees when mounting above another mount
1200              * point.
1201              */
1202             if (isad) {
1203                 if (  curdir->d_did == DIRDID_ROOT ) {
1204                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1205                     memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1206                 } else {
1207                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1208                 }
1209             }
1210             buf += 32;
1211             break;
1212
1213         case DIRPBIT_UID :      /* What kind of loser mounts as root? */
1214             memcpy( &aint, buf, sizeof(aint));
1215             buf += sizeof( aint );
1216             if ( (curdir->d_did == DIRDID_ROOT) &&
1217                     (setdeskowner( ntohl(aint), -1 ) < 0)) {
1218                 switch ( errno ) {
1219                 case EPERM :
1220                 case EACCES :
1221                     err = AFPERR_ACCESS;
1222                     goto setdirparam_done;
1223                     break;
1224                 case EROFS :
1225                     err = AFPERR_VLOCK;
1226                     goto setdirparam_done;
1227                     break;
1228                 default :
1229                     syslog( LOG_ERR, "setdirparam: setdeskowner: %s",
1230                             strerror(errno) );
1231                     if (!isad) {
1232                         err = AFPERR_PARAM;
1233                         goto setdirparam_done;
1234                     }
1235                     break;
1236                 }
1237             }
1238             if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1239                 switch ( errno ) {
1240                 case EPERM :
1241                 case EACCES :
1242                     err = AFPERR_ACCESS;
1243                     goto setdirparam_done;
1244                     break;
1245                 case EROFS :
1246                     err = AFPERR_VLOCK;
1247                     goto setdirparam_done;
1248                     break;
1249                 default :
1250                     syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1251                             strerror(errno) );
1252                     break;
1253                 }
1254             }
1255             break;
1256         case DIRPBIT_GID :
1257             memcpy( &aint, buf, sizeof( aint ));
1258             buf += sizeof( aint );
1259             if (curdir->d_did == DIRDID_ROOT)
1260                 setdeskowner( -1, ntohl(aint) );
1261
1262 #if 0       /* don't error if we can't set the desktop owner. */
1263             switch ( errno ) {
1264             case EPERM :
1265             case EACCES :
1266                 err = AFPERR_ACCESS;
1267                 goto setdirparam_done;
1268                 break;
1269             case EROFS :
1270                 err = AFPERR_VLOCK;
1271                 goto setdirparam_done;
1272                 break;
1273             default :
1274                 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1275                 if (!isad) {
1276                     err = AFPERR_PARAM;
1277                     goto setdirparam_done;
1278                 }
1279                 break;
1280             }
1281 #endif /* 0 */
1282
1283             if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1284                 switch ( errno ) {
1285                 case EPERM :
1286                 case EACCES :
1287                     err = AFPERR_ACCESS;
1288                     goto setdirparam_done;
1289                     break;
1290                 case EROFS :
1291                     err = AFPERR_VLOCK;
1292                     goto setdirparam_done;
1293                     break;
1294                 default :
1295                     syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1296                             strerror(errno) );
1297                     break;
1298                 }
1299             }
1300             break;
1301
1302         case DIRPBIT_ACCESS :
1303             ma.ma_user = *buf++;
1304             ma.ma_world = *buf++;
1305             ma.ma_group = *buf++;
1306             ma.ma_owner = *buf++;
1307
1308             if (curdir->d_did == DIRDID_ROOT)
1309                 setdeskmode(mtoumode( &ma ));
1310 #if 0 /* don't error if we can't set the desktop mode */
1311             switch ( errno ) {
1312             case EPERM :
1313             case EACCES :
1314                 err = AFPERR_ACCESS;
1315                 goto setdirparam_done;
1316             case EROFS :
1317                 err = AFPERR_VLOCK;
1318                 goto setdirparam_done;
1319             default :
1320                 syslog( LOG_ERR, "setdirparam: setdeskmode: %s",
1321                         strerror(errno) );
1322                 break;
1323                 err = AFPERR_PARAM;
1324                 goto setdirparam_done;
1325             }
1326         }
1327 #endif /* 0 */
1328
1329         if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1330                          (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1331             switch ( errno ) {
1332             case EPERM :
1333             case EACCES :
1334                 err = AFPERR_ACCESS;
1335                 goto setdirparam_done;
1336             case EROFS :
1337                 err = AFPERR_VLOCK;
1338                 goto setdirparam_done;
1339             default :
1340                 syslog( LOG_ERR, "setdirparam: setdirmode: %s",
1341                         strerror(errno) );
1342                 err = AFPERR_PARAM;
1343                 goto setdirparam_done;
1344             }
1345         }
1346         break;
1347
1348         /* Ignore what the client thinks we should do to the
1349            ProDOS information block.  Skip over the data and
1350            report nothing amiss. <shirsch@ibm.net> */
1351     case DIRPBIT_PDINFO :
1352         buf += 6;
1353         break;
1354
1355     default :
1356         err = AFPERR_BITMAP;
1357         goto setdirparam_done;
1358         break;
1359     }
1360
1361     bitmap = bitmap>>1;
1362     bit++;
1363 }
1364
1365
1366 setdirparam_done:
1367 if ( isad ) {
1368     ad_flush( &ad, ADFLAGS_HF );
1369     ad_close( &ad, ADFLAGS_HF );
1370 }
1371
1372 #ifdef FORCE_UIDGID
1373 restore_uidgid ( &uidgid );
1374 #endif /* FORCE_UIDGID */
1375 return err;
1376 }
1377
1378 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1379 AFPObj      *obj;
1380 char    *ibuf, *rbuf;
1381 int             ibuflen, *rbuflen;
1382 {
1383     struct adouble      ad;
1384     struct stat         st;
1385     struct vol          *vol;
1386     struct dir          *dir;
1387     char                *path, *upath;
1388     u_int32_t           did;
1389     u_int16_t           vid;
1390 #ifdef FORCE_UIDGID
1391     uidgidset           *uidgid;
1392
1393     memset(&uidgid, 0, sizeof(uidgid));
1394 #endif /* FORCE_UIDGID */
1395
1396     *rbuflen = 0;
1397     ibuf += 2;
1398
1399     memcpy( &vid, ibuf, sizeof( vid ));
1400     ibuf += sizeof( vid );
1401     if (( vol = getvolbyvid( vid )) == NULL ) {
1402         return( AFPERR_PARAM );
1403     }
1404
1405     if (vol->v_flags & AFPVOL_RO)
1406         return AFPERR_VLOCK;
1407
1408     memcpy( &did, ibuf, sizeof( did ));
1409     ibuf += sizeof( did );
1410     if (( dir = dirsearch( vol, did )) == NULL ) {
1411         return( AFPERR_NOOBJ );
1412     }
1413
1414     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1415         switch( errno ) {
1416         case EACCES:
1417             return( AFPERR_ACCESS );
1418         case EEXIST:
1419             return( AFPERR_EXIST );
1420         default:
1421             return( AFPERR_NOOBJ );
1422         }
1423     }
1424
1425     /* check for illegal bits */
1426     if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1427             strpbrk(path, MSWINDOWS_BADCHARS))
1428         return AFPERR_PARAM;
1429
1430     upath = mtoupath(vol, path);
1431
1432     if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1433         return AFPERR_PARAM;
1434
1435     if (!validupath(vol, upath))
1436         return AFPERR_EXIST;
1437
1438     /* check for vetoed filenames */
1439     if (veto_file(vol->v_veto, upath))
1440         return AFPERR_EXIST;
1441
1442 #ifdef FORCE_UIDGID
1443     save_uidgid ( &uidgid );
1444     set_uidgid  ( vol );
1445 #endif /* FORCE_UIDGID */
1446
1447     if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1448 #ifdef FORCE_UIDGID
1449         restore_uidgid ( &uidgid );
1450 #endif /* FORCE_UIDGID */
1451         switch ( errno ) {
1452         case ENOENT :
1453             return( AFPERR_NOOBJ );
1454         case EROFS :
1455             return( AFPERR_VLOCK );
1456         case EACCES :
1457             return( AFPERR_ACCESS );
1458         case EEXIST :
1459             return( AFPERR_EXIST );
1460         case ENOSPC :
1461         case EDQUOT :
1462             return( AFPERR_DFULL );
1463         default :
1464             return( AFPERR_PARAM );
1465         }
1466     }
1467
1468     if (stat(upath, &st) < 0) {
1469 #ifdef FORCE_UIDGID
1470         restore_uidgid ( &uidgid );
1471 #endif /* FORCE_UIDGID */
1472         return AFPERR_MISC;
1473     }
1474
1475     if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1476                        strlen(upath), &st)) == NULL) {
1477 #ifdef FORCE_UIDGID
1478         restore_uidgid ( &uidgid );
1479 #endif /* FORCE_UIDGID */
1480         return AFPERR_MISC;
1481     }
1482
1483     if ( movecwd( vol, dir ) < 0 ) {
1484 #ifdef FORCE_UIDGID
1485         restore_uidgid ( &uidgid );
1486 #endif /* FORCE_UIDGID */
1487         return( AFPERR_PARAM );
1488     }
1489
1490     memset(&ad, 0, sizeof(ad));
1491     if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1492                  O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
1493         if (vol_noadouble(vol))
1494             goto createdir_done;
1495 #ifdef FORCE_UIDGID
1496         restore_uidgid ( &uidgid );
1497 #endif /* FORCE_UIDGID */
1498         return( AFPERR_ACCESS );
1499     }
1500
1501     ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1502     memcpy( ad_entry( &ad, ADEID_NAME ), path,
1503             ad_getentrylen( &ad, ADEID_NAME ));
1504     ad_flush( &ad, ADFLAGS_HF );
1505     ad_close( &ad, ADFLAGS_HF );
1506
1507 createdir_done:
1508     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1509     *rbuflen = sizeof( u_int32_t );
1510     setvoltime(obj, vol );
1511 #ifdef FORCE_UIDGID
1512     restore_uidgid ( &uidgid );
1513 #endif /* FORCE_UIDGID */
1514     return( AFP_OK );
1515 }
1516
1517
1518 int renamedir(src, dst, dir, newparent, newname, noadouble)
1519 char    *src, *dst, *newname;
1520 struct dir      *dir, *newparent;
1521 const int noadouble;
1522 {
1523     struct adouble      ad;
1524     struct dir          *parent;
1525     char                *buf;
1526     int                 len, err;
1527
1528     /* existence check moved to afp_moveandrename */
1529     if ( rename( src, dst ) < 0 ) {
1530         switch ( errno ) {
1531         case ENOENT :
1532             return( AFPERR_NOOBJ );
1533         case EACCES :
1534             return( AFPERR_ACCESS );
1535         case EROFS:
1536             return AFPERR_VLOCK;
1537         case EINVAL:
1538             /* tried to move directory into a subdirectory of itself */
1539             return AFPERR_CANTMOVE;
1540         case EXDEV:
1541             /* this needs to copy and delete. bleah. that means we have
1542              * to deal with entire directory hierarchies. */
1543             if ((err = copydir(src, dst, noadouble)) < 0) {
1544                 deletedir(dst);
1545                 return err;
1546             }
1547             if ((err = deletedir(src)) < 0)
1548                 return err;
1549             break;
1550         default :
1551             return( AFPERR_PARAM );
1552         }
1553     }
1554
1555     memset(&ad, 0, sizeof(ad));
1556     if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1557         switch ( errno ) {
1558         case ENOENT :
1559             if (noadouble) {
1560                 len = strlen(newname);
1561                 goto renamedir_done;
1562             }
1563             return( AFPERR_NOOBJ );
1564         case EACCES :
1565             return( AFPERR_ACCESS );
1566         default :
1567             return( AFPERR_PARAM );
1568         }
1569     }
1570     len = strlen( newname );
1571     ad_setentrylen( &ad, ADEID_NAME, len );
1572     memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1573     ad_flush( &ad, ADFLAGS_HF );
1574     ad_close( &ad, ADFLAGS_HF );
1575
1576 renamedir_done:
1577     if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1578         syslog( LOG_ERR, "renamedir: realloc: %s", strerror(errno) );
1579         return AFPERR_MISC;
1580     }
1581     dir->d_name = buf;
1582     strcpy( dir->d_name, newname );
1583
1584     if (( parent = dir->d_parent ) == NULL ) {
1585         return( AFP_OK );
1586     }
1587     if ( parent == newparent ) {
1588         return( AFP_OK );
1589     }
1590
1591     /* detach from old parent and add to new one. */
1592     dirchildremove(parent, dir);
1593     dir->d_parent = newparent;
1594     dirchildadd(newparent, dir);
1595     return( AFP_OK );
1596 }
1597
1598 #define DOT_APPLEDOUBLE_LEN 13
1599 /* delete an empty directory */
1600 int deletecurdir( vol, path, pathlen )
1601 const struct vol        *vol;
1602 char *path;
1603 int pathlen;
1604 {
1605     struct dirent *de;
1606     struct stat st;
1607     struct dir  *fdir;
1608     DIR *dp;
1609 #ifdef FORCE_UIDGID
1610     uidgidset           *uidgid;
1611
1612     memset(&uidgid, 0, sizeof(uidgid));
1613 #endif /* FORCE_UIDGID */
1614
1615     if ( curdir->d_parent == NULL ) {
1616         return( AFPERR_ACCESS );
1617     }
1618
1619     if ( curdir->d_child != NULL ) {
1620         return( AFPERR_DIRNEMPT );
1621     }
1622
1623     fdir = curdir;
1624
1625 #ifdef FORCE_UIDGID
1626     save_uidgid ( &uidgid );
1627     set_uidgid  ( vol );
1628 #endif /* FORCE_UIDGID */
1629
1630     /* delete stray .AppleDouble files. this happens to get .Parent files
1631        as well. */
1632     if ((dp = opendir(".AppleDouble"))) {
1633         strcpy(path, ".AppleDouble/");
1634         while ((de = readdir(dp))) {
1635             /* skip this and previous directory */
1636             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1637                 continue;
1638
1639             /* bail if the file exists in the current directory.
1640              * note: this will not fail with dangling symlinks */
1641             if (stat(de->d_name, &st) == 0) {
1642                 closedir(dp);
1643 #ifdef FORCE_UIDGID
1644                 restore_uidgid ( &uidgid );
1645 #endif /* FORCE_UIDGID */
1646                 return AFPERR_DIRNEMPT;
1647             }
1648
1649             strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1650             if (unlink(path) < 0) {
1651                 closedir(dp);
1652                 switch (errno) {
1653                 case EPERM:
1654                 case EACCES :
1655 #ifdef FORCE_UIDGID
1656                     restore_uidgid ( &uidgid );
1657 #endif /* FORCE_UIDGID */
1658                     return( AFPERR_ACCESS );
1659                 case EROFS:
1660 #ifdef FORCE_UIDGID
1661                     restore_uidgid ( &uidgid );
1662 #endif /* FORCE_UIDGID */
1663                     return AFPERR_VLOCK;
1664                 case ENOENT :
1665                     continue;
1666                 default :
1667 #ifdef FORCE_UIDGID
1668                     restore_uidgid ( &uidgid );
1669 #endif /* FORCE_UIDGID */
1670                     return( AFPERR_PARAM );
1671                 }
1672             }
1673         }
1674         closedir(dp);
1675     }
1676
1677     if ( rmdir( ".AppleDouble" ) < 0 ) {
1678         switch ( errno ) {
1679         case ENOENT :
1680             break;
1681         case ENOTEMPTY :
1682 #ifdef FORCE_UIDGID
1683             restore_uidgid ( &uidgid );
1684 #endif /* FORCE_UIDGID */
1685             return( AFPERR_DIRNEMPT );
1686         case EROFS:
1687 #ifdef FORCE_UIDGID
1688             restore_uidgid ( &uidgid );
1689 #endif /* FORCE_UIDGID */
1690             return AFPERR_VLOCK;
1691         case EPERM:
1692         case EACCES :
1693 #ifdef FORCE_UIDGID
1694             restore_uidgid ( &uidgid );
1695 #endif /* FORCE_UIDGID */
1696             return( AFPERR_ACCESS );
1697         default :
1698 #ifdef FORCE_UIDGID
1699             restore_uidgid ( &uidgid );
1700 #endif /* FORCE_UIDGID */
1701             return( AFPERR_PARAM );
1702         }
1703     }
1704
1705     /* now get rid of dangling symlinks */
1706     if ((dp = opendir("."))) {
1707         while ((de = readdir(dp))) {
1708             /* skip this and previous directory */
1709             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1710                 continue;
1711
1712             /* bail if it's not a symlink */
1713             if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1714 #ifdef FORCE_UIDGID
1715                 restore_uidgid ( &uidgid );
1716 #endif /* FORCE_UIDGID */
1717                 return AFPERR_DIRNEMPT;
1718             }
1719
1720             if (unlink(de->d_name) < 0) {
1721                 switch (errno) {
1722                 case EPERM:
1723                 case EACCES :
1724 #ifdef FORCE_UIDGID
1725                     restore_uidgid ( &uidgid );
1726 #endif /* FORCE_UIDGID */
1727                     return( AFPERR_ACCESS );
1728                 case EROFS:
1729 #ifdef FORCE_UIDGID
1730                     restore_uidgid ( &uidgid );
1731 #endif /* FORCE_UIDGID */
1732                     return AFPERR_VLOCK;
1733                 case ENOENT :
1734                     continue;
1735                 default :
1736 #ifdef FORCE_UIDGID
1737                     restore_uidgid ( &uidgid );
1738 #endif /* FORCE_UIDGID */
1739                     return( AFPERR_PARAM );
1740                 }
1741             }
1742         }
1743         closedir(dp);
1744     }
1745
1746     if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1747 #ifdef FORCE_UIDGID
1748         restore_uidgid ( &uidgid );
1749 #endif /* FORCE_UIDGID */
1750         return( AFPERR_NOOBJ );
1751     }
1752
1753     if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1754         switch ( errno ) {
1755         case ENOENT :
1756 #ifdef FORCE_UIDGID
1757             restore_uidgid ( &uidgid );
1758 #endif /* FORCE_UIDGID */
1759             return( AFPERR_NOOBJ );
1760         case ENOTEMPTY :
1761 #ifdef FORCE_UIDGID
1762             restore_uidgid ( &uidgid );
1763 #endif /* FORCE_UIDGID */
1764             return( AFPERR_DIRNEMPT );
1765         case EPERM:
1766         case EACCES :
1767 #ifdef FORCE_UIDGID
1768             restore_uidgid ( &uidgid );
1769 #endif /* FORCE_UIDGID */
1770             return( AFPERR_ACCESS );
1771         case EROFS:
1772 #ifdef FORCE_UIDGID
1773             restore_uidgid ( &uidgid );
1774 #endif /* FORCE_UIDGID */
1775             return AFPERR_VLOCK;
1776         default :
1777 #ifdef FORCE_UIDGID
1778             restore_uidgid ( &uidgid );
1779 #endif /* FORCE_UIDGID */
1780             return( AFPERR_PARAM );
1781         }
1782     }
1783
1784     dirchildremove(curdir, fdir);
1785 #ifdef CNID_DB
1786     cnid_delete(vol->v_db, fdir->d_did);
1787 #endif /* CNID_DB */
1788     dir_remove( vol, fdir );
1789
1790 #ifdef FORCE_UIDGID
1791     restore_uidgid ( &uidgid );
1792 #endif /* FORCE_UIDGID */
1793     return( AFP_OK );
1794 }
1795
1796 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1797 AFPObj      *obj;
1798 char    *ibuf, *rbuf;
1799 int             ibuflen, *rbuflen;
1800 {
1801     struct passwd       *pw;
1802     struct group        *gr;
1803     char                *name;
1804     u_int32_t           id;
1805     int                 len, sfunc;
1806
1807     ibuf++;
1808     sfunc = (unsigned char) *ibuf++;
1809     memcpy( &id, ibuf, sizeof( id ));
1810
1811     id = ntohl(id);
1812
1813     if ( id != 0 ) {
1814         switch ( sfunc ) {
1815         case 1 :
1816             if (( pw = getpwuid( id )) == NULL ) {
1817                 *rbuflen = 0;
1818                 return( AFPERR_NOITEM );
1819             }
1820             name = pw->pw_name;
1821             break;
1822
1823         case 2 :
1824             if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1825                 *rbuflen = 0;
1826                 return( AFPERR_NOITEM );
1827             }
1828             name = gr->gr_name;
1829             break;
1830
1831         default :
1832             *rbuflen = 0;
1833             return( AFPERR_PARAM );
1834         }
1835
1836         len = strlen( name );
1837
1838     } else {
1839         len = 0;
1840         name = NULL;
1841     }
1842
1843     *rbuf++ = len;
1844     if ( len > 0 ) {
1845         memcpy( rbuf, name, len );
1846     }
1847     *rbuflen = len + 1;
1848     return( AFP_OK );
1849 }
1850
1851 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1852 AFPObj      *obj;
1853 char    *ibuf, *rbuf;
1854 int             ibuflen, *rbuflen;
1855 {
1856     struct passwd       *pw;
1857     struct group        *gr;
1858     int                 len, sfunc;
1859     u_int32_t           id;
1860
1861     ibuf++;
1862     sfunc = (unsigned char) *ibuf++;
1863     len = (unsigned char) *ibuf++;
1864     ibuf[ len ] = '\0';
1865
1866     if ( len != 0 ) {
1867         switch ( sfunc ) {
1868         case 3 :
1869             if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1870                 *rbuflen = 0;
1871                 return( AFPERR_NOITEM );
1872             }
1873             id = pw->pw_uid;
1874             break;
1875
1876         case 4 :
1877             if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1878                 *rbuflen = 0;
1879                 return( AFPERR_NOITEM );
1880             }
1881             id = gr->gr_gid;
1882             break;
1883         default :
1884             *rbuflen = 0;
1885             return( AFPERR_PARAM );
1886         }
1887     } else {
1888         id = 0;
1889     }
1890     id = htonl(id);
1891     memcpy( rbuf, &id, sizeof( id ));
1892     *rbuflen = sizeof( id );
1893     return( AFP_OK );
1894 }
1895
1896 /* variable DID support */
1897 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1898 AFPObj      *obj;
1899 char    *ibuf, *rbuf;
1900 int             ibuflen, *rbuflen;
1901 {
1902 #if 0
1903     struct vol   *vol;
1904     struct dir   *dir;
1905     u_int16_t    vid;
1906     u_int32_t    did;
1907 #endif /* 0 */
1908
1909     *rbuflen = 0;
1910
1911     /* do nothing as dids are static for the life of the process. */
1912 #if 0
1913     ibuf += 2;
1914
1915     memcpy(&vid,  ibuf, sizeof( vid ));
1916     ibuf += sizeof( vid );
1917     if (( vol = getvolbyvid( vid )) == NULL ) {
1918         return( AFPERR_PARAM );
1919     }
1920
1921     memcpy( &did, ibuf, sizeof( did ));
1922     ibuf += sizeof( did );
1923     if (( dir = dirsearch( vol, did )) == NULL ) {
1924         return( AFPERR_PARAM );
1925     }
1926
1927     /* dir_remove -- deletedid */
1928 #endif /* 0 */
1929
1930     return AFP_OK;
1931 }
1932
1933 /* did creation gets done automatically */
1934 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1935 AFPObj      *obj;
1936 char    *ibuf, *rbuf;
1937 int             ibuflen, *rbuflen;
1938 {
1939     struct vol          *vol;
1940     struct dir          *dir, *parentdir;
1941     struct stat         st;
1942     char                *path, *upath;
1943     u_int32_t           did;
1944     u_int16_t           vid;
1945 #ifdef FORCE_UIDGID
1946     uidgidset           *uidgid;
1947
1948     memset(&uidgid, 0, sizeof(uidgid));
1949 #endif /* FORCE_UIDGID */
1950
1951     *rbuflen = 0;
1952     ibuf += 2;
1953
1954     memcpy(&vid, ibuf, sizeof(vid));
1955     ibuf += sizeof( vid );
1956
1957     if (( vol = getvolbyvid( vid )) == NULL ) {
1958         return( AFPERR_PARAM );
1959     }
1960
1961     memcpy(&did, ibuf, sizeof(did));
1962     ibuf += sizeof(did);
1963
1964     if (( parentdir = dirsearch( vol, did )) == NULL ) {
1965         return( AFPERR_NOOBJ );
1966     }
1967
1968     if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1969         switch( errno ) {
1970         case EACCES:
1971             return( AFPERR_ACCESS );
1972         default:
1973             return( AFPERR_NOOBJ );
1974         }
1975     }
1976
1977     /* see if we already have the directory. */
1978     upath = mtoupath(vol, path);
1979     if ( stat( upath, &st ) < 0 ) {
1980         return( AFPERR_NOOBJ );
1981     }
1982
1983     dir = parentdir->d_child;
1984     while (dir) {
1985         if (strdiacasecmp(dir->d_name, path) == 0) {
1986             memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1987             *rbuflen = sizeof(dir->d_did);
1988             return AFP_OK;
1989         }
1990         dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1991     }
1992
1993 #ifdef FORCE_UIDGID
1994     save_uidgid ( &uidgid );
1995     set_uidgid  ( vol );
1996 #endif /* FORCE_UIDGID */
1997
1998     /* we don't already have a did. add one in. */
1999     if ((dir = adddir(vol, parentdir, path, strlen(path),
2000                       upath, strlen(upath), &st)) == NULL) {
2001 #ifdef FORCE_UIDGID
2002         restore_uidgid ( &uidgid );
2003 #endif /* FORCE_UIDGID */
2004         return AFPERR_MISC;
2005     }
2006
2007     memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2008     *rbuflen = sizeof(dir->d_did);
2009 #ifdef FORCE_UIDGID
2010     restore_uidgid ( &uidgid );
2011 #endif /* FORCE_UIDGID */
2012     return AFP_OK;
2013 }