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