2010-11-05 09:07:38 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-01-02 14:18:27 -05:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-11-05 09:07:38 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2010-11-05 09:07:38 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2010-11-05 07:39:46 -04:00
|
|
|
|
2010-11-07 10:54:49 -05:00
|
|
|
#ifdef HAVE_FSETXATTR
|
2011-10-06 11:39:00 -04:00
|
|
|
# include <sys/xattr.h> /* header from libc, not from libattr */
|
2013-10-01 15:57:14 -04:00
|
|
|
# define USE_XATTR
|
|
|
|
#elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/extattr.h>
|
|
|
|
# define USE_XATTR
|
2011-09-14 05:27:12 -04:00
|
|
|
#endif
|
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_xattr.h"
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2013-10-01 15:57:14 -04:00
|
|
|
#ifdef USE_XATTR
|
2010-11-05 09:07:38 -04:00
|
|
|
|
2010-11-05 07:39:46 -04:00
|
|
|
/* mapping table of curl metadata to extended attribute names */
|
2010-11-09 21:47:16 -05:00
|
|
|
static const struct xattr_mapping {
|
2010-11-08 03:10:33 -05:00
|
|
|
const char *attr; /* name of the xattr */
|
2010-11-05 07:39:46 -04:00
|
|
|
CURLINFO info;
|
|
|
|
} mappings[] = {
|
|
|
|
/* mappings proposed by
|
2017-08-08 15:22:34 -04:00
|
|
|
* https://freedesktop.org/wiki/CommonExtendedAttributes/
|
2010-11-05 07:39:46 -04:00
|
|
|
*/
|
|
|
|
{ "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
|
2011-10-06 11:39:00 -04:00
|
|
|
{ "user.mime_type", CURLINFO_CONTENT_TYPE },
|
|
|
|
{ NULL, CURLINFO_NONE } /* last element, abort loop here */
|
2010-11-05 07:39:46 -04:00
|
|
|
};
|
|
|
|
|
2019-01-02 14:18:27 -05:00
|
|
|
/* returns TRUE if a new URL is returned, that then needs to be freed */
|
|
|
|
/* @unittest: 1621 */
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
bool stripcredentials(char **url);
|
|
|
|
#else
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
bool stripcredentials(char **url)
|
|
|
|
{
|
|
|
|
CURLU *u;
|
|
|
|
CURLUcode uc;
|
|
|
|
char *nurl;
|
|
|
|
u = curl_url();
|
|
|
|
if(u) {
|
|
|
|
uc = curl_url_set(u, CURLUPART_URL, *url, 0);
|
|
|
|
if(uc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
|
|
|
|
if(uc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
|
|
|
|
if(uc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
uc = curl_url_get(u, CURLUPART_URL, &nurl, 0);
|
|
|
|
if(uc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
curl_url_cleanup(u);
|
|
|
|
|
|
|
|
*url = nurl;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
error:
|
|
|
|
curl_url_cleanup(u);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-05 07:39:46 -04:00
|
|
|
/* store metadata from the curl request alongside the downloaded
|
|
|
|
* file using extended attributes
|
|
|
|
*/
|
2010-11-07 10:54:49 -05:00
|
|
|
int fwrite_xattr(CURL *curl, int fd)
|
2010-11-05 07:39:46 -04:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int err = 0;
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-09 21:47:16 -05:00
|
|
|
/* loop through all xattr-curlinfo pairs and abort on a set error */
|
2011-04-25 16:44:39 -04:00
|
|
|
while(err == 0 && mappings[i].attr != NULL) {
|
2010-11-05 07:39:46 -04:00
|
|
|
char *value = NULL;
|
2014-12-25 06:23:44 -05:00
|
|
|
CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
|
|
|
|
if(!result && value) {
|
2019-01-02 14:18:27 -05:00
|
|
|
bool freeptr = FALSE;
|
|
|
|
if(CURLINFO_EFFECTIVE_URL == mappings[i].info)
|
|
|
|
freeptr = stripcredentials(&value);
|
|
|
|
if(value) {
|
2010-11-10 12:39:44 -05:00
|
|
|
#ifdef HAVE_FSETXATTR_6
|
2019-01-02 14:18:27 -05:00
|
|
|
err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
|
2010-11-10 12:39:44 -05:00
|
|
|
#elif defined(HAVE_FSETXATTR_5)
|
2019-01-02 14:18:27 -05:00
|
|
|
err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
|
2013-10-01 15:57:14 -04:00
|
|
|
#elif defined(__FreeBSD_version)
|
2019-02-11 04:09:18 -05:00
|
|
|
{
|
|
|
|
ssize_t rc = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER,
|
|
|
|
mappings[i].attr, value, strlen(value));
|
|
|
|
/* FreeBSD's extattr_set_fd returns the length of the extended
|
|
|
|
attribute */
|
2019-02-11 08:22:36 -05:00
|
|
|
err = (rc < 0 ? -1 : 0);
|
2019-02-11 04:09:18 -05:00
|
|
|
}
|
2010-11-10 12:39:44 -05:00
|
|
|
#endif
|
2019-01-02 14:18:27 -05:00
|
|
|
if(freeptr)
|
|
|
|
curl_free(value);
|
|
|
|
}
|
2010-11-05 07:39:46 -04:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-05 07:39:46 -04:00
|
|
|
return err;
|
|
|
|
}
|
2010-11-05 09:07:38 -04:00
|
|
|
#else
|
2010-11-07 10:54:49 -05:00
|
|
|
int fwrite_xattr(CURL *curl, int fd)
|
2010-11-05 09:07:38 -04:00
|
|
|
{
|
|
|
|
(void)curl;
|
2010-11-07 10:54:49 -05:00
|
|
|
(void)fd;
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-05 09:07:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|