113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "tool_setup.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#define ENABLE_CURLX_PRINTF
2713498266Sopenharmony_ci/* use our own printf() functions */
2813498266Sopenharmony_ci#include "curlx.h"
2913498266Sopenharmony_ci#include "dynbuf.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#include "tool_cfgable.h"
3213498266Sopenharmony_ci#include "tool_msgs.h"
3313498266Sopenharmony_ci#include "tool_ipfs.h"
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci#include "memdebug.h" /* keep this as LAST include */
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci/* ensure input ends in slash */
3813498266Sopenharmony_cistatic CURLcode ensure_trailing_slash(char **input)
3913498266Sopenharmony_ci{
4013498266Sopenharmony_ci  if(*input && **input) {
4113498266Sopenharmony_ci    size_t len = strlen(*input);
4213498266Sopenharmony_ci    if(((*input)[len - 1] != '/')) {
4313498266Sopenharmony_ci      struct curlx_dynbuf dyn;
4413498266Sopenharmony_ci      curlx_dyn_init(&dyn, len + 2);
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci      if(curlx_dyn_addn(&dyn, *input, len)) {
4713498266Sopenharmony_ci        Curl_safefree(*input);
4813498266Sopenharmony_ci        return CURLE_OUT_OF_MEMORY;
4913498266Sopenharmony_ci      }
5013498266Sopenharmony_ci
5113498266Sopenharmony_ci      Curl_safefree(*input);
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci      if(curlx_dyn_addn(&dyn, "/", 1))
5413498266Sopenharmony_ci        return CURLE_OUT_OF_MEMORY;
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci      *input = curlx_dyn_ptr(&dyn);
5713498266Sopenharmony_ci    }
5813498266Sopenharmony_ci  }
5913498266Sopenharmony_ci
6013498266Sopenharmony_ci  return CURLE_OK;
6113498266Sopenharmony_ci}
6213498266Sopenharmony_ci
6313498266Sopenharmony_cistatic char *ipfs_gateway(void)
6413498266Sopenharmony_ci{
6513498266Sopenharmony_ci  char *ipfs_path = NULL;
6613498266Sopenharmony_ci  char *gateway_composed_file_path = NULL;
6713498266Sopenharmony_ci  FILE *gateway_file = NULL;
6813498266Sopenharmony_ci  char *gateway = curlx_getenv("IPFS_GATEWAY");
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  /* Gateway is found from environment variable. */
7113498266Sopenharmony_ci  if(gateway) {
7213498266Sopenharmony_ci    if(ensure_trailing_slash(&gateway))
7313498266Sopenharmony_ci      goto fail;
7413498266Sopenharmony_ci    return gateway;
7513498266Sopenharmony_ci  }
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci  /* Try to find the gateway in the IPFS data folder. */
7813498266Sopenharmony_ci  ipfs_path = curlx_getenv("IPFS_PATH");
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  if(!ipfs_path) {
8113498266Sopenharmony_ci    char *home = curlx_getenv("HOME");
8213498266Sopenharmony_ci    if(home && *home)
8313498266Sopenharmony_ci      ipfs_path = aprintf("%s/.ipfs/", home);
8413498266Sopenharmony_ci    /* fallback to "~/.ipfs", as that's the default location. */
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci    Curl_safefree(home);
8713498266Sopenharmony_ci  }
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci  if(!ipfs_path || ensure_trailing_slash(&ipfs_path))
9013498266Sopenharmony_ci    goto fail;
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  gateway_composed_file_path = aprintf("%sgateway", ipfs_path);
9313498266Sopenharmony_ci
9413498266Sopenharmony_ci  if(!gateway_composed_file_path)
9513498266Sopenharmony_ci    goto fail;
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci  gateway_file = fopen(gateway_composed_file_path, FOPEN_READTEXT);
9813498266Sopenharmony_ci  Curl_safefree(gateway_composed_file_path);
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci  if(gateway_file) {
10113498266Sopenharmony_ci    int c;
10213498266Sopenharmony_ci    struct curlx_dynbuf dyn;
10313498266Sopenharmony_ci    curlx_dyn_init(&dyn, MAX_GATEWAY_URL_LEN);
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci    /* get the first line of the gateway file, ignore the rest */
10613498266Sopenharmony_ci    while((c = getc(gateway_file)) != EOF && c != '\n' && c != '\r') {
10713498266Sopenharmony_ci      char c_char = (char)c;
10813498266Sopenharmony_ci      if(curlx_dyn_addn(&dyn, &c_char, 1))
10913498266Sopenharmony_ci        goto fail;
11013498266Sopenharmony_ci    }
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci    fclose(gateway_file);
11313498266Sopenharmony_ci    gateway_file = NULL;
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci    if(curlx_dyn_len(&dyn))
11613498266Sopenharmony_ci      gateway = curlx_dyn_ptr(&dyn);
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci    if(gateway)
11913498266Sopenharmony_ci      ensure_trailing_slash(&gateway);
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci    if(!gateway)
12213498266Sopenharmony_ci      goto fail;
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci    Curl_safefree(ipfs_path);
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci    return gateway;
12713498266Sopenharmony_ci  }
12813498266Sopenharmony_cifail:
12913498266Sopenharmony_ci  if(gateway_file)
13013498266Sopenharmony_ci    fclose(gateway_file);
13113498266Sopenharmony_ci  Curl_safefree(gateway);
13213498266Sopenharmony_ci  Curl_safefree(ipfs_path);
13313498266Sopenharmony_ci  return NULL;
13413498266Sopenharmony_ci}
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci/*
13713498266Sopenharmony_ci * Rewrite ipfs://<cid> and ipns://<cid> to a HTTP(S)
13813498266Sopenharmony_ci * URL that can be handled by an IPFS gateway.
13913498266Sopenharmony_ci */
14013498266Sopenharmony_ciCURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url,
14113498266Sopenharmony_ci                          struct OperationConfig *config)
14213498266Sopenharmony_ci{
14313498266Sopenharmony_ci  CURLcode result = CURLE_URL_MALFORMAT;
14413498266Sopenharmony_ci  CURLUcode getResult;
14513498266Sopenharmony_ci  char *gateway = NULL;
14613498266Sopenharmony_ci  char *gwhost = NULL;
14713498266Sopenharmony_ci  char *gwpath = NULL;
14813498266Sopenharmony_ci  char *gwquery = NULL;
14913498266Sopenharmony_ci  char *gwscheme = NULL;
15013498266Sopenharmony_ci  char *gwport = NULL;
15113498266Sopenharmony_ci  char *inputpath = NULL;
15213498266Sopenharmony_ci  char *cid = NULL;
15313498266Sopenharmony_ci  char *pathbuffer = NULL;
15413498266Sopenharmony_ci  char *cloneurl;
15513498266Sopenharmony_ci  CURLU *gatewayurl = curl_url();
15613498266Sopenharmony_ci
15713498266Sopenharmony_ci  if(!gatewayurl) {
15813498266Sopenharmony_ci    result = CURLE_FAILED_INIT;
15913498266Sopenharmony_ci    goto clean;
16013498266Sopenharmony_ci  }
16113498266Sopenharmony_ci
16213498266Sopenharmony_ci  getResult = curl_url_get(uh, CURLUPART_HOST, &cid, CURLU_URLDECODE);
16313498266Sopenharmony_ci  if(getResult || !cid)
16413498266Sopenharmony_ci    goto clean;
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci  /* We might have a --ipfs-gateway argument. Check it first and use it. Error
16713498266Sopenharmony_ci   * if we do have something but if it's an invalid url.
16813498266Sopenharmony_ci   */
16913498266Sopenharmony_ci  if(config->ipfs_gateway) {
17013498266Sopenharmony_ci    /* ensure the gateway ends in a trailing / */
17113498266Sopenharmony_ci    if(ensure_trailing_slash(&config->ipfs_gateway) != CURLE_OK) {
17213498266Sopenharmony_ci      result = CURLE_OUT_OF_MEMORY;
17313498266Sopenharmony_ci      goto clean;
17413498266Sopenharmony_ci    }
17513498266Sopenharmony_ci
17613498266Sopenharmony_ci    if(!curl_url_set(gatewayurl, CURLUPART_URL, config->ipfs_gateway,
17713498266Sopenharmony_ci                    CURLU_GUESS_SCHEME)) {
17813498266Sopenharmony_ci      gateway = strdup(config->ipfs_gateway);
17913498266Sopenharmony_ci      if(!gateway) {
18013498266Sopenharmony_ci        result = CURLE_URL_MALFORMAT;
18113498266Sopenharmony_ci        goto clean;
18213498266Sopenharmony_ci      }
18313498266Sopenharmony_ci
18413498266Sopenharmony_ci    }
18513498266Sopenharmony_ci    else {
18613498266Sopenharmony_ci      result = CURLE_BAD_FUNCTION_ARGUMENT;
18713498266Sopenharmony_ci      goto clean;
18813498266Sopenharmony_ci    }
18913498266Sopenharmony_ci  }
19013498266Sopenharmony_ci  else {
19113498266Sopenharmony_ci    /* this is ensured to end in a trailing / within ipfs_gateway() */
19213498266Sopenharmony_ci    gateway = ipfs_gateway();
19313498266Sopenharmony_ci    if(!gateway) {
19413498266Sopenharmony_ci      result = CURLE_FILE_COULDNT_READ_FILE;
19513498266Sopenharmony_ci      goto clean;
19613498266Sopenharmony_ci    }
19713498266Sopenharmony_ci
19813498266Sopenharmony_ci    if(curl_url_set(gatewayurl, CURLUPART_URL, gateway, 0)) {
19913498266Sopenharmony_ci      result = CURLE_URL_MALFORMAT;
20013498266Sopenharmony_ci      goto clean;
20113498266Sopenharmony_ci    }
20213498266Sopenharmony_ci  }
20313498266Sopenharmony_ci
20413498266Sopenharmony_ci  /* check for unsupported gateway parts */
20513498266Sopenharmony_ci  if(curl_url_get(gatewayurl, CURLUPART_QUERY, &gwquery, 0)
20613498266Sopenharmony_ci                  != CURLUE_NO_QUERY) {
20713498266Sopenharmony_ci    result = CURLE_URL_MALFORMAT;
20813498266Sopenharmony_ci    goto clean;
20913498266Sopenharmony_ci  }
21013498266Sopenharmony_ci
21113498266Sopenharmony_ci  /* get gateway parts */
21213498266Sopenharmony_ci  if(curl_url_get(gatewayurl, CURLUPART_HOST,
21313498266Sopenharmony_ci                  &gwhost, CURLU_URLDECODE)) {
21413498266Sopenharmony_ci    goto clean;
21513498266Sopenharmony_ci  }
21613498266Sopenharmony_ci
21713498266Sopenharmony_ci  if(curl_url_get(gatewayurl, CURLUPART_SCHEME,
21813498266Sopenharmony_ci                  &gwscheme, CURLU_URLDECODE)) {
21913498266Sopenharmony_ci    goto clean;
22013498266Sopenharmony_ci  }
22113498266Sopenharmony_ci
22213498266Sopenharmony_ci  curl_url_get(gatewayurl, CURLUPART_PORT, &gwport, CURLU_URLDECODE);
22313498266Sopenharmony_ci  curl_url_get(gatewayurl, CURLUPART_PATH, &gwpath, CURLU_URLDECODE);
22413498266Sopenharmony_ci
22513498266Sopenharmony_ci  /* get the path from user input */
22613498266Sopenharmony_ci  curl_url_get(uh, CURLUPART_PATH, &inputpath, CURLU_URLDECODE);
22713498266Sopenharmony_ci  /* inputpath might be NULL or a valid pointer now */
22813498266Sopenharmony_ci
22913498266Sopenharmony_ci  /* set gateway parts in input url */
23013498266Sopenharmony_ci  if(curl_url_set(uh, CURLUPART_SCHEME, gwscheme, CURLU_URLENCODE) ||
23113498266Sopenharmony_ci     curl_url_set(uh, CURLUPART_HOST, gwhost, CURLU_URLENCODE) ||
23213498266Sopenharmony_ci     curl_url_set(uh, CURLUPART_PORT, gwport, CURLU_URLENCODE))
23313498266Sopenharmony_ci    goto clean;
23413498266Sopenharmony_ci
23513498266Sopenharmony_ci  /* if the input path is just a slash, clear it */
23613498266Sopenharmony_ci  if(inputpath && (inputpath[0] == '/') && !inputpath[1])
23713498266Sopenharmony_ci    *inputpath = '\0';
23813498266Sopenharmony_ci
23913498266Sopenharmony_ci  /* ensure the gateway path ends with a trailing slash */
24013498266Sopenharmony_ci  ensure_trailing_slash(&gwpath);
24113498266Sopenharmony_ci
24213498266Sopenharmony_ci  pathbuffer = aprintf("%s%s/%s%s", gwpath, protocol, cid,
24313498266Sopenharmony_ci                       inputpath ? inputpath : "");
24413498266Sopenharmony_ci  if(!pathbuffer) {
24513498266Sopenharmony_ci    goto clean;
24613498266Sopenharmony_ci  }
24713498266Sopenharmony_ci
24813498266Sopenharmony_ci  if(curl_url_set(uh, CURLUPART_PATH, pathbuffer, CURLU_URLENCODE)) {
24913498266Sopenharmony_ci    goto clean;
25013498266Sopenharmony_ci  }
25113498266Sopenharmony_ci
25213498266Sopenharmony_ci  /* Free whatever it has now, rewriting is next */
25313498266Sopenharmony_ci  Curl_safefree(*url);
25413498266Sopenharmony_ci
25513498266Sopenharmony_ci  if(curl_url_get(uh, CURLUPART_URL, &cloneurl, CURLU_URLENCODE)) {
25613498266Sopenharmony_ci    goto clean;
25713498266Sopenharmony_ci  }
25813498266Sopenharmony_ci  /* we need to strdup the URL so that we can call free() on it later */
25913498266Sopenharmony_ci  *url = strdup(cloneurl);
26013498266Sopenharmony_ci  curl_free(cloneurl);
26113498266Sopenharmony_ci  if(!*url)
26213498266Sopenharmony_ci    goto clean;
26313498266Sopenharmony_ci
26413498266Sopenharmony_ci  result = CURLE_OK;
26513498266Sopenharmony_ci
26613498266Sopenharmony_ciclean:
26713498266Sopenharmony_ci  free(gateway);
26813498266Sopenharmony_ci  curl_free(gwhost);
26913498266Sopenharmony_ci  curl_free(gwpath);
27013498266Sopenharmony_ci  curl_free(gwquery);
27113498266Sopenharmony_ci  curl_free(inputpath);
27213498266Sopenharmony_ci  curl_free(gwscheme);
27313498266Sopenharmony_ci  curl_free(gwport);
27413498266Sopenharmony_ci  curl_free(cid);
27513498266Sopenharmony_ci  curl_free(pathbuffer);
27613498266Sopenharmony_ci  curl_url_cleanup(gatewayurl);
27713498266Sopenharmony_ci  {
27813498266Sopenharmony_ci    switch(result) {
27913498266Sopenharmony_ci    case CURLE_URL_MALFORMAT:
28013498266Sopenharmony_ci      helpf(tool_stderr, "malformed target URL");
28113498266Sopenharmony_ci      break;
28213498266Sopenharmony_ci    case CURLE_FILE_COULDNT_READ_FILE:
28313498266Sopenharmony_ci      helpf(tool_stderr, "IPFS automatic gateway detection failed");
28413498266Sopenharmony_ci      break;
28513498266Sopenharmony_ci    case CURLE_BAD_FUNCTION_ARGUMENT:
28613498266Sopenharmony_ci      helpf(tool_stderr, "--ipfs-gateway was given a malformed URL");
28713498266Sopenharmony_ci      break;
28813498266Sopenharmony_ci    default:
28913498266Sopenharmony_ci      break;
29013498266Sopenharmony_ci    }
29113498266Sopenharmony_ci  }
29213498266Sopenharmony_ci  return result;
29313498266Sopenharmony_ci}
294