2 * $Id: filedir.c,v 1.45.2.2.2.3 2003-10-30 05:57:44 bfernhomberg 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 */
30 #endif /* HAVE_UNISTD_H */
33 #endif /* HAVE_FCNTL_H */
39 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <netatalk/endian.h>
43 #include <atalk/adouble.h>
44 #include <atalk/afp.h>
45 #include <atalk/util.h>
46 #include <atalk/cnid.h>
47 #include <atalk/logger.h>
49 #include "directory.h"
59 int matchfile2dirperms(upath, vol, did)
60 /* Since it's kinda' big; I decided against an
65 /* The below code changes the way file ownership is determined in the name of
66 fixing dropboxes. It has known security problem. See the netatalk FAQ for
75 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
78 if (stat(upath, &st ) < 0) {
79 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
83 adpath = ad_path( upath, ADFLAGS_HF );
84 /* FIXME dirsearch doesn't move cwd to did ! */
85 if (( dir = dirlookup( vol, did )) == NULL ) {
86 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
89 else if (stat(".", &sb) < 0) {
90 LOG(log_error, logtype_afpd,
91 "matchfile2dirperms: Error checking directory \"%s\": %s",
92 dir->d_m_name, strerror(errno));
97 if ( uid != sb.st_uid )
100 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms: Error changing owner/gid of %s: %s",
104 upath, strerror(errno));
107 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
109 LOG(log_error, logtype_afpd,
110 "matchfile2dirperms: Error adding file read permissions: %s",
114 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
116 LOG(log_error, logtype_afpd,
117 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
118 adpath, strerror(errno));
121 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
123 LOG(log_error, logtype_afpd,
124 "matchfile2dirperms: Error adding AD file read permissions: %s",
130 } /* end else if stat success */
133 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
139 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
142 int ibuflen, *rbuflen;
149 u_int16_t fbitmap, dbitmap, vid;
153 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
159 memcpy( &vid, ibuf, sizeof( vid ));
160 ibuf += sizeof( vid );
161 if (NULL == ( vol = getvolbyvid( vid )) ) {
162 return( AFPERR_PARAM );
165 memcpy( &did, ibuf, sizeof( did ));
166 ibuf += sizeof( did );
168 if (NULL == ( dir = dirlookup( vol, did )) ) {
172 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
173 fbitmap = ntohs( fbitmap );
174 ibuf += sizeof( fbitmap );
175 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
176 dbitmap = ntohs( dbitmap );
177 ibuf += sizeof( dbitmap );
179 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
180 return get_afp_errno(AFPERR_NOOBJ);
184 if (!s_path->st_valid) {
185 /* it's a dir and it should be there
186 * because we chdir in it in cname or
187 * it's curdir (maybe deleted, but then we can't know)
192 if ( s_path->st_errno != 0 ) {
193 return( AFPERR_NOOBJ );
197 if (S_ISDIR(st->st_mode)) {
203 ret = getdirparams(vol, dbitmap, s_path, dir,
204 rbuf + 3 * sizeof( u_int16_t ), &buflen );
208 /* this is a directory */
209 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
211 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
212 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
216 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
218 *rbuflen = buflen + 3 * sizeof( u_int16_t );
219 fbitmap = htons( fbitmap );
220 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
221 rbuf += sizeof( fbitmap );
222 dbitmap = htons( dbitmap );
223 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
224 rbuf += sizeof( dbitmap ) + sizeof( u_char );
228 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
234 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
237 int ibuflen, *rbuflen;
243 u_int16_t vid, bitmap;
247 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
252 memcpy( &vid, ibuf, sizeof(vid));
253 ibuf += sizeof( vid );
255 if (NULL == ( vol = getvolbyvid( vid )) ) {
256 return( AFPERR_PARAM );
259 if (vol->v_flags & AFPVOL_RO)
262 memcpy( &did, ibuf, sizeof( did));
263 ibuf += sizeof( did);
265 if (NULL == ( dir = dirlookup( vol, did )) ) {
269 memcpy( &bitmap, ibuf, sizeof( bitmap ));
270 bitmap = ntohs( bitmap );
271 ibuf += sizeof( bitmap );
273 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
274 return get_afp_errno(AFPERR_NOOBJ);
278 if (!path->st_valid) {
279 /* it's a dir and it should be there
280 * because we chdir in it in cname
285 if ( path->st_errno != 0 ) {
286 return( AFPERR_NOOBJ );
289 * If ibuf is odd, make it even.
291 if ((u_long)ibuf & 1 ) {
295 if (S_ISDIR(st->st_mode)) {
296 rc = setdirparams(vol, path, bitmap, ibuf );
298 rc = setfilparams(vol, path, bitmap, ibuf );
300 if ( rc == AFP_OK ) {
301 setvoltime(obj, vol );
305 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
311 /* --------------------------------------------
312 Factorise some check on a pathname
314 int check_name(const struct vol *vol, char *name)
316 /* check for illegal characters in the unix filename */
317 if (!wincheck(vol, name))
320 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
323 if (!validupath(vol, name))
326 /* check for vetoed filenames */
327 if (veto_file(vol->v_veto, name))
332 /* -------------------------
333 move and rename sdir:oldname to curdir:newname in volume vol
335 special care is needed for lock
337 static int moveandrename(vol, sdir, oldname, newname, isdir)
338 const struct vol *vol;
351 struct ofork *opened = NULL;
355 ad_init(&ad, vol->v_adouble);
360 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
362 return AFPERR_PARAM; /* can't convert */
364 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
365 p = ctoupath( vol, sdir, oldname );
367 return AFPERR_PARAM; /* pathname too long */
371 if ((opened = of_findname(&path))) {
372 /* reuse struct adouble so it won't break locks */
377 id = sdir->d_did; /* we already have the CNID */
378 p = ctoupath( vol, sdir->d_parent, oldname );
382 adflags = ADFLAGS_DIR;
385 * p now points to the full pathname of the source fs object.
387 * we are in the dest folder so we need to use p for ad_open
390 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
393 ad_getattr(adp, &bshort);
394 ad_close( adp, ADFLAGS_HF );
395 if ((bshort & htons(ATTRBIT_NORENAME)))
396 return(AFPERR_OLOCK);
399 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 (curdir == sdir) {
410 if (strcmp(oldname, newname) == 0)
413 /* deal with case insensitive, case-preserving filesystems. */
414 if ((stat(upath, st) == 0) && strdiacasecmp(oldname, newname))
417 } else if (stat(upath, st ) == 0)
422 path.st_errno = errno;
423 if (of_findname(&path)) {
424 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
426 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
428 of_rename(vol, opened, sdir, oldname, curdir, newname);
431 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
433 if ( rc == AFP_OK && id ) {
434 /* renaming may have moved the file/dir across a filesystem */
435 if (stat(upath, st) < 0)
438 /* fix up the catalog entry */
439 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
445 /* -------------------------------------------- */
446 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
449 int ibuflen, *rbuflen;
453 char *oldname, *newname;
461 LOG(log_info, logtype_afpd, "begin afp_rename:");
467 memcpy( &vid, ibuf, sizeof( vid ));
468 ibuf += sizeof( vid );
469 if (NULL == ( vol = getvolbyvid( vid )) ) {
470 return( AFPERR_PARAM );
473 if (vol->v_flags & AFPVOL_RO)
476 memcpy( &did, ibuf, sizeof( did ));
477 ibuf += sizeof( did );
478 if (NULL == ( sdir = dirlookup( vol, did )) ) {
482 /* source pathname */
483 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
484 return get_afp_errno(AFPERR_NOOBJ);
488 newname = obj->newtmp;
489 oldname = obj->oldtmp;
490 isdir = path_isadir(path);
491 if ( *path->m_name != '\0' ) {
492 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
494 /* curdir parent dir, need to move sdir back */
499 if ( sdir->d_parent == NULL ) { /* root directory */
500 return( AFPERR_NORENAME );
502 /* move to destination dir */
503 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
506 strcpy(oldname, sdir->d_m_name);
509 /* another place where we know about the path type */
510 if ((plen = copy_path_name(newname, ibuf)) < 0) {
511 return( AFPERR_PARAM );
515 return AFP_OK; /* newname == oldname same dir */
518 rc = moveandrename(vol, sdir, oldname, newname, isdir);
520 if ( rc == AFP_OK ) {
521 setvoltime(obj, vol );
525 LOG(log_info, logtype_afpd, "end afp_rename:");
531 /* ------------------------------- */
532 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
535 int ibuflen, *rbuflen;
545 LOG(log_info, logtype_afpd, "begin afp_delete:");
551 memcpy( &vid, ibuf, sizeof( vid ));
552 ibuf += sizeof( vid );
553 if (NULL == ( vol = getvolbyvid( vid )) ) {
554 return( AFPERR_PARAM );
557 if (vol->v_flags & AFPVOL_RO)
560 memcpy( &did, ibuf, sizeof( did ));
561 ibuf += sizeof( int );
562 if (NULL == ( dir = dirlookup( vol, did )) ) {
566 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
567 return get_afp_errno(AFPERR_NOOBJ);
570 upath = s_path->u_name;
571 if ( path_isadir( s_path) ) {
572 if (*s_path->m_name != '\0') {
576 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
578 } else if (of_findname(s_path)) {
581 rc = deletefile(vol, upath, 1);
583 if ( rc == AFP_OK ) {
585 setvoltime(obj, vol );
589 LOG(log_info, logtype_afpd, "end afp_delete:");
594 /* ------------------------ */
595 char *absupath( vol, dir, u )
596 const struct vol *vol;
601 static char path[ MAXPATHLEN + 1];
608 p = path + sizeof( path ) - 1;
612 strncpy( p, u, len );
613 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
616 if (p -len -1 < path) {
618 rather rare so LOG error and/or client message ?
624 strncpy( p, u, len );
626 len = strlen( vol->v_path );
627 if (p -len -1 < path) {
632 strncpy( p, vol->v_path, len );
637 /* ------------------------
638 * FIXME dir could be NULL
640 char *ctoupath( vol, dir, name )
641 const struct vol *vol;
645 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
648 /* ------------------------- */
649 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
652 int ibuflen, *rbuflen;
655 struct dir *sdir, *ddir;
657 char *oldname, *newname;
666 #endif /* DROPKLUDGE */
669 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
675 memcpy( &vid, ibuf, sizeof( vid ));
676 ibuf += sizeof( vid );
677 if (NULL == ( vol = getvolbyvid( vid )) ) {
678 return( AFPERR_PARAM );
681 if (vol->v_flags & AFPVOL_RO)
684 /* source did followed by dest did */
685 memcpy( &did, ibuf, sizeof( did ));
686 ibuf += sizeof( int );
687 if (NULL == ( sdir = dirlookup( vol, did )) ) {
688 return afp_errno; /* was AFPERR_PARAM */
691 memcpy( &did, ibuf, sizeof( did ));
692 ibuf += sizeof( int );
694 /* source pathname */
695 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
696 return get_afp_errno(AFPERR_NOOBJ);
700 newname = obj->newtmp;
701 oldname = obj->oldtmp;
703 isdir = path_isadir(path);
704 if ( *path->m_name != '\0' ) {
708 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
710 strcpy(oldname, sdir->d_m_name);
713 /* get the destination directory */
714 if (NULL == ( ddir = dirlookup( vol, did )) ) {
715 return afp_errno; /* was AFPERR_PARAM */
717 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
718 return( AFPERR_NOOBJ );
720 pdid = curdir->d_did;
721 if ( *path->m_name != '\0' ) {
722 return path_error(path, AFPERR_NOOBJ);
725 /* one more place where we know about path type */
726 if ((plen = copy_path_name(newname, ibuf)) < 0) {
727 return( AFPERR_PARAM );
731 strcpy(newname, oldname);
734 rc = moveandrename(vol, sdir, oldname, newname, isdir);
736 if ( rc == AFP_OK ) {
737 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
745 if (vol->v_flags & AFPVOL_DROPBOX) {
746 /* FIXME did is not always the source id */
747 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);
757 setfilmode(upath, admode, NULL);
758 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
760 setvoltime(obj, vol );
764 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
770 int veto_file(const char*veto_str, const char*path)
771 /* given a veto_str like "abc/zxc/" and path "abc", return 1
772 * veto_str should be '/' delimited
773 * if path matches any one of the veto_str elements exactly, then 1 is returned
774 * otherwise, 0 is returned.
777 int i; /* index to veto_str */
778 int j; /* index to path */
780 if ((veto_str == NULL) || (path == NULL))
784 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
787 for(i=0, j=0; veto_str[i] != '\0'; i++) {
788 if (veto_str[i] == '/') {
789 if ((j>0) && (path[j] == '\0'))
793 if (veto_str[i] != path[j]) {
794 while ((veto_str[i] != '/')
795 && (veto_str[i] != '\0'))