2 * $Id: unix.c,v 1.22 2001-10-09 04:03:33 jmarcus 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 <sys/syslog.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 Posix access() calls to
154 * estimate permission, a la mdw.
156 void accessmode( path, ma, dir )
161 if ( access( path, R_OK|W_OK|X_OK ) == 0 ) {
162 ma->ma_user = AR_UREAD|AR_UWRITE|AR_USEARCH|AR_UOWN;
163 ma->ma_owner = AR_UREAD|AR_UWRITE|AR_USEARCH;
164 } else if ( access( path, R_OK|X_OK ) == 0 ) {
165 ma->ma_user = AR_UREAD|AR_USEARCH;
166 ma->ma_owner = AR_UREAD|AR_USEARCH;
168 ma->ma_user = ma->ma_owner = 0;
169 if ( access( path, R_OK ) == 0 ) {
170 ma->ma_user |= AR_UREAD;
171 ma->ma_owner |= AR_UREAD;
173 if ( access( path, X_OK ) == 0 ) {
174 ma->ma_user |= AR_USEARCH;
175 ma->ma_owner |= AR_USEARCH;
177 if ( access( path, W_OK ) == 0 ) {
178 ma->ma_user |= AR_UWRITE|AR_UOWN;
179 ma->ma_owner |= AR_UWRITE;
189 for ( i = 0; i < ngroups; i++ ) {
190 if ( groups[ i ] == gid ) {
197 static __inline__ mode_t mtoubits( bits )
204 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
205 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
206 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
207 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
212 mode_t mtoumode( ma )
218 mode |= mtoubits( ma->ma_owner );
221 mode |= mtoubits( ma->ma_group );
224 mode |= mtoubits( ma->ma_world );
229 inline int stickydirmode(name, mode, dropbox)
237 #endif /* DROPKLUDGE */
239 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
243 if (mode & S_IWOTH) {
245 else { /* if S_IWOTH and not S_IROTH */
247 if ( seteuid(0) < 0) {
248 syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %m");
250 if ( retval=chmod( name, ( DIRBITS | mode | S_ISVTX) ) < 0) {
251 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %m", name );
252 return(AFPERR_ACCESS);
255 syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\": %m", name );
258 } /* end getting retval */
259 } /* end if not & S_IROTH */
260 } else { /* end if S_IWOTH and not S_IROTH */
261 #endif /* DROPKLUDGE */
264 * Ignore EPERM errors: We may be dealing with a directory that is
265 * group writable, in which case chmod will fail.
267 if ( chmod( name, DIRBITS | mode ) < 0 && errno != EPERM) {
268 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
269 name, strerror(errno) );
273 } /* end if not mode */
274 } /* end checking for "dropbox" */
275 #endif /* DROPKLUDGE */
279 int setdeskmode( mode )
282 char wd[ MAXPATHLEN + 1];
284 char modbuf[ 12 + 1], *m;
285 struct dirent *deskp, *subp;
288 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
291 if ( chdir( ".AppleDesktop" ) < 0 ) {
294 if (( desk = opendir( "." )) == NULL ) {
295 if ( chdir( wd ) < 0 ) {
296 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
300 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
301 if ( strcmp( deskp->d_name, "." ) == 0 ||
302 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
305 strcpy( modbuf, deskp->d_name );
306 strcat( modbuf, "/" );
307 m = strchr( modbuf, '\0' );
308 if (( sub = opendir( deskp->d_name )) == NULL ) {
311 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
312 if ( strcmp( subp->d_name, "." ) == 0 ||
313 strcmp( subp->d_name, ".." ) == 0 ) {
317 strcat( modbuf, subp->d_name );
318 /* XXX: need to preserve special modes */
319 if (stat(modbuf, &st) < 0) {
320 syslog( LOG_ERR, "setdeskmode: stat %s: %s",
321 modbuf, strerror(errno) );
325 if (S_ISDIR(st.st_mode)) {
326 if ( chmod( modbuf, DIRBITS | mode ) < 0 && errno != EPERM ) {
327 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
328 modbuf, strerror(errno) );
330 } else if ( chmod( modbuf, mode ) < 0 && errno != EPERM ) {
331 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
332 modbuf, strerror(errno) );
337 /* XXX: need to preserve special modes */
338 if ( chmod( deskp->d_name, DIRBITS | mode ) < 0 && errno != EPERM ) {
339 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
340 deskp->d_name, strerror(errno) );
344 if ( chdir( wd ) < 0 ) {
345 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
348 /* XXX: need to preserve special modes */
349 if ( chmod( ".AppleDesktop", DIRBITS | mode ) < 0 && errno != EPERM ) {
350 syslog( LOG_ERR, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
355 int setdirmode( mode, noadouble, dropbox )
360 char buf[ MAXPATHLEN + 1];
366 if (( dir = opendir( "." )) == NULL ) {
367 syslog( LOG_ERR, "setdirmode: opendir .: %s", strerror(errno) );
371 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
372 if ( *dirp->d_name == '.' ) {
375 if ( stat( dirp->d_name, &st ) < 0 ) {
376 syslog( LOG_ERR, "setdirmode: stat %s: %s",
377 dirp->d_name, strerror(errno) );
381 if (S_ISREG(st.st_mode)) {
382 /* XXX: need to preserve special modes */
383 if (S_ISDIR(st.st_mode)) {
384 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
386 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
391 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
393 goto setdirmode_noadouble;
394 syslog( LOG_ERR, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
397 strcpy( buf, ".AppleDouble" );
399 m = strchr( buf, '\0' );
400 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
401 if ( strcmp( dirp->d_name, "." ) == 0 ||
402 strcmp( dirp->d_name, ".." ) == 0 ) {
406 strcat( buf, dirp->d_name );
408 if ( stat( buf, &st ) < 0 ) {
409 syslog( LOG_ERR, "setdirmode: stat %s: %s", buf, strerror(errno) );
413 if (S_ISDIR(st.st_mode)) {
414 stickydirmode( buf, DIRBITS | mode, dropbox );
416 stickydirmode( buf, mode, dropbox );
420 /* XXX: use special bits to tag directory permissions */
422 /* XXX: need to preserve special modes */
423 if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
426 setdirmode_noadouble:
427 /* XXX: need to preserve special modes */
428 if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
433 int setdeskowner( uid, gid )
437 char wd[ MAXPATHLEN + 1];
438 char modbuf[12 + 1], *m;
439 struct dirent *deskp, *subp;
442 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
445 if ( chdir( ".AppleDesktop" ) < 0 ) {
448 if (( desk = opendir( "." )) == NULL ) {
449 if ( chdir( wd ) < 0 ) {
450 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
454 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
455 if ( strcmp( deskp->d_name, "." ) == 0 ||
456 strcmp( deskp->d_name, ".." ) == 0 ||
457 strlen( deskp->d_name ) > 2 ) {
460 strcpy( modbuf, deskp->d_name );
461 strcat( modbuf, "/" );
462 m = strchr( modbuf, '\0' );
463 if (( sub = opendir( deskp->d_name )) == NULL ) {
466 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
467 if ( strcmp( subp->d_name, "." ) == 0 ||
468 strcmp( subp->d_name, ".." ) == 0 ) {
472 strcat( modbuf, subp->d_name );
473 /* XXX: add special any uid, ignore group bits */
474 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
475 syslog( LOG_ERR, "setdeskown: chown %s: %s",
476 modbuf, strerror(errno) );
480 /* XXX: add special any uid, ignore group bits */
481 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
482 syslog( LOG_ERR, "setdeskowner: chown %s: %s",
483 deskp->d_name, strerror(errno) );
487 if ( chdir( wd ) < 0 ) {
488 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
491 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
492 syslog( LOG_ERR, "setdeskowner: chown .AppleDesktop: %s",
499 /* uid/gid == 0 need to be handled as special cases. they really mean
500 * that user/group should inherit from other, but that doesn't fit
501 * into the unix permission scheme. we can get around this by
502 * co-opting some bits. */
503 int setdirowner( uid, gid, noadouble )
508 char buf[ MAXPATHLEN + 1];
514 if (( dir = opendir( "." )) == NULL ) {
517 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
518 if ( *dirp->d_name == '.' ) {
521 if ( stat( dirp->d_name, &st ) < 0 ) {
522 syslog( LOG_ERR, "setdirowner: stat %s: %s",
523 dirp->d_name, strerror(errno) );
526 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
527 if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
528 syslog( LOG_DEBUG, "setdirowner: chown %s: %s",
529 dirp->d_name, strerror(errno) );
530 /* return ( -1 ); Sometimes this is okay */
535 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
537 goto setdirowner_noadouble;
540 strcpy( buf, ".AppleDouble" );
542 m = strchr( buf, '\0' );
543 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
544 if ( strcmp( dirp->d_name, "." ) == 0 ||
545 strcmp( dirp->d_name, ".." ) == 0 ) {
549 strcat( buf, dirp->d_name );
550 if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
551 syslog( LOG_DEBUG, "setdirowner: chown %d/%d %s: %s",
552 uid, gid, buf, strerror(errno) );
553 /* return ( -1 ); Sometimes this is okay */
559 * We cheat: we know that chown doesn't do anything.
561 if ( stat( ".AppleDouble", &st ) < 0 ) {
562 syslog( LOG_ERR, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
565 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
567 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .AppleDouble: %s",
568 uid, gid, strerror(errno) );
569 /* return ( -1 ); Sometimes this is okay */
572 setdirowner_noadouble:
573 if ( stat( ".", &st ) < 0 ) {
576 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
578 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .: %s",
579 uid, gid, strerror(errno) );