]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/filedir.c
Remove bdb env on exit
[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 #include <atalk/globals.h>
44 #include <atalk/fce_api.h>
45
46 #include "directory.h"
47 #include "dircache.h"
48 #include "desktop.h"
49 #include "volume.h"
50 #include "fork.h"
51 #include "file.h"
52 #include "filedir.h"
53 #include "unix.h"
54
55 #ifdef DROPKLUDGE
56 int matchfile2dirperms(
57 /* Since it's kinda' big; I decided against an
58    inline function */
59     char    *upath,
60     struct vol  *vol,
61     int     did)
62 /* The below code changes the way file ownership is determined in the name of
63    fixing dropboxes.  It has known security problem.  See the netatalk FAQ for
64    more information */
65 {
66     struct stat st, sb;
67     struct dir  *dir;
68     char    *adpath;
69     uid_t       uid;
70     int         ret = AFP_OK;
71 #ifdef DEBUG
72     LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
73 #endif
74
75     if (stat(upath, &st ) < 0) {
76         LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
77         return AFPERR_NOOBJ ;
78     }
79
80     adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
81     /* FIXME dirsearch doesn't move cwd to did ! */
82     if (( dir = dirlookup( vol, did )) == NULL ) {
83         LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
84         ret = AFPERR_NOOBJ;
85     }
86     else if (stat(".", &sb) < 0) {
87         LOG(log_error, logtype_afpd,
88             "matchfile2dirperms: Error checking directory \"%s\": %s",
89             dir->d_m_name, strerror(errno));
90         ret = AFPERR_NOOBJ;
91     }
92     else {
93         uid=geteuid();
94         if ( uid != sb.st_uid )
95         {
96             seteuid(0);
97             if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
98             {
99                 LOG(log_error, logtype_afpd,
100                     "matchfile2dirperms(%s): Error changing owner/gid: %s",
101                     upath, strerror(errno));
102                 ret = AFPERR_ACCESS;
103             }
104             else if ((!S_ISLNK(st->st_mode)) && (chmod_acl(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_acl(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             bstring dname;
611             if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
612                 return AFPERR_MISC;
613             if ((rc = deletecurdir(vol)) == AFP_OK)
614                 fce_register_delete_dir(cfrombstr(dname));
615             bdestroy(dname);
616         }
617     } else if (of_findname(s_path)) {
618         rc = AFPERR_BUSY;
619     } else {
620         /* it's a file st_valid should always be true
621          * only test for ENOENT because EACCES needs
622          * to read meta data in deletefile
623          */
624         if (s_path->st_valid && s_path->st_errno == ENOENT) {
625             rc = AFPERR_NOOBJ;
626         } else {
627             if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
628                                 fce_register_delete_file( s_path );
629                 if (vol->v_tm_used < s_path->st.st_size)
630                     vol->v_tm_used = 0;
631                 else 
632                     vol->v_tm_used -= s_path->st.st_size;
633             }
634             struct dir *cachedfile;
635             if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
636                 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
637                 dir_free(cachedfile);
638             }
639         }
640     }
641     if ( rc == AFP_OK ) {
642         curdir->d_offcnt--;
643         setvoltime(obj, vol );
644     }
645
646     return( rc );
647 }
648 /* ------------------------ */
649 char *absupath(const struct vol *vol, struct dir *dir, char *u)
650 {
651     static char pathbuf[MAXPATHLEN + 1];
652     bstring path;
653
654     if (u == NULL || dir == NULL || vol == NULL)
655         return NULL;
656
657     if ((path = bstrcpy(dir->d_fullpath)) == NULL)
658         return NULL;
659     if (bcatcstr(path, "/") != BSTR_OK)
660         return NULL;
661     if (bcatcstr(path, u) != BSTR_OK)
662         return NULL;
663     if (path->slen > MAXPATHLEN)
664         return NULL;
665
666     LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
667
668     strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
669     bdestroy(path);
670
671     return(pathbuf);
672 }
673
674 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
675 {
676     if (vol == NULL || dir == NULL || name == NULL)
677         return NULL;
678     return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
679 }
680
681 /* ------------------------- */
682 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
683 {
684     struct vol  *vol;
685     struct dir  *sdir, *ddir;
686     int         isdir;
687     char    *oldname, *newname;
688     struct path *path;
689     int     did;
690     int     pdid;
691     int         plen;
692     u_int16_t   vid;
693     int         rc;
694 #ifdef DROPKLUDGE
695     int     retvalue;
696 #endif /* DROPKLUDGE */
697     int     sdir_fd = -1;
698
699
700     *rbuflen = 0;
701     ibuf += 2;
702
703     memcpy( &vid, ibuf, sizeof( vid ));
704     ibuf += sizeof( vid );
705     if (NULL == ( vol = getvolbyvid( vid )) ) {
706         return( AFPERR_PARAM );
707     }
708
709     if (vol->v_flags & AFPVOL_RO)
710         return AFPERR_VLOCK;
711
712     /* source did followed by dest did */
713     memcpy( &did, ibuf, sizeof( did ));
714     ibuf += sizeof( int );
715     if (NULL == ( sdir = dirlookup( vol, did )) ) {
716         return afp_errno; /* was AFPERR_PARAM */
717     }
718
719     memcpy( &did, ibuf, sizeof( did ));
720     ibuf += sizeof( int );
721
722     /* source pathname */
723     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
724         return get_afp_errno(AFPERR_NOOBJ);
725     }
726
727     sdir = curdir;
728     newname = obj->newtmp;
729     oldname = obj->oldtmp;
730
731     isdir = path_isadir(path);
732     if ( *path->m_name != '\0' ) {
733         if (isdir) {
734             sdir = path->d_dir;
735         }
736         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
737     } else {
738         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
739     }
740
741 #ifdef HAVE_ATFUNCS
742     if ((sdir_fd = open(".", O_RDONLY)) == -1)
743         return AFPERR_MISC;
744 #endif
745
746     /* get the destination directory */
747     if (NULL == ( ddir = dirlookup( vol, did )) ) {
748         rc = afp_errno; /*  was AFPERR_PARAM */
749         goto exit;
750     }
751     if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
752         rc = AFPERR_NOOBJ;
753         goto exit;
754     }
755     pdid = curdir->d_did;
756     if ( *path->m_name != '\0' ) {
757         rc = path_error(path, AFPERR_NOOBJ);
758         goto exit;
759     }
760
761     /* one more place where we know about path type */
762     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
763         rc = AFPERR_PARAM;
764         goto exit;
765     }
766
767     if (!plen) {
768         strcpy(newname, oldname);
769     }
770
771     /* This does the work */
772     LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
773         oldname, newname, isdir);
774     rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
775
776     if ( rc == AFP_OK ) {
777         char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
778
779         if (NULL == upath) {
780             rc = AFPERR_PARAM;
781             goto exit;
782         }
783         curdir->d_offcnt++;
784         sdir->d_offcnt--;
785 #ifdef DROPKLUDGE
786         if (vol->v_flags & AFPVOL_DROPBOX) {
787             /* FIXME did is not always the source id */
788             if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
789                 rc = retvalue;
790                 goto exit;
791             }
792         }
793         else
794 #endif /* DROPKLUDGE */
795             /* if unix priv don't try to match perm with dest folder */
796             if (!isdir && !vol_unix_priv(vol)) {
797                 int  admode = ad_mode("", 0777) | vol->v_fperm;
798
799                 setfilmode(upath, admode, NULL, vol->v_umask);
800                 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
801             }
802         setvoltime(obj, vol );
803     }
804
805 exit:
806 #ifdef HAVE_ATFUNCS
807     if (sdir_fd != -1)
808         close(sdir_fd);
809 #endif
810
811     return( rc );
812 }
813
814 int veto_file(const char*veto_str, const char*path)
815 /* given a veto_str like "abc/zxc/" and path "abc", return 1
816  * veto_str should be '/' delimited
817  * if path matches any one of the veto_str elements exactly, then 1 is returned
818  * otherwise, 0 is returned.
819  */
820 {
821     int i;  /* index to veto_str */
822     int j;  /* index to path */
823
824     if ((veto_str == NULL) || (path == NULL))
825         return 0;
826
827     for(i=0, j=0; veto_str[i] != '\0'; i++) {
828         if (veto_str[i] == '/') {
829             if ((j>0) && (path[j] == '\0')) {
830                 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
831                 return 1;
832             }
833             j = 0;
834         } else {
835             if (veto_str[i] != path[j]) {
836                 while ((veto_str[i] != '/')
837                        && (veto_str[i] != '\0'))
838                     i++;
839                 j = 0;
840                 continue;
841             }
842             j++;
843         }
844     }
845     return 0;
846 }
847