2 * $Id: filedir.c,v 1.73 2010/03/12 15:16:49 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #else /* STDC_HEADERS */
21 #endif /* HAVE_STRCHR */
22 char *strchr (), *strrchr ();
24 #define memcpy(d,s,n) bcopy ((s), (d), (n))
25 #define memmove(d,s,n) bcopy ((s), (d), (n))
26 #endif /* ! HAVE_MEMCPY */
27 #endif /* STDC_HEADERS */
33 #include <sys/param.h>
35 #include <atalk/adouble.h>
36 #include <atalk/vfs.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
41 #include <atalk/unix.h>
42 #include <atalk/bstrlib.h>
43 #include <atalk/bstradd.h>
45 #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), cfrombstring(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,
335 struct stat *st, nst;
339 struct ofork *opened = NULL;
344 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
349 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
350 return AFPERR_PARAM; /* can't convert */
352 #ifndef HAVE_RENAMEAT
354 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
355 p = ctoupath( vol, sdir, oldname );
357 return AFPERR_PARAM; /* pathname too long */
358 #endif /* HAVE_RENAMEAT */
363 opened = of_findnameat(sdir_fd, &path);
365 opened = of_findname(&path);
366 #endif /* HAVE_RENAMEAT */
368 /* reuse struct adouble so it won't break locks */
372 id = sdir->d_did; /* we already have the CNID */
373 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
377 adflags = ADFLAGS_DIR;
381 * p now points to either
382 * a) full pathname of the source fs object (if renameat is not available)
383 * b) the oldname (renameat is available)
384 * we are in the dest folder so we need to use
386 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
390 if ((cwd_fd = open(".", O_RDONLY)) == -1)
392 if (fchdir(sdir_fd) != 0)
395 if (!ad_metadata(p, adflags, adp)) {
398 ad_getattr(adp, &bshort);
399 ad_close_metadata( adp);
400 if ((bshort & htons(ATTRBIT_NORENAME)))
401 return(AFPERR_OLOCK);
404 if (fchdir(cwd_fd) != 0) {
405 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
410 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
415 if (0 != (rc = check_name(vol, upath))) {
419 /* source == destination. we just silently accept this. */
420 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
421 if (strcmp(oldname, newname) == 0)
424 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
425 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
426 /* not the same file */
431 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
436 path.st_errno = errno;
437 if (of_findname(&path)) {
438 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
440 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
442 of_rename(vol, opened, sdir, oldname, curdir, newname);
445 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
447 if ( rc == AFP_OK && id ) {
448 /* renaming may have moved the file/dir across a filesystem */
449 if (stat(upath, st) < 0)
452 /* fix up the catalog entry */
453 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
459 /* -------------------------------------------- */
460 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
464 char *oldname, *newname;
475 memcpy( &vid, ibuf, sizeof( vid ));
476 ibuf += sizeof( vid );
477 if (NULL == ( vol = getvolbyvid( vid )) ) {
478 return( AFPERR_PARAM );
481 if (vol->v_flags & AFPVOL_RO)
484 memcpy( &did, ibuf, sizeof( did ));
485 ibuf += sizeof( did );
486 if (NULL == ( sdir = dirlookup( vol, did )) ) {
490 /* source pathname */
491 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
492 return get_afp_errno(AFPERR_NOOBJ);
496 newname = obj->newtmp;
497 oldname = obj->oldtmp;
498 isdir = path_isadir(path);
499 if ( *path->m_name != '\0' ) {
500 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
502 /* curdir parent dir, need to move sdir back */
507 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
508 return( AFPERR_NORENAME );
510 /* move to destination dir */
511 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
514 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) +1);
517 /* another place where we know about the path type */
518 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
519 return( AFPERR_PARAM );
523 return AFP_OK; /* newname == oldname same dir */
526 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
527 if ( rc == AFP_OK ) {
528 setvoltime(obj, vol );
534 /* ------------------------------- */
535 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
547 memcpy( &vid, ibuf, sizeof( vid ));
548 ibuf += sizeof( vid );
549 if (NULL == ( vol = getvolbyvid( vid )) ) {
550 return( AFPERR_PARAM );
553 if (vol->v_flags & AFPVOL_RO)
556 memcpy( &did, ibuf, sizeof( did ));
557 ibuf += sizeof( int );
559 if (NULL == ( dir = dirlookup( vol, did )) ) {
563 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
564 return get_afp_errno(AFPERR_NOOBJ);
567 upath = s_path->u_name;
568 if ( path_isadir( s_path) ) {
569 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
572 rc = deletecurdir( vol);
573 } else if (of_findname(s_path)) {
576 /* it's a file st_valid should always be true
577 * only test for ENOENT because EACCES needs
578 * to read meta data in deletefile
580 if (s_path->st_valid && s_path->st_errno == ENOENT) {
584 rc = deletefile(vol, -1, upath, 1);
586 struct dir *cachedfile;
587 if (cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath))) {
588 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
589 dir_free(cachedfile);
593 if ( rc == AFP_OK ) {
595 setvoltime(obj, vol );
600 /* ------------------------ */
601 char *absupath(const struct vol *vol, struct dir *dir, char *u)
603 static char pathbuf[MAXPATHLEN + 1];
606 if (u == NULL || dir == NULL || vol == NULL)
609 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
611 if (bcatcstr(path, "/") != BSTR_OK)
613 if (bcatcstr(path, u) != BSTR_OK)
615 if (path->slen > MAXPATHLEN)
618 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstring(path));
620 strncpy(pathbuf, cfrombstring(path), blength(path) + 1);
626 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
628 if (vol == NULL || dir == NULL || name == NULL)
630 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
633 /* ------------------------- */
634 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
637 struct dir *sdir, *ddir;
639 char *oldname, *newname;
648 #endif /* DROPKLUDGE */
655 memcpy( &vid, ibuf, sizeof( vid ));
656 ibuf += sizeof( vid );
657 if (NULL == ( vol = getvolbyvid( vid )) ) {
658 return( AFPERR_PARAM );
661 if (vol->v_flags & AFPVOL_RO)
664 /* source did followed by dest did */
665 memcpy( &did, ibuf, sizeof( did ));
666 ibuf += sizeof( int );
667 if (NULL == ( sdir = dirlookup( vol, did )) ) {
668 return afp_errno; /* was AFPERR_PARAM */
671 memcpy( &did, ibuf, sizeof( did ));
672 ibuf += sizeof( int );
674 /* source pathname */
675 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
676 return get_afp_errno(AFPERR_NOOBJ);
680 newname = obj->newtmp;
681 oldname = obj->oldtmp;
683 isdir = path_isadir(path);
684 if ( *path->m_name != '\0' ) {
688 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
690 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) + 1);
694 if ((sdir_fd = open(".", O_RDONLY)) == -1)
698 /* get the destination directory */
699 if (NULL == ( ddir = dirlookup( vol, did )) ) {
700 rc = afp_errno; /* was AFPERR_PARAM */
703 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
707 pdid = curdir->d_did;
708 if ( *path->m_name != '\0' ) {
709 rc = path_error(path, AFPERR_NOOBJ);
713 /* one more place where we know about path type */
714 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
720 strcpy(newname, oldname);
723 /* This does the work */
724 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
725 oldname, newname, isdir);
726 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
728 if ( rc == AFP_OK ) {
729 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
738 if (vol->v_flags & AFPVOL_DROPBOX) {
739 /* FIXME did is not always the source id */
740 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
746 #endif /* DROPKLUDGE */
747 /* if unix priv don't try to match perm with dest folder */
748 if (!isdir && !vol_unix_priv(vol)) {
749 int admode = ad_mode("", 0777) | vol->v_fperm;
751 setfilmode(upath, admode, NULL, vol->v_umask);
752 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
754 setvoltime(obj, vol );
766 int veto_file(const char*veto_str, const char*path)
767 /* given a veto_str like "abc/zxc/" and path "abc", return 1
768 * veto_str should be '/' delimited
769 * if path matches any one of the veto_str elements exactly, then 1 is returned
770 * otherwise, 0 is returned.
773 int i; /* index to veto_str */
774 int j; /* index to path */
776 if ((veto_str == NULL) || (path == NULL))
779 for(i=0, j=0; veto_str[i] != '\0'; i++) {
780 if (veto_str[i] == '/') {
781 if ((j>0) && (path[j] == '\0')) {
782 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
787 if (veto_str[i] != path[j]) {
788 while ((veto_str[i] != '/')
789 && (veto_str[i] != '\0'))