auth-html.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2015-2021 David Woodhouse, Daniel Lenski
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>, Daniel Lenski <dlenski@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include <libxml/HTMLparser.h>
  20. #include <libxml/HTMLtree.h>
  21. #include <errno.h>
  22. xmlNodePtr htmlnode_next(xmlNodePtr top, xmlNodePtr node)
  23. {
  24. while (!node->next) {
  25. node = node->parent;
  26. if (!node || node == top)
  27. return NULL;
  28. }
  29. return node->next;
  30. }
  31. xmlNodePtr htmlnode_dive(xmlNodePtr top, xmlNodePtr node)
  32. {
  33. if (node->children)
  34. return node->children;
  35. return htmlnode_next(top, node);
  36. }
  37. xmlNodePtr find_form_node(xmlDocPtr doc)
  38. {
  39. xmlNodePtr root, node;
  40. for (root = node = xmlDocGetRootElement(doc); node; node = htmlnode_dive(root, node)) {
  41. if (node->name && !strcasecmp((char *)node->name, "form"))
  42. return node;
  43. }
  44. return NULL;
  45. }
  46. int parse_input_node(struct openconnect_info *vpninfo, struct oc_auth_form *form,
  47. xmlNodePtr node, const char *submit_button,
  48. int (*can_gen_tokencode)(struct openconnect_info *vpninfo, struct oc_auth_form *form, struct oc_form_opt *opt))
  49. {
  50. char *type = (char *)xmlGetProp(node, (unsigned char *)"type"), *style = (char *)xmlGetProp(node, (unsigned char *)"style");
  51. struct oc_form_opt **p = &form->opts;
  52. struct oc_form_opt *opt;
  53. int ret = 0;
  54. int nodisplay = style && !strcmp(style, "display: none;"); /* XX: Fortinet-specific */
  55. if (!type)
  56. return -EINVAL;
  57. opt = calloc(1, sizeof(*opt));
  58. if (!opt) {
  59. ret = -ENOMEM;
  60. goto out;
  61. }
  62. if (nodisplay || !strcasecmp(type, "hidden")) {
  63. opt->type = OC_FORM_OPT_HIDDEN;
  64. xmlnode_get_prop(node, "name", &opt->name);
  65. xmlnode_get_prop(node, "value", &opt->_value);
  66. /* XXX: Handle tz_offset / tz */
  67. } else if (!strcasecmp(type, "password")) {
  68. opt->type = OC_FORM_OPT_PASSWORD;
  69. xmlnode_get_prop(node, "name", &opt->name);
  70. if (asprintf(&opt->label, "%s:", opt->name) == -1) {
  71. ret = -ENOMEM;
  72. goto out;
  73. }
  74. if (can_gen_tokencode && !can_gen_tokencode(vpninfo, form, opt))
  75. opt->type = OC_FORM_OPT_TOKEN;
  76. } else if (!strcasecmp(type, "text") || !strcasecmp(type, "username") || !strcasecmp(type, "email")) {
  77. opt->type = OC_FORM_OPT_TEXT;
  78. xmlnode_get_prop(node, "name", &opt->name);
  79. if (asprintf(&opt->label, "%s:", opt->name) == -1) {
  80. ret = -ENOMEM;
  81. goto out;
  82. }
  83. if (vpninfo->proto->proto == PROTO_NC &&
  84. !strcmp(form->auth_id, "loginForm") &&
  85. !strcmp(opt->name, "VerificationCode") &&
  86. can_gen_tokencode && !can_gen_tokencode(vpninfo, form, opt))
  87. opt->type = OC_FORM_OPT_TOKEN;
  88. } else if (!strcasecmp(type, "submit")) {
  89. /* XX: can be ignored except for Juniper */
  90. if (vpninfo->proto->proto != PROTO_NC)
  91. goto free_out;
  92. xmlnode_get_prop(node, "name", &opt->name);
  93. if (opt->name && submit_button && (!strcmp(opt->name, submit_button) ||
  94. !strcmp(opt->name, "sn-postauth-proceed") ||
  95. !strcmp(opt->name, "sn-preauth-proceed") ||
  96. !strcmp(opt->name, "secidactionEnter"))) {
  97. /* Use this as the 'Submit' action for the form, by
  98. implicitly adding it as a hidden option. */
  99. xmlnode_get_prop(node, "value", &opt->_value);
  100. opt->type = OC_FORM_OPT_HIDDEN;
  101. } else {
  102. vpn_progress(vpninfo, PRG_DEBUG,
  103. _("Ignoring unknown form submit item '%s'\n"),
  104. opt->name);
  105. ret = -EINVAL;
  106. goto out;
  107. }
  108. } else if (!strcasecmp(type, "checkbox")) {
  109. opt->type = OC_FORM_OPT_HIDDEN;
  110. xmlnode_get_prop(node, "name", &opt->name);
  111. xmlnode_get_prop(node, "value", &opt->_value);
  112. } else {
  113. vpn_progress(vpninfo, PRG_DEBUG,
  114. _("Ignoring unknown form input type '%s'\n"),
  115. type);
  116. ret = -EINVAL;
  117. goto out;
  118. }
  119. /* Append to the existing list */
  120. while (*p) {
  121. if (!strcmp((*p)->name, opt->name)) {
  122. vpn_progress(vpninfo, PRG_DEBUG,
  123. _("Discarding duplicate option '%s'\n"),
  124. opt->name);
  125. free_opt(opt);
  126. goto out;
  127. }
  128. p = &(*p)->next;
  129. }
  130. *p = opt;
  131. out:
  132. if (ret)
  133. free_out:
  134. free_opt(opt);
  135. free(type);
  136. free(style);
  137. return ret;
  138. }
  139. int parse_select_node(struct openconnect_info *vpninfo, struct oc_auth_form *form,
  140. xmlNodePtr node)
  141. {
  142. xmlNodePtr child;
  143. struct oc_form_opt_select *opt;
  144. struct oc_choice *choice;
  145. opt = calloc(1, sizeof(*opt));
  146. if (!opt)
  147. return -ENOMEM;
  148. xmlnode_get_prop(node, "name", &opt->form.name);
  149. opt->form.label = strdup(opt->form.name);
  150. opt->form.type = OC_FORM_OPT_SELECT;
  151. if ((vpninfo->proto->proto == PROTO_NC && !strcmp(opt->form.name, "realm")) ||
  152. (vpninfo->proto->proto == PROTO_F5 && !strcmp(opt->form.name, "domain")))
  153. form->authgroup_opt = opt;
  154. for (child = node->children; child; child = child->next) {
  155. struct oc_choice **new_choices;
  156. if (!child->name || strcasecmp((const char *)child->name, "option"))
  157. continue;
  158. choice = calloc(1, sizeof(*choice));
  159. if (!choice) {
  160. free_opt((void *)opt);
  161. return -ENOMEM;
  162. }
  163. xmlnode_get_prop(child, "value", &choice->name);
  164. choice->label = (char *)xmlNodeGetContent(child);
  165. new_choices = realloc(opt->choices, sizeof(opt->choices[0]) * (opt->nr_choices+1));
  166. if (!new_choices) {
  167. free_opt((void *)opt);
  168. free(choice);
  169. return -ENOMEM;
  170. }
  171. opt->choices = new_choices;
  172. opt->choices[opt->nr_choices++] = choice;
  173. }
  174. /* Prepend to the existing list */
  175. opt->form.next = form->opts;
  176. form->opts = &opt->form;
  177. return 0;
  178. }
  179. struct oc_auth_form *parse_form_node(struct openconnect_info *vpninfo,
  180. xmlNodePtr node, const char *submit_button,
  181. int (*can_gen_tokencode)(struct openconnect_info *vpninfo, struct oc_auth_form *form, struct oc_form_opt *opt))
  182. {
  183. struct oc_auth_form *form = calloc(1, sizeof(*form));
  184. xmlNodePtr child;
  185. if (!form)
  186. return NULL;
  187. xmlnode_get_prop(node, "method", &form->method);
  188. xmlnode_get_prop(node, "action", &form->action);
  189. if (!form->method || strcasecmp(form->method, "POST")) {
  190. vpn_progress(vpninfo, PRG_ERR,
  191. _("Cannot handle form method='%s', action='%s'\n"),
  192. form->method, form->action);
  193. free(form);
  194. return NULL;
  195. }
  196. if (vpninfo->proto->proto == PROTO_NC) {
  197. /* XX: some forms have 'id', but no 'name' */
  198. if (!xmlnode_get_prop(node, "name", &form->auth_id) ||
  199. !xmlnode_get_prop(node, "id", &form->auth_id))
  200. form->banner = strdup(form->auth_id);
  201. } else if (vpninfo->proto->proto == PROTO_F5)
  202. xmlnode_get_prop(node, "id", &form->auth_id);
  203. /* XX: fallback auth_id (since other functions expect it to exist) */
  204. if (!form->auth_id)
  205. form->auth_id = strdup("unknown");
  206. for (child = htmlnode_dive(node, node); child && child != node; child = htmlnode_dive(node, child)) {
  207. if (!child->name)
  208. continue;
  209. if (!strcasecmp((char *)child->name, "input"))
  210. parse_input_node(vpninfo, form, child, submit_button, can_gen_tokencode);
  211. else if (!strcasecmp((char *)child->name, "select")) {
  212. parse_select_node(vpninfo, form, child);
  213. /* Skip its children */
  214. while (child->children)
  215. child = child->last;
  216. } else if (vpninfo->proto->proto == PROTO_F5
  217. && !strcasecmp((char *)child->name, "td")) {
  218. char *id = (char *)xmlGetProp(child, (unsigned char *)"id");
  219. if (id && !strcmp(id, "credentials_table_header")) {
  220. char *msg = (char *)xmlNodeGetContent(child);
  221. if (msg) {
  222. free(form->banner);
  223. form->banner = msg;
  224. }
  225. } else if (id && !strcmp(id, "credentials_table_postheader")) {
  226. char *msg = (char *)xmlNodeGetContent(child);
  227. if (msg) {
  228. free(form->message);
  229. form->message = msg;
  230. }
  231. }
  232. free(id);
  233. } else if (vpninfo->proto->proto == PROTO_NC &&
  234. !strcasecmp((char *)child->name, "textarea")) {
  235. /* display the post sign-in message, if any */
  236. char *fieldname = (char *)xmlGetProp(child, (unsigned char *)"name");
  237. if (fieldname && (!strcasecmp(fieldname, "sn-postauth-text") || /* XX: Juniper-specific */
  238. !strcasecmp(fieldname, "sn-preauth-text"))) {
  239. char *postauth_msg = (char *)xmlNodeGetContent(child);
  240. if (postauth_msg) {
  241. free(form->banner);
  242. form->banner = postauth_msg;
  243. }
  244. } else {
  245. vpn_progress(vpninfo, PRG_ERR,
  246. _("Unknown textarea field: '%s'\n"), fieldname);
  247. }
  248. free(fieldname);
  249. } else if (vpninfo->proto->proto == PROTO_FORTINET &&
  250. !strcasecmp((char *)child->name, "b")) {
  251. char *msg = (char *)xmlNodeGetContent(child);
  252. if (msg) {
  253. free(form->message);
  254. form->message = msg;
  255. }
  256. }
  257. }
  258. return form;
  259. }