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): encountered symlink with kXAttrNoFollow", uname);
91 LOG(log_error, logtype_afpd, "sys_getextattr_size: error: %s", strerror(errno));
96 if (ret > MAX_EA_SIZE)
99 /* Start building reply packet */
100 LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\", size: %u", uname, attruname, ret);
102 /* length of attribute data */
103 attrsize = htonl((uint32_t)ret);
104 memcpy(rbuf, &attrsize, 4);
112 * Function: sys_get_eacontent
114 * Purpose: copy native EA into rbuf
118 * vol (r) current volume
119 * rbuf (w) DSI reply buffer
120 * rbuflen (rw) current length of data in reply buffer
122 * oflag (r) link and create flag
123 * attruname (r) name of attribute
124 * maxreply (r) maximum EA size as of current specs/real-life
126 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
130 * Copies EA into rbuf. Increments *rbuflen accordingly.
132 int sys_get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
137 /* Start building reply packet */
139 maxreply -= MAX_REPLY_EXTRA_BYTES;
141 if (maxreply > MAX_EA_SIZE)
142 maxreply = MAX_EA_SIZE;
144 LOG(log_debug7, logtype_afpd, "sys_getextattr_content(%s): attribute: \"%s\", size: %u", uname, attruname, maxreply);
146 if ((oflag & O_NOFOLLOW) ) {
147 ret = sys_lgetxattr(uname, attruname, rbuf +4, maxreply);
150 ret = sys_getxattr(uname, attruname, rbuf +4, maxreply);
157 case OPEN_NOFOLLOW_ERRNO:
158 /* its a symlink and client requested O_NOFOLLOW */
159 LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): encountered symlink with kXAttrNoFollow", uname);
166 LOG(log_error, logtype_afpd, "sys_getextattr_content(%s): error: %s", attruname, strerror(errno));
171 /* remember where we must store length of attribute data in rbuf */
174 attrsize = htonl((uint32_t)ret);
175 memcpy(rbuf, &attrsize, 4);
181 * Function: sys_list_eas
183 * Purpose: copy names of native EAs into attrnamebuf
187 * vol (r) current volume
188 * attrnamebuf (w) store names a consecutive C strings here
189 * buflen (rw) length of names in attrnamebuf
191 * oflag (r) link and create flag
193 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
197 * Copies names of all EAs of uname as consecutive C strings into rbuf.
198 * Increments *rbuflen accordingly.
199 * We hide the adouble:ea extended attributes here, but we currently
200 * allow reading, writing and deleteting them.
202 int sys_list_eas(VFS_FUNC_ARGS_EA_LIST)
204 ssize_t attrbuflen = *buflen;
209 buf = malloc(ATTRNAMEBUFSIZ);
213 if ((oflag & O_NOFOLLOW)) {
214 ret = sys_llistxattr(uname, buf, ATTRNAMEBUFSIZ);
217 ret = sys_listxattr(uname, buf, ATTRNAMEBUFSIZ);
220 if (ret == -1) switch(errno) {
221 case OPEN_NOFOLLOW_ERRNO:
222 /* its a symlink and client requested O_NOFOLLOW */
223 ret = AFPERR_BADTYPE;
225 #ifdef HAVE_ATTROPEN /* Solaris */
231 LOG(log_error, logtype_afpd, "sys_list_extattr(%s): error opening atttribute dir: %s", uname, strerror(errno));
239 if (NOT_NETATALK_EA(ptr)) {
240 /* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
241 if ( 0 >= ( nlen = convert_string(vol->v_volcharset, CH_UTF8_MAC, ptr, len, attrnamebuf + attrbuflen, 256)) ) {
246 LOG(log_debug7, logtype_afpd, "sys_list_extattr(%s): attribute: %s", uname, ptr);
248 attrbuflen += nlen + 1;
249 if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
250 /* Next EA name could overflow, so bail out with error.
251 FIXME: evantually malloc/memcpy/realloc whatever.
253 LOG(log_warning, logtype_afpd, "sys_list_extattr(%s): running out of buffer for EA names", uname);
266 *buflen = attrbuflen;
271 * Function: sys_set_ea
273 * Purpose: set a native EA
277 * vol (r) current volume
279 * attruname (r) EA name
280 * ibuf (r) buffer with EA content
281 * attrsize (r) length EA in ibuf
282 * oflag (r) link and create flag
284 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
289 int sys_set_ea(VFS_FUNC_ARGS_EA_SET)
295 if ((oflag & O_CREAT) )
296 attr_flag |= XATTR_CREATE;
298 else if ((oflag & O_TRUNC) )
299 attr_flag |= XATTR_REPLACE;
301 if ((oflag & O_NOFOLLOW) ) {
302 ret = sys_lsetxattr(uname, attruname, ibuf, attrsize,attr_flag);
305 ret = sys_setxattr(uname, attruname, ibuf, attrsize, attr_flag);
310 case OPEN_NOFOLLOW_ERRNO:
311 /* its a symlink and client requested O_NOFOLLOW */
312 LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s'): encountered symlink with kXAttrNoFollow",
313 getcwdpath(), uname, attruname);
316 LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s'): EA already exists",
317 getcwdpath(), uname, attruname);
320 LOG(log_error, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s', size: %u, flags: %s|%s|%s): %s",
321 getcwdpath(), uname, attruname, attrsize,
322 oflag & O_CREAT ? "XATTR_CREATE" : "-",
323 oflag & O_TRUNC ? "XATTR_REPLACE" : "-",
324 oflag & O_NOFOLLOW ? "O_NOFOLLOW" : "-",
334 * Function: sys_remove_ea
336 * Purpose: remove a native EA
340 * vol (r) current volume
342 * attruname (r) EA name
343 * oflag (r) link and create flag
345 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
349 * Removes EA attruname from file uname.
351 int sys_remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
355 if ((oflag & O_NOFOLLOW) ) {
356 ret = sys_lremovexattr(uname, attruname);
359 ret = sys_removexattr(uname, attruname);
364 case OPEN_NOFOLLOW_ERRNO:
365 /* its a symlink and client requested O_NOFOLLOW */
366 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): encountered symlink with kXAttrNoFollow", uname);
369 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
370 return AFPERR_ACCESS;
372 LOG(log_error, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
383 * @note Supports *at semantics, therfor switches back and forth between sfd and cwd
385 int sys_ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
390 char *names = NULL, *end_names, *name, *value = NULL;
391 unsigned int setxattr_ENOTSUP = 0;
394 if ((cwd = open(".", O_RDONLY)) == -1) {
395 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant open cwd: %s",
403 if (fchdir(sfd) == -1) {
404 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
411 size = sys_listxattr(src, NULL, 0);
413 if (errno != ENOSYS && errno != ENOTSUP) {
418 names = malloc(size+1);
423 size = sys_listxattr(src, names, size);
429 end_names = names + size;
433 if (fchdir(cwd) == -1) {
434 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
441 for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
444 /* check if this attribute shall be preserved */
449 if (fchdir(sfd) == -1) {
450 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
457 size = sys_getxattr (src, name, NULL, 0);
462 value = realloc(old_value = value, size);
463 if (size != 0 && value == NULL) {
467 size = sys_getxattr(src, name, value, size);
474 if (fchdir(cwd) == -1) {
475 LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
482 if (sys_setxattr(dst, name, value, size, 0) != 0) {
483 if (errno == ENOTSUP)
486 if (errno == ENOSYS) {
488 /* no hope of getting any further */
496 if (setxattr_ENOTSUP) {
514 LOG(log_debug, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));
515 return AFPERR_ACCESS;
517 LOG(log_error, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));