]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/filedir.c
First working IPC reconnect
[netatalk.git] / etc / afpd / filedir.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 /* STDC check */
13 #if STDC_HEADERS
14 #include <string.h>
15 #else /* STDC_HEADERS */
16 #ifndef HAVE_STRCHR
17 #define strchr index
18 #define strrchr index
19 #endif /* HAVE_STRCHR */
20 char *strchr (), *strrchr ();
21 #ifndef HAVE_MEMCPY
22 #define memcpy(d,s,n) bcopy ((s), (d), (n))
23 #define memmove(d,s,n) bcopy ((s), (d), (n))
24 #endif /* ! HAVE_MEMCPY */
25 #endif /* STDC_HEADERS */
26
27 #ifdef HAVE_STRINGS_H
28 #include <strings.h>
29 #endif
30 #include <errno.h>
31 #include <sys/param.h>
32
33 #include <atalk/adouble.h>
34 #include <atalk/vfs.h>
35 #include <atalk/afp.h>
36 #include <atalk/util.h>
37 #include <atalk/cnid.h>
38 #include <atalk/logger.h>
39 #include <atalk/unix.h>
40 #include <atalk/bstrlib.h>
41 #include <atalk/bstradd.h>
42 #include <atalk/acl.h>
43 #include <atalk/globals.h>
44
45 #include "directory.h"
46 #include "dircache.h"
47 #include "desktop.h"
48 #include "volume.h"
49 #include "fork.h"
50 #include "file.h"
51 #include "filedir.h"
52 #include "unix.h"
53
54 #ifdef DROPKLUDGE
55 int matchfile2dirperms(
56 /* Since it's kinda' big; I decided against an
57    inline function */
58     char    *upath,
59     struct vol  *vol,
60     int     did)
61 /* The below code changes the way file ownership is determined in the name of
62    fixing dropboxes.  It has known security problem.  See the netatalk FAQ for
63    more information */
64 {
65     struct stat st, sb;
66     struct dir  *dir;
67     char    *adpath;
68     uid_t       uid;
69     int         ret = AFP_OK;
70 #ifdef DEBUG
71     LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
72 #endif
73
74     if (stat(upath, &st ) < 0) {
75         LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
76         return AFPERR_NOOBJ ;
77     }
78
79     adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
80     /* FIXME dirsearch doesn't move cwd to did ! */
81     if (( dir = dirlookup( vol, did )) == NULL ) {
82         LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
83         ret = AFPERR_NOOBJ;
84     }
85     else if (stat(".", &sb) < 0) {
86         LOG(log_error, logtype_afpd,
87             "matchfile2dirperms: Error checking directory \"%s\": %s",
88             dir->d_m_name, strerror(errno));
89         ret = AFPERR_NOOBJ;
90     }
91     else {
92         uid=geteuid();
93         if ( uid != sb.st_uid )
94         {
95             seteuid(0);
96             if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
97             {
98                 LOG(log_error, logtype_afpd,
99                     "matchfile2dirperms(%s): Error changing owner/gid: %s",
100                     upath, strerror(errno));
101                 ret = AFPERR_ACCESS;
102             }
103             else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
104             {
105                 LOG(log_error, logtype_afpd,
106                     "matchfile2dirperms(%s): Error adding file read permissions: %s",
107                     upath, strerror(errno));
108                 ret = AFPERR_ACCESS;
109             }
110             else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
111             {
112                 LOG(log_error, logtype_afpd,
113                     "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
114                     adpath, strerror(errno));
115                 ret = AFPERR_ACCESS;
116             }
117             else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
118             {
119                 LOG(log_error, logtype_afpd,
120                     "matchfile2dirperms(%s):  Error adding AD file read permissions: %s",
121                     adpath, strerror(errno));
122                 ret = AFPERR_ACCESS;
123             }
124             seteuid(uid);
125         }
126     } /* end else if stat success */
127
128 #ifdef DEBUG
129     LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
130 #endif
131     return ret;
132 }
133 #endif
134
135 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
136 {
137     struct stat     *st;
138     struct vol      *vol;
139     struct dir      *dir;
140     u_int32_t           did;
141     int         ret;
142     size_t      buflen;
143     u_int16_t       fbitmap, dbitmap, vid;
144     struct path         *s_path;
145
146     *rbuflen = 0;
147     ibuf += 2;
148
149     memcpy( &vid, ibuf, sizeof( vid ));
150     ibuf += sizeof( vid );
151     if (NULL == ( vol = getvolbyvid( vid )) ) {
152         /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
153          * from the list.
154          */
155         return( AFPERR_ACCESS );
156     }
157
158     memcpy( &did, ibuf, sizeof( did ));
159     ibuf += sizeof( did );
160
161     if (NULL == ( dir = dirlookup( vol, did )) ) {
162         return afp_errno;
163     }
164
165     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
166     fbitmap = ntohs( fbitmap );
167     ibuf += sizeof( fbitmap );
168     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
169     dbitmap = ntohs( dbitmap );
170     ibuf += sizeof( dbitmap );
171
172     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
173         return get_afp_errno(AFPERR_NOOBJ);
174     }
175
176     LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
177         ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
178         ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
179
180     st   = &s_path->st;
181     if (!s_path->st_valid) {
182         /* it's a dir and it should be there
183          * because we chdir in it in cname or
184          * it's curdir (maybe deleted, but then we can't know).
185          * So we need to try harder.
186          */
187         of_statdir(vol, s_path);
188     }
189     if ( s_path->st_errno != 0 ) {
190         if (afp_errno != AFPERR_ACCESS) {
191             return( AFPERR_NOOBJ );
192         }
193     }
194
195
196     buflen = 0;
197     if (S_ISDIR(st->st_mode)) {
198         if (dbitmap) {
199             dir = s_path->d_dir;
200             if (!dir)
201                 return AFPERR_NOOBJ;
202
203             ret = getdirparams(vol, dbitmap, s_path, dir,
204                                rbuf + 3 * sizeof( u_int16_t ), &buflen );
205             if (ret != AFP_OK )
206                 return( ret );
207         }
208         /* this is a directory */
209         *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
210     } else {
211         if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
212                                                      rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
213             return( ret );
214         }
215         /* this is a file */
216         *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
217     }
218     *rbuflen = buflen + 3 * sizeof( u_int16_t );
219     fbitmap = htons( fbitmap );
220     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
221     rbuf += sizeof( fbitmap );
222     dbitmap = htons( dbitmap );
223     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
224     rbuf += sizeof( dbitmap ) + sizeof( u_char );
225     *rbuf = 0;
226
227     return( AFP_OK );
228 }
229
230 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
231 {
232     struct stat *st;
233     struct vol  *vol;
234     struct dir  *dir;
235     struct path *path;
236     u_int16_t   vid, bitmap;
237     int     did, rc;
238
239     *rbuflen = 0;
240     ibuf += 2;
241     memcpy( &vid, ibuf, sizeof(vid));
242     ibuf += sizeof( vid );
243
244     if (NULL == ( vol = getvolbyvid( vid )) ) {
245         return( AFPERR_PARAM );
246     }
247
248     if (vol->v_flags & AFPVOL_RO)
249         return AFPERR_VLOCK;
250
251     memcpy( &did, ibuf, sizeof( did));
252     ibuf += sizeof( did);
253
254     if (NULL == ( dir = dirlookup( vol, did )) ) {
255         return afp_errno;
256     }
257
258     memcpy( &bitmap, ibuf, sizeof( bitmap ));
259     bitmap = ntohs( bitmap );
260     ibuf += sizeof( bitmap );
261
262     if (NULL == ( path = cname( vol, dir, &ibuf ))) {
263         return get_afp_errno(AFPERR_NOOBJ);
264     }
265
266     st   = &path->st;
267     if (!path->st_valid) {
268         /* it's a dir and it should be there
269          * because we chdir in it in cname
270          */
271         of_statdir(vol, path);
272     }
273
274     if ( path->st_errno != 0 ) {
275         if (afp_errno != AFPERR_ACCESS)
276             return( AFPERR_NOOBJ );
277     }
278     /*
279      * If ibuf is odd, make it even.
280      */
281     if ((u_long)ibuf & 1 ) {
282         ibuf++;
283     }
284
285     if (S_ISDIR(st->st_mode)) {
286         rc = setdirparams(vol, path, bitmap, ibuf );
287     } else {
288         rc = setfilparams(vol, path, bitmap, ibuf );
289     }
290     if ( rc == AFP_OK ) {
291         setvoltime(obj, vol );
292     }
293
294     return( rc );
295 }
296
297 /* --------------------------------------------
298    Factorise some checks on a pathname
299 */
300 int check_name(const struct vol *vol, char *name)
301 {
302     /* check for illegal characters in the unix filename */
303     if (!wincheck(vol, name))
304         return AFPERR_PARAM;
305
306     if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
307         return AFPERR_PARAM;
308
309     if (!vol->vfs->vfs_validupath(vol, name)) {
310         LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
311         return AFPERR_EXIST;
312     }
313
314     /* check for vetoed filenames */
315     if (veto_file(vol->v_veto, name))
316         return AFPERR_EXIST;
317     return 0;
318 }
319
320 /* ------------------------- 
321     move and rename sdir:oldname to curdir:newname in volume vol
322     special care is needed for lock   
323 */
324 static int moveandrename(const struct vol *vol,
325                          struct dir *sdir,
326                          int sdir_fd,
327                          char *oldname,
328                          char *newname,
329                          int isdir)
330 {
331     char            *oldunixname = NULL;
332     char            *upath;
333     int             rc;
334     struct stat     *st, nst;
335     int             adflags;
336     struct adouble      ad;
337     struct adouble      *adp;
338     struct ofork        *opened = NULL;
339     struct path     path;
340     cnid_t          id;
341     int             cwd_fd = -1;
342
343     LOG(log_debug, logtype_afpd,
344         "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
345         cfrombstr(sdir->d_u_name), oldname, newname);
346
347     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
348     adp = &ad;
349     adflags = 0;
350
351     if (!isdir) {
352         if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
353             return AFPERR_PARAM; /* can't convert */
354         id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
355
356 #ifndef HAVE_ATFUNCS
357         /* Need full path */
358         free(oldunixname);
359         if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
360             return AFPERR_PARAM; /* pathname too long */
361 #endif /* HAVE_ATFUNCS */
362
363         path.st_valid = 0;
364         path.u_name = oldunixname;
365
366 #ifdef HAVE_ATFUNCS
367         opened = of_findnameat(sdir_fd, &path);
368 #else
369         opened = of_findname(&path);
370 #endif /* HAVE_ATFUNCS */
371
372         if (opened) {
373             /* reuse struct adouble so it won't break locks */
374             adp = opened->of_ad;
375         }
376     } else {
377         id = sdir->d_did; /* we already have the CNID */
378         if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
379             return AFPERR_PARAM;
380         adflags = ADFLAGS_DIR;
381     }
382
383     /*
384      * oldunixname now points to either
385      *   a) full pathname of the source fs object (if renameat is not available)
386      *   b) the oldname (renameat is available)
387      * we are in the dest folder so we need to use 
388      *   a) oldunixname for ad_open
389      *   b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
390      */
391
392     if (sdir_fd != -1) {
393         if ((cwd_fd = open(".", O_RDONLY)) == -1)
394             return AFPERR_MISC;
395         if (fchdir(sdir_fd) != 0) {
396             rc = AFPERR_MISC;
397             goto exit;
398         }
399     }
400     if (!ad_metadata(oldunixname, adflags, adp)) {
401         u_int16_t bshort;
402
403         ad_getattr(adp, &bshort);
404         ad_close_metadata( adp);
405         if ((bshort & htons(ATTRBIT_NORENAME))) {
406             rc = AFPERR_OLOCK;
407             goto exit;
408         }
409     }
410     if (sdir_fd != -1) {
411         if (fchdir(cwd_fd) != 0) {
412             LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
413             rc = AFPERR_MISC;
414             goto exit;
415         }
416     }
417
418     if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){ 
419         rc = AFPERR_PARAM;
420         goto exit;
421     }
422     path.u_name = upath;
423     st = &path.st;
424     if (0 != (rc = check_name(vol, upath))) {
425         goto exit;
426     }
427
428     /* source == destination. we just silently accept this. */
429     if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
430         if (strcmp(oldname, newname) == 0) {
431             rc = AFP_OK;
432             goto exit;
433         }
434
435         if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
436             if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
437                 /* not the same file */
438                 rc = AFPERR_EXIST;
439                 goto exit;
440             }
441             errno = 0;
442         }
443     } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
444         rc = AFPERR_EXIST;
445         goto exit;
446     }
447
448     if ( !isdir ) {
449         path.st_valid = 1;
450         path.st_errno = errno;
451         if (of_findname(&path)) {
452             rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
453         } else {
454             rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
455             if (rc == AFP_OK)
456                 of_rename(vol, opened, sdir, oldname, curdir, newname);
457         }
458     } else {
459         rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
460     }
461     if ( rc == AFP_OK && id ) {
462         /* renaming may have moved the file/dir across a filesystem */
463         if (stat(upath, st) < 0) {
464             rc = AFPERR_MISC;
465             goto exit;
466         }
467
468         /* Remove it from the cache */
469         struct dir *cacheddir = dircache_search_by_did(vol, id);
470         if (cacheddir) {
471             LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
472             (void)dir_remove(vol, cacheddir);
473         }
474
475         /* Fixup adouble info */
476         if (!ad_metadata(upath, adflags, adp)) {
477             ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
478             ad_flush(adp);
479             ad_close_metadata(adp);
480         }
481
482         /* fix up the catalog entry */
483         cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
484     }
485
486 exit:
487     if (cwd_fd != -1)
488         close(cwd_fd);
489     if (oldunixname)
490         free(oldunixname);
491     return rc;
492 }
493
494 /* -------------------------------------------- */
495 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
496 {
497     struct vol  *vol;
498     struct dir  *sdir;
499     char        *oldname, *newname;
500     struct path *path;
501     u_int32_t   did;
502     int         plen;
503     u_int16_t   vid;
504     int         isdir = 0;
505     int         rc;
506
507     *rbuflen = 0;
508     ibuf += 2;
509
510     memcpy( &vid, ibuf, sizeof( vid ));
511     ibuf += sizeof( vid );
512     if (NULL == ( vol = getvolbyvid( vid )) ) {
513         return( AFPERR_PARAM );
514     }
515
516     if (vol->v_flags & AFPVOL_RO)
517         return AFPERR_VLOCK;
518
519     memcpy( &did, ibuf, sizeof( did ));
520     ibuf += sizeof( did );
521     if (NULL == ( sdir = dirlookup( vol, did )) ) {
522         return afp_errno;
523     }
524
525     /* source pathname */
526     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
527         return get_afp_errno(AFPERR_NOOBJ);
528     }
529
530     sdir = curdir;
531     newname = obj->newtmp;
532     oldname = obj->oldtmp;
533     isdir = path_isadir(path);
534     if ( *path->m_name != '\0' ) {
535         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
536         if (isdir) {
537             /* curdir parent dir, need to move sdir back */
538             sdir = path->d_dir;
539         }
540     }
541     else {
542         if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
543             return( AFPERR_NORENAME );
544         }
545         /* move to destination dir */
546         if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
547             return afp_errno;
548         }
549         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
550     }
551
552     /* another place where we know about the path type */
553     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
554         return( AFPERR_PARAM );
555     }
556
557     if (!plen) {
558         return AFP_OK; /* newname == oldname same dir */
559     }
560     
561     rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
562     if ( rc == AFP_OK ) {
563         setvoltime(obj, vol );
564     }
565
566     return( rc );
567 }
568
569 /* ------------------------------- */
570 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
571 {
572     struct vol      *vol;
573     struct dir      *dir;
574     struct path         *s_path;
575     char        *upath;
576     int         did, rc;
577     u_int16_t       vid;
578
579     *rbuflen = 0;
580     ibuf += 2;
581
582     memcpy( &vid, ibuf, sizeof( vid ));
583     ibuf += sizeof( vid );
584     if (NULL == ( vol = getvolbyvid( vid )) ) {
585         return( AFPERR_PARAM );
586     }
587
588     if (vol->v_flags & AFPVOL_RO)
589         return AFPERR_VLOCK;
590
591     memcpy( &did, ibuf, sizeof( did ));
592     ibuf += sizeof( int );
593
594     if (NULL == ( dir = dirlookup( vol, did )) ) {
595         return afp_errno;
596     }
597
598     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
599         return get_afp_errno(AFPERR_NOOBJ);
600     }
601
602     upath = s_path->u_name;
603     if ( path_isadir( s_path) ) {
604         if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
605             rc = AFPERR_ACCESS;
606         else
607             rc = deletecurdir( vol);
608     } else if (of_findname(s_path)) {
609         rc = AFPERR_BUSY;
610     } else {
611         /* it's a file st_valid should always be true
612          * only test for ENOENT because EACCES needs
613          * to read meta data in deletefile
614          */
615         if (s_path->st_valid && s_path->st_errno == ENOENT) {
616             rc = AFPERR_NOOBJ;
617         }
618         else {
619             rc = deletefile(vol, -1, upath, 1);
620
621             struct dir *cachedfile;
622             if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
623                 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
624                 dir_free(cachedfile);
625             }
626         }
627     }
628     if ( rc == AFP_OK ) {
629         curdir->d_offcnt--;
630         setvoltime(obj, vol );
631     }
632
633     return( rc );
634 }
635 /* ------------------------ */
636 char *absupath(const struct vol *vol, struct dir *dir, char *u)
637 {
638     static char pathbuf[MAXPATHLEN + 1];
639     bstring path;
640
641     if (u == NULL || dir == NULL || vol == NULL)
642         return NULL;
643
644     if ((path = bstrcpy(dir->d_fullpath)) == NULL)
645         return NULL;
646     if (bcatcstr(path, "/") != BSTR_OK)
647         return NULL;
648     if (bcatcstr(path, u) != BSTR_OK)
649         return NULL;
650     if (path->slen > MAXPATHLEN)
651         return NULL;
652
653     LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
654
655     strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
656     bdestroy(path);
657
658     return(pathbuf);
659 }
660
661 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
662 {
663     if (vol == NULL || dir == NULL || name == NULL)
664         return NULL;
665     return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
666 }
667
668 /* ------------------------- */
669 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
670 {
671     struct vol  *vol;
672     struct dir  *sdir, *ddir;
673     int         isdir;
674     char    *oldname, *newname;
675     struct path *path;
676     int     did;
677     int     pdid;
678     int         plen;
679     u_int16_t   vid;
680     int         rc;
681 #ifdef DROPKLUDGE
682     int     retvalue;
683 #endif /* DROPKLUDGE */
684     int     sdir_fd = -1;
685
686
687     *rbuflen = 0;
688     ibuf += 2;
689
690     memcpy( &vid, ibuf, sizeof( vid ));
691     ibuf += sizeof( vid );
692     if (NULL == ( vol = getvolbyvid( vid )) ) {
693         return( AFPERR_PARAM );
694     }
695
696     if (vol->v_flags & AFPVOL_RO)
697         return AFPERR_VLOCK;
698
699     /* source did followed by dest did */
700     memcpy( &did, ibuf, sizeof( did ));
701     ibuf += sizeof( int );
702     if (NULL == ( sdir = dirlookup( vol, did )) ) {
703         return afp_errno; /* was AFPERR_PARAM */
704     }
705
706     memcpy( &did, ibuf, sizeof( did ));
707     ibuf += sizeof( int );
708
709     /* source pathname */
710     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
711         return get_afp_errno(AFPERR_NOOBJ);
712     }
713
714     sdir = curdir;
715     newname = obj->newtmp;
716     oldname = obj->oldtmp;
717
718     isdir = path_isadir(path);
719     if ( *path->m_name != '\0' ) {
720         if (isdir) {
721             sdir = path->d_dir;
722         }
723         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
724     } else {
725         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
726     }
727
728 #ifdef HAVE_ATFUNCS
729     if ((sdir_fd = open(".", O_RDONLY)) == -1)
730         return AFPERR_MISC;
731 #endif
732
733     /* get the destination directory */
734     if (NULL == ( ddir = dirlookup( vol, did )) ) {
735         rc = afp_errno; /*  was AFPERR_PARAM */
736         goto exit;
737     }
738     if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
739         rc = AFPERR_NOOBJ;
740         goto exit;
741     }
742     pdid = curdir->d_did;
743     if ( *path->m_name != '\0' ) {
744         rc = path_error(path, AFPERR_NOOBJ);
745         goto exit;
746     }
747
748     /* one more place where we know about path type */
749     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
750         rc = AFPERR_PARAM;
751         goto exit;
752     }
753
754     if (!plen) {
755         strcpy(newname, oldname);
756     }
757
758     /* This does the work */
759     LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
760         oldname, newname, isdir);
761     rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
762
763     if ( rc == AFP_OK ) {
764         char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
765
766         if (NULL == upath) {
767             rc = AFPERR_PARAM;
768             goto exit;
769         }
770         curdir->d_offcnt++;
771         sdir->d_offcnt--;
772 #ifdef DROPKLUDGE
773         if (vol->v_flags & AFPVOL_DROPBOX) {
774             /* FIXME did is not always the source id */
775             if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
776                 rc = retvalue;
777                 goto exit;
778             }
779         }
780         else
781 #endif /* DROPKLUDGE */
782             /* if unix priv don't try to match perm with dest folder */
783             if (!isdir && !vol_unix_priv(vol)) {
784                 int  admode = ad_mode("", 0777) | vol->v_fperm;
785
786                 setfilmode(upath, admode, NULL, vol->v_umask);
787                 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
788             }
789         setvoltime(obj, vol );
790     }
791
792 exit:
793 #ifdef HAVE_ATFUNCS
794     if (sdir_fd != -1)
795         close(sdir_fd);
796 #endif
797
798     return( rc );
799 }
800
801 int veto_file(const char*veto_str, const char*path)
802 /* given a veto_str like "abc/zxc/" and path "abc", return 1
803  * veto_str should be '/' delimited
804  * if path matches any one of the veto_str elements exactly, then 1 is returned
805  * otherwise, 0 is returned.
806  */
807 {
808     int i;  /* index to veto_str */
809     int j;  /* index to path */
810
811     if ((veto_str == NULL) || (path == NULL))
812         return 0;
813
814     for(i=0, j=0; veto_str[i] != '\0'; i++) {
815         if (veto_str[i] == '/') {
816             if ((j>0) && (path[j] == '\0')) {
817                 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
818                 return 1;
819             }
820             j = 0;
821         } else {
822             if (veto_str[i] != path[j]) {
823                 while ((veto_str[i] != '/')
824                        && (veto_str[i] != '\0'))
825                     i++;
826                 j = 0;
827                 continue;
828             }
829             j++;
830         }
831     }
832     return 0;
833 }
834