2 * $Id: unix.c,v 1.31 2002-03-05 01:41:36 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>
19 #include <atalk/logger.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 LOG(log_error, logtype_default, "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 LOG(log_error, logtype_default, "stickydirmode: unable to seteuid root: %s", strerror(errno));
237 if ( retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) ) < 0) {
238 LOG(log_error, logtype_default, "stickydirmode: chmod \"%s\": %s", name, strerror(errno) );
239 return(AFPERR_ACCESS);
242 LOG(log_info, logtype_default, "stickydirmode: (debug) chmod \"%s\": %s", name, strerror(retval) );
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 LOG(log_error, logtype_default, "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 LOG(log_error, logtype_default, "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 LOG(log_error, logtype_default, "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 LOG(log_error, logtype_default, "setdeskmode: chmod %s: %s",
315 modbuf, strerror(errno) );
317 } else if ( chmod( modbuf, mode & ~default_options.umask ) < 0 && errno != EPERM ) {
318 LOG(log_error, logtype_default, "setdeskmode: chmod %s: %s",
319 modbuf, strerror(errno) );
324 /* XXX: need to preserve special modes */
325 if ( chmod( deskp->d_name, (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
326 LOG(log_error, logtype_default, "setdeskmode: chmod %s: %s",
327 deskp->d_name, strerror(errno) );
331 if ( chdir( wd ) < 0 ) {
332 LOG(log_error, logtype_default, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
335 /* XXX: need to preserve special modes */
336 if ( chmod( ".AppleDesktop", (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
337 LOG(log_error, logtype_default, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
342 int setdirmode( mode, noadouble, dropbox )
347 char buf[ MAXPATHLEN + 1];
353 if (( dir = opendir( "." )) == NULL ) {
354 LOG(log_error, logtype_default, "setdirmode: opendir .: %s", strerror(errno) );
358 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
359 if ( *dirp->d_name == '.' ) {
362 if ( stat( dirp->d_name, &st ) < 0 ) {
363 LOG(log_error, logtype_default, "setdirmode: stat %s: %s",
364 dirp->d_name, strerror(errno) );
368 if (S_ISREG(st.st_mode)) {
369 /* XXX: need to preserve special modes */
370 if (S_ISDIR(st.st_mode)) {
371 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
373 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
378 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
380 goto setdirmode_noadouble;
381 LOG(log_error, logtype_default, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
384 strcpy( buf, ".AppleDouble" );
386 m = strchr( buf, '\0' );
387 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
388 if ( strcmp( dirp->d_name, "." ) == 0 ||
389 strcmp( dirp->d_name, ".." ) == 0 ) {
393 strcat( buf, dirp->d_name );
395 if ( stat( buf, &st ) < 0 ) {
396 LOG(log_error, logtype_default, "setdirmode: stat %s: %s", buf, strerror(errno) );
400 if (S_ISDIR(st.st_mode)) {
401 stickydirmode( buf, DIRBITS | mode, dropbox );
403 stickydirmode( buf, mode, dropbox );
407 /* XXX: use special bits to tag directory permissions */
409 /* XXX: need to preserve special modes */
410 if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
413 setdirmode_noadouble:
414 /* XXX: need to preserve special modes */
415 if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
420 int setdeskowner( uid, gid )
424 char wd[ MAXPATHLEN + 1];
425 char modbuf[12 + 1], *m;
426 struct dirent *deskp, *subp;
429 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
432 if ( chdir( ".AppleDesktop" ) < 0 ) {
435 if (( desk = opendir( "." )) == NULL ) {
436 if ( chdir( wd ) < 0 ) {
437 LOG(log_error, logtype_default, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
441 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
442 if ( strcmp( deskp->d_name, "." ) == 0 ||
443 strcmp( deskp->d_name, ".." ) == 0 ||
444 strlen( deskp->d_name ) > 2 ) {
447 strcpy( modbuf, deskp->d_name );
448 strcat( modbuf, "/" );
449 m = strchr( modbuf, '\0' );
450 if (( sub = opendir( deskp->d_name )) == NULL ) {
453 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
454 if ( strcmp( subp->d_name, "." ) == 0 ||
455 strcmp( subp->d_name, ".." ) == 0 ) {
459 strcat( modbuf, subp->d_name );
460 /* XXX: add special any uid, ignore group bits */
461 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
462 LOG(log_error, logtype_default, "setdeskown: chown %s: %s",
463 modbuf, strerror(errno) );
467 /* XXX: add special any uid, ignore group bits */
468 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
469 LOG(log_error, logtype_default, "setdeskowner: chown %s: %s",
470 deskp->d_name, strerror(errno) );
474 if ( chdir( wd ) < 0 ) {
475 LOG(log_error, logtype_default, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
478 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
479 LOG(log_error, logtype_default, "setdeskowner: chown .AppleDesktop: %s",
486 /* uid/gid == 0 need to be handled as special cases. they really mean
487 * that user/group should inherit from other, but that doesn't fit
488 * into the unix permission scheme. we can get around this by
489 * co-opting some bits. */
490 int setdirowner( uid, gid, noadouble )
495 char buf[ MAXPATHLEN + 1];
501 if (( dir = opendir( "." )) == NULL ) {
504 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
505 if ( *dirp->d_name == '.' ) {
508 if ( stat( dirp->d_name, &st ) < 0 ) {
509 LOG(log_error, logtype_default, "setdirowner: stat %s: %s",
510 dirp->d_name, strerror(errno) );
513 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
514 if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
515 LOG(log_debug, logtype_default, "setdirowner: chown %s: %s",
516 dirp->d_name, strerror(errno) );
517 /* return ( -1 ); Sometimes this is okay */
522 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
524 goto setdirowner_noadouble;
527 strcpy( buf, ".AppleDouble" );
529 m = strchr( buf, '\0' );
530 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
531 if ( strcmp( dirp->d_name, "." ) == 0 ||
532 strcmp( dirp->d_name, ".." ) == 0 ) {
536 strcat( buf, dirp->d_name );
537 if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
538 LOG(log_debug, logtype_default, "setdirowner: chown %d/%d %s: %s",
539 uid, gid, buf, strerror(errno) );
540 /* return ( -1 ); Sometimes this is okay */
546 * We cheat: we know that chown doesn't do anything.
548 if ( stat( ".AppleDouble", &st ) < 0 ) {
549 LOG(log_error, logtype_default, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
552 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
554 LOG(log_debug, logtype_default, "setdirowner: chown %d/%d .AppleDouble: %s",
555 uid, gid, strerror(errno) );
556 /* return ( -1 ); Sometimes this is okay */
559 setdirowner_noadouble:
560 if ( stat( ".", &st ) < 0 ) {
563 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
565 LOG(log_debug, logtype_default, "setdirowner: chown %d/%d .: %s",
566 uid, gid, strerror(errno) );