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