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