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