xref: /curl/src/tool_ipfs.c (revision ce7d0d41)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #ifndef CURL_DISABLE_IPFS
27 #include "curlx.h"
28 #include "dynbuf.h"
29 
30 #include "tool_cfgable.h"
31 #include "tool_msgs.h"
32 #include "tool_ipfs.h"
33 
34 #include "memdebug.h" /* keep this as LAST include */
35 
36 /* ensure input ends in slash */
ensure_trailing_slash(char ** input)37 static CURLcode ensure_trailing_slash(char **input)
38 {
39   if(*input && **input) {
40     size_t len = strlen(*input);
41     if(((*input)[len - 1] != '/')) {
42       struct curlx_dynbuf dyn;
43       curlx_dyn_init(&dyn, len + 2);
44 
45       if(curlx_dyn_addn(&dyn, *input, len)) {
46         Curl_safefree(*input);
47         return CURLE_OUT_OF_MEMORY;
48       }
49 
50       Curl_safefree(*input);
51 
52       if(curlx_dyn_addn(&dyn, "/", 1))
53         return CURLE_OUT_OF_MEMORY;
54 
55       *input = curlx_dyn_ptr(&dyn);
56     }
57   }
58 
59   return CURLE_OK;
60 }
61 
ipfs_gateway(void)62 static char *ipfs_gateway(void)
63 {
64   char *ipfs_path = NULL;
65   char *gateway_composed_file_path = NULL;
66   FILE *gateway_file = NULL;
67   char *gateway = curl_getenv("IPFS_GATEWAY");
68 
69   /* Gateway is found from environment variable. */
70   if(gateway) {
71     if(ensure_trailing_slash(&gateway))
72       goto fail;
73     return gateway;
74   }
75 
76   /* Try to find the gateway in the IPFS data folder. */
77   ipfs_path = curl_getenv("IPFS_PATH");
78 
79   if(!ipfs_path) {
80     char *home = getenv("HOME");
81     if(home && *home)
82       ipfs_path = aprintf("%s/.ipfs/", home);
83     /* fallback to "~/.ipfs", as that is the default location. */
84   }
85 
86   if(!ipfs_path || ensure_trailing_slash(&ipfs_path))
87     goto fail;
88 
89   gateway_composed_file_path = aprintf("%sgateway", ipfs_path);
90 
91   if(!gateway_composed_file_path)
92     goto fail;
93 
94   gateway_file = fopen(gateway_composed_file_path, FOPEN_READTEXT);
95   Curl_safefree(gateway_composed_file_path);
96 
97   if(gateway_file) {
98     int c;
99     struct curlx_dynbuf dyn;
100     curlx_dyn_init(&dyn, MAX_GATEWAY_URL_LEN);
101 
102     /* get the first line of the gateway file, ignore the rest */
103     while((c = getc(gateway_file)) != EOF && c != '\n' && c != '\r') {
104       char c_char = (char)c;
105       if(curlx_dyn_addn(&dyn, &c_char, 1))
106         goto fail;
107     }
108 
109     fclose(gateway_file);
110     gateway_file = NULL;
111 
112     if(curlx_dyn_len(&dyn))
113       gateway = curlx_dyn_ptr(&dyn);
114 
115     if(gateway)
116       ensure_trailing_slash(&gateway);
117 
118     if(!gateway)
119       goto fail;
120 
121     Curl_safefree(ipfs_path);
122 
123     return gateway;
124   }
125 fail:
126   if(gateway_file)
127     fclose(gateway_file);
128   Curl_safefree(gateway);
129   Curl_safefree(ipfs_path);
130   return NULL;
131 }
132 
133 /*
134  * Rewrite ipfs://<cid> and ipns://<cid> to an HTTP(S)
135  * URL that can be handled by an IPFS gateway.
136  */
ipfs_url_rewrite(CURLU * uh,const char * protocol,char ** url,struct OperationConfig * config)137 CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url,
138                           struct OperationConfig *config)
139 {
140   CURLcode result = CURLE_URL_MALFORMAT;
141   CURLUcode getResult;
142   char *gateway = NULL;
143   char *gwhost = NULL;
144   char *gwpath = NULL;
145   char *gwquery = NULL;
146   char *gwscheme = NULL;
147   char *gwport = NULL;
148   char *inputpath = NULL;
149   char *cid = NULL;
150   char *pathbuffer = NULL;
151   char *cloneurl;
152   CURLU *gatewayurl = curl_url();
153 
154   if(!gatewayurl) {
155     result = CURLE_FAILED_INIT;
156     goto clean;
157   }
158 
159   getResult = curl_url_get(uh, CURLUPART_HOST, &cid, CURLU_URLDECODE);
160   if(getResult || !cid)
161     goto clean;
162 
163   /* We might have a --ipfs-gateway argument. Check it first and use it. Error
164    * if we do have something but if it is an invalid url.
165    */
166   if(config->ipfs_gateway) {
167     /* ensure the gateway ends in a trailing / */
168     if(ensure_trailing_slash(&config->ipfs_gateway) != CURLE_OK) {
169       result = CURLE_OUT_OF_MEMORY;
170       goto clean;
171     }
172 
173     if(!curl_url_set(gatewayurl, CURLUPART_URL, config->ipfs_gateway,
174                     CURLU_GUESS_SCHEME)) {
175       gateway = strdup(config->ipfs_gateway);
176       if(!gateway) {
177         result = CURLE_URL_MALFORMAT;
178         goto clean;
179       }
180 
181     }
182     else {
183       result = CURLE_BAD_FUNCTION_ARGUMENT;
184       goto clean;
185     }
186   }
187   else {
188     /* this is ensured to end in a trailing / within ipfs_gateway() */
189     gateway = ipfs_gateway();
190     if(!gateway) {
191       result = CURLE_FILE_COULDNT_READ_FILE;
192       goto clean;
193     }
194 
195     if(curl_url_set(gatewayurl, CURLUPART_URL, gateway, 0)) {
196       result = CURLE_URL_MALFORMAT;
197       goto clean;
198     }
199   }
200 
201   /* check for unsupported gateway parts */
202   if(curl_url_get(gatewayurl, CURLUPART_QUERY, &gwquery, 0)
203                   != CURLUE_NO_QUERY) {
204     result = CURLE_URL_MALFORMAT;
205     goto clean;
206   }
207 
208   /* get gateway parts */
209   if(curl_url_get(gatewayurl, CURLUPART_HOST,
210                   &gwhost, CURLU_URLDECODE)) {
211     goto clean;
212   }
213 
214   if(curl_url_get(gatewayurl, CURLUPART_SCHEME,
215                   &gwscheme, CURLU_URLDECODE)) {
216     goto clean;
217   }
218 
219   curl_url_get(gatewayurl, CURLUPART_PORT, &gwport, CURLU_URLDECODE);
220   curl_url_get(gatewayurl, CURLUPART_PATH, &gwpath, CURLU_URLDECODE);
221 
222   /* get the path from user input */
223   curl_url_get(uh, CURLUPART_PATH, &inputpath, CURLU_URLDECODE);
224   /* inputpath might be NULL or a valid pointer now */
225 
226   /* set gateway parts in input url */
227   if(curl_url_set(uh, CURLUPART_SCHEME, gwscheme, CURLU_URLENCODE) ||
228      curl_url_set(uh, CURLUPART_HOST, gwhost, CURLU_URLENCODE) ||
229      curl_url_set(uh, CURLUPART_PORT, gwport, CURLU_URLENCODE))
230     goto clean;
231 
232   /* if the input path is just a slash, clear it */
233   if(inputpath && (inputpath[0] == '/') && !inputpath[1])
234     *inputpath = '\0';
235 
236   /* ensure the gateway path ends with a trailing slash */
237   ensure_trailing_slash(&gwpath);
238 
239   pathbuffer = aprintf("%s%s/%s%s", gwpath, protocol, cid,
240                        inputpath ? inputpath : "");
241   if(!pathbuffer) {
242     goto clean;
243   }
244 
245   if(curl_url_set(uh, CURLUPART_PATH, pathbuffer, CURLU_URLENCODE)) {
246     goto clean;
247   }
248 
249   /* Free whatever it has now, rewriting is next */
250   Curl_safefree(*url);
251 
252   if(curl_url_get(uh, CURLUPART_URL, &cloneurl, CURLU_URLENCODE)) {
253     goto clean;
254   }
255   /* we need to strdup the URL so that we can call free() on it later */
256   *url = strdup(cloneurl);
257   curl_free(cloneurl);
258   if(!*url)
259     goto clean;
260 
261   result = CURLE_OK;
262 
263 clean:
264   free(gateway);
265   curl_free(gwhost);
266   curl_free(gwpath);
267   curl_free(gwquery);
268   curl_free(inputpath);
269   curl_free(gwscheme);
270   curl_free(gwport);
271   curl_free(cid);
272   curl_free(pathbuffer);
273   curl_url_cleanup(gatewayurl);
274   {
275     switch(result) {
276     case CURLE_URL_MALFORMAT:
277       helpf(tool_stderr, "malformed target URL");
278       break;
279     case CURLE_FILE_COULDNT_READ_FILE:
280       helpf(tool_stderr, "IPFS automatic gateway detection failed");
281       break;
282     case CURLE_BAD_FUNCTION_ARGUMENT:
283       helpf(tool_stderr, "--ipfs-gateway was given a malformed URL");
284       break;
285     default:
286       break;
287     }
288   }
289   return result;
290 }
291 #endif /* !CURL_DISABLE_IPFS */
292