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(const AFPObj *obj,
230 char *oldunixname = NULL;
233 struct stat *st, nst;
237 struct ofork *opened = NULL;
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));
256 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
257 return AFPERR_PARAM; /* pathname too long */
258 #endif /* HAVE_ATFUNCS */
261 path.u_name = oldunixname;
264 opened = of_findnameat(sdir_fd, &path);
266 opened = of_findname(vol, &path);
267 #endif /* HAVE_ATFUNCS */
270 /* reuse struct adouble so it won't break locks */
274 id = sdir->d_did; /* we already have the CNID */
275 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
277 adflags = ADFLAGS_DIR;
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
290 if ((cwd_fd = open(".", O_RDONLY)) == -1)
292 if (fchdir(sdir_fd) != 0) {
297 if (!ad_metadata(oldunixname, adflags, adp)) {
300 ad_getattr(adp, &bshort);
302 ad_close(adp, ADFLAGS_HF);
303 if (!(vol->v_ignattr & ATTRBIT_NORENAME) && (bshort & htons(ATTRBIT_NORENAME))) {
309 if (fchdir(cwd_fd) != 0) {
310 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
316 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding(vol->v_obj)))){
322 if (0 != (rc = check_name(vol, upath))) {
327 LOG(log_debug, logtype_afpd,
328 "moveandrename(\"%s\" -> \"%s/%s\")",
329 oldunixname, bdata(curdir->d_fullpath), upath);
331 LOG(log_debug, logtype_afpd,
332 "moveandrename(\"%s/%s\" -> \"%s/%s\")",
333 bdata(sdir->d_fullpath), oldunixname, bdata(curdir->d_fullpath), upath);
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) {
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 */
350 } else if (stat(upath, st ) == 0) {
357 path.st_errno = errno;
358 if (of_findname(vol, &path)) {
359 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
361 rc = renamefile(vol, curdir, sdir_fd, oldunixname, upath, newname, adp );
363 of_rename(vol, opened, sdir, oldname, curdir, newname);
366 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
368 if ( rc == AFP_OK && id ) {
369 /* renaming may have moved the file/dir across a filesystem */
370 if (stat(upath, st) < 0) {
375 /* Remove it from the cache */
376 struct dir *cacheddir = dircache_search_by_did(vol, id);
378 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
379 (void)dir_remove(vol, cacheddir);
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);
386 ad_close(adp, ADFLAGS_HF);
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));
396 fce_register(obj, FCE_DIR_MOVE, fullpathname(upath), oldunixname);
398 bstring srcpath = bformat("%s/%s", bdata(sdir->d_fullpath), oldunixname);
399 fce_register(obj, FCE_FILE_MOVE, fullpathname(upath), bdata(srcpath));
412 /* -------------------------------------------- */
413 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
417 char *oldname, *newname;
428 memcpy( &vid, ibuf, sizeof( vid ));
429 ibuf += sizeof( vid );
430 if (NULL == ( vol = getvolbyvid( vid )) ) {
431 return( AFPERR_PARAM );
434 if (vol->v_flags & AFPVOL_RO)
437 memcpy( &did, ibuf, sizeof( did ));
438 ibuf += sizeof( did );
439 if (NULL == ( sdir = dirlookup( vol, did )) ) {
443 /* source pathname */
444 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
445 return get_afp_errno(AFPERR_NOOBJ);
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 */
455 /* curdir parent dir, need to move sdir back */
460 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
461 return( AFPERR_NORENAME );
463 /* move to destination dir */
464 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
467 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
470 /* another place where we know about the path type */
471 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
472 return( AFPERR_PARAM );
476 return AFP_OK; /* newname == oldname same dir */
479 rc = moveandrename(obj, vol, sdir, -1, oldname, newname, isdir);
480 if ( rc == AFP_OK ) {
481 setvoltime(obj, vol );
488 * Recursivley delete vetoed files and directories if the volume option is set
490 * @param vol (r) volume handle
491 * @param upath (r) path of directory
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.
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.
500 int delete_vetoed_files(struct vol *vol, const char *upath, bool in_vetodir)
509 if (!(vol->v_flags & AFPVOL_DELVETO))
512 EC_NEG1( pwd = open(".", O_RDONLY));
513 EC_ZERO( chdir(upath) );
514 EC_NULL( dp = opendir(".") );
516 while ((de = readdir(dp))) {
517 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
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);
526 if (in_vetodir || veto_file(vol->v_veto, de->d_name))
532 LOG(log_debug, logtype_afpd, "delete_vetoed_files(\"%s/%s\"): deleting vetoed file",
534 switch (sb.st_mode & S_IFMT) {
537 EC_ZERO( delete_vetoed_files(vol, de->d_name, vetoed));
541 EC_ZERO( netatalk_unlink(de->d_name) );
549 EC_ZERO_LOG( fchdir(pwd) );
551 EC_ZERO_LOG( rmdir(upath) );
557 if (fchdir(pwd) != 0)
564 /* ------------------------------- */
565 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
578 memcpy( &vid, ibuf, sizeof( vid ));
579 ibuf += sizeof( vid );
580 if (NULL == ( vol = getvolbyvid( vid )) ) {
581 return( AFPERR_PARAM );
584 if (vol->v_flags & AFPVOL_RO)
587 memcpy( &did, ibuf, sizeof( did ));
588 ibuf += sizeof( int );
590 if (NULL == ( dir = dirlookup( vol, did )) ) {
594 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
595 return get_afp_errno(AFPERR_NOOBJ);
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) {
608 if (delete_vetoed_files(vol, upath, false) != 0)
609 return AFPERR_DIRNEMPT;
612 return AFPERR_ACCESS;
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);
623 if (delcnid == CNID_INVALID) {
624 AFP_CNID_START("cnid_get");
625 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
628 if (delcnid != CNID_INVALID) {
629 AFP_CNID_START("cnid_delete");
630 cnid_delete(vol->v_cdb, delcnid);
633 fce_register(obj, FCE_DIR_DELETE, fullpathname(upath), NULL);
635 /* we have to cache this, the structs are lost in deletcurdir*/
636 /* but we need the positive returncode to send our event */
638 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
640 if ((rc = deletecurdir(vol)) == AFP_OK)
641 fce_register(obj, FCE_DIR_DELETE, fullpathname(cfrombstr(dname)), NULL);
644 } else if (of_findname(vol, s_path)) {
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
651 if (s_path->st_valid && s_path->st_errno == ENOENT) {
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)
659 vol->v_tm_used -= s_path->st.st_size;
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);
668 if ( rc == AFP_OK ) {
670 setvoltime(obj, vol );
675 /* ------------------------ */
676 char *absupath(const struct vol *vol, struct dir *dir, char *u)
678 static char pathbuf[MAXPATHLEN + 1];
681 if (u == NULL || dir == NULL || vol == NULL)
684 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
686 if (bcatcstr(path, "/") != BSTR_OK)
688 if (bcatcstr(path, u) != BSTR_OK)
690 if (path->slen > MAXPATHLEN) {
695 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
697 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
703 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
705 if (vol == NULL || dir == NULL || name == NULL)
707 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding(vol->v_obj)));
710 /* ------------------------- */
711 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
714 struct dir *sdir, *ddir;
716 char *oldname, *newname;
729 memcpy( &vid, ibuf, sizeof( vid ));
730 ibuf += sizeof( vid );
731 if (NULL == ( vol = getvolbyvid( vid )) ) {
732 return( AFPERR_PARAM );
735 if (vol->v_flags & AFPVOL_RO)
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 */
745 memcpy( &did, ibuf, sizeof( did ));
746 ibuf += sizeof( int );
748 /* source pathname */
749 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
750 return get_afp_errno(AFPERR_NOOBJ);
754 newname = obj->newtmp;
755 oldname = obj->oldtmp;
757 isdir = path_isadir(path);
758 if ( *path->m_name != '\0' ) {
762 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
764 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
768 if ((sdir_fd = open(".", O_RDONLY)) == -1)
772 /* get the destination directory */
773 if (NULL == ( ddir = dirlookup( vol, did )) ) {
774 rc = afp_errno; /* was AFPERR_PARAM */
777 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
781 pdid = curdir->d_did;
782 if ( *path->m_name != '\0' ) {
783 rc = path_error(path, AFPERR_NOOBJ);
787 /* one more place where we know about path type */
788 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
794 strcpy(newname, oldname);
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);
802 if ( rc == AFP_OK ) {
803 char *upath = mtoupath(vol, newname, pdid, utf8_encoding(obj));
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;
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);
816 setvoltime(obj, vol );
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.
835 int i; /* index to veto_str */
836 int j; /* index to path */
838 if ((veto_str == NULL) || (path == NULL))
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);
849 if (veto_str[i] != path[j]) {
850 while ((veto_str[i] != '/')
851 && (veto_str[i] != '\0'))