]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/unix.c
Fix a problem with setting perms on directories. Thanks to
[netatalk.git] / etc / afpd / unix.c
1 /*
2  * $Id: unix.c,v 1.22 2001-10-09 04:03:33 jmarcus Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <sys/stat.h>
19 #include <sys/syslog.h>
20 #include <netatalk/endian.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <atalk/afp.h>
24
25 /* STDC check */
26 #if STDC_HEADERS
27 #include <string.h>
28 #else /* STDC_HEADERS */
29 #ifndef HAVE_STRCHR
30 #define strchr index
31 #define strrchr index
32 #endif /* HAVE_STRCHR */
33 char *strchr (), *strrchr ();
34 #ifndef HAVE_MEMCPY
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 */
39
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif /* HAVE_FCNTL_H */
43 #include "auth.h"
44 #include "directory.h"
45 #include "volume.h"
46 #include "unix.h"
47
48 /*
49  * Get the free space on a partition.
50  */
51 int ustatfs_getvolspace( vol, bfree, btotal, bsize )
52     const struct vol    *vol;
53     VolSpace    *bfree, *btotal;
54     u_int32_t   *bsize;
55 {
56   VolSpace maxVolSpace = (~(VolSpace)0);
57   
58 #ifdef ultrix
59     struct fs_data      sfs;
60 #else /*ultrix*/
61     struct statfs       sfs;
62 #endif /*ultrix*/
63
64    
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 );
68     }
69
70 #ifdef ultrix
71     *bfree = (VolSpace) sfs.fd_req.bfreen;
72     *bsize = 1024;
73 #else /* !ultrix */
74     *bfree = (VolSpace) sfs.f_bavail;
75     *bsize = sfs.f_frsize;
76 #endif /* ultrix */
77
78     if ( *bfree > maxVolSpace / *bsize ) {
79         *bfree = maxVolSpace;
80     } else {
81         *bfree *= *bsize;
82     }
83
84 #ifdef ultrix
85     *btotal = (VolSpace) 
86       ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
87 #else /* !ultrix */
88     *btotal = (VolSpace) 
89       ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
90 #endif /* ultrix */
91
92     // see similar block above comments
93     if ( *btotal > maxVolSpace / *bsize ) {
94         *btotal = maxVolSpace;
95     } else {
96         *btotal *= *bsize;
97     }
98
99     return( AFP_OK );
100 }
101
102 static __inline__ int utombits( bits )
103     mode_t      bits;
104 {
105     int         mbits;
106
107     mbits = 0;
108
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; */
113
114     return( mbits );
115 }
116
117 void utommode( stat, ma )
118     struct stat         *stat;
119     struct maccess      *ma;
120 {
121     mode_t              mode;
122
123     mode = stat->st_mode;
124
125     ma->ma_world = utombits( mode );
126     mode = mode >> 3;
127
128     ma->ma_group = utombits( mode );
129     mode = mode >> 3;
130
131     ma->ma_owner = utombits( mode );
132
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;
137     } else {
138         ma->ma_user = ma->ma_world;
139     }
140
141     /*
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?
145      */
146     if ( ma->ma_user & AR_UWRITE ) {
147         ma->ma_user |= AR_UOWN;
148     }
149 }
150
151
152 /*
153  * Calculate the mode for a directory using Posix access() calls to
154  * estimate permission, a la mdw.
155  */
156 void accessmode( path, ma, dir )
157     char                *path;
158     struct maccess      *ma;
159     struct dir          *dir;
160 {
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;
167     } else {
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;
172         }
173         if ( access( path, X_OK ) == 0 ) {
174             ma->ma_user |= AR_USEARCH;
175             ma->ma_owner |= AR_USEARCH;
176         }
177         if ( access( path, W_OK ) == 0 ) {
178             ma->ma_user |= AR_UWRITE|AR_UOWN;
179             ma->ma_owner |= AR_UWRITE;
180         }
181     }
182 }
183
184 int gmem( gid )
185     const gid_t gid;
186 {
187     int         i;
188
189     for ( i = 0; i < ngroups; i++ ) {
190         if ( groups[ i ] == gid ) {
191             return( 1 );
192         }
193     }
194     return( 0 );
195 }
196
197 static __inline__ mode_t mtoubits( bits )
198     u_char      bits;
199 {
200     mode_t      mode;
201
202     mode = 0;
203
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; */
208
209     return( mode );
210 }
211
212 mode_t mtoumode( ma )
213     struct maccess      *ma;
214 {
215     mode_t              mode;
216
217     mode = 0;
218     mode |= mtoubits( ma->ma_owner );
219     mode = mode << 3;
220
221     mode |= mtoubits( ma->ma_group );
222     mode = mode << 3;
223
224     mode |= mtoubits( ma->ma_world );
225
226     return( mode );
227 }
228
229 inline int stickydirmode(name, mode, dropbox)
230     char * name;
231     const mode_t mode;
232     const int dropbox;
233 {
234   int retval;
235 #ifdef DROPKLUDGE
236   int uid;
237 #endif /* DROPKLUDGE */
238   
239 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
240    retval=0;
241 #ifdef DROPKLUDGE
242    if (dropbox) {
243     if (mode & S_IWOTH) { 
244       if (mode & S_IROTH); 
245       else { /* if S_IWOTH and not S_IROTH */
246         uid=geteuid();
247         if ( seteuid(0) < 0) {
248            syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %m");
249         }
250         if ( retval=chmod( name, ( DIRBITS | mode | S_ISVTX) ) < 0) {
251            syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %m", name );
252            return(AFPERR_ACCESS);
253         } else {
254 #ifdef DEBUG
255            syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\": %m", name );
256 #endif /* DEBUG */
257            seteuid(uid);
258         } /* end getting retval */
259       } /* end if not & S_IROTH */
260    } else { /* end if S_IWOTH and not S_IROTH */
261 #endif /* DROPKLUDGE */
262
263        /*
264         *  Ignore EPERM errors:  We may be dealing with a directory that is
265         *  group writable, in which case chmod will fail.
266         */
267        if ( chmod( name, DIRBITS | mode ) < 0 && errno != EPERM)  {
268           syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
269                   name, strerror(errno) );
270           retval = -1;
271        }
272 #ifdef DROPKLUDGE
273      } /* end if not mode */
274    } /* end checking for "dropbox" */
275 #endif /* DROPKLUDGE */
276    return retval;
277 }
278
279 int setdeskmode( mode )
280     const mode_t        mode;
281 {
282     char                wd[ MAXPATHLEN + 1];
283     struct stat         st;
284     char                modbuf[ 12 + 1], *m;
285     struct dirent       *deskp, *subp;
286     DIR                 *desk, *sub;
287
288     if ( getcwd( wd , MAXPATHLEN) == NULL ) {
289         return( -1 );
290     }
291     if ( chdir( ".AppleDesktop" ) < 0 ) {
292         return( -1 );
293     }
294     if (( desk = opendir( "." )) == NULL ) {
295         if ( chdir( wd ) < 0 ) {
296             syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
297         }
298         return( -1 );
299     }
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 ) {
303             continue;
304         }
305         strcpy( modbuf, deskp->d_name );
306         strcat( modbuf, "/" );
307         m = strchr( modbuf, '\0' );
308         if (( sub = opendir( deskp->d_name )) == NULL ) {
309             continue;
310         }
311         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
312             if ( strcmp( subp->d_name, "." ) == 0 ||
313                     strcmp( subp->d_name, ".." ) == 0 ) {
314                 continue;
315             }
316             *m = '\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) );
322                 continue;
323             }       
324
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) );
329               }
330             } else if ( chmod( modbuf,  mode ) < 0 && errno != EPERM ) {
331                 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
332                         modbuf, strerror(errno) );
333             }
334
335         }
336         closedir( sub );
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) );
341         }
342     }
343     closedir( desk );
344     if ( chdir( wd ) < 0 ) {
345         syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
346         return -1;
347     }
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) );
351     }
352     return( 0 );
353 }
354
355 int setdirmode( mode, noadouble, dropbox )
356     const mode_t mode;
357     const int noadouble;
358     const int dropbox;
359 {
360     char                buf[ MAXPATHLEN + 1];
361     struct stat         st;
362     char                *m;
363     struct dirent       *dirp;
364     DIR                 *dir;
365
366     if (( dir = opendir( "." )) == NULL ) {
367         syslog( LOG_ERR, "setdirmode: opendir .: %s", strerror(errno) );
368         return( -1 );
369     }
370
371     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
372         if ( *dirp->d_name == '.' ) {
373             continue;
374         }
375         if ( stat( dirp->d_name, &st ) < 0 ) {
376             syslog( LOG_ERR, "setdirmode: stat %s: %s",
377                     dirp->d_name, strerror(errno) );
378             continue;
379         }
380
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)
385                 return (-1);
386             } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
387                 return (-1);
388         }
389     }
390     closedir( dir );
391     if (( dir = opendir( ".AppleDouble" )) == NULL ) {
392         if (noadouble)
393           goto setdirmode_noadouble;
394         syslog( LOG_ERR, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
395         return( -1 );
396     }
397     strcpy( buf, ".AppleDouble" );
398     strcat( buf, "/" );
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 ) {
403             continue;
404         }
405         *m = '\0';
406         strcat( buf, dirp->d_name );
407
408         if ( stat( buf, &st ) < 0 ) {
409             syslog( LOG_ERR, "setdirmode: stat %s: %s", buf, strerror(errno) );
410             continue;
411         }
412
413         if (S_ISDIR(st.st_mode)) {
414            stickydirmode( buf, DIRBITS | mode, dropbox );
415         } else 
416            stickydirmode( buf, mode, dropbox );
417     } /* end for */
418     closedir( dir );
419
420     /* XXX: use special bits to tag directory permissions */
421       
422     /* XXX: need to preserve special modes */
423     if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
424         return( -1 );
425
426 setdirmode_noadouble:
427     /* XXX: need to preserve special modes */
428     if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
429         return( -1 );
430     return( 0 );
431 }
432
433 int setdeskowner( uid, gid )
434     const uid_t uid;
435     const gid_t gid;
436 {
437     char                wd[ MAXPATHLEN + 1];
438     char                modbuf[12 + 1], *m;
439     struct dirent       *deskp, *subp;
440     DIR                 *desk, *sub;
441
442     if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
443         return( -1 );
444     }
445     if ( chdir( ".AppleDesktop" ) < 0 ) {
446         return( -1 );
447     }
448     if (( desk = opendir( "." )) == NULL ) {
449         if ( chdir( wd ) < 0 ) {
450             syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
451         }
452         return( -1 );
453     }
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 ) {
458             continue;
459         }
460         strcpy( modbuf, deskp->d_name );
461         strcat( modbuf, "/" );
462         m = strchr( modbuf, '\0' );
463         if (( sub = opendir( deskp->d_name )) == NULL ) {
464             continue;
465         }
466         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
467             if ( strcmp( subp->d_name, "." ) == 0 ||
468                  strcmp( subp->d_name, ".." ) == 0 ) {
469                 continue;
470             }
471             *m = '\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) );
477             }
478         }
479         closedir( sub );
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) );
484         }
485     }
486     closedir( desk );
487     if ( chdir( wd ) < 0 ) {
488         syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
489         return -1;
490     }
491     if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
492         syslog( LOG_ERR, "setdeskowner: chown .AppleDesktop: %s",
493                 strerror(errno) );
494     }
495     return( 0 );
496 }
497
498
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 )
504     const uid_t uid;
505     const gid_t gid;
506     const int   noadouble;
507 {
508     char                buf[ MAXPATHLEN + 1];
509     struct stat         st;
510     char                *m;
511     struct dirent       *dirp;
512     DIR                 *dir;
513
514     if (( dir = opendir( "." )) == NULL ) {
515         return( -1 );
516     }
517     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
518         if ( *dirp->d_name == '.' ) {
519             continue;
520         };
521         if ( stat( dirp->d_name, &st ) < 0 ) {
522             syslog( LOG_ERR, "setdirowner: stat %s: %s",
523                     dirp->d_name, strerror(errno) );
524             continue;
525         }
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 */
531             }
532         }
533     }
534     closedir( dir );
535     if (( dir = opendir( ".AppleDouble" )) == NULL ) {
536       if (noadouble)
537         goto setdirowner_noadouble;
538       return( -1 );
539     }
540     strcpy( buf, ".AppleDouble" );
541     strcat( buf, "/" );
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 ) {
546             continue;
547         }
548         *m = '\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 */
554         }
555     }
556     closedir( dir );
557
558     /*
559      * We cheat: we know that chown doesn't do anything.
560      */
561     if ( stat( ".AppleDouble", &st ) < 0 ) {
562         syslog( LOG_ERR, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
563         return( -1 );
564     }
565     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
566          errno != EPERM ) {
567         syslog( LOG_DEBUG, "setdirowner: chown %d/%d .AppleDouble: %s",
568                 uid, gid, strerror(errno) );
569         /* return ( -1 ); Sometimes this is okay */
570     }
571
572 setdirowner_noadouble:
573     if ( stat( ".", &st ) < 0 ) {
574         return( -1 );
575     }
576     if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
577          errno != EPERM ) {
578         syslog( LOG_DEBUG, "setdirowner: chown %d/%d .: %s",
579                 uid, gid, strerror(errno) );
580     }
581
582     return( 0 );
583 }