Home | History | Annotate | Download | only in src
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel (at) 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.haxx.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.origin.url", CURLINFO_EFFECTIVE_URL },
     48   { "user.mime_type",      CURLINFO_CONTENT_TYPE },
     49   { NULL,                  CURLINFO_NONE } /* last element, abort loop here */
     50 };
     51 
     52 /* returns TRUE if a new URL is returned, that then needs to be freed */
     53 /* @unittest: 1621 */
     54 #ifdef UNITTESTS
     55 bool stripcredentials(char **url);
     56 #else
     57 static
     58 #endif
     59 bool stripcredentials(char **url)
     60 {
     61   CURLU *u;
     62   CURLUcode uc;
     63   char *nurl;
     64   u = curl_url();
     65   if(u) {
     66     uc = curl_url_set(u, CURLUPART_URL, *url, 0);
     67     if(uc)
     68       goto error;
     69 
     70     uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
     71     if(uc)
     72       goto error;
     73 
     74     uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
     75     if(uc)
     76       goto error;
     77 
     78     uc = curl_url_get(u, CURLUPART_URL, &nurl, 0);
     79     if(uc)
     80       goto error;
     81 
     82     curl_url_cleanup(u);
     83 
     84     *url = nurl;
     85     return TRUE;
     86   }
     87   error:
     88   curl_url_cleanup(u);
     89   return FALSE;
     90 }
     91 
     92 /* store metadata from the curl request alongside the downloaded
     93  * file using extended attributes
     94  */
     95 int fwrite_xattr(CURL *curl, int fd)
     96 {
     97   int i = 0;
     98   int err = 0;
     99 
    100   /* loop through all xattr-curlinfo pairs and abort on a set error */
    101   while(err == 0 && mappings[i].attr != NULL) {
    102     char *value = NULL;
    103     CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
    104     if(!result && value) {
    105       bool freeptr = FALSE;
    106       if(CURLINFO_EFFECTIVE_URL == mappings[i].info)
    107         freeptr = stripcredentials(&value);
    108       if(value) {
    109 #ifdef HAVE_FSETXATTR_6
    110         err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
    111 #elif defined(HAVE_FSETXATTR_5)
    112         err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
    113 #elif defined(__FreeBSD_version)
    114         {
    115           ssize_t rc = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER,
    116                                       mappings[i].attr, value, strlen(value));
    117           /* FreeBSD's extattr_set_fd returns the length of the extended
    118              attribute */
    119           err = (rc < 0 ? -1 : 0);
    120         }
    121 #endif
    122         if(freeptr)
    123           curl_free(value);
    124       }
    125     }
    126     i++;
    127   }
    128 
    129   return err;
    130 }
    131 #else
    132 int fwrite_xattr(CURL *curl, int fd)
    133 {
    134   (void)curl;
    135   (void)fd;
    136 
    137   return 0;
    138 }
    139 #endif
    140