2 * $Id: unix.c,v 1.43.2.1.2.7 2004-05-10 18:40:32 didg 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>
36 #include <atalk/logger.h>
37 #include <atalk/adouble.h>
38 #include <atalk/afp.h>
39 #include <atalk/util.h>
42 #include "directory.h"
48 * Get the free space on a partition.
50 int ustatfs_getvolspace( vol, bfree, btotal, bsize )
51 const struct vol *vol;
52 VolSpace *bfree, *btotal;
55 VolSpace maxVolSpace = (~(VolSpace)0);
64 if ( statfs( vol->v_path, &sfs ) < 0 ) {
65 LOG(log_error, logtype_afpd, "ustatfs_getvolspace unable to stat %s", vol->v_path);
66 return( AFPERR_PARAM );
70 *bfree = (VolSpace) sfs.fd_req.bfreen;
73 *bfree = (VolSpace) sfs.f_bavail;
74 *bsize = sfs.f_frsize;
77 if ( *bfree > maxVolSpace / *bsize ) {
85 ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
88 ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
91 /* see similar block above comments */
92 if ( *btotal > maxVolSpace / *bsize ) {
93 *btotal = maxVolSpace;
101 static __inline__ int utombits( bits )
108 mbits |= ( bits & ( S_IREAD >> 6 )) ? AR_UREAD : 0;
109 mbits |= ( bits & ( S_IWRITE >> 6 )) ? AR_UWRITE : 0;
110 /* Do we really need this? */
111 mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0;
116 /* --------------------------------
119 void utommode( stat, ma )
125 mode = stat->st_mode;
126 ma->ma_world = utombits( mode );
129 ma->ma_group = utombits( mode );
132 ma->ma_owner = utombits( mode );
134 /* ma_user is a union of all permissions but we must follow
137 if ( (uuid == stat->st_uid) || (uuid == 0)) {
138 ma->ma_user = ma->ma_owner | AR_UOWN;
140 else if ( gmem( stat->st_gid )) {
141 ma->ma_user = ma->ma_group;
144 ma->ma_user = ma->ma_world;
148 * There are certain things the mac won't try if you don't have
149 * the "owner" bit set, even tho you can do these things on unix wiht
150 * only write permission. What were the things?
152 * FIXME and so what ?
155 if ( ma->ma_user & AR_UWRITE ) {
156 ma->ma_user |= AR_UOWN;
166 * Calculate the mode for a directory using a stat() call to
167 * estimate permission.
169 * Note: the previous method, using access(), does not work correctly
171 * FIXME what about ACL?
173 void accessmode( path, ma, dir, st )
182 ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
184 if (stat(path, &sb) != 0)
197 for ( i = 0; i < ngroups; i++ ) {
198 if ( groups[ i ] == gid ) {
205 static __inline__ mode_t mtoubits( bits )
212 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
213 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
214 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
215 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
220 /* ----------------------------------
221 from the finder's share windows (menu--> File--> sharing...)
222 and from AFP 3.0 spec page 63
223 the mac mode should be save somewhere
225 mode_t mtoumode( ma )
231 mode |= mtoubits( ma->ma_owner |ma->ma_world);
234 mode |= mtoubits( ma->ma_group |ma->ma_world);
237 mode |= mtoubits( ma->ma_world );
242 /* ----------------------------- */
243 char *fullpathname(const char *name)
245 static char wd[ MAXPATHLEN + 1];
247 if ( getcwd( wd , MAXPATHLEN) ) {
248 strlcat(wd, "/", MAXPATHLEN);
249 strlcat(wd, name, MAXPATHLEN);
252 strlcpy(wd, name, MAXPATHLEN);
257 /* -----------------------------
258 a dropbox is a folder where w is set but not r eg:
259 rwx-wx-wx or rwx-wx--
260 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
262 static int stickydirmode(name, mode, dropbox)
270 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
274 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
275 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
278 if ( seteuid(0) < 0) {
279 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
281 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) )) < 0) {
282 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
285 LOG(log_info, logtype_afpd, "stickydirmode: (debug) chmod \"%s\": %s", fullpathname(name), strerror(retval) );
292 #endif /* DROPKLUDGE */
295 * Ignore EPERM errors: We may be dealing with a directory that is
296 * group writable, in which case chmod will fail.
298 if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM) {
299 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
306 /* ------------------------- */
307 int dir_rx_set(mode_t mode)
309 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
312 #define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
314 int setdeskmode( mode )
317 char wd[ MAXPATHLEN + 1];
319 char modbuf[ 12 + 1], *m;
320 struct dirent *deskp, *subp;
323 if (!dir_rx_set(mode)) {
324 /* want to remove read and search access to owner it will screw the volume */
327 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
330 if ( chdir( ".AppleDesktop" ) < 0 ) {
333 if (( desk = opendir( "." )) == NULL ) {
334 if ( chdir( wd ) < 0 ) {
335 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
339 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
340 if ( strcmp( deskp->d_name, "." ) == 0 ||
341 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
344 strcpy( modbuf, deskp->d_name );
345 strcat( modbuf, "/" );
346 m = strchr( modbuf, '\0' );
347 if (( sub = opendir( deskp->d_name )) == NULL ) {
350 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
351 if ( strcmp( subp->d_name, "." ) == 0 ||
352 strcmp( subp->d_name, ".." ) == 0 ) {
356 strcat( modbuf, subp->d_name );
357 /* XXX: need to preserve special modes */
358 if (stat(modbuf, &st) < 0) {
359 LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
363 if (S_ISDIR(st.st_mode)) {
364 if ( chmod( modbuf, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
365 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
367 } else if ( chmod( modbuf, mode & ~(default_options.umask | EXEC_MODE) ) < 0 && errno != EPERM ) {
368 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
373 /* XXX: need to preserve special modes */
374 if ( chmod( deskp->d_name, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
375 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
379 if ( chdir( wd ) < 0 ) {
380 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
383 /* XXX: need to preserve special modes */
384 if ( chmod( ".AppleDesktop", (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
385 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
390 /* --------------------- */
391 int setfilunixmode (vol, path, mode)
392 const struct vol *vol;
396 if (!path->st_valid) {
400 if (path->st_errno) {
404 if (setfilmode( path->u_name, mode, &path->st) < 0)
406 /* we need to set write perm if read set for resource fork */
407 return setfilmode(vol->ad_path( path->u_name, ADFLAGS_HF ), ad_hf_mode(mode), &path->st);
410 /* --------------------- */
411 int setfilmode(name, mode, st)
417 mode_t mask = S_IRUSR |S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH;
420 if (stat(name, &sb) != 0)
424 mode &= mask; /* keep only rw-rw-rw in mode */
425 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
426 if ( chmod( name, mode & ~default_options.umask ) < 0 && errno != EPERM ) {
432 /* --------------------- */
433 int setdirunixmode( vol, name, mode )
434 const struct vol *vol;
438 char *adouble = vol->ad_path( name, ADFLAGS_DIR );
440 int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
442 if (dir_rx_set(mode)) {
443 /* extending right? dir first then .AppleDouble */
444 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
446 if (vol->v_adouble != AD_VERSION2_OSX) {
447 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
452 if (setfilmode(adouble, ad_hf_mode(mode), NULL) < 0 && !vol_noadouble(vol)) {
455 if (!dir_rx_set(mode)) {
456 if (vol->v_adouble != AD_VERSION2_OSX) {
457 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
461 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
467 /* --------------------- */
468 int setdirmode( vol, name, mode )
469 const struct vol *vol;
473 char buf[ MAXPATHLEN + 1];
478 int osx = vol->v_adouble == AD_VERSION2_OSX;
479 int hf_mode = ad_hf_mode(mode);
480 int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
481 char *adouble = vol->ad_path( name, ADFLAGS_DIR );
482 char *adouble_p = ad_dir(adouble);
484 if (dir_rx_set(mode)) {
485 /* extending right? dir first then .AppleDouble */
486 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
489 if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
495 if (( dir = opendir( name )) == NULL ) {
496 LOG(log_error, logtype_afpd, "setdirmode: opendir: %s", fullpathname(name), strerror(errno) );
500 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
502 if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
505 if ( stat( dirp->d_name, &st ) < 0 ) {
506 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s",dirp->d_name, strerror(errno) );
510 if (!S_ISDIR(st.st_mode)) {
511 int setmode = (osx && *dirp->d_name == '.')?hf_mode:mode;
513 if (setfilmode(dirp->d_name, setmode, &st) < 0) {
514 LOG(log_error, logtype_afpd, "setdirmode: chmod %s: %s",dirp->d_name, strerror(errno) );
519 /* Don't change subdir perm */
520 else if (S_ISDIR(st.st_mode)) {
521 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
523 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
531 goto setdirmode_noadouble;
534 /* change perm of .AppleDouble's files
536 if (( dir = opendir( adouble_p )) == NULL ) {
537 if (vol_noadouble(vol))
538 goto setdirmode_noadouble;
539 LOG(log_error, logtype_afpd, "setdirmode: opendir %s: %s", fullpathname(".AppleDouble"),strerror(errno) );
542 strcpy( buf, adouble_p);
544 m = strchr( buf, '\0' );
545 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
546 if ( strcmp( dirp->d_name, "." ) == 0 ||
547 strcmp( dirp->d_name, ".." ) == 0 ) {
551 strcat( buf, dirp->d_name );
553 if ( stat( buf, &st ) < 0 ) {
554 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
557 if (!S_ISDIR(st.st_mode)) {
558 if (setfilmode(buf, hf_mode , &st) < 0) {
559 /* FIXME what do we do then? */
565 if (!dir_rx_set(mode)) {
566 /* XXX: need to preserve special modes */
567 if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 ) {
572 setdirmode_noadouble:
573 if (!dir_rx_set(mode)) {
574 if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
580 int setdeskowner( uid, gid )
584 char wd[ MAXPATHLEN + 1];
585 char modbuf[12 + 1], *m;
586 struct dirent *deskp, *subp;
589 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
592 if ( chdir( ".AppleDesktop" ) < 0 ) {
595 if (( desk = opendir( "." )) == NULL ) {
596 if ( chdir( wd ) < 0 ) {
597 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
601 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
602 if ( strcmp( deskp->d_name, "." ) == 0 ||
603 strcmp( deskp->d_name, ".." ) == 0 ||
604 strlen( deskp->d_name ) > 2 ) {
607 strcpy( modbuf, deskp->d_name );
608 strcat( modbuf, "/" );
609 m = strchr( modbuf, '\0' );
610 if (( sub = opendir( deskp->d_name )) == NULL ) {
613 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
614 if ( strcmp( subp->d_name, "." ) == 0 ||
615 strcmp( subp->d_name, ".." ) == 0 ) {
619 strcat( modbuf, subp->d_name );
620 /* XXX: add special any uid, ignore group bits */
621 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
622 LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
626 /* XXX: add special any uid, ignore group bits */
627 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
628 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s",
629 deskp->d_name, strerror(errno) );
633 if ( chdir( wd ) < 0 ) {
634 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
637 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
638 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
643 /* ----------------------------- */
644 int setfilowner(vol, uid, gid, path)
645 const struct vol *vol;
653 if (!path->st_valid) {
657 if (path->st_errno) {
661 if ( chown( path->u_name, uid, gid ) < 0 && errno != EPERM ) {
662 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
663 uid, gid, path->u_name, strerror(errno) );
667 ad_p = vol->ad_path( path->u_name, ADFLAGS_HF );
669 if ( stat( ad_p, &st ) < 0 ) {
673 if ( chown( ad_p, uid, gid ) < 0 &&
675 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
676 uid, gid, ad_p, strerror(errno) );
683 /* ---------------------------------
684 * uid/gid == 0 need to be handled as special cases. they really mean
685 * that user/group should inherit from other, but that doesn't fit
686 * into the unix permission scheme. we can get around this by
687 * co-opting some bits. */
688 int setdirowner(vol, name, uid, gid )
689 const struct vol *vol;
694 char buf[ MAXPATHLEN + 1];
699 int osx = vol->v_adouble == AD_VERSION2_OSX;
700 int noadouble = vol_noadouble(vol);
704 if (( dir = opendir( name )) == NULL ) {
707 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
708 if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
711 if ( stat( dirp->d_name, &st ) < 0 ) {
712 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s",
713 fullpathname(dirp->d_name), strerror(errno) );
716 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
717 if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
718 LOG(log_debug, logtype_afpd, "setdirowner: chown %s: %s",
719 fullpathname(dirp->d_name), strerror(errno) );
720 /* return ( -1 ); Sometimes this is okay */
727 goto setdirowner_noadouble;
730 adouble = vol->ad_path( name, ADFLAGS_DIR );
731 adouble_p = ad_dir(adouble);
732 if (( dir = opendir( adouble_p )) == NULL ) {
734 goto setdirowner_noadouble;
737 strcpy( buf, adouble_p );
739 m = strchr( buf, '\0' );
740 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
741 if ( strcmp( dirp->d_name, "." ) == 0 ||
742 strcmp( dirp->d_name, ".." ) == 0 ) {
746 strcat( buf, dirp->d_name );
747 if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
748 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
749 uid, gid, fullpathname(buf), strerror(errno) );
750 /* return ( -1 ); Sometimes this is okay */
756 * We cheat: we know that chown doesn't do anything.
758 if ( stat( ".AppleDouble", &st ) < 0 ) {
759 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
762 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
764 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
765 uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
766 /* return ( -1 ); Sometimes this is okay */
769 setdirowner_noadouble:
770 if ( stat( ".", &st ) < 0 ) {
773 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
775 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
776 uid, gid, fullpathname("."), strerror(errno) );
783 /* recursive chown()ing of a directory */
784 static int recursive_chown(const char *path, uid_t uid, gid_t gid) {
787 struct dirent *entry;
790 char newpath[PATH_MAX+1];
791 newpath[PATH_MAX] = '\0';
793 if (chown(path, uid, gid) < 0) {
794 LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
798 if (stat(path, &sbuf) < 0) {
799 LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
803 if (S_ISDIR(sbuf.st_mode)) {
804 odir = opendir(path);
806 LOG(log_error, logtype_afpd, "cannot opendir() [%s] (uid = %d): %s\n", path, uid, strerror(errno));
807 goto recursive_chown_end;
809 while (NULL != (entry=readdir(odir)) ) {
810 name = entry->d_name;
811 if (name[0] == '.' && name[1] == '\0')
813 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
815 sprintf(newpath, "%s/%s", path, name);
816 if (recursive_chown(newpath, uid, gid) < 0)
829 /* This is equivalent of unix rename(). */
830 int unix_rename(const char *oldpath, const char *newpath)
833 char pd_name[PATH_MAX+1];
839 if (rename(oldpath, newpath) < 0)
842 for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
843 pd_name[i] = newpath[i];
846 while (i > 0 && pd_name[i] != '/') i--;
847 if (pd_name[i] == '/') i++;
849 pd_name[i++] = '.'; pd_name[i++] = '\0';
851 if (stat(pd_name, &pd_stat) < 0) {
852 LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s\n",
853 pd_name, geteuid(), strerror(errno));
857 /* So we have SGID bit set... */
858 if ((S_ISGID & pd_stat.st_mode) != 0) {
861 LOG(log_error, logtype_afpd, "seteuid() failed: %s\n", strerror(errno));
862 if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
863 LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s\n",
864 pd_name, geteuid(), strerror(errno));