tool_xattr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #ifdef HAVE_FSETXATTR
  26. # include <sys/xattr.h> /* header from libc, not from libattr */
  27. # define USE_XATTR
  28. #elif (defined(__FreeBSD_version) && (__FreeBSD_version > 500000)) || \
  29. defined(__MidnightBSD_version)
  30. # include <sys/types.h>
  31. # include <sys/extattr.h>
  32. # define USE_XATTR
  33. #endif
  34. #include "tool_xattr.h"
  35. #include "memdebug.h" /* keep this as LAST include */
  36. #ifdef USE_XATTR
  37. /* mapping table of curl metadata to extended attribute names */
  38. static const struct xattr_mapping {
  39. const char *attr; /* name of the xattr */
  40. CURLINFO info;
  41. } mappings[] = {
  42. /* mappings proposed by
  43. * https://freedesktop.org/wiki/CommonExtendedAttributes/
  44. */
  45. { "user.xdg.referrer.url", CURLINFO_REFERER },
  46. { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
  47. { "user.mime_type", CURLINFO_CONTENT_TYPE },
  48. { NULL, CURLINFO_NONE } /* last element, abort here */
  49. };
  50. /* returns TRUE if a new URL is returned, that then needs to be freed */
  51. /* @unittest: 1621 */
  52. #ifdef UNITTESTS
  53. bool stripcredentials(char **url);
  54. #else
  55. static
  56. #endif
  57. bool stripcredentials(char **url)
  58. {
  59. CURLU *u;
  60. CURLUcode uc;
  61. char *nurl;
  62. u = curl_url();
  63. if(u) {
  64. uc = curl_url_set(u, CURLUPART_URL, *url, 0);
  65. if(uc)
  66. goto error;
  67. uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
  68. if(uc)
  69. goto error;
  70. uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
  71. if(uc)
  72. goto error;
  73. uc = curl_url_get(u, CURLUPART_URL, &nurl, 0);
  74. if(uc)
  75. goto error;
  76. curl_url_cleanup(u);
  77. *url = nurl;
  78. return TRUE;
  79. }
  80. error:
  81. curl_url_cleanup(u);
  82. return FALSE;
  83. }
  84. /* store metadata from the curl request alongside the downloaded
  85. * file using extended attributes
  86. */
  87. int fwrite_xattr(CURL *curl, int fd)
  88. {
  89. int i = 0;
  90. int err = 0;
  91. /* loop through all xattr-curlinfo pairs and abort on a set error */
  92. while(err == 0 && mappings[i].attr) {
  93. char *value = NULL;
  94. CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
  95. if(!result && value) {
  96. bool freeptr = FALSE;
  97. if(CURLINFO_EFFECTIVE_URL == mappings[i].info)
  98. freeptr = stripcredentials(&value);
  99. if(value) {
  100. #ifdef HAVE_FSETXATTR_6
  101. err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
  102. #elif defined(HAVE_FSETXATTR_5)
  103. err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
  104. #elif defined(__FreeBSD_version) || defined(__MidnightBSD_version)
  105. {
  106. ssize_t rc = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER,
  107. mappings[i].attr, value, strlen(value));
  108. /* FreeBSD's extattr_set_fd returns the length of the extended
  109. attribute */
  110. err = (rc < 0 ? -1 : 0);
  111. }
  112. #endif
  113. if(freeptr)
  114. curl_free(value);
  115. }
  116. }
  117. i++;
  118. }
  119. return err;
  120. }
  121. #else
  122. int fwrite_xattr(CURL *curl, int fd)
  123. {
  124. (void)curl;
  125. (void)fd;
  126. return 0;
  127. }
  128. #endif