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