altsvc.c 18 KB

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