2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #else /* STDC_HEADERS */
19 #endif /* HAVE_STRCHR */
20 char *strchr (), *strrchr ();
22 #define memcpy(d,s,n) bcopy ((s), (d), (n))
23 #define memmove(d,s,n) bcopy ((s), (d), (n))
24 #endif /* ! HAVE_MEMCPY */
25 #endif /* STDC_HEADERS */
31 #include <sys/param.h>
33 #include <atalk/adouble.h>
34 #include <atalk/vfs.h>
35 #include <atalk/afp.h>
36 #include <atalk/util.h>
37 #include <atalk/cnid.h>
38 #include <atalk/logger.h>
39 #include <atalk/unix.h>
40 #include <atalk/bstrlib.h>
41 #include <atalk/bstradd.h>
42 #include <atalk/acl.h>
43 #include <atalk/globals.h>
44 #include <atalk/fce_api.h>
46 #include "directory.h"
56 int matchfile2dirperms(
57 /* Since it's kinda' big; I decided against an
62 /* The below code changes the way file ownership is determined in the name of
63 fixing dropboxes. It has known security problem. See the netatalk FAQ for
72 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
75 if (stat(upath, &st ) < 0) {
76 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
80 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
81 /* FIXME dirsearch doesn't move cwd to did ! */
82 if (( dir = dirlookup( vol, did )) == NULL ) {
83 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
86 else if (stat(".", &sb) < 0) {
87 LOG(log_error, logtype_afpd,
88 "matchfile2dirperms: Error checking directory \"%s\": %s",
89 dir->d_m_name, strerror(errno));
94 if ( uid != sb.st_uid )
97 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
99 LOG(log_error, logtype_afpd,
100 "matchfile2dirperms(%s): Error changing owner/gid: %s",
101 upath, strerror(errno));
104 else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
106 LOG(log_error, logtype_afpd,
107 "matchfile2dirperms(%s): Error adding file read permissions: %s",
108 upath, strerror(errno));
111 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
113 LOG(log_error, logtype_afpd,
114 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
115 adpath, strerror(errno));
118 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
120 LOG(log_error, logtype_afpd,
121 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
122 adpath, strerror(errno));
127 } /* end else if stat success */
130 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
136 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
144 u_int16_t fbitmap, dbitmap, vid;
150 memcpy( &vid, ibuf, sizeof( vid ));
151 ibuf += sizeof( vid );
152 if (NULL == ( vol = getvolbyvid( vid )) ) {
153 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
156 return( AFPERR_ACCESS );
159 memcpy( &did, ibuf, sizeof( did ));
160 ibuf += sizeof( did );
162 if (NULL == ( dir = dirlookup( vol, did )) ) {
166 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
167 fbitmap = ntohs( fbitmap );
168 ibuf += sizeof( fbitmap );
169 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
170 dbitmap = ntohs( dbitmap );
171 ibuf += sizeof( dbitmap );
173 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
174 return get_afp_errno(AFPERR_NOOBJ);
177 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
178 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
179 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
182 if (!s_path->st_valid) {
183 /* it's a dir and it should be there
184 * because we chdir in it in cname or
185 * it's curdir (maybe deleted, but then we can't know).
186 * So we need to try harder.
188 of_statdir(vol, s_path);
190 if ( s_path->st_errno != 0 ) {
191 if (afp_errno != AFPERR_ACCESS) {
192 return( AFPERR_NOOBJ );
198 if (S_ISDIR(st->st_mode)) {
204 ret = getdirparams(vol, dbitmap, s_path, dir,
205 rbuf + 3 * sizeof( u_int16_t ), &buflen );
209 /* this is a directory */
210 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
212 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
213 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
217 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
219 *rbuflen = buflen + 3 * sizeof( u_int16_t );
220 fbitmap = htons( fbitmap );
221 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
222 rbuf += sizeof( fbitmap );
223 dbitmap = htons( dbitmap );
224 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
225 rbuf += sizeof( dbitmap ) + sizeof( u_char );
231 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
237 u_int16_t vid, bitmap;
242 memcpy( &vid, ibuf, sizeof(vid));
243 ibuf += sizeof( vid );
245 if (NULL == ( vol = getvolbyvid( vid )) ) {
246 return( AFPERR_PARAM );
249 if (vol->v_flags & AFPVOL_RO)
252 memcpy( &did, ibuf, sizeof( did));
253 ibuf += sizeof( did);
255 if (NULL == ( dir = dirlookup( vol, did )) ) {
259 memcpy( &bitmap, ibuf, sizeof( bitmap ));
260 bitmap = ntohs( bitmap );
261 ibuf += sizeof( bitmap );
263 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
264 return get_afp_errno(AFPERR_NOOBJ);
268 if (!path->st_valid) {
269 /* it's a dir and it should be there
270 * because we chdir in it in cname
272 of_statdir(vol, path);
275 if ( path->st_errno != 0 ) {
276 if (afp_errno != AFPERR_ACCESS)
277 return( AFPERR_NOOBJ );
280 * If ibuf is odd, make it even.
282 if ((u_long)ibuf & 1 ) {
286 if (S_ISDIR(st->st_mode)) {
287 rc = setdirparams(vol, path, bitmap, ibuf );
289 rc = setfilparams(vol, path, bitmap, ibuf );
291 if ( rc == AFP_OK ) {
292 setvoltime(obj, vol );
298 /* --------------------------------------------
299 Factorise some checks on a pathname
301 int check_name(const struct vol *vol, char *name)
303 /* check for illegal characters in the unix filename */
304 if (!wincheck(vol, name))
307 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
310 if (!vol->vfs->vfs_validupath(vol, name)) {
311 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
315 /* check for vetoed filenames */
316 if (veto_file(vol->v_veto, name))
321 /* -------------------------
322 move and rename sdir:oldname to curdir:newname in volume vol
323 special care is needed for lock
325 static int moveandrename(const struct vol *vol,
332 char *oldunixname = NULL;
335 struct stat *st, nst;
339 struct ofork *opened = NULL;
344 LOG(log_debug, logtype_afpd,
345 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
346 cfrombstr(sdir->d_u_name), oldname, newname);
348 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
353 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
354 return AFPERR_PARAM; /* can't convert */
355 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
360 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
361 return AFPERR_PARAM; /* pathname too long */
362 #endif /* HAVE_ATFUNCS */
365 path.u_name = oldunixname;
368 opened = of_findnameat(sdir_fd, &path);
370 opened = of_findname(&path);
371 #endif /* HAVE_ATFUNCS */
374 /* reuse struct adouble so it won't break locks */
378 id = sdir->d_did; /* we already have the CNID */
379 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
381 adflags = ADFLAGS_DIR;
385 * oldunixname now points to either
386 * a) full pathname of the source fs object (if renameat is not available)
387 * b) the oldname (renameat is available)
388 * we are in the dest folder so we need to use
389 * a) oldunixname for ad_open
390 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
394 if ((cwd_fd = open(".", O_RDONLY)) == -1)
396 if (fchdir(sdir_fd) != 0) {
401 if (!ad_metadata(oldunixname, adflags, adp)) {
404 ad_getattr(adp, &bshort);
405 ad_close_metadata( adp);
406 if ((bshort & htons(ATTRBIT_NORENAME))) {
412 if (fchdir(cwd_fd) != 0) {
413 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
419 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
425 if (0 != (rc = check_name(vol, upath))) {
429 /* source == destination. we just silently accept this. */
430 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
431 if (strcmp(oldname, newname) == 0) {
436 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
437 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
438 /* not the same file */
444 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
451 path.st_errno = errno;
452 if (of_findname(&path)) {
453 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
455 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
457 of_rename(vol, opened, sdir, oldname, curdir, newname);
460 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
462 if ( rc == AFP_OK && id ) {
463 /* renaming may have moved the file/dir across a filesystem */
464 if (stat(upath, st) < 0) {
469 /* Remove it from the cache */
470 struct dir *cacheddir = dircache_search_by_did(vol, id);
472 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
473 (void)dir_remove(vol, cacheddir);
476 /* Fixup adouble info */
477 if (!ad_metadata(upath, adflags, adp)) {
478 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
480 ad_close_metadata(adp);
483 /* fix up the catalog entry */
484 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
495 /* -------------------------------------------- */
496 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
500 char *oldname, *newname;
511 memcpy( &vid, ibuf, sizeof( vid ));
512 ibuf += sizeof( vid );
513 if (NULL == ( vol = getvolbyvid( vid )) ) {
514 return( AFPERR_PARAM );
517 if (vol->v_flags & AFPVOL_RO)
520 memcpy( &did, ibuf, sizeof( did ));
521 ibuf += sizeof( did );
522 if (NULL == ( sdir = dirlookup( vol, did )) ) {
526 /* source pathname */
527 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
528 return get_afp_errno(AFPERR_NOOBJ);
532 newname = obj->newtmp;
533 oldname = obj->oldtmp;
534 isdir = path_isadir(path);
535 if ( *path->m_name != '\0' ) {
536 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
538 /* curdir parent dir, need to move sdir back */
543 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
544 return( AFPERR_NORENAME );
546 /* move to destination dir */
547 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
550 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
553 /* another place where we know about the path type */
554 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
555 return( AFPERR_PARAM );
559 return AFP_OK; /* newname == oldname same dir */
562 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
563 if ( rc == AFP_OK ) {
564 setvoltime(obj, vol );
570 /* ------------------------------- */
571 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
583 memcpy( &vid, ibuf, sizeof( vid ));
584 ibuf += sizeof( vid );
585 if (NULL == ( vol = getvolbyvid( vid )) ) {
586 return( AFPERR_PARAM );
589 if (vol->v_flags & AFPVOL_RO)
592 memcpy( &did, ibuf, sizeof( did ));
593 ibuf += sizeof( int );
595 if (NULL == ( dir = dirlookup( vol, did )) ) {
599 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
600 return get_afp_errno(AFPERR_NOOBJ);
603 upath = s_path->u_name;
604 if ( path_isadir( s_path) ) {
605 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
608 /* we have to cache this, the structs are lost in deletcurdir*/
609 /* but we need the positive returncode to send our event */
611 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
613 if ((rc = deletecurdir(vol)) == AFP_OK)
614 fce_register_delete_dir(cfrombstr(dname));
617 } else if (of_findname(s_path)) {
620 /* it's a file st_valid should always be true
621 * only test for ENOENT because EACCES needs
622 * to read meta data in deletefile
624 if (s_path->st_valid && s_path->st_errno == ENOENT) {
627 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
628 fce_register_delete_file( s_path );
629 if (vol->v_tm_used < s_path->st.st_size)
632 vol->v_tm_used -= s_path->st.st_size;
634 struct dir *cachedfile;
635 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
636 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
637 dir_free(cachedfile);
641 if ( rc == AFP_OK ) {
643 setvoltime(obj, vol );
648 /* ------------------------ */
649 char *absupath(const struct vol *vol, struct dir *dir, char *u)
651 static char pathbuf[MAXPATHLEN + 1];
654 if (u == NULL || dir == NULL || vol == NULL)
657 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
659 if (bcatcstr(path, "/") != BSTR_OK)
661 if (bcatcstr(path, u) != BSTR_OK)
663 if (path->slen > MAXPATHLEN)
666 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
668 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
674 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
676 if (vol == NULL || dir == NULL || name == NULL)
678 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
681 /* ------------------------- */
682 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
685 struct dir *sdir, *ddir;
687 char *oldname, *newname;
696 #endif /* DROPKLUDGE */
703 memcpy( &vid, ibuf, sizeof( vid ));
704 ibuf += sizeof( vid );
705 if (NULL == ( vol = getvolbyvid( vid )) ) {
706 return( AFPERR_PARAM );
709 if (vol->v_flags & AFPVOL_RO)
712 /* source did followed by dest did */
713 memcpy( &did, ibuf, sizeof( did ));
714 ibuf += sizeof( int );
715 if (NULL == ( sdir = dirlookup( vol, did )) ) {
716 return afp_errno; /* was AFPERR_PARAM */
719 memcpy( &did, ibuf, sizeof( did ));
720 ibuf += sizeof( int );
722 /* source pathname */
723 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
724 return get_afp_errno(AFPERR_NOOBJ);
728 newname = obj->newtmp;
729 oldname = obj->oldtmp;
731 isdir = path_isadir(path);
732 if ( *path->m_name != '\0' ) {
736 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
738 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
742 if ((sdir_fd = open(".", O_RDONLY)) == -1)
746 /* get the destination directory */
747 if (NULL == ( ddir = dirlookup( vol, did )) ) {
748 rc = afp_errno; /* was AFPERR_PARAM */
751 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
755 pdid = curdir->d_did;
756 if ( *path->m_name != '\0' ) {
757 rc = path_error(path, AFPERR_NOOBJ);
761 /* one more place where we know about path type */
762 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
768 strcpy(newname, oldname);
771 /* This does the work */
772 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
773 oldname, newname, isdir);
774 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
776 if ( rc == AFP_OK ) {
777 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
786 if (vol->v_flags & AFPVOL_DROPBOX) {
787 /* FIXME did is not always the source id */
788 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
794 #endif /* DROPKLUDGE */
795 /* if unix priv don't try to match perm with dest folder */
796 if (!isdir && !vol_unix_priv(vol)) {
797 int admode = ad_mode("", 0777) | vol->v_fperm;
799 setfilmode(upath, admode, NULL, vol->v_umask);
800 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
802 setvoltime(obj, vol );
814 int veto_file(const char*veto_str, const char*path)
815 /* given a veto_str like "abc/zxc/" and path "abc", return 1
816 * veto_str should be '/' delimited
817 * if path matches any one of the veto_str elements exactly, then 1 is returned
818 * otherwise, 0 is returned.
821 int i; /* index to veto_str */
822 int j; /* index to path */
824 if ((veto_str == NULL) || (path == NULL))
827 for(i=0, j=0; veto_str[i] != '\0'; i++) {
828 if (veto_str[i] == '/') {
829 if ((j>0) && (path[j] == '\0')) {
830 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
835 if (veto_str[i] != path[j]) {
836 while ((veto_str[i] != '/')
837 && (veto_str[i] != '\0'))