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