]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/unix.c
added dropbox as a volume option when DROPKLUDGE is compiled in
[netatalk.git] / etc / afpd / unix.c
index 2874abd6ed81f23ecb8fbfd91125c8393dd15037..d8378f638d105b9485cb45db7094ec317315b340 100644 (file)
@@ -129,6 +129,40 @@ void utommode( stat, ma )
 }
 
 
+/*
+ * Calculate the mode for a directory using Posix access() calls to
+ * estimate permission, a la mdw.
+ */
+accessmode( path, ma, dir )
+    char               *path;
+    struct maccess     *ma;
+    struct dir         *dir;
+{
+    if ( access( path, R_OK|W_OK|X_OK ) == 0 ) {
+       ma->ma_user = AR_UREAD|AR_UWRITE|AR_USEARCH|AR_UOWN;
+       ma->ma_owner = AR_UREAD|AR_UWRITE|AR_USEARCH;
+    } else if ( access( path, R_OK|X_OK ) == 0 ) {
+       ma->ma_user = AR_UREAD|AR_USEARCH;
+       ma->ma_owner = AR_UREAD|AR_USEARCH;
+    } else {
+       ma->ma_user = ma->ma_owner = 0;
+       if ( access( path, R_OK ) == 0 ) {
+           ma->ma_user |= AR_UREAD;
+           ma->ma_owner |= AR_UREAD;
+       }
+       if ( access( path, X_OK ) == 0 ) {
+           ma->ma_user |= AR_USEARCH;
+           ma->ma_owner |= AR_USEARCH;
+       }
+       if ( access( path, W_OK ) == 0 ) {
+           ma->ma_user |= AR_UWRITE|AR_UOWN;
+           ma->ma_owner |= AR_UWRITE;
+       }
+    }
+
+    return;
+}
+
 int gmem( gid )
     const gid_t        gid;
 {
@@ -174,39 +208,43 @@ mode_t mtoumode( ma )
     return( mode );
 }
 
-inline int stickydirmode(name, mode)
-char * name;
-const mode_t mode;
+inline int stickydirmode(name, mode, dropbox)
+    char * name;
+    const mode_t mode;
+    const int dropbox;
 {
   int uid, retval;
 
-/* Turn on the sticky bit if this is a drop box */
+/* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
    retval=0;
 #ifdef DROPKLUDGE
-   if (mode & S_IWOTH) 
+   if (dropbox) {
+    if (mode & S_IWOTH) { 
       if (mode & S_IROTH); 
-      else /* if S_IWOTH and not S_IROTH */
-      {
+      else { /* if S_IWOTH and not S_IROTH */
         uid=geteuid();
-        if ( seteuid(0) < 0)
+        if ( seteuid(0) < 0) {
           syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %m");
-        if ( retval=chmod( name, (DIRBITS | mode | S_ISVTX)) < 0)
-        {
+        }
+        if ( retval=chmod( name, ( DIRBITS | mode | S_ISVTX) ) < 0) {
            syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %m", name );
            return(AFPERR_ACCESS);
-        }
-        else
-        {
+        } else {
 #ifdef DEBUG
            syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\": %m", name );
 #endif
            seteuid(uid);
-        }
-      }
-   else 
+        } /* end getting retval */
+      } /* end if not & S_IROTH */
+   } else { /* end if S_IWOTH and not S_IROTH */
 #endif DROPKLUDGE
-       if ( retval=chmod( name, DIRBITS | mode ) < 0 ) 
+       if ( retval=chmod( name, DIRBITS | mode ) < 0 )  {
           syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %m", name );
+       }
+#ifdef DROPKLUDGE
+     } /* end if not mode */
+   } /* end checking for "dropbox" */
+#endif /* DROPKLUDGE */
    return retval;
 }
 
@@ -282,9 +320,10 @@ int setdeskmode( mode )
     return( 0 );
 }
 
-int setdirmode( mode, noadouble )
+int setdirmode( mode, noadouble, dropbox )
     const mode_t mode;
     const int noadouble;
+    const int dropbox;
 {
     char               buf[ MAXPATHLEN + 1];
     struct stat                st;
@@ -309,9 +348,9 @@ int setdirmode( mode, noadouble )
        if (S_ISREG(st.st_mode)) {
            /* XXX: need to preserve special modes */
            if (S_ISDIR(st.st_mode)) {
-              if (stickydirmode(dirp->d_name, DIRBITS | mode) < 0)
+              if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
                return (-1);
-           } else if (stickydirmode(dirp->d_name, mode) < 0)
+           } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
                return (-1);
        }
     }
@@ -339,21 +378,21 @@ int setdirmode( mode, noadouble )
        }
 
        if (S_ISDIR(st.st_mode)) {
-           stickydirmode( buf, DIRBITS | mode );
+           stickydirmode( buf, DIRBITS | mode, dropbox );
        } else 
-           stickydirmode( buf, mode );
+           stickydirmode( buf, mode, dropbox );
     } /* end for */
     closedir( dir );
 
     /* XXX: use special bits to tag directory permissions */
       
     /* XXX: need to preserve special modes */
-    if ( stickydirmode(".AppleDouble", DIRBITS | mode) < 0 )
+    if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
        return( -1 );
 
 setdirmode_noadouble:
     /* XXX: need to preserve special modes */
-    if ( stickydirmode(".", DIRBITS | mode) < 0 )
+    if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
        return( -1 );
     return( 0 );
 }
@@ -450,6 +489,7 @@ int setdirowner( uid, gid, noadouble )
        if (( st.st_mode & S_IFMT ) == S_IFREG ) {
            if ( chown( dirp->d_name, uid, gid ) < 0 ) {
                syslog( LOG_ERR, "setdirowner: chown %s: %m", dirp->d_name );
+               /* return ( -1 ); Sometimes this is okay */
            }
        }
     }
@@ -472,6 +512,7 @@ int setdirowner( uid, gid, noadouble )
        if ( chown( buf, uid, gid ) < 0 ) {
            syslog( LOG_ERR, "setdirowner: chown %d/%d %s: %m",
                    uid, gid, buf );
+           /* return ( -1 ); Sometimes this is okay */
        }
     }
     closedir( dir );
@@ -486,6 +527,7 @@ int setdirowner( uid, gid, noadouble )
     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 ) {
        syslog( LOG_ERR, "setdirowner: chown %d/%d .AppleDouble: %m",
                uid, gid);
+       /* return ( -1 ); Sometimes this is okay */
     }
 
 setdirowner_noadouble: