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>
29 #include <atalk/adouble.h>
31 #include <atalk/afp.h>
32 #include <atalk/logger.h>
33 #include <atalk/volume.h>
34 #include <atalk/vfs.h>
35 #include <atalk/util.h>
36 #include <atalk/unix.h>
39 * Store Extended Attributes inside .AppleDouble folders as follows:
41 * filename "fileWithEAs" with EAs "testEA1" and "testEA2"
43 * - create header with with the format struct adouble_ea_ondisk, the file is written to
44 * ".AppleDouble/fileWithEAs::EA"
45 * - store EAs in files "fileWithEAs::EA::testEA1" and "fileWithEAs::EA::testEA2"
49 * Build mode for EA header from file mode
51 static inline mode_t ea_header_mode(mode_t mode)
53 /* Same as ad_hf_mode(mode) */
54 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
55 /* Owner must be able to open, read and w-lock it, in order to chmod from eg 0000 -> 0xxxx*/
56 mode |= S_IRUSR | S_IWUSR;
61 * Build mode for EA file from file mode
63 static inline mode_t ea_mode(mode_t mode)
65 /* Same as ad_hf_mode(mode) */
66 mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
71 Taken form afpd/desktop.c
73 static char *mtoupath(const struct vol *vol, const char *mpath)
75 static char upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
80 uint16_t flags = CONV_ESCAPEHEX | CONV_ALLOW_COLON;
85 if ( *mpath == '\0' ) {
95 if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
98 m, inplen, u, outlen, &flags)) ) {
107 * Function: unpack_header
109 * Purpose: unpack and verify header file data buffer at ea->ea_data into struct ea
113 * ea (rw) handle to struct ea
115 * Returns: 0 on success, -1 on error
119 * Verifies magic and version.
121 static int unpack_header(struct ea * restrict ea)
124 unsigned int count = 0;
128 /* Check magic and version */
130 if (*(uint32_t *)buf != htonl(EA_MAGIC)) {
131 LOG(log_error, logtype_afpd, "unpack_header: wrong magic 0x%08x", *(uint32_t *)buf);
136 if (*(uint16_t *)buf != htons(EA_VERSION)) {
137 LOG(log_error, logtype_afpd, "unpack_header: wrong version 0x%04x", *(uint16_t *)buf);
144 ea->ea_count = ntohs(*(uint16_t *)buf);
145 LOG(log_debug, logtype_afpd, "unpack_header: number of EAs: %u", ea->ea_count);
148 if (ea->ea_count == 0)
151 /* Allocate storage for the ea_entries array */
152 ea->ea_entries = malloc(sizeof(struct ea_entry) * ea->ea_count);
153 if ( ! ea->ea_entries) {
154 LOG(log_error, logtype_afpd, "unpack_header: OOM");
159 buf = ea->ea_data + EA_HEADER_SIZE;
160 while (count < ea->ea_count) {
161 memcpy(&uint32, buf, 4); /* EA size */
163 (*(ea->ea_entries))[count].ea_size = ntohl(uint32);
164 (*(ea->ea_entries))[count].ea_name = strdup(buf);
165 if (! (*(ea->ea_entries))[count].ea_name) {
166 LOG(log_error, logtype_afpd, "unpack_header: OOM");
170 (*(ea->ea_entries))[count].ea_namelen = strlen((*(ea->ea_entries))[count].ea_name);
171 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
173 LOG(log_maxdebug, logtype_afpd, "unpack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
174 (*(ea->ea_entries))[count].ea_name,
175 (*(ea->ea_entries))[count].ea_size,
176 (*(ea->ea_entries))[count].ea_namelen);
186 * Function: pack_header
188 * Purpose: pack everything from struct ea into buffer at ea->ea_data
192 * ea (rw) handle to struct ea
194 * Returns: 0 on success, -1 on error
198 * adjust ea->ea_count in case an ea entry deletetion is detected
200 static int pack_header(struct ea * restrict ea)
202 unsigned int count = 0, eacount = 0;
205 size_t bufsize = EA_HEADER_SIZE;
207 char *buf = ea->ea_data + EA_HEADER_SIZE;
209 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
210 ea->filename, ea->ea_count, ea->ea_size);
212 if (ea->ea_count == 0)
213 /* nothing to do, magic, version and count are still valid in buffer */
216 while(count < ea->ea_count) { /* the names */
217 /* Check if its a deleted entry */
218 if ( ! ((*ea->ea_entries)[count].ea_name)) {
223 bufsize += (*(ea->ea_entries))[count].ea_namelen + 1;
228 bufsize += (eacount * 4); /* header + ea_size for each EA */
229 if (bufsize > ea->ea_size) {
230 /* we must realloc */
231 if ( ! (buf = realloc(ea->ea_data, bufsize)) ) {
232 LOG(log_error, logtype_afpd, "pack_header: OOM");
237 ea->ea_size = bufsize;
240 uint16 = htons(eacount);
241 memcpy(ea->ea_data + EA_COUNT_OFF, &uint16, 2);
244 buf = ea->ea_data + EA_HEADER_SIZE;
245 while (count < ea->ea_count) {
246 /* Check if its a deleted entry */
247 if ( ! ((*ea->ea_entries)[count].ea_name)) {
253 uint32 = htonl((*(ea->ea_entries))[count].ea_size);
254 memcpy(buf, &uint32, 4);
257 /* Second: EA name as C-string */
258 strcpy(buf, (*(ea->ea_entries))[count].ea_name);
259 buf += (*(ea->ea_entries))[count].ea_namelen + 1;
261 LOG(log_maxdebug, logtype_afpd, "pack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
262 (*(ea->ea_entries))[count].ea_name,
263 (*(ea->ea_entries))[count].ea_size,
264 (*(ea->ea_entries))[count].ea_namelen);
269 ea->ea_count = eacount;
271 LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
272 ea->filename, ea->ea_count, ea->ea_size);
278 * Function: ea_addentry
280 * Purpose: add one EA into ea->ea_entries[]
284 * ea (rw) pointer to struct ea
285 * attruname (r) name of EA
286 * attrsize (r) size of ea
287 * bitmap (r) bitmap from FP func
289 * Returns: new number of EA entries, -1 on error
293 * Grow array ea->ea_entries[]. If ea->ea_entries is still NULL, start allocating.
294 * Otherwise realloc and put entry at the end. Increments ea->ea_count.
296 static int ea_addentry(struct ea * restrict ea,
297 const char * restrict attruname,
302 unsigned int count = 0;
305 /* First check if an EA of the requested name already exist */
306 if (ea->ea_count > 0) {
307 while (count < ea->ea_count) {
308 if (strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
310 LOG(log_debug, logtype_afpd, "ea_addentry('%s', bitmap:0x%x): exists", attruname, bitmap);
311 if (bitmap & kXAttrCreate)
312 /* its like O_CREAT|O_EXCL -> fail */
314 (*(ea->ea_entries))[count].ea_size = attrsize;
321 if ((bitmap & kXAttrReplace) && ! ea_existed)
322 /* replace was requested, but EA didn't exist */
325 if (ea->ea_count == 0) {
326 ea->ea_entries = malloc(sizeof(struct ea_entry));
327 if ( ! ea->ea_entries) {
328 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
331 } else if (! ea_existed) {
332 tmprealloc = realloc(ea->ea_entries, sizeof(struct ea_entry) * (ea->ea_count + 1));
334 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
337 ea->ea_entries = tmprealloc;
340 /* We've grown the array, now store the entry */
341 (*(ea->ea_entries))[ea->ea_count].ea_size = attrsize;
342 (*(ea->ea_entries))[ea->ea_count].ea_name = strdup(attruname);
343 if ( ! (*(ea->ea_entries))[ea->ea_count].ea_name) {
344 LOG(log_error, logtype_afpd, "ea_addentry: OOM");
347 (*(ea->ea_entries))[ea->ea_count].ea_namelen = strlen(attruname);
353 if (ea->ea_count == 0 && ea->ea_entries) {
354 /* We just allocated storage but had an error somewhere -> free storage*/
355 free(ea->ea_entries);
356 ea->ea_entries = NULL;
363 * Function: create_ea_header
365 * Purpose: create EA header file, only called from ea_open
369 * uname (r) filename for which we have to create a header
370 * ea (rw) ea handle with already allocated storage pointed to
373 * Returns: fd of open header file on success, -1 on error, errno semantics:
374 * EEXIST: open with O_CREAT | O_EXCL failed
378 * Creates EA header file and initialize ea->ea_data buffer.
379 * Possibe race condition with other afpd processes:
380 * we were called because header file didn't exist in eg. ea_open. We then
381 * try to create a file with O_CREAT | O_EXCL, but the whole process in not atomic.
382 * What do we do then? Someone else is in the process of creating the header too, but
383 * it might not have finished it. That means we cant just open, read and use it!
384 * We therefor currently just break with an error.
385 * On return the header file is still r/w locked.
387 static int create_ea_header(const char * restrict uname,
388 struct ea * restrict ea)
390 int fd = -1, err = 0;
393 if ((fd = open(uname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
394 LOG(log_error, logtype_afpd, "ea_create: open race condition with ea header for file: %s", uname);
399 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
400 LOG(log_error, logtype_afpd, "ea_create: lock race condition with ea header for file: %s", uname);
407 *(uint32_t *)ptr = htonl(EA_MAGIC);
409 *(uint16_t *)ptr = htons(EA_VERSION);
410 ptr += EA_VERSION_LEN;
411 *(uint16_t *)ptr = 0; /* count */
413 ea->ea_size = EA_HEADER_SIZE;
414 ea->ea_inited = EA_INITED;
427 * Purpose: write an EA to disk
431 * ea (r) struct ea handle
432 * attruname (r) EA name
433 * ibuf (r) buffer with EA content
434 * attrsize (r) size of EA
436 * Returns: 0 on success, -1 on error
440 * Creates/overwrites EA file.
443 static int write_ea(const struct ea * restrict ea,
444 const char * restrict attruname,
445 const char * restrict ibuf,
448 int fd = -1, ret = AFP_OK;
452 if ((eaname = ea_path(ea, attruname, 1)) == NULL) {
453 LOG(log_error, logtype_afpd, "write_ea('%s'): ea_path error", attruname);
457 LOG(log_maxdebug, logtype_afpd, "write_ea('%s')", eaname);
459 /* Check if it exists, remove if yes*/
460 if ((stat(eaname, &st)) == 0) {
461 if ((unlink(eaname)) != 0) {
463 return AFPERR_ACCESS;
469 if ((fd = open(eaname, O_RDWR | O_CREAT | O_EXCL, 0666 & ~ea->vol->v_umask)) == -1) {
470 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
475 if ((write_lock(fd, 0, SEEK_SET, 0)) != 0) {
476 LOG(log_error, logtype_afpd, "write_ea: open race condition: %s", eaname);
481 if (write(fd, ibuf, attrsize) != (ssize_t)attrsize) {
482 LOG(log_error, logtype_afpd, "write_ea('%s'): write: %s", eaname, strerror(errno));
489 close(fd); /* and unlock */
494 * Function: ea_delentry
496 * Purpose: delete one EA from ea->ea_entries[]
500 * ea (rw) pointer to struct ea
501 * attruname (r) EA name
503 * Returns: new number of EA entries, -1 on error
507 * Remove entry from ea->ea_entries[]. Decrement ea->ea_count.
508 * Marks it as unused just by freeing name and setting it to NULL.
509 * ea_close and pack_buffer must honor this.
511 static int ea_delentry(struct ea * restrict ea, const char * restrict attruname)
514 unsigned int count = 0;
516 if (ea->ea_count == 0) {
517 LOG(log_error, logtype_afpd, "ea_delentry('%s'): illegal ea_count of 0 on deletion");
521 while (count < ea->ea_count) {
522 /* search matching EA */
523 if (strcmp(attruname, (*ea->ea_entries)[count].ea_name) == 0) {
524 free((*ea->ea_entries)[count].ea_name);
525 (*ea->ea_entries)[count].ea_name = NULL;
527 LOG(log_debug, logtype_afpd, "ea_delentry('%s'): deleted no %u/%u",
528 attruname, count + 1, ea->ea_count);
539 * Function: delete_ea_file
541 * Purpose: delete EA file from disk
545 * ea (r) struct ea handle
546 * attruname (r) EA name
548 * Returns: 0 on success, -1 on error
550 static int delete_ea_file(const struct ea * restrict ea, const char *eaname)
556 if ((eafile = ea_path(ea, eaname, 1)) == NULL) {
557 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): ea_path error", eaname);
561 /* Check if it exists, remove if yes*/
562 if ((stat(eafile, &st)) == 0) {
563 if ((unlink(eafile)) != 0) {
564 LOG(log_error, logtype_afpd, "delete_ea_file('%s'): unlink: %s",
565 eafile, strerror(errno));
568 LOG(log_debug, logtype_afpd, "delete_ea_file('%s'): success", eafile);
574 /*************************************************************************************
575 * ea_path, ea_open and ea_close are only global so that dbd can call them
576 *************************************************************************************/
581 * Purpose: return name of ea header filename
586 * eaname (r) name of EA or NULL
587 * macname (r) if != 0 call mtoupath on eaname
589 * Returns: pointer to name in static buffer, NULL on error
593 * Calls ad_open, copies buffer, appends "::EA" and if supplied append eanme
594 * Files: "file" -> "file/.AppleDouble/file::EA"
595 * Dirs: "dir" -> "dir/.AppleDouble/.Parent::EA"
596 * "file" with EA "myEA" -> "file/.AppleDouble/file::EA:myEA"
598 char *ea_path(const struct ea * restrict ea, const char * restrict eaname, int macname)
601 static char pathbuf[MAXPATHLEN + 1];
603 /* get name of a adouble file from uname */
604 adname = ea->vol->ad_path(ea->filename, (ea->ea_flags & EA_DIR) ? ADFLAGS_DIR : 0);
605 /* copy it so we can work with it */
606 strlcpy(pathbuf, adname, MAXPATHLEN + 1);
608 strlcat(pathbuf, "::EA", MAXPATHLEN + 1);
611 strlcat(pathbuf, "::", MAXPATHLEN + 1);
613 if ((eaname = mtoupath(ea->vol, eaname)) == NULL)
615 strlcat(pathbuf, eaname, MAXPATHLEN + 1);
624 * Purpose: open EA header file, create if it doesnt exits and called with O_CREATE
628 * vol (r) current volume
629 * uname (r) filename for which we have to open a header
630 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
631 * EA_RDONLY: open read only
632 * EA_RDWR: open read/write
633 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
634 * ea (w) pointer to a struct ea that we fill
636 * Returns: 0 on success
637 * -1 on misc error with errno = EFAULT
638 * -2 if no EA header exists with errno = ENOENT
642 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
643 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
644 * file is either read or write locked depending on the open flags.
645 * When you're done with struct ea you must call ea_close on it.
647 int ea_open(const struct vol * restrict vol,
648 const char * restrict uname,
650 struct ea * restrict ea)
656 /* Enforce usage rules! */
657 if ( ! (eaflags & (EA_RDONLY | EA_RDWR))) {
658 LOG(log_error, logtype_afpd, "ea_open: called without EA_RDONLY | EA_RDWR", uname);
662 /* Set it all to 0 */
663 memset(ea, 0, sizeof(struct ea));
665 ea->vol = vol; /* ea_close needs it */
666 ea->ea_flags = eaflags;
667 ea->dirfd = -1; /* no *at (cf openat) semantics by default */
669 /* Dont care for errors, eg when removing the file is already gone */
670 if (!stat(uname, &st) && S_ISDIR(st.st_mode))
671 ea->ea_flags |= EA_DIR;
673 if ( ! (ea->filename = strdup(uname))) {
674 LOG(log_error, logtype_afpd, "ea_open: OOM");
678 eaname = ea_path(ea, NULL, 0);
679 LOG(log_maxdebug, logtype_afpd, "ea_open: ea_path: %s", eaname);
681 /* Check if it exists, if not create it if EA_CREATE is in eaflags */
682 if ((stat(eaname, &st)) != 0) {
683 if (errno == ENOENT) {
685 /* It doesnt exist */
687 if ( ! (eaflags & EA_CREATE)) {
688 /* creation was not requested, so return with error */
693 /* Now create a header file */
695 /* malloc buffer for minimal on disk data */
696 ea->ea_data = malloc(EA_HEADER_SIZE);
698 LOG(log_error, logtype_afpd, "ea_open: OOM");
704 ea->ea_fd = create_ea_header(eaname, ea);
705 if (ea->ea_fd == -1) {
712 } else {/* errno != ENOENT */
718 /* header file exists, so read and parse it */
720 /* malloc buffer where we read disk file into */
721 if (st.st_size < EA_HEADER_SIZE) {
722 LOG(log_error, logtype_afpd, "ea_open('%s'): bogus EA header file", eaname);
726 ea->ea_size = st.st_size;
727 ea->ea_data = malloc(st.st_size);
729 LOG(log_error, logtype_afpd, "ea_open: OOM");
734 /* Now lock, open and read header file from disk */
735 if ((ea->ea_fd = open(eaname, (ea->ea_flags & EA_RDWR) ? O_RDWR : O_RDONLY)) == -1) {
736 LOG(log_error, logtype_afpd, "ea_open('%s'): error: %s", eaname, strerror(errno));
742 if (ea->ea_flags & EA_RDONLY) {
744 if ((read_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
745 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
749 } else { /* EA_RDWR */
751 if ((write_lock(ea->ea_fd, 0, SEEK_SET, 0)) != 0) {
752 LOG(log_error, logtype_afpd, "ea_open: lock error on header: %s", eaname);
759 if (read(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
760 LOG(log_error, logtype_afpd, "ea_open: short read on header: %s", eaname);
765 if ((unpack_header(ea)) != 0) {
766 LOG(log_error, logtype_afpd, "ea_open: error unpacking header for: %s", eaname);
774 ea->ea_inited = EA_INITED;
777 errno = EFAULT; /* force some errno distinguishable from ENOENT */
795 * Function: ea_openat
797 * Purpose: openat like wrapper for ea_open, takes a additional file descriptor
801 * vol (r) current volume
802 * sfd (r) openat like file descriptor
803 * uname (r) filename for which we have to open a header
804 * flags (r) EA_CREATE: create if it doesn't exist (without it won't be created)
805 * EA_RDONLY: open read only
806 * EA_RDWR: open read/write
807 * Eiterh EA_RDONLY or EA_RDWR MUST be requested
808 * ea (w) pointer to a struct ea that we fill
810 * Returns: 0 on success
811 * -1 on misc error with errno = EFAULT
812 * -2 if no EA header exists with errno = ENOENT
816 * opens header file and stores fd in ea->ea_fd. Size of file is put into ea->ea_size.
817 * number of EAs is stored in ea->ea_count. flags are remembered in ea->ea_flags.
818 * file is either read or write locked depending on the open flags.
819 * When you're done with struct ea you must call ea_close on it.
821 int ea_openat(const struct vol * restrict vol,
823 const char * restrict uname,
825 struct ea * restrict ea)
831 if (((cwdfd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
837 ret = ea_open(vol, uname, eaflags, ea);
841 if (fchdir(cwdfd) != 0) {
842 LOG(log_error, logtype_afpd, "ea_openat: cant chdir back, exiting");
859 * Purpose: flushes and closes an ea handle
863 * ea (rw) pointer to ea handle
865 * Returns: 0 on success, -1 on error
869 * Flushes and then closes and frees all resouces held by ea handle.
870 * Pack data in ea into ea_data, then write ea_data to disk
872 int ea_close(struct ea * restrict ea)
875 unsigned int count = 0;
879 LOG(log_debug, logtype_afpd, "ea_close('%s')", ea->filename);
881 if (ea->ea_inited != EA_INITED) {
882 LOG(log_warning, logtype_afpd, "ea_close('%s'): non initialized ea", ea->filename);
886 /* pack header and write it to disk if it was opened EA_RDWR*/
887 if (ea->ea_flags & EA_RDWR) {
888 if ((pack_header(ea)) != 0) {
889 LOG(log_error, logtype_afpd, "ea_close: pack header");
892 if (ea->ea_count == 0) {
893 /* Check if EA header exists and remove it */
894 eaname = ea_path(ea, NULL, 0);
895 if ((lstatat(ea->dirfd, eaname, &st)) == 0) {
896 if ((netatalk_unlinkat(ea->dirfd, eaname)) != 0) {
897 LOG(log_error, logtype_afpd, "ea_close('%s'): unlink: %s",
898 eaname, strerror(errno));
902 LOG(log_debug, logtype_afpd, "ea_close(unlink '%s'): success", eaname);
905 if (errno != ENOENT) {
906 LOG(log_error, logtype_afpd, "ea_close('%s'): stat: %s",
907 eaname, strerror(errno));
911 } else { /* ea->ea_count > 0 */
912 if ((lseek(ea->ea_fd, 0, SEEK_SET)) == -1) {
913 LOG(log_error, logtype_afpd, "ea_close: lseek: %s", strerror(errno));
918 if ((ftruncate(ea->ea_fd, 0)) == -1) {
919 LOG(log_error, logtype_afpd, "ea_close: ftruncate: %s", strerror(errno));
924 if (write(ea->ea_fd, ea->ea_data, ea->ea_size) != (ssize_t)ea->ea_size) {
925 LOG(log_error, logtype_afpd, "ea_close: write: %s", strerror(errno));
934 while(count < ea->ea_count) {
935 if ( (*ea->ea_entries)[count].ea_name ) {
936 free((*ea->ea_entries)[count].ea_name);
937 (*ea->ea_entries)[count].ea_name = NULL;
948 if (ea->ea_entries) {
949 free(ea->ea_entries);
950 ea->ea_entries = NULL;
957 if (ea->ea_fd != -1) {
958 close(ea->ea_fd); /* also releases the fcntl lock */
967 /************************************************************************************
968 * VFS funcs called from afp_ea* funcs
969 ************************************************************************************/
972 * Function: get_easize
974 * Purpose: get size of an EA
978 * vol (r) current volume
979 * rbuf (w) DSI reply buffer
980 * rbuflen (rw) current length of data in reply buffer
982 * oflag (r) link and create flag
983 * attruname (r) name of attribute
985 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
989 * Copies EA size into rbuf in network order. Increments *rbuflen +4.
991 int get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
993 int ret = AFPERR_MISC;
994 unsigned int count = 0;
998 LOG(log_debug, logtype_afpd, "get_easize: file: %s", uname);
1000 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1001 if (errno != ENOENT)
1002 LOG(log_error, logtype_afpd, "get_easize: error calling ea_open for file: %s", uname);
1009 while (count < ea.ea_count) {
1010 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1011 uint32 = htonl((*ea.ea_entries)[count].ea_size);
1012 memcpy(rbuf, &uint32, 4);
1016 LOG(log_debug, logtype_afpd, "get_easize(\"%s\"): size: %u",
1017 attruname, (*ea.ea_entries)[count].ea_size);
1023 if ((ea_close(&ea)) != 0) {
1024 LOG(log_error, logtype_afpd, "get_easize: error closing ea handle for file: %s", uname);
1032 * Function: get_eacontent
1034 * Purpose: copy EA into rbuf
1038 * vol (r) current volume
1039 * rbuf (w) DSI reply buffer
1040 * rbuflen (rw) current length of data in reply buffer
1041 * uname (r) filename
1042 * oflag (r) link and create flag
1043 * attruname (r) name of attribute
1044 * maxreply (r) maximum EA size as of current specs/real-life
1046 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
1050 * Copies EA into rbuf. Increments *rbuflen accordingly.
1052 int get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
1054 int ret = AFPERR_MISC, fd = -1;
1055 unsigned int count = 0;
1061 LOG(log_debug, logtype_afpd, "get_eacontent('%s/%s')", uname, attruname);
1063 if ((ea_open(vol, uname, EA_RDONLY, &ea)) != 0) {
1064 if (errno != ENOENT)
1065 LOG(log_error, logtype_afpd, "get_eacontent('%s'): ea_open error", uname);
1071 while (count < ea.ea_count) {
1072 if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
1073 if ( (eafile = ea_path(&ea, attruname, 1)) == NULL) {
1078 if ((fd = open(eafile, O_RDONLY)) == -1) {
1079 LOG(log_error, logtype_afpd, "get_eacontent('%s'): open error: %s", uname, strerror(errno));
1084 /* Check how much the client wants, give him what we think is right */
1085 maxreply -= MAX_REPLY_EXTRA_BYTES;
1086 if (maxreply > MAX_EA_SIZE)
1087 maxreply = MAX_EA_SIZE;
1088 toread = (maxreply < (*ea.ea_entries)[count].ea_size) ? maxreply : (*ea.ea_entries)[count].ea_size;
1089 LOG(log_debug, logtype_afpd, "get_eacontent('%s'): sending %u bytes", attruname, toread);
1091 /* Put length of EA data in reply buffer */
1092 uint32 = htonl(toread);
1093 memcpy(rbuf, &uint32, 4);
1097 if (read(fd, rbuf, toread) != (ssize_t)toread) {
1098 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);