1 /* 2 * Copyright (c) 1990,1991 Regents of The University of Michigan. 3 * All Rights Reserved. 4 * 5 * Permission to use, copy, modify, and distribute this software and 6 * its documentation for any purpose and without fee is hereby granted, 7 * provided that the above copyright notice appears in all copies and 8 * that both that copyright notice and this permission notice appear 9 * in supporting documentation, and that the name of The University 10 * of Michigan not be used in advertising or publicity pertaining to 11 * distribution of the software without specific, written prior 12 * permission. This software is supplied as is without expressed or 13 * implied warranties of any kind. 14 * 15 * Research Systems Unix Group 16 * The University of Michigan 17 * c/o Mike Clark 18 * 535 W. William Street 19 * Ann Arbor, Michigan 20 * +1-313-763-0525 21 * netatalk@itd.umich.edu 22 */ 23 24/*! 25 * @file 26 * @brief Part of Netatalk's AppleDouble implementatation 27 */ 28 29#ifndef _ATALK_ADOUBLE_H 30#define _ATALK_ADOUBLE_H 31 32#ifdef HAVE_CONFIG_H 33#include <config.h> 34#endif 35 36#include <atalk/standards.h> 37 38#include <inttypes.h> 39#include <sys/types.h> 40#include <sys/stat.h> 41#include <unistd.h> 42#include <fcntl.h> 43 44#include <sys/mman.h> 45 46#include <sys/time.h> 47 48#include <atalk/bstrlib.h> 49 50/* version info */ 51#define AD_VERSION2 0x00020000 52#define AD_VERSION_EA 0x00020002 53 54/* default */ 55#define AD_VERSION AD_VERSION_EA 56 57/* 58 * AppleDouble entry IDs. 59 */ 60#define ADEID_DFORK 1 61#define ADEID_RFORK 2 62#define ADEID_NAME 3 63#define ADEID_COMMENT 4 64#define ADEID_ICONBW 5 65#define ADEID_ICONCOL 6 66#define ADEID_FILEI 7 /* v1, replaced by: */ 67#define ADEID_FILEDATESI 8 /* this */ 68#define ADEID_FINDERI 9 69#define ADEID_MACFILEI 10 /* we don't use this */ 70#define ADEID_PRODOSFILEI 11 /* we store prodos info here */ 71#define ADEID_MSDOSFILEI 12 /* we don't use this */ 72#define ADEID_SHORTNAME 13 73#define ADEID_AFPFILEI 14 /* where the rest of the FILEI info goes */ 74#define ADEID_DID 15 75 76/* netatalk private note fileid reused DID */ 77#define ADEID_PRIVDEV 16 78#define ADEID_PRIVINO 17 79#define ADEID_PRIVSYN 18 /* in synch with database */ 80#define ADEID_PRIVID 19 81#define ADEID_MAX (ADEID_PRIVID + 1) 82 83/* These are the real ids for these entries, as stored in the adouble file */ 84#define AD_DEV 0x80444556 85#define AD_INO 0x80494E4F 86#define AD_SYN 0x8053594E 87#define AD_ID 0x8053567E 88 89/* magic */ 90#define AD_APPLESINGLE_MAGIC 0x00051600 91#define AD_APPLEDOUBLE_MAGIC 0x00051607 92#define AD_MAGIC AD_APPLEDOUBLE_MAGIC 93 94/* sizes of relevant entry bits */ 95#define ADEDLEN_MAGIC 4 96#define ADEDLEN_VERSION 4 97#define ADEDLEN_FILLER 16 98#define ADEDLEN_NENTRIES 2 99#define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */ 100#define AD_ENTRY_LEN 12 /* size of a single entry header */ 101 102/* field widths */ 103#define ADEDLEN_NAME 255 104#define ADEDLEN_COMMENT 200 105#define ADEDLEN_FILEI 16 106#define ADEDLEN_FINDERI 32 107#define ADEDLEN_FILEDATESI 16 108#define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */ 109#define ADEDLEN_AFPFILEI 4 110#define ADEDLEN_MACFILEI 4 111#define ADEDLEN_PRODOSFILEI 8 112#define ADEDLEN_MSDOSFILEI 2 113#define ADEDLEN_DID 4 114#define ADEDLEN_PRIVDEV 8 115#define ADEDLEN_PRIVINO 8 116#define ADEDLEN_PRIVSYN 8 117#define ADEDLEN_PRIVID 4 118 119#define ADEID_NUM_V2 13 120#define ADEID_NUM_EA 8 121#define ADEID_NUM_OSX 2 122 123#define AD_DATASZ2 (AD_HEADER_LEN + ADEDLEN_NAME + ADEDLEN_COMMENT + ADEDLEN_FILEI + \ 124 ADEDLEN_FINDERI + ADEDLEN_DID + ADEDLEN_AFPFILEI + ADEDLEN_SHORTNAME + \ 125 ADEDLEN_PRODOSFILEI + ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \ 126 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID + (ADEID_NUM_V2 * AD_ENTRY_LEN)) 127#if AD_DATASZ2 != 741 128#error bad size for AD_DATASZ2 129#endif 130 131#define AD_DATASZ_EA (AD_HEADER_LEN + (ADEID_NUM_EA * AD_ENTRY_LEN) + \ 132 ADEDLEN_FINDERI + ADEDLEN_COMMENT + ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \ 133 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + ADEDLEN_PRIVSYN + ADEDLEN_PRIVID) 134 135#if AD_DATASZ_EA != 402 136#error bad size for AD_DATASZ_EA 137#endif 138 139#define AD_DATASZ_OSX (AD_HEADER_LEN + (ADEID_NUM_OSX * AD_ENTRY_LEN) + ADEDLEN_FINDERI) 140 141#if AD_DATASZ_OSX != 82 142#error bad size for AD_DATASZ_OSX 143#endif 144 145#define AD_DATASZ_MAX 1024 146 147#if AD_VERSION == AD_VERSION2 148#define AD_DATASZ AD_DATASZ2 149#elif AD_VERSION == AD_VERSION_EA 150#define AD_DATASZ AD_DATASZ_EA 151#endif 152 153/* fallback for ad:ea on filesystems without fds for EAs, like old adouble:osx */ 154#define ADEDOFF_FINDERI_OSX (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN) 155#define ADEDOFF_RFORK_OSX (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI) 156 157/* special fd value used to indicate an open fork file is a (not open) symlink */ 158#define AD_SYMLINK -2 159 160typedef uint32_t cnid_t; 161 162struct ad_entry { 163 off_t ade_off; 164 ssize_t ade_len; 165}; 166 167typedef struct adf_lock_t { 168 struct flock lock; 169 int user; 170 int *refcount; /* handle read locks with multiple users */ 171} adf_lock_t; 172 173struct ad_fd { 174 int adf_fd; /* -1: invalid, AD_SYMLINK: symlink */ 175 char *adf_syml; 176 int adf_flags; 177 adf_lock_t *adf_lock; 178 int adf_refcount, adf_lockcount, adf_lockmax; 179}; 180 181/* some header protection */ 182#define AD_INITED 0xad494e54 /* ad"INT" */ 183#define AD_CLOSED 0xadc10ced 184 185struct adouble; 186 187struct adouble_fops { 188 const char *(*ad_path)(const char *, int); 189 int (*ad_mkrf)(const char *); 190 int (*ad_rebuild_header)(struct adouble *); 191 int (*ad_header_read)(const char *, struct adouble *, const struct stat *); 192 int (*ad_header_upgrade)(struct adouble *, const char *); 193}; 194 195struct adouble { 196 uint32_t ad_magic; /* Official adouble magic */ 197 uint32_t ad_version; /* Official adouble version number */ 198 char ad_filler[16]; 199 struct ad_entry ad_eid[ADEID_MAX]; 200 201 struct ad_fd ad_data_fork; /* the data fork */ 202 203 struct ad_fd ad_resource_fork; /* adouble:v2 -> the adouble file * 204 * adouble:ea -> the EA fd */ 205 206 struct ad_fd *ad_rfp; /* adouble:v2 -> ad_resource_fork * 207 * adouble:ea -> ad_resource_fork */ 208 209 struct ad_fd *ad_mdp; /* adouble:v2 -> ad_resource_fork * 210 * adouble:ea -> ad_data_fork */ 211 212 int ad_vers; /* Our adouble version info (AD_VERSION*) */ 213 int ad_adflags; /* ad_open flags adflags like ADFLAGS_DIR */ 214 uint32_t ad_inited; 215 int ad_options; 216 int ad_refcount; /* multiple forks may open one adouble */ 217 int ad_data_refcount; 218 int ad_meta_refcount; 219 int ad_reso_refcount; 220 off_t ad_rlen; /* ressource fork len with AFP 3.0 * 221 * the header parameter size is too small. */ 222 char *ad_name; /* name (UTF8-MAC) */ 223 struct adouble_fops *ad_ops; 224 uint16_t ad_open_forks; /* open forks (by others) */ 225 char ad_data[AD_DATASZ_MAX]; 226}; 227 228#define ADFLAGS_DF (1<<0) 229#define ADFLAGS_RF (1<<1) 230#define ADFLAGS_HF (1<<2) 231#define ADFLAGS_DIR (1<<3) 232#define ADFLAGS_NOHF (1<<4) /* not an error if no metadata fork */ 233#define ADFLAGS_NORF (1<<5) /* not an error if no ressource fork */ 234#define ADFLAGS_CHECK_OF (1<<6) /* check for open forks from us and other afpd's */ 235#define ADFLAGS_SETSHRMD (1<<7) /* setting share mode must be done with excl fcnt lock, 236 which implies that the file must be openend rw. 237 If it can't be opened rw (eg EPERM or EROFS) it will 238 be opened ro and the fcntl locks will be shared, that 239 at least prevent other users who have rw access to the 240 file from placing excl locks. */ 241#define ADFLAGS_RDWR (1<<8) /* open read/write */ 242#define ADFLAGS_RDONLY (1<<9) /* open read only */ 243#define ADFLAGS_CREATE (1<<10) /* create file, open called with O_CREAT */ 244#define ADFLAGS_EXCL (1<<11) /* exclusive open, open called with O_EXCL */ 245#define ADFLAGS_TRUNC (1<<12) /* truncate, open called with O_TRUNC */ 246 247#define ADVOL_NODEV (1 << 0) 248#define ADVOL_RO (1 << 1) 249#define ADVOL_UNIXPRIV (1 << 2) /* adouble unix priv */ 250#define ADVOL_INVDOTS (1 << 3) /* dot files (.DS_Store) are invisible) */ 251#define ADVOL_FOLLO_SYML (1 << 4) 252 253/* lock flags */ 254#define ADLOCK_CLR (0) 255#define ADLOCK_RD (1<<0) 256#define ADLOCK_WR (1<<1) 257#define ADLOCK_MASK (ADLOCK_RD | ADLOCK_WR) 258#define ADLOCK_UPGRADE (1<<2) 259#define ADLOCK_FILELOCK (1<<3) 260 261/* we use this so that we can use the same mechanism for both byte 262 * locks and file synchronization locks. */ 263#if _FILE_OFFSET_BITS == 64 264#define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9) 265#else 266#define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9) 267#endif 268 269#define BYTELOCK_MAX (AD_FILELOCK_BASE - 1) 270 271/* datafork and rsrcfork sharemode locks */ 272#define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0) 273#define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1) 274#define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2) 275#define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3) 276 277#define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4) 278#define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5) 279#define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6) 280#define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7) 281 282#define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8) 283#define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9) 284 285/* time stuff. we overload the bits a little. */ 286#define AD_DATE_CREATE 0 287#define AD_DATE_MODIFY 4 288#define AD_DATE_BACKUP 8 289#define AD_DATE_ACCESS 12 290#define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \ 291 AD_DATE_BACKUP | AD_DATE_ACCESS) 292#define AD_DATE_UNIX (1 << 10) 293#define AD_DATE_START htonl(0x80000000) 294#define AD_DATE_DELTA 946684800 295#define AD_DATE_FROM_UNIX(x) htonl((x) - AD_DATE_DELTA) 296#define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA) 297 298/* various finder offset and info bits */ 299#define FINDERINFO_FRTYPEOFF 0 300#define FINDERINFO_FRCREATOFF 4 301#define FINDERINFO_FRFLAGOFF 8 302 303/* FinderInfo Flags, char in `ad ls`, valid for files|dirs */ 304#define FINDERINFO_ISONDESK (1) /* "d", fd */ 305#define FINDERINFO_COLOR (0x0e) 306#define FINDERINFO_HIDEEXT (1<<4) /* "e", fd */ 307#define FINDERINFO_ISHARED (1<<6) /* "m", f */ 308#define FINDERINFO_HASNOINITS (1<<7) /* "n", f */ 309#define FINDERINFO_HASBEENINITED (1<<8) /* "i", fd */ 310#define FINDERINFO_HASCUSTOMICON (1<<10) /* "c", fd */ 311#define FINDERINFO_ISSTATIONNERY (1<<11) /* "t", f */ 312#define FINDERINFO_NAMELOCKED (1<<12) /* "s", fd */ 313#define FINDERINFO_HASBUNDLE (1<<13) /* "b", fd */ 314#define FINDERINFO_INVISIBLE (1<<14) /* "v", fd */ 315#define FINDERINFO_ISALIAS (1<<15) /* "a", fd */ 316 317#define FINDERINFO_FRVIEWOFF 14 318#define FINDERINFO_CUSTOMICON 0x4 319#define FINDERINFO_CLOSEDVIEW 0x100 320 321/* 322 The "shared" and "invisible" attributes are opaque and stored and 323 retrieved from the FinderFlags. This fixes Bug #2802236: 324 <https://sourceforge.net/tracker/?func=detail&aid=2802236&group_id=8642&atid=108642> 325*/ 326 327/* AFP attributes, char in `ad ls`, valid for files|dirs */ 328#define ATTRBIT_INVISIBLE (1<<0) /* opaque from FinderInfo */ 329#define ATTRBIT_MULTIUSER (1<<1) /* file: opaque, dir: see below */ 330#define ATTRBIT_SYSTEM (1<<2) /* "y", fd */ 331#define ATTRBIT_DOPEN (1<<3) /* data fork already open. Not stored, computed on the fly */ 332#define ATTRBIT_ROPEN (1<<4) /* resource fork already open. Not stored, computed on the fly */ 333#define ATTRBIT_NOWRITE (1<<5) /* "w", f, write inhibit(v2)/read-only(v1) bit */ 334#define ATTRBIT_BACKUP (1<<6) /* "p", fd */ 335#define ATTRBIT_NORENAME (1<<7) /* "r", fd */ 336#define ATTRBIT_NODELETE (1<<8) /* "l", fd */ 337#define ATTRBIT_NOCOPY (1<<10) /* "o", f */ 338#define ATTRBIT_SETCLR (1<<15) /* set/clear bit (d) */ 339 340/* AFP attributes for dirs. These should probably be computed on the fly. 341 * We don't do that, nor does e.g. OS S X 10.5 Server */ 342#define ATTRBIT_EXPFLDR (1<<1) /* Folder is a sharepoint */ 343#define ATTRBIT_MOUNTED (1<<3) /* Directory is mounted by a user */ 344#define ATTRBIT_SHARED (1<<4) /* Shared area, called IsExpFolder in spec */ 345 346/* private AFPFileInfo bits */ 347#define AD_AFPFILEI_OWNER (1 << 0) /* any owner */ 348#define AD_AFPFILEI_GROUP (1 << 1) /* ignore group */ 349#define AD_AFPFILEI_BLANKACCESS (1 << 2) /* blank access permissions */ 350 351#define ad_data_fileno(ad) ((ad)->ad_data_fork.adf_fd) 352#define ad_reso_fileno(ad) ((ad)->ad_rfp->adf_fd) 353#define ad_meta_fileno(ad) ((ad)->ad_mdp->adf_fd) 354 355/* -1: not open, AD_SYMLINK (-2): it's a symlink */ 356#define AD_DATA_OPEN(ad) (((ad)->ad_data_refcount) && (ad_data_fileno(ad) >= 0)) 357#define AD_META_OPEN(ad) (((ad)->ad_meta_refcount) && (ad_meta_fileno(ad) >= 0)) 358#define AD_RSRC_OPEN(ad) (((ad)->ad_reso_refcount) && (ad_reso_fileno(ad) >= 0)) 359 360#define ad_getversion(ad) ((ad)->ad_version) 361 362#define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len) 363#define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len)) 364#define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off)) 365#define ad_entry(ad,eid) ((caddr_t)(ad)->ad_data + (ad)->ad_eid[(eid)].ade_off) 366 367#define ad_get_RF_flags(ad) ((ad)->ad_rfp->adf_flags) 368#define ad_get_MD_flags(ad) ((ad)->ad_mdp->adf_flags) 369 370/* Refcounting open forks using one struct adouble */ 371#define ad_ref(ad) (ad)->ad_refcount++ 372#define ad_unref(ad) --((ad)->ad_refcount) 373 374#define ad_get_syml_opt(ad) (((ad)->ad_options & ADVOL_FOLLO_SYML) ? 0 : O_NOFOLLOW) 375 376/* ad_flush.c */ 377extern int ad_rebuild_adouble_header_v2(struct adouble *); 378extern int ad_rebuild_adouble_header_ea(struct adouble *); 379extern int ad_copy_header (struct adouble *, struct adouble *); 380extern int ad_flush (struct adouble *); 381extern int ad_close (struct adouble *, int); 382extern int fsetrsrcea(struct adouble *ad, int fd, const char *eaname, const void *value, size_t size, int flags); 383 384/* ad_lock.c */ 385extern int ad_testlock (struct adouble *adp, int eid, off_t off); 386extern uint16_t ad_openforks(struct adouble *adp, uint16_t); 387extern int ad_lock(struct adouble *, uint32_t eid, int type, off_t off, off_t len, int fork); 388extern void ad_unlock(struct adouble *, int fork, int unlckbrl); 389extern int ad_tmplock(struct adouble *, uint32_t eid, int type, off_t off, off_t len, int fork); 390 391/* ad_open.c */ 392extern off_t ad_getentryoff(const struct adouble *ad, int eid); 393extern const char *adflags2logstr(int adflags); 394extern int ad_setfuid (const uid_t ); 395extern uid_t ad_getfuid (void ); 396extern char *ad_dir (const char *); 397extern const char *ad_path (const char *, int); 398extern const char *ad_path_ea (const char *, int); 399extern const char *ad_path_osx (const char *path, int adflags); 400extern int ad_mode (const char *, mode_t); 401extern int ad_mkdir (const char *, mode_t); 402struct vol; 403extern void ad_init (struct adouble *, const struct vol * restrict); 404extern void ad_init_old (struct adouble *ad, int flags, int options); 405extern int ad_init_offsets(struct adouble *ad); 406extern int ad_open (struct adouble *ad, const char *path, int adflags, ...); 407extern int ad_openat (struct adouble *, int dirfd, const char *path, int adflags, ...); 408extern int ad_refresh (const char *path, struct adouble *); 409extern int ad_stat (const char *, struct stat *); 410extern int ad_metadata (const char *, int, struct adouble *); 411extern int ad_metadataat (int, const char *, int, struct adouble *); 412extern mode_t ad_hf_mode(mode_t mode); 413extern int ad_valid_header_osx(const char *path); 414extern off_t ad_reso_size(const char *path, int adflags, struct adouble *ad); 415 416/* ad_conv.c */ 417extern int ad_convert(const char *path, const struct stat *sp, const struct vol *vol, const char **newpath); 418 419/* ad_read.c/ad_write.c */ 420extern int sys_ftruncate(int fd, off_t length); 421extern ssize_t ad_read(struct adouble *, uint32_t, off_t, char *, size_t); 422extern ssize_t ad_pread(struct ad_fd *, void *, size_t, off_t); 423extern ssize_t ad_write(struct adouble *, uint32_t, off_t, int, const char *, size_t); 424extern ssize_t adf_pread(struct ad_fd *, void *, size_t, off_t); 425extern ssize_t adf_pwrite(struct ad_fd *, const void *, size_t, off_t); 426extern int ad_dtruncate(struct adouble *, off_t); 427extern int ad_rtruncate(struct adouble *, const char *, off_t); 428extern int copy_fork(int eid, struct adouble *add, struct adouble *ads); 429 430/* ad_size.c */ 431extern off_t ad_size (const struct adouble *, uint32_t ); 432 433/* ad_mmap.c */ 434extern void *ad_mmapread(struct adouble *, uint32_t, off_t, size_t); 435extern void *ad_mmapwrite(struct adouble *, uint32_t, off_t, int, size_t); 436#define ad_munmap(buf, len) (munmap((buf), (len))) 437 438/* ad_date.c */ 439extern int ad_setdate(struct adouble *, unsigned int, uint32_t); 440extern int ad_getdate(const struct adouble *, unsigned int, uint32_t *); 441 442/* ad_attr.c */ 443extern int ad_setattr(const struct adouble *, const uint16_t); 444extern int ad_getattr(const struct adouble *, uint16_t *); 445extern int ad_setname(struct adouble *, const char *); 446extern int ad_setid(struct adouble *, dev_t dev, ino_t ino, uint32_t, uint32_t, const void *); 447extern uint32_t ad_getid(struct adouble *, dev_t, ino_t, cnid_t, const void *); 448extern uint32_t ad_forcegetid(struct adouble *adp); 449 450#ifdef WITH_SENDFILE 451extern int ad_readfile_init(const struct adouble *ad, int eid, off_t *off, int end); 452#endif 453 454#endif /* _ATALK_ADOUBLE_H */ 455