2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/param.h>
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>
31 #include "directory.h"
40 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
48 uint16_t fbitmap, dbitmap, vid;
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
60 return( AFPERR_ACCESS );
63 memcpy( &did, ibuf, sizeof( did ));
64 ibuf += sizeof( did );
66 if (NULL == ( dir = dirlookup( vol, did )) ) {
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 );
77 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
78 return get_afp_errno(AFPERR_NOOBJ);
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);
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.
92 of_statdir(vol, s_path);
94 if ( s_path->st_errno != 0 ) {
95 if (afp_errno != AFPERR_ACCESS) {
96 return( AFPERR_NOOBJ );
102 if (S_ISDIR(st->st_mode)) {
108 ret = getdirparams(obj, vol, dbitmap, s_path, dir,
109 rbuf + 3 * sizeof( uint16_t ), &buflen );
113 /* this is a directory */
114 *(rbuf + 2 * sizeof( uint16_t )) = (char) FILDIRBIT_ISDIR;
116 if (fbitmap && AFP_OK != (ret = getfilparams(obj, vol, fbitmap, s_path, curdir,
117 rbuf + 3 * sizeof( uint16_t ), &buflen, 0)) ) {
121 *(rbuf + 2 * sizeof( uint16_t )) = FILDIRBIT_ISFILE;
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 );
135 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
141 uint16_t vid, bitmap;
146 memcpy( &vid, ibuf, sizeof(vid));
147 ibuf += sizeof( vid );
149 if (NULL == ( vol = getvolbyvid( vid )) ) {
150 return( AFPERR_PARAM );
153 if (vol->v_flags & AFPVOL_RO)
156 memcpy( &did, ibuf, sizeof( did));
157 ibuf += sizeof( did);
159 if (NULL == ( dir = dirlookup( vol, did )) ) {
163 memcpy( &bitmap, ibuf, sizeof( bitmap ));
164 bitmap = ntohs( bitmap );
165 ibuf += sizeof( bitmap );
167 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
168 return get_afp_errno(AFPERR_NOOBJ);
172 if (!path->st_valid) {
173 /* it's a dir and it should be there
174 * because we chdir in it in cname
176 of_statdir(vol, path);
179 if ( path->st_errno != 0 ) {
180 if (afp_errno != AFPERR_ACCESS)
181 return( AFPERR_NOOBJ );
184 * If ibuf is odd, make it even.
186 if ((u_long)ibuf & 1 ) {
190 if (S_ISDIR(st->st_mode)) {
191 rc = setdirparams(vol, path, bitmap, ibuf );
193 rc = setfilparams(obj, vol, path, bitmap, ibuf );
195 if ( rc == AFP_OK ) {
196 setvoltime(obj, vol );
202 /* --------------------------------------------
203 Factorise some checks on a pathname
205 int check_name(const struct vol *vol, char *name)
207 if (!vol->vfs->vfs_validupath(vol, name)) {
208 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
212 /* check for vetoed filenames */
213 if (veto_file(vol->v_veto, name))
218 /* -------------------------
219 move and rename sdir:oldname to curdir:newname in volume vol
220 special care is needed for lock
222 static int moveandrename(struct vol *vol,
229 char *oldunixname = NULL;
232 struct stat *st, nst;
236 struct ofork *opened = NULL;
241 LOG(log_debug, logtype_afpd,
242 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
243 cfrombstr(sdir->d_u_name), oldname, newname);
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));
259 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
260 return AFPERR_PARAM; /* pathname too long */
261 #endif /* HAVE_ATFUNCS */
264 path.u_name = oldunixname;
267 opened = of_findnameat(sdir_fd, &path);
269 opened = of_findname(vol, &path);
270 #endif /* HAVE_ATFUNCS */
273 /* reuse struct adouble so it won't break locks */
277 id = sdir->d_did; /* we already have the CNID */
278 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
280 adflags = ADFLAGS_DIR;
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
293 if ((cwd_fd = open(".", O_RDONLY)) == -1)
295 if (fchdir(sdir_fd) != 0) {
300 if (!ad_metadata(oldunixname, adflags, adp)) {
303 ad_getattr(adp, &bshort);
305 ad_close(adp, ADFLAGS_HF);
306 if (!(vol->v_ignattr & ATTRBIT_NORENAME) && (bshort & htons(ATTRBIT_NORENAME))) {
312 if (fchdir(cwd_fd) != 0) {
313 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
319 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding(vol->v_obj)))){
325 if (0 != (rc = check_name(vol, upath))) {
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) {
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 */
344 } else if (stat(upath, st ) == 0) {
351 path.st_errno = errno;
352 if (of_findname(vol, &path)) {
353 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
355 rc = renamefile(vol, curdir, sdir_fd, oldunixname, upath, newname, adp );
357 of_rename(vol, opened, sdir, oldname, curdir, newname);
360 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
362 if ( rc == AFP_OK && id ) {
363 /* renaming may have moved the file/dir across a filesystem */
364 if (stat(upath, st) < 0) {
369 /* Remove it from the cache */
370 struct dir *cacheddir = dircache_search_by_did(vol, id);
372 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
373 (void)dir_remove(vol, cacheddir);
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);
380 ad_close(adp, ADFLAGS_HF);
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));
397 /* -------------------------------------------- */
398 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
402 char *oldname, *newname;
413 memcpy( &vid, ibuf, sizeof( vid ));
414 ibuf += sizeof( vid );
415 if (NULL == ( vol = getvolbyvid( vid )) ) {
416 return( AFPERR_PARAM );
419 if (vol->v_flags & AFPVOL_RO)
422 memcpy( &did, ibuf, sizeof( did ));
423 ibuf += sizeof( did );
424 if (NULL == ( sdir = dirlookup( vol, did )) ) {
428 /* source pathname */
429 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
430 return get_afp_errno(AFPERR_NOOBJ);
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 */
440 /* curdir parent dir, need to move sdir back */
445 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
446 return( AFPERR_NORENAME );
448 /* move to destination dir */
449 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
452 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
455 /* another place where we know about the path type */
456 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
457 return( AFPERR_PARAM );
461 return AFP_OK; /* newname == oldname same dir */
464 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
465 if ( rc == AFP_OK ) {
466 setvoltime(obj, vol );
473 * Recursivley delete vetoed files and directories if the volume option is set
475 * @param vol (r) volume handle
476 * @param upath (r) path of directory
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.
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.
485 int delete_vetoed_files(struct vol *vol, const char *upath, bool in_vetodir)
494 if (!(vol->v_flags & AFPVOL_DELVETO))
497 EC_NEG1( pwd = open(".", O_RDONLY));
498 EC_ZERO( chdir(upath) );
499 EC_NULL( dp = opendir(".") );
501 while ((de = readdir(dp))) {
502 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
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);
511 if (in_vetodir || veto_file(vol->v_veto, de->d_name))
517 LOG(log_debug, logtype_afpd, "delete_vetoed_files(\"%s/%s\"): deleting vetoed file",
519 switch (sb.st_mode & S_IFMT) {
522 EC_ZERO( delete_vetoed_files(vol, de->d_name, vetoed));
526 EC_ZERO( netatalk_unlink(de->d_name) );
534 EC_ZERO_LOG( fchdir(pwd) );
536 EC_ZERO_LOG( rmdir(upath) );
542 if (fchdir(pwd) != 0)
549 /* ------------------------------- */
550 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
563 memcpy( &vid, ibuf, sizeof( vid ));
564 ibuf += sizeof( vid );
565 if (NULL == ( vol = getvolbyvid( vid )) ) {
566 return( AFPERR_PARAM );
569 if (vol->v_flags & AFPVOL_RO)
572 memcpy( &did, ibuf, sizeof( did ));
573 ibuf += sizeof( int );
575 if (NULL == ( dir = dirlookup( vol, did )) ) {
579 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
580 return get_afp_errno(AFPERR_NOOBJ);
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) {
593 if (delete_vetoed_files(vol, upath, false) != 0)
594 return AFPERR_DIRNEMPT;
597 return AFPERR_ACCESS;
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);
608 if (delcnid == CNID_INVALID) {
609 AFP_CNID_START("cnid_get");
610 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
613 if (delcnid != CNID_INVALID) {
614 AFP_CNID_START("cnid_delete");
615 cnid_delete(vol->v_cdb, delcnid);
618 fce_register(FCE_DIR_DELETE, fullpathname(upath), NULL, fce_dir);
620 /* we have to cache this, the structs are lost in deletcurdir*/
621 /* but we need the positive returncode to send our event */
623 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
625 if ((rc = deletecurdir(vol)) == AFP_OK)
626 fce_register(FCE_DIR_DELETE, fullpathname(cfrombstr(dname)), NULL, fce_dir);
629 } else if (of_findname(vol, s_path)) {
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
636 if (s_path->st_valid && s_path->st_errno == ENOENT) {
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)
644 vol->v_tm_used -= s_path->st.st_size;
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);
653 if ( rc == AFP_OK ) {
655 setvoltime(obj, vol );
660 /* ------------------------ */
661 char *absupath(const struct vol *vol, struct dir *dir, char *u)
663 static char pathbuf[MAXPATHLEN + 1];
666 if (u == NULL || dir == NULL || vol == NULL)
669 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
671 if (bcatcstr(path, "/") != BSTR_OK)
673 if (bcatcstr(path, u) != BSTR_OK)
675 if (path->slen > MAXPATHLEN) {
680 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
682 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
688 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
690 if (vol == NULL || dir == NULL || name == NULL)
692 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding(vol->v_obj)));
695 /* ------------------------- */
696 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
699 struct dir *sdir, *ddir;
701 char *oldname, *newname;
714 memcpy( &vid, ibuf, sizeof( vid ));
715 ibuf += sizeof( vid );
716 if (NULL == ( vol = getvolbyvid( vid )) ) {
717 return( AFPERR_PARAM );
720 if (vol->v_flags & AFPVOL_RO)
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 */
730 memcpy( &did, ibuf, sizeof( did ));
731 ibuf += sizeof( int );
733 /* source pathname */
734 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
735 return get_afp_errno(AFPERR_NOOBJ);
739 newname = obj->newtmp;
740 oldname = obj->oldtmp;
742 isdir = path_isadir(path);
743 if ( *path->m_name != '\0' ) {
747 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
749 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
753 if ((sdir_fd = open(".", O_RDONLY)) == -1)
757 /* get the destination directory */
758 if (NULL == ( ddir = dirlookup( vol, did )) ) {
759 rc = afp_errno; /* was AFPERR_PARAM */
762 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
766 pdid = curdir->d_did;
767 if ( *path->m_name != '\0' ) {
768 rc = path_error(path, AFPERR_NOOBJ);
772 /* one more place where we know about path type */
773 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
779 strcpy(newname, oldname);
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);
787 if ( rc == AFP_OK ) {
788 char *upath = mtoupath(vol, newname, pdid, utf8_encoding(obj));
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;
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);
801 setvoltime(obj, vol );
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.
820 int i; /* index to veto_str */
821 int j; /* index to path */
823 if ((veto_str == NULL) || (path == NULL))
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);
834 if (veto_str[i] != path[j]) {
835 while ((veto_str[i] != '/')
836 && (veto_str[i] != '\0'))