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 static void utommode(const AFPObj *obj, const 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 ( (obj->uid == stat->st_uid) || (obj->uid == 0)) {
118 ma->ma_user = ma->ma_owner | AR_UOWN;
120 else if (gmem(stat->st_gid, obj->ngroups, obj->groups)) {
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 AFPObj *obj, 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 (ostat(path, &sb, vol_syml_opt(vol)) != 0)
165 utommode(obj, st, ma );
167 acltoownermode(obj, vol, path, st, ma);
171 static mode_t mtoubits(u_char bits)
177 mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
178 mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
179 /* I don't think there's a way to set the SEARCH bit by itself on a Mac
180 mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
185 /* ----------------------------------
186 from the finder's share windows (menu--> File--> sharing...)
187 and from AFP 3.0 spec page 63
188 the mac mode should be save somewhere
190 mode_t mtoumode(struct maccess *ma)
195 mode |= mtoubits( ma->ma_owner |ma->ma_world);
198 mode |= mtoubits( ma->ma_group |ma->ma_world);
201 mode |= mtoubits( ma->ma_world );
206 /* --------------------- */
207 int setfilunixmode (const struct vol *vol, struct path* path, mode_t mode)
209 if (!path->st_valid) {
213 if (path->st_errno) {
217 mode |= vol->v_fperm;
219 if (setfilmode(vol, path->u_name, mode, &path->st) < 0)
221 /* we need to set write perm if read set for resource fork */
222 return vol->vfs->vfs_setfilmode(vol, path->u_name, mode, &path->st);
226 /* --------------------- */
227 int setdirunixmode(const struct vol *vol, char *name, mode_t mode)
229 LOG(log_debug, logtype_afpd, "setdirunixmode('%s', mode:%04o) {v_dperm:%04o}",
230 fullpathname(name), mode, vol->v_dperm);
232 mode |= vol->v_dperm;
234 if (dir_rx_set(mode)) {
235 /* extending right? dir first then .AppleDouble in rf_setdirmode */
236 if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
239 if (vol->vfs->vfs_setdirunixmode(vol, name, mode, NULL) < 0) {
242 if (!dir_rx_set(mode)) {
243 if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
249 /* ----------------------------- */
250 int setfilowner(const struct vol *vol, const uid_t uid, const gid_t gid, struct path* path)
252 if (ochown( path->u_name, uid, gid, vol_syml_opt(vol)) < 0 && errno != EPERM ) {
253 LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
254 uid, gid, path->u_name, strerror(errno));
258 if (vol->vfs->vfs_chown(vol, path->u_name, uid, gid) < 0 && errno != EPERM) {
259 LOG(log_debug, logtype_afpd, "setfilowner: rf_chown %d/%d %s: %s",
260 uid, gid, path->u_name, strerror(errno) );
267 /* ---------------------------------
268 * uid/gid == 0 need to be handled as special cases. they really mean
269 * that user/group should inherit from other, but that doesn't fit
270 * into the unix permission scheme. we can get around this by
271 * co-opting some bits. */
272 int setdirowner(const struct vol *vol, const char *name, const uid_t uid, const gid_t gid)
274 if (ochown(name, uid, gid, vol_syml_opt(vol)) < 0 && errno != EPERM ) {
275 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
276 uid, gid, fullpathname(name), strerror(errno) );
279 if (vol->vfs->vfs_setdirowner(vol, name, uid, gid) < 0)