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