auth-common.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  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 <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. int xmlnode_is_named(xmlNode *xml_node, const char *name)
  30. {
  31. return !strcmp((char *)xml_node->name, name);
  32. }
  33. /* similar to auth.c's xmlnode_get_text, including that *var should be freed by the caller,
  34. but without the hackish param / %s handling that Cisco needs. */
  35. int xmlnode_get_val(xmlNode *xml_node, const char *name, char **var)
  36. {
  37. char *str;
  38. if (name && !xmlnode_is_named(xml_node, name))
  39. return -EINVAL;
  40. str = (char *)xmlNodeGetContent(xml_node);
  41. if (!str)
  42. return -ENOENT;
  43. free(*var);
  44. *var = str;
  45. return 0;
  46. }
  47. int xmlnode_get_prop(xmlNode *xml_node, const char *name, char **var)
  48. {
  49. char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
  50. if (!str)
  51. return -ENOENT;
  52. free(*var);
  53. *var = str;
  54. return 0;
  55. }
  56. int xmlnode_match_prop(xmlNode *xml_node, const char *name, const char *match)
  57. {
  58. char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
  59. int ret = 0;
  60. if (!str)
  61. return -ENOENT;
  62. if (strcmp(str, match))
  63. ret = -EEXIST;
  64. free(str);
  65. return ret;
  66. }
  67. int append_opt(struct oc_text_buf *body, const char *opt, const char *name)
  68. {
  69. if (buf_error(body))
  70. return buf_error(body);
  71. if (body->pos)
  72. buf_append(body, "&");
  73. buf_append_urlencoded(body, opt);
  74. buf_append(body, "=");
  75. buf_append_urlencoded(body, name);
  76. return 0;
  77. }
  78. int append_form_opts(struct openconnect_info *vpninfo,
  79. struct oc_auth_form *form, struct oc_text_buf *body)
  80. {
  81. struct oc_form_opt *opt;
  82. int ret;
  83. for (opt = form->opts; opt; opt = opt->next) {
  84. ret = append_opt(body, opt->name, opt->_value);
  85. if (ret)
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. void clear_mem(void *p, size_t s)
  91. {
  92. #if defined(HAVE_MEMSET_S)
  93. memset_s(p, s, 0x5a, s);
  94. #elif defined(HAVE_EXPLICIT_MEMSET)
  95. explicit_memset(p, 0x5a, s);
  96. #elif defined(HAVE_EXPLICIT_BZERO)
  97. explicit_bzero(p, s);
  98. #elif defined(_WIN32)
  99. SecureZeroMemory(p, s);
  100. #else
  101. volatile char *pp = (volatile char *)p;
  102. while (s--)
  103. *(pp++) = 0x5a;
  104. #endif
  105. }
  106. void free_pass(char **p)
  107. {
  108. if (!*p)
  109. return;
  110. clear_mem(*p, strlen(*p));
  111. free(*p);
  112. *p = NULL;
  113. }
  114. void free_opt(struct oc_form_opt *opt)
  115. {
  116. if (!opt)
  117. return;
  118. /* for SELECT options, opt->value is a pointer to oc_choice->name */
  119. if (opt->type != OC_FORM_OPT_SELECT) {
  120. free_pass(&opt->_value);
  121. } else {
  122. struct oc_form_opt_select *sel = (void *)opt;
  123. int i;
  124. for (i = 0; i < sel->nr_choices; i++) {
  125. free(sel->choices[i]->name);
  126. free(sel->choices[i]->label);
  127. free(sel->choices[i]->auth_type);
  128. free(sel->choices[i]->override_name);
  129. free(sel->choices[i]->override_label);
  130. free(sel->choices[i]);
  131. }
  132. free(sel->choices);
  133. }
  134. free(opt->name);
  135. free(opt->label);
  136. free(opt);
  137. }
  138. void free_auth_form(struct oc_auth_form *form)
  139. {
  140. if (!form)
  141. return;
  142. while (form->opts) {
  143. struct oc_form_opt *tmp = form->opts->next;
  144. free_opt(form->opts);
  145. form->opts = tmp;
  146. }
  147. free(form->error);
  148. free(form->message);
  149. free(form->banner);
  150. free(form->auth_id);
  151. free(form->method);
  152. free(form->action);
  153. free(form);
  154. }
  155. /* Return value:
  156. * < 0, if unable to generate a tokencode
  157. * = 0, on success
  158. */
  159. int do_gen_tokencode(struct openconnect_info *vpninfo,
  160. struct oc_auth_form *form)
  161. {
  162. struct oc_form_opt *opt;
  163. for (opt = form->opts; ; opt = opt->next) {
  164. /* this form might not have anything for us to do */
  165. if (!opt)
  166. return 0;
  167. if (opt->type == OC_FORM_OPT_TOKEN)
  168. break;
  169. }
  170. switch (vpninfo->token_mode) {
  171. #ifdef HAVE_LIBSTOKEN
  172. case OC_TOKEN_MODE_STOKEN:
  173. return do_gen_stoken_code(vpninfo, form, opt);
  174. #endif
  175. case OC_TOKEN_MODE_TOTP:
  176. return do_gen_totp_code(vpninfo, form, opt);
  177. case OC_TOKEN_MODE_HOTP:
  178. return do_gen_hotp_code(vpninfo, form, opt);
  179. #ifdef HAVE_LIBPCSCLITE
  180. case OC_TOKEN_MODE_YUBIOATH:
  181. return do_gen_yubikey_code(vpninfo, form, opt);
  182. #endif
  183. default:
  184. return -EINVAL;
  185. }
  186. }
  187. int can_gen_tokencode(struct openconnect_info *vpninfo,
  188. struct oc_auth_form *form,
  189. struct oc_form_opt *opt)
  190. {
  191. switch (vpninfo->token_mode) {
  192. #ifdef HAVE_LIBSTOKEN
  193. case OC_TOKEN_MODE_STOKEN:
  194. return can_gen_stoken_code(vpninfo, form, opt);
  195. #endif
  196. case OC_TOKEN_MODE_TOTP:
  197. return can_gen_totp_code(vpninfo, form, opt);
  198. case OC_TOKEN_MODE_HOTP:
  199. return can_gen_hotp_code(vpninfo, form, opt);
  200. #ifdef HAVE_LIBPCSCLITE
  201. case OC_TOKEN_MODE_YUBIOATH:
  202. return can_gen_yubikey_code(vpninfo, form, opt);
  203. #endif
  204. default:
  205. return -EINVAL;
  206. }
  207. }