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