2 * $Id: unix.c,v 1.24.2.6 2002-03-12 15:25:07 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
17 #include <sys/param.h>
20 #include <netatalk/endian.h>
23 #include <atalk/afp.h>
28 #else /* STDC_HEADERS */
32 #endif /* HAVE_STRCHR */
33 char *strchr (), *strrchr ();
35 #define memcpy(d,s,n) bcopy ((s), (d), (n))
36 #define memmove(d,s,n) bcopy ((s), (d), (n))
37 #endif /* ! HAVE_MEMCPY */
38 #endif /* STDC_HEADERS */
42 #endif /* HAVE_FCNTL_H */
44 #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 syslog(LOG_ERR, "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 | AR_USEARCH) : 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 void utommode( stat, ma )
123 mode = stat->st_mode;
125 ma->ma_world = utombits( mode );
128 ma->ma_group = utombits( mode );
131 ma->ma_owner = utombits( mode );
133 if ( (uuid == stat->st_uid) || (uuid == 0)) {
134 ma->ma_user = ma->ma_owner | AR_UOWN;
135 } else if ( gmem( stat->st_gid )) {
136 ma->ma_user = ma->ma_group;
138 ma->ma_user = ma->ma_world;
142 * There are certain things the mac won't try if you don't have
143 * the "owner" bit set, even tho you can do these things on unix wiht
144 * only write permission. What were the things?
146 if ( ma->ma_user & AR_UWRITE ) {
147 ma->ma_user |= AR_UOWN;
153 * Calculate the mode for a directory using a stat() call to
154 * estimate permission.
156 * Note: the previous method, using access(), does not work correctly
159 void accessmode( path, ma, dir )
165 ma->ma_user = ma->ma_owner = 0;
166 if ( stat( path, &sb ) == 0 )
176 for ( i = 0; i < ngroups; i++ ) {
177 if ( groups[ i ] == gid ) {
184 static __inline__ mode_t mtoubits( bits )
191 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
192 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
193 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
194 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
199 mode_t mtoumode( ma )
205 mode |= mtoubits( ma->ma_owner );
208 mode |= mtoubits( ma->ma_group );
211 mode |= mtoubits( ma->ma_world );
216 inline int stickydirmode(name, mode, dropbox)
224 #endif /* DROPKLUDGE */
226 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
230 if (mode & S_IWOTH) {
232 else { /* if S_IWOTH and not S_IROTH */
234 if ( seteuid(0) < 0) {
235 syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %s", strerror(errno));
237 if ( retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) ) < 0) {
238 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s", name, strerror(errno) );
239 return(AFPERR_ACCESS);
242 syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\"", name );
245 } /* end getting retval */
246 } /* end if not & S_IROTH */
247 } else { /* end if S_IWOTH and not S_IROTH */
248 #endif /* DROPKLUDGE */
251 * Ignore EPERM errors: We may be dealing with a directory that is
252 * group writable, in which case chmod will fail.
254 if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM) {
255 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
256 name, strerror(errno) );
260 } /* end if not mode */
261 } /* end checking for "dropbox" */
262 #endif /* DROPKLUDGE */
266 int setdeskmode( mode )
269 char wd[ MAXPATHLEN + 1];
271 char modbuf[ 12 + 1], *m;
272 struct dirent *deskp, *subp;
275 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
278 if ( chdir( ".AppleDesktop" ) < 0 ) {
281 if (( desk = opendir( "." )) == NULL ) {
282 if ( chdir( wd ) < 0 ) {
283 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
287 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
288 if ( strcmp( deskp->d_name, "." ) == 0 ||
289 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
292 strcpy( modbuf, deskp->d_name );
293 strcat( modbuf, "/" );
294 m = strchr( modbuf, '\0' );
295 if (( sub = opendir( deskp->d_name )) == NULL ) {
298 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
299 if ( strcmp( subp->d_name, "." ) == 0 ||
300 strcmp( subp->d_name, ".." ) == 0 ) {
304 strcat( modbuf, subp->d_name );
305 /* XXX: need to preserve special modes */
306 if (stat(modbuf, &st) < 0) {
307 syslog( LOG_ERR, "setdeskmode: stat %s: %s",
308 modbuf, strerror(errno) );
312 if (S_ISDIR(st.st_mode)) {
313 if ( chmod( modbuf, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
314 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
315 modbuf, strerror(errno) );
317 } else if ( chmod( modbuf, mode & ~default_options.umask ) < 0 && errno != EPERM ) {
318 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
319 modbuf, strerror(errno) );
324 if ( chmod( deskp->d_name, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
325 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
326 deskp->d_name, strerror(errno) );
330 if ( chdir( wd ) < 0 ) {
331 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
334 if ( chmod( ".AppleDesktop", (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
335 syslog( LOG_ERR, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
340 int setdirmode( mode, noadouble, dropbox )
345 char buf[ MAXPATHLEN + 1];
351 if (( dir = opendir( "." )) == NULL ) {
352 syslog( LOG_ERR, "setdirmode: opendir .: %s", strerror(errno) );
356 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
357 if ( *dirp->d_name == '.' ) {
360 if ( stat( dirp->d_name, &st ) < 0 ) {
361 syslog( LOG_ERR, "setdirmode: stat %s: %s",
362 dirp->d_name, strerror(errno) );
366 if (S_ISREG(st.st_mode)) {
367 /* XXX: need to preserve special modes */
368 if (S_ISDIR(st.st_mode)) {
369 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
371 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
376 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
378 goto setdirmode_noadouble;
379 syslog( LOG_ERR, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
382 strcpy( buf, ".AppleDouble" );
384 m = strchr( buf, '\0' );
385 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
386 if ( strcmp( dirp->d_name, "." ) == 0 ||
387 strcmp( dirp->d_name, ".." ) == 0 ) {
391 strcat( buf, dirp->d_name );
393 if ( stat( buf, &st ) < 0 ) {
394 syslog( LOG_ERR, "setdirmode: stat %s: %s", buf, strerror(errno) );
398 if (S_ISDIR(st.st_mode)) {
399 stickydirmode( buf, DIRBITS | mode, dropbox );
401 stickydirmode( buf, mode, dropbox );
405 /* XXX: use special bits to tag directory permissions */
407 /* XXX: need to preserve special modes */
408 if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
411 setdirmode_noadouble:
412 /* XXX: need to preserve special modes */
413 if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
418 int setdeskowner( uid, gid )
422 char wd[ MAXPATHLEN + 1];
423 char modbuf[12 + 1], *m;
424 struct dirent *deskp, *subp;
427 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
430 if ( chdir( ".AppleDesktop" ) < 0 ) {
433 if (( desk = opendir( "." )) == NULL ) {
434 if ( chdir( wd ) < 0 ) {
435 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
439 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
440 if ( strcmp( deskp->d_name, "." ) == 0 ||
441 strcmp( deskp->d_name, ".." ) == 0 ||
442 strlen( deskp->d_name ) > 2 ) {
445 strcpy( modbuf, deskp->d_name );
446 strcat( modbuf, "/" );
447 m = strchr( modbuf, '\0' );
448 if (( sub = opendir( deskp->d_name )) == NULL ) {
451 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
452 if ( strcmp( subp->d_name, "." ) == 0 ||
453 strcmp( subp->d_name, ".." ) == 0 ) {
457 strcat( modbuf, subp->d_name );
458 /* XXX: add special any uid, ignore group bits */
459 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
460 syslog( LOG_ERR, "setdeskown: chown %s: %s",
461 modbuf, strerror(errno) );
465 /* XXX: add special any uid, ignore group bits */
466 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
467 syslog( LOG_ERR, "setdeskowner: chown %s: %s",
468 deskp->d_name, strerror(errno) );
472 if ( chdir( wd ) < 0 ) {
473 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
476 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
477 syslog( LOG_ERR, "setdeskowner: chown .AppleDesktop: %s",
484 /* uid/gid == 0 need to be handled as special cases. they really mean
485 * that user/group should inherit from other, but that doesn't fit
486 * into the unix permission scheme. we can get around this by
487 * co-opting some bits. */
488 int setdirowner( uid, gid, noadouble )
493 char buf[ MAXPATHLEN + 1];
499 if (( dir = opendir( "." )) == NULL ) {
502 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
503 if ( *dirp->d_name == '.' ) {
506 if ( stat( dirp->d_name, &st ) < 0 ) {
507 syslog( LOG_ERR, "setdirowner: stat %s: %s",
508 dirp->d_name, strerror(errno) );
511 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
512 if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
513 syslog( LOG_DEBUG, "setdirowner: chown %s: %s",
514 dirp->d_name, strerror(errno) );
515 /* return ( -1 ); Sometimes this is okay */
520 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
522 goto setdirowner_noadouble;
525 strcpy( buf, ".AppleDouble" );
527 m = strchr( buf, '\0' );
528 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
529 if ( strcmp( dirp->d_name, "." ) == 0 ||
530 strcmp( dirp->d_name, ".." ) == 0 ) {
534 strcat( buf, dirp->d_name );
535 if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
536 syslog( LOG_DEBUG, "setdirowner: chown %d/%d %s: %s",
537 uid, gid, buf, strerror(errno) );
538 /* return ( -1 ); Sometimes this is okay */
544 * We cheat: we know that chown doesn't do anything.
546 if ( stat( ".AppleDouble", &st ) < 0 ) {
547 syslog( LOG_ERR, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
550 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
552 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .AppleDouble: %s",
553 uid, gid, strerror(errno) );
554 /* return ( -1 ); Sometimes this is okay */
557 setdirowner_noadouble:
558 if ( stat( ".", &st ) < 0 ) {
561 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
563 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .: %s",
564 uid, gid, strerror(errno) );