2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/param.h>
17 #include <atalk/logger.h>
18 #include <atalk/adouble.h>
19 #include <atalk/vfs.h>
20 #include <atalk/afp.h>
21 #include <atalk/util.h>
22 #include <atalk/unix.h>
23 #include <atalk/acl.h>
26 #include "directory.h"
35 * Get the free space on a partition.
37 int ustatfs_getvolspace(const struct vol *vol, VolSpace *bfree, VolSpace *btotal, uint32_t *bsize)
39 VolSpace maxVolSpace = UINT64_MAX;
47 if ( statfs( vol->v_path, &sfs ) < 0 ) {
48 LOG(log_error, logtype_afpd, "ustatfs_getvolspace unable to stat %s", vol->v_path);
49 return( AFPERR_PARAM );
53 *bfree = (VolSpace) sfs.fd_req.bfreen;
56 *bfree = (VolSpace) sfs.f_bavail;
57 *bsize = sfs.f_frsize;
60 if ( *bfree > maxVolSpace / *bsize ) {
68 ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
71 ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
74 /* see similar block above comments */
75 if ( *btotal > maxVolSpace / *bsize ) {
76 *btotal = maxVolSpace;
84 static int utombits(mode_t bits)
90 mbits |= ( bits & ( S_IREAD >> 6 )) ? AR_UREAD : 0;
91 mbits |= ( bits & ( S_IWRITE >> 6 )) ? AR_UWRITE : 0;
92 /* Do we really need this? */
93 mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0;
98 /* --------------------------------
101 void utommode(struct stat *stat, struct maccess *ma)
105 mode = stat->st_mode;
106 ma->ma_world = utombits( mode );
109 ma->ma_group = utombits( mode );
112 ma->ma_owner = utombits( mode );
114 /* ma_user is a union of all permissions but we must follow
117 if ( (uuid == stat->st_uid) || (uuid == 0)) {
118 ma->ma_user = ma->ma_owner | AR_UOWN;
120 else if ( gmem( stat->st_gid )) {
121 ma->ma_user = ma->ma_group;
124 ma->ma_user = ma->ma_world;
128 * There are certain things the mac won't try if you don't have
129 * the "owner" bit set, even tho you can do these things on unix wiht
130 * only write permission. What were the things?
133 * ditto seems to care if st_uid is 0 ?
134 * was ma->ma_user & AR_UWRITE
135 * but 0 as owner is a can of worms.
137 if ( !stat->st_uid ) {
138 ma->ma_user |= AR_UOWN;
147 * Calculate the mode for a directory using a stat() call to
148 * estimate permission.
150 * Note: the previous method, using access(), does not work correctly
153 * dir parameter is used by AFS
155 void accessmode(const struct vol *vol, char *path, struct maccess *ma, struct dir *dir _U_, struct stat *st)
159 ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
161 if (lstat(path, &sb) != 0)
167 acltoownermode(vol, path, st, ma);
171 int gmem(const gid_t gid)
175 for ( i = 0; i < ngroups; i++ ) {
176 if ( groups[ i ] == gid ) {
183 static mode_t mtoubits(u_char bits)
189 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
190 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
191 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
192 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
197 /* ----------------------------------
198 from the finder's share windows (menu--> File--> sharing...)
199 and from AFP 3.0 spec page 63
200 the mac mode should be save somewhere
202 mode_t mtoumode(struct maccess *ma)
207 mode |= mtoubits( ma->ma_owner |ma->ma_world);
210 mode |= mtoubits( ma->ma_group |ma->ma_world);
213 mode |= mtoubits( ma->ma_world );
218 #define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
220 int setdeskmode(const mode_t mode)
222 char wd[ MAXPATHLEN + 1];
224 char modbuf[ 12 + 1], *m;
225 struct dirent *deskp, *subp;
228 if (!dir_rx_set(mode)) {
229 /* want to remove read and search access to owner it will screw the volume */
232 if ( getcwd( wd , MAXPATHLEN) == NULL ) {
235 if ( chdir( ".AppleDesktop" ) < 0 ) {
238 if (( desk = opendir( "." )) == NULL ) {
239 if ( chdir( wd ) < 0 ) {
240 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
244 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
245 if ( strcmp( deskp->d_name, "." ) == 0 ||
246 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
249 strcpy( modbuf, deskp->d_name );
250 strcat( modbuf, "/" );
251 m = strchr( modbuf, '\0' );
252 if (( sub = opendir( deskp->d_name )) == NULL ) {
255 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
256 if ( strcmp( subp->d_name, "." ) == 0 ||
257 strcmp( subp->d_name, ".." ) == 0 ) {
261 strcat( modbuf, subp->d_name );
262 /* XXX: need to preserve special modes */
263 if (lstat(modbuf, &st) < 0) {
264 LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
268 if (S_ISDIR(st.st_mode)) {
269 if ( chmod_acl( modbuf, (DIRBITS | mode)) < 0 && errno != EPERM ) {
270 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
272 } else if ( chmod_acl( modbuf, mode & ~EXEC_MODE ) < 0 && errno != EPERM ) {
273 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
278 /* XXX: need to preserve special modes */
279 if ( chmod_acl( deskp->d_name, (DIRBITS | mode)) < 0 && errno != EPERM ) {
280 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
284 if ( chdir( wd ) < 0 ) {
285 LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
288 /* XXX: need to preserve special modes */
289 if ( chmod_acl( ".AppleDesktop", (DIRBITS | mode)) < 0 && errno != EPERM ) {
290 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
295 /* --------------------- */
296 int setfilunixmode (const struct vol *vol, struct path* path, mode_t mode)
298 if (!path->st_valid) {
302 if (path->st_errno) {
306 mode |= vol->v_fperm;
308 if (setfilmode( path->u_name, mode, &path->st, vol->v_umask) < 0)
310 /* we need to set write perm if read set for resource fork */
311 return vol->vfs->vfs_setfilmode(vol, path->u_name, mode, &path->st);
315 /* --------------------- */
316 int setdirunixmode(const struct vol *vol, const char *name, mode_t mode)
318 LOG(log_debug, logtype_afpd, "setdirunixmode('%s', mode:%04o) {v_dperm:%04o}",
319 fullpathname(name), mode, vol->v_dperm);
321 mode |= vol->v_dperm;
323 if (dir_rx_set(mode)) {
324 /* extending right? dir first then .AppleDouble in rf_setdirmode */
325 if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
328 if (vol->vfs->vfs_setdirunixmode(vol, name, mode, NULL) < 0) {
331 if (!dir_rx_set(mode)) {
332 if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
338 /* ----------------------------- */
339 int setdeskowner(const uid_t uid, const gid_t gid)
341 char wd[ MAXPATHLEN + 1];
342 char modbuf[12 + 1], *m;
343 struct dirent *deskp, *subp;
346 if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
349 if ( chdir( ".AppleDesktop" ) < 0 ) {
352 if (( desk = opendir( "." )) == NULL ) {
353 if ( chdir( wd ) < 0 ) {
354 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
358 for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
359 if ( strcmp( deskp->d_name, "." ) == 0 ||
360 strcmp( deskp->d_name, ".." ) == 0 ||
361 strlen( deskp->d_name ) > 2 ) {
364 strcpy( modbuf, deskp->d_name );
365 strcat( modbuf, "/" );
366 m = strchr( modbuf, '\0' );
367 if (( sub = opendir( deskp->d_name )) == NULL ) {
370 for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
371 if ( strcmp( subp->d_name, "." ) == 0 ||
372 strcmp( subp->d_name, ".." ) == 0 ) {
376 strcat( modbuf, subp->d_name );
377 /* XXX: add special any uid, ignore group bits */
378 if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
379 LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
383 /* XXX: add special any uid, ignore group bits */
384 if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
385 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s",
386 deskp->d_name, strerror(errno) );
390 if ( chdir( wd ) < 0 ) {
391 LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
394 if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
395 LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
400 /* ----------------------------- */
401 int setfilowner(const struct vol *vol, const uid_t uid, const gid_t gid, struct path* path)
403 if (lchown(path->u_name, uid, gid) < 0 && errno != EPERM) {
404 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
405 uid, gid, path->u_name, strerror(errno));
409 if (vol->vfs->vfs_chown(vol, path->u_name, uid, gid) < 0 && errno != EPERM) {
410 LOG(log_debug, logtype_afpd, "setfilowner: rf_chown %d/%d %s: %s",
411 uid, gid, path->u_name, strerror(errno) );
418 /* ---------------------------------
419 * uid/gid == 0 need to be handled as special cases. they really mean
420 * that user/group should inherit from other, but that doesn't fit
421 * into the unix permission scheme. we can get around this by
422 * co-opting some bits. */
423 int setdirowner(const struct vol *vol, const char *name, const uid_t uid, const gid_t gid)
425 if (lchown(name, uid, gid ) < 0 && errno != EPERM ) {
426 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
427 uid, gid, fullpathname(name), strerror(errno) );
430 if (vol->vfs->vfs_setdirowner(vol, name, uid, gid) < 0)