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