2 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
3 * Copyright (c) 1990,1991 Regents of The University of Michigan.
4 * Copyright (c) 2010 Frank Lahm
8 * Permission to use, copy, modify, and distribute this software and
9 * its documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appears in all copies and
11 * that both that copyright notice and this permission notice appear
12 * in supporting documentation, and that the name of The University
13 * of Michigan not be used in advertising or publicity pertaining to
14 * distribution of the software without specific, written prior
15 * permission. This software is supplied as is without expressed or
16 * implied warranties of any kind.
18 * Research Systems Unix Group
19 * The University of Michigan
21 * 535 W. William Street
24 * netatalk@itd.umich.edu
30 * Part of Netatalk's AppleDouble implementatation
31 * @sa include/atalk/adouble.h
36 #endif /* HAVE_CONFIG_H */
39 #include <sys/param.h>
43 #include <arpa/inet.h>
45 #include <atalk/logger.h>
46 #include <atalk/adouble.h>
47 #include <atalk/util.h>
49 #include <atalk/bstrlib.h>
50 #include <atalk/bstradd.h>
51 #include <atalk/compat.h>
52 #include <atalk/errchk.h>
53 #include <atalk/volume.h>
57 #define ADEDOFF_MAGIC (0)
58 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
59 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
60 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
62 /* initial lengths of some of the fields */
63 #define ADEDLEN_INIT 0
65 /* i stick things in a slightly different order than their eid order in
66 * case i ever want to separate RootInfo behaviour from the rest of the
70 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
71 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
72 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
73 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
74 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
75 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
76 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
77 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
78 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
79 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
80 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
81 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
82 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
85 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
86 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
87 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
88 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
90 /* this is to prevent changing timezones from causing problems with
91 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
92 #define TIMEWARP_DELTA 157680000
95 uint32_t id, offset, len;
98 /* --------------------------- */
99 static uid_t default_uid = -1;
101 /* Forward declarations */
102 static int ad_mkrf(const char *path);
103 static int ad_header_read(const char *path, struct adouble *ad, struct stat *hst);
104 static int ad_header_upgrade(struct adouble *ad, const char *name);
106 static int ad_mkrf_ea(const char *path);
107 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst);
108 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
111 static const char *ad_path_osx(const char *path, int adflags);
112 static int ad_mkrf_osx(const char *path);
115 static struct adouble_fops ad_adouble = {
118 &ad_rebuild_adouble_header,
123 static struct adouble_fops ad_adouble_ea = {
131 &ad_rebuild_adouble_header,
133 &ad_header_upgrade_ea,
136 static struct adouble_fops ad_osx = {
139 &ad_rebuild_adouble_header,
144 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
145 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
146 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
147 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
148 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
149 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
150 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
151 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
152 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
153 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
154 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
155 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
156 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
157 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
161 /* Using Extended Attributes */
162 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
163 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
164 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
165 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
166 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
167 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
171 /* fallback for EAs */
172 static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
173 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
174 {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
178 #define ADFLAGS2LOGSTRBUFSIZ 128
179 const char *adflags2logstr(int adflags)
182 static char buf[ADFLAGS2LOGSTRBUFSIZ];
186 if (adflags & ADFLAGS_DF) {
187 strlcat(buf, "DF", ADFLAGS2LOGSTRBUFSIZ);
190 if (adflags & ADFLAGS_RF) {
192 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
193 strlcat(buf, "RF", ADFLAGS2LOGSTRBUFSIZ);
196 if (adflags & ADFLAGS_HF) {
198 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
199 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
202 if (adflags & ADFLAGS_NOHF) {
204 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
205 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
208 if (adflags & ADFLAGS_DIR) {
210 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
211 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
214 if (adflags & ADFLAGS_CHECK_OF) {
216 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
217 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
220 if (adflags & ADFLAGS_RDWR) {
222 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
223 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
226 if (adflags & ADFLAGS_RDONLY) {
228 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
229 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
232 if (adflags & ADFLAGS_CREATE) {
234 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
235 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
238 if (adflags & ADFLAGS_EXCL) {
240 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
241 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
244 if (adflags & ADFLAGS_TRUNC) {
246 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
247 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
254 static uint32_t get_eid(uint32_t eid)
259 return ADEID_PRIVDEV;
261 return ADEID_PRIVINO;
263 return ADEID_PRIVSYN;
270 /* ----------------------------------- */
271 static int new_ad_header(struct adouble *ad, const char *path, struct stat *stp, int adflags)
273 const struct entry *eid;
277 LOG(log_debug, logtype_default, "new_ad_header(\"%s\")", path);
281 if (lstat(path, &st) != 0)
285 ad->ad_magic = AD_MAGIC;
286 ad->ad_version = ad->ad_vers & 0x0f0000;
287 if (!ad->ad_version) {
288 ad->ad_version = AD_VERSION;
292 memset(ad->ad_data, 0, sizeof(ad->ad_data));
294 if (ad->ad_vers == AD_VERSION2)
296 else if (ad->ad_vers == AD_VERSION_EA)
297 eid = entry_order_ea;
298 else if (ad->ad_vers == AD_VERSION2_OSX)
299 eid = entry_order_osx;
305 ad->ad_eid[eid->id].ade_off = eid->offset;
306 ad->ad_eid[eid->id].ade_len = eid->len;
310 /* put something sane in the directory finderinfo */
311 if (ad->ad_vers != AD_VERSION2_OSX) {
312 if ((adflags & ADFLAGS_DIR)) {
313 /* set default view */
314 ashort = htons(FINDERINFO_CLOSEDVIEW);
315 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
317 /* set default creator/type fields */
318 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
319 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
322 /* make things invisible */
323 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
324 ashort = htons(ATTRBIT_INVISIBLE);
325 ad_setattr(ad, ashort);
326 ashort = htons(FINDERINFO_INVISIBLE);
327 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
330 /* put something sane in the date fields */
331 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, stp->st_mtime);
332 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, stp->st_mtime);
333 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, stp->st_mtime);
334 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
339 /* -------------------------------------
342 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
344 uint32_t eid, len, off;
347 /* now, read in the entry bits */
348 for (; nentries > 0; nentries-- ) {
349 memcpy(&eid, buf, sizeof( eid ));
350 eid = get_eid(ntohl(eid));
351 buf += sizeof( eid );
352 memcpy(&off, buf, sizeof( off ));
354 buf += sizeof( off );
355 memcpy(&len, buf, sizeof( len ));
357 buf += sizeof( len );
361 && off < sizeof(ad->ad_data)
362 && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
363 ad->ad_eid[ eid ].ade_off = off;
364 ad->ad_eid[ eid ].ade_len = len;
365 } else if (!warning) {
367 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
372 /* this reads enough of the header so that we can figure out all of
373 * the entry lengths and offsets. once that's done, we just read/mmap
374 * the rest of the header in.
376 * NOTE: we're assuming that the resource fork is kept at the end of
377 * the file. also, mmapping won't work for the hfs fs until it
378 * understands how to mmap header files. */
379 static int ad_header_read(const char *path _U_, struct adouble *ad, struct stat *hst)
381 char *buf = ad->ad_data;
387 /* read the header */
388 if ((header_len = adf_pread( ad->ad_mdp, buf, AD_DATASZ, 0)) < 0) {
391 if (header_len < AD_HEADER_LEN) {
396 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
397 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
398 ad->ad_magic = ntohl( ad->ad_magic );
399 ad->ad_version = ntohl( ad->ad_version );
401 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
402 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
407 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
408 nentries = ntohs( nentries );
410 /* read in all the entry headers. if we have more than the
411 * maximum, just hope that the rfork is specified early on. */
412 len = nentries*AD_ENTRY_LEN;
414 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
415 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
417 buf += AD_HEADER_LEN;
418 if (len > header_len - AD_HEADER_LEN) {
419 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
424 /* figure out all of the entry offsets and lengths. if we aren't
425 * able to read a resource fork entry, bail. */
426 nentries = len / AD_ENTRY_LEN;
427 parse_entries(ad, buf, nentries);
428 if (!ad_getentryoff(ad, ADEID_RFORK)
429 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
431 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
436 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
437 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
444 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
445 return 1; /* fail silently */
449 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
454 /* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
455 static int ad_header_read_osx(const char *path _U_, struct adouble *ad, struct stat *hst)
458 struct adouble adosx;
459 char *buf = &adosx.ad_data[0];
465 memset(buf, 0, sizeof(adosx.ad_data));
467 /* read the header */
468 EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
470 if (header_len < AD_HEADER_LEN) {
475 memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
476 memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
477 adosx.ad_magic = ntohl(adosx.ad_magic);
478 adosx.ad_version = ntohl(adosx.ad_version);
480 if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
481 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't parse AppleDouble header");
486 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
487 nentries = ntohs(nentries);
488 len = nentries * AD_ENTRY_LEN;
490 if (len + AD_HEADER_LEN > sizeof(adosx.ad_data))
491 len = sizeof(adosx.ad_data) - AD_HEADER_LEN;
493 buf += AD_HEADER_LEN;
494 if (len > header_len - AD_HEADER_LEN) {
495 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't read entry info.");
500 nentries = len / AD_ENTRY_LEN;
501 parse_entries(&adosx, buf, nentries);
503 if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
504 || ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
505 || ad_getentryoff(&adosx, ADEID_RFORK) > header_len
507 LOG(log_error, logtype_afpd, "ad_header_read_osx: problem with rfork entry offset.");
514 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
517 ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(&adosx, ADEID_RFORK));
518 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
524 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst _U_)
529 char *buf = ad->ad_data;
531 /* read the header */
532 if ((ad_meta_fileno(ad) == -1)
533 && (ad->ad_adflags & ADFLAGS_RDWR)) {
534 LOG(log_error, logtype_default, "ad_header_read_ea: need filedescriptor for rw access");
538 if (ad_meta_fileno(ad) != -1)
539 header_len = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA);
541 header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA);
542 if (header_len < 1) {
543 LOG(log_debug, logtype_default, "ad_header_read_ea: %s", strerror(errno));
547 if (header_len < AD_HEADER_LEN) {
548 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
553 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
554 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
556 ad->ad_magic = ntohl( ad->ad_magic );
557 ad->ad_version = ntohl( ad->ad_version );
559 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
560 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
565 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
566 nentries = ntohs( nentries );
568 /* Protect against bogus nentries */
569 len = nentries * AD_ENTRY_LEN;
570 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
571 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
572 if (len > header_len - AD_HEADER_LEN) {
573 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
577 nentries = len / AD_ENTRY_LEN;
579 /* Now parse entries */
580 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
586 * Takes a path to an AppleDouble file and creates the parrent .AppleDouble directory
589 * path: "/path/.AppleDouble/file"
590 * => mkdir("/path/.AppleDouble/") (in ad_mkdir())
592 static int ad_mkrf(const char *path)
596 * Probably .AppleDouble doesn't exist, try to mkdir it.
598 if (NULL == ( slash = strrchr( path, '/' )) ) {
603 if ( ad_mkdir( path, 0777 ) < 0 ) {
610 static int ad_mkrf_ea(const char *path _U_)
612 AFP_PANIC("ad_mkrf_ea: dont use");
616 static int ad_mkrf_osx(const char *path _U_)
622 if we are root change path user/ group
623 It can be a native function for BSD cf. FAQ.Q10
624 path: pathname to chown
625 stbuf: parent directory inode
627 use fstat and fchown or lchown with linux?
629 #define EMULATE_SUIDDIR
631 static int ad_chown(const char *path, struct stat *stbuf)
634 #ifdef EMULATE_SUIDDIR
637 if (default_uid != (uid_t)-1) {
638 /* we are root (admin) */
639 id = (default_uid)?default_uid:stbuf->st_uid;
640 ret = lchown( path, id, stbuf->st_gid );
646 #define DEFMASK 07700 /* be conservative */
649 return access right and inode of path parent directory
651 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
656 if (ad_stat(path, stbuf) != 0) {
660 *mode &= stbuf->st_mode;
664 /* --------------------------- */
665 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
670 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
672 AFP_PANIC("ad_header_upgrade_ea: dont use");
677 * Error handling for adouble header(=metadata) file open error
679 * We're called because opening ADFLAGS_HF caused an error.
680 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
681 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
682 * ought to close it before returning with an error condition.
684 static int ad_error(struct adouble *ad, int adflags)
687 if (adflags & ADFLAGS_NOHF) { /* 1 */
688 ad->ad_adflags &= ~ADFLAGS_HF;
691 if (adflags & ADFLAGS_DF) { /* 2 */
692 ad_close( ad, ADFLAGS_DF );
698 /* Map ADFLAGS to open() flags */
699 static int ad2openflags(int adflags)
703 if (adflags & ADFLAGS_RDWR)
705 if (adflags & ADFLAGS_RDONLY) {
706 if (adflags & ADFLAGS_SETSHRMD)
711 if (adflags & ADFLAGS_CREATE)
713 if (adflags & ADFLAGS_EXCL)
715 if (adflags & ADFLAGS_TRUNC)
721 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
729 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
730 fullpathname(path), mode);
732 if (ad_data_fileno(ad) != -1) {
733 /* the file is already open, but we want write access: */
734 if ((adflags & ADFLAGS_RDWR)
735 /* and it was denied the first time: */
736 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
740 /* it's not new anymore */
741 ad->ad_data_fork.adf_flags &= ~( O_TRUNC | O_CREAT );
742 ad->ad_data_fork.adf_refcount++;
746 oflags = O_NOFOLLOW | ad2openflags(adflags);
749 if ((adflags & ADFLAGS_CREATE)) {
750 st_invalid = ad_mode_st(path, &admode, &st_dir);
751 if ((ad->ad_options & ADVOL_UNIXPRIV))
755 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
757 if (ad->ad_data_fork.adf_fd == -1) {
762 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
765 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
770 case OPEN_NOFOLLOW_ERRNO:
771 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
772 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
773 free(ad->ad_data_fork.adf_syml);
776 ad->ad_data_fork.adf_syml[lsz] = 0;
777 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
785 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
787 ad->ad_data_fork.adf_flags = oflags;
788 adf_lock_init(&ad->ad_data_fork);
789 ad->ad_data_fork.adf_refcount++;
794 /* TODO: error handling */
795 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
799 struct stat *pst = NULL;
801 int oflags, nocreatflags;
805 if (ad_meta_fileno(ad) != -1) {
806 /* the file is already open, but we want write access: */
807 if ((adflags & ADFLAGS_RDWR) &&
808 /* and it was already denied: */
809 (ad->ad_mdp->adf_flags & O_RDONLY)) {
813 ad_refresh(path, ad);
814 /* it's not new anymore */
815 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
816 ad->ad_mdp->adf_refcount++;
820 ad_p = ad->ad_ops->ad_path(path, adflags);
821 oflags = O_NOFOLLOW | ad2openflags(adflags);
822 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
824 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
826 if (ad->ad_mdp->adf_fd != -1) {
827 ad->ad_mdp->adf_flags = nocreatflags;
833 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
834 nocreatflags &= ~O_RDWR;
835 nocreatflags |= O_RDONLY;
836 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
838 ad->ad_mdp->adf_flags = nocreatflags;
843 if (!(oflags & O_CREAT))
844 return ad_error(ad, adflags);
846 * We're expecting to create a new adouble header file here
848 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
852 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
853 if ((ad->ad_options & ADVOL_UNIXPRIV))
855 admode = ad_hf_mode(admode);
856 if ((errno == ENOENT) && (ad->ad_vers != AD_VERSION2_OSX)) {
857 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
858 return ad_error(ad, adflags);
861 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
862 if ((ad->ad_options & ADVOL_UNIXPRIV))
864 admode = ad_hf_mode(admode);
867 /* retry with O_CREAT */
868 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
869 if ( ad->ad_mdp->adf_fd < 0 )
870 return ad_error(ad, adflags);
872 ad->ad_mdp->adf_flags = oflags;
873 /* just created, set owner if admin owner (root) */
875 ad_chown(ad_p, &st_dir);
882 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
883 /* check for 0 length files, treat them as new. */
884 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
885 if (st_meta.st_size == 0)
886 ad->ad_mdp->adf_flags |= O_TRUNC;
888 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
893 adf_lock_init(ad->ad_mdp);
894 ad->ad_mdp->adf_refcount = 1;
896 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
897 /* This is a new adouble header file, create it */
898 if (new_ad_header(ad, path, pst, adflags) < 0) {
900 /* the file is already deleted, perm, whatever, so return an error */
901 ad_close(ad, adflags);
907 /* Read the adouble header in and parse it.*/
908 if (ad->ad_ops->ad_header_read(path, ad, pst) < 0
909 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
911 ad_close(ad, adflags);
920 /* TODO: error handling */
921 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
926 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\", %s, %04o)",
927 path, adflags2logstr(adflags), mode);
929 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~(O_CREAT | O_TRUNC));
931 if (ad_meta_fileno(ad) != -1) {
932 /* the file is already open, but we want write access: */
933 if ((adflags & ADFLAGS_RDWR) &&
934 /* and it was already denied: */
935 (ad->ad_mdp->adf_flags & O_RDONLY)) {
936 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
937 fullpathname(path), strerror(errno));
941 /* it's not new anymore */
942 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
943 ad->ad_mdp->adf_refcount++;
945 if (adflags & ADFLAGS_RDWR) {
946 /* Fo a RDONLY adouble we just use sys_lgetxattr instead if sys_fgetxattr */
947 if (adflags & ADFLAGS_DIR)
948 /* For directories we open the directory RDONYL so we can later fchdir() */
949 oflags = (oflags & ~O_RDWR) | O_RDONLY;
950 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): opening base file for meta adouble EA", path);
951 if ((ad_meta_fileno(ad) = open(path, oflags)) == -1)
953 ad->ad_mdp->adf_flags = oflags;
955 ad->ad_mdp->adf_refcount = 1;
958 /* Read the adouble header in and parse it.*/
959 if (ad->ad_ops->ad_header_read(path, ad, NULL) != 0) {
960 LOG(log_error, logtype_default, "ad_open_hf_ea: no EA adouble");
962 if (!(adflags & ADFLAGS_CREATE))
965 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): creating metadata EA", path);
967 /* It doesnt exist, EPERM or another error */
968 if (!(errno == ENOATTR || errno == ENOENT)) {
969 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
974 if (new_ad_header(ad, path, NULL, adflags) < 0) {
975 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
979 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
981 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
984 /* TODO: ad_rlen calculation */
990 if (ad_meta_fileno(ad) != -1) {
991 close(ad_meta_fileno(ad));
992 ad_meta_fileno(ad) = -1;
993 ad->ad_mdp->adf_refcount = 0;
995 return ad_error(ad, adflags);
998 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
1002 LOG(log_debug, logtype_default, "ad_open_hf(\"%s\", %04o)", path, mode);
1004 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1007 switch (ad->ad_vers) {
1009 ret = ad_open_hf_v2(path, adflags, mode, ad);
1012 ret = ad_open_hf_ea(path, adflags, mode, ad);
1020 return ad_error(ad, adflags);
1027 * Open ressource fork
1029 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
1030 * of the adouble file which is openend by ADFLAGS_HF.
1032 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
1036 int closeflags = adflags & (ADFLAGS_DF | ADFLAGS_HF);
1043 if (ad->ad_vers != AD_VERSION_EA)
1046 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", %04o)", path, mode);
1048 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~O_CREAT);
1050 if (ad_reso_fileno(ad) != -1) {
1051 /* the file is already open, but we want write access: */
1052 if ((adflags & ADFLAGS_RDWR)
1053 /* and it was already denied: */
1054 && (ad->ad_rfp->adf_flags & O_RDONLY)) {
1058 ad->ad_rfp->adf_flags &= ~( O_TRUNC | O_CREAT );
1059 ad->ad_rfp->adf_refcount++;
1063 if ((ad_reso_fileno(ad) = sys_getxattrfd(path, oflags)) == -1) {
1064 if (!(adflags & ADFLAGS_CREATE))
1067 EC_NEG1_LOG( ad_reso_fileno(ad) = sys_getxattrfd(path, oflags, 0666) );
1070 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags) );
1071 if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {
1072 if (!(adflags & ADFLAGS_CREATE))
1075 EC_NEG1_LOG( ad_reso_fileno(ad) = open(rfpath, oflags, mode) );
1076 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork: \"%s\"",
1081 ad->ad_rfp->adf_refcount = 1;
1082 ad->ad_rfp->adf_flags = oflags;
1085 EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &st) );
1086 if (ad->ad_rfp->adf_flags & O_CREAT) {
1087 /* This is a new adouble header file, create it */
1088 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, initializing: \"%s\"",
1090 // EC_NEG1_LOG( new_ad_header(ad, rfpath, &st, adflags) );
1091 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, flushing: \"%s\"",
1095 /* Read the adouble header */
1096 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): reading adouble rfork: \"%s\"",
1098 EC_NEG1_LOG( ad_header_read_osx(NULL, ad, &st) );
1104 if (ad_reso_fileno(ad) != -1) {
1105 close(ad_reso_fileno(ad));
1106 ad_reso_fileno(ad) = -1;
1107 ad->ad_rfp->adf_refcount = 0;
1110 (void)ad_close(ad, closeflags);
1115 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): END", path);
1120 /***********************************************************************************
1122 ********************************************************************************* */
1124 const char *ad_path_ea( const char *path, int adflags _U_)
1129 const char *ad_path_osx(const char *path, int adflags _U_)
1131 static char pathbuf[ MAXPATHLEN + 1];
1132 char c, *slash, buf[MAXPATHLEN + 1];
1134 if (!strcmp(path,".")) {
1136 getcwd(buf, MAXPATHLEN);
1139 strlcpy(buf, path, MAXPATHLEN +1);
1141 if (NULL != ( slash = strrchr( buf, '/' )) ) {
1144 strlcpy( pathbuf, buf, MAXPATHLEN +1);
1147 pathbuf[ 0 ] = '\0';
1150 strlcat( pathbuf, "._", MAXPATHLEN +1);
1151 strlcat( pathbuf, slash, MAXPATHLEN +1);
1156 * Put the .AppleDouble where it needs to be:
1158 * / a/.AppleDouble/b
1160 * \ b/.AppleDouble/.Parent
1162 * FIXME: should do something for pathname > MAXPATHLEN
1164 const char *ad_path( const char *path, int adflags)
1166 static char pathbuf[ MAXPATHLEN + 1];
1170 if ( adflags & ADFLAGS_DIR ) {
1171 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1173 if ( l && l < MAXPATHLEN) {
1176 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1178 if (NULL != ( slash = strrchr( path, '/' )) ) {
1181 /* XXX we must return NULL here and test in the caller */
1184 memcpy( pathbuf, path, l);
1189 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1190 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1196 /* -------------------------
1197 * Support inherited protection modes for AppleDouble files. The supplied
1198 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1199 * and that value is returned.
1201 char *ad_dir(const char *path)
1203 static char modebuf[ MAXPATHLEN + 1];
1206 * For a path with directories in it, remove the final component
1207 * (path or subdirectory name) to get the name we want to stat.
1208 * For a path which is just a filename, use "." instead.
1210 slash = strrchr( path, '/' );
1215 if (len >= MAXPATHLEN) {
1216 errno = ENAMETOOLONG;
1217 return NULL; /* can't do it */
1219 memcpy( modebuf, path, len );
1220 modebuf[len] = '\0';
1221 /* is last char a '/' ? */
1222 if (slash[1] == 0) {
1223 slash = modebuf+ len;
1225 while (modebuf < slash && slash[-1] == '/') {
1228 if (modebuf == slash) {
1232 while (modebuf < slash && *slash != '/') {
1235 if (modebuf == slash) {
1238 *slash = '\0'; /* remove pathname component */
1243 modebuf[0] = '.'; /* use current directory */
1248 int ad_setfuid(const uid_t id)
1254 /* ---------------- */
1255 uid_t ad_getfuid(void)
1261 stat path parent directory
1263 int ad_stat(const char *path, struct stat *stbuf)
1270 return lstat( p, stbuf );
1274 return access right of path parent directory
1276 int ad_mode( const char *path, mode_t mode)
1279 ad_mode_st(path, &mode, &stbuf);
1284 * Use mkdir() with mode bits taken from ad_mode().
1286 int ad_mkdir( const char *path, mode_t mode)
1292 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1293 path, mode, getcwdpath());
1295 st_invalid = ad_mode_st(path, &mode, &stbuf);
1296 ret = mkdir( path, mode );
1297 if (ret || st_invalid)
1299 ad_chown(path, &stbuf);
1304 static void ad_init_func(struct adouble *ad)
1306 switch (ad->ad_vers) {
1308 ad->ad_ops = &ad_adouble;
1309 ad->ad_rfp = &ad->ad_resource_fork;
1310 ad->ad_mdp = &ad->ad_resource_fork;
1313 ad->ad_ops = &ad_adouble_ea;
1314 ad->ad_rfp = &ad->ad_resource_fork;
1315 ad->ad_mdp = &ad->ad_data_fork;
1318 AFP_PANIC("ad_init: unknown AD version");
1322 ad_data_fileno(ad) = -1;
1323 ad_reso_fileno(ad) = -1;
1324 ad_meta_fileno(ad) = -1;
1325 ad->ad_refcount = 1;
1329 void ad_init_old(struct adouble *ad, int flags, int options)
1331 memset(ad, 0, sizeof(struct adouble));
1332 ad->ad_vers = flags;
1333 ad->ad_options = options;
1337 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1339 memset(ad, 0, sizeof(struct adouble));
1340 ad->ad_vers = vol->v_adouble;
1341 ad->ad_options = vol->v_ad_options;
1346 * Open data-, metadata(header)- or ressource fork
1348 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1349 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1351 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1354 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1357 * Open a files data fork, metadata fork or ressource fork.
1359 * @param ad (rw) pointer to struct adouble
1360 * @param path (r) Path to file or directory
1361 * @param adflags (r) Flags specifying which fork to open, can be or'd:
1362 * ADFLAGS_DF: open data fork
1363 * ADFLAGS_RF: open ressource fork
1364 * ADFLAGS_HF: open header (metadata) file
1365 * ADFLAGS_NOHF: it's not an error if header file couldn't be created
1366 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags
1368 * Access mode for the forks:
1369 * ADFLAGS_RDONLY: open read only
1370 * ADFLAGS_RDWR: open read write
1373 * ADFLAGS_CREATE: create if not existing
1374 * ADFLAGS_TRUNC: truncate
1377 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1378 * ADFLAGS_SETSHRMD: this adouble struct will be used to set sharemode locks.
1379 * This basically results in the files being opened RW instead of RDONLY.
1380 * @param mode (r) mode used with O_CREATE
1382 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1383 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1384 * fds of the same file
1386 * @returns 0 on success, any other value indicates an error
1388 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1394 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1395 fullpathname(path), adflags2logstr(adflags));
1397 if (adflags & ADFLAGS_CHECK_OF)
1398 /* Checking for open forks requires sharemode lock support (ie RDWR instead of RDONLY) */
1399 adflags |= ADFLAGS_SETSHRMD;
1401 if ((ad->ad_vers == AD_VERSION_EA) && (adflags & ADFLAGS_SETSHRMD))
1402 /* adouble:ea sets sharemode locks on the datafork */
1403 adflags |= ADFLAGS_DF;
1405 if (ad->ad_inited != AD_INITED) {
1406 ad->ad_adflags = adflags;
1407 ad->ad_inited = AD_INITED;
1409 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1410 if (ad->ad_resource_fork.adf_refcount > 0)
1411 ad->ad_open_forks |= ATTRBIT_ROPEN;
1414 va_start(args, adflags);
1415 if (adflags & ADFLAGS_CREATE)
1416 mode = va_arg(args, mode_t);
1419 if (adflags & ADFLAGS_DF) {
1420 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1421 ad->ad_adflags |= ADFLAGS_DF;
1424 if (adflags & ADFLAGS_HF) {
1425 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1426 ad->ad_adflags |= ADFLAGS_HF;
1429 if (adflags & ADFLAGS_RF) {
1430 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1431 ad->ad_adflags |= ADFLAGS_RF;
1434 if (adflags & ADFLAGS_CHECK_OF) {
1435 ad->ad_open_forks |= ad_openforks(ad, ad->ad_open_forks);
1443 * @brief open metadata, possibly as root
1445 * Return only metadata but try very hard ie at first try as user, then try as root.
1447 * @param name name of file/dir
1448 * @param flags ADFLAGS_DIR: name is a directory \n
1449 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1451 * @param adp pointer to struct adouble
1453 int ad_metadata(const char *name, int flags, struct adouble *adp)
1456 int ret, err, oflags;
1458 /* Sanitize flags */
1459 oflags = (flags & (ADFLAGS_CHECK_OF | ADFLAGS_DIR)) | ADFLAGS_HF | ADFLAGS_RDONLY;
1461 if ((ret = ad_open(adp, name, oflags)) < 0 && errno == EACCES) {
1464 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1468 /* we are root open read only */
1469 ret = ad_open(adp, name, oflags);
1471 if ( seteuid(uid) < 0) {
1472 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1482 * @brief openat like wrapper for ad_metadata
1484 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1490 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1496 if (ad_metadata(name, flags, adp) < 0) {
1502 if (fchdir(cwdfd) != 0) {
1503 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1516 int ad_refresh(const char *path, struct adouble *ad)
1518 switch (ad->ad_vers) {
1520 if (ad_meta_fileno(ad) == -1)
1522 return ad->ad_ops->ad_header_read(NULL, ad, NULL);
1526 if (AD_META_OPEN(ad)) {
1527 if (ad_data_fileno(ad) == -1)
1529 // TODO: read meta EA
1532 if (AD_RSRC_OPEN(ad)) {
1533 if (ad_reso_fileno(ad) == -1)
1536 if ((len = fstat(ad_reso_fileno(ad))) == -1)
1541 if (AD_META_OPEN(ad)) {
1542 if (ad_data_fileno(ad) == -1)
1544 // TODO: read meta EA
1546 if (AD_RSRC_OPEN(ad)) {
1547 if (ad_reso_fileno(ad) == -1)
1549 if (ad_header_read_osx(path, ad, NULL) < 0)
1553 return ad->ad_ops->ad_header_read(path, ad, NULL);
1562 int ad_openat(struct adouble *ad,
1563 int dirfd, /* dir fd openat like */
1573 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1577 va_start(args, adflags);
1578 if (adflags & ADFLAGS_CREATE)
1579 mode = va_arg(args, mode_t);
1582 EC_NEG1( ad_open(ad, path, adflags, mode) );
1585 if (fchdir(cwdfd) != 0) {
1586 AFP_PANIC("ad_openat: cant chdir back");