]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/unix.c
Merge master
[netatalk.git] / etc / afpd / unix.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <inttypes.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <limits.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>
24
25 #include "auth.h"
26 #include "directory.h"
27 #include "volume.h"
28 #include "unix.h"
29 #include "fork.h"
30 #ifdef HAVE_ACLS
31 #include "acls.h"
32 #endif
33
34 /*
35  * Get the free space on a partition.
36  */
37 int ustatfs_getvolspace(const struct vol *vol, VolSpace *bfree, VolSpace *btotal, uint32_t *bsize)
38 {
39     VolSpace maxVolSpace = UINT64_MAX;
40
41 #ifdef ultrix
42     struct fs_data      sfs;
43 #else /*ultrix*/
44     struct statfs       sfs;
45 #endif /*ultrix*/
46
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 );
50     }
51
52 #ifdef ultrix
53     *bfree = (VolSpace) sfs.fd_req.bfreen;
54     *bsize = 1024;
55 #else /* !ultrix */
56     *bfree = (VolSpace) sfs.f_bavail;
57     *bsize = sfs.f_frsize;
58 #endif /* ultrix */
59
60     if ( *bfree > maxVolSpace / *bsize ) {
61         *bfree = maxVolSpace;
62     } else {
63         *bfree *= *bsize;
64     }
65
66 #ifdef ultrix
67     *btotal = (VolSpace)
68               ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
69 #else /* !ultrix */
70     *btotal = (VolSpace)
71               ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
72 #endif /* ultrix */
73
74     /* see similar block above comments */
75     if ( *btotal > maxVolSpace / *bsize ) {
76         *btotal = maxVolSpace;
77     } else {
78         *btotal *= *bsize;
79     }
80
81     return( AFP_OK );
82 }
83
84 static int utombits(mode_t bits)
85 {
86     int         mbits;
87
88     mbits = 0;
89
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;
94
95     return( mbits );
96 }
97
98 /* --------------------------------
99     cf AFP 3.0 page 63
100 */
101 static void utommode(const AFPObj *obj, const struct stat *stat, struct maccess *ma)
102 {
103     mode_t mode;
104
105     mode = stat->st_mode;
106     ma->ma_world = utombits( mode );
107     mode = mode >> 3;
108
109     ma->ma_group = utombits( mode );
110     mode = mode >> 3;
111
112     ma->ma_owner = utombits( mode );
113
114     /* ma_user is a union of all permissions but we must follow
115      * unix perm
116     */
117     if ( (obj->uid == stat->st_uid) || (obj->uid == 0)) {
118         ma->ma_user = ma->ma_owner | AR_UOWN;
119     }
120     else if (gmem(stat->st_gid, obj->ngroups, obj->groups)) {
121         ma->ma_user = ma->ma_group;
122     }
123     else {
124         ma->ma_user = ma->ma_world;
125     }
126
127     /*
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?
131      * 
132      * FIXME 
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.
136      */
137     if ( !stat->st_uid ) {
138         ma->ma_user |= AR_UOWN;
139     }
140 }
141
142 #ifdef accessmode
143
144 #undef accessmode
145 #endif
146 /*
147  * Calculate the mode for a directory using a stat() call to
148  * estimate permission.
149  *
150  * Note: the previous method, using access(), does not work correctly
151  * over NFS.
152  *
153  * dir parameter is used by AFS
154  */
155 void accessmode(const AFPObj *obj, const struct vol *vol, char *path, struct maccess *ma, struct dir *dir _U_, struct stat *st)
156 {
157     struct stat     sb;
158
159     ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
160     if (!st) {
161         if (lstat(path, &sb) != 0)
162             return;
163         st = &sb;
164     }
165     utommode(obj, st, ma );
166 #ifdef HAVE_ACLS
167     acltoownermode(obj, vol, path, st, ma);
168 #endif
169 }
170
171 static mode_t mtoubits(u_char bits)
172 {
173     mode_t      mode;
174
175     mode = 0;
176
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; */
181
182     return( mode );
183 }
184
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 
189 */
190 mode_t mtoumode(struct maccess *ma)
191 {
192     mode_t              mode;
193
194     mode = 0;
195     mode |= mtoubits( ma->ma_owner |ma->ma_world);
196     mode = mode << 3;
197
198     mode |= mtoubits( ma->ma_group |ma->ma_world);
199     mode = mode << 3;
200
201     mode |= mtoubits( ma->ma_world );
202
203     return( mode );
204 }
205
206 #define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
207
208 int setdeskmode(const mode_t mode)
209 {
210     char                wd[ MAXPATHLEN + 1];
211     struct stat         st;
212     char                modbuf[ 12 + 1], *m;
213     struct dirent       *deskp, *subp;
214     DIR                 *desk, *sub;
215
216     if (!dir_rx_set(mode)) {
217         /* want to remove read and search access to owner it will screw the volume */
218         return -1 ;
219     }
220     if ( getcwd( wd , MAXPATHLEN) == NULL ) {
221         return( -1 );
222     }
223     if ( chdir( ".AppleDesktop" ) < 0 ) {
224         return( -1 );
225     }
226     if (( desk = opendir( "." )) == NULL ) {
227         if ( chdir( wd ) < 0 ) {
228             LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
229         }
230         return( -1 );
231     }
232     for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
233         if ( strcmp( deskp->d_name, "." ) == 0 ||
234                 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
235             continue;
236         }
237         strcpy( modbuf, deskp->d_name );
238         strcat( modbuf, "/" );
239         m = strchr( modbuf, '\0' );
240         if (( sub = opendir( deskp->d_name )) == NULL ) {
241             continue;
242         }
243         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
244             if ( strcmp( subp->d_name, "." ) == 0 ||
245                     strcmp( subp->d_name, ".." ) == 0 ) {
246                 continue;
247             }
248             *m = '\0';
249             strcat( modbuf, subp->d_name );
250             /* XXX: need to preserve special modes */
251             if (lstat(modbuf, &st) < 0) {
252                 LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
253                 continue;
254             }
255
256             if (S_ISDIR(st.st_mode)) {
257                 if ( chmod_acl( modbuf,  (DIRBITS | mode)) < 0 && errno != EPERM ) {
258                      LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
259                 }
260             } else if ( chmod_acl( modbuf,  mode & ~EXEC_MODE ) < 0 && errno != EPERM ) {
261                 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
262             }
263
264         }
265         closedir( sub );
266         /* XXX: need to preserve special modes */
267         if ( chmod_acl( deskp->d_name,  (DIRBITS | mode)) < 0 && errno != EPERM ) {
268             LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
269         }
270     }
271     closedir( desk );
272     if ( chdir( wd ) < 0 ) {
273         LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
274         return -1;
275     }
276     /* XXX: need to preserve special modes */
277     if ( chmod_acl( ".AppleDesktop",  (DIRBITS | mode)) < 0 && errno != EPERM ) {
278         LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
279     }
280     return( 0 );
281 }
282
283 /* --------------------- */
284 int setfilunixmode (const struct vol *vol, struct path* path, mode_t mode)
285 {
286     if (!path->st_valid) {
287         of_stat(path);
288     }
289
290     if (path->st_errno) {
291         return -1;
292     }
293         
294     mode |= vol->v_fperm;
295
296     if (setfilmode( path->u_name, mode, &path->st, vol->v_umask) < 0)
297         return -1;
298     /* we need to set write perm if read set for resource fork */
299     return vol->vfs->vfs_setfilmode(vol, path->u_name, mode, &path->st);
300 }
301
302
303 /* --------------------- */
304 int setdirunixmode(const struct vol *vol, const char *name, mode_t mode)
305 {
306     LOG(log_debug, logtype_afpd, "setdirunixmode('%s', mode:%04o) {v_dperm:%04o}",
307         fullpathname(name), mode, vol->v_dperm);
308
309     mode |= vol->v_dperm;
310
311     if (dir_rx_set(mode)) {
312         /* extending right? dir first then .AppleDouble in rf_setdirmode */
313         if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
314                 return -1;
315     }
316     if (vol->vfs->vfs_setdirunixmode(vol, name, mode, NULL) < 0) {
317         return  -1 ;
318     }
319     if (!dir_rx_set(mode)) {
320         if (chmod_acl(name, (DIRBITS | mode) & ~vol->v_umask) < 0 )
321             return -1;
322     }
323     return 0;
324 }
325
326 /* ----------------------------- */
327 int setdeskowner(const uid_t uid, const gid_t gid)
328 {
329     char                wd[ MAXPATHLEN + 1];
330     char                modbuf[12 + 1], *m;
331     struct dirent       *deskp, *subp;
332     DIR                 *desk, *sub;
333
334     if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
335         return( -1 );
336     }
337     if ( chdir( ".AppleDesktop" ) < 0 ) {
338         return( -1 );
339     }
340     if (( desk = opendir( "." )) == NULL ) {
341         if ( chdir( wd ) < 0 ) {
342             LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
343         }
344         return( -1 );
345     }
346     for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
347         if ( strcmp( deskp->d_name, "." ) == 0 ||
348                 strcmp( deskp->d_name, ".." ) == 0 ||
349                 strlen( deskp->d_name ) > 2 ) {
350             continue;
351         }
352         strcpy( modbuf, deskp->d_name );
353         strcat( modbuf, "/" );
354         m = strchr( modbuf, '\0' );
355         if (( sub = opendir( deskp->d_name )) == NULL ) {
356             continue;
357         }
358         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
359             if ( strcmp( subp->d_name, "." ) == 0 ||
360                     strcmp( subp->d_name, ".." ) == 0 ) {
361                 continue;
362             }
363             *m = '\0';
364             strcat( modbuf, subp->d_name );
365             /* XXX: add special any uid, ignore group bits */
366             if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
367                 LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
368             }
369         }
370         closedir( sub );
371         /* XXX: add special any uid, ignore group bits */
372         if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
373             LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s",
374                 deskp->d_name, strerror(errno) );
375         }
376     }
377     closedir( desk );
378     if ( chdir( wd ) < 0 ) {
379         LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
380         return -1;
381     }
382     if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
383         LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
384     }
385     return( 0 );
386 }
387
388 /* ----------------------------- */
389 int setfilowner(const struct vol *vol, const uid_t uid, const gid_t gid, struct path* path)
390 {
391     if (lchown(path->u_name, uid, gid) < 0 && errno != EPERM) {
392         LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
393             uid, gid, path->u_name, strerror(errno));
394         return -1;
395     }
396
397     if (vol->vfs->vfs_chown(vol, path->u_name, uid, gid) < 0 && errno != EPERM) {
398         LOG(log_debug, logtype_afpd, "setfilowner: rf_chown %d/%d %s: %s",
399             uid, gid, path->u_name, strerror(errno) );
400         return -1;
401     }
402
403     return 0;
404 }
405
406 /* --------------------------------- 
407  * uid/gid == 0 need to be handled as special cases. they really mean
408  * that user/group should inherit from other, but that doesn't fit
409  * into the unix permission scheme. we can get around this by
410  * co-opting some bits. */
411 int setdirowner(const struct vol *vol, const char *name, const uid_t uid, const gid_t gid)
412 {
413     if (lchown(name, uid, gid ) < 0 && errno != EPERM ) {
414         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
415             uid, gid, fullpathname(name), strerror(errno) );
416     }
417
418     if (vol->vfs->vfs_setdirowner(vol, name, uid, gid) < 0)
419         return -1;
420
421     return( 0 );
422 }
423