]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/unix.c
AC_HEADER_STDC autoconf change
[netatalk.git] / etc / afpd / unix.c
1 /*
2  * $Id: unix.c,v 1.21 2001-09-06 20:00:59 rufustfirefly 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        if ( (retval=chmod( name, DIRBITS | mode )) < 0 )  {
263           syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
264                   name, strerror(errno) );
265        }
266 #ifdef DROPKLUDGE
267      } /* end if not mode */
268    } /* end checking for "dropbox" */
269 #endif /* DROPKLUDGE */
270    return retval;
271 }
272
273 int setdeskmode( mode )
274     const mode_t        mode;
275 {
276     char                wd[ MAXPATHLEN + 1];
277     struct stat         st;
278     char                modbuf[ 12 + 1], *m;
279     struct dirent       *deskp, *subp;
280     DIR                 *desk, *sub;
281
282     if ( getcwd( wd , MAXPATHLEN) == NULL ) {
283         return( -1 );
284     }
285     if ( chdir( ".AppleDesktop" ) < 0 ) {
286         return( -1 );
287     }
288     if (( desk = opendir( "." )) == NULL ) {
289         if ( chdir( wd ) < 0 ) {
290             syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
291         }
292         return( -1 );
293     }
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 ) {
297             continue;
298         }
299         strcpy( modbuf, deskp->d_name );
300         strcat( modbuf, "/" );
301         m = strchr( modbuf, '\0' );
302         if (( sub = opendir( deskp->d_name )) == NULL ) {
303             continue;
304         }
305         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
306             if ( strcmp( subp->d_name, "." ) == 0 ||
307                     strcmp( subp->d_name, ".." ) == 0 ) {
308                 continue;
309             }
310             *m = '\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) );
316                 continue;
317             }       
318
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) );
323               }
324             } else if ( chmod( modbuf,  mode ) < 0 ) {
325                 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
326                         modbuf, strerror(errno) );
327             }
328
329         }
330         closedir( sub );
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) );
335         }
336     }
337     closedir( desk );
338     if ( chdir( wd ) < 0 ) {
339         syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
340         return -1;
341     }
342     /* XXX: need to preserve special modes */
343     if ( chmod( ".AppleDesktop",  DIRBITS | mode ) < 0 ) {
344         syslog( LOG_ERR, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
345     }
346     return( 0 );
347 }
348
349 int setdirmode( mode, noadouble, dropbox )
350     const mode_t mode;
351     const int noadouble;
352     const int dropbox;
353 {
354     char                buf[ MAXPATHLEN + 1];
355     struct stat         st;
356     char                *m;
357     struct dirent       *dirp;
358     DIR                 *dir;
359
360     if (( dir = opendir( "." )) == NULL ) {
361         syslog( LOG_ERR, "setdirmode: opendir .: %s", strerror(errno) );
362         return( -1 );
363     }
364
365     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
366         if ( *dirp->d_name == '.' ) {
367             continue;
368         }
369         if ( stat( dirp->d_name, &st ) < 0 ) {
370             syslog( LOG_ERR, "setdirmode: stat %s: %s",
371                     dirp->d_name, strerror(errno) );
372             continue;
373         }
374
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)
379                 return (-1);
380             } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
381                 return (-1);
382         }
383     }
384     closedir( dir );
385     if (( dir = opendir( ".AppleDouble" )) == NULL ) {
386         if (noadouble)
387           goto setdirmode_noadouble;
388         syslog( LOG_ERR, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
389         return( -1 );
390     }
391     strcpy( buf, ".AppleDouble" );
392     strcat( buf, "/" );
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 ) {
397             continue;
398         }
399         *m = '\0';
400         strcat( buf, dirp->d_name );
401
402         if ( stat( buf, &st ) < 0 ) {
403             syslog( LOG_ERR, "setdirmode: stat %s: %s", buf, strerror(errno) );
404             continue;
405         }
406
407         if (S_ISDIR(st.st_mode)) {
408            stickydirmode( buf, DIRBITS | mode, dropbox );
409         } else 
410            stickydirmode( buf, mode, dropbox );
411     } /* end for */
412     closedir( dir );
413
414     /* XXX: use special bits to tag directory permissions */
415       
416     /* XXX: need to preserve special modes */
417     if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
418         return( -1 );
419
420 setdirmode_noadouble:
421     /* XXX: need to preserve special modes */
422     if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
423         return( -1 );
424     return( 0 );
425 }
426
427 int setdeskowner( uid, gid )
428     const uid_t uid;
429     const gid_t gid;
430 {
431     char                wd[ MAXPATHLEN + 1];
432     char                modbuf[12 + 1], *m;
433     struct dirent       *deskp, *subp;
434     DIR                 *desk, *sub;
435
436     if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
437         return( -1 );
438     }
439     if ( chdir( ".AppleDesktop" ) < 0 ) {
440         return( -1 );
441     }
442     if (( desk = opendir( "." )) == NULL ) {
443         if ( chdir( wd ) < 0 ) {
444             syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
445         }
446         return( -1 );
447     }
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 ) {
452             continue;
453         }
454         strcpy( modbuf, deskp->d_name );
455         strcat( modbuf, "/" );
456         m = strchr( modbuf, '\0' );
457         if (( sub = opendir( deskp->d_name )) == NULL ) {
458             continue;
459         }
460         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
461             if ( strcmp( subp->d_name, "." ) == 0 ||
462                  strcmp( subp->d_name, ".." ) == 0 ) {
463                 continue;
464             }
465             *m = '\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) );
471             }
472         }
473         closedir( sub );
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) );
478         }
479     }
480     closedir( desk );
481     if ( chdir( wd ) < 0 ) {
482         syslog( LOG_ERR, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
483         return -1;
484     }
485     if ( chown( ".AppleDesktop", uid, gid ) < 0 ) {
486         syslog( LOG_ERR, "setdeskowner: chown .AppleDesktop: %s",
487                 strerror(errno) );
488     }
489     return( 0 );
490 }
491
492
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 )
498     const uid_t uid;
499     const gid_t gid;
500     const int   noadouble;
501 {
502     char                buf[ MAXPATHLEN + 1];
503     struct stat         st;
504     char                *m;
505     struct dirent       *dirp;
506     DIR                 *dir;
507
508     if (( dir = opendir( "." )) == NULL ) {
509         return( -1 );
510     }
511     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
512         if ( *dirp->d_name == '.' ) {
513             continue;
514         };
515         if ( stat( dirp->d_name, &st ) < 0 ) {
516             syslog( LOG_ERR, "setdirowner: stat %s: %s",
517                     dirp->d_name, strerror(errno) );
518             continue;
519         }
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 */
525             }
526         }
527     }
528     closedir( dir );
529     if (( dir = opendir( ".AppleDouble" )) == NULL ) {
530       if (noadouble)
531         goto setdirowner_noadouble;
532       return( -1 );
533     }
534     strcpy( buf, ".AppleDouble" );
535     strcat( buf, "/" );
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 ) {
540             continue;
541         }
542         *m = '\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 */
548         }
549     }
550     closedir( dir );
551
552     /*
553      * We cheat: we know that chown doesn't do anything.
554      */
555     if ( stat( ".AppleDouble", &st ) < 0 ) {
556         syslog( LOG_ERR, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
557         return( -1 );
558     }
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 */
563     }
564
565 setdirowner_noadouble:
566     if ( stat( ".", &st ) < 0 ) {
567         return( -1 );
568     }
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) );
572     }
573
574     return( 0 );
575 }