]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/unix.c
82b84eb3d0c4dfa6e5afa7e377bf033f6f26eb63
[netatalk.git] / etc / afpd / unix.c
1 /*
2  * $Id: unix.c,v 1.43.2.1.2.7 2004-05-10 18:40:32 didg 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
15 /* STDC check */
16 #ifdef STDC_HEADERS
17 #include <string.h>
18 #else /* STDC_HEADERS */
19
20 #ifndef HAVE_STRCHR
21 #define strchr index
22 #define strrchr index
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
25
26 #ifndef HAVE_MEMCPY
27 #define memcpy(d,s,n) bcopy ((s), (d), (n))
28 #define memmove(d,s,n) bcopy ((s), (d), (n))
29 #endif /* ! HAVE_MEMCPY */
30 #endif /* STDC_HEADERS */
31
32 #include <errno.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <sys/param.h>
36 #include <atalk/logger.h>
37 #include <atalk/adouble.h>
38 #include <atalk/afp.h>
39 #include <atalk/util.h>
40
41 #include "auth.h"
42 #include "directory.h"
43 #include "volume.h"
44 #include "unix.h"
45 #include "fork.h"
46
47 /*
48  * Get the free space on a partition.
49  */
50 int ustatfs_getvolspace( vol, bfree, btotal, bsize )
51 const struct vol        *vol;
52 VolSpace    *bfree, *btotal;
53 u_int32_t   *bsize;
54 {
55     VolSpace maxVolSpace = (~(VolSpace)0);
56
57 #ifdef ultrix
58     struct fs_data      sfs;
59 #else /*ultrix*/
60     struct statfs       sfs;
61 #endif /*ultrix*/
62
63
64     if ( statfs( vol->v_path, &sfs ) < 0 ) {
65         LOG(log_error, logtype_afpd, "ustatfs_getvolspace unable to stat %s", vol->v_path);
66         return( AFPERR_PARAM );
67     }
68
69 #ifdef ultrix
70     *bfree = (VolSpace) sfs.fd_req.bfreen;
71     *bsize = 1024;
72 #else /* !ultrix */
73     *bfree = (VolSpace) sfs.f_bavail;
74     *bsize = sfs.f_frsize;
75 #endif /* ultrix */
76
77     if ( *bfree > maxVolSpace / *bsize ) {
78         *bfree = maxVolSpace;
79     } else {
80         *bfree *= *bsize;
81     }
82
83 #ifdef ultrix
84     *btotal = (VolSpace)
85               ( sfs.fd_req.btot - ( sfs.fd_req.bfree - sfs.fd_req.bfreen ));
86 #else /* !ultrix */
87     *btotal = (VolSpace)
88               ( sfs.f_blocks - ( sfs.f_bfree - sfs.f_bavail ));
89 #endif /* ultrix */
90
91     /* see similar block above comments */
92     if ( *btotal > maxVolSpace / *bsize ) {
93         *btotal = maxVolSpace;
94     } else {
95         *btotal *= *bsize;
96     }
97
98     return( AFP_OK );
99 }
100
101 static __inline__ int utombits( bits )
102 mode_t  bits;
103 {
104     int         mbits;
105
106     mbits = 0;
107
108     mbits |= ( bits & ( S_IREAD >> 6 ))  ? AR_UREAD  : 0;
109     mbits |= ( bits & ( S_IWRITE >> 6 )) ? AR_UWRITE : 0;
110     /* Do we really need this? */
111     mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0;
112
113     return( mbits );
114 }
115
116 /* --------------------------------
117     cf AFP 3.0 page 63
118 */
119 void utommode( stat, ma )
120 struct stat             *stat;
121 struct maccess  *ma;
122 {
123 mode_t mode;
124
125     mode = stat->st_mode;
126     ma->ma_world = utombits( mode );
127     mode = mode >> 3;
128
129     ma->ma_group = utombits( mode );
130     mode = mode >> 3;
131
132     ma->ma_owner = utombits( mode );
133
134     /* ma_user is a union of all permissions but we must follow
135      * unix perm
136     */
137     if ( (uuid == stat->st_uid) || (uuid == 0)) {
138         ma->ma_user = ma->ma_owner | AR_UOWN;
139     }
140     else if ( gmem( stat->st_gid )) {
141         ma->ma_user = ma->ma_group;
142     }
143     else {
144         ma->ma_user = ma->ma_world;
145     }
146
147     /*
148      * There are certain things the mac won't try if you don't have
149      * the "owner" bit set, even tho you can do these things on unix wiht
150      * only write permission.  What were the things?
151      * 
152      * FIXME and so what ?
153      */
154 #if 0
155     if ( ma->ma_user & AR_UWRITE ) {
156         ma->ma_user |= AR_UOWN;
157     }
158 #endif    
159 }
160
161
162 #ifdef accessmode
163 #undef accessmode
164 #endif
165 /*
166  * Calculate the mode for a directory using a stat() call to
167  * estimate permission.
168  *
169  * Note: the previous method, using access(), does not work correctly
170  * over NFS.
171  * FIXME what about ACL?
172  */
173 void accessmode( path, ma, dir, st )
174 char            *path;
175 struct maccess  *ma;
176 struct dir      *dir;
177 struct stat     *st;
178
179 {
180 struct stat     sb;
181
182     ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
183     if (!st) {
184         if (stat(path, &sb) != 0)
185             return;
186         st = &sb;
187     }
188     utommode( st, ma );
189     return;
190 }
191
192 int gmem( gid )
193 const gid_t     gid;
194 {
195     int         i;
196
197     for ( i = 0; i < ngroups; i++ ) {
198         if ( groups[ i ] == gid ) {
199             return( 1 );
200         }
201     }
202     return( 0 );
203 }
204
205 static __inline__ mode_t mtoubits( bits )
206 u_char  bits;
207 {
208     mode_t      mode;
209
210     mode = 0;
211
212     mode |= ( bits & AR_UREAD ) ? ( (S_IREAD | S_IEXEC) >> 6 ) : 0;
213     mode |= ( bits & AR_UWRITE ) ? ( (S_IWRITE | S_IEXEC) >> 6 ) : 0;
214     /* I don't think there's a way to set the SEARCH bit by itself on a Mac
215         mode |= ( bits & AR_USEARCH ) ? ( S_IEXEC >> 6 ) : 0; */
216
217     return( mode );
218 }
219
220 /* ----------------------------------
221    from the finder's share windows (menu--> File--> sharing...)
222    and from AFP 3.0 spec page 63 
223    the mac mode should be save somewhere 
224 */
225 mode_t mtoumode( ma )
226 struct maccess  *ma;
227 {
228     mode_t              mode;
229
230     mode = 0;
231     mode |= mtoubits( ma->ma_owner |ma->ma_world);
232     mode = mode << 3;
233
234     mode |= mtoubits( ma->ma_group |ma->ma_world);
235     mode = mode << 3;
236
237     mode |= mtoubits( ma->ma_world );
238
239     return( mode );
240 }
241
242 /* ----------------------------- */
243 char *fullpathname(const char *name)
244 {
245     static char wd[ MAXPATHLEN + 1];
246
247     if ( getcwd( wd , MAXPATHLEN) ) {
248         strlcat(wd, "/", MAXPATHLEN);
249         strlcat(wd, name, MAXPATHLEN);
250     }
251     else {
252         strlcpy(wd, name, MAXPATHLEN);
253     }
254     return wd;
255 }
256
257 /* -----------------------------
258    a dropbox is a folder where w is set but not r eg:
259    rwx-wx-wx or rwx-wx-- 
260    rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
261 */
262 static int stickydirmode(name, mode, dropbox)
263 char * name;
264 const mode_t mode;
265 const int dropbox;
266 {
267     int retval = 0;
268
269 #ifdef DROPKLUDGE
270     /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
271     if (dropbox) {
272         int uid;
273
274         if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
275              ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
276         {
277             uid=geteuid();
278             if ( seteuid(0) < 0) {
279                 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
280             }
281             if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) )) < 0) {
282                 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
283             } else {
284 #ifdef DEBUG
285                 LOG(log_info, logtype_afpd, "stickydirmode: (debug) chmod \"%s\": %s", fullpathname(name), strerror(retval) );
286 #endif /* DEBUG */
287             }
288             seteuid(uid);
289             return retval;
290         }
291     }
292 #endif /* DROPKLUDGE */
293
294     /*
295      *  Ignore EPERM errors:  We may be dealing with a directory that is
296      *  group writable, in which case chmod will fail.
297      */
298     if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM)  {
299         LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
300         retval = -1;
301     }
302
303     return retval;
304 }
305
306 /* ------------------------- */
307 int dir_rx_set(mode_t mode)
308 {
309     return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
310 }
311
312 #define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
313
314 int setdeskmode( mode )
315 const mode_t    mode;
316 {
317     char                wd[ MAXPATHLEN + 1];
318     struct stat         st;
319     char                modbuf[ 12 + 1], *m;
320     struct dirent       *deskp, *subp;
321     DIR                 *desk, *sub;
322
323     if (!dir_rx_set(mode)) {
324         /* want to remove read and search access to owner it will screw the volume */
325         return -1 ;
326     }
327     if ( getcwd( wd , MAXPATHLEN) == NULL ) {
328         return( -1 );
329     }
330     if ( chdir( ".AppleDesktop" ) < 0 ) {
331         return( -1 );
332     }
333     if (( desk = opendir( "." )) == NULL ) {
334         if ( chdir( wd ) < 0 ) {
335             LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
336         }
337         return( -1 );
338     }
339     for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
340         if ( strcmp( deskp->d_name, "." ) == 0 ||
341                 strcmp( deskp->d_name, ".." ) == 0 || strlen( deskp->d_name ) > 2 ) {
342             continue;
343         }
344         strcpy( modbuf, deskp->d_name );
345         strcat( modbuf, "/" );
346         m = strchr( modbuf, '\0' );
347         if (( sub = opendir( deskp->d_name )) == NULL ) {
348             continue;
349         }
350         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
351             if ( strcmp( subp->d_name, "." ) == 0 ||
352                     strcmp( subp->d_name, ".." ) == 0 ) {
353                 continue;
354             }
355             *m = '\0';
356             strcat( modbuf, subp->d_name );
357             /* XXX: need to preserve special modes */
358             if (stat(modbuf, &st) < 0) {
359                 LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
360                 continue;
361             }
362
363             if (S_ISDIR(st.st_mode)) {
364                 if ( chmod( modbuf,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
365                      LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
366                 }
367             } else if ( chmod( modbuf,  mode & ~(default_options.umask | EXEC_MODE) ) < 0 && errno != EPERM ) {
368                 LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
369             }
370
371         }
372         closedir( sub );
373         /* XXX: need to preserve special modes */
374         if ( chmod( deskp->d_name,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
375             LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
376         }
377     }
378     closedir( desk );
379     if ( chdir( wd ) < 0 ) {
380         LOG(log_error, logtype_afpd, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
381         return -1;
382     }
383     /* XXX: need to preserve special modes */
384     if ( chmod( ".AppleDesktop",  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
385         LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
386     }
387     return( 0 );
388 }
389
390 /* --------------------- */
391 int setfilunixmode (vol, path, mode)
392 const struct vol *vol;
393 struct path* path;
394 mode_t mode;
395 {
396     if (!path->st_valid) {
397         of_stat(path);
398     }
399
400     if (path->st_errno) {
401         return -1;
402     }
403         
404     if (setfilmode( path->u_name, mode, &path->st) < 0)
405         return -1;
406     /* we need to set write perm if read set for resource fork */
407     return setfilmode(vol->ad_path( path->u_name, ADFLAGS_HF ), ad_hf_mode(mode), &path->st);
408 }
409
410 /* --------------------- */
411 int setfilmode(name, mode, st)
412 char * name;
413 mode_t mode;
414 struct stat *st;
415 {
416 struct stat sb;
417 mode_t mask = S_IRUSR |S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH;
418
419     if (!st) {
420         if (stat(name, &sb) != 0)
421             return -1;
422         st = &sb;
423     }
424    mode &= mask;        /* keep only rw-rw-rw in mode */
425    mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
426    if ( chmod( name,  mode & ~default_options.umask ) < 0 && errno != EPERM ) {
427        return -1;
428    }
429    return 0;
430 }
431
432 /* --------------------- */
433 int setdirunixmode( vol, name, mode )
434 const struct vol *vol;
435 const char       *name;
436 const mode_t     mode;
437 {
438 char *adouble = vol->ad_path( name, ADFLAGS_DIR );
439
440     int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
441
442     if (dir_rx_set(mode)) {
443         /* extending right? dir first then .AppleDouble */
444         if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
445                 return -1;
446         if (vol->v_adouble != AD_VERSION2_OSX) {
447             if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
448                 return  -1 ;
449             }
450         }
451     }
452     if (setfilmode(adouble, ad_hf_mode(mode), NULL) < 0 && !vol_noadouble(vol)) {
453         return  -1 ;
454     }
455     if (!dir_rx_set(mode)) {
456         if (vol->v_adouble != AD_VERSION2_OSX) {
457             if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
458                 return  -1 ;
459             }
460         }
461         if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
462             return -1;
463     }
464     return 0;
465 }
466
467 /* --------------------- */
468 int setdirmode( vol, name, mode )
469 const struct vol *vol;
470 const char       *name;
471 const mode_t mode;
472 {
473     char                buf[ MAXPATHLEN + 1];
474     struct stat         st;
475     char                *m;
476     struct dirent       *dirp;
477     DIR                 *dir;
478     int                 osx = vol->v_adouble == AD_VERSION2_OSX;
479     int                 hf_mode = ad_hf_mode(mode);
480     int                 dropbox = (vol->v_flags & AFPVOL_DROPBOX);
481     char                *adouble = vol->ad_path( name, ADFLAGS_DIR );
482     char                *adouble_p = ad_dir(adouble);
483     
484     if (dir_rx_set(mode)) {
485         /* extending right? dir first then .AppleDouble */
486         if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
487                 return -1;
488         if (!osx) {
489             if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
490                 return  -1 ;
491             }
492         }
493     }
494     
495     if (( dir = opendir( name )) == NULL ) {
496         LOG(log_error, logtype_afpd, "setdirmode: opendir: %s", fullpathname(name), strerror(errno) );
497         return( -1 );
498     }
499
500     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
501         /* FIXME */
502         if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
503             continue;
504         }
505         if ( stat( dirp->d_name, &st ) < 0 ) {
506             LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s",dirp->d_name, strerror(errno) );
507             continue;
508         }
509
510         if (!S_ISDIR(st.st_mode)) {
511            int setmode = (osx && *dirp->d_name == '.')?hf_mode:mode;
512
513            if (setfilmode(dirp->d_name, setmode, &st) < 0) {
514                 LOG(log_error, logtype_afpd, "setdirmode: chmod %s: %s",dirp->d_name, strerror(errno) );
515                 return -1;
516            }
517         }
518 #if 0
519         /* Don't change subdir perm */
520         else if (S_ISDIR(st.st_mode)) {
521                 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
522                     return (-1);
523             } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
524                 return (-1);
525         }
526 #endif
527     }
528     closedir( dir );
529     
530     if (osx) {
531         goto setdirmode_noadouble;
532     }
533     
534     /* change perm of .AppleDouble's files
535     */
536     if (( dir = opendir( adouble_p )) == NULL ) {
537         if (vol_noadouble(vol))
538             goto setdirmode_noadouble;
539         LOG(log_error, logtype_afpd, "setdirmode: opendir %s: %s", fullpathname(".AppleDouble"),strerror(errno) );
540         return( -1 );
541     }
542     strcpy( buf, adouble_p);
543     strcat( buf, "/" );
544     m = strchr( buf, '\0' );
545     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
546         if ( strcmp( dirp->d_name, "." ) == 0 ||
547                 strcmp( dirp->d_name, ".." ) == 0 ) {
548             continue;
549         }
550         *m = '\0';
551         strcat( buf, dirp->d_name );
552
553         if ( stat( buf, &st ) < 0 ) {
554             LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
555             continue;
556         }
557         if (!S_ISDIR(st.st_mode)) {
558            if (setfilmode(buf, hf_mode , &st) < 0) {
559                /* FIXME what do we do then? */
560            }
561         }
562     } /* end for */
563     closedir( dir );
564
565     if (!dir_rx_set(mode)) {
566         /* XXX: need to preserve special modes */
567         if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 ) {
568                 return  -1 ;
569         }
570     }
571
572 setdirmode_noadouble:
573     if (!dir_rx_set(mode)) {
574         if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
575                 return -1;
576     }
577     return( 0 );
578 }
579
580 int setdeskowner( uid, gid )
581 const uid_t     uid;
582 const gid_t     gid;
583 {
584     char                wd[ MAXPATHLEN + 1];
585     char                modbuf[12 + 1], *m;
586     struct dirent       *deskp, *subp;
587     DIR                 *desk, *sub;
588
589     if ( getcwd( wd, MAXPATHLEN ) == NULL ) {
590         return( -1 );
591     }
592     if ( chdir( ".AppleDesktop" ) < 0 ) {
593         return( -1 );
594     }
595     if (( desk = opendir( "." )) == NULL ) {
596         if ( chdir( wd ) < 0 ) {
597             LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
598         }
599         return( -1 );
600     }
601     for ( deskp = readdir( desk ); deskp != NULL; deskp = readdir( desk )) {
602         if ( strcmp( deskp->d_name, "." ) == 0 ||
603                 strcmp( deskp->d_name, ".." ) == 0 ||
604                 strlen( deskp->d_name ) > 2 ) {
605             continue;
606         }
607         strcpy( modbuf, deskp->d_name );
608         strcat( modbuf, "/" );
609         m = strchr( modbuf, '\0' );
610         if (( sub = opendir( deskp->d_name )) == NULL ) {
611             continue;
612         }
613         for ( subp = readdir( sub ); subp != NULL; subp = readdir( sub )) {
614             if ( strcmp( subp->d_name, "." ) == 0 ||
615                     strcmp( subp->d_name, ".." ) == 0 ) {
616                 continue;
617             }
618             *m = '\0';
619             strcat( modbuf, subp->d_name );
620             /* XXX: add special any uid, ignore group bits */
621             if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
622                 LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
623             }
624         }
625         closedir( sub );
626         /* XXX: add special any uid, ignore group bits */
627         if ( chown( deskp->d_name, uid, gid ) < 0 && errno != EPERM ) {
628             LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s",
629                 deskp->d_name, strerror(errno) );
630         }
631     }
632     closedir( desk );
633     if ( chdir( wd ) < 0 ) {
634         LOG(log_error, logtype_afpd, "setdeskowner: chdir %s: %s", wd, strerror(errno) );
635         return -1;
636     }
637     if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
638         LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
639     }
640     return( 0 );
641 }
642
643 /* ----------------------------- */
644 int setfilowner(vol, uid, gid, path)
645 const struct vol *vol;
646 const uid_t     uid;
647 const gid_t     gid;
648 struct path* path;
649 {
650     struct stat st;
651     char  *ad_p;
652
653     if (!path->st_valid) {
654         of_stat(path);
655     }
656
657     if (path->st_errno) {
658         return -1;
659     }
660
661     if ( chown( path->u_name, uid, gid ) < 0 && errno != EPERM ) {
662         LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
663             uid, gid, path->u_name, strerror(errno) );
664         return -1;
665     }
666
667     ad_p = vol->ad_path( path->u_name, ADFLAGS_HF );
668
669     if ( stat( ad_p, &st ) < 0 ) {
670         /* ignore */
671         return 0;
672     }
673     if ( chown( ad_p, uid, gid ) < 0 &&
674             errno != EPERM ) {
675         LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
676             uid, gid, ad_p, strerror(errno) );
677         return -1;
678     }
679     return 0;
680 }
681
682
683 /* --------------------------------- 
684  * uid/gid == 0 need to be handled as special cases. they really mean
685  * that user/group should inherit from other, but that doesn't fit
686  * into the unix permission scheme. we can get around this by
687  * co-opting some bits. */
688 int setdirowner(vol, name, uid, gid )
689 const struct vol *vol;
690 const char      *name;
691 const uid_t     uid;
692 const gid_t     gid;
693 {
694     char                buf[ MAXPATHLEN + 1];
695     struct stat         st;
696     char                *m;
697     struct dirent       *dirp;
698     DIR                 *dir;
699     int                 osx = vol->v_adouble == AD_VERSION2_OSX;
700     int                 noadouble = vol_noadouble(vol);
701     char                *adouble; 
702     char                *adouble_p;
703
704     if (( dir = opendir( name )) == NULL ) {
705         return( -1 );
706     }
707     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
708         if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
709             continue;
710         }
711         if ( stat( dirp->d_name, &st ) < 0 ) {
712             LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s",
713                 fullpathname(dirp->d_name), strerror(errno) );
714             continue;
715         }
716         if (( st.st_mode & S_IFMT ) == S_IFREG ) {
717             if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
718                 LOG(log_debug, logtype_afpd, "setdirowner: chown %s: %s",
719                     fullpathname(dirp->d_name), strerror(errno) );
720                 /* return ( -1 ); Sometimes this is okay */
721             }
722         }
723     }
724     closedir( dir );
725     
726     if (osx) {
727        goto setdirowner_noadouble;
728     }
729
730     adouble = vol->ad_path( name, ADFLAGS_DIR );
731     adouble_p = ad_dir(adouble);
732     if (( dir = opendir( adouble_p )) == NULL ) {
733         if (noadouble)
734             goto setdirowner_noadouble;
735         return( -1 );
736     }
737     strcpy( buf, adouble_p );
738     strcat( buf, "/" );
739     m = strchr( buf, '\0' );
740     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
741         if ( strcmp( dirp->d_name, "." ) == 0 ||
742                 strcmp( dirp->d_name, ".." ) == 0 ) {
743             continue;
744         }
745         *m = '\0';
746         strcat( buf, dirp->d_name );
747         if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
748             LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
749                 uid, gid, fullpathname(buf), strerror(errno) );
750             /* return ( -1 ); Sometimes this is okay */
751         }
752     }
753     closedir( dir );
754
755     /*
756      * We cheat: we know that chown doesn't do anything.
757      */
758     if ( stat( ".AppleDouble", &st ) < 0 ) {
759         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
760         return( -1 );
761     }
762     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
763             errno != EPERM ) {
764         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
765             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
766         /* return ( -1 ); Sometimes this is okay */
767     }
768
769 setdirowner_noadouble:
770     if ( stat( ".", &st ) < 0 ) {
771         return( -1 );
772     }
773     if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
774             errno != EPERM ) {
775         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
776             uid, gid, fullpathname("."), strerror(errno) );
777     }
778
779     return( 0 );
780 }
781
782 #if 0
783 /* recursive chown()ing of a directory */
784 static int recursive_chown(const char *path, uid_t uid, gid_t gid) {
785     struct stat sbuf;
786     DIR *odir = NULL;
787     struct dirent *entry;
788     char *name;
789     int ret = 0;
790     char newpath[PATH_MAX+1];
791     newpath[PATH_MAX] = '\0';
792     
793     if (chown(path, uid, gid) < 0) {
794         LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
795         return -1;
796     }
797
798     if (stat(path, &sbuf) < 0) {
799         LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
800         return -1;
801     }
802         
803     if (S_ISDIR(sbuf.st_mode)) {
804         odir = opendir(path);
805         if (odir == NULL) {
806             LOG(log_error, logtype_afpd, "cannot opendir() [%s] (uid = %d): %s\n", path, uid, strerror(errno));
807             goto recursive_chown_end;
808         }
809         while (NULL != (entry=readdir(odir)) ) {
810             name = entry->d_name;
811             if (name[0] == '.' && name[1] == '\0')
812                 continue;
813             if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
814                 continue;
815             sprintf(newpath, "%s/%s", path, name);
816             if (recursive_chown(newpath, uid, gid) < 0)
817                 ret = -1;
818         } /* while */
819     } /* if */
820
821 recursive_chown_end:
822     if (odir != NULL) {
823         closedir(odir);
824     }
825     return ret;
826 }
827 #endif
828
829 /* This is equivalent of unix rename(). */
830 int unix_rename(const char *oldpath, const char *newpath)
831 {
832 #if 0
833         char pd_name[PATH_MAX+1];
834         int i;
835         struct stat pd_stat;
836         uid_t uid;
837 #endif
838
839         if (rename(oldpath, newpath) < 0)
840                 return -1;
841 #if 0
842         for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
843                 pd_name[i] = newpath[i];
844         pd_name[i] = '\0';
845
846         while (i > 0 && pd_name[i] != '/') i--;
847         if (pd_name[i] == '/') i++;
848
849         pd_name[i++] = '.'; pd_name[i++] = '\0';
850
851         if (stat(pd_name, &pd_stat) < 0) {
852             LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s\n",
853                 pd_name, geteuid(), strerror(errno));
854                 return 0;
855         }
856
857         /* So we have SGID bit set... */
858         if ((S_ISGID & pd_stat.st_mode) != 0) {
859             uid = geteuid();
860             if (seteuid(0) < 0)
861                 LOG(log_error, logtype_afpd, "seteuid() failed: %s\n", strerror(errno));
862             if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
863                 LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s\n",
864                     pd_name, geteuid(), strerror(errno));
865             seteuid(uid);
866         }
867 #endif
868         return 0;
869 }
870