2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
24 #include <sys/types.h>
28 #include <arpa/inet.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>
38 #include <atalk/compat.h>
41 * Store Extended Attributes inside .AppleDouble folders as follows:
43 * filename "fileWithEAs" with EAs "testEA1" and "testEA2"
45 * - create header with with the format struct adouble_ea_ondisk, the file is written to
46 * ".AppleDouble/fileWithEAs::EA"
47 * - store EAs in files "fileWithEAs::EA::testEA1" and "fileWithEAs::EA::testEA2"
51 * Build mode for EA header from file mode
53 static inline mode_t ea_header_mode(mode_t mode)
55 /* Same as ad_hf_mode(mode) */
56 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
57 /* Owner must be able to open, read and w-lock it, in order to chmod from eg 0000 -> 0xxxx*/
58 mode |= S_IRUSR | S_IWUSR;
63 * Build mode for EA file from file mode
65 static inline mode_t ea_mode(mode_t mode)
67 /* Same as ad_hf_mode(mode) */
68 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
73 Taken form afpd/desktop.c
75 static char *mtoupath(const struct vol *vol, const char *mpath)
77 static char upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
82 uint16_t flags = CONV_ESCAPEHEX | CONV_ALLOW_COLON;
87 if ( *mpath == '\0' ) {
97 if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
100 m, inplen, u, outlen, &flags)) ) {
109 * Function: unpack_header
111 * Purpose: unpack and verify header file data buffer at ea->ea_data into struct ea
115 * ea (rw) handle to struct ea
117 * Returns: 0 on success, -1 on error
121 * Verifies magic and version.
123 static int unpack_header(struct ea * restrict ea)
126 unsigned int count = 0;
130 /* Check magic and version */
132 if (*(uint32_t *)buf != htonl(EA_MAGIC)) {
133 LOG(log_error, logtype_afpd, "unpack_header: wrong magic 0x%08x", *(uint32_t *)buf);
138 if (*(uint16_t *)buf != htons(EA_VERSION)) {
139 LOG(log_error, logtype_afpd, "unpack_header: wrong version 0x%04x", *(uint16_t *)buf);
146 ea->ea_count = ntohs(*(uint16_t *)buf);
147 LOG(log_debug, logtype_afpd, "unpack_header: number of EAs: %u", ea->ea_count);
150 if (ea->ea_count == 0)
153 /* Allocate storage for the ea_entries array */
154 ea->ea_entries = malloc(sizeof(struct ea_entry) * ea->ea_count);
155 if ( ! ea->ea_entries) {
156 LOG(log_error, logtype_afpd, "unpack_header: OOM");
161 buf = ea->ea_data + EA_HEADER_SIZE;
162 while (count < ea->ea_count) {
163 memcpy(&uint32, buf, 4); /* EA size */
165 (*(ea->ea_entries))[count].ea_size = ntohl(uint32);
166 (*(ea->ea_entries))[count].ea_name = strdup(buf);
167 if (! (*(ea->ea_entries))[count].ea_name) {
168 LOG(log_error, logtype_afpd, "unpack_header: OOM");
172 (*(ea->ea_entries))[count].ea_namelen = strlen((*(ea->ea_entries))[count].ea_name);
173 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
175 LOG(log_maxdebug, logtype_afpd, "unpack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
176 (*(ea->ea_entries))[count].ea_name,
177 (*(ea->ea_entries))[count].ea_size,
178 (*(ea->ea_entries))[count].ea_namelen);
188 * Function: pack_header
190 * Purpose: pack everything from struct ea into buffer at ea->ea_data
194 * ea (rw) handle to struct ea
196 * Returns: 0 on success, -1 on error
200 * adjust ea->ea_count in case an ea entry deletetion is detected
202 static int pack_header(struct ea * restrict ea)
204 unsigned int count = 0, eacount = 0;
207 size_t bufsize = EA_HEADER_SIZE;
209 char *buf = ea->ea_data + EA_HEADER_SIZE;
211 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
212 ea->filename, ea->ea_count, ea->ea_size);
214 if (ea->ea_count == 0)
215 /* nothing to do, magic, version and count are still valid in buffer */
218 while(count < ea->ea_count) { /* the names */
219 /* Check if its a deleted entry */
220 if ( ! ((*ea->ea_entries)[count].ea_name)) {
225 bufsize += (*(ea->ea_entries))[count].ea_namelen + 1;
230 bufsize += (eacount * 4); /* header + ea_size for each EA */
231 if (bufsize > ea->ea_size) {
232 /* we must realloc */
233 if ( ! (buf = realloc(ea->ea_data, bufsize)) ) {
234 LOG(log_error, logtype_afpd, "pack_header: OOM");
239 ea->ea_size = bufsize;
242 uint16 = htons(eacount);
243 memcpy(ea->ea_data + EA_COUNT_OFF, &uint16, 2);
246 buf = ea->ea_data + EA_HEADER_SIZE;
247 while (count < ea->ea_count) {
248 /* Check if its a deleted entry */
249 if ( ! ((*ea->ea_entries)[count].ea_name)) {
255 uint32 = htonl((*(ea->ea_entries))[count].ea_size);
256 memcpy(buf, &uint32, 4);
259 /* Second: EA name as C-string */
260 strcpy(buf, (*(ea->ea_entries))[count].ea_name);
261 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
263 LOG(log_maxdebug, logtype_afpd, "pack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
264 (*(ea->ea_entries))[count].ea_name,
265 (*(ea->ea_entries))[count].ea_size,
266 (*(ea->ea_entries))[count].ea_namelen);
271 ea->ea_count = eacount;
273 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
274 ea->filename, ea->ea_count, ea->ea_size);
280 * Function: ea_addentry
282 * Purpose: add one EA into ea->ea_entries[]
286 * ea (rw) pointer to struct ea
287 * attruname (r) name of EA
288 * attrsize (r) size of ea
289 * bitmap (r) bitmap from FP func
291 * Returns: new number of EA entries, -1 on error
295 * Grow array ea->ea_entries[]. If ea->ea_entries is still NULL, start allocating.
296 * Otherwise realloc and put entry at the end. Increments ea->ea_count.
298 static int ea_addentry(struct ea * restrict ea,
299 const char * restrict attruname,
304 unsigned int count = 0;
307 /* First check if an EA of the requested name already exist */
308 if (ea->ea_count > 0) {
309 while (count < ea->ea_count) {
310 if (strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
312 LOG(log_debug, logtype_afpd, "ea_addentry('%s', bitmap:0x%x): exists", attruname, bitmap);
313 if (bitmap & kXAttrCreate)
314 /* its like O_CREAT|O_EXCL -> fail */
316 (*(ea->ea_entries))[count].ea_size = attrsize;
323 if ((bitmap & kXAttrReplace) && ! ea_existed)
324 /* replace was requested, but EA didn't exist */
327 if (ea->ea_count == 0) {
328 ea->ea_entries = malloc(sizeof(struct ea_entry));
329 if ( ! ea->ea_entries) {
330 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
333 } else if (! ea_existed) {
334 tmprealloc = realloc(ea->ea_entries, sizeof(struct ea_entry) * (ea->ea_count + 1));
336 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
339 ea->ea_entries = tmprealloc;
342 /* We've grown the array, now store the entry */
343 (*(ea->ea_entries))[ea->ea_count].ea_size = attrsize;
344 (*(ea->ea_entries))[ea->ea_count].ea_name = strdup(attruname);
345 if ( ! (*(ea->ea_entries))[ea->ea_count].ea_name) {
346 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
349 (*(ea->ea_entries))[ea->ea_count].ea_namelen = strlen(attruname);
355 if (ea->ea_count == 0 && ea->ea_entries) {
356 /* We just allocated storage but had an error somewhere -> free storage*/
357 free(ea->ea_entries);
358 ea->ea_entries = NULL;
365 * Function: create_ea_header
367 * Purpose: create EA header file, only called from ea_open
371 * uname (r) filename for which we have to create a header
372 * ea (rw) ea handle with already allocated storage pointed to
375 * Returns: fd of open header file on success, -1 on error, errno semantics:
376 * EEXIST: open with O_CREAT | O_EXCL failed
380 * Creates EA header file and initialize ea->ea_data buffer.
381 * Possibe race condition with other afpd processes:
382 * we were called because header file didn't exist in eg. ea_open. We then
383 * try to create a file with O_CREAT | O_EXCL, but the whole process in not atomic.
384 * What do we do then? Someone else is in the process of creating the header too, but
385 * it might not have finished it. That means we cant just open, read and use it!
386 * We therefor currently just break with an error.
387 * On return the header file is still r/w locked.
389 static int create_ea_header(const char * restrict uname,
390 struct ea * restrict ea)
392 int fd = -1, err = 0;
395 if ((fd = open(uname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
396 LOG(log_error, logtype_afpd, "ea_create: open race condition with ea header for file: %s", uname);
401 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
402 LOG(log_error, logtype_afpd, "ea_create: lock race condition with ea header for file: %s", uname);
409 *(uint32_t *)ptr = htonl(EA_MAGIC);
411 *(uint16_t *)ptr = htons(EA_VERSION);
412 ptr += EA_VERSION_LEN;
413 *(uint16_t *)ptr = 0; /* count */
415 ea->ea_size = EA_HEADER_SIZE;
416 ea->ea_inited = EA_INITED;
429 * Purpose: write an EA to disk
433 * ea (r) struct ea handle
434 * attruname (r) EA name
435 * ibuf (r) buffer with EA content
436 * attrsize (r) size of EA
438 * Returns: 0 on success, -1 on error
442 * Creates/overwrites EA file.
445 static int write_ea(const struct ea * restrict ea,
446 const char * restrict attruname,
447 const char * restrict ibuf,
450 int fd = -1, ret = AFP_OK;
454 if ((eaname = ea_path(ea, attruname, 1)) == NULL) {
455 LOG(log_error, logtype_afpd, "write_ea('%s'): ea_path error", attruname);
459 LOG(log_maxdebug, logtype_afpd, "write_ea('%s')", eaname);
461 /* Check if it exists, remove if yes*/
462 if ((stat(eaname, &st)) == 0) {
463 if ((unlink(eaname)) != 0) {
465 return AFPERR_ACCESS;
471 if ((fd = open(eaname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
472 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
477 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
478 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
483 if (write(fd, ibuf, attrsize) != (ssize_t)attrsize) {
484 LOG(log_error, logtype_afpd, "write_ea('%s'): write: %s", eaname, strerror(errno));
491 close(fd); /* and unlock */
496 * Function: ea_delentry
498 * Purpose: delete one EA from ea->ea_entries[]
502 * ea (rw) pointer to struct ea
503 * attruname (r) EA name
505 * Returns: new number of EA entries, -1 on error
509 * Remove entry from ea->ea_entries[]. Decrement ea->ea_count.
510 * Marks it as unused just by freeing name and setting it to NULL.
511 * ea_close and pack_buffer must honor this.
513 static int ea_delentry(struct ea * restrict ea, const char * restrict attruname)
516 unsigned int count = 0;
518 if (ea->ea_count == 0) {
519 LOG(log_error, logtype_afpd, "ea_delentry('%s'): illegal ea_count of 0 on deletion",
524 while (count < ea->ea_count) {
525 /* search matching EA */
526 if ((*ea->ea_entries)[count].ea_name &&
527 strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
528 free((*ea->ea_entries)[count].ea_name);
529 (*ea->ea_entries)[count].ea_name = NULL;
531 LOG(log_debug, logtype_afpd, "ea_delentry('%s'): deleted no %u/%u",
532 attruname, count + 1, ea->ea_count);
543 * Function: delete_ea_file
545 * Purpose: delete EA file from disk
549 * ea (r) struct ea handle
550 * attruname (r) EA name
552 * Returns: 0 on success, -1 on error
554 static int delete_ea_file(const struct ea * restrict ea, const char *eaname)
560 if ((eafile = ea_path(ea, eaname, 1)) == NULL) {
561 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): ea_path error", eaname);
565 /* Check if it exists, remove if yes*/
566 if ((stat(eafile, &st)) == 0) {
567 if ((unlink(eafile)) != 0) {
568 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): unlink: %s",
569 eafile, strerror(errno));
572 LOG(log_debug, logtype_afpd, "delete_ea_file('%s'): success", eafile);
578 /*************************************************************************************
579 * ea_path, ea_open and ea_close are only global so that dbd can call them
580 *************************************************************************************/
585 * Purpose: return name of ea header filename
590 * eaname (r) name of EA or NULL
591 * macname (r) if != 0 call mtoupath on eaname
593 * Returns: pointer to name in static buffer, NULL on error
597 * Calls ad_open, copies buffer, appends "::EA" and if supplied append eanme
598 * Files: "file" -> "file/.AppleDouble/file::EA"
599 * Dirs: "dir" -> "dir/.AppleDouble/.Parent::EA"
600 * "file" with EA "myEA" -> "file/.AppleDouble/file::EA:myEA"
602 char *ea_path(const struct ea * restrict ea, const char * restrict eaname, int macname)
605 static char pathbuf[MAXPATHLEN + 1];
607 /* get name of a adouble file from uname */
608 adname = ea->vol->ad_path(ea->filename, (ea->ea_flags & EA_DIR) ? ADFLAGS_DIR : 0);
609 /* copy it so we can work with it */
610 strlcpy(pathbuf, adname, MAXPATHLEN + 1);
612 strlcat(pathbuf, "::EA", MAXPATHLEN + 1);
615 strlcat(pathbuf, "::", MAXPATHLEN + 1);
617 if ((eaname = mtoupath(ea->vol, eaname)) == NULL)
619 strlcat(pathbuf, eaname, MAXPATHLEN + 1);
628 * Purpose: open EA header file, create if it doesnt exits and called with O_CREATE
632 * vol (r) current volume
633 * uname (r) filename for which we have to open a header
634 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
635 * EA_RDONLY: open read only
636 * EA_RDWR: open read/write
637 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
638 * ea (w) pointer to a struct ea that we fill
640 * Returns: 0 on success
641 * -1 on misc error with errno = EFAULT
642 * -2 if no EA header exists with errno = ENOENT
646 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
647 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
648 * file is either read or write locked depending on the open flags.
649 * When you're done with struct ea you must call ea_close on it.
651 int ea_open(const struct vol * restrict vol,
652 const char * restrict uname,
654 struct ea * restrict ea)
660 /* Enforce usage rules! */
661 if ( ! (eaflags & (EA_RDONLY | EA_RDWR))) {
662 LOG(log_error, logtype_afpd, "ea_open: called without EA_RDONLY | EA_RDWR", uname);
666 /* Set it all to 0 */
667 memset(ea, 0, sizeof(struct ea));
669 ea->vol = vol; /* ea_close needs it */
670 ea->ea_flags = eaflags;
671 ea->dirfd = -1; /* no *at (cf openat) semantics by default */
673 /* Dont care for errors, eg when removing the file is already gone */
674 if (!stat(uname, &st) && S_ISDIR(st.st_mode))
675 ea->ea_flags |= EA_DIR;
677 if ( ! (ea->filename = strdup(uname))) {
678 LOG(log_error, logtype_afpd, "ea_open: OOM");
682 eaname = ea_path(ea, NULL, 0);
683 LOG(log_maxdebug, logtype_afpd, "ea_open: ea_path: %s", eaname);
685 /* Check if it exists, if not create it if EA_CREATE is in eaflags */
686 if ((stat(eaname, &st)) != 0) {
687 if (errno == ENOENT) {
689 /* It doesnt exist */
691 if ( ! (eaflags & EA_CREATE)) {
692 /* creation was not requested, so return with error */
697 /* Now create a header file */
699 /* malloc buffer for minimal on disk data */
700 ea->ea_data = malloc(EA_HEADER_SIZE);
702 LOG(log_error, logtype_afpd, "ea_open: OOM");
708 ea->ea_fd = create_ea_header(eaname, ea);
709 if (ea->ea_fd == -1) {
716 } else {/* errno != ENOENT */
722 /* header file exists, so read and parse it */
724 /* malloc buffer where we read disk file into */
725 if (st.st_size < EA_HEADER_SIZE) {
726 LOG(log_error, logtype_afpd, "ea_open('%s'): bogus EA header file", eaname);
730 ea->ea_size = st.st_size;
731 ea->ea_data = malloc(st.st_size);
733 LOG(log_error, logtype_afpd, "ea_open: OOM");
738 /* Now lock, open and read header file from disk */
739 if ((ea->ea_fd = open(eaname, (ea->ea_flags & EA_RDWR) ? O_RDWR : O_RDONLY)) == -1) {
740 LOG(log_error, logtype_afpd, "ea_open('%s'): error: %s", eaname, strerror(errno));
746 if (ea->ea_flags & EA_RDONLY) {
748 if ((read_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
749 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
753 } else { /* EA_RDWR */
755 if ((write_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
756 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
763 if (read(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
764 LOG(log_error, logtype_afpd, "ea_open: short read on header: %s", eaname);
769 if ((unpack_header(ea)) != 0) {
770 LOG(log_error, logtype_afpd, "ea_open: error unpacking header for: %s", eaname);
778 ea->ea_inited = EA_INITED;
781 errno = EFAULT; /* force some errno distinguishable from ENOENT */
799 * Function: ea_openat
801 * Purpose: openat like wrapper for ea_open, takes a additional file descriptor
805 * vol (r) current volume
806 * sfd (r) openat like file descriptor
807 * uname (r) filename for which we have to open a header
808 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
809 * EA_RDONLY: open read only
810 * EA_RDWR: open read/write
811 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
812 * ea (w) pointer to a struct ea that we fill
814 * Returns: 0 on success
815 * -1 on misc error with errno = EFAULT
816 * -2 if no EA header exists with errno = ENOENT
820 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
821 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
822 * file is either read or write locked depending on the open flags.
823 * When you're done with struct ea you must call ea_close on it.
825 int ea_openat(const struct vol * restrict vol,
827 const char * restrict uname,
829 struct ea * restrict ea)
835 if (((cwdfd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
841 ret = ea_open(vol, uname, eaflags, ea);
845 if (fchdir(cwdfd) != 0) {
846 LOG(log_error, logtype_afpd, "ea_openat: cant chdir back, exiting");
863 * Purpose: flushes and closes an ea handle
867 * ea (rw) pointer to ea handle
869 * Returns: 0 on success, -1 on error
873 * Flushes and then closes and frees all resouces held by ea handle.
874 * Pack data in ea into ea_data, then write ea_data to disk
876 int ea_close(struct ea * restrict ea)
879 unsigned int count = 0;
883 LOG(log_debug, logtype_afpd, "ea_close('%s')", ea->filename);
885 if (ea->ea_inited != EA_INITED) {
886 LOG(log_warning, logtype_afpd, "ea_close('%s'): non initialized ea", ea->filename);
890 /* pack header and write it to disk if it was opened EA_RDWR*/
891 if (ea->ea_flags & EA_RDWR) {
892 if ((pack_header(ea)) != 0) {
893 LOG(log_error, logtype_afpd, "ea_close: pack header");
896 if (ea->ea_count == 0) {
897 /* Check if EA header exists and remove it */
898 eaname = ea_path(ea, NULL, 0);
899 if ((lstatat(ea->dirfd, eaname, &st)) == 0) {
900 if ((netatalk_unlinkat(ea->dirfd, eaname)) != 0) {
901 LOG(log_error, logtype_afpd, "ea_close('%s'): unlink: %s",
902 eaname, strerror(errno));
906 LOG(log_debug, logtype_afpd, "ea_close(unlink '%s'): success", eaname);
909 if (errno != ENOENT) {
910 LOG(log_error, logtype_afpd, "ea_close('%s'): stat: %s",
911 eaname, strerror(errno));
915 } else { /* ea->ea_count > 0 */
916 if ((lseek(ea->ea_fd, 0, SEEK_SET)) == -1) {
917 LOG(log_error, logtype_afpd, "ea_close: lseek: %s", strerror(errno));
922 if ((ftruncate(ea->ea_fd, 0)) == -1) {
923 LOG(log_error, logtype_afpd, "ea_close: ftruncate: %s", strerror(errno));
928 if (write(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
929 LOG(log_error, logtype_afpd, "ea_close: write: %s", strerror(errno));
938 while(count < ea->ea_count) {
939 if ( (*ea->ea_entries)[count].ea_name ) {
940 free((*ea->ea_entries)[count].ea_name);
941 (*ea->ea_entries)[count].ea_name = NULL;
952 if (ea->ea_entries) {
953 free(ea->ea_entries);
954 ea->ea_entries = NULL;
961 if (ea->ea_fd != -1) {
962 close(ea->ea_fd); /* also releases the fcntl lock */
971 /************************************************************************************
972 * VFS funcs called from afp_ea* funcs
973 ************************************************************************************/
976 * Function: get_easize
978 * Purpose: get size of an EA
982 * vol (r) current volume
983 * rbuf (w) DSI reply buffer
984 * rbuflen (rw) current length of data in reply buffer
986 * oflag (r) link and create flag
987 * attruname (r) name of attribute
989 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
993 * Copies EA size into rbuf in network order. Increments *rbuflen +4.
995 int get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
997 int ret = AFPERR_MISC;
998 unsigned int count = 0;
1002 LOG(log_debug, logtype_afpd, "get_easize: file: %s", uname);
1004 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1005 if (errno != ENOENT)
1006 LOG(log_error, logtype_afpd, "get_easize: error calling ea_open for file: %s", uname);
1013 while (count < ea.ea_count) {
1014 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1015 uint32 = htonl((*ea.ea_entries)[count].ea_size);
1016 memcpy(rbuf, &uint32, 4);
1020 LOG(log_debug, logtype_afpd, "get_easize(\"%s\"): size: %u",
1021 attruname, (*ea.ea_entries)[count].ea_size);
1027 if ((ea_close(&ea)) != 0) {
1028 LOG(log_error, logtype_afpd, "get_easize: error closing ea handle for file: %s", uname);
1036 * Function: get_eacontent
1038 * Purpose: copy EA into rbuf
1042 * vol (r) current volume
1043 * rbuf (w) DSI reply buffer
1044 * rbuflen (rw) current length of data in reply buffer
1045 * uname (r) filename
1046 * oflag (r) link and create flag
1047 * attruname (r) name of attribute
1048 * maxreply (r) maximum EA size as of current specs/real-life
1050 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1054 * Copies EA into rbuf. Increments *rbuflen accordingly.
1056 int get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
1058 int ret = AFPERR_MISC, fd = -1;
1059 unsigned int count = 0;
1065 LOG(log_debug, logtype_afpd, "get_eacontent('%s/%s')", uname, attruname);
1067 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1068 if (errno != ENOENT)
1069 LOG(log_error, logtype_afpd, "get_eacontent('%s'): ea_open error", uname);
1075 while (count < ea.ea_count) {
1076 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1077 if ( (eafile = ea_path(&ea, attruname, 1)) == NULL) {
1082 if ((fd = open(eafile, O_RDONLY)) == -1) {
1083 LOG(log_error, logtype_afpd, "get_eacontent('%s'): open error: %s", uname, strerror(errno));
1088 /* Check how much the client wants, give him what we think is right */
1089 maxreply -= MAX_REPLY_EXTRA_BYTES;
1090 if (maxreply > MAX_EA_SIZE)
1091 maxreply = MAX_EA_SIZE;
1092 toread = (maxreply < (*ea.ea_entries)[count].ea_size) ? maxreply : (*ea.ea_entries)[count].ea_size;
1093 LOG(log_debug, logtype_afpd, "get_eacontent('%s'): sending %u bytes", attruname, toread);
1095 /* Put length of EA data in reply buffer */
1096 uint32 = htonl(toread);
1097 memcpy(rbuf, &uint32, 4);
1101 if (read(fd, rbuf, toread) != (ssize_t)toread) {
1102 LOG(log_error, logtype_afpd, "get_eacontent('%s/%s'): short read", uname, attruname);
1116 if ((ea_close(&ea)) != 0) {
1117 LOG(log_error, logtype_afpd, "get_eacontent('%s'): error closing ea handle", uname);
1126 * Function: list_eas
1128 * Purpose: copy names of EAs into attrnamebuf
1132 * vol (r) current volume
1133 * attrnamebuf (w) store names a consecutive C strings here
1134 * buflen (rw) length of names in attrnamebuf
1135 * uname (r) filename
1136 * oflag (r) link and create flag
1138 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1142 * Copies names of all EAs of uname as consecutive C strings into rbuf.
1143 * Increments *buflen accordingly.
1145 int list_eas(VFS_FUNC_ARGS_EA_LIST)
1147 unsigned int count = 0;
1148 int attrbuflen = *buflen, ret = AFP_OK, len;
1149 char *buf = attrnamebuf;
1152 LOG(log_debug, logtype_afpd, "list_eas: file: %s", uname);
1154 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1155 if (errno != ENOENT) {
1156 LOG(log_error, logtype_afpd, "list_eas: error calling ea_open for file: %s", uname);
1163 while (count < ea.ea_count) {
1164 /* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
1165 if ( ( len = convert_string(vol->v_volcharset,
1167 (*ea.ea_entries)[count].ea_name,
1168 (*ea.ea_entries)[count].ea_namelen,
1176 /* convert_string didn't 0-terminate */
1177 attrnamebuf[attrbuflen + 255] = 0;
1179 LOG(log_debug7, logtype_afpd, "list_eas(%s): EA: %s",
1180 uname, (*ea.ea_entries)[count].ea_name);
1182 attrbuflen += len + 1;
1183 if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
1184 /* Next EA name could overflow, so bail out with error.
1185 FIXME: evantually malloc/memcpy/realloc whatever.
1187 LOG(log_warning, logtype_afpd, "list_eas(%s): running out of buffer for EA names", uname);
1195 *buflen = attrbuflen;
1197 if ((ea_close(&ea)) != 0) {
1198 LOG(log_error, logtype_afpd, "list_eas: error closing ea handle for file: %s", uname);
1208 * Purpose: set a Solaris native EA
1212 * vol (r) current volume
1213 * uname (r) filename
1214 * attruname (r) EA name
1215 * ibuf (r) buffer with EA content
1216 * attrsize (r) length EA in ibuf
1217 * oflag (r) link and create flag
1219 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1223 * Copies names of all EAs of uname as consecutive C strings into rbuf.
1224 * Increments *rbuflen accordingly.
1226 int set_ea(VFS_FUNC_ARGS_EA_SET)
1231 LOG(log_debug, logtype_afpd, "set_ea: file: %s", uname);
1233 if ((ea_open(vol, uname, EA_CREATE | EA_RDWR, &ea)) != 0) {
1234 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_open error", uname);
1238 if ((ea_addentry(&ea, attruname, attrsize, oflag)) == -1) {
1239 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_addentry error", uname);
1244 if ((write_ea(&ea, attruname, ibuf, attrsize)) != 0) {
1245 LOG(log_error, logtype_afpd, "set_ea('%s'): write_ea error", uname);
1251 if ((ea_close(&ea)) != 0) {
1252 LOG(log_error, logtype_afpd, "set_ea('%s'): ea_close error", uname);
1261 * Function: remove_ea
1263 * Purpose: remove a EA from a file
1267 * vol (r) current volume
1268 * uname (r) filename
1269 * attruname (r) EA name
1270 * oflag (r) link and create flag
1272 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1276 * Removes EA attruname from file uname.
1278 int remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
1283 LOG(log_debug, logtype_afpd, "remove_ea('%s/%s')", uname, attruname);
1285 if ((ea_open(vol, uname, EA_RDWR, &ea)) != 0) {
1286 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_open error", uname);
1290 if ((ea_delentry(&ea, attruname)) == -1) {
1291 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_delentry error", uname);
1296 if ((delete_ea_file(&ea, attruname)) != 0) {
1297 LOG(log_error, logtype_afpd, "remove_ea('%s'): delete_ea error", uname);
1303 if ((ea_close(&ea)) != 0) {
1304 LOG(log_error, logtype_afpd, "remove_ea('%s'): ea_close error", uname);
1312 /******************************************************************************************
1313 * EA VFS funcs that deal with file/dir cp/mv/rm
1314 ******************************************************************************************/
1316 int ea_deletefile(VFS_FUNC_ARGS_DELETEFILE)
1318 unsigned int count = 0;
1323 LOG(log_debug, logtype_afpd, "ea_deletefile('%s')", file);
1326 if ((ea_openat(vol, dirfd, file, EA_RDWR, &ea)) != 0) {
1327 if (errno == ENOENT)
1328 /* no EA files, nothing to do */
1331 LOG(log_error, logtype_afpd, "ea_deletefile('%s'): error calling ea_open", file);
1337 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
1343 while (count < ea.ea_count) {
1344 if ((delete_ea_file(&ea, (*ea.ea_entries)[count].ea_name)) != 0) {
1348 free((*ea.ea_entries)[count].ea_name);
1349 (*ea.ea_entries)[count].ea_name = NULL;
1353 /* ea_close removes the EA header file for us because all names are NULL */
1354 if ((ea_close(&ea)) != 0) {
1355 LOG(log_error, logtype_afpd, "ea_deletefile('%s'): error closing ea handle", file);
1359 if (dirfd != -1 && fchdir(cwd) != 0) {
1360 LOG(log_error, logtype_afpd, "ea_deletefile: cant chdir back. exit!");
1371 int ea_renamefile(VFS_FUNC_ARGS_RENAMEFILE)
1373 unsigned int count = 0;
1376 char srceapath[ MAXPATHLEN + 1];
1383 LOG(log_debug, logtype_afpd, "ea_renamefile('%s'/'%s')", src, dst);
1387 if ((ea_openat(vol, dirfd, src, EA_RDWR, &srcea)) != 0) {
1388 if (errno == ENOENT)
1389 /* no EA files, nothing to do */
1392 LOG(log_error, logtype_afpd, "ea_renamefile('%s'/'%s'): ea_open error: '%s'", src, dst, src);
1397 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1398 if (errno == ENOENT) {
1399 /* Possibly the .AppleDouble folder didn't exist, we create it and try again */
1401 if ((ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666)) != 0) {
1402 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): ad_open error: '%s'", src, dst, dst);
1406 ad_close(&ad, ADFLAGS_HF);
1407 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1414 /* Loop through all EAs: */
1415 while (count < srcea.ea_count) {
1417 eaname = (*srcea.ea_entries)[count].ea_name;
1418 easize = (*srcea.ea_entries)[count].ea_size;
1420 /* Build src and dst paths for rename() */
1421 if ((eapath = ea_path(&srcea, eaname, 1)) == NULL) {
1425 strcpy(srceapath, eapath);
1426 if ((eapath = ea_path(&dstea, eaname, 1)) == NULL) {
1431 LOG(log_maxdebug, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1432 src, dst, srceapath, eapath);
1434 /* Add EA to dstea */
1435 if ((ea_addentry(&dstea, eaname, easize, 0)) == -1) {
1436 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1437 src, dst, srceapath, eapath);
1442 /* Remove EA entry from srcea */
1443 if ((ea_delentry(&srcea, eaname)) == -1) {
1444 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1445 src, dst, srceapath, eapath);
1446 ea_delentry(&dstea, eaname);
1451 /* Now rename the EA */
1452 if ((unix_rename(dirfd, srceapath, -1, eapath)) < 0) {
1453 LOG(log_error, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
1454 src, dst, srceapath, eapath);
1469 int ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
1471 unsigned int count = 0;
1474 char srceapath[ MAXPATHLEN + 1];
1481 LOG(log_debug, logtype_afpd, "ea_copyfile('%s'/'%s')", src, dst);
1484 if ((ea_openat(vol, sfd, src, EA_RDWR, &srcea)) != 0) {
1485 if (errno == ENOENT)
1486 /* no EA files, nothing to do */
1489 LOG(log_error, logtype_afpd, "ea_copyfile('%s'/'%s'): ea_open error: '%s'", src, dst, src);
1494 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1495 if (errno == ENOENT) {
1496 /* Possibly the .AppleDouble folder didn't exist, we create it and try again */
1498 if ((ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666)) != 0) {
1499 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): ad_open error: '%s'", src, dst, dst);
1503 ad_close(&ad, ADFLAGS_HF);
1504 if ((ea_open(vol, dst, EA_RDWR | EA_CREATE, &dstea)) != 0) {
1511 /* Loop through all EAs: */
1512 while (count < srcea.ea_count) {
1514 eaname = (*srcea.ea_entries)[count].ea_name;
1515 easize = (*srcea.ea_entries)[count].ea_size;
1517 /* Build src and dst paths for copy_file() */
1518 if ((eapath = ea_path(&srcea, eaname, 1)) == NULL) {
1522 strcpy(srceapath, eapath);
1523 if ((eapath = ea_path(&dstea, eaname, 1)) == NULL) {
1528 LOG(log_maxdebug, logtype_afpd, "ea_copyfile('%s/%s'): copying EA '%s' to '%s'",
1529 src, dst, srceapath, eapath);
1531 /* Add EA to dstea */
1532 if ((ea_addentry(&dstea, eaname, easize, 0)) == -1) {
1533 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): ea_addentry('%s') error",
1539 /* Now copy the EA */
1540 if ((copy_file(sfd, srceapath, eapath, (0666 & ~vol->v_umask))) < 0) {
1541 LOG(log_error, logtype_afpd, "ea_copyfile('%s/%s'): copying EA '%s' to '%s'",
1542 src, dst, srceapath, eapath);
1556 int ea_chown(VFS_FUNC_ARGS_CHOWN)
1559 unsigned int count = 0;
1564 LOG(log_debug, logtype_afpd, "ea_chown('%s')", path);
1566 if ((ea_open(vol, path, EA_RDWR, &ea)) != 0) {
1567 if (errno == ENOENT)
1568 /* no EA files, nothing to do */
1571 LOG(log_error, logtype_afpd, "ea_chown('%s'): error calling ea_open", path);
1576 if ((lchown(ea_path(&ea, NULL, 0), uid, gid)) != 0) {
1580 ret = AFPERR_ACCESS;
1588 while (count < ea.ea_count) {
1589 if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 1)) == NULL) {
1593 if ((lchown(eaname, uid, gid)) != 0) {
1597 ret = AFPERR_ACCESS;
1610 if ((ea_close(&ea)) != 0) {
1611 LOG(log_error, logtype_afpd, "ea_chown('%s'): error closing ea handle", path);
1618 int ea_chmod_file(VFS_FUNC_ARGS_SETFILEMODE)
1621 unsigned int count = 0;
1626 LOG(log_debug, logtype_afpd, "ea_chmod_file('%s')", name);
1628 if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
1629 if (errno == ENOENT)
1630 /* no EA files, nothing to do */
1636 /* Set mode on EA header file */
1637 if ((setfilmode(ea_path(&ea, NULL, 0), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
1638 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", ea_path(&ea, NULL, 0), strerror(errno));
1642 ret = AFPERR_ACCESS;
1650 /* Set mode on EA files */
1651 while (count < ea.ea_count) {
1652 if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 1)) == NULL) {
1656 if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
1657 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", eaname, strerror(errno));
1661 ret = AFPERR_ACCESS;
1674 if ((ea_close(&ea)) != 0) {
1675 LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): error closing ea handle", name);
1682 int ea_chmod_dir(VFS_FUNC_ARGS_SETDIRUNIXMODE)
1686 unsigned int count = 0;
1689 const char *eaname_safe = NULL;
1692 LOG(log_debug, logtype_afpd, "ea_chmod_dir('%s')", name);
1693 /* .AppleDouble already might be inaccesible, so we must run as id 0 */
1696 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): seteuid: %s", name, strerror(errno));
1701 if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
1702 /* ENOENT --> no EA files, nothing to do */
1703 if (errno != ENOENT)
1705 if (seteuid(uid) < 0) {
1706 LOG(log_error, logtype_afpd, "can't seteuid back: %s", strerror(errno));
1712 /* Set mode on EA header */
1713 if ((setfilmode(ea_path(&ea, NULL, 0), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
1714 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", ea_path(&ea, NULL, 0), strerror(errno));
1718 ret = AFPERR_ACCESS;
1726 /* Set mode on EA files */
1727 while (count < ea.ea_count) {
1728 eaname = (*ea.ea_entries)[count].ea_name;
1730 * Be careful with EA names from the EA header!
1731 * Eg NFS users might have access to them, can inject paths using ../ or /.....
1733 * Until the EA code escapes / in EA name requests from the client, these therefor wont work.
1735 if ((eaname_safe = strrchr(eaname, '/'))) {
1736 LOG(log_warning, logtype_afpd, "ea_chmod_dir('%s'): contains a slash", eaname);
1737 eaname = eaname_safe;
1739 if ((eaname = ea_path(&ea, eaname, 1)) == NULL) {
1743 if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
1744 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", eaname, strerror(errno));
1748 ret = AFPERR_ACCESS;
1761 if (seteuid(uid) < 0) {
1762 LOG(log_error, logtype_afpd, "can't seteuid back: %s", strerror(errno));
1766 if ((ea_close(&ea)) != 0) {
1767 LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): error closing ea handle", name);