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>
27 #include "directory.h"
38 int matchfile2dirperms(
39 /* Since it's kinda' big; I decided against an
44 /* The below code changes the way file ownership is determined in the name of
45 fixing dropboxes. It has known security problem. See the netatalk FAQ for
54 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
57 if (stat(upath, &st ) < 0) {
58 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
62 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
63 /* FIXME dirsearch doesn't move cwd to did ! */
64 if (( dir = dirlookup( vol, did )) == NULL ) {
65 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
68 else if (stat(".", &sb) < 0) {
69 LOG(log_error, logtype_afpd,
70 "matchfile2dirperms: Error checking directory \"%s\": %s",
71 dir->d_m_name, strerror(errno));
76 if ( uid != sb.st_uid )
79 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
81 LOG(log_error, logtype_afpd,
82 "matchfile2dirperms(%s): Error changing owner/gid: %s",
83 upath, strerror(errno));
86 else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
88 LOG(log_error, logtype_afpd,
89 "matchfile2dirperms(%s): Error adding file read permissions: %s",
90 upath, strerror(errno));
93 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
95 LOG(log_error, logtype_afpd,
96 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
97 adpath, strerror(errno));
100 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
104 adpath, strerror(errno));
109 } /* end else if stat success */
112 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
118 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
126 u_int16_t fbitmap, dbitmap, vid;
132 memcpy( &vid, ibuf, sizeof( vid ));
133 ibuf += sizeof( vid );
134 if (NULL == ( vol = getvolbyvid( vid )) ) {
135 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
138 return( AFPERR_ACCESS );
141 memcpy( &did, ibuf, sizeof( did ));
142 ibuf += sizeof( did );
144 if (NULL == ( dir = dirlookup( vol, did )) ) {
148 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
149 fbitmap = ntohs( fbitmap );
150 ibuf += sizeof( fbitmap );
151 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
152 dbitmap = ntohs( dbitmap );
153 ibuf += sizeof( dbitmap );
155 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
156 return get_afp_errno(AFPERR_NOOBJ);
159 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
160 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
161 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
164 if (!s_path->st_valid) {
165 /* it's a dir and it should be there
166 * because we chdir in it in cname or
167 * it's curdir (maybe deleted, but then we can't know).
168 * So we need to try harder.
170 of_statdir(vol, s_path);
172 if ( s_path->st_errno != 0 ) {
173 if (afp_errno != AFPERR_ACCESS) {
174 return( AFPERR_NOOBJ );
180 if (S_ISDIR(st->st_mode)) {
186 ret = getdirparams(vol, dbitmap, s_path, dir,
187 rbuf + 3 * sizeof( u_int16_t ), &buflen );
191 /* this is a directory */
192 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
194 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
195 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
199 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
201 *rbuflen = buflen + 3 * sizeof( u_int16_t );
202 fbitmap = htons( fbitmap );
203 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
204 rbuf += sizeof( fbitmap );
205 dbitmap = htons( dbitmap );
206 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
207 rbuf += sizeof( dbitmap ) + sizeof( u_char );
213 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
219 u_int16_t vid, bitmap;
224 memcpy( &vid, ibuf, sizeof(vid));
225 ibuf += sizeof( vid );
227 if (NULL == ( vol = getvolbyvid( vid )) ) {
228 return( AFPERR_PARAM );
231 if (vol->v_flags & AFPVOL_RO)
234 memcpy( &did, ibuf, sizeof( did));
235 ibuf += sizeof( did);
237 if (NULL == ( dir = dirlookup( vol, did )) ) {
241 memcpy( &bitmap, ibuf, sizeof( bitmap ));
242 bitmap = ntohs( bitmap );
243 ibuf += sizeof( bitmap );
245 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
246 return get_afp_errno(AFPERR_NOOBJ);
250 if (!path->st_valid) {
251 /* it's a dir and it should be there
252 * because we chdir in it in cname
254 of_statdir(vol, path);
257 if ( path->st_errno != 0 ) {
258 if (afp_errno != AFPERR_ACCESS)
259 return( AFPERR_NOOBJ );
262 * If ibuf is odd, make it even.
264 if ((u_long)ibuf & 1 ) {
268 if (S_ISDIR(st->st_mode)) {
269 rc = setdirparams(vol, path, bitmap, ibuf );
271 rc = setfilparams(vol, path, bitmap, ibuf );
273 if ( rc == AFP_OK ) {
274 setvoltime(obj, vol );
280 /* --------------------------------------------
281 Factorise some checks on a pathname
283 int check_name(const struct vol *vol, char *name)
285 /* check for illegal characters in the unix filename */
286 if (!wincheck(vol, name))
289 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
292 if (!vol->vfs->vfs_validupath(vol, name)) {
293 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
297 /* check for vetoed filenames */
298 if (veto_file(vol->v_veto, name))
303 /* -------------------------
304 move and rename sdir:oldname to curdir:newname in volume vol
305 special care is needed for lock
307 static int moveandrename(const struct vol *vol,
317 struct stat *st, nst;
321 struct ofork *opened = NULL;
326 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
331 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
332 return AFPERR_PARAM; /* can't convert */
334 #ifndef HAVE_RENAMEAT
336 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
337 p = ctoupath( vol, sdir, oldname );
339 return AFPERR_PARAM; /* pathname too long */
340 #endif /* HAVE_RENAMEAT */
345 opened = of_findnameat(sdir_fd, &path);
347 opened = of_findname(&path);
348 #endif /* HAVE_RENAMEAT */
350 /* reuse struct adouble so it won't break locks */
354 id = sdir->d_did; /* we already have the CNID */
355 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
359 adflags = ADFLAGS_DIR;
363 * p now points to either
364 * a) full pathname of the source fs object (if renameat is not available)
365 * b) the oldname (renameat is available)
366 * we are in the dest folder so we need to use
368 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
372 if ((cwd_fd = open(".", O_RDONLY)) == -1)
374 if (fchdir(sdir_fd) != 0) {
379 if (!ad_metadata(p, adflags, adp)) {
382 ad_getattr(adp, &bshort);
383 ad_close_metadata( adp);
384 if ((bshort & htons(ATTRBIT_NORENAME))) {
390 if (fchdir(cwd_fd) != 0) {
391 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
397 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
403 if (0 != (rc = check_name(vol, upath))) {
407 /* source == destination. we just silently accept this. */
408 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
409 if (strcmp(oldname, newname) == 0) {
414 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
415 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
416 /* not the same file */
422 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
429 path.st_errno = errno;
430 if (of_findname(&path)) {
431 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
433 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
435 of_rename(vol, opened, sdir, oldname, curdir, newname);
438 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
440 if ( rc == AFP_OK && id ) {
441 /* renaming may have moved the file/dir across a filesystem */
442 if (stat(upath, st) < 0) {
447 /* Remove it from the cache */
448 struct dir *cacheddir = dircache_search_by_did(vol, id);
450 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
451 (void)dir_remove(vol, cacheddir);
454 /* fix up the catalog entry */
455 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
464 /* -------------------------------------------- */
465 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
469 char *oldname, *newname;
480 memcpy( &vid, ibuf, sizeof( vid ));
481 ibuf += sizeof( vid );
482 if (NULL == ( vol = getvolbyvid( vid )) ) {
483 return( AFPERR_PARAM );
486 if (vol->v_flags & AFPVOL_RO)
489 memcpy( &did, ibuf, sizeof( did ));
490 ibuf += sizeof( did );
491 if (NULL == ( sdir = dirlookup( vol, did )) ) {
495 /* source pathname */
496 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
497 return get_afp_errno(AFPERR_NOOBJ);
501 newname = obj->newtmp;
502 oldname = obj->oldtmp;
503 isdir = path_isadir(path);
504 if ( *path->m_name != '\0' ) {
505 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
507 /* curdir parent dir, need to move sdir back */
512 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
513 return( AFPERR_NORENAME );
515 /* move to destination dir */
516 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
519 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
522 /* another place where we know about the path type */
523 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
524 return( AFPERR_PARAM );
528 return AFP_OK; /* newname == oldname same dir */
531 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
532 if ( rc == AFP_OK ) {
533 setvoltime(obj, vol );
539 /* ------------------------------- */
540 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
552 memcpy( &vid, ibuf, sizeof( vid ));
553 ibuf += sizeof( vid );
554 if (NULL == ( vol = getvolbyvid( vid )) ) {
555 return( AFPERR_PARAM );
558 if (vol->v_flags & AFPVOL_RO)
561 memcpy( &did, ibuf, sizeof( did ));
562 ibuf += sizeof( int );
564 if (NULL == ( dir = dirlookup( vol, did )) ) {
568 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
569 return get_afp_errno(AFPERR_NOOBJ);
572 upath = s_path->u_name;
573 if ( path_isadir( s_path) ) {
574 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
577 rc = deletecurdir( vol);
578 } else if (of_findname(s_path)) {
581 /* it's a file st_valid should always be true
582 * only test for ENOENT because EACCES needs
583 * to read meta data in deletefile
585 if (s_path->st_valid && s_path->st_errno == ENOENT) {
589 rc = deletefile(vol, -1, upath, 1);
591 struct dir *cachedfile;
592 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath), s_path->st.st_ctime))) {
593 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
594 dir_free(cachedfile);
598 if ( rc == AFP_OK ) {
600 setvoltime(obj, vol );
605 /* ------------------------ */
606 char *absupath(const struct vol *vol, struct dir *dir, char *u)
608 static char pathbuf[MAXPATHLEN + 1];
611 if (u == NULL || dir == NULL || vol == NULL)
614 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
616 if (bcatcstr(path, "/") != BSTR_OK)
618 if (bcatcstr(path, u) != BSTR_OK)
620 if (path->slen > MAXPATHLEN)
623 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
625 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
631 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
633 if (vol == NULL || dir == NULL || name == NULL)
635 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
638 /* ------------------------- */
639 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
642 struct dir *sdir, *ddir;
644 char *oldname, *newname;
653 #endif /* DROPKLUDGE */
660 memcpy( &vid, ibuf, sizeof( vid ));
661 ibuf += sizeof( vid );
662 if (NULL == ( vol = getvolbyvid( vid )) ) {
663 return( AFPERR_PARAM );
666 if (vol->v_flags & AFPVOL_RO)
669 /* source did followed by dest did */
670 memcpy( &did, ibuf, sizeof( did ));
671 ibuf += sizeof( int );
672 if (NULL == ( sdir = dirlookup( vol, did )) ) {
673 return afp_errno; /* was AFPERR_PARAM */
676 memcpy( &did, ibuf, sizeof( did ));
677 ibuf += sizeof( int );
679 /* source pathname */
680 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
681 return get_afp_errno(AFPERR_NOOBJ);
685 newname = obj->newtmp;
686 oldname = obj->oldtmp;
688 isdir = path_isadir(path);
689 if ( *path->m_name != '\0' ) {
693 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
695 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
699 if ((sdir_fd = open(".", O_RDONLY)) == -1)
703 /* get the destination directory */
704 if (NULL == ( ddir = dirlookup( vol, did )) ) {
705 rc = afp_errno; /* was AFPERR_PARAM */
708 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
712 pdid = curdir->d_did;
713 if ( *path->m_name != '\0' ) {
714 rc = path_error(path, AFPERR_NOOBJ);
718 /* one more place where we know about path type */
719 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
725 strcpy(newname, oldname);
728 /* This does the work */
729 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
730 oldname, newname, isdir);
731 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
733 if ( rc == AFP_OK ) {
734 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
743 if (vol->v_flags & AFPVOL_DROPBOX) {
744 /* FIXME did is not always the source id */
745 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
751 #endif /* DROPKLUDGE */
752 /* if unix priv don't try to match perm with dest folder */
753 if (!isdir && !vol_unix_priv(vol)) {
754 int admode = ad_mode("", 0777) | vol->v_fperm;
756 setfilmode(upath, admode, NULL, vol->v_umask);
757 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
759 setvoltime(obj, vol );
771 int veto_file(const char*veto_str, const char*path)
772 /* given a veto_str like "abc/zxc/" and path "abc", return 1
773 * veto_str should be '/' delimited
774 * if path matches any one of the veto_str elements exactly, then 1 is returned
775 * otherwise, 0 is returned.
778 int i; /* index to veto_str */
779 int j; /* index to path */
781 if ((veto_str == NULL) || (path == NULL))
784 for(i=0, j=0; veto_str[i] != '\0'; i++) {
785 if (veto_str[i] == '/') {
786 if ((j>0) && (path[j] == '\0')) {
787 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
792 if (veto_str[i] != path[j]) {
793 while ((veto_str[i] != '/')
794 && (veto_str[i] != '\0'))