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