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