altsvc.c 18 KB

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