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>
40 /**********************************************************************************
41 * EA VFS funcs for storing EAs in nativa filesystem EAs
42 **********************************************************************************/
45 * Function: sys_get_easize
47 * Purpose: get size of a native EA
51 * vol (r) current volume
52 * rbuf (w) DSI reply buffer
53 * rbuflen (rw) current length of data in reply buffer
55 * oflag (r) link and create flag
56 * attruname (r) name of attribute
58 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
62 * Copies EA size into rbuf in network order. Increments *rbuflen +4.
64 int sys_get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
69 LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\"", uname, attruname);
71 if ((oflag & O_NOFOLLOW) ) {
72 ret = sys_lgetxattr(uname, attruname, rbuf +4, 0);
75 ret = sys_getxattr(uname, attruname, rbuf +4, 0);
82 case OPEN_NOFOLLOW_ERRNO:
83 /* its a symlink and client requested O_NOFOLLOW */
84 LOG(log_debug, logtype_afpd, "sys_getextattr_size(%s): symlink with kXAttrNoFollow", uname);
89 if (vol->v_obj->afp_version >= 34)
94 LOG(log_debug, logtype_afpd, "sys_getextattr_size: error: %s", strerror(errno));
99 if (ret > MAX_EA_SIZE)
102 /* Start building reply packet */
103 LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\", size: %u", uname, attruname, ret);
105 /* length of attribute data */
106 attrsize = htonl((uint32_t)ret);
107 memcpy(rbuf, &attrsize, 4);
115 * Function: sys_get_eacontent
117 * Purpose: copy native EA into rbuf
121 * vol (r) current volume
122 * rbuf (w) DSI reply buffer
123 * rbuflen (rw) current length of data in reply buffer
125 * oflag (r) link and create flag
126 * attruname (r) name of attribute
127 * maxreply (r) maximum EA size as of current specs/real-life
129 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
133 * Copies EA into rbuf. Increments *rbuflen accordingly.
135 int sys_get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
140 /* Start building reply packet */
142 maxreply -= MAX_REPLY_EXTRA_BYTES;
144 if (maxreply > MAX_EA_SIZE)
145 maxreply = MAX_EA_SIZE;
147 LOG(log_debug7, logtype_afpd, "sys_getextattr_content(%s): attribute: \"%s\", size: %u", uname, attruname, maxreply);
149 if ((oflag & O_NOFOLLOW) ) {
150 ret = sys_lgetxattr(uname, attruname, rbuf +4, maxreply);
153 ret = sys_getxattr(uname, attruname, rbuf +4, maxreply);
160 case OPEN_NOFOLLOW_ERRNO:
161 /* its a symlink and client requested O_NOFOLLOW */
162 LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): symlink with kXAttrNoFollow", uname);
166 if (vol->v_obj->afp_version >= 34)
167 return AFPERR_NOITEM;
171 LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): error: %s", attruname, strerror(errno));
176 /* remember where we must store length of attribute data in rbuf */
179 attrsize = htonl((uint32_t)ret);
180 memcpy(rbuf, &attrsize, 4);
186 * Function: sys_list_eas
188 * Purpose: copy names of native EAs into attrnamebuf
192 * vol (r) current volume
193 * attrnamebuf (w) store names a consecutive C strings here
194 * buflen (rw) length of names in attrnamebuf
196 * oflag (r) link and create flag
198 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
202 * Copies names of all EAs of uname as consecutive C strings into rbuf.
203 * Increments *rbuflen accordingly.
204 * We hide the adouble:ea extended attributes here, but we currently
205 * allow reading, writing and deleteting them.
207 int sys_list_eas(VFS_FUNC_ARGS_EA_LIST)
209 ssize_t attrbuflen = *buflen;
214 buf = malloc(ATTRNAMEBUFSIZ);
218 if ((oflag & O_NOFOLLOW)) {
219 ret = sys_llistxattr(uname, buf, ATTRNAMEBUFSIZ);
222 ret = sys_listxattr(uname, buf, ATTRNAMEBUFSIZ);
225 if (ret == -1) switch(errno) {
226 case OPEN_NOFOLLOW_ERRNO:
227 /* its a symlink and client requested O_NOFOLLOW, we pretend 0 EAs */
228 LOG(log_debug, logtype_afpd, "sys_list_extattr(%s): symlink with kXAttrNoFollow", uname);
232 LOG(log_debug, logtype_afpd, "sys_list_extattr(%s): error opening atttribute dir: %s", uname, strerror(errno));
240 if (NOT_NETATALK_EA(ptr)) {
241 /* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
242 if ( 0 >= ( nlen = convert_string(vol->v_volcharset, CH_UTF8_MAC, ptr, len, attrnamebuf + attrbuflen, 256)) ) {
247 LOG(log_debug7, logtype_afpd, "sys_list_extattr(%s): attribute: %s", uname, ptr);
249 attrbuflen += nlen + 1;
250 if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
251 /* Next EA name could overflow, so bail out with error.
252 FIXME: evantually malloc/memcpy/realloc whatever.
254 LOG(log_warning, logtype_afpd, "sys_list_extattr(%s): running out of buffer for EA names", uname);
267 *buflen = attrbuflen;
272 * Function: sys_set_ea
274 * Purpose: set a native EA
278 * vol (r) current volume
280 * attruname (r) EA name
281 * ibuf (r) buffer with EA content
282 * attrsize (r) length EA in ibuf
283 * oflag (r) link and create flag
285 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
290 int sys_set_ea(VFS_FUNC_ARGS_EA_SET)
296 if ((oflag & O_CREAT) )
297 attr_flag |= XATTR_CREATE;
299 else if ((oflag & O_TRUNC) )
300 attr_flag |= XATTR_REPLACE;
302 if ((oflag & O_NOFOLLOW) ) {
303 ret = sys_lsetxattr(uname, attruname, ibuf, attrsize,attr_flag);
306 ret = sys_setxattr(uname, attruname, ibuf, attrsize, attr_flag);
311 case OPEN_NOFOLLOW_ERRNO:
312 /* its a symlink and client requested O_NOFOLLOW */
313 LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s\", ea:'%s'): symlink with kXAttrNoFollow",
317 LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s'): EA already exists",
318 getcwdpath(), uname, attruname);
322 if ((attr_flag & XATTR_REPLACE) && (vol->v_obj->afp_version >= 34))
323 return AFPERR_NOITEM;
326 LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s', size: %u, flags: %s|%s|%s): %s",
327 getcwdpath(), uname, attruname, attrsize,
328 oflag & O_CREAT ? "XATTR_CREATE" : "-",
329 oflag & O_TRUNC ? "XATTR_REPLACE" : "-",
330 oflag & O_NOFOLLOW ? "O_NOFOLLOW" : "-",
340 * Function: sys_remove_ea
342 * Purpose: remove a native EA
346 * vol (r) current volume
348 * attruname (r) EA name
349 * oflag (r) link and create flag
351 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
355 * Removes EA attruname from file uname.
357 int sys_remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
361 if ((oflag & O_NOFOLLOW) ) {
362 ret = sys_lremovexattr(uname, attruname);
365 ret = sys_removexattr(uname, attruname);
370 case OPEN_NOFOLLOW_ERRNO:
371 /* its a symlink and client requested O_NOFOLLOW */
372 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): symlink with kXAttrNoFollow", uname);
375 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
386 * @note Supports *at semantics, therfor switches back and forth between sfd and cwd
388 int sys_ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
393 char *names = NULL, *end_names, *name, *value = NULL;
394 unsigned int setxattr_ENOTSUP = 0;
397 if ((cwd = open(".", O_RDONLY)) == -1) {
398 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant open cwd: %s",
406 if (fchdir(sfd) == -1) {
407 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
414 size = sys_listxattr(src, NULL, 0);
416 if (errno != ENOSYS && errno != ENOTSUP) {
421 names = malloc(size+1);
426 size = sys_listxattr(src, names, size);
432 end_names = names + size;
436 if (fchdir(cwd) == -1) {
437 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
444 for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
447 /* check if this attribute shall be preserved */
451 if (STRCMP(name, ==, AD_EA_META))
455 if (fchdir(sfd) == -1) {
456 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
463 size = sys_getxattr (src, name, NULL, 0);
468 value = realloc(old_value = value, size);
469 if (size != 0 && value == NULL) {
473 size = sys_getxattr(src, name, value, size);
480 if (fchdir(cwd) == -1) {
481 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
488 if (sys_setxattr(dst, name, value, size, 0) != 0) {
489 if (errno == ENOTSUP)
492 if (errno == ENOSYS) {
494 /* no hope of getting any further */
502 if (setxattr_ENOTSUP) {
520 LOG(log_debug, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));
521 return AFPERR_ACCESS;
523 LOG(log_error, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));