2 * $Id: unix.c,v 1.21 2001-09-06 20:00:59 rufustfirefly 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 */
262 if ( (retval=chmod( name, DIRBITS | mode )) < 0 ) {
263 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
264 name, strerror(errno) );
267 } /* end if not mode */
268 } /* end checking for "dropbox" */
269 #endif /* DROPKLUDGE */
273 int setdeskmode( mode )
276 char wd[ MAXPATHLEN + 1];
278 char modbuf[ 12 + 1], *m;
279 struct dirent *deskp, *subp;
282 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
285 if ( chdir( ".AppleDesktop" ) < 0 ) {
288 if (( desk = opendir( "." )) == NULL ) {
289 if ( chdir( wd ) < 0 ) {
290 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
294 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
295 if ( strcmp( deskp->d_name, "." ) == 0 ||
296 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
299 strcpy( modbuf, deskp->d_name );
300 strcat( modbuf, "/" );
301 m = strchr( modbuf, '\0' );
302 if (( sub = opendir( deskp->d_name )) == NULL ) {
305 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
306 if ( strcmp( subp->d_name, "." ) == 0 ||
307 strcmp( subp->d_name, ".." ) == 0 ) {
311 strcat( modbuf, subp->d_name );
312 /* XXX: need to preserve special modes */
313 if (stat(modbuf, &st) < 0) {
314 syslog( LOG_ERR, "setdeskmode: stat %s: %s",
315 modbuf, strerror(errno) );
319 if (S_ISDIR(st.st_mode)) {
320 if ( chmod( modbuf, DIRBITS | mode ) < 0 ) {
321 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
322 modbuf, strerror(errno) );
324 } else if ( chmod( modbuf, mode ) < 0 ) {
325 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
326 modbuf, strerror(errno) );
331 /* XXX: need to preserve special modes */
332 if ( chmod( deskp->d_name, DIRBITS | mode ) < 0 ) {
333 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
334 deskp->d_name, strerror(errno) );
338 if ( chdir( wd ) < 0 ) {
339 syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
342 /* XXX: need to preserve special modes */
343 if ( chmod( ".AppleDesktop", DIRBITS | mode ) < 0 ) {
344 syslog( LOG_ERR, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
349 int setdirmode( mode, noadouble, dropbox )
354 char buf[ MAXPATHLEN + 1];
360 if (( dir = opendir( "." )) == NULL ) {
361 syslog( LOG_ERR, "setdirmode: opendir .: %s", strerror(errno) );
365 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
366 if ( *dirp->d_name == '.' ) {
369 if ( stat( dirp->d_name, &st ) < 0 ) {
370 syslog( LOG_ERR, "setdirmode: stat %s: %s",
371 dirp->d_name, strerror(errno) );
375 if (S_ISREG(st.st_mode)) {
376 /* XXX: need to preserve special modes */
377 if (S_ISDIR(st.st_mode)) {
378 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
380 } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
385 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
387 goto setdirmode_noadouble;
388 syslog( LOG_ERR, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
391 strcpy( buf, ".AppleDouble" );
393 m = strchr( buf, '\0' );
394 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
395 if ( strcmp( dirp->d_name, "." ) == 0 ||
396 strcmp( dirp->d_name, ".." ) == 0 ) {
400 strcat( buf, dirp->d_name );
402 if ( stat( buf, &st ) < 0 ) {
403 syslog( LOG_ERR, "setdirmode: stat %s: %s", buf, strerror(errno) );
407 if (S_ISDIR(st.st_mode)) {
408 stickydirmode( buf, DIRBITS | mode, dropbox );
410 stickydirmode( buf, mode, dropbox );
414 /* XXX: use special bits to tag directory permissions */
416 /* XXX: need to preserve special modes */
417 if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
420 setdirmode_noadouble:
421 /* XXX: need to preserve special modes */
422 if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
427 int setdeskowner( uid, gid )
431 char wd[ MAXPATHLEN + 1];
432 char modbuf[12 + 1], *m;
433 struct dirent *deskp, *subp;
436 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
439 if ( chdir( ".AppleDesktop" ) < 0 ) {
442 if (( desk = opendir( "." )) == NULL ) {
443 if ( chdir( wd ) < 0 ) {
444 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
448 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
449 if ( strcmp( deskp->d_name, "." ) == 0 ||
450 strcmp( deskp->d_name, ".." ) == 0 ||
451 strlen( deskp->d_name ) > 2 ) {
454 strcpy( modbuf, deskp->d_name );
455 strcat( modbuf, "/" );
456 m = strchr( modbuf, '\0' );
457 if (( sub = opendir( deskp->d_name )) == NULL ) {
460 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
461 if ( strcmp( subp->d_name, "." ) == 0 ||
462 strcmp( subp->d_name, ".." ) == 0 ) {
466 strcat( modbuf, subp->d_name );
467 /* XXX: add special any uid, ignore group bits */
468 if ( chown( modbuf, uid, gid ) < 0 ) {
469 syslog( LOG_ERR, "setdeskown: chown %s: %s",
470 modbuf, strerror(errno) );
474 /* XXX: add special any uid, ignore group bits */
475 if ( chown( deskp->d_name, uid, gid ) < 0 ) {
476 syslog( LOG_ERR, "setdeskowner: chown %s: %s",
477 deskp->d_name, strerror(errno) );
481 if ( chdir( wd ) < 0 ) {
482 syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
485 if ( chown( ".AppleDesktop", uid, gid ) < 0 ) {
486 syslog( LOG_ERR, "setdeskowner: chown .AppleDesktop: %s",
493 /* uid/gid == 0 need to be handled as special cases. they really mean
494 * that user/group should inherit from other, but that doesn't fit
495 * into the unix permission scheme. we can get around this by
496 * co-opting some bits. */
497 int setdirowner( uid, gid, noadouble )
502 char buf[ MAXPATHLEN + 1];
508 if (( dir = opendir( "." )) == NULL ) {
511 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
512 if ( *dirp->d_name == '.' ) {
515 if ( stat( dirp->d_name, &st ) < 0 ) {
516 syslog( LOG_ERR, "setdirowner: stat %s: %s",
517 dirp->d_name, strerror(errno) );
520 if (( st.st_mode & S_IFMT ) == S_IFREG ) {
521 if ( chown( dirp->d_name, uid, gid ) < 0 ) {
522 syslog( LOG_DEBUG, "setdirowner: chown %s: %s",
523 dirp->d_name, strerror(errno) );
524 /* return ( -1 ); Sometimes this is okay */
529 if (( dir = opendir( ".AppleDouble" )) == NULL ) {
531 goto setdirowner_noadouble;
534 strcpy( buf, ".AppleDouble" );
536 m = strchr( buf, '\0' );
537 for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
538 if ( strcmp( dirp->d_name, "." ) == 0 ||
539 strcmp( dirp->d_name, ".." ) == 0 ) {
543 strcat( buf, dirp->d_name );
544 if ( chown( buf, uid, gid ) < 0 ) {
545 syslog( LOG_DEBUG, "setdirowner: chown %d/%d %s: %s",
546 uid, gid, buf, strerror(errno) );
547 /* return ( -1 ); Sometimes this is okay */
553 * We cheat: we know that chown doesn't do anything.
555 if ( stat( ".AppleDouble", &st ) < 0 ) {
556 syslog( LOG_ERR, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
559 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 ) {
560 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .AppleDouble: %s",
561 uid, gid, strerror(errno) );
562 /* return ( -1 ); Sometimes this is okay */
565 setdirowner_noadouble:
566 if ( stat( ".", &st ) < 0 ) {
569 if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 ) {
570 syslog( LOG_DEBUG, "setdirowner: chown %d/%d .: %s",
571 uid, gid, strerror(errno) );