1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, 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 ***************************************************************************/
22 #include "tool_setup.h"
23
24 #ifdef HAVE_FSETXATTR
25 # include <sys/xattr.h> /* header from libc, not from libattr */
26 # define USE_XATTR
27 #elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
28 # include <sys/types.h>
29 # include <sys/extattr.h>
30 # define USE_XATTR
31 #endif
32
33 #include "tool_xattr.h"
34
35 #include "memdebug.h" /* keep this as LAST include */
36
37 #ifdef USE_XATTR
38
39 /* mapping table of curl metadata to extended attribute names */
40 static const struct xattr_mapping {
41 const char *attr; /* name of the xattr */
42 CURLINFO info;
43 } mappings[] = {
44 /* mappings proposed by
45 * https://freedesktop.org/wiki/CommonExtendedAttributes/
46 */
47 { "user.xdg.referrer.url", CURLINFO_REFERER },
48 { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
49 { "user.mime_type", CURLINFO_CONTENT_TYPE },
50 { NULL, CURLINFO_NONE } /* last element, abort here */
51 };
52
53 /* returns TRUE if a new URL is returned, that then needs to be freed */
54 /* @unittest: 1621 */
55 #ifdef UNITTESTS
56 bool stripcredentials(char **url);
57 #else
58 static
59 #endif
stripcredentials(char ** url)60 bool stripcredentials(char **url)
61 {
62 CURLU *u;
63 CURLUcode uc;
64 char *nurl;
65 u = curl_url();
66 if(u) {
67 uc = curl_url_set(u, CURLUPART_URL, *url, 0);
68 if(uc)
69 goto error;
70
71 uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
72 if(uc)
73 goto error;
74
75 uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
76 if(uc)
77 goto error;
78
79 uc = curl_url_get(u, CURLUPART_URL, &nurl, 0);
80 if(uc)
81 goto error;
82
83 curl_url_cleanup(u);
84
85 *url = nurl;
86 return TRUE;
87 }
88 error:
89 curl_url_cleanup(u);
90 return FALSE;
91 }
92
93 /* store metadata from the curl request alongside the downloaded
94 * file using extended attributes
95 */
fwrite_xattr(CURL * curl,int fd)96 int fwrite_xattr(CURL *curl, int fd)
97 {
98 int i = 0;
99 int err = 0;
100
101 /* loop through all xattr-curlinfo pairs and abort on a set error */
102 while(err == 0 && mappings[i].attr != NULL) {
103 char *value = NULL;
104 CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
105 if(!result && value) {
106 bool freeptr = FALSE;
107 if(CURLINFO_EFFECTIVE_URL == mappings[i].info)
108 freeptr = stripcredentials(&value);
109 if(value) {
110 #ifdef HAVE_FSETXATTR_6
111 err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
112 #elif defined(HAVE_FSETXATTR_5)
113 err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
114 #elif defined(__FreeBSD_version)
115 {
116 ssize_t rc = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER,
117 mappings[i].attr, value, strlen(value));
118 /* FreeBSD's extattr_set_fd returns the length of the extended
119 attribute */
120 err = (rc < 0 ? -1 : 0);
121 }
122 #endif
123 if(freeptr)
124 curl_free(value);
125 }
126 }
127 i++;
128 }
129
130 return err;
131 }
132 #else
fwrite_xattr(CURL * curl,int fd)133 int fwrite_xattr(CURL *curl, int fd)
134 {
135 (void)curl;
136 (void)fd;
137
138 return 0;
139 }
140 #endif
141