2 $Id: ea.c,v 1.22 2010-04-18 11:11:17 franklahm Exp $
3 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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.
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.
18 #endif /* HAVE_CONFIG_H */
25 #include <sys/types.h>
30 #include <atalk/adouble.h>
32 #include <atalk/afp.h>
33 #include <atalk/logger.h>
34 #include <atalk/volume.h>
35 #include <atalk/vfs.h>
36 #include <atalk/util.h>
37 #include <atalk/unix.h>
40 * Store Extended Attributes inside .AppleDouble folders as follows:
42 * filename "fileWithEAs" with EAs "testEA1" and "testEA2"
44 * - create header with with the format struct adouble_ea_ondisk, the file is written to
45 * ".AppleDouble/fileWithEAs::EA"
46 * - store EAs in files "fileWithEAs::EA::testEA1" and "fileWithEAs::EA::testEA2"
50 * Build mode for EA header from file mode
52 static inline mode_t ea_header_mode(mode_t mode)
54 /* Same as ad_hf_mode(mode) */
55 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
56 /* Owner must be able to open, read and w-lock it, in order to chmod from eg 0000 -> 0xxxx*/
57 mode |= S_IRUSR | S_IWUSR;
62 * Build mode for EA file from file mode
64 static inline mode_t ea_mode(mode_t mode)
66 /* Same as ad_hf_mode(mode) */
67 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
72 Taken form afpd/desktop.c
74 static char *mtoupath(const struct vol *vol, const char *mpath)
76 static char upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
81 uint16_t flags = CONV_ESCAPEHEX | CONV_ALLOW_COLON;
86 if ( *mpath == '\0' ) {
96 if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
99 m, inplen, u, outlen, &flags)) ) {
108 * Function: unpack_header
110 * Purpose: unpack and verify header file data buffer at ea->ea_data into struct ea
114 * ea (rw) handle to struct ea
116 * Returns: 0 on success, -1 on error
120 * Verifies magic and version.
122 static int unpack_header(struct ea * restrict ea)
125 unsigned int count = 0;
129 /* Check magic and version */
131 if (*(uint32_t *)buf != htonl(EA_MAGIC)) {
132 LOG(log_error, logtype_afpd, "unpack_header: wrong magic 0x%08x", *(uint32_t *)buf);
137 if (*(uint16_t *)buf != htons(EA_VERSION)) {
138 LOG(log_error, logtype_afpd, "unpack_header: wrong version 0x%04x", *(uint16_t *)buf);
145 ea->ea_count = ntohs(*(uint16_t *)buf);
146 LOG(log_debug, logtype_afpd, "unpack_header: number of EAs: %u", ea->ea_count);
149 if (ea->ea_count == 0)
152 /* Allocate storage for the ea_entries array */
153 ea->ea_entries = malloc(sizeof(struct ea_entry) * ea->ea_count);
154 if ( ! ea->ea_entries) {
155 LOG(log_error, logtype_afpd, "unpack_header: OOM");
160 buf = ea->ea_data + EA_HEADER_SIZE;
161 while (count < ea->ea_count) {
162 memcpy(&uint32, buf, 4); /* EA size */
164 (*(ea->ea_entries))[count].ea_size = ntohl(uint32);
165 (*(ea->ea_entries))[count].ea_name = strdup(buf);
166 if (! (*(ea->ea_entries))[count].ea_name) {
167 LOG(log_error, logtype_afpd, "unpack_header: OOM");
171 (*(ea->ea_entries))[count].ea_namelen = strlen((*(ea->ea_entries))[count].ea_name);
172 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
174 LOG(log_maxdebug, logtype_afpd, "unpack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
175 (*(ea->ea_entries))[count].ea_name,
176 (*(ea->ea_entries))[count].ea_size,
177 (*(ea->ea_entries))[count].ea_namelen);
187 * Function: pack_header
189 * Purpose: pack everything from struct ea into buffer at ea->ea_data
193 * ea (rw) handle to struct ea
195 * Returns: 0 on success, -1 on error
199 * adjust ea->ea_count in case an ea entry deletetion is detected
201 static int pack_header(struct ea * restrict ea)
203 unsigned int count = 0, eacount = 0;
206 size_t bufsize = EA_HEADER_SIZE;
208 char *buf = ea->ea_data + EA_HEADER_SIZE;
210 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
211 ea->filename, ea->ea_count, ea->ea_size);
213 if (ea->ea_count == 0)
214 /* nothing to do, magic, version and count are still valid in buffer */
217 while(count < ea->ea_count) { /* the names */
218 /* Check if its a deleted entry */
219 if ( ! ((*ea->ea_entries)[count].ea_name)) {
224 bufsize += (*(ea->ea_entries))[count].ea_namelen + 1;
229 bufsize += (eacount * 4); /* header + ea_size for each EA */
230 if (bufsize > ea->ea_size) {
231 /* we must realloc */
232 if ( ! (buf = realloc(ea->ea_data, bufsize)) ) {
233 LOG(log_error, logtype_afpd, "pack_header: OOM");
238 ea->ea_size = bufsize;
241 uint16 = htons(eacount);
242 memcpy(ea->ea_data + EA_COUNT_OFF, &uint16, 2);
245 buf = ea->ea_data + EA_HEADER_SIZE;
246 while (count < ea->ea_count) {
247 /* Check if its a deleted entry */
248 if ( ! ((*ea->ea_entries)[count].ea_name)) {
254 uint32 = htonl((*(ea->ea_entries))[count].ea_size);
255 memcpy(buf, &uint32, 4);
258 /* Second: EA name as C-string */
259 strcpy(buf, (*(ea->ea_entries))[count].ea_name);
260 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
262 LOG(log_maxdebug, logtype_afpd, "pack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
263 (*(ea->ea_entries))[count].ea_name,
264 (*(ea->ea_entries))[count].ea_size,
265 (*(ea->ea_entries))[count].ea_namelen);
270 ea->ea_count = eacount;
272 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
273 ea->filename, ea->ea_count, ea->ea_size);
279 * Function: ea_addentry
281 * Purpose: add one EA into ea->ea_entries[]
285 * ea (rw) pointer to struct ea
286 * attruname (r) name of EA
287 * attrsize (r) size of ea
288 * bitmap (r) bitmap from FP func
290 * Returns: new number of EA entries, -1 on error
294 * Grow array ea->ea_entries[]. If ea->ea_entries is still NULL, start allocating.
295 * Otherwise realloc and put entry at the end. Increments ea->ea_count.
297 static int ea_addentry(struct ea * restrict ea,
298 const char * restrict attruname,
303 unsigned int count = 0;
306 /* First check if an EA of the requested name already exist */
307 if (ea->ea_count > 0) {
308 while (count < ea->ea_count) {
309 if (strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
311 LOG(log_debug, logtype_afpd, "ea_addentry('%s', bitmap:0x%x): exists", attruname, bitmap);
312 if (bitmap & kXAttrCreate)
313 /* its like O_CREAT|O_EXCL -> fail */
315 (*(ea->ea_entries))[count].ea_size = attrsize;
322 if ((bitmap & kXAttrReplace) && ! ea_existed)
323 /* replace was requested, but EA didn't exist */
326 if (ea->ea_count == 0) {
327 ea->ea_entries = malloc(sizeof(struct ea_entry));
328 if ( ! ea->ea_entries) {
329 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
332 } else if (! ea_existed) {
333 tmprealloc = realloc(ea->ea_entries, sizeof(struct ea_entry) * (ea->ea_count + 1));
335 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
338 ea->ea_entries = tmprealloc;
341 /* We've grown the array, now store the entry */
342 (*(ea->ea_entries))[ea->ea_count].ea_size = attrsize;
343 (*(ea->ea_entries))[ea->ea_count].ea_name = strdup(attruname);
344 if ( ! (*(ea->ea_entries))[ea->ea_count].ea_name) {
345 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
348 (*(ea->ea_entries))[ea->ea_count].ea_namelen = strlen(attruname);
354 if (ea->ea_count == 0 && ea->ea_entries) {
355 /* We just allocated storage but had an error somewhere -> free storage*/
356 free(ea->ea_entries);
357 ea->ea_entries = NULL;
364 * Function: create_ea_header
366 * Purpose: create EA header file, only called from ea_open
370 * uname (r) filename for which we have to create a header
371 * ea (rw) ea handle with already allocated storage pointed to
374 * Returns: fd of open header file on success, -1 on error, errno semantics:
375 * EEXIST: open with O_CREAT | O_EXCL failed
379 * Creates EA header file and initialize ea->ea_data buffer.
380 * Possibe race condition with other afpd processes:
381 * we were called because header file didn't exist in eg. ea_open. We then
382 * try to create a file with O_CREAT | O_EXCL, but the whole process in not atomic.
383 * What do we do then? Someone else is in the process of creating the header too, but
384 * it might not have finished it. That means we cant just open, read and use it!
385 * We therefor currently just break with an error.
386 * On return the header file is still r/w locked.
388 static int create_ea_header(const char * restrict uname,
389 struct ea * restrict ea)
391 int fd = -1, err = 0;
394 if ((fd = open(uname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
395 LOG(log_error, logtype_afpd, "ea_create: open race condition with ea header for file: %s", uname);
400 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
401 LOG(log_error, logtype_afpd, "ea_create: lock race condition with ea header for file: %s", uname);
408 *(uint32_t *)ptr = htonl(EA_MAGIC);
410 *(uint16_t *)ptr = htons(EA_VERSION);
411 ptr += EA_VERSION_LEN;
412 *(uint16_t *)ptr = 0; /* count */
414 ea->ea_size = EA_HEADER_SIZE;
415 ea->ea_inited = EA_INITED;
428 * Purpose: write an EA to disk
432 * ea (r) struct ea handle
433 * attruname (r) EA name
434 * ibuf (r) buffer with EA content
435 * attrsize (r) size of EA
437 * Returns: 0 on success, -1 on error
441 * Creates/overwrites EA file.
444 static int write_ea(const struct ea * restrict ea,
445 const char * restrict attruname,
446 const char * restrict ibuf,
449 int fd = -1, ret = AFP_OK;
453 if ((eaname = ea_path(ea, attruname, 1)) == NULL) {
454 LOG(log_error, logtype_afpd, "write_ea('%s'): ea_path error", attruname);
458 LOG(log_maxdebug, logtype_afpd, "write_ea('%s')", eaname);
460 /* Check if it exists, remove if yes*/
461 if ((stat(eaname, &st)) == 0) {
462 if ((unlink(eaname)) != 0) {
464 return AFPERR_ACCESS;
470 if ((fd = open(eaname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
471 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
476 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
477 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
482 if (write(fd, ibuf, attrsize) != (ssize_t)attrsize) {
483 LOG(log_error, logtype_afpd, "write_ea('%s'): write: %s", eaname, strerror(errno));
490 close(fd); /* and unlock */
495 * Function: ea_delentry
497 * Purpose: delete one EA from ea->ea_entries[]
501 * ea (rw) pointer to struct ea
502 * attruname (r) EA name
504 * Returns: new number of EA entries, -1 on error
508 * Remove entry from ea->ea_entries[]. Decrement ea->ea_count.
509 * Marks it as unused just by freeing name and setting it to NULL.
510 * ea_close and pack_buffer must honor this.
512 static int ea_delentry(struct ea * restrict ea, const char * restrict attruname)
515 unsigned int count = 0;
517 if (ea->ea_count == 0) {
518 LOG(log_error, logtype_afpd, "ea_delentry('%s'): illegal ea_count of 0 on deletion");
522 while (count < ea->ea_count) {
523 /* search matching EA */
524 if (strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
525 free((*ea->ea_entries)[count].ea_name);
526 (*ea->ea_entries)[count].ea_name = NULL;
528 LOG(log_debug, logtype_afpd, "ea_delentry('%s'): deleted no %u/%u",
529 attruname, count + 1, ea->ea_count);
540 * Function: delete_ea_file
542 * Purpose: delete EA file from disk
546 * ea (r) struct ea handle
547 * attruname (r) EA name
549 * Returns: 0 on success, -1 on error
551 static int delete_ea_file(const struct ea * restrict ea, const char *eaname)
557 if ((eafile = ea_path(ea, eaname, 1)) == NULL) {
558 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): ea_path error", eaname);
562 /* Check if it exists, remove if yes*/
563 if ((stat(eafile, &st)) == 0) {
564 if ((unlink(eafile)) != 0) {
565 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): unlink: %s",
566 eafile, strerror(errno));
569 LOG(log_debug, logtype_afpd, "delete_ea_file('%s'): success", eafile);
575 /*************************************************************************************
576 * ea_path, ea_open and ea_close are only global so that dbd can call them
577 *************************************************************************************/
582 * Purpose: return name of ea header filename
587 * eaname (r) name of EA or NULL
588 * macname (r) if != 0 call mtoupath on eaname
590 * Returns: pointer to name in static buffer, NULL on error
594 * Calls ad_open, copies buffer, appends "::EA" and if supplied append eanme
595 * Files: "file" -> "file/.AppleDouble/file::EA"
596 * Dirs: "dir" -> "dir/.AppleDouble/.Parent::EA"
597 * "file" with EA "myEA" -> "file/.AppleDouble/file::EA:myEA"
599 char *ea_path(const struct ea * restrict ea, const char * restrict eaname, int macname)
602 static char pathbuf[MAXPATHLEN + 1];
604 /* get name of a adouble file from uname */
605 adname = ea->vol->ad_path(ea->filename, (ea->ea_flags & EA_DIR) ? ADFLAGS_DIR : 0);
606 /* copy it so we can work with it */
607 strlcpy(pathbuf, adname, MAXPATHLEN + 1);
609 strlcat(pathbuf, "::EA", MAXPATHLEN + 1);
612 strlcat(pathbuf, "::", MAXPATHLEN + 1);
614 if ((eaname = mtoupath(ea->vol, eaname)) == NULL)
616 strlcat(pathbuf, eaname, MAXPATHLEN + 1);
625 * Purpose: open EA header file, create if it doesnt exits and called with O_CREATE
629 * vol (r) current volume
630 * uname (r) filename for which we have to open a header
631 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
632 * EA_RDONLY: open read only
633 * EA_RDWR: open read/write
634 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
635 * ea (w) pointer to a struct ea that we fill
637 * Returns: 0 on success
638 * -1 on misc error with errno = EFAULT
639 * -2 if no EA header exists with errno = ENOENT
643 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
644 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
645 * file is either read or write locked depending on the open flags.
646 * When you're done with struct ea you must call ea_close on it.
648 int ea_open(const struct vol * restrict vol,
649 const char * restrict uname,
651 struct ea * restrict ea)
657 /* Enforce usage rules! */
658 if ( ! (eaflags & (EA_RDONLY | EA_RDWR))) {
659 LOG(log_error, logtype_afpd, "ea_open: called without EA_RDONLY | EA_RDWR", uname);
663 /* Set it all to 0 */
664 memset(ea, 0, sizeof(struct ea));
666 ea->vol = vol; /* ea_close needs it */
667 ea->ea_flags = eaflags;
668 ea->dirfd = -1; /* no *at (cf openat) semantics by default */
670 /* Dont care for errors, eg when removing the file is already gone */
671 if (!stat(uname, &st) && S_ISDIR(st.st_mode))
672 ea->ea_flags |= EA_DIR;
674 if ( ! (ea->filename = strdup(uname))) {
675 LOG(log_error, logtype_afpd, "ea_open: OOM");
679 eaname = ea_path(ea, NULL, 0);
680 LOG(log_maxdebug, logtype_afpd, "ea_open: ea_path: %s", eaname);
682 /* Check if it exists, if not create it if EA_CREATE is in eaflags */
683 if ((stat(eaname, &st)) != 0) {
684 if (errno == ENOENT) {
686 /* It doesnt exist */
688 if ( ! (eaflags & EA_CREATE)) {
689 /* creation was not requested, so return with error */
694 /* Now create a header file */
696 /* malloc buffer for minimal on disk data */
697 ea->ea_data = malloc(EA_HEADER_SIZE);
699 LOG(log_error, logtype_afpd, "ea_open: OOM");
705 ea->ea_fd = create_ea_header(eaname, ea);
706 if (ea->ea_fd == -1) {
713 } else {/* errno != ENOENT */
719 /* header file exists, so read and parse it */
721 /* malloc buffer where we read disk file into */
722 if (st.st_size < EA_HEADER_SIZE) {
723 LOG(log_error, logtype_afpd, "ea_open('%s'): bogus EA header file", eaname);
727 ea->ea_size = st.st_size;
728 ea->ea_data = malloc(st.st_size);
730 LOG(log_error, logtype_afpd, "ea_open: OOM");
735 /* Now lock, open and read header file from disk */
736 if ((ea->ea_fd = open(eaname, (ea->ea_flags & EA_RDWR) ? O_RDWR : O_RDONLY)) == -1) {
737 LOG(log_error, logtype_afpd, "ea_open('%s'): error: %s", eaname, strerror(errno));
743 if (ea->ea_flags & EA_RDONLY) {
745 if ((read_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
746 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
750 } else { /* EA_RDWR */
752 if ((write_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
753 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
760 if (read(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
761 LOG(log_error, logtype_afpd, "ea_open: short read on header: %s", eaname);
766 if ((unpack_header(ea)) != 0) {
767 LOG(log_error, logtype_afpd, "ea_open: error unpacking header for: %s", eaname);
775 ea->ea_inited = EA_INITED;
778 errno = EFAULT; /* force some errno distinguishable from ENOENT */
796 * Function: ea_openat
798 * Purpose: openat like wrapper for ea_open, takes a additional file descriptor
802 * vol (r) current volume
803 * sfd (r) openat like file descriptor
804 * uname (r) filename for which we have to open a header
805 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
806 * EA_RDONLY: open read only
807 * EA_RDWR: open read/write
808 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
809 * ea (w) pointer to a struct ea that we fill
811 * Returns: 0 on success
812 * -1 on misc error with errno = EFAULT
813 * -2 if no EA header exists with errno = ENOENT
817 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
818 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
819 * file is either read or write locked depending on the open flags.
820 * When you're done with struct ea you must call ea_close on it.
822 int ea_openat(const struct vol * restrict vol,
824 const char * restrict uname,
826 struct ea * restrict ea)
832 if (((cwdfd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
838 ret = ea_open(vol, uname, eaflags, ea);
842 if (fchdir(cwdfd) != 0) {
843 LOG(log_error, logtype_afpd, "ea_openat: cant chdir back, exiting");
860 * Purpose: flushes and closes an ea handle
864 * ea (rw) pointer to ea handle
866 * Returns: 0 on success, -1 on error
870 * Flushes and then closes and frees all resouces held by ea handle.
871 * Pack data in ea into ea_data, then write ea_data to disk
873 int ea_close(struct ea * restrict ea)
876 unsigned int count = 0;
880 LOG(log_debug, logtype_afpd, "ea_close('%s')", ea->filename);
882 if (ea->ea_inited != EA_INITED) {
883 LOG(log_warning, logtype_afpd, "ea_close('%s'): non initialized ea", ea->filename);
887 /* pack header and write it to disk if it was opened EA_RDWR*/
888 if (ea->ea_flags & EA_RDWR) {
889 if ((pack_header(ea)) != 0) {
890 LOG(log_error, logtype_afpd, "ea_close: pack header");
893 if (ea->ea_count == 0) {
894 /* Check if EA header exists and remove it */
895 eaname = ea_path(ea, NULL, 0);
896 if ((lstatat(ea->dirfd, eaname, &st)) == 0) {
897 if ((netatalk_unlinkat(ea->dirfd, eaname)) != 0) {
898 LOG(log_error, logtype_afpd, "ea_close('%s'): unlink: %s",
899 eaname, strerror(errno));
903 LOG(log_debug, logtype_afpd, "ea_close(unlink '%s'): success", eaname);
906 if (errno != ENOENT) {
907 LOG(log_error, logtype_afpd, "ea_close('%s'): stat: %s",
908 eaname, strerror(errno));
912 } else { /* ea->ea_count > 0 */
913 if ((lseek(ea->ea_fd, 0, SEEK_SET)) == -1) {
914 LOG(log_error, logtype_afpd, "ea_close: lseek: %s", strerror(errno));
919 if ((ftruncate(ea->ea_fd, 0)) == -1) {
920 LOG(log_error, logtype_afpd, "ea_close: ftruncate: %s", strerror(errno));
925 if (write(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
926 LOG(log_error, logtype_afpd, "ea_close: write: %s", strerror(errno));
935 while(count < ea->ea_count) {
936 if ( (*ea->ea_entries)[count].ea_name ) {
937 free((*ea->ea_entries)[count].ea_name);
938 (*ea->ea_entries)[count].ea_name = NULL;
949 if (ea->ea_entries) {
950 free(ea->ea_entries);
951 ea->ea_entries = NULL;
958 if (ea->ea_fd != -1) {
959 close(ea->ea_fd); /* also releases the fcntl lock */
968 /************************************************************************************
969 * VFS funcs called from afp_ea* funcs
970 ************************************************************************************/
973 * Function: get_easize
975 * Purpose: get size of an EA
979 * vol (r) current volume
980 * rbuf (w) DSI reply buffer
981 * rbuflen (rw) current length of data in reply buffer
983 * oflag (r) link and create flag
984 * attruname (r) name of attribute
986 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
990 * Copies EA size into rbuf in network order. Increments *rbuflen +4.
992 int get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
994 int ret = AFPERR_MISC;
995 unsigned int count = 0;
999 LOG(log_debug, logtype_afpd, "get_easize: file: %s", uname);
1001 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1002 if (errno != ENOENT)
1003 LOG(log_error, logtype_afpd, "get_easize: error calling ea_open for file: %s", uname);
1010 while (count < ea.ea_count) {
1011 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1012 uint32 = htonl((*ea.ea_entries)[count].ea_size);
1013 memcpy(rbuf, &uint32, 4);
1017 LOG(log_debug, logtype_afpd, "get_easize(\"%s\"): size: %u",
1018 attruname, (*ea.ea_entries)[count].ea_size);
1024 if ((ea_close(&ea)) != 0) {
1025 LOG(log_error, logtype_afpd, "get_easize: error closing ea handle for file: %s", uname);
1033 * Function: get_eacontent
1035 * Purpose: copy EA into rbuf
1039 * vol (r) current volume
1040 * rbuf (w) DSI reply buffer
1041 * rbuflen (rw) current length of data in reply buffer
1042 * uname (r) filename
1043 * oflag (r) link and create flag
1044 * attruname (r) name of attribute
1045 * maxreply (r) maximum EA size as of current specs/real-life
1047 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1051 * Copies EA into rbuf. Increments *rbuflen accordingly.
1053 int get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
1055 int ret = AFPERR_MISC, fd = -1;
1056 unsigned int count = 0;
1062 LOG(log_debug, logtype_afpd, "get_eacontent('%s/%s')", uname, attruname);
1064 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1065 if (errno != ENOENT)
1066 LOG(log_error, logtype_afpd, "get_eacontent('%s'): ea_open error", uname);
1072 while (count < ea.ea_count) {
1073 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1074 if ( (eafile = ea_path(&ea, attruname, 1)) == NULL) {
1079 if ((fd = open(eafile, O_RDONLY)) == -1) {
1080 LOG(log_error, logtype_afpd, "get_eacontent('%s'): open error: %s", uname, strerror(errno));
1085 /* Check how much the client wants, give him what we think is right */
1086 maxreply -= MAX_REPLY_EXTRA_BYTES;
1087 if (maxreply > MAX_EA_SIZE)
1088 maxreply = MAX_EA_SIZE;
1089 toread = (maxreply < (*ea.ea_entries)[count].ea_size) ? maxreply : (*ea.ea_entries)[count].ea_size;
1090 LOG(log_debug, logtype_afpd, "get_eacontent('%s'): sending %u bytes", attruname, toread);
1092 /* Put length of EA data in reply buffer */
1093 uint32 = htonl(toread);
1094 memcpy(rbuf, &uint32, 4);
1098 if (read(fd, rbuf, toread) != (ssize_t)toread) {
1099 LOG(log_error, logtype_afpd, "get_eacontent('%s/%s'): short read", uname, attruname);
1112 if ((ea_close(&ea)) != 0) {
1113 LOG(log_error, logtype_afpd, "get_eacontent('%s'): error closing ea handle", uname);
1122 * Function: list_eas
1124 * Purpose: copy names of EAs into attrnamebuf
1128 * vol (r) current volume
1129 * attrnamebuf (w) store names a consecutive C strings here
1130 * buflen (rw) length of names in attrnamebuf
1131 * uname (r) filename
1132 * oflag (r) link and create flag
1134 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1138 * Copies names of all EAs of uname as consecutive C strings into rbuf.
1139 * Increments *buflen accordingly.
1141 int list_eas(VFS_FUNC_ARGS_EA_LIST)
1143 unsigned int count = 0;
1144 int attrbuflen = *buflen, ret = AFP_OK, len;
1145 char *buf = attrnamebuf;
1148 LOG(log_debug, logtype_afpd, "list_eas: file: %s", uname);
1150 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1151 if (errno != ENOENT) {
1152 LOG(log_error, logtype_afpd, "list_eas: error calling ea_open for file: %s", uname);
1159 while (count < ea.ea_count) {
1160 /* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
1161 if ( ( len = convert_string(vol->v_volcharset,
1163 (*ea.ea_entries)[count].ea_name,
1164 (*ea.ea_entries)[count].ea_namelen,
1172 /* convert_string didn't 0-terminate */
1173 attrnamebuf[attrbuflen + 255] = 0;
1175 LOG(log_debug7, logtype_afpd, "list_eas(%s): EA: %s",
1176 uname, (*ea.ea_entries)[count].ea_name);
1178 attrbuflen += len + 1;
1179 if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
1180 /* Next EA name could overflow, so bail out with error.
1181 FIXME: evantually malloc/memcpy/realloc whatever.
1183 LOG(log_warning, logtype_afpd, "list_eas(%s): running out of buffer for EA names", uname);
1191 *buflen = attrbuflen;
1193 if ((ea_close(&ea)) != 0) {
1194 LOG(log_error, logtype_afpd, "list_eas: error closing ea handle for file: %s", uname);
1204 * Purpose: set a Solaris native EA
1208 * vol (r) current volume
1209 * uname (r) filename
1210 * attruname (r) EA name
1211 * ibuf (r) buffer with EA content
1212 * attrsize (r) length EA in ibuf
1213 * oflag (r) link and create flag
1215 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1219 * Copies names of all EAs of uname as consecutive C strings into rbuf.
1220 * Increments *rbuflen accordingly.
1222 int set_ea(VFS_FUNC_ARGS_EA_SET)
1227 LOG(log_debug, logtype_afpd, "set_ea: file: %s", uname);
1229 if ((ea_open(vol, uname, EA_CREATE | EA_RDWR, &ea)) != 0) {
1230 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_open error", uname);
1234 if ((ea_addentry(&ea, attruname, attrsize, oflag)) == -1) {
1235 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_addentry error", uname);
1240 if ((write_ea(&ea, attruname, ibuf, attrsize)) != 0) {
1241 LOG(log_error, logtype_afpd, "set_ea('%s'): write_ea error", uname);
1247 if ((ea_close(&ea)) != 0) {
1248 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_close error", uname);
1257 * Function: remove_ea
1259 * Purpose: remove a EA from a file
1263 * vol (r) current volume
1264 * uname (r) filename
1265 * attruname (r) EA name
1266 * oflag (r) link and create flag
1268 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1272 * Removes EA attruname from file uname.
1274 int remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
1279 LOG(log_debug, logtype_afpd, "remove_ea('%s/%s')", uname, attruname);
1281 if ((ea_open(vol, uname, EA_RDWR, &ea)) != 0) {
1282 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_open error", uname);
1286 if ((ea_delentry(&ea, attruname)) == -1) {
1287 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_delentry error", uname);
1292 if ((delete_ea_file(&ea, attruname)) != 0) {
1293 LOG(log_error, logtype_afpd, "remove_ea('%s'): delete_ea error", uname);
1299 if ((ea_close(&ea)) != 0) {
1300 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_close error", uname);
1308 /******************************************************************************************
1309 * EA VFS funcs that deal with file/dir cp/mv/rm
1310 ******************************************************************************************/
1312 int ea_deletefile(VFS_FUNC_ARGS_DELETEFILE)
1314 unsigned int count = 0;
1319 LOG(log_debug, logtype_afpd, "ea_deletefile('%s')", file);
1322 if ((ea_openat(vol, dirfd, file, EA_RDWR, &ea)) != 0) {
1323 if (errno == ENOENT)
1324 /* no EA files, nothing to do */
1327 LOG(log_error, logtype_afpd, "ea_deletefile('%s'): error calling ea_open", file);
1333 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
1339 while (count < ea.ea_count) {
1340 if ((delete_ea_file(&ea, (*ea.ea_entries)[count].ea_name)) != 0) {
1344 free((*ea.ea_entries)[count].ea_name);
1345 (*ea.ea_entries)[count].ea_name = NULL;
1349 /* ea_close removes the EA header file for us because all names are NULL */
1350 if ((ea_close(&ea)) != 0) {
1351 LOG(log_error, logtype_afpd, "ea_deletefile('%s'): error closing ea handle", file);
1355 if (dirfd != -1 && fchdir(cwd) != 0) {
1356 LOG(log_error, logtype_afpd, "ea_deletefile: cant chdir back. exit!");
1367 int ea_renamefile(VFS_FUNC_ARGS_RENAMEFILE)
1369 unsigned int count = 0;
1372 char srceapath[ MAXPATHLEN + 1];
1379 LOG(log_debug, logtype_afpd, "ea_renamefile('%s'/'%s')", src, dst);
1383 if ((ea_openat(vol, dirfd, src, EA_RDWR, &srcea)) != 0) {
1384 if (errno == ENOENT)
1385 /* no EA files, nothing to do */
1388 LOG(log_error, logtype_afpd, "ea_renamefile('%s'/'%s'): ea_open error: '%s'", src, dst, src);
1393 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1394 if (errno == ENOENT) {
1395 /* Possibly the .AppleDouble folder didn't exist, we create it and try again */
1396 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1397 if ((ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) != 0) {
1398 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): ad_open error: '%s'", src, dst, dst);
1402 ad_close(&ad, ADFLAGS_HF);
1403 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1410 /* Loop through all EAs: */
1411 while (count < srcea.ea_count) {
1413 eaname = (*srcea.ea_entries)[count].ea_name;
1414 easize = (*srcea.ea_entries)[count].ea_size;
1416 /* Build src and dst paths for rename() */
1417 if ((eapath = ea_path(&srcea, eaname, 1)) == NULL) {
1421 strcpy(srceapath, eapath);
1422 if ((eapath = ea_path(&dstea, eaname, 1)) == NULL) {
1427 LOG(log_maxdebug, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1428 src, dst, srceapath, eapath);
1430 /* Add EA to dstea */
1431 if ((ea_addentry(&dstea, eaname, easize, 0)) == -1) {
1432 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1433 src, dst, srceapath, eapath);
1438 /* Remove EA entry from srcea */
1439 if ((ea_delentry(&srcea, eaname)) == -1) {
1440 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1441 src, dst, srceapath, eapath);
1442 ea_delentry(&dstea, eaname);
1447 /* Now rename the EA */
1448 if ((unix_rename(dirfd, srceapath, -1, eapath)) < 0) {
1449 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1450 src, dst, srceapath, eapath);
1465 int ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
1467 unsigned int count = 0;
1470 char srceapath[ MAXPATHLEN + 1];
1477 LOG(log_debug, logtype_afpd, "ea_copyfile('%s'/'%s')", src, dst);
1480 if ((ea_openat(vol, sfd, src, EA_RDWR, &srcea)) != 0) {
1481 if (errno == ENOENT)
1482 /* no EA files, nothing to do */
1485 LOG(log_error, logtype_afpd, "ea_copyfile('%s'/'%s'): ea_open error: '%s'", src, dst, src);
1490 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1491 if (errno == ENOENT) {
1492 /* Possibly the .AppleDouble folder didn't exist, we create it and try again */
1493 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1494 if ((ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) != 0) {
1495 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): ad_open error: '%s'", src, dst, dst);
1499 ad_close(&ad, ADFLAGS_HF);
1500 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1507 /* Loop through all EAs: */
1508 while (count < srcea.ea_count) {
1510 eaname = (*srcea.ea_entries)[count].ea_name;
1511 easize = (*srcea.ea_entries)[count].ea_size;
1513 /* Build src and dst paths for copy_file() */
1514 if ((eapath = ea_path(&srcea, eaname, 1)) == NULL) {
1518 strcpy(srceapath, eapath);
1519 if ((eapath = ea_path(&dstea, eaname, 1)) == NULL) {
1524 LOG(log_maxdebug, logtype_afpd, "ea_copyfile('%s/%s'): copying EA '%s' to '%s'",
1525 src, dst, srceapath, eapath);
1527 /* Add EA to dstea */
1528 if ((ea_addentry(&dstea, eaname, easize, 0)) == -1) {
1529 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): ea_addentry('%s') error",
1535 /* Now copy the EA */
1536 if ((copy_file(sfd, srceapath, eapath, (0666 & ~vol->v_umask))) < 0) {
1537 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): copying EA '%s' to '%s'",
1538 src, dst, srceapath, eapath);
1552 int ea_chown(VFS_FUNC_ARGS_CHOWN)
1555 unsigned int count = 0;
1560 LOG(log_debug, logtype_afpd, "ea_chown('%s')", path);
1562 if ((ea_open(vol, path, EA_RDWR, &ea)) != 0) {
1563 if (errno == ENOENT)
1564 /* no EA files, nothing to do */
1567 LOG(log_error, logtype_afpd, "ea_chown('%s'): error calling ea_open", path);
1572 if ((lchown(ea_path(&ea, NULL, 0), uid, gid)) != 0) {
1576 ret = AFPERR_ACCESS;
1584 while (count < ea.ea_count) {
1585 if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 1)) == NULL) {
1589 if ((lchown(eaname, uid, gid)) != 0) {
1593 ret = AFPERR_ACCESS;
1606 if ((ea_close(&ea)) != 0) {
1607 LOG(log_error, logtype_afpd, "ea_chown('%s'): error closing ea handle", path);
1614 int ea_chmod_file(VFS_FUNC_ARGS_SETFILEMODE)
1617 unsigned int count = 0;
1622 LOG(log_debug, logtype_afpd, "ea_chmod_file('%s')", name);
1624 if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
1625 if (errno == ENOENT)
1626 /* no EA files, nothing to do */
1632 /* Set mode on EA header file */
1633 if ((setfilmode(ea_path(&ea, NULL, 0), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
1634 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", ea_path(&ea, NULL, 0), strerror(errno));
1638 ret = AFPERR_ACCESS;
1646 /* Set mode on EA files */
1647 while (count < ea.ea_count) {
1648 if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 1)) == NULL) {
1652 if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
1653 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", eaname, strerror(errno));
1657 ret = AFPERR_ACCESS;
1670 if ((ea_close(&ea)) != 0) {
1671 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): error closing ea handle", name);
1678 int ea_chmod_dir(VFS_FUNC_ARGS_SETDIRUNIXMODE)
1682 unsigned int count = 0;
1685 const char *eaname_safe = NULL;
1688 LOG(log_debug, logtype_afpd, "ea_chmod_dir('%s')", name);
1689 /* .AppleDouble already might be inaccesible, so we must run as id 0 */
1692 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): seteuid: %s", name, strerror(errno));
1697 if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
1698 /* ENOENT --> no EA files, nothing to do */
1699 if (errno != ENOENT)
1701 if (seteuid(uid) < 0) {
1702 LOG(log_error, logtype_afpd, "can't seteuid back: %s", strerror(errno));
1708 /* Set mode on EA header */
1709 if ((setfilmode(ea_path(&ea, NULL, 0), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
1710 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", ea_path(&ea, NULL, 0), strerror(errno));
1714 ret = AFPERR_ACCESS;
1722 /* Set mode on EA files */
1723 while (count < ea.ea_count) {
1724 eaname = (*ea.ea_entries)[count].ea_name;
1726 * Be careful with EA names from the EA header!
1727 * Eg NFS users might have access to them, can inject paths using ../ or /.....
1729 * Until the EA code escapes / in EA name requests from the client, these therefor wont work.
1731 if ((eaname_safe = strrchr(eaname, '/'))) {
1732 LOG(log_warning, logtype_afpd, "ea_chmod_dir('%s'): contains a slash", eaname);
1733 eaname = eaname_safe;
1735 if ((eaname = ea_path(&ea, eaname, 1)) == NULL) {
1739 if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
1740 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", eaname, strerror(errno));
1744 ret = AFPERR_ACCESS;
1757 if (seteuid(uid) < 0) {
1758 LOG(log_error, logtype_afpd, "can't seteuid back: %s", strerror(errno));
1762 if ((ea_close(&ea)) != 0) {
1763 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): error closing ea handle", name);