]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/filedir.c
fce: FCE version 2 with new event types and new config options
[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(const AFPObj *obj,
223                          struct vol *vol,
224                          struct dir *sdir,
225                          int sdir_fd,
226                          char *oldname,
227                          char *newname,
228                          int isdir)
229 {
230     char            *oldunixname = NULL;
231     char            *upath;
232     int             rc;
233     struct stat     *st, nst;
234     int             adflags;
235     struct adouble      ad;
236     struct adouble      *adp;
237     struct ofork        *opened = NULL;
238     struct path     path;
239     cnid_t          id;
240     int             cwd_fd = -1;
241
242     ad_init(&ad, vol);
243     adp = &ad;
244     adflags = 0;
245
246     if (!isdir) {
247         if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding(vol->v_obj)))) == NULL)
248             return AFPERR_PARAM; /* can't convert */
249         AFP_CNID_START("cnid_get");
250         id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
251         AFP_CNID_DONE();
252
253 #ifndef HAVE_ATFUNCS
254         /* Need full path */
255         free(oldunixname);
256         if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
257             return AFPERR_PARAM; /* pathname too long */
258 #endif /* HAVE_ATFUNCS */
259
260         path.st_valid = 0;
261         path.u_name = oldunixname;
262
263 #ifdef HAVE_ATFUNCS
264         opened = of_findnameat(sdir_fd, &path);
265 #else
266         opened = of_findname(vol, &path);
267 #endif /* HAVE_ATFUNCS */
268
269         if (opened) {
270             /* reuse struct adouble so it won't break locks */
271             adp = opened->of_ad;
272         }
273     } else {
274         id = sdir->d_did; /* we already have the CNID */
275         if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
276             return AFPERR_PARAM;
277         adflags = ADFLAGS_DIR;
278     }
279
280     /*
281      * oldunixname now points to either
282      *   a) full pathname of the source fs object (if renameat is not available)
283      *   b) the oldname (renameat is available)
284      * we are in the dest folder so we need to use 
285      *   a) oldunixname for ad_open
286      *   b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
287      */
288
289     if (sdir_fd != -1) {
290         if ((cwd_fd = open(".", O_RDONLY)) == -1)
291             return AFPERR_MISC;
292         if (fchdir(sdir_fd) != 0) {
293             rc = AFPERR_MISC;
294             goto exit;
295         }
296     }
297     if (!ad_metadata(oldunixname, adflags, adp)) {
298         uint16_t bshort;
299
300         ad_getattr(adp, &bshort);
301         
302         ad_close(adp, ADFLAGS_HF);
303         if (!(vol->v_ignattr & ATTRBIT_NORENAME) && (bshort & htons(ATTRBIT_NORENAME))) {
304             rc = AFPERR_OLOCK;
305             goto exit;
306         }
307     }
308     if (sdir_fd != -1) {
309         if (fchdir(cwd_fd) != 0) {
310             LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
311             rc = AFPERR_MISC;
312             goto exit;
313         }
314     }
315
316     if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding(vol->v_obj)))){ 
317         rc = AFPERR_PARAM;
318         goto exit;
319     }
320     path.u_name = upath;
321     st = &path.st;
322     if (0 != (rc = check_name(vol, upath))) {
323         goto exit;
324     }
325
326     if (isdir)
327         LOG(log_debug, logtype_afpd,
328             "moveandrename(\"%s\" -> \"%s/%s\")",
329             oldunixname, bdata(curdir->d_fullpath), upath);
330     else
331         LOG(log_debug, logtype_afpd,
332             "moveandrename(\"%s/%s\" -> \"%s/%s\")",
333             bdata(sdir->d_fullpath), oldunixname, bdata(curdir->d_fullpath), upath);
334
335     /* source == destination. we just silently accept this. */
336     if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
337         if (strcmp(oldname, newname) == 0) {
338             rc = AFP_OK;
339             goto exit;
340         }
341
342         if (stat(upath, st) == 0) {
343             if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
344                 /* not the same file */
345                 rc = AFPERR_EXIST;
346                 goto exit;
347             }
348             errno = 0;
349         }
350     } else if (stat(upath, st ) == 0) {
351         rc = AFPERR_EXIST;
352         goto exit;
353     }
354
355     if ( !isdir ) {
356         path.st_valid = 1;
357         path.st_errno = errno;
358         if (of_findname(vol, &path)) {
359             rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
360         } else {
361             rc = renamefile(vol, curdir, sdir_fd, oldunixname, upath, newname, adp );
362             if (rc == AFP_OK)
363                 of_rename(vol, opened, sdir, oldname, curdir, newname);
364         }
365     } else {
366         rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
367     }
368     if ( rc == AFP_OK && id ) {
369         /* renaming may have moved the file/dir across a filesystem */
370         if (stat(upath, st) < 0) {
371             rc = AFPERR_MISC;
372             goto exit;
373         }
374
375         /* Remove it from the cache */
376         struct dir *cacheddir = dircache_search_by_did(vol, id);
377         if (cacheddir) {
378             LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
379             (void)dir_remove(vol, cacheddir);
380         }
381
382         /* Fixup adouble info */
383         if (!ad_metadata(upath, adflags, adp)) {
384             ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
385             ad_flush(adp);
386             ad_close(adp, ADFLAGS_HF);
387         }
388
389         /* fix up the catalog entry */
390         AFP_CNID_START("cnid_update");
391         cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
392         AFP_CNID_DONE();
393
394         /* Send FCE event */
395         if (isdir) {
396             fce_register(obj, FCE_DIR_MOVE, fullpathname(upath), oldunixname);
397         } else {
398             bstring srcpath = bformat("%s/%s", bdata(sdir->d_fullpath), oldunixname);
399             fce_register(obj, FCE_FILE_MOVE, fullpathname(upath), bdata(srcpath));
400             bdestroy(srcpath);
401         }
402     }
403
404 exit:
405     if (cwd_fd != -1)
406         close(cwd_fd);
407     if (oldunixname)
408         free(oldunixname);
409     return rc;
410 }
411
412 /* -------------------------------------------- */
413 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
414 {
415     struct vol  *vol;
416     struct dir  *sdir;
417     char        *oldname, *newname;
418     struct path *path;
419     uint32_t   did;
420     int         plen;
421     uint16_t   vid;
422     int         isdir = 0;
423     int         rc;
424
425     *rbuflen = 0;
426     ibuf += 2;
427
428     memcpy( &vid, ibuf, sizeof( vid ));
429     ibuf += sizeof( vid );
430     if (NULL == ( vol = getvolbyvid( vid )) ) {
431         return( AFPERR_PARAM );
432     }
433
434     if (vol->v_flags & AFPVOL_RO)
435         return AFPERR_VLOCK;
436
437     memcpy( &did, ibuf, sizeof( did ));
438     ibuf += sizeof( did );
439     if (NULL == ( sdir = dirlookup( vol, did )) ) {
440         return afp_errno;
441     }
442
443     /* source pathname */
444     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
445         return get_afp_errno(AFPERR_NOOBJ);
446     }
447
448     sdir = curdir;
449     newname = obj->newtmp;
450     oldname = obj->oldtmp;
451     isdir = path_isadir(path);
452     if ( *path->m_name != '\0' ) {
453         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
454         if (isdir) {
455             /* curdir parent dir, need to move sdir back */
456             sdir = path->d_dir;
457         }
458     }
459     else {
460         if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
461             return( AFPERR_NORENAME );
462         }
463         /* move to destination dir */
464         if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
465             return afp_errno;
466         }
467         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
468     }
469
470     /* another place where we know about the path type */
471     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
472         return( AFPERR_PARAM );
473     }
474
475     if (!plen) {
476         return AFP_OK; /* newname == oldname same dir */
477     }
478     
479     rc = moveandrename(obj, vol, sdir, -1, oldname, newname, isdir);
480     if ( rc == AFP_OK ) {
481         setvoltime(obj, vol );
482     }
483
484     return( rc );
485 }
486
487 /* 
488  * Recursivley delete vetoed files and directories if the volume option is set
489  *
490  * @param vol   (r) volume handle
491  * @param upath (r) path of directory
492  *
493  * If the volume option delete veto files is set, this function recursively scans the
494  * directory "upath" for vetoed files and tries deletes these, the it will try to delete
495  * the directory. That may fail if the directory contains normal files that aren't vetoed.
496  *
497  * @returns 0 if the directory upath and all of its contents were deleted, otherwise -1.
498  *            If the volume option is not set it returns -1.
499  */
500 int delete_vetoed_files(struct vol *vol, const char *upath, bool in_vetodir)
501 {
502     EC_INIT;
503     DIR            *dp = NULL;
504     struct dirent  *de;
505     struct stat     sb;
506     int             pwd = -1;
507     bool            vetoed;
508
509     if (!(vol->v_flags & AFPVOL_DELVETO))
510         return -1;
511
512     EC_NEG1( pwd = open(".", O_RDONLY));
513     EC_ZERO( chdir(upath) );
514     EC_NULL( dp = opendir(".") );
515
516     while ((de = readdir(dp))) {
517         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
518             continue;
519
520         if (stat(de->d_name, &sb) != 0) {
521             LOG(log_error, logtype_afpd, "delete_vetoed_files(\"%s/%s\"): %s",
522                 upath, de->d_name, strerror(errno));
523                 EC_EXIT_STATUS(AFPERR_DIRNEMPT);
524         }
525
526         if (in_vetodir || veto_file(vol->v_veto, de->d_name))
527             vetoed = true;
528         else
529             vetoed = false;
530
531         if (vetoed) {
532             LOG(log_debug, logtype_afpd, "delete_vetoed_files(\"%s/%s\"): deleting vetoed file",
533                 upath, de->d_name);
534             switch (sb.st_mode & S_IFMT) {
535             case S_IFDIR:
536                 /* recursion */
537                 EC_ZERO( delete_vetoed_files(vol, de->d_name, vetoed));
538                 break;
539             case S_IFREG:
540             case S_IFLNK:
541                 EC_ZERO( netatalk_unlink(de->d_name) );
542                 break;
543             default:
544                 break;
545             }
546         }
547     }
548
549     EC_ZERO_LOG( fchdir(pwd) );
550     pwd = -1;
551     EC_ZERO_LOG( rmdir(upath) );
552
553 EC_CLEANUP:
554     if (dp)
555         closedir(dp);
556     if (pwd != -1) {
557         if (fchdir(pwd) != 0)
558             ret = -1;
559     }
560
561     EC_EXIT;
562 }
563
564 /* ------------------------------- */
565 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
566 {
567     struct vol  *vol;
568     struct dir  *dir;
569     struct path *s_path;
570     char        *upath;
571     int         did;
572     int         rc = AFP_OK;
573     uint16_t    vid;
574
575     *rbuflen = 0;
576     ibuf += 2;
577
578     memcpy( &vid, ibuf, sizeof( vid ));
579     ibuf += sizeof( vid );
580     if (NULL == ( vol = getvolbyvid( vid )) ) {
581         return( AFPERR_PARAM );
582     }
583
584     if (vol->v_flags & AFPVOL_RO)
585         return AFPERR_VLOCK;
586
587     memcpy( &did, ibuf, sizeof( did ));
588     ibuf += sizeof( int );
589
590     if (NULL == ( dir = dirlookup( vol, did )) ) {
591         return afp_errno;
592     }
593
594     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
595         return get_afp_errno(AFPERR_NOOBJ);
596     }
597
598     upath = s_path->u_name;
599     if (path_isadir(s_path)) {
600         if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
601             if (vol->v_adouble == AD_VERSION2)
602                 return AFPERR_ACCESS;
603             if (*s_path->m_name == '\0' && curdir->d_did == DIRDID_ROOT)
604                 return AFPERR_ACCESS;
605             if (rmdir(upath) != 0) {
606                 switch (errno) {
607                 case ENOTEMPTY:
608                     if (delete_vetoed_files(vol, upath, false) != 0)
609                         return AFPERR_DIRNEMPT;
610                     break;
611                 case EACCES:
612                     return AFPERR_ACCESS;
613                 default:
614                     return AFPERR_MISC;
615                 }
616             }
617             struct dir *deldir;
618             cnid_t delcnid = CNID_INVALID;
619             if ((deldir = dircache_search_by_name(vol, curdir, upath, strlen(upath)))) {
620                 delcnid = deldir->d_did;
621                 dir_remove(vol, deldir);
622             }
623             if (delcnid == CNID_INVALID) {
624                 AFP_CNID_START("cnid_get");
625                 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
626                 AFP_CNID_DONE();
627             }
628             if (delcnid != CNID_INVALID) {
629                 AFP_CNID_START("cnid_delete");
630                 cnid_delete(vol->v_cdb, delcnid);
631                 AFP_CNID_DONE();
632             }
633             fce_register(obj, FCE_DIR_DELETE, fullpathname(upath), NULL);
634         } else {
635             /* we have to cache this, the structs are lost in deletcurdir*/
636             /* but we need the positive returncode to send our event */
637             bstring dname;
638             if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
639                 return AFPERR_MISC;
640             if ((rc = deletecurdir(vol)) == AFP_OK)
641                 fce_register(obj, FCE_DIR_DELETE, fullpathname(cfrombstr(dname)), NULL);
642             bdestroy(dname);
643         }
644     } else if (of_findname(vol, s_path)) {
645         rc = AFPERR_BUSY;
646     } else {
647         /* it's a file st_valid should always be true
648          * only test for ENOENT because EACCES needs
649          * to read meta data in deletefile
650          */
651         if (s_path->st_valid && s_path->st_errno == ENOENT) {
652             rc = AFPERR_NOOBJ;
653         } else {
654             if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
655                                 fce_register(obj, FCE_FILE_DELETE, fullpathname(upath), NULL);
656                 if (vol->v_tm_used < s_path->st.st_size)
657                     vol->v_tm_used = 0;
658                 else 
659                     vol->v_tm_used -= s_path->st.st_size;
660             }
661             struct dir *cachedfile;
662             if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
663                 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
664                 dir_free(cachedfile);
665             }
666         }
667     }
668     if ( rc == AFP_OK ) {
669         curdir->d_offcnt--;
670         setvoltime(obj, vol );
671     }
672
673     return( rc );
674 }
675 /* ------------------------ */
676 char *absupath(const struct vol *vol, struct dir *dir, char *u)
677 {
678     static char pathbuf[MAXPATHLEN + 1];
679     bstring path;
680
681     if (u == NULL || dir == NULL || vol == NULL)
682         return NULL;
683
684     if ((path = bstrcpy(dir->d_fullpath)) == NULL)
685         return NULL;
686     if (bcatcstr(path, "/") != BSTR_OK)
687         return NULL;
688     if (bcatcstr(path, u) != BSTR_OK)
689         return NULL;
690     if (path->slen > MAXPATHLEN) {
691         bdestroy(path);
692         return NULL;
693     }
694
695     LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
696
697     strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
698     bdestroy(path);
699
700     return(pathbuf);
701 }
702
703 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
704 {
705     if (vol == NULL || dir == NULL || name == NULL)
706         return NULL;
707     return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding(vol->v_obj)));
708 }
709
710 /* ------------------------- */
711 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
712 {
713     struct vol  *vol;
714     struct dir  *sdir, *ddir;
715     int         isdir;
716     char    *oldname, *newname;
717     struct path *path;
718     int     did;
719     int     pdid;
720     int         plen;
721     uint16_t   vid;
722     int         rc;
723     int     sdir_fd = -1;
724
725
726     *rbuflen = 0;
727     ibuf += 2;
728
729     memcpy( &vid, ibuf, sizeof( vid ));
730     ibuf += sizeof( vid );
731     if (NULL == ( vol = getvolbyvid( vid )) ) {
732         return( AFPERR_PARAM );
733     }
734
735     if (vol->v_flags & AFPVOL_RO)
736         return AFPERR_VLOCK;
737
738     /* source did followed by dest did */
739     memcpy( &did, ibuf, sizeof( did ));
740     ibuf += sizeof( int );
741     if (NULL == ( sdir = dirlookup( vol, did )) ) {
742         return afp_errno; /* was AFPERR_PARAM */
743     }
744
745     memcpy( &did, ibuf, sizeof( did ));
746     ibuf += sizeof( int );
747
748     /* source pathname */
749     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
750         return get_afp_errno(AFPERR_NOOBJ);
751     }
752
753     sdir = curdir;
754     newname = obj->newtmp;
755     oldname = obj->oldtmp;
756
757     isdir = path_isadir(path);
758     if ( *path->m_name != '\0' ) {
759         if (isdir) {
760             sdir = path->d_dir;
761         }
762         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
763     } else {
764         memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
765     }
766
767 #ifdef HAVE_ATFUNCS
768     if ((sdir_fd = open(".", O_RDONLY)) == -1)
769         return AFPERR_MISC;
770 #endif
771
772     /* get the destination directory */
773     if (NULL == ( ddir = dirlookup( vol, did )) ) {
774         rc = afp_errno; /*  was AFPERR_PARAM */
775         goto exit;
776     }
777     if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
778         rc = AFPERR_NOOBJ;
779         goto exit;
780     }
781     pdid = curdir->d_did;
782     if ( *path->m_name != '\0' ) {
783         rc = path_error(path, AFPERR_NOOBJ);
784         goto exit;
785     }
786
787     /* one more place where we know about path type */
788     if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
789         rc = AFPERR_PARAM;
790         goto exit;
791     }
792
793     if (!plen) {
794         strcpy(newname, oldname);
795     }
796
797     /* This does the work */
798     LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
799         oldname, newname, isdir);
800     rc = moveandrename(obj, vol, sdir, sdir_fd, oldname, newname, isdir);
801
802     if ( rc == AFP_OK ) {
803         char *upath = mtoupath(vol, newname, pdid, utf8_encoding(obj));
804
805         if (NULL == upath) {
806             rc = AFPERR_PARAM;
807             goto exit;
808         }
809         /* if unix priv don't try to match perm with dest folder */
810         if (!isdir && !vol_unix_priv(vol)) {
811             int  admode = ad_mode("", 0777) | vol->v_fperm;
812
813             setfilmode(vol, upath, admode, path->st_valid ? &path->st : NULL);
814             vol->vfs->vfs_setfilmode(vol, upath, admode, path->st_valid ? &path->st : NULL);
815         }
816         setvoltime(obj, vol );
817     }
818
819 exit:
820 #ifdef HAVE_ATFUNCS
821     if (sdir_fd != -1)
822         close(sdir_fd);
823 #endif
824
825     return( rc );
826 }
827
828 int veto_file(const char*veto_str, const char*path)
829 /* given a veto_str like "abc/zxc/" and path "abc", return 1
830  * veto_str should be '/' delimited
831  * if path matches any one of the veto_str elements exactly, then 1 is returned
832  * otherwise, 0 is returned.
833  */
834 {
835     int i;  /* index to veto_str */
836     int j;  /* index to path */
837
838     if ((veto_str == NULL) || (path == NULL))
839         return 0;
840
841     for(i=0, j=0; veto_str[i] != '\0'; i++) {
842         if (veto_str[i] == '/') {
843             if ((j>0) && (path[j] == '\0')) {
844                 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
845                 return 1;
846             }
847             j = 0;
848         } else {
849             if (veto_str[i] != path[j]) {
850                 while ((veto_str[i] != '/')
851                        && (veto_str[i] != '\0'))
852                     i++;
853                 j = 0;
854                 continue;
855             }
856             j++;
857         }
858     }
859     return 0;
860 }
861