altsvc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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.haxx.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(USE_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 *altsvc)
  276. {
  277. struct curl_llist_element *e;
  278. struct curl_llist_element *n;
  279. if(altsvc) {
  280. for(e = altsvc->list.head; e; e = n) {
  281. struct altsvc *as = e->ptr;
  282. n = e->next;
  283. altsvc_free(as);
  284. }
  285. free(altsvc->filename);
  286. free(altsvc);
  287. }
  288. }
  289. /*
  290. * Curl_altsvc_save() writes the altsvc cache to a file.
  291. */
  292. CURLcode Curl_altsvc_save(struct Curl_easy *data,
  293. struct altsvcinfo *altsvc, const char *file)
  294. {
  295. struct curl_llist_element *e;
  296. struct curl_llist_element *n;
  297. CURLcode result = CURLE_OK;
  298. FILE *out;
  299. char *tempstore;
  300. unsigned char randsuffix[9];
  301. if(!altsvc)
  302. /* no cache activated */
  303. return CURLE_OK;
  304. /* if not new name is given, use the one we stored from the load */
  305. if(!file && altsvc->filename)
  306. file = altsvc->filename;
  307. if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
  308. /* marked as read-only, no file or zero length file name */
  309. return CURLE_OK;
  310. if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
  311. return CURLE_FAILED_INIT;
  312. tempstore = aprintf("%s.%s.tmp", file, randsuffix);
  313. if(!tempstore)
  314. return CURLE_OUT_OF_MEMORY;
  315. out = fopen(tempstore, FOPEN_WRITETEXT);
  316. if(!out)
  317. result = CURLE_WRITE_ERROR;
  318. else {
  319. fputs("# Your alt-svc cache. https://curl.haxx.se/docs/alt-svc.html\n"
  320. "# This file was generated by libcurl! Edit at your own risk.\n",
  321. out);
  322. for(e = altsvc->list.head; e; e = n) {
  323. struct altsvc *as = e->ptr;
  324. n = e->next;
  325. result = altsvc_out(as, out);
  326. if(result)
  327. break;
  328. }
  329. fclose(out);
  330. if(!result && Curl_rename(tempstore, file))
  331. result = CURLE_WRITE_ERROR;
  332. if(result)
  333. unlink(tempstore);
  334. }
  335. free(tempstore);
  336. return result;
  337. }
  338. static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
  339. {
  340. size_t len;
  341. const char *protop;
  342. const char *p = *ptr;
  343. while(*p && ISBLANK(*p))
  344. p++;
  345. protop = p;
  346. while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '='))
  347. p++;
  348. len = p - protop;
  349. *ptr = p;
  350. if(!len || (len >= buflen))
  351. return CURLE_BAD_FUNCTION_ARGUMENT;
  352. memcpy(alpnbuf, protop, len);
  353. alpnbuf[len] = 0;
  354. return CURLE_OK;
  355. }
  356. /* altsvc_flush() removes all alternatives for this source origin from the
  357. list */
  358. static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
  359. const char *srchost, unsigned short srcport)
  360. {
  361. struct curl_llist_element *e;
  362. struct curl_llist_element *n;
  363. for(e = asi->list.head; e; e = n) {
  364. struct altsvc *as = e->ptr;
  365. n = e->next;
  366. if((srcalpnid == as->src.alpnid) &&
  367. (srcport == as->src.port) &&
  368. strcasecompare(srchost, as->src.host)) {
  369. Curl_llist_remove(&asi->list, e, NULL);
  370. altsvc_free(as);
  371. }
  372. }
  373. }
  374. #ifdef DEBUGBUILD
  375. /* to play well with debug builds, we can *set* a fixed time this will
  376. return */
  377. static time_t debugtime(void *unused)
  378. {
  379. char *timestr = getenv("CURL_TIME");
  380. (void)unused;
  381. if(timestr) {
  382. unsigned long val = strtol(timestr, NULL, 10);
  383. return (time_t)val;
  384. }
  385. return time(NULL);
  386. }
  387. #define time(x) debugtime(x)
  388. #endif
  389. #define ISNEWLINE(x) (((x) == '\n') || (x) == '\r')
  390. /*
  391. * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
  392. * the data correctly in the cache.
  393. *
  394. * 'value' points to the header *value*. That's contents to the right of the
  395. * header name.
  396. *
  397. * Currently this function rejects invalid data without returning an error.
  398. * Invalid host name, port number will result in the specific alternative
  399. * being rejected. Unknown protocols are skipped.
  400. */
  401. CURLcode Curl_altsvc_parse(struct Curl_easy *data,
  402. struct altsvcinfo *asi, const char *value,
  403. enum alpnid srcalpnid, const char *srchost,
  404. unsigned short srcport)
  405. {
  406. const char *p = value;
  407. size_t len;
  408. enum alpnid dstalpnid = srcalpnid; /* the same by default */
  409. char namebuf[MAX_ALTSVC_HOSTLEN] = "";
  410. char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
  411. struct altsvc *as;
  412. unsigned short dstport = srcport; /* the same by default */
  413. CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  414. if(result) {
  415. infof(data, "Excessive alt-svc header, ignoring...\n");
  416. return CURLE_OK;
  417. }
  418. DEBUGASSERT(asi);
  419. /* Flush all cached alternatives for this source origin, if any */
  420. altsvc_flush(asi, srcalpnid, srchost, srcport);
  421. /* "clear" is a magic keyword */
  422. if(strcasecompare(alpnbuf, "clear")) {
  423. return CURLE_OK;
  424. }
  425. do {
  426. if(*p == '=') {
  427. /* [protocol]="[host][:port]" */
  428. dstalpnid = alpn2alpnid(alpnbuf);
  429. p++;
  430. if(*p == '\"') {
  431. const char *dsthost = "";
  432. const char *value_ptr;
  433. char option[32];
  434. unsigned long num;
  435. char *end_ptr;
  436. bool quoted = FALSE;
  437. time_t maxage = 24 * 3600; /* default is 24 hours */
  438. bool persist = FALSE;
  439. p++;
  440. if(*p != ':') {
  441. /* host name starts here */
  442. const char *hostp = p;
  443. while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
  444. p++;
  445. len = p - hostp;
  446. if(!len || (len >= MAX_ALTSVC_HOSTLEN)) {
  447. infof(data, "Excessive alt-svc host name, ignoring...\n");
  448. dstalpnid = ALPN_none;
  449. }
  450. else {
  451. memcpy(namebuf, hostp, len);
  452. namebuf[len] = 0;
  453. dsthost = namebuf;
  454. }
  455. }
  456. else {
  457. /* no destination name, use source host */
  458. dsthost = srchost;
  459. }
  460. if(*p == ':') {
  461. /* a port number */
  462. unsigned long port = strtoul(++p, &end_ptr, 10);
  463. if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
  464. infof(data, "Unknown alt-svc port number, ignoring...\n");
  465. dstalpnid = ALPN_none;
  466. }
  467. p = end_ptr;
  468. dstport = curlx_ultous(port);
  469. }
  470. if(*p++ != '\"')
  471. break;
  472. /* Handle the optional 'ma' and 'persist' flags. Unknown flags
  473. are skipped. */
  474. for(;;) {
  475. while(ISBLANK(*p))
  476. p++;
  477. if(*p != ';')
  478. break;
  479. p++; /* pass the semicolon */
  480. if(!*p || ISNEWLINE(*p))
  481. break;
  482. result = getalnum(&p, option, sizeof(option));
  483. if(result) {
  484. /* skip option if name is too long */
  485. option[0] = '\0';
  486. }
  487. while(*p && ISBLANK(*p))
  488. p++;
  489. if(*p != '=')
  490. return CURLE_OK;
  491. p++;
  492. while(*p && ISBLANK(*p))
  493. p++;
  494. if(!*p)
  495. return CURLE_OK;
  496. if(*p == '\"') {
  497. /* quoted value */
  498. p++;
  499. quoted = TRUE;
  500. }
  501. value_ptr = p;
  502. if(quoted) {
  503. while(*p && *p != '\"')
  504. p++;
  505. if(!*p++)
  506. return CURLE_OK;
  507. }
  508. else {
  509. while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',')
  510. p++;
  511. }
  512. num = strtoul(value_ptr, &end_ptr, 10);
  513. if((end_ptr != value_ptr) && (num < ULONG_MAX)) {
  514. if(strcasecompare("ma", option))
  515. maxage = num;
  516. else if(strcasecompare("persist", option) && (num == 1))
  517. persist = TRUE;
  518. }
  519. }
  520. if(dstalpnid) {
  521. as = altsvc_createid(srchost, dsthost,
  522. srcalpnid, dstalpnid,
  523. srcport, dstport);
  524. if(as) {
  525. /* The expires time also needs to take the Age: value (if any) into
  526. account. [See RFC 7838 section 3.1] */
  527. as->expires = maxage + time(NULL);
  528. as->persist = persist;
  529. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  530. infof(data, "Added alt-svc: %s:%d over %s\n", dsthost, dstport,
  531. Curl_alpnid2str(dstalpnid));
  532. }
  533. }
  534. else {
  535. infof(data, "Unknown alt-svc protocol \"%s\", skipping...\n",
  536. alpnbuf);
  537. }
  538. }
  539. else
  540. break;
  541. /* after the double quote there can be a comma if there's another
  542. string or a semicolon if no more */
  543. if(*p == ',') {
  544. /* comma means another alternative is presented */
  545. p++;
  546. result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  547. if(result)
  548. break;
  549. }
  550. }
  551. else
  552. break;
  553. } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
  554. return CURLE_OK;
  555. }
  556. /*
  557. * Return TRUE on a match
  558. */
  559. bool Curl_altsvc_lookup(struct altsvcinfo *asi,
  560. enum alpnid srcalpnid, const char *srchost,
  561. int srcport,
  562. struct altsvc **dstentry,
  563. const int versions) /* one or more bits */
  564. {
  565. struct curl_llist_element *e;
  566. struct curl_llist_element *n;
  567. time_t now = time(NULL);
  568. DEBUGASSERT(asi);
  569. DEBUGASSERT(srchost);
  570. DEBUGASSERT(dstentry);
  571. for(e = asi->list.head; e; e = n) {
  572. struct altsvc *as = e->ptr;
  573. n = e->next;
  574. if(as->expires < now) {
  575. /* an expired entry, remove */
  576. Curl_llist_remove(&asi->list, e, NULL);
  577. altsvc_free(as);
  578. continue;
  579. }
  580. if((as->src.alpnid == srcalpnid) &&
  581. strcasecompare(as->src.host, srchost) &&
  582. (as->src.port == srcport) &&
  583. (versions & as->dst.alpnid)) {
  584. /* match */
  585. *dstentry = as;
  586. return TRUE;
  587. }
  588. }
  589. return FALSE;
  590. }
  591. #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */