]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/filedir.c
Fixed conflicts from merge
[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
44 #include "directory.h"
45 #include "dircache.h"
46 #include "desktop.h"
47 #include "volume.h"
48 #include "fork.h"
49 #include "file.h"
50 #include "globals.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            *p;
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;
342
343     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
344     adp = &ad;
345     adflags = 0;
346
347     if (!isdir) {
348         if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
349             return AFPERR_PARAM; /* can't convert */
350
351 #ifndef HAVE_RENAMEAT
352         /* Need full path */
353         id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
354         p = ctoupath( vol, sdir, oldname );
355         if (!p)
356             return AFPERR_PARAM; /* pathname too long */
357 #endif /* HAVE_RENAMEAT */
358
359         path.st_valid = 0;
360         path.u_name = p;
361 #ifdef HAVE_RENAMEAT
362         opened = of_findnameat(sdir_fd, &path);
363 #else
364         opened = of_findname(&path);
365 #endif /* HAVE_RENAMEAT */
366         if (opened) {
367             /* reuse struct adouble so it won't break locks */
368             adp = opened->of_ad;
369         }
370     } else {
371         id = sdir->d_did; /* we already have the CNID */
372         p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
373         if (!p) {
374             return AFPERR_PARAM;
375         }
376         adflags = ADFLAGS_DIR;
377     }
378
379     /*
380      * p now points to either
381      *   a) full pathname of the source fs object (if renameat is not available)
382      *   b) the oldname (renameat is available)
383      * we are in the dest folder so we need to use 
384      *   a) p for ad_open
385      *   b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
386      */
387
388     if (sdir_fd != -1) {
389         if ((cwd_fd = open(".", O_RDONLY)) == -1)
390             return AFPERR_MISC;
391         if (fchdir(sdir_fd) != 0)
392             return AFPERR_MISC;
393     }
394     if (!ad_metadata(p, adflags, adp)) {
395         u_int16_t bshort;
396
397         ad_getattr(adp, &bshort);
398         ad_close_metadata( adp);
399         if ((bshort & htons(ATTRBIT_NORENAME)))
400             return(AFPERR_OLOCK);
401     }
402     if (sdir_fd != -1) {
403         if (fchdir(cwd_fd) != 0) {
404             LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
405             return AFPERR_MISC;
406         }
407     }
408
409     if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
410         return AFPERR_PARAM;
411     }
412     path.u_name = upath;
413     st = &path.st;
414     if (0 != (rc = check_name(vol, upath))) {
415         return  rc;
416     }
417
418     /* source == destination. we just silently accept this. */
419     if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
420         if (strcmp(oldname, newname) == 0)
421             return AFP_OK;
422
423         if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
424             if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
425                 /* not the same file */
426                 return AFPERR_EXIST;
427             }
428             errno = 0;
429         }
430     } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
431         return AFPERR_EXIST;
432
433     if ( !isdir ) {
434         path.st_valid = 1;
435         path.st_errno = errno;
436         if (of_findname(&path)) {
437             rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
438         } else {
439             rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
440             if (rc == AFP_OK)
441                 of_rename(vol, opened, sdir, oldname, curdir, newname);
442         }
443     } else {
444         rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
445     }
446     if ( rc == AFP_OK && id ) {
447         /* renaming may have moved the file/dir across a filesystem */
448         if (stat(upath, st) < 0)
449             return AFPERR_MISC;
450
451         if (dir_modify(vol,
452                        sdir,
453                        curdir->d_did,
454                        0,
455                        newname,
456                        upath,
457                        S_ISDIR(st->st_mode) ? curdir->d_fullpath : NULL) != 0) {
458             LOG(log_error, logtype_afpd, "moveandrename: dir_modify error: %s -> %s",
459                 p, upath);
460             return AFPERR_MISC;
461         }
462
463         /* fix up the catalog entry */
464         cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
465     }
466
467     return rc;
468 }
469
470 /* -------------------------------------------- */
471 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
472 {
473     struct vol  *vol;
474     struct dir  *sdir;
475     char        *oldname, *newname;
476     struct path *path;
477     u_int32_t   did;
478     int         plen;
479     u_int16_t   vid;
480     int         isdir = 0;
481     int         rc;
482
483     *rbuflen = 0;
484     ibuf += 2;
485
486     memcpy( &vid, ibuf, sizeof( vid ));
487     ibuf += sizeof( vid );
488     if (NULL == ( vol = getvolbyvid( vid )) ) {
489         return( AFPERR_PARAM );
490     }
491
492     if (vol->v_flags & AFPVOL_RO)
493         return AFPERR_VLOCK;
494
495     memcpy( &did, ibuf, sizeof( did ));
496     ibuf += sizeof( did );
497     if (NULL == ( sdir = dirlookup( vol, did )) ) {
498         return afp_errno;
499     }
500
501     /* source pathname */
502     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
503         return get_afp_errno(AFPERR_NOOBJ);
504     }
505
506     sdir = curdir;
507     newname = obj->newtmp;
508     oldname = obj->oldtmp;
509     isdir = path_isadir(path);
510     if ( *path->m_name != '\0' ) {
511         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
512         if (isdir) {
513             /* curdir parent dir, need to move sdir back */
514             sdir = path->d_dir;
515         }
516     }
517     else {
518         if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
519             return( AFPERR_NORENAME );
520         }
521         /* move to destination dir */
522         if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
523             return afp_errno;
524         }
525         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
526     }
527
528     /* another place where we know about the path type */
529     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
530         return( AFPERR_PARAM );
531     }
532
533     if (!plen) {
534         return AFP_OK; /* newname == oldname same dir */
535     }
536     
537     rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
538     if ( rc == AFP_OK ) {
539         setvoltime(obj, vol );
540     }
541
542     return( rc );
543 }
544
545 /* ------------------------------- */
546 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
547 {
548     struct vol      *vol;
549     struct dir      *dir;
550     struct path         *s_path;
551     char        *upath;
552     int         did, rc;
553     u_int16_t       vid;
554
555     *rbuflen = 0;
556     ibuf += 2;
557
558     memcpy( &vid, ibuf, sizeof( vid ));
559     ibuf += sizeof( vid );
560     if (NULL == ( vol = getvolbyvid( vid )) ) {
561         return( AFPERR_PARAM );
562     }
563
564     if (vol->v_flags & AFPVOL_RO)
565         return AFPERR_VLOCK;
566
567     memcpy( &did, ibuf, sizeof( did ));
568     ibuf += sizeof( int );
569
570     if (NULL == ( dir = dirlookup( vol, did )) ) {
571         return afp_errno;
572     }
573
574     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
575         return get_afp_errno(AFPERR_NOOBJ);
576     }
577
578     upath = s_path->u_name;
579     if ( path_isadir( s_path) ) {
580         if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
581             rc = AFPERR_ACCESS;
582         else
583             rc = deletecurdir( vol);
584     } else if (of_findname(s_path)) {
585         rc = AFPERR_BUSY;
586     } else {
587         /* it's a file st_valid should always be true
588          * only test for ENOENT because EACCES needs
589          * to read meta data in deletefile
590          */
591         if (s_path->st_valid && s_path->st_errno == ENOENT) {
592             rc = AFPERR_NOOBJ;
593         }
594         else {
595             rc = deletefile(vol, -1, upath, 1);
596
597             struct dir *cachedfile;
598             if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
599                 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
600                 dir_free(cachedfile);
601             }
602         }
603     }
604     if ( rc == AFP_OK ) {
605         curdir->offcnt--;
606         setvoltime(obj, vol );
607     }
608
609     return( rc );
610 }
611 /* ------------------------ */
612 char *absupath(const struct vol *vol, struct dir *dir, char *u)
613 {
614     static char pathbuf[MAXPATHLEN + 1];
615     bstring path;
616
617     if (u == NULL || dir == NULL || vol == NULL)
618         return NULL;
619
620     if ((path = bstrcpy(dir->d_fullpath)) == NULL)
621         return NULL;
622     if (bcatcstr(path, "/") != BSTR_OK)
623         return NULL;
624     if (bcatcstr(path, u) != BSTR_OK)
625         return NULL;
626     if (path->slen > MAXPATHLEN)
627         return NULL;
628
629     LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
630
631     strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
632     bdestroy(path);
633
634     return(pathbuf);
635 }
636
637 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
638 {
639     if (vol == NULL || dir == NULL || name == NULL)
640         return NULL;
641     return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
642 }
643
644 /* ------------------------- */
645 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
646 {
647     struct vol  *vol;
648     struct dir  *sdir, *ddir;
649     int         isdir;
650     char    *oldname, *newname;
651     struct path *path;
652     int     did;
653     int     pdid;
654     int         plen;
655     u_int16_t   vid;
656     int         rc;
657 #ifdef DROPKLUDGE
658     int     retvalue;
659 #endif /* DROPKLUDGE */
660     int     sdir_fd = -1;
661
662
663     *rbuflen = 0;
664     ibuf += 2;
665
666     memcpy( &vid, ibuf, sizeof( vid ));
667     ibuf += sizeof( vid );
668     if (NULL == ( vol = getvolbyvid( vid )) ) {
669         return( AFPERR_PARAM );
670     }
671
672     if (vol->v_flags & AFPVOL_RO)
673         return AFPERR_VLOCK;
674
675     /* source did followed by dest did */
676     memcpy( &did, ibuf, sizeof( did ));
677     ibuf += sizeof( int );
678     if (NULL == ( sdir = dirlookup( vol, did )) ) {
679         return afp_errno; /* was AFPERR_PARAM */
680     }
681
682     memcpy( &did, ibuf, sizeof( did ));
683     ibuf += sizeof( int );
684
685     /* source pathname */
686     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
687         return get_afp_errno(AFPERR_NOOBJ);
688     }
689
690     sdir = curdir;
691     newname = obj->newtmp;
692     oldname = obj->oldtmp;
693
694     isdir = path_isadir(path);
695     if ( *path->m_name != '\0' ) {
696         if (isdir) {
697             sdir = path->d_dir;
698         }
699         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
700     } else {
701         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
702     }
703
704 #ifdef HAVE_RENAMEAT
705     if ((sdir_fd = open(".", O_RDONLY)) == -1)
706         return AFPERR_MISC;
707 #endif
708
709     /* get the destination directory */
710     if (NULL == ( ddir = dirlookup( vol, did )) ) {
711         rc = afp_errno; /*  was AFPERR_PARAM */
712         goto exit;
713     }
714     if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
715         rc = AFPERR_NOOBJ;
716         goto exit;
717     }
718     pdid = curdir->d_did;
719     if ( *path->m_name != '\0' ) {
720         rc = path_error(path, AFPERR_NOOBJ);
721         goto exit;
722     }
723
724     /* one more place where we know about path type */
725     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
726         rc = AFPERR_PARAM;
727         goto exit;
728     }
729
730     if (!plen) {
731         strcpy(newname, oldname);
732     }
733
734     /* This does the work */
735     LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
736         oldname, newname, isdir);
737     rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
738
739     if ( rc == AFP_OK ) {
740         char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
741
742         if (NULL == upath) {
743             rc = AFPERR_PARAM;
744             goto exit;
745         }
746         curdir->offcnt++;
747         sdir->offcnt--;
748 #ifdef DROPKLUDGE
749         if (vol->v_flags & AFPVOL_DROPBOX) {
750             /* FIXME did is not always the source id */
751             if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
752                 rc = retvalue;
753                 goto exit;
754             }
755         }
756         else
757 #endif /* DROPKLUDGE */
758             /* if unix priv don't try to match perm with dest folder */
759             if (!isdir && !vol_unix_priv(vol)) {
760                 int  admode = ad_mode("", 0777) | vol->v_fperm;
761
762                 setfilmode(upath, admode, NULL, vol->v_umask);
763                 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
764             }
765         setvoltime(obj, vol );
766     }
767
768 exit:
769 #ifdef HAVE_RENAMEAT
770     if (sdir_fd != -1)
771         close(sdir_fd);
772 #endif
773
774     return( rc );
775 }
776
777 int veto_file(const char*veto_str, const char*path)
778 /* given a veto_str like "abc/zxc/" and path "abc", return 1
779  * veto_str should be '/' delimited
780  * if path matches any one of the veto_str elements exactly, then 1 is returned
781  * otherwise, 0 is returned.
782  */
783 {
784     int i;  /* index to veto_str */
785     int j;  /* index to path */
786
787     if ((veto_str == NULL) || (path == NULL))
788         return 0;
789
790     for(i=0, j=0; veto_str[i] != '\0'; i++) {
791         if (veto_str[i] == '/') {
792             if ((j>0) && (path[j] == '\0')) {
793                 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
794                 return 1;
795             }
796             j = 0;
797         } else {
798             if (veto_str[i] != path[j]) {
799                 while ((veto_str[i] != '/')
800                        && (veto_str[i] != '\0'))
801                     i++;
802                 j = 0;
803                 continue;
804             }
805             j++;
806         }
807     }
808     return 0;
809 }
810