]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/vfs.c
Dont set owner of directory entries in setdirowner
[netatalk.git] / libatalk / vfs / vfs.c
1 /*
2     Copyright (c) 2004 Didier Gautheron
3     Copyright (c) 2009 Frank Lahm
4  
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9  
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <libgen.h>
31
32 #include <atalk/afp.h>    
33 #include <atalk/adouble.h>
34 #include <atalk/ea.h>
35 #include <atalk/acl.h>
36 #include <atalk/logger.h>
37 #include <atalk/util.h>
38 #include <atalk/volume.h>
39 #include <atalk/vfs.h>
40 #include <atalk/directory.h>
41 #include <atalk/unix.h>
42 #include <atalk/errchk.h>
43 #include <atalk/bstrlib.h>
44 #include <atalk/bstradd.h>
45 #include <atalk/compat.h>
46
47 struct perm {
48     uid_t uid;
49     gid_t gid;
50 };
51
52 typedef int (*rf_loop)(struct dirent *, char *, void *, int , mode_t );
53
54 /* ----------------------------- */
55 static int 
56 for_each_adouble(const char *from, const char *name, rf_loop fn, void *data, int flag, mode_t v_umask)
57 {
58     char            buf[ MAXPATHLEN + 1];
59     char            *m;
60     DIR             *dp;
61     struct dirent   *de;
62     int             ret;
63     
64
65     if (NULL == ( dp = opendir( name)) ) {
66         if (!flag) {
67             LOG(log_error, logtype_afpd, "%s: opendir %s: %s", from, fullpathname(name),strerror(errno) );
68             return -1;
69         }
70         return 0;
71     }
72     strlcpy( buf, name, sizeof(buf));
73     strlcat( buf, "/", sizeof(buf) );
74     m = strchr( buf, '\0' );
75     ret = 0;
76     while ((de = readdir(dp))) {
77         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
78                 continue;
79         }
80         
81         strlcat(buf, de->d_name, sizeof(buf));
82         if (fn && (ret = fn(de, buf, data, flag, v_umask))) {
83            closedir(dp);
84            return ret;
85         }
86         *m = 0;
87     }
88     closedir(dp);
89     return ret;
90 }
91
92 /*******************************************************************************
93  * classic adouble format 
94  *******************************************************************************/
95
96 static int netatalk_name(const char *name)
97 {
98     return strcasecmp(name,".AppleDouble") &&
99         strcasecmp(name,".AppleDB") &&
100         strcasecmp(name,".AppleDesktop");
101 }
102
103 static int validupath_adouble(VFS_FUNC_ARGS_VALIDUPATH)
104 {
105     if (name[0] != '.')
106         return 1;
107     
108     if (!(vol->v_flags & AFPVOL_USEDOTS))
109         return 0;
110         
111     return netatalk_name(name) && strcasecmp(name,".Parent");
112 }                                           
113
114 /* ----------------- */
115 static int RF_chown_adouble(VFS_FUNC_ARGS_CHOWN)
116 {
117     struct stat st;
118     const char *ad_p;
119
120     ad_p = vol->ad_path(path, ADFLAGS_HF );
121
122     if ( stat( ad_p, &st ) < 0 )
123         return 0; /* ignore */
124
125     return chown( ad_p, uid, gid );
126 }
127
128 /* ----------------- */
129 static int RF_renamedir_adouble(VFS_FUNC_ARGS_RENAMEDIR)
130 {
131     return 0;
132 }
133
134 /* ----------------- */
135 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask)
136 {
137     struct stat st;
138     int         err;
139     
140     /* bail if the file exists in the current directory.
141      * note: this will not fail with dangling symlinks */
142     
143     if (lstat(de->d_name, &st) == 0)
144         return AFPERR_DIRNEMPT;
145
146     if ((err = netatalk_unlink(name)))
147         return err;
148
149     return 0;
150 }
151
152 static int RF_deletecurdir_adouble(VFS_FUNC_ARGS_DELETECURDIR)
153 {
154     int err;
155
156     /* delete stray .AppleDouble files. this happens to get .Parent files
157        as well. */
158     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1, vol->v_umask))) 
159         return err;
160     return netatalk_rmdir(-1, ".AppleDouble" );
161 }
162
163 /* ----------------- */
164 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
165 {
166     return setfilmode(name, ad_hf_mode(mode), st, v_umask);
167 }
168
169 static int RF_setfilmode_adouble(VFS_FUNC_ARGS_SETFILEMODE)
170 {
171     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_HF ), mode, st, vol->v_umask);
172 }
173
174 /* ----------------- */
175 static int RF_setdirunixmode_adouble(VFS_FUNC_ARGS_SETDIRUNIXMODE)
176 {
177     const char *adouble = vol->ad_path(name, ADFLAGS_DIR );
178     int  dropbox = vol->v_flags;
179
180     if (dir_rx_set(mode)) {
181         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0 ) 
182             return -1;
183     }
184
185     if (adouble_setfilmode(vol->ad_path(name, ADFLAGS_DIR ), mode, st, vol->v_umask) < 0) 
186         return -1;
187
188     if (!dir_rx_set(mode)) {
189         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0 ) 
190             return  -1 ;
191     }
192     return 0;
193 }
194
195 /* ----------------- */
196 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag, mode_t v_umask)
197 {
198     mode_t hf_mode = *(mode_t *)data;
199     struct stat st;
200
201     if ( stat( name, &st ) < 0 ) {
202         if (flag)
203             return 0;
204         LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
205     }
206     else if (!S_ISDIR(st.st_mode)) {
207         if (setfilmode(name, hf_mode , &st, v_umask) < 0) {
208                /* FIXME what do we do then? */
209         }
210     }
211     return 0;
212 }
213
214 static int RF_setdirmode_adouble(VFS_FUNC_ARGS_SETDIRMODE)
215 {
216     int   dropbox = vol->v_flags;
217     mode_t hf_mode = ad_hf_mode(mode);
218     const char  *adouble = vol->ad_path(name, ADFLAGS_DIR );
219     const char  *adouble_p = ad_dir(adouble);
220
221     if (dir_rx_set(mode)) {
222         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
223             return -1;
224     }
225
226     if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, 0, vol->v_umask))
227         return -1;
228
229     if (!dir_rx_set(mode)) {
230         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
231             return  -1 ;
232     }
233     return 0;
234 }
235
236 static int RF_setdirowner_adouble(VFS_FUNC_ARGS_SETDIROWNER)
237 {
238     if (lchown(".AppleDouble", uid, gid) < 0 && errno != EPERM ) {
239         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
240             uid, gid,fullpathname(".AppleDouble"), strerror(errno));
241     }
242     return 0;
243 }
244
245 /* ----------------- */
246 static int RF_deletefile_adouble(VFS_FUNC_ARGS_DELETEFILE)
247 {
248         return netatalk_unlinkat(dirfd, vol->ad_path(file, ADFLAGS_HF));
249 }
250
251 /* ----------------- */
252 static int RF_renamefile_adouble(VFS_FUNC_ARGS_RENAMEFILE)
253 {
254     char  adsrc[ MAXPATHLEN + 1];
255     int   err = 0;
256
257     strcpy( adsrc, vol->ad_path(src, 0 ));
258     if (unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) < 0) {
259         struct stat st;
260
261         err = errno;
262         if (errno == ENOENT) {
263                 struct adouble    ad;
264
265             if (lstatat(dirfd, adsrc, &st)) /* source has no ressource fork, */
266                 return 0;
267
268             /* We are here  because :
269              * -there's no dest folder. 
270              * -there's no .AppleDouble in the dest folder.
271              * if we use the struct adouble passed in parameter it will not
272              * create .AppleDouble if the file is already opened, so we
273              * use a diff one, it's not a pb,ie it's not the same file, yet.
274              */
275             ad_init(&ad, vol); 
276             if (ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) == 0) {
277                 ad_close(&ad, ADFLAGS_HF);
278                 if (!unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) ) 
279                    err = 0;
280                 else 
281                    err = errno;
282             }
283             else { /* it's something else, bail out */
284                     err = errno;
285                 }
286             }
287         }
288         if (err) {
289                 errno = err;
290                 return -1;
291         }
292         return 0;
293 }
294
295 static int RF_copyfile_adouble(VFS_FUNC_ARGS_COPYFILE)
296 /* const struct vol *vol, int sfd, const char *src, const char *dst */
297 {
298     return 0;
299 #if 0
300     EC_INIT;
301     bstring s = NULL, d = NULL;
302     char *dup1 = NULL;
303     char *dup2 = NULL;
304     char *dup3 = NULL;
305     char *dup4 = NULL;
306     const char *name = NULL;
307     const char *dir = NULL;
308
309     struct stat st;
310     EC_ZERO(stat(dst, &st));
311
312     if (S_ISDIR(st.st_mode)) {
313         /* build src path to AppleDouble file*/
314         EC_NULL(s = bfromcstr(src));
315         EC_ZERO(bcatcstr(s, "/.AppleDouble/.Parent"));
316
317         /* build dst path to AppleDouble file*/
318         EC_NULL(d = bfromcstr(dst));
319         EC_ZERO(bcatcstr(d, "/.AppleDouble/.Parent"));
320     } else {
321         /* get basename */
322
323         /* build src path to AppleDouble file*/
324         EC_NULL(dup1 = strdup(src));
325         EC_NULL(name = basename(strdup(dup1)));
326
327         EC_NULL(dup2 = strdup(src));
328         EC_NULL(dir = dirname(dup2));
329         EC_NULL(s = bfromcstr(dir));
330         EC_ZERO(bcatcstr(s, "/.AppleDouble/"));
331         EC_ZERO(bcatcstr(s, name));
332
333         /* build dst path to AppleDouble file*/
334         EC_NULL(dup4 = strdup(dst));
335         EC_NULL(name = basename(strdup(dup4)));
336
337         EC_NULL(dup3 = strdup(dst));
338         EC_NULL(dir = dirname(dup3));
339         EC_NULL(d = bfromcstr(dir));
340         EC_ZERO(bcatcstr(d, "/.AppleDouble/"));
341         EC_ZERO(bcatcstr(d, name));
342     }
343
344     EC_ZERO(copy_file(sfd, cfrombstr(s), cfrombstr(d), 0666));
345
346 EC_CLEANUP:
347     bdestroy(s);
348     bdestroy(d);
349     if (dup1) free(dup1);
350     if (dup2) free(dup2);
351     if (dup3) free(dup3);
352     if (dup4) free(dup4);
353
354     EC_EXIT;
355 #endif
356 }
357
358 #ifdef HAVE_SOLARIS_ACLS
359 static int RF_solaris_acl(VFS_FUNC_ARGS_ACL)
360 {
361     static char buf[ MAXPATHLEN + 1];
362     struct stat st;
363     int len;
364
365     if ((stat(path, &st)) != 0)
366         return -1;
367     if (S_ISDIR(st.st_mode)) {
368         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
369         if (len < 0 || len >=  MAXPATHLEN)
370             return -1;
371         /* set acl on .AppleDouble dir first */
372         if ((acl(buf, cmd, count, aces)) != 0)
373             return -1;
374         /* now set ACL on ressource fork */
375         if ((acl(vol->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
376             return -1;
377     } else
378         /* set ACL on ressource fork */
379         if ((acl(vol->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
380             return -1;
381
382     return 0;
383 }
384
385 static int RF_solaris_remove_acl(VFS_FUNC_ARGS_REMOVE_ACL)
386 {
387     int ret;
388     static char buf[ MAXPATHLEN + 1];
389     int len;
390
391     if (dir) {
392         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
393         if (len < 0 || len >=  MAXPATHLEN)
394             return AFPERR_MISC;
395         /* remove ACL from .AppleDouble/.Parent first */
396         if ((ret = remove_acl_vfs(vol->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
397             return ret;
398         /* now remove from .AppleDouble dir */
399         if ((ret = remove_acl_vfs(buf)) != AFP_OK)
400             return ret;
401     } else
402         /* remove ACL from ressource fork */
403         if ((ret = remove_acl_vfs(vol->ad_path(path, ADFLAGS_HF))) != AFP_OK)
404             return ret;
405
406     return AFP_OK;
407 }
408 #endif
409
410 #ifdef HAVE_POSIX_ACLS
411 static int RF_posix_acl(VFS_FUNC_ARGS_ACL)
412 {
413     EC_INIT;
414     static char buf[ MAXPATHLEN + 1];
415     struct stat st;
416     int len;
417
418     if (S_ISDIR(st.st_mode)) {
419         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
420         if (len < 0 || len >=  MAXPATHLEN)
421             EC_FAIL;
422         /* set acl on .AppleDouble dir first */
423         EC_ZERO_LOG(acl_set_file(buf, type, acl));
424
425         if (type == ACL_TYPE_ACCESS)
426             /* set ACL on ressource fork (".Parent") too */
427             EC_ZERO_LOG(acl_set_file(vol->ad_path(path, ADFLAGS_DIR), type, acl));
428     } else {
429         /* set ACL on ressource fork */
430         EC_ZERO_LOG(acl_set_file(vol->ad_path(path, ADFLAGS_HF), type, acl));
431     }
432     
433 EC_CLEANUP:
434     if (ret != 0)
435         return AFPERR_MISC;
436     return AFP_OK;
437 }
438
439 static int RF_posix_remove_acl(VFS_FUNC_ARGS_REMOVE_ACL)
440 {
441     EC_INIT;
442     static char buf[ MAXPATHLEN + 1];
443     int len;
444
445     if (dir) {
446         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
447         if (len < 0 || len >=  MAXPATHLEN)
448             return AFPERR_MISC;
449         /* remove ACL from .AppleDouble/.Parent first */
450         EC_ZERO_LOG_ERR(remove_acl_vfs(vol->ad_path(path, ADFLAGS_DIR)), AFPERR_MISC);
451
452         /* now remove from .AppleDouble dir */
453         EC_ZERO_LOG_ERR(remove_acl_vfs(buf), AFPERR_MISC);
454     } else {
455         /* remove ACL from ressource fork */
456         EC_ZERO_LOG_ERR(remove_acl_vfs(vol->ad_path(path, ADFLAGS_HF)), AFPERR_MISC);
457     }
458
459 EC_CLEANUP:
460     EC_EXIT;
461 }
462 #endif
463
464 /*************************************************************************
465  * EA adouble format 
466  ************************************************************************/
467 static int validupath_ea(VFS_FUNC_ARGS_VALIDUPATH)
468 {
469     if (name[0] != '.')
470         return 1;
471     
472     if (!(vol->v_flags & AFPVOL_USEDOTS))
473         return 0;
474
475 #ifndef HAVE_EAFD
476     if (name[1] == '_')
477         return 0;
478 #endif
479     return netatalk_name(name) && strcasecmp(name,".Parent");
480 }             
481
482 /* ----------------- */
483 static int RF_chown_ea(VFS_FUNC_ARGS_CHOWN)
484 {
485 #ifndef HAVE_EAFD
486     return chown(vol->ad_path(path, ADFLAGS_HF ), uid, gid);
487 #endif
488     return 0;
489 }
490
491 /* ---------------- */
492 static int RF_renamedir_ea(VFS_FUNC_ARGS_RENAMEDIR)
493 {
494     return 0;
495 }
496
497 /* Returns 1 if the entry is NOT an ._ file */
498 static int deletecurdir_ea_osx_chkifempty_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
499 {
500     if (de->d_name[0] != '.' || de->d_name[0] == '_')
501         return 1;
502
503     return 0;
504 }
505
506 static int deletecurdir_ea_osx_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
507 {
508     int ret;
509     
510     if ((ret = netatalk_unlink(name)) != 0)
511         return ret;
512
513     return 0;
514 }
515
516 /* ---------------- */
517 static int RF_deletecurdir_ea(VFS_FUNC_ARGS_DELETECURDIR)
518 {
519 #ifndef HAVE_EAFD
520     int err;
521     /* delete stray ._AppleDouble files */
522
523     /* first check if there's really no other file besides files starting with ._ */
524     if ((err = for_each_adouble("deletecurdir_ea_osx", ".",
525                                 deletecurdir_ea_osx_chkifempty_loop,
526                                 NULL, 0, 0)) != 0) {
527         if (err == 1)
528             return AFPERR_DIRNEMPT;
529         return AFPERR_MISC;
530     }
531
532     /* Now delete orphaned ._ files */
533     if ((err = for_each_adouble("deletecurdir_ea_osx", ".",
534                                 deletecurdir_ea_osx_loop,
535                                 NULL, 0, 0)) != 0)
536         return err;
537
538 #endif
539     return 0;
540 }
541
542 /* ---------------- */
543 static int RF_setdirunixmode_ea(VFS_FUNC_ARGS_SETDIRUNIXMODE)
544 {
545 #ifndef HAVE_EAFD
546 #endif
547     return 0;
548 }
549
550 static int RF_setfilmode_ea(VFS_FUNC_ARGS_SETFILEMODE)
551 {
552 #ifndef HAVE_EAFD
553     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_HF ), mode, st, vol->v_umask);
554 #endif
555     return 0;
556 }
557
558 /* ---------------- */
559 static int RF_setdirmode_ea(VFS_FUNC_ARGS_SETDIRMODE)
560 {
561 #ifndef HAVE_EAFD
562 #endif
563     return 0;
564 }
565
566 /* ---------------- */
567 static int RF_setdirowner_ea(VFS_FUNC_ARGS_SETDIROWNER)
568 {
569 #ifndef HAVE_EAFD
570 #endif
571         return 0;
572 }
573
574 static int RF_deletefile_ea(VFS_FUNC_ARGS_DELETEFILE)
575 {
576 #ifndef HAVE_EAFD
577         return netatalk_unlinkat(dirfd, vol->ad_path(file, ADFLAGS_HF));
578 #endif
579     return 0;
580 }
581 static int RF_copyfile_ea(VFS_FUNC_ARGS_COPYFILE)
582 {
583     return 0;
584 #if 0
585     EC_INIT;
586
587     /* copy meta EA */
588     if (copy_ea(AD_EA_META, sfd, src, dst, 0666) != 0)
589         return AFPERR_MISC;
590
591     /* copy reso */
592 #ifdef HAVE_EAFD
593     int sfile = -1, dfile = -1, sea = -1, dea = -1;
594
595     if ((sfile = openat(sfd, src, O_RDONLY)) == -1) {
596         ret = AFPERR_MISC;
597         goto copyresoerr;
598     }
599
600     if ((dfile = open(dts, O_WRONLY)) == -1) {
601         ret = AFPERR_MISC;
602         goto copyresoerr;
603     }
604
605     if ((sea = openat(sfile, AD_EA_RESO, O_RDONLY | O_XATTR)) == -1) {
606         ret = AFPERR_MISC;
607         goto copyresoerr;
608     }
609
610     if ((dea = openat(dfile, AD_EA_RESO, O_RDWR | O_CREAT | O_XATTR)) == -1) {
611         ret = AFPERR_MISC;
612         goto copyresoerr;
613     }
614
615     ret = copy_file_fd(sea, dea);
616
617 copyresoerr:
618     if (sfile != -1) close(sfile);
619     if (dfile != -1) close(dfile);
620     if (sea != -1) close(sea);
621     if (dea != -1) close(dea);
622     if (ret != 0)
623         return ret;
624
625 EC_CLEANUP:
626 #else
627     bstring s = NULL, d = NULL;
628     char *dup1 = NULL;
629     char *dup2 = NULL;
630     char *dup3 = NULL;
631     char *dup4 = NULL;
632     const char *name = NULL;
633     const char *dir = NULL;
634
635     /* get basename */
636
637     /* build src path to ._ file*/
638     EC_NULL(dup1 = strdup(src));
639     EC_NULL(name = basename(strdup(dup1)));
640
641     EC_NULL(dup2 = strdup(src));
642     EC_NULL(dir = dirname(dup2));
643     EC_NULL(s = bfromcstr(dir));
644     EC_ZERO(bcatcstr(s, "/._"));
645     EC_ZERO(bcatcstr(s, name));
646
647     /* build dst path to ._file*/
648     EC_NULL(dup4 = strdup(dst));
649     EC_NULL(name = basename(strdup(dup4)));
650
651     EC_NULL(dup3 = strdup(dst));
652     EC_NULL(dir = dirname(dup3));
653     EC_NULL(d = bfromcstr(dir));
654     EC_ZERO(bcatcstr(d, "/._"));
655     EC_ZERO(bcatcstr(d, name));
656
657     EC_ZERO(copy_file(sfd, cfrombstr(s), cfrombstr(d), 0666));
658
659 EC_CLEANUP:
660     bdestroy(s);
661     bdestroy(d);
662     free(dup1);
663     free(dup2);
664     free(dup3);
665     free(dup4);
666 #endif
667 out:
668     EC_EXIT;
669 #endif
670 }
671
672 /* ---------------- */
673 static int RF_renamefile_ea(VFS_FUNC_ARGS_RENAMEFILE)
674 {
675 #ifndef HAVE_EAFD
676     char  adsrc[ MAXPATHLEN + 1];
677     int   err = 0;
678
679     strcpy( adsrc, vol->ad_path(src, 0 ));
680
681     if (unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) < 0) {
682         struct stat st;
683
684         err = errno;
685         if (errno == ENOENT && lstatat(dirfd, adsrc, &st)) /* source has no ressource fork, */
686             return 0;
687         errno = err;
688         return -1;
689     }
690     return 0;
691 #endif
692     return 0;
693 }
694
695 /********************************************************************************************
696  * VFS chaining
697  ********************************************************************************************/
698
699 /* 
700  * Up until we really start stacking many VFS modules on top of one another or use
701  * dynamic module loading like we do for UAMs, up until then we just stack VFS modules
702  * via an fixed size array.
703  * All VFS funcs must return AFP_ERR codes. When a func in the chain returns an error
704  * this error code will be returned to the caller, BUT the chain in followed and all
705  * following funcs are called in order to give them a chance.
706  */
707
708 /* 
709  * Define most VFS funcs with macros as they all do the same.
710  * Only "ad_path" and "validupath" will NOT do stacking and only
711  * call the func from the first module.
712  */
713
714 #define VFS_MFUNC(name, args, vars) \
715     static int vfs_ ## name(args) \
716     { \
717         int i = 0, ret = AFP_OK, err; \
718         while (vol->vfs_modules[i]) { \
719             if (vol->vfs_modules[i]->vfs_ ## name) { \
720                 err = vol->vfs_modules[i]->vfs_ ## name (vars); \
721                 if ((ret == AFP_OK) && (err != AFP_OK)) \
722                     ret = err; \
723             } \
724             i ++; \
725         } \
726         return ret; \
727     }
728
729 VFS_MFUNC(chown, VFS_FUNC_ARGS_CHOWN, VFS_FUNC_VARS_CHOWN)
730 VFS_MFUNC(renamedir, VFS_FUNC_ARGS_RENAMEDIR, VFS_FUNC_VARS_RENAMEDIR) 
731 VFS_MFUNC(deletecurdir, VFS_FUNC_ARGS_DELETECURDIR, VFS_FUNC_VARS_DELETECURDIR)
732 VFS_MFUNC(setfilmode, VFS_FUNC_ARGS_SETFILEMODE, VFS_FUNC_VARS_SETFILEMODE)
733 VFS_MFUNC(setdirmode, VFS_FUNC_ARGS_SETDIRMODE, VFS_FUNC_VARS_SETDIRMODE)
734 VFS_MFUNC(setdirunixmode, VFS_FUNC_ARGS_SETDIRUNIXMODE, VFS_FUNC_VARS_SETDIRUNIXMODE)
735 VFS_MFUNC(setdirowner, VFS_FUNC_ARGS_SETDIROWNER, VFS_FUNC_VARS_SETDIROWNER)
736 VFS_MFUNC(deletefile, VFS_FUNC_ARGS_DELETEFILE, VFS_FUNC_VARS_DELETEFILE)
737 VFS_MFUNC(renamefile, VFS_FUNC_ARGS_RENAMEFILE, VFS_FUNC_VARS_RENAMEFILE)
738 VFS_MFUNC(copyfile, VFS_FUNC_ARGS_COPYFILE, VFS_FUNC_VARS_COPYFILE)
739 #ifdef HAVE_ACLS
740 VFS_MFUNC(acl, VFS_FUNC_ARGS_ACL, VFS_FUNC_VARS_ACL)
741 VFS_MFUNC(remove_acl, VFS_FUNC_ARGS_REMOVE_ACL, VFS_FUNC_VARS_REMOVE_ACL)
742 #endif
743 VFS_MFUNC(ea_getsize, VFS_FUNC_ARGS_EA_GETSIZE, VFS_FUNC_VARS_EA_GETSIZE)
744 VFS_MFUNC(ea_getcontent, VFS_FUNC_ARGS_EA_GETCONTENT, VFS_FUNC_VARS_EA_GETCONTENT)
745 VFS_MFUNC(ea_list, VFS_FUNC_ARGS_EA_LIST, VFS_FUNC_VARS_EA_LIST)
746 VFS_MFUNC(ea_set, VFS_FUNC_ARGS_EA_SET, VFS_FUNC_VARS_EA_SET)
747 VFS_MFUNC(ea_remove, VFS_FUNC_ARGS_EA_REMOVE, VFS_FUNC_VARS_EA_REMOVE)
748
749 static int vfs_validupath(VFS_FUNC_ARGS_VALIDUPATH)
750 {
751     return vol->vfs_modules[0]->vfs_validupath(VFS_FUNC_VARS_VALIDUPATH);
752 }
753
754 /*
755  * These function pointers get called from the lib users via vol->vfs->func.
756  * These funcs are defined via the macros above.
757  */
758 static struct vfs_ops vfs_master_funcs = {
759     vfs_validupath,
760     vfs_chown,
761     vfs_renamedir,
762     vfs_deletecurdir,
763     vfs_setfilmode,
764     vfs_setdirmode,
765     vfs_setdirunixmode,
766     vfs_setdirowner,
767     vfs_deletefile,
768     vfs_renamefile,
769     vfs_copyfile,
770 #ifdef HAVE_ACLS
771     vfs_acl,
772     vfs_remove_acl,
773 #endif
774     vfs_ea_getsize,
775     vfs_ea_getcontent,
776     vfs_ea_list,
777     vfs_ea_set,
778     vfs_ea_remove
779 };
780
781 /* 
782  * Primary adouble modules: v2, ea
783  */
784
785 static struct vfs_ops netatalk_adouble_v2 = {
786     /* vfs_validupath:    */ validupath_adouble,
787     /* vfs_chown:         */ RF_chown_adouble,
788     /* vfs_renamedir:     */ RF_renamedir_adouble,
789     /* vfs_deletecurdir:  */ RF_deletecurdir_adouble,
790     /* vfs_setfilmode:    */ RF_setfilmode_adouble,
791     /* vfs_setdirmode:    */ RF_setdirmode_adouble,
792     /* vfs_setdirunixmode:*/ RF_setdirunixmode_adouble,
793     /* vfs_setdirowner:   */ RF_setdirowner_adouble,
794     /* vfs_deletefile:    */ RF_deletefile_adouble,
795     /* vfs_renamefile:    */ RF_renamefile_adouble,
796     /* vfs_copyfile:      */ RF_copyfile_adouble,
797     NULL
798 };
799
800 static struct vfs_ops netatalk_adouble_ea = {
801     /* vfs_validupath:    */ validupath_ea,
802     /* vfs_chown:         */ RF_chown_ea,
803     /* vfs_renamedir:     */ RF_renamedir_ea,
804     /* vfs_deletecurdir:  */ RF_deletecurdir_ea,
805     /* vfs_setfilmode:    */ RF_setfilmode_ea,
806     /* vfs_setdirmode:    */ RF_setdirmode_ea,
807     /* vfs_setdirunixmode:*/ RF_setdirunixmode_ea,
808     /* vfs_setdirowner:   */ RF_setdirowner_ea,
809     /* vfs_deletefile:    */ RF_deletefile_ea,
810     /* vfs_renamefile:    */ RF_renamefile_ea,
811     /* vfs_copyfile:      */ RF_copyfile_ea,
812     NULL
813 };
814
815 /* 
816  * Secondary vfs modules for Extended Attributes
817  */
818
819 static struct vfs_ops netatalk_ea_adouble = {
820     /* vfs_validupath:    */ NULL,
821     /* vfs_chown:         */ ea_chown,
822     /* vfs_renamedir:     */ NULL, /* ok */
823     /* vfs_deletecurdir:  */ NULL, /* ok */
824     /* vfs_setfilmode:    */ ea_chmod_file,
825     /* vfs_setdirmode:    */ NULL, /* ok */
826     /* vfs_setdirunixmode:*/ ea_chmod_dir,
827     /* vfs_setdirowner:   */ NULL, /* ok */
828     /* vfs_deletefile:    */ ea_deletefile,
829     /* vfs_renamefile:    */ ea_renamefile,
830     /* vfs_copyfile       */ ea_copyfile,
831 #ifdef HAVE_ACLS
832     /* vfs_acl:           */ NULL,
833     /* vfs_remove_acl     */ NULL,
834 #endif
835     /* vfs_getsize        */ get_easize,
836     /* vfs_getcontent     */ get_eacontent,
837     /* vfs_list           */ list_eas,
838     /* vfs_set            */ set_ea,
839     /* vfs_remove         */ remove_ea
840 };
841
842 static struct vfs_ops netatalk_ea_sys = {
843     /* validupath:        */ NULL,
844     /* rf_chown:          */ NULL,
845     /* rf_renamedir:      */ NULL,
846     /* rf_deletecurdir:   */ NULL,
847     /* rf_setfilmode:     */ NULL,
848     /* rf_setdirmode:     */ NULL,
849     /* rf_setdirunixmode: */ NULL,
850     /* rf_setdirowner:    */ NULL,
851     /* rf_deletefile:     */ NULL,
852     /* rf_renamefile:     */ NULL,
853     /* vfs_copyfile:      */ sys_ea_copyfile,
854 #ifdef HAVE_ACLS
855     /* rf_acl:            */ NULL,
856     /* rf_remove_acl      */ NULL,
857 #endif
858     /* ea_getsize         */ sys_get_easize,
859     /* ea_getcontent      */ sys_get_eacontent,
860     /* ea_list            */ sys_list_eas,
861     /* ea_set             */ sys_set_ea,
862     /* ea_remove          */ sys_remove_ea
863 };
864
865 /* 
866  * Tertiary VFS modules for ACLs
867  */
868
869 #ifdef HAVE_SOLARIS_ACLS
870 static struct vfs_ops netatalk_solaris_acl_adouble = {
871     /* validupath:        */ NULL,
872     /* rf_chown:          */ NULL,
873     /* rf_renamedir:      */ NULL,
874     /* rf_deletecurdir:   */ NULL,
875     /* rf_setfilmode:     */ NULL,
876     /* rf_setdirmode:     */ NULL,
877     /* rf_setdirunixmode: */ NULL,
878     /* rf_setdirowner:    */ NULL,
879     /* rf_deletefile:     */ NULL,
880     /* rf_renamefile:     */ NULL,
881     /* vfs_copyfile       */ NULL,
882     /* rf_acl:            */ RF_solaris_acl,
883     /* rf_remove_acl      */ RF_solaris_remove_acl,
884     NULL
885 };
886 #endif
887
888 #ifdef HAVE_POSIX_ACLS
889 static struct vfs_ops netatalk_posix_acl_adouble = {
890     /* validupath:        */ NULL,
891     /* rf_chown:          */ NULL,
892     /* rf_renamedir:      */ NULL,
893     /* rf_deletecurdir:   */ NULL,
894     /* rf_setfilmode:     */ NULL,
895     /* rf_setdirmode:     */ NULL,
896     /* rf_setdirunixmode: */ NULL,
897     /* rf_setdirowner:    */ NULL,
898     /* rf_deletefile:     */ NULL,
899     /* rf_renamefile:     */ NULL,
900     /* vfs_copyfile       */ NULL,
901     /* rf_acl:            */ RF_posix_acl,
902     /* rf_remove_acl      */ RF_posix_remove_acl,
903     NULL
904 };
905 #endif
906
907 /* ---------------- */
908 void initvol_vfs(struct vol *vol)
909 {
910     vol->vfs = &vfs_master_funcs;
911
912     /* Default adouble stuff */
913     if (vol->v_adouble == AD_VERSION2) {
914         vol->vfs_modules[0] = &netatalk_adouble_v2;
915         vol->ad_path = ad_path;
916     } else {
917         vol->vfs_modules[0] = &netatalk_adouble_ea;
918 #ifdef HAVE_EAFD
919         vol->ad_path = ad_path_ea;
920 #else
921         vol->ad_path = ad_path_osx;
922 #endif
923     }
924
925     /* Extended Attributes */
926     if (vol->v_vfs_ea == AFPVOL_EA_SYS) {
927         LOG(log_debug, logtype_afpd, "initvol_vfs: enabling EA support with native EAs");
928         vol->vfs_modules[1] = &netatalk_ea_sys;
929     } else if (vol->v_vfs_ea == AFPVOL_EA_AD) {
930         LOG(log_debug, logtype_afpd, "initvol_vfs: enabling EA support with adouble files");
931         vol->vfs_modules[1] = &netatalk_ea_adouble;
932     } else {
933         LOG(log_debug, logtype_afpd, "initvol_vfs: volume without EA support");
934     }
935
936     /* ACLs */
937 #ifdef HAVE_SOLARIS_ACLS
938     vol->vfs_modules[2] = &netatalk_solaris_acl_adouble;
939 #endif
940 #ifdef HAVE_POSIX_ACLS
941     vol->vfs_modules[2] = &netatalk_posix_acl_adouble;
942 #endif
943
944 }