1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
/*
Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <atalk/adouble.h>
#include <atalk/ea.h>
#include <atalk/afp.h>
#include <atalk/logger.h>
#include <atalk/volume.h>
#include <atalk/vfs.h>
#include <atalk/util.h>
#include <atalk/unix.h>
#include <atalk/compat.h>
/**********************************************************************************
* EA VFS funcs for storing EAs in nativa filesystem EAs
**********************************************************************************/
/*
* Function: sys_get_easize
*
* Purpose: get size of a native EA
*
* Arguments:
*
* vol (r) current volume
* rbuf (w) DSI reply buffer
* rbuflen (rw) current length of data in reply buffer
* uname (r) filename
* oflag (r) link and create flag
* attruname (r) name of attribute
*
* Returns: AFP code: AFP_OK on success or appropriate AFP error code
*
* Effects:
*
* Copies EA size into rbuf in network order. Increments *rbuflen +4.
*/
int sys_get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
{
ssize_t ret;
uint32_t attrsize;
LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\"", uname, attruname);
/* PBaranski fix */
if (fd != -1) {
LOG(log_debug, logtype_afpd, "sys_get_easize(%s): file is already opened", uname);
ret = sys_fgetxattr(fd, attruname, rbuf +4, 0);
} else {
if ((oflag & O_NOFOLLOW) ) {
ret = sys_lgetxattr(uname, attruname, rbuf +4, 0);
}
else {
ret = sys_getxattr(uname, attruname, rbuf +4, 0);
}
}
/* PBaranski fix */
if (ret == -1) {
memset(rbuf, 0, 4);
*rbuflen += 4;
switch(errno) {
case OPEN_NOFOLLOW_ERRNO:
/* its a symlink and client requested O_NOFOLLOW */
LOG(log_debug, logtype_afpd, "sys_getextattr_size(%s): symlink with kXAttrNoFollow", uname);
return AFPERR_MISC;
case ENOATTR:
case ENOENT:
if (vol->v_obj->afp_version >= 34)
return AFPERR_NOITEM;
return AFPERR_MISC;
default:
LOG(log_debug, logtype_afpd, "sys_getextattr_size: error: %s", strerror(errno));
return AFPERR_MISC;
}
}
if (ret > MAX_EA_SIZE)
ret = MAX_EA_SIZE;
if (vol->v_flags & AFPVOL_EA_SAMBA) {
/* What can we do about xattrs that are 1 byte large? */
if (ret < 2) {
memset(rbuf, 0, 4);
*rbuflen += 4;
if (vol->v_obj->afp_version >= 34) {
return AFPERR_NOITEM;
}
return AFPERR_MISC;
}
ret--;
}
/* Start building reply packet */
LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\", size: %u", uname, attruname, ret);
/* length of attribute data */
attrsize = htonl((uint32_t)ret);
memcpy(rbuf, &attrsize, 4);
*rbuflen += 4;
ret = AFP_OK;
return ret;
}
/*
* Function: sys_get_eacontent
*
* Purpose: copy native EA into rbuf
*
* Arguments:
*
* vol (r) current volume
* rbuf (w) DSI reply buffer
* rbuflen (rw) current length of data in reply buffer
* uname (r) filename
* oflag (r) link and create flag
* attruname (r) name of attribute
* maxreply (r) maximum EA size as of current specs/real-life
* fd (r) file descriptor
* Returns: AFP code: AFP_OK on success or appropriate AFP error code
*
* Effects:
*
* Copies EA into rbuf. Increments *rbuflen accordingly.
*/
int sys_get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
{
ssize_t ret;
uint32_t attrsize;
size_t extra = 0;
#ifdef SOLARIS
/* Protect special attributes set by NFS server */
if (!strcmp(attruname, VIEW_READONLY) || !strcmp(attruname, VIEW_READWRITE))
return AFPERR_ACCESS;
/* Protect special attributes set by SMB server */
if (!strncmp(attruname, SMB_ATTR_PREFIX, SMB_ATTR_PREFIX_LEN))
return AFPERR_ACCESS;
#endif
/* Start building reply packet */
if (maxreply <= MAX_REPLY_EXTRA_BYTES) {
/*
* maxreply must be at least size of xattr + MAX_REPLY_EXTRA_BYTES (6)
* bytes. The 6 bytes are the AFP reply packets bitmap and length field.
*/
memset(rbuf, 0, 4);
*rbuflen += 4;
return AFPERR_PARAM;
}
maxreply -= MAX_REPLY_EXTRA_BYTES;
if (maxreply > MAX_EA_SIZE)
maxreply = MAX_EA_SIZE;
LOG(log_debug7, logtype_afpd, "sys_getextattr_content(%s): attribute: \"%s\", size: %u", uname, attruname, maxreply);
if (vol->v_flags & AFPVOL_EA_SAMBA) {
extra = 1;
}
/* PBaranski fix */
if (fd != -1) {
LOG(log_debug, logtype_afpd, "sys_get_eacontent(%s): file is already opened", uname);
ret = sys_fgetxattr(fd, attruname, rbuf +4, maxreply + extra);
} else {
if ((oflag & O_NOFOLLOW) ) {
ret = sys_lgetxattr(uname, attruname, rbuf +4, maxreply + extra);
}
else {
ret = sys_getxattr(uname, attruname, rbuf +4, maxreply + extra);
}
}
/* PBaranski fix */
if (ret == -1) {
memset(rbuf, 0, 4);
*rbuflen += 4;
switch(errno) {
case OPEN_NOFOLLOW_ERRNO:
/* its a symlink and client requested O_NOFOLLOW */
LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): symlink with kXAttrNoFollow", uname);
return AFPERR_MISC;
case ENOATTR:
if (vol->v_obj->afp_version >= 34)
return AFPERR_NOITEM;
return AFPERR_MISC;
case ERANGE:
return AFPERR_PARAM;
default:
LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): error: %s", attruname, strerror(errno));
return AFPERR_MISC;
}
}
if (vol->v_flags & AFPVOL_EA_SAMBA) {
/* What can we do about xattrs that are 1 byte large? */
if (ret < 2) {
memset(rbuf, 0, 4);
*rbuflen += 4;
if (vol->v_obj->afp_version >= 34) {
return AFPERR_NOITEM;
}
return AFPERR_MISC;
}
ret--;
}
/* remember where we must store length of attribute data in rbuf */
*rbuflen += 4 +ret;
attrsize = htonl((uint32_t)ret);
memcpy(rbuf, &attrsize, 4);
return AFP_OK;
}
/*
* Function: sys_list_eas
*
* Purpose: copy names of native EAs into attrnamebuf
*
* Arguments:
*
* vol (r) current volume
* attrnamebuf (w) store names a consecutive C strings here
* buflen (rw) length of names in attrnamebuf
* uname (r) filename
* oflag (r) link and create flag
*
* Returns: AFP code: AFP_OK on success or appropriate AFP error code
*
* Effects:
*
* Copies names of all EAs of uname as consecutive C strings into rbuf.
* Increments *rbuflen accordingly.
* We hide the adouble:ea extended attributes here, but we currently
* allow reading, writing and deleteting them.
*/
int sys_list_eas(VFS_FUNC_ARGS_EA_LIST)
{
ssize_t attrbuflen = *buflen;
int ret, len, nlen;
char *buf;
char *ptr;
struct adouble ad, *adp;
buf = malloc(ATTRNAMEBUFSIZ);
if (!buf)
return AFPERR_MISC;
/* PBaranski fix */
if ( fd != -1) {
LOG(log_debug, logtype_afpd, "sys_list_eas(%s): file is already opened", uname);
ret = sys_flistxattr(fd, uname, buf, ATTRNAMEBUFSIZ);
} else {
if ((oflag & O_NOFOLLOW)) {
ret = sys_llistxattr(uname, buf, ATTRNAMEBUFSIZ);
}
else {
ret = sys_listxattr(uname, buf, ATTRNAMEBUFSIZ);
}
}
/* PBaranski fix */
if (ret == -1) switch(errno) {
case OPEN_NOFOLLOW_ERRNO:
/* its a symlink and client requested O_NOFOLLOW, we pretend 0 EAs */
LOG(log_debug, logtype_afpd, "sys_list_extattr(%s): symlink with kXAttrNoFollow", uname);
ret = AFP_OK;
goto exit;
default:
LOG(log_debug, logtype_afpd, "sys_list_extattr(%s): error opening attribute dir: %s", uname, strerror(errno));
ret = AFPERR_MISC;
goto exit;
}
ptr = buf;
while (ret > 0) {
len = strlen(ptr);
if (NOT_NETATALK_EA(ptr)) {
/* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
if ( 0 >= ( nlen = convert_string(vol->v_volcharset, CH_UTF8_MAC, ptr, len, attrnamebuf + attrbuflen, 256)) ) {
ret = AFPERR_MISC;
goto exit;
}
LOG(log_debug7, logtype_afpd, "sys_list_extattr(%s): attribute: %s", uname, ptr);
attrbuflen += nlen + 1;
if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
/* Next EA name could overflow, so bail out with error.
FIXME: evantually malloc/memcpy/realloc whatever.
Is it worth it ? */
LOG(log_warning, logtype_afpd, "sys_list_extattr(%s): running out of buffer for EA names", uname);
ret = AFPERR_MISC;
goto exit;
}
}
ret -= len + 1;
ptr += len + 1;
}
ret = AFP_OK;
exit:
free(buf);
*buflen = attrbuflen;
return ret;
}
/*
* Function: sys_set_ea
*
* Purpose: set a native EA
*
* Arguments:
*
* vol (r) current volume
* uname (r) filename
* attruname (r) EA name
* ibuf (r) buffer with EA content
* attrsize (r) length EA in ibuf
* oflag (r) link and create flag
*
* Returns: AFP code: AFP_OK on success or appropriate AFP error code
*
* Effects:
*
*/
int sys_set_ea(VFS_FUNC_ARGS_EA_SET)
{
int attr_flag;
int ret;
char *eabuf;
#ifdef SOLARIS
/* Protect special attributes set by NFS server */
if (!strcmp(attruname, VIEW_READONLY) || !strcmp(attruname, VIEW_READWRITE))
return AFPERR_ACCESS;
/* Protect special attributes set by SMB server */
if (!strncmp(attruname, SMB_ATTR_PREFIX, SMB_ATTR_PREFIX_LEN))
return AFPERR_ACCESS;
#endif
/*
* Buffer for a copy of the xattr plus one byte in case
* AFPVOL_EA_SAMBA is used
*/
eabuf = malloc(attrsize + 1);
if (eabuf == NULL) {
return AFPERR_MISC;
}
memcpy(eabuf, ibuf, attrsize);
eabuf[attrsize] = 0;
attr_flag = 0;
if ((oflag & O_CREAT) )
attr_flag |= XATTR_CREATE;
else if ((oflag & O_TRUNC) )
attr_flag |= XATTR_REPLACE;
if (vol->v_flags & AFPVOL_EA_SAMBA) {
attrsize++;
}
/* PBaranski fix */
if ( fd != -1) {
LOG(log_debug, logtype_afpd, "sys_set_ea(%s): file is already opened", uname);
ret = sys_fsetxattr(fd, attruname, eabuf, attrsize, attr_flag);
} else {
if ((oflag & O_NOFOLLOW) ) {
ret = sys_lsetxattr(uname, attruname, eabuf, attrsize,attr_flag);
}
else {
ret = sys_setxattr(uname, attruname, eabuf, attrsize, attr_flag);
}
}
/* PBaranski fix */
if (ret == -1) {
switch(errno) {
case OPEN_NOFOLLOW_ERRNO:
/* its a symlink and client requested O_NOFOLLOW */
LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s\", ea:'%s'): symlink with kXAttrNoFollow",
uname, attruname);
return AFP_OK;
case EEXIST:
LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s'): EA already exists",
getcwdpath(), uname, attruname);
return AFPERR_EXIST;
case ENOATTR:
case ENOENT:
if ((attr_flag & XATTR_REPLACE) && (vol->v_obj->afp_version >= 34))
return AFPERR_NOITEM;
return AFPERR_MISC;
default:
LOG(log_debug, logtype_afpd, "sys_set_ea(\"%s/%s\", ea:'%s', size: %u, flags: %s|%s|%s): %s",
getcwdpath(), uname, attruname, attrsize,
oflag & O_CREAT ? "XATTR_CREATE" : "-",
oflag & O_TRUNC ? "XATTR_REPLACE" : "-",
oflag & O_NOFOLLOW ? "O_NOFOLLOW" : "-",
strerror(errno));
return AFPERR_MISC;
}
}
return AFP_OK;
}
/*
* Function: sys_remove_ea
*
* Purpose: remove a native EA
*
* Arguments:
*
* vol (r) current volume
* uname (r) filename
* attruname (r) EA name
* oflag (r) link and create flag
* fd (r) file descriptor
*
* Returns: AFP code: AFP_OK on success or appropriate AFP error code
*
* Effects:
*
* Removes EA attruname from file uname.
*/
int sys_remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
{
int ret;
#ifdef SOLARIS
/* Protect special attributes set by NFS server */
if (!strcmp(attruname, VIEW_READONLY) || !strcmp(attruname, VIEW_READWRITE))
return AFPERR_ACCESS;
/* Protect special attributes set by SMB server */
if (!strncmp(attruname, SMB_ATTR_PREFIX, SMB_ATTR_PREFIX_LEN))
return AFPERR_ACCESS;
#endif
/* PBaranski fix */
if ( fd != -1) {
LOG(log_debug, logtype_afpd, "sys_remove_ea(%s): file is already opened", uname);
ret = sys_fremovexattr(fd, uname, attruname);
} else {
if ((oflag & O_NOFOLLOW) ) {
ret = sys_lremovexattr(uname, attruname);
}
else {
ret = sys_removexattr(uname, attruname);
}
}
/* PBaranski fix */
if (ret == -1) {
switch(errno) {
case OPEN_NOFOLLOW_ERRNO:
/* its a symlink and client requested O_NOFOLLOW */
LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): symlink with kXAttrNoFollow", uname);
return AFP_OK;
default:
LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
return AFPERR_MISC;
}
}
return AFP_OK;
}
/*
* Function: sys_ea_copyfile
*
* Purpose: copy EAs
*
* Arguments:
*
* vol (r) current volume
* sdf (r) source file descriptor
* src (r) source path
* dst (r) destination path
*
* Return AFP code AFP_OK on success or appropriate AFP error code
*
* Effects:
*
* Copies EAs from source file to dest file.
*/
int sys_ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
{
int ret = 0;
int cwd = -1;
ssize_t size;
char *names = NULL, *end_names, *name, *value = NULL;
unsigned int setxattr_ENOTSUP = 0;
if (sfd != -1) {
if ((cwd = open(".", O_RDONLY)) == -1) {
LOG(log_error, logtype_afpd, "sys_ea_copyfile: can't open cwd: %s",
strerror(errno));
ret = -1;
goto getout;
}
}
if (sfd != -1) {
if (fchdir(sfd) == -1) {
LOG(log_error, logtype_afpd, "sys_ea_copyfile: can't chdir to sfd: %s",
strerror(errno));
ret = -1;
goto getout;
}
}
size = sys_listxattr(src, NULL, 0);
if (size < 0) {
if (errno != ENOSYS && errno != ENOTSUP) {
ret = -1;
}
goto getout;
}
names = malloc(size+1);
if (names == NULL) {
ret = -1;
goto getout;
}
size = sys_listxattr(src, names, size);
if (size < 0) {
ret = -1;
goto getout;
} else {
names[size] = '\0';
end_names = names + size;
}
if (sfd != -1) {
if (fchdir(cwd) == -1) {
LOG(log_error, logtype_afpd, "sys_ea_copyfile: can't chdir to cwd: %s",
strerror(errno));
ret = -1;
goto getout;
}
}
for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
void *old_value;
/* check if this attribute shall be preserved */
if (!*name)
continue;
if (STRCMP(name, ==, AD_EA_META))
continue;
#ifdef SOLARIS
/* Skip special attributes set by NFS server */
if (!strcmp(name, VIEW_READONLY) || !strcmp(name, VIEW_READWRITE))
continue;
/* Skip special attributes set by SMB server */
if (!strncmp(name, SMB_ATTR_PREFIX, SMB_ATTR_PREFIX_LEN))
continue;
#endif
if (sfd != -1) {
if (fchdir(sfd) == -1) {
LOG(log_error, logtype_afpd, "sys_ea_copyfile: can't chdir to sfd: %s",
strerror(errno));
ret = -1;
goto getout;
}
}
size = sys_getxattr (src, name, NULL, 0);
if (size < 0) {
ret = -1;
continue;
}
value = realloc(old_value = value, size);
if (size != 0 && value == NULL) {
free(old_value);
ret = -1;
}
size = sys_getxattr(src, name, value, size);
if (size < 0) {
ret = -1;
continue;
}
if (sfd != -1) {
if (fchdir(cwd) == -1) {
LOG(log_error, logtype_afpd, "sys_ea_copyfile: can't chdir to cwd: %s",
strerror(errno));
ret = -1;
goto getout;
}
}
if (sys_setxattr(dst, name, value, size, 0) != 0) {
if (errno == ENOTSUP)
setxattr_ENOTSUP++;
else {
if (errno == ENOSYS) {
ret = -1;
/* no hope of getting any further */
break;
} else {
ret = -1;
}
}
}
}
if (setxattr_ENOTSUP) {
errno = ENOTSUP;
ret = -1;
}
getout:
if (cwd != -1)
close(cwd);
free(value);
free(names);
if (ret == -1) {
switch(errno) {
case ENOENT:
/* no attribute */
break;
case EACCES:
LOG(log_debug, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));
return AFPERR_ACCESS;
default:
LOG(log_error, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));
return AFPERR_MISC;
}
}
return AFP_OK;
}
|