1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2019 - 2022, Daniel Stenberg, <[email protected]>, 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/*
25 * The Alt-Svc: header is defined in RFC 7838:
26 * https://datatracker.ietf.org/doc/html/rfc7838
27 */
28#include "curl_setup.h"
29
30#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_ALTSVC)
31#include <curl/curl.h>
32#include "urldata.h"
33#include "altsvc.h"
34#include "curl_get_line.h"
35#include "strcase.h"
36#include "parsedate.h"
37#include "sendf.h"
38#include "warnless.h"
39#include "fopen.h"
40#include "rename.h"
41
42/* The last 3 #include files should be in this order */
43#include "curl_printf.h"
44#include "curl_memory.h"
45#include "memdebug.h"
46
47#define MAX_ALTSVC_LINE 4095
48#define MAX_ALTSVC_DATELENSTR "64"
49#define MAX_ALTSVC_DATELEN 64
50#define MAX_ALTSVC_HOSTLENSTR "512"
51#define MAX_ALTSVC_HOSTLEN 512
52#define MAX_ALTSVC_ALPNLENSTR "10"
53#define MAX_ALTSVC_ALPNLEN 10
54
55#if defined(USE_QUICHE) && !defined(UNITTESTS)
56#define H3VERSION "h3-29"
57#elif defined(USE_NGTCP2) && !defined(UNITTESTS)
58#define H3VERSION "h3-29"
59#elif defined(USE_MSH3) && !defined(UNITTESTS)
60#define H3VERSION "h3-29"
61#else
62#define H3VERSION "h3"
63#endif
64
65static enum alpnid alpn2alpnid(char *name)
66{
67 if(strcasecompare(name, "h1"))
68 return ALPN_h1;
69 if(strcasecompare(name, "h2"))
70 return ALPN_h2;
71 if(strcasecompare(name, H3VERSION))
72 return ALPN_h3;
73 return ALPN_none; /* unknown, probably rubbish input */
74}
75
76/* Given the ALPN ID, return the name */
77const char *Curl_alpnid2str(enum alpnid id)
78{
79 switch(id) {
80 case ALPN_h1:
81 return "h1";
82 case ALPN_h2:
83 return "h2";
84 case ALPN_h3:
85 return H3VERSION;
86 default:
87 return ""; /* bad */
88 }
89}
90
91
92static void altsvc_free(struct altsvc *as)
93{
94 free(as->src.host);
95 free(as->dst.host);
96 free(as);
97}
98
99static struct altsvc *altsvc_createid(const char *srchost,
100 const char *dsthost,
101 enum alpnid srcalpnid,
102 enum alpnid dstalpnid,
103 unsigned int srcport,
104 unsigned int dstport)
105{
106 struct altsvc *as = calloc(sizeof(struct altsvc), 1);
107 size_t hlen;
108 if(!as)
109 return NULL;
110 hlen = strlen(srchost);
111 DEBUGASSERT(hlen);
112 as->src.host = strdup(srchost);
113 if(!as->src.host)
114 goto error;
115 if(hlen && (srchost[hlen - 1] == '.'))
116 /* strip off trailing any dot */
117 as->src.host[--hlen] = 0;
118 as->dst.host = strdup(dsthost);
119 if(!as->dst.host)
120 goto error;
121
122 as->src.alpnid = srcalpnid;
123 as->dst.alpnid = dstalpnid;
124 as->src.port = curlx_ultous(srcport);
125 as->dst.port = curlx_ultous(dstport);
126
127 return as;
128 error:
129 altsvc_free(as);
130 return NULL;
131}
132
133static struct altsvc *altsvc_create(char *srchost,
134 char *dsthost,
135 char *srcalpn,
136 char *dstalpn,
137 unsigned int srcport,
138 unsigned int dstport)
139{
140 enum alpnid dstalpnid = alpn2alpnid(dstalpn);
141 enum alpnid srcalpnid = alpn2alpnid(srcalpn);
142 if(!srcalpnid || !dstalpnid)
143 return NULL;
144 return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid,
145 srcport, dstport);
146}
147
148/* only returns SERIOUS errors */
149static CURLcode altsvc_add(struct altsvcinfo *asi, char *line)
150{
151 /* Example line:
152 h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
153 */
154 char srchost[MAX_ALTSVC_HOSTLEN + 1];
155 char dsthost[MAX_ALTSVC_HOSTLEN + 1];
156 char srcalpn[MAX_ALTSVC_ALPNLEN + 1];
157 char dstalpn[MAX_ALTSVC_ALPNLEN + 1];
158 char date[MAX_ALTSVC_DATELEN + 1];
159 unsigned int srcport;
160 unsigned int dstport;
161 unsigned int prio;
162 unsigned int persist;
163 int rc;
164
165 rc = sscanf(line,
166 "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
167 "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
168 "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u",
169 srcalpn, srchost, &srcport,
170 dstalpn, dsthost, &dstport,
171 date, &persist, &prio);
172 if(9 == rc) {
173 struct altsvc *as;
174 time_t expires = Curl_getdate_capped(date);
175 as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport);
176 if(as) {
177 as->expires = expires;
178 as->prio = prio;
179 as->persist = persist ? 1 : 0;
180 Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
181 }
182 }
183
184 return CURLE_OK;
185}
186
187/*
188 * Load alt-svc entries from the given file. The text based line-oriented file
189 * format is documented here: https://curl.se/docs/alt-svc.html
190 *
191 * This function only returns error on major problems that prevent alt-svc
192 * handling to work completely. It will ignore individual syntactical errors
193 * etc.
194 */
195static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
196{
197 CURLcode result = CURLE_OK;
198 char *line = NULL;
199 FILE *fp;
200
201 /* we need a private copy of the file name so that the altsvc cache file
202 name survives an easy handle reset */
203 free(asi->filename);
204 asi->filename = strdup(file);
205 if(!asi->filename)
206 return CURLE_OUT_OF_MEMORY;
207
208 fp = fopen(file, FOPEN_READTEXT);
209 if(fp) {
210 line = malloc(MAX_ALTSVC_LINE);
211 if(!line)
212 goto fail;
213 while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) {
214 char *lineptr = line;
215 while(*lineptr && ISBLANK(*lineptr))
216 lineptr++;
217 if(*lineptr == '#')
218 /* skip commented lines */
219 continue;
220
221 altsvc_add(asi, lineptr);
222 }
223 free(line); /* free the line buffer */
224 fclose(fp);
225 }
226 return result;
227
228 fail:
229 Curl_safefree(asi->filename);
230 free(line);
231 fclose(fp);
232 return CURLE_OUT_OF_MEMORY;
233}
234
235/*
236 * Write this single altsvc entry to a single output line
237 */
238
239static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
240{
241 struct tm stamp;
242 CURLcode result = Curl_gmtime(as->expires, &stamp);
243 if(result)
244 return result;
245
246 fprintf(fp,
247 "%s %s %u "
248 "%s %s %u "
249 "\"%d%02d%02d "
250 "%02d:%02d:%02d\" "
251 "%u %d\n",
252 Curl_alpnid2str(as->src.alpnid), as->src.host, as->src.port,
253 Curl_alpnid2str(as->dst.alpnid), as->dst.host, as->dst.port,
254 stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
255 stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
256 as->persist, as->prio);
257 return CURLE_OK;
258}
259
260/* ---- library-wide functions below ---- */
261
262/*
263 * Curl_altsvc_init() creates a new altsvc cache.
264 * It returns the new instance or NULL if something goes wrong.
265 */
266struct altsvcinfo *Curl_altsvc_init(void)
267{
268 struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1);
269 if(!asi)
270 return NULL;
271 Curl_llist_init(&asi->list, NULL);
272
273 /* set default behavior */
274 asi->flags = CURLALTSVC_H1
275#ifdef USE_HTTP2
276 | CURLALTSVC_H2
277#endif
278#ifdef ENABLE_QUIC
279 | CURLALTSVC_H3
280#endif
281 ;
282 return asi;
283}
284
285/*
286 * Curl_altsvc_load() loads alt-svc from file.
287 */
288CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
289{
290 CURLcode result;
291 DEBUGASSERT(asi);
292 result = altsvc_load(asi, file);
293 return result;
294}
295
296/*
297 * Curl_altsvc_ctrl() passes on the external bitmask.
298 */
299CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
300{
301 DEBUGASSERT(asi);
302 if(!ctrl)
303 /* unexpected */
304 return CURLE_BAD_FUNCTION_ARGUMENT;
305 asi->flags = ctrl;
306 return CURLE_OK;
307}
308
309/*
310 * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
311 * resources.
312 */
313void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp)
314{
315 struct Curl_llist_element *e;
316 struct Curl_llist_element *n;
317 if(*altsvcp) {
318 struct altsvcinfo *altsvc = *altsvcp;
319 for(e = altsvc->list.head; e; e = n) {
320 struct altsvc *as = e->ptr;
321 n = e->next;
322 altsvc_free(as);
323 }
324 free(altsvc->filename);
325 free(altsvc);
326 *altsvcp = NULL; /* clear the pointer */
327 }
328}
329
330/*
331 * Curl_altsvc_save() writes the altsvc cache to a file.
332 */
333CURLcode Curl_altsvc_save(struct Curl_easy *data,
334 struct altsvcinfo *altsvc, const char *file)
335{
336 struct Curl_llist_element *e;
337 struct Curl_llist_element *n;
338 CURLcode result = CURLE_OK;
339 FILE *out;
340 char *tempstore = NULL;
341
342 if(!altsvc)
343 /* no cache activated */
344 return CURLE_OK;
345
346 /* if not new name is given, use the one we stored from the load */
347 if(!file && altsvc->filename)
348 file = altsvc->filename;
349
350 if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
351 /* marked as read-only, no file or zero length file name */
352 return CURLE_OK;
353
354 result = Curl_fopen(data, file, &out, &tempstore);
355 if(!result) {
356 fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
357 "# This file was generated by libcurl! Edit at your own risk.\n",
358 out);
359 for(e = altsvc->list.head; e; e = n) {
360 struct altsvc *as = e->ptr;
361 n = e->next;
362 result = altsvc_out(as, out);
363 if(result)
364 break;
365 }
366 fclose(out);
367 if(!result && tempstore && Curl_rename(tempstore, file))
368 result = CURLE_WRITE_ERROR;
369
370 if(result && tempstore)
371 unlink(tempstore);
372 }
373 free(tempstore);
374 return result;
375}
376
377static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
378{
379 size_t len;
380 const char *protop;
381 const char *p = *ptr;
382 while(*p && ISBLANK(*p))
383 p++;
384 protop = p;
385 while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '='))
386 p++;
387 len = p - protop;
388 *ptr = p;
389
390 if(!len || (len >= buflen))
391 return CURLE_BAD_FUNCTION_ARGUMENT;
392 memcpy(alpnbuf, protop, len);
393 alpnbuf[len] = 0;
394 return CURLE_OK;
395}
396
397/* hostcompare() returns true if 'host' matches 'check'. The first host
398 * argument may have a trailing dot present that will be ignored.
399 */
400static bool hostcompare(const char *host, const char *check)
401{
402 size_t hlen = strlen(host);
403 size_t clen = strlen(check);
404
405 if(hlen && (host[hlen - 1] == '.'))
406 hlen--;
407 if(hlen != clen)
408 /* they can't match if they have different lengths */
409 return FALSE;
410 return strncasecompare(host, check, hlen);
411}
412
413/* altsvc_flush() removes all alternatives for this source origin from the
414 list */
415static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
416 const char *srchost, unsigned short srcport)
417{
418 struct Curl_llist_element *e;
419 struct Curl_llist_element *n;
420 for(e = asi->list.head; e; e = n) {
421 struct altsvc *as = e->ptr;
422 n = e->next;
423 if((srcalpnid == as->src.alpnid) &&
424 (srcport == as->src.port) &&
425 hostcompare(srchost, as->src.host)) {
426 Curl_llist_remove(&asi->list, e, NULL);
427 altsvc_free(as);
428 }
429 }
430}
431
432#ifdef DEBUGBUILD
433/* to play well with debug builds, we can *set* a fixed time this will
434 return */
435static time_t debugtime(void *unused)
436{
437 char *timestr = getenv("CURL_TIME");
438 (void)unused;
439 if(timestr) {
440 unsigned long val = strtol(timestr, NULL, 10);
441 return (time_t)val;
442 }
443 return time(NULL);
444}
445#define time(x) debugtime(x)
446#endif
447
448#define ISNEWLINE(x) (((x) == '\n') || (x) == '\r')
449
450/*
451 * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
452 * the data correctly in the cache.
453 *
454 * 'value' points to the header *value*. That's contents to the right of the
455 * header name.
456 *
457 * Currently this function rejects invalid data without returning an error.
458 * Invalid host name, port number will result in the specific alternative
459 * being rejected. Unknown protocols are skipped.
460 */
461CURLcode Curl_altsvc_parse(struct Curl_easy *data,
462 struct altsvcinfo *asi, const char *value,
463 enum alpnid srcalpnid, const char *srchost,
464 unsigned short srcport)
465{
466 const char *p = value;
467 size_t len;
468 char namebuf[MAX_ALTSVC_HOSTLEN] = "";
469 char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
470 struct altsvc *as;
471 unsigned short dstport = srcport; /* the same by default */
472 CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
473#ifdef CURL_DISABLE_VERBOSE_STRINGS
474 (void)data;
475#endif
476 if(result) {
477 infof(data, "Excessive alt-svc header, ignoring.");
478 return CURLE_OK;
479 }
480
481 DEBUGASSERT(asi);
482
483 /* Flush all cached alternatives for this source origin, if any */
484 altsvc_flush(asi, srcalpnid, srchost, srcport);
485
486 /* "clear" is a magic keyword */
487 if(strcasecompare(alpnbuf, "clear")) {
488 return CURLE_OK;
489 }
490
491 do {
492 if(*p == '=') {
493 /* [protocol]="[host][:port]" */
494 enum alpnid dstalpnid = alpn2alpnid(alpnbuf); /* the same by default */
495 p++;
496 if(*p == '\"') {
497 const char *dsthost = "";
498 const char *value_ptr;
499 char option[32];
500 unsigned long num;
501 char *end_ptr;
502 bool quoted = FALSE;
503 time_t maxage = 24 * 3600; /* default is 24 hours */
504 bool persist = FALSE;
505 p++;
506 if(*p != ':') {
507 /* host name starts here */
508 const char *hostp = p;
509 while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
510 p++;
511 len = p - hostp;
512 if(!len || (len >= MAX_ALTSVC_HOSTLEN)) {
513 infof(data, "Excessive alt-svc host name, ignoring.");
514 dstalpnid = ALPN_none;
515 }
516 else {
517 memcpy(namebuf, hostp, len);
518 namebuf[len] = 0;
519 dsthost = namebuf;
520 }
521 }
522 else {
523 /* no destination name, use source host */
524 dsthost = srchost;
525 }
526 if(*p == ':') {
527 /* a port number */
528 unsigned long port = strtoul(++p, &end_ptr, 10);
529 if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
530 infof(data, "Unknown alt-svc port number, ignoring.");
531 dstalpnid = ALPN_none;
532 }
533 p = end_ptr;
534 dstport = curlx_ultous(port);
535 }
536 if(*p++ != '\"')
537 break;
538 /* Handle the optional 'ma' and 'persist' flags. Unknown flags
539 are skipped. */
540 for(;;) {
541 while(ISBLANK(*p))
542 p++;
543 if(*p != ';')
544 break;
545 p++; /* pass the semicolon */
546 if(!*p || ISNEWLINE(*p))
547 break;
548 result = getalnum(&p, option, sizeof(option));
549 if(result) {
550 /* skip option if name is too long */
551 option[0] = '\0';
552 }
553 while(*p && ISBLANK(*p))
554 p++;
555 if(*p != '=')
556 return CURLE_OK;
557 p++;
558 while(*p && ISBLANK(*p))
559 p++;
560 if(!*p)
561 return CURLE_OK;
562 if(*p == '\"') {
563 /* quoted value */
564 p++;
565 quoted = TRUE;
566 }
567 value_ptr = p;
568 if(quoted) {
569 while(*p && *p != '\"')
570 p++;
571 if(!*p++)
572 return CURLE_OK;
573 }
574 else {
575 while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',')
576 p++;
577 }
578 num = strtoul(value_ptr, &end_ptr, 10);
579 if((end_ptr != value_ptr) && (num < ULONG_MAX)) {
580 if(strcasecompare("ma", option))
581 maxage = num;
582 else if(strcasecompare("persist", option) && (num == 1))
583 persist = TRUE;
584 }
585 }
586 if(dstalpnid) {
587 as = altsvc_createid(srchost, dsthost,
588 srcalpnid, dstalpnid,
589 srcport, dstport);
590 if(as) {
591 /* The expires time also needs to take the Age: value (if any) into
592 account. [See RFC 7838 section 3.1] */
593 as->expires = maxage + time(NULL);
594 as->persist = persist;
595 Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
596 infof(data, "Added alt-svc: %s:%d over %s", dsthost, dstport,
597 Curl_alpnid2str(dstalpnid));
598 }
599 }
600 else {
601 infof(data, "Unknown alt-svc protocol \"%s\", skipping.",
602 alpnbuf);
603 }
604 }
605 else
606 break;
607 /* after the double quote there can be a comma if there's another
608 string or a semicolon if no more */
609 if(*p == ',') {
610 /* comma means another alternative is presented */
611 p++;
612 result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
613 if(result)
614 break;
615 }
616 }
617 else
618 break;
619 } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
620
621 return CURLE_OK;
622}
623
624/*
625 * Return TRUE on a match
626 */
627bool Curl_altsvc_lookup(struct altsvcinfo *asi,
628 enum alpnid srcalpnid, const char *srchost,
629 int srcport,
630 struct altsvc **dstentry,
631 const int versions) /* one or more bits */
632{
633 struct Curl_llist_element *e;
634 struct Curl_llist_element *n;
635 time_t now = time(NULL);
636 DEBUGASSERT(asi);
637 DEBUGASSERT(srchost);
638 DEBUGASSERT(dstentry);
639
640 for(e = asi->list.head; e; e = n) {
641 struct altsvc *as = e->ptr;
642 n = e->next;
643 if(as->expires < now) {
644 /* an expired entry, remove */
645 Curl_llist_remove(&asi->list, e, NULL);
646 altsvc_free(as);
647 continue;
648 }
649 if((as->src.alpnid == srcalpnid) &&
650 hostcompare(srchost, as->src.host) &&
651 (as->src.port == srcport) &&
652 (versions & as->dst.alpnid)) {
653 /* match */
654 *dstentry = as;
655 return TRUE;
656 }
657 }
658 return FALSE;
659}
660
661#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */
662