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);
384 ad_close_metadata( adp);
385 if ((bshort & htons(ATTRBIT_NORENAME))) {
391 if (fchdir(cwd_fd) != 0) {
392 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
398 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
404 if (0 != (rc = check_name(vol, upath))) {
408 /* source == destination. we just silently accept this. */
409 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
410 if (strcmp(oldname, newname) == 0) {
415 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
416 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
417 /* not the same file */
423 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
430 path.st_errno = errno;
431 if (of_findname(&path)) {
432 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
434 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
436 of_rename(vol, opened, sdir, oldname, curdir, newname);
439 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
441 if ( rc == AFP_OK && id ) {
442 /* renaming may have moved the file/dir across a filesystem */
443 if (stat(upath, st) < 0) {
448 /* Remove it from the cache */
449 struct dir *cacheddir = dircache_search_by_did(vol, id);
451 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
452 (void)dir_remove(vol, cacheddir);
455 /* fix up the catalog entry */
456 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
465 /* -------------------------------------------- */
466 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
470 char *oldname, *newname;
481 memcpy( &vid, ibuf, sizeof( vid ));
482 ibuf += sizeof( vid );
483 if (NULL == ( vol = getvolbyvid( vid )) ) {
484 return( AFPERR_PARAM );
487 if (vol->v_flags & AFPVOL_RO)
490 memcpy( &did, ibuf, sizeof( did ));
491 ibuf += sizeof( did );
492 if (NULL == ( sdir = dirlookup( vol, did )) ) {
496 /* source pathname */
497 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
498 return get_afp_errno(AFPERR_NOOBJ);
502 newname = obj->newtmp;
503 oldname = obj->oldtmp;
504 isdir = path_isadir(path);
505 if ( *path->m_name != '\0' ) {
506 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
508 /* curdir parent dir, need to move sdir back */
513 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
514 return( AFPERR_NORENAME );
516 /* move to destination dir */
517 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
520 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
523 /* another place where we know about the path type */
524 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
525 return( AFPERR_PARAM );
529 return AFP_OK; /* newname == oldname same dir */
532 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
533 if ( rc == AFP_OK ) {
534 setvoltime(obj, vol );
540 /* ------------------------------- */
541 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
553 memcpy( &vid, ibuf, sizeof( vid ));
554 ibuf += sizeof( vid );
555 if (NULL == ( vol = getvolbyvid( vid )) ) {
556 return( AFPERR_PARAM );
559 if (vol->v_flags & AFPVOL_RO)
562 memcpy( &did, ibuf, sizeof( did ));
563 ibuf += sizeof( int );
565 if (NULL == ( dir = dirlookup( vol, did )) ) {
569 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
570 return get_afp_errno(AFPERR_NOOBJ);
573 upath = s_path->u_name;
574 if ( path_isadir( s_path) ) {
575 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
578 rc = deletecurdir( vol);
579 } else if (of_findname(s_path)) {
582 /* it's a file st_valid should always be true
583 * only test for ENOENT because EACCES needs
584 * to read meta data in deletefile
586 if (s_path->st_valid && s_path->st_errno == ENOENT) {
590 rc = deletefile(vol, -1, upath, 1);
592 struct dir *cachedfile;
593 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath), s_path->st.st_ctime))) {
594 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
595 dir_free(cachedfile);
599 if ( rc == AFP_OK ) {
601 setvoltime(obj, vol );
606 /* ------------------------ */
607 char *absupath(const struct vol *vol, struct dir *dir, char *u)
609 static char pathbuf[MAXPATHLEN + 1];
612 if (u == NULL || dir == NULL || vol == NULL)
615 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
617 if (bcatcstr(path, "/") != BSTR_OK)
619 if (bcatcstr(path, u) != BSTR_OK)
621 if (path->slen > MAXPATHLEN)
624 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
626 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
632 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
634 if (vol == NULL || dir == NULL || name == NULL)
636 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
639 /* ------------------------- */
640 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
643 struct dir *sdir, *ddir;
645 char *oldname, *newname;
654 #endif /* DROPKLUDGE */
661 memcpy( &vid, ibuf, sizeof( vid ));
662 ibuf += sizeof( vid );
663 if (NULL == ( vol = getvolbyvid( vid )) ) {
664 return( AFPERR_PARAM );
667 if (vol->v_flags & AFPVOL_RO)
670 /* source did followed by dest did */
671 memcpy( &did, ibuf, sizeof( did ));
672 ibuf += sizeof( int );
673 if (NULL == ( sdir = dirlookup( vol, did )) ) {
674 return afp_errno; /* was AFPERR_PARAM */
677 memcpy( &did, ibuf, sizeof( did ));
678 ibuf += sizeof( int );
680 /* source pathname */
681 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
682 return get_afp_errno(AFPERR_NOOBJ);
686 newname = obj->newtmp;
687 oldname = obj->oldtmp;
689 isdir = path_isadir(path);
690 if ( *path->m_name != '\0' ) {
694 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
696 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
700 if ((sdir_fd = open(".", O_RDONLY)) == -1)
704 /* get the destination directory */
705 if (NULL == ( ddir = dirlookup( vol, did )) ) {
706 rc = afp_errno; /* was AFPERR_PARAM */
709 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
713 pdid = curdir->d_did;
714 if ( *path->m_name != '\0' ) {
715 rc = path_error(path, AFPERR_NOOBJ);
719 /* one more place where we know about path type */
720 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
726 strcpy(newname, oldname);
729 /* This does the work */
730 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
731 oldname, newname, isdir);
732 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
734 if ( rc == AFP_OK ) {
735 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
744 if (vol->v_flags & AFPVOL_DROPBOX) {
745 /* FIXME did is not always the source id */
746 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
752 #endif /* DROPKLUDGE */
753 /* if unix priv don't try to match perm with dest folder */
754 if (!isdir && !vol_unix_priv(vol)) {
755 int admode = ad_mode("", 0777) | vol->v_fperm;
757 setfilmode(upath, admode, NULL, vol->v_umask);
758 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
760 setvoltime(obj, vol );
772 int veto_file(const char*veto_str, const char*path)
773 /* given a veto_str like "abc/zxc/" and path "abc", return 1
774 * veto_str should be '/' delimited
775 * if path matches any one of the veto_str elements exactly, then 1 is returned
776 * otherwise, 0 is returned.
779 int i; /* index to veto_str */
780 int j; /* index to path */
782 if ((veto_str == NULL) || (path == NULL))
785 for(i=0, j=0; veto_str[i] != '\0'; i++) {
786 if (veto_str[i] == '/') {
787 if ((j>0) && (path[j] == '\0')) {
788 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
793 if (veto_str[i] != path[j]) {
794 while ((veto_str[i] != '/')
795 && (veto_str[i] != '\0'))