chat_login.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* chat_login: see chat_login.h. */
  2. #include "chat_login.h"
  3. /* ---- small string helpers (no libc: also built on the host) ---- */
  4. static unsigned int c_len(const char *s)
  5. {
  6. unsigned int n = 0;
  7. while (s && s[n])
  8. n++;
  9. return n;
  10. }
  11. static void c_copy(char *d, const char *s, unsigned int cap)
  12. {
  13. unsigned int i = 0;
  14. if (!cap)
  15. return;
  16. while (s && s[i] && i + 1 < cap) {
  17. d[i] = s[i];
  18. i++;
  19. }
  20. d[i] = '\0';
  21. }
  22. static int c_prefix(const char *s, const char *p)
  23. {
  24. while (*p) {
  25. if (*s != *p)
  26. return 0;
  27. s++;
  28. p++;
  29. }
  30. return 1;
  31. }
  32. static int __attribute__((unused)) c_suffix(const char *s, const char *suf)
  33. {
  34. unsigned int a = c_len(s), b = c_len(suf), i;
  35. if (b > a)
  36. return 0;
  37. for (i = 0; i < b; i++)
  38. if (s[a - b + i] != suf[i])
  39. return 0;
  40. return 1;
  41. }
  42. /* Find needle in hay[0..len); index or -1. */
  43. static int c_find(const char *hay, unsigned int len, const char *needle,
  44. unsigned int from)
  45. {
  46. unsigned int n = c_len(needle), i, j;
  47. if (!n || n > len)
  48. return -1;
  49. for (i = from; i + n <= len; i++) {
  50. for (j = 0; j < n; j++)
  51. if (hay[i + j] != needle[j])
  52. break;
  53. if (j == n)
  54. return (int)i;
  55. }
  56. return -1;
  57. }
  58. /* Cloudron's consent interaction page is a bare auto-submit form, not the
  59. * login SPA: <form id="submitForm" method="post"
  60. * action="/openid/interaction/<uid>/confirm">
  61. * Return the action of such an interaction form (relative to the page). */
  62. static int c_form_action(const char *html, unsigned int len, char *out,
  63. unsigned int cap)
  64. {
  65. unsigned int at = 0;
  66. while (at < len) {
  67. int f = c_find(html, len, "<form", at);
  68. int end, a;
  69. if (f < 0)
  70. return 0;
  71. end = c_find(html, len, ">", (unsigned int)f);
  72. if (end < 0)
  73. return 0;
  74. a = c_find(html, (unsigned int)end, "action", (unsigned int)f);
  75. if (a >= 0) {
  76. unsigned int i = (unsigned int)a + 6, k = 0;
  77. char q;
  78. while (i < (unsigned int)end &&
  79. (html[i] == ' ' || html[i] == '\t' ||
  80. html[i] == '\r' || html[i] == '\n'))
  81. i++;
  82. if (i < (unsigned int)end && html[i] == '=') {
  83. i++;
  84. while (i < (unsigned int)end &&
  85. (html[i] == ' ' || html[i] == '\t' ||
  86. html[i] == '\r' || html[i] == '\n'))
  87. i++;
  88. if (i < (unsigned int)end &&
  89. (html[i] == '"' || html[i] == '\'')) {
  90. q = html[i++];
  91. while (i < (unsigned int)end &&
  92. html[i] != q) {
  93. if (k + 1 >= cap)
  94. return 0;
  95. out[k++] = html[i++];
  96. }
  97. out[k] = '\0';
  98. if (i < (unsigned int)end &&
  99. html[i] == q &&
  100. c_find(out, k,
  101. "/openid/interaction/", 0) >= 0 &&
  102. (c_suffix(out, "/confirm") ||
  103. c_suffix(out, "/abort")))
  104. return 1;
  105. }
  106. }
  107. }
  108. at = (unsigned int)end + 1;
  109. }
  110. return 0;
  111. }
  112. /* ---- server URL / config file ---- */
  113. int chat_server_normalize(const char *in, char *out, unsigned int cap)
  114. {
  115. unsigned int n = 0, i = 0, len;
  116. while (in[i] == ' ' || in[i] == '\t')
  117. i++;
  118. if (!c_prefix(in + i, "https://") && !c_prefix(in + i, "http://")) {
  119. const char *pre = "https://";
  120. while (*pre && n + 1 < cap)
  121. out[n++] = *pre++;
  122. }
  123. while (in[i] && in[i] != ' ' && in[i] != '\t' && n + 1 < cap)
  124. out[n++] = in[i++];
  125. out[n] = '\0';
  126. if (in[i] && in[i] != ' ' && in[i] != '\t')
  127. return -1; /* did not fit */
  128. while (in[i] == ' ' || in[i] == '\t')
  129. i++;
  130. if (in[i])
  131. return -1; /* embedded space */
  132. while (n > 0 && out[n - 1] == '/')
  133. out[--n] = '\0';
  134. len = n;
  135. /* need a host after the scheme */
  136. if (c_prefix(out, "https://"))
  137. return len > 8 ? 0 : -1;
  138. return len > 7 ? 0 : -1;
  139. }
  140. int chat_config_parse(const char *text, unsigned int len, char *server,
  141. unsigned int scap, char *cookie, unsigned int ccap)
  142. {
  143. unsigned int i = 0;
  144. int found = 0;
  145. if (scap)
  146. server[0] = '\0';
  147. if (ccap)
  148. cookie[0] = '\0';
  149. while (i < len) {
  150. unsigned int s = i, e, k;
  151. char *dst = 0;
  152. unsigned int cap = 0;
  153. while (i < len && text[i] != '\n')
  154. i++;
  155. e = i;
  156. if (i < len)
  157. i++;
  158. while (e > s && (text[e - 1] == '\r' || text[e - 1] == ' '))
  159. e--;
  160. while (s < e && (text[s] == ' ' || text[s] == '\t'))
  161. s++;
  162. if (s == e || text[s] == '#')
  163. continue;
  164. if (e - s > 7 && c_prefix(text + s, "server=")) {
  165. dst = server;
  166. cap = scap;
  167. s += 7;
  168. } else if (e - s > 7 && c_prefix(text + s, "cookie=")) {
  169. dst = cookie;
  170. cap = ccap;
  171. s += 7;
  172. }
  173. if (!dst || !cap)
  174. continue;
  175. for (k = 0; s + k < e && k + 1 < cap; k++)
  176. dst[k] = text[s + k];
  177. dst[k] = '\0';
  178. found = 1;
  179. }
  180. return found ? 0 : -1;
  181. }
  182. int chat_config_format(char *out, unsigned int cap, const char *server,
  183. const char *cookie)
  184. {
  185. unsigned int n = 0;
  186. #define APP(s) do { const char *ap = (s); \
  187. while (*ap) { if (n + 1 >= cap) return -1; out[n++] = *ap++; } } while (0)
  188. APP("# NebOS chat session (the cookie is a bearer credential)\n");
  189. APP("server=");
  190. APP(server);
  191. APP("\n");
  192. if (cookie && cookie[0]) {
  193. APP("cookie=");
  194. APP(cookie);
  195. APP("\n");
  196. }
  197. #undef APP
  198. out[n] = '\0';
  199. return (int)n;
  200. }
  201. int chat_login_body(char *out, unsigned int cap, const char *user,
  202. const char *pass, const char *totp)
  203. {
  204. unsigned int n = 0;
  205. int r;
  206. #define LIT(s) do { const char *ap = (s); \
  207. while (*ap) { if (n + 1 >= cap) return -1; out[n++] = *ap++; } } while (0)
  208. #define STR(s) do { \
  209. r = ujson_escape(out + n, cap - n, (s)); \
  210. if (r < 0) \
  211. return -1; \
  212. n += (unsigned int)r; \
  213. } while (0)
  214. LIT("{\"username\":");
  215. STR(user);
  216. LIT(",\"password\":");
  217. STR(pass);
  218. LIT(",\"totpToken\":");
  219. STR(totp ? totp : "");
  220. LIT("}");
  221. #undef LIT
  222. #undef STR
  223. out[n] = '\0';
  224. return (int)n;
  225. }
  226. /* ---- page / reply parsing ---- */
  227. static struct ujson_node pnodes[512];
  228. static char psbuf[8192];
  229. /* Resolve href against an absolute base URL. Handles absolute URLs,
  230. * scheme-relative, absolute-path and directory-relative references. */
  231. static int resolve(const char *base, const char *href, char *out,
  232. unsigned int cap)
  233. {
  234. unsigned int n = 0, i, origin_end, dir_end;
  235. if (c_prefix(href, "http://") || c_prefix(href, "https://")) {
  236. if (c_len(href) + 1 > cap)
  237. return -1;
  238. c_copy(out, href, cap);
  239. return 0;
  240. }
  241. /* origin = scheme://host[:port] */
  242. i = 0;
  243. while (base[i] && base[i] != ':')
  244. i++;
  245. if (base[i] != ':' || base[i + 1] != '/' || base[i + 2] != '/')
  246. return -1;
  247. if (href[0] == '/' && href[1] == '/') {
  248. if (i + 1 + c_len(href) + 1 > cap)
  249. return -1;
  250. for (n = 0; n <= i; n++)
  251. out[n] = base[n];
  252. c_copy(out + n, href, cap - n);
  253. return 0;
  254. }
  255. i += 3;
  256. while (base[i] && base[i] != '/' && base[i] != '?' && base[i] != '#')
  257. i++;
  258. origin_end = i;
  259. dir_end = origin_end;
  260. while (base[i] && base[i] != '?' && base[i] != '#') {
  261. if (base[i] == '/')
  262. dir_end = i;
  263. i++;
  264. }
  265. if (href[0] == '/') {
  266. if (origin_end + c_len(href) + 1 > cap)
  267. return -1;
  268. for (n = 0; n < origin_end; n++)
  269. out[n] = base[n];
  270. c_copy(out + n, href, cap - n);
  271. return 0;
  272. }
  273. if (dir_end + 1 + c_len(href) + 1 > cap)
  274. return -1;
  275. for (n = 0; n < dir_end; n++)
  276. out[n] = base[n];
  277. out[n++] = '/';
  278. c_copy(out + n, href, cap - n);
  279. return 0;
  280. }
  281. int chat_login_parse_page(const char *html, unsigned int len,
  282. const char *base_url, char *submit_url,
  283. unsigned int cap, char *app_name,
  284. unsigned int name_cap)
  285. {
  286. struct ujson j;
  287. char raw[1024];
  288. int at, s, e, root;
  289. submit_url[0] = '\0';
  290. if (name_cap)
  291. app_name[0] = '\0';
  292. raw[0] = '\0';
  293. at = c_find(html, len, "id=\"cloudron-config\"", 0);
  294. if (at >= 0) {
  295. s = c_find(html, len, ">", (unsigned int)at);
  296. e = s >= 0 ? c_find(html, len, "</script", (unsigned int)s) : -1;
  297. if (s >= 0 && e > s) {
  298. ujson_init(&j, pnodes, sizeof(pnodes) / sizeof(pnodes[0]),
  299. psbuf, sizeof(psbuf));
  300. root = ujson_parse(&j, html + s + 1,
  301. (unsigned int)(e - s - 1));
  302. if (root >= 0) {
  303. c_copy(raw, ujson_get_str(&j, root, "submitUrl"),
  304. sizeof(raw));
  305. if (name_cap)
  306. c_copy(app_name,
  307. ujson_get_str(&j, root, "name"),
  308. name_cap);
  309. }
  310. }
  311. }
  312. if (!raw[0]) {
  313. /* Fallback: a plain scan, unescaping \/ only. */
  314. at = c_find(html, len, "\"submitUrl\":\"", 0);
  315. if (at >= 0) {
  316. unsigned int i = (unsigned int)at + 13, k = 0;
  317. while (i < len && html[i] != '"' && k + 1 < sizeof(raw)) {
  318. if (html[i] == '\\' && i + 1 < len)
  319. i++;
  320. raw[k++] = html[i++];
  321. }
  322. raw[k] = '\0';
  323. if (i >= len || html[i] != '"')
  324. raw[0] = '\0';
  325. }
  326. }
  327. if (!raw[0]) {
  328. if (!c_form_action(html, len, raw, sizeof(raw)))
  329. raw[0] = '\0';
  330. }
  331. if (!raw[0])
  332. return -1;
  333. return resolve(base_url, raw, submit_url, cap);
  334. }
  335. int chat_login_parse_reply(int status, const char *body, unsigned int len,
  336. char *redirect, unsigned int rcap, char *msg,
  337. unsigned int mcap)
  338. {
  339. struct ujson j;
  340. int root;
  341. if (rcap)
  342. redirect[0] = '\0';
  343. if (mcap)
  344. msg[0] = '\0';
  345. ujson_init(&j, pnodes, sizeof(pnodes) / sizeof(pnodes[0]), psbuf,
  346. sizeof(psbuf));
  347. root = ujson_parse(&j, body, len);
  348. if (root >= 0 && ujson_type(&j, root) == UJSON_OBJECT) {
  349. const char *m = ujson_get_str(&j, root, "message");
  350. if (!m[0])
  351. m = ujson_get_str(&j, root, "error");
  352. c_copy(msg, m, mcap);
  353. }
  354. if (status == 401 || status == 403)
  355. return CHAT_REPLY_DENIED;
  356. if (status != 200 || root < 0 || ujson_type(&j, root) != UJSON_OBJECT)
  357. return CHAT_REPLY_ERROR;
  358. if (ujson_truthy(&j, ujson_get(&j, root, "twoFactorRequired"))) {
  359. if (ujson_truthy(&j, ujson_get(&j, root, "totpRequired")))
  360. return CHAT_REPLY_TOTP;
  361. return CHAT_REPLY_PASSKEY;
  362. }
  363. c_copy(redirect, ujson_get_str(&j, root, "redirectTo"), rcap);
  364. if (!redirect[0])
  365. return CHAT_REPLY_ERROR;
  366. return CHAT_REPLY_REDIRECT;
  367. }
  368. #ifndef CHAT_LOGIN_PURE
  369. /* ---- HTTP helpers ---- */
  370. int chat_http_body(struct uhttp *h, char *buf, unsigned int cap)
  371. {
  372. unsigned int n = 0;
  373. for (;;) {
  374. int r;
  375. if (n + 1 >= cap) {
  376. char scratch[1];
  377. /* Full: is there more? */
  378. r = uhttp_read(h, scratch, 1, UHTTP_IDLE_TIMEOUT_MS);
  379. buf[n] = '\0';
  380. if (r > 0) {
  381. h->keepalive = 0;
  382. uhttp_close(h);
  383. return UHTTP_ERR_TOOLONG;
  384. }
  385. break;
  386. }
  387. r = uhttp_read(h, buf + n, cap - 1 - n, UHTTP_IDLE_TIMEOUT_MS);
  388. if (r < 0) {
  389. buf[n] = '\0';
  390. h->keepalive = 0;
  391. uhttp_close(h);
  392. return r;
  393. }
  394. if (r == 0)
  395. break;
  396. n += (unsigned int)r;
  397. }
  398. buf[n] = '\0';
  399. uhttp_close(h);
  400. return (int)n;
  401. }
  402. int chat_http(struct uhttp *h, const char *method, const char *url,
  403. const char *json, char *buf, unsigned int cap,
  404. unsigned int *blen)
  405. {
  406. int r;
  407. if (blen)
  408. *blen = 0;
  409. h->keepalive = 1;
  410. if (method && json)
  411. uhttp_set_method(h, method, json, c_len(json),
  412. "application/json");
  413. else if (method)
  414. /* uhttp only sends Content-Length with a body, so a bodyless
  415. * POST carries an empty JSON object instead. */
  416. uhttp_set_method(h, method, "{}", 2, "application/json");
  417. else
  418. uhttp_set_method(h, 0, 0, 0, 0);
  419. r = uhttp_open(h, url, 0, 0);
  420. if (r != 0)
  421. return r;
  422. r = chat_http_body(h, buf, cap);
  423. if (r < 0)
  424. return r;
  425. if (blen)
  426. *blen = (unsigned int)r;
  427. return h->resp.status;
  428. }
  429. int chat_server_host(const char *server, char *host, unsigned int cap,
  430. int *secure)
  431. {
  432. struct uhttp_url u;
  433. if (uhttp_parse_url(server, &u) != 0)
  434. return -1;
  435. c_copy(host, u.host, cap);
  436. if (secure)
  437. *secure = u.secure;
  438. return 0;
  439. }
  440. static void set_err(struct chat_login *l, const char *a, const char *b)
  441. {
  442. unsigned int n = 0;
  443. while (a && *a && n + 1 < sizeof(l->error))
  444. l->error[n++] = *a++;
  445. while (b && *b && n + 1 < sizeof(l->error))
  446. l->error[n++] = *b++;
  447. l->error[n] = '\0';
  448. }
  449. /* 1 when the response that ended at final_url is back on the Chat host. */
  450. static int on_server(const struct chat_login *l, const char *final_url)
  451. {
  452. unsigned int n = c_len(l->server);
  453. unsigned int i;
  454. for (i = 0; i < n; i++)
  455. if (final_url[i] != l->server[i])
  456. return 0;
  457. return final_url[n] == '\0' || final_url[n] == '/' ||
  458. final_url[n] == '?';
  459. }
  460. int chat_session_check(struct uhttp *h, struct uhttp_jar *jar,
  461. const char *server, char *buf, unsigned int cap,
  462. unsigned int *blen)
  463. {
  464. char url[CHAT_SERVER_MAX + 16];
  465. int st;
  466. c_copy(url, server, sizeof(url));
  467. c_copy(url + c_len(url), "/api/me", sizeof(url) - c_len(url));
  468. h->jar = jar;
  469. h->accept = "application/json";
  470. st = chat_http(h, 0, url, 0, buf, cap, blen);
  471. if (st == 200)
  472. return 0;
  473. if (st == 401 || st == 403)
  474. return 1;
  475. if (st > 0)
  476. return -100 - st;
  477. return st;
  478. }
  479. /* After a successful submit (or an existing provider session) the page may
  480. * still be an interaction: a consent prompt (auto-confirmed, like the web
  481. * UI does) or an abort page (no access to the app). */
  482. static int finish(struct chat_login *l, struct uhttp *h,
  483. struct uhttp_jar *jar, const char *url, char *buf,
  484. unsigned int cap)
  485. {
  486. unsigned int len;
  487. int st, round;
  488. const char *method = 0;
  489. c_copy(l->redirect, url, sizeof(l->redirect));
  490. for (round = 0; round < 3; round++) {
  491. h->jar = jar;
  492. h->accept = "text/html,application/json,*/*";
  493. st = chat_http(h, method, l->redirect, 0, buf, cap, &len);
  494. if (st < 0) {
  495. set_err(l, "Login redirect failed: ",
  496. uhttp_error_string(st));
  497. return CHAT_LOGIN_ERROR;
  498. }
  499. if (on_server(l, h->resp.final_url)) {
  500. unsigned int me_len;
  501. if (c_find(h->resp.final_url,
  502. c_len(h->resp.final_url),
  503. "error=auth_failed", 0) >= 0) {
  504. set_err(l, "The Chat server rejected the login "
  505. "(auth_failed)", 0);
  506. return CHAT_LOGIN_ERROR;
  507. }
  508. st = chat_session_check(h, jar, l->server, buf, cap,
  509. &me_len);
  510. if (st == 0)
  511. return CHAT_LOGIN_OK;
  512. set_err(l, st == 1 ? "Signed in, but the Chat session "
  513. "was not accepted" : "Session check failed", 0);
  514. return CHAT_LOGIN_ERROR;
  515. }
  516. if (chat_login_parse_page(buf, len, h->resp.final_url,
  517. l->submit_url,
  518. sizeof(l->submit_url), l->app_name,
  519. sizeof(l->app_name)) != 0) {
  520. set_err(l, "Login did not return to the Chat server", 0);
  521. return CHAT_LOGIN_ERROR;
  522. }
  523. if (c_suffix(l->submit_url, "/confirm")) {
  524. c_copy(l->redirect, l->submit_url, sizeof(l->redirect));
  525. method = "POST";
  526. continue;
  527. }
  528. if (c_suffix(l->submit_url, "/abort")) {
  529. set_err(l, "This account has no access to the Chat app",
  530. 0);
  531. return CHAT_LOGIN_ERROR;
  532. }
  533. /* Back at a login form: the provider wants credentials. */
  534. return CHAT_LOGIN_FORM;
  535. }
  536. set_err(l, "Too many login interaction steps", 0);
  537. return CHAT_LOGIN_ERROR;
  538. }
  539. int chat_login_start(struct chat_login *l, struct uhttp *h,
  540. struct uhttp_jar *jar, const char *server, char *buf,
  541. unsigned int cap)
  542. {
  543. char url[CHAT_SERVER_MAX + 16];
  544. unsigned int len;
  545. int st;
  546. l->error[0] = '\0';
  547. l->submit_url[0] = '\0';
  548. l->app_name[0] = '\0';
  549. l->totp_required = 0;
  550. if (chat_server_normalize(server, l->server, sizeof(l->server)) != 0) {
  551. set_err(l, "Invalid server address", 0);
  552. return CHAT_LOGIN_ERROR;
  553. }
  554. c_copy(url, l->server, sizeof(url));
  555. c_copy(url + c_len(url), "/auth/login", sizeof(url) - c_len(url));
  556. h->jar = jar;
  557. h->accept = "text/html,*/*";
  558. st = chat_http(h, 0, url, 0, buf, cap, &len);
  559. if (st < 0) {
  560. set_err(l, "Cannot reach the server: ", uhttp_error_string(st));
  561. return CHAT_LOGIN_ERROR;
  562. }
  563. if (on_server(l, h->resp.final_url) && st == 200) {
  564. /* The provider already knew us and sent us straight back. */
  565. unsigned int me_len;
  566. if (chat_session_check(h, jar, l->server, buf, cap,
  567. &me_len) == 0)
  568. return CHAT_LOGIN_OK;
  569. }
  570. if (st != 200) {
  571. char code[8];
  572. unsigned int v = (unsigned int)st, k = 0, i;
  573. char tmp[8];
  574. while (v && k < sizeof(tmp)) {
  575. tmp[k++] = (char)('0' + v % 10);
  576. v /= 10;
  577. }
  578. for (i = 0; i < k; i++)
  579. code[i] = tmp[k - 1 - i];
  580. code[k] = '\0';
  581. set_err(l, "Login page returned HTTP ", code);
  582. return CHAT_LOGIN_ERROR;
  583. }
  584. if (chat_login_parse_page(buf, len, h->resp.final_url, l->submit_url,
  585. sizeof(l->submit_url), l->app_name,
  586. sizeof(l->app_name)) != 0) {
  587. set_err(l, "No Cloudron login form (external SSO or passkey "
  588. "login?) - use the cookie option", 0);
  589. return CHAT_LOGIN_NOFORM;
  590. }
  591. if (c_suffix(l->submit_url, "/confirm") ||
  592. c_suffix(l->submit_url, "/abort"))
  593. return finish(l, h, jar, h->resp.final_url, buf, cap);
  594. return CHAT_LOGIN_FORM;
  595. }
  596. int chat_login_submit(struct chat_login *l, struct uhttp *h,
  597. struct uhttp_jar *jar, const char *user,
  598. const char *pass, const char *totp, char *buf,
  599. unsigned int cap)
  600. {
  601. static char body[1600];
  602. char msg[128];
  603. unsigned int len;
  604. int st, kind;
  605. l->error[0] = '\0';
  606. if (!l->submit_url[0]) {
  607. set_err(l, "No login form", 0);
  608. return CHAT_LOGIN_ERROR;
  609. }
  610. if (chat_login_body(body, sizeof(body), user, pass, totp) < 0) {
  611. set_err(l, "Username or password too long", 0);
  612. return CHAT_LOGIN_ERROR;
  613. }
  614. h->jar = jar;
  615. h->accept = "application/json";
  616. st = chat_http(h, "POST", l->submit_url, body, buf, cap, &len);
  617. /* The body held the password: wipe it right away. */
  618. {
  619. unsigned int i;
  620. for (i = 0; i < sizeof(body); i++)
  621. body[i] = 0;
  622. }
  623. if (st < 0) {
  624. set_err(l, "Login request failed: ", uhttp_error_string(st));
  625. return CHAT_LOGIN_ERROR;
  626. }
  627. kind = chat_login_parse_reply(st, buf, len, l->redirect,
  628. sizeof(l->redirect), msg, sizeof(msg));
  629. switch (kind) {
  630. case CHAT_REPLY_DENIED:
  631. set_err(l, msg[0] ? msg : "Username and password do not match",
  632. 0);
  633. return CHAT_LOGIN_DENIED;
  634. case CHAT_REPLY_TOTP:
  635. l->totp_required = 1;
  636. return CHAT_LOGIN_TOTP;
  637. case CHAT_REPLY_PASSKEY:
  638. set_err(l, "This account signs in with a passkey; use the "
  639. "cookie option", 0);
  640. return CHAT_LOGIN_PASSKEY;
  641. case CHAT_REPLY_REDIRECT:
  642. break;
  643. default:
  644. set_err(l, msg[0] ? msg : "Unexpected reply from the login "
  645. "server", 0);
  646. return CHAT_LOGIN_ERROR;
  647. }
  648. {
  649. char next[CHAT_URL_MAX];
  650. /* redirectTo may be relative to the submit URL. */
  651. if (resolve(l->submit_url, l->redirect, next,
  652. sizeof(next)) != 0) {
  653. set_err(l, "Bad login redirect", 0);
  654. return CHAT_LOGIN_ERROR;
  655. }
  656. st = finish(l, h, jar, next, buf, cap);
  657. }
  658. if (st == CHAT_LOGIN_FORM) {
  659. set_err(l, "The login server asked for the password again", 0);
  660. return CHAT_LOGIN_ERROR;
  661. }
  662. return st;
  663. }
  664. #endif