2 * $Id: unix.c,v 1.43.2.1.2.9 2004-06-15 00:46:01 bfernhomberg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
27 #define memcpy(d,s,n) bcopy ((s), (d), (n))
28 #define memmove(d,s,n) bcopy ((s), (d), (n))
29 #endif /* ! HAVE_MEMCPY */
30 #endif /* STDC_HEADERS */
35 #include <sys/param.h>
37 #include <atalk/logger.h>
38 #include <atalk/adouble.h>
39 #include <atalk/afp.h>
40 #include <atalk/util.h>
43 #include "directory.h"
49 * Get the free space on a partition.
51 int ustatfs_getvolspace( vol, bfree, btotal, bsize )
52 const struct vol *vol;
53 VolSpace *bfree, *btotal;
56 VolSpace maxVolSpace = (~(VolSpace)0);
65 if ( statfs( vol->v_path, &sfs ) < 0 ) {
66 LOG(log_error, logtype_afpd, "ustatfs_getvolspace unable to stat %s", vol->v_path);
67 return( AFPERR_PARAM );
71 *bfree = (VolSpace) sfs.fd_req.bfreen;
74 *bfree = (VolSpace) sfs.f_bavail;
75 *bsize = sfs.f_frsize;
78 if ( *bfree > maxVolSpace / *bsize ) {
86 ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
89 ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
92 /* see similar block above comments */
93 if ( *btotal > maxVolSpace / *bsize ) {
94 *btotal = maxVolSpace;
102 static __inline__ int utombits( bits )
109 mbits |= ( bits & ( S_IREAD >> 6 )) ? AR_UREAD : 0;
110 mbits |= ( bits & ( S_IWRITE >> 6 )) ? AR_UWRITE : 0;
111 /* Do we really need this? */
112 mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0;
117 /* --------------------------------
120 void utommode( stat, ma )
126 mode = stat->st_mode;
127 ma->ma_world = utombits( mode );
130 ma->ma_group = utombits( mode );
133 ma->ma_owner = utombits( mode );
135 /* ma_user is a union of all permissions but we must follow
138 if ( (uuid == stat->st_uid) || (uuid == 0)) {
139 ma->ma_user = ma->ma_owner | AR_UOWN;
141 else if ( gmem( stat->st_gid )) {
142 ma->ma_user = ma->ma_group;
145 ma->ma_user = ma->ma_world;
149 * There are certain things the mac won't try if you don't have
150 * the "owner" bit set, even tho you can do these things on unix wiht
151 * only write permission. What were the things?
153 * FIXME and so what ?
156 if ( ma->ma_user & AR_UWRITE ) {
157 ma->ma_user |= AR_UOWN;
167 * Calculate the mode for a directory using a stat() call to
168 * estimate permission.
170 * Note: the previous method, using access(), does not work correctly
172 * FIXME what about ACL?
174 void accessmode( path, ma, dir, st )
183 ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
185 if (stat(path, &sb) != 0)
198 for ( i = 0; i < ngroups; i++ ) {
199 if ( groups[ i ] == gid ) {
206 static __inline__ mode_t mtoubits( bits )
213 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
214 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
215 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
216 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
221 /* ----------------------------------
222 from the finder's share windows (menu--> File--> sharing...)
223 and from AFP 3.0 spec page 63
224 the mac mode should be save somewhere
226 mode_t mtoumode( ma )
232 mode |= mtoubits( ma->ma_owner |ma->ma_world);
235 mode |= mtoubits( ma->ma_group |ma->ma_world);
238 mode |= mtoubits( ma->ma_world );
243 /* ----------------------------- */
244 char *fullpathname(const char *name)
246 static char wd[ MAXPATHLEN + 1];
248 if ( getcwd( wd , MAXPATHLEN) ) {
249 strlcat(wd, "/", MAXPATHLEN);
250 strlcat(wd, name, MAXPATHLEN);
253 strlcpy(wd, name, MAXPATHLEN);
258 /* -----------------------------
259 a dropbox is a folder where w is set but not r eg:
260 rwx-wx-wx or rwx-wx--
261 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
263 static int stickydirmode(name, mode, dropbox)
271 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
275 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
276 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
279 if ( seteuid(0) < 0) {
280 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
282 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) )) < 0) {
283 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
286 LOG(log_info, logtype_afpd, "stickydirmode: (debug) chmod \"%s\": %s", fullpathname(name), strerror(retval) );
293 #endif /* DROPKLUDGE */
296 * Ignore EPERM errors: We may be dealing with a directory that is
297 * group writable, in which case chmod will fail.
299 if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM) {
300 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
307 /* ------------------------- */
308 int dir_rx_set(mode_t mode)
310 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
313 #define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
315 int setdeskmode( mode )
318 char wd[ MAXPATHLEN + 1];
320 char modbuf[ 12 + 1], *m;
321 struct dirent *deskp, *subp;
324 if (!dir_rx_set(mode)) {
325 /* want to remove read and search access to owner it will screw the volume */
328 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
331 if ( chdir( ".AppleDesktop" ) < 0 ) {
334 if (( desk = opendir( "." )) == NULL ) {
335 if ( chdir( wd ) < 0 ) {
336 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
340 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
341 if ( strcmp( deskp->d_name, "." ) == 0 ||
342 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
345 strcpy( modbuf, deskp->d_name );
346 strcat( modbuf, "/" );
347 m = strchr( modbuf, '\0' );
348 if (( sub = opendir( deskp->d_name )) == NULL ) {
351 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
352 if ( strcmp( subp->d_name, "." ) == 0 ||
353 strcmp( subp->d_name, ".." ) == 0 ) {
357 strcat( modbuf, subp->d_name );
358 /* XXX: need to preserve special modes */
359 if (stat(modbuf, &st) < 0) {
360 LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
364 if (S_ISDIR(st.st_mode)) {
365 if ( chmod( modbuf, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
366 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
368 } else if ( chmod( modbuf, mode & ~(default_options.umask | EXEC_MODE) ) < 0 && errno != EPERM ) {
369 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
374 /* XXX: need to preserve special modes */
375 if ( chmod( deskp->d_name, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
376 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
380 if ( chdir( wd ) < 0 ) {
381 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
384 /* XXX: need to preserve special modes */
385 if ( chmod( ".AppleDesktop", (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
386 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
391 /* --------------------- */
392 int setfilunixmode (vol, path, mode)
393 const struct vol *vol;
397 if (!path->st_valid) {
401 if (path->st_errno) {
405 if (setfilmode( path->u_name, mode, &path->st) < 0)
407 /* we need to set write perm if read set for resource fork */
408 return setfilmode(vol->ad_path( path->u_name, ADFLAGS_HF ), ad_hf_mode(mode), &path->st);
411 /* --------------------- */
412 int setfilmode(name, mode, st)
418 mode_t mask = S_IRUSR |S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH;
421 if (stat(name, &sb) != 0)
425 mode &= mask; /* keep only rw-rw-rw in mode */
426 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
427 if ( chmod( name, mode & ~default_options.umask ) < 0 && errno != EPERM ) {
433 /* --------------------- */
434 int setdirunixmode( vol, name, mode )
435 const struct vol *vol;
439 char *adouble = vol->ad_path( name, ADFLAGS_DIR );
441 int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
443 if (dir_rx_set(mode)) {
444 /* extending right? dir first then .AppleDouble */
445 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
447 if (vol->v_adouble != AD_VERSION2_OSX) {
448 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
453 if (setfilmode(adouble, ad_hf_mode(mode), NULL) < 0 && !vol_noadouble(vol)) {
456 if (!dir_rx_set(mode)) {
457 if (vol->v_adouble != AD_VERSION2_OSX) {
458 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
462 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
468 /* --------------------- */
469 int setdirmode( vol, name, mode )
470 const struct vol *vol;
474 char buf[ MAXPATHLEN + 1];
479 int osx = vol->v_adouble == AD_VERSION2_OSX;
480 int hf_mode = ad_hf_mode(mode);
481 int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
482 char *adouble = vol->ad_path( name, ADFLAGS_DIR );
483 char *adouble_p = ad_dir(adouble);
485 if (dir_rx_set(mode)) {
486 /* extending right? dir first then .AppleDouble */
487 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
490 if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
496 if (( dir = opendir( name )) == NULL ) {
497 LOG(log_error, logtype_afpd, "setdirmode: opendir: %s", fullpathname(name), strerror(errno) );
501 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
503 if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
506 if ( stat( dirp->d_name, &st ) < 0 ) {
507 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s",dirp->d_name, strerror(errno) );
511 if (!S_ISDIR(st.st_mode)) {
512 int setmode = (osx && *dirp->d_name == '.')?hf_mode:mode;
514 if (setfilmode(dirp->d_name, setmode, &st) < 0) {
515 LOG(log_error, logtype_afpd, "setdirmode: chmod %s: %s",dirp->d_name, strerror(errno) );
520 /* Don't change subdir perm */
521 else if (S_ISDIR(st.st_mode)) {
522 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
524 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
532 goto setdirmode_noadouble;
535 /* change perm of .AppleDouble's files
537 if (( dir = opendir( adouble_p )) == NULL ) {
538 if (vol_noadouble(vol))
539 goto setdirmode_noadouble;
540 LOG(log_error, logtype_afpd, "setdirmode: opendir %s: %s", fullpathname(".AppleDouble"),strerror(errno) );
543 strcpy( buf, adouble_p);
545 m = strchr( buf, '\0' );
546 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
547 if ( strcmp( dirp->d_name, "." ) == 0 ||
548 strcmp( dirp->d_name, ".." ) == 0 ) {
552 strcat( buf, dirp->d_name );
554 if ( stat( buf, &st ) < 0 ) {
555 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
558 if (!S_ISDIR(st.st_mode)) {
559 if (setfilmode(buf, hf_mode , &st) < 0) {
560 /* FIXME what do we do then? */
566 if (!dir_rx_set(mode)) {
567 /* XXX: need to preserve special modes */
568 if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 ) {
573 setdirmode_noadouble:
574 if (!dir_rx_set(mode)) {
575 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
581 int setdeskowner( uid, gid )
585 char wd[ MAXPATHLEN + 1];
586 char modbuf[12 + 1], *m;
587 struct dirent *deskp, *subp;
590 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
593 if ( chdir( ".AppleDesktop" ) < 0 ) {
596 if (( desk = opendir( "." )) == NULL ) {
597 if ( chdir( wd ) < 0 ) {
598 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
602 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
603 if ( strcmp( deskp->d_name, "." ) == 0 ||
604 strcmp( deskp->d_name, ".." ) == 0 ||
605 strlen( deskp->d_name ) > 2 ) {
608 strcpy( modbuf, deskp->d_name );
609 strcat( modbuf, "/" );
610 m = strchr( modbuf, '\0' );
611 if (( sub = opendir( deskp->d_name )) == NULL ) {
614 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
615 if ( strcmp( subp->d_name, "." ) == 0 ||
616 strcmp( subp->d_name, ".." ) == 0 ) {
620 strcat( modbuf, subp->d_name );
621 /* XXX: add special any uid, ignore group bits */
622 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
623 LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
627 /* XXX: add special any uid, ignore group bits */
628 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
629 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s",
630 deskp->d_name, strerror(errno) );
634 if ( chdir( wd ) < 0 ) {
635 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
638 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
639 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
644 /* ----------------------------- */
645 int setfilowner(vol, uid, gid, path)
646 const struct vol *vol;
654 if (!path->st_valid) {
658 if (path->st_errno) {
662 if ( chown( path->u_name, uid, gid ) < 0 && errno != EPERM ) {
663 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
664 uid, gid, path->u_name, strerror(errno) );
668 ad_p = vol->ad_path( path->u_name, ADFLAGS_HF );
670 if ( stat( ad_p, &st ) < 0 ) {
674 if ( chown( ad_p, uid, gid ) < 0 &&
676 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
677 uid, gid, ad_p, strerror(errno) );
684 /* ---------------------------------
685 * uid/gid == 0 need to be handled as special cases. they really mean
686 * that user/group should inherit from other, but that doesn't fit
687 * into the unix permission scheme. we can get around this by
688 * co-opting some bits. */
689 int setdirowner(vol, name, uid, gid )
690 const struct vol *vol;
695 char buf[ MAXPATHLEN + 1];
700 int osx = vol->v_adouble == AD_VERSION2_OSX;
701 int noadouble = vol_noadouble(vol);
705 if (( dir = opendir( name )) == NULL ) {
708 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
709 if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
712 if ( stat( dirp->d_name, &st ) < 0 ) {
713 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s",
714 fullpathname(dirp->d_name), strerror(errno) );
717 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
718 if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
719 LOG(log_debug, logtype_afpd, "setdirowner: chown %s: %s",
720 fullpathname(dirp->d_name), strerror(errno) );
721 /* return ( -1 ); Sometimes this is okay */
728 goto setdirowner_noadouble;
731 adouble = vol->ad_path( name, ADFLAGS_DIR );
732 adouble_p = ad_dir(adouble);
733 if (( dir = opendir( adouble_p )) == NULL ) {
735 goto setdirowner_noadouble;
738 strcpy( buf, adouble_p );
740 m = strchr( buf, '\0' );
741 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
742 if ( strcmp( dirp->d_name, "." ) == 0 ||
743 strcmp( dirp->d_name, ".." ) == 0 ) {
747 strcat( buf, dirp->d_name );
748 if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
749 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
750 uid, gid, fullpathname(buf), strerror(errno) );
751 /* return ( -1 ); Sometimes this is okay */
757 * We cheat: we know that chown doesn't do anything.
759 if ( stat( ".AppleDouble", &st ) < 0 ) {
760 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
763 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
765 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
766 uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
767 /* return ( -1 ); Sometimes this is okay */
770 setdirowner_noadouble:
771 if ( stat( ".", &st ) < 0 ) {
774 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
776 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
777 uid, gid, fullpathname("."), strerror(errno) );
784 /* recursive chown()ing of a directory */
785 static int recursive_chown(const char *path, uid_t uid, gid_t gid) {
788 struct dirent *entry;
791 char newpath[PATH_MAX+1];
792 newpath[PATH_MAX] = '\0';
794 if (chown(path, uid, gid) < 0) {
795 LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s", path, uid, strerror(errno));
799 if (stat(path, &sbuf) < 0) {
800 LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s", path, uid, strerror(errno));
804 if (S_ISDIR(sbuf.st_mode)) {
805 odir = opendir(path);
807 LOG(log_error, logtype_afpd, "cannot opendir() [%s] (uid = %d): %s", path, uid, strerror(errno));
808 goto recursive_chown_end;
810 while (NULL != (entry=readdir(odir)) ) {
811 name = entry->d_name;
812 if (name[0] == '.' && name[1] == '\0')
814 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
816 sprintf(newpath, "%s/%s", path, name);
817 if (recursive_chown(newpath, uid, gid) < 0)
830 /* This is equivalent of unix rename(). */
831 int unix_rename(const char *oldpath, const char *newpath)
834 char pd_name[PATH_MAX+1];
840 if (rename(oldpath, newpath) < 0)
843 for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
844 pd_name[i] = newpath[i];
847 while (i > 0 && pd_name[i] != '/') i--;
848 if (pd_name[i] == '/') i++;
850 pd_name[i++] = '.'; pd_name[i++] = '\0';
852 if (stat(pd_name, &pd_stat) < 0) {
853 LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s",
854 pd_name, geteuid(), strerror(errno));
858 /* So we have SGID bit set... */
859 if ((S_ISGID & pd_stat.st_mode) != 0) {
862 LOG(log_error, logtype_afpd, "seteuid() failed: %s", strerror(errno));
863 if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
864 LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s",
865 pd_name, geteuid(), strerror(errno));