openconnect.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2016 Intel Corporation.
  5. * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
  6. * Copyright © 2013 John Morrissey <jwm@horde.net>
  7. *
  8. * Author: David Woodhouse <dwmw2@infradead.org>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public License
  12. * version 2.1, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. */
  19. #ifndef __OPENCONNECT_H__
  20. #define __OPENCONNECT_H__
  21. #include <sys/types.h>
  22. #include <stdint.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #ifdef _WIN32
  27. #define uid_t unsigned
  28. #endif
  29. #define OPENCONNECT_API_VERSION_MAJOR 5
  30. #define OPENCONNECT_API_VERSION_MINOR 8
  31. /*
  32. * API version 5.8 (v9.00; 2022-04-29):
  33. * - Add openconnect_set_useragent()
  34. * - Add openconnect_set_external_browser_callback()
  35. * - Add openconnect_set_mca_cert() and openconnect_set_mca_key_password()
  36. *
  37. * API version 5.7 (v8.20; 2022-02-20):
  38. * - Add openconnect_get_connect_url()
  39. * - Add openconnect_set_cookie()
  40. * - Add openconnect_set_allow_insecure_crypto()
  41. * - Add openconnect_get_auth_expiration()
  42. * - Add openconnect_disable_dtls()
  43. * - Make openconnect_disable_ipv6() return int
  44. * - Add openconnect_set_webview_callback()
  45. * - Add openconnect_webview_load_changed()
  46. *
  47. * API version 5.6 (v8.06; 2020-03-31):
  48. * - Add openconnect_set_trojan_interval()
  49. *
  50. * API version 5.5 (v8.00; 2019-01-05):
  51. * - add openconnect_set_version_string()
  52. * - add openconnect_set_key_password()
  53. * - Add openconnect_has_tss2_blob_support()
  54. * - Add openconnect_get_supported_protocols()
  55. * - Add openconnect_free_supported_protocols()
  56. * - Add openconnect_get_protocol()
  57. * - Add openconnect_get_idle_timeout()
  58. *
  59. * API version 5.4 (v7.08; 2016-12-13):
  60. * - Add openconnect_set_pass_tos()
  61. *
  62. * API version 5.3 (v7.07; 2016-07-11):
  63. * - Add openconnect_set_localname().
  64. * - Add openconnect_override_getaddrinfo().
  65. * - Add openconnect_get_cstp_compression().
  66. * - Add openconnect_get_dtls_compression().
  67. * - Add openconnect_disable_ipv6().
  68. * - Add ip_info->gateway_addr.
  69. * - Add openconnect_set_setup_tun_handler().
  70. * - Add openconnect_set_reconnected_handler().
  71. * - Add openconnect_get_dnsname().
  72. * - Add openconnect_get_peer_cert_chain() and
  73. * openconnect_free_peer_cert_chain().
  74. *
  75. * API version 5.2 (v7.05; 2015-03-10):
  76. * - Add openconnect_set_http_auth(), openconnect_set_protocol().
  77. *
  78. * API version 5.1 (v7.05; 2015-03-10):
  79. * - Add openconnect_set_compression_mode(), openconnect_set_loglevel()
  80. *
  81. * (Note: API 5.1 and openconnect_set_compression_mode() were present in
  82. * this file in the v7.04 release on 2015-01-25, but the symbol versioning
  83. * for the new function was OPENCONNECT_5_0, and openconnect_set_loglevel()
  84. * was not yet present.)
  85. *
  86. * API version 5.0 (v7.00; 2014-11-27):
  87. * - Remove OPENCONNECT_X509 and openconnect_get_peer_cert().
  88. * - Change openconnect_get_cert_der() to openconnect_get_peer_cert_DER() etc.
  89. * - Add openconnect_check_peer_cert_hash().
  90. * - Remove openconnect_set_server_cert_sha1().
  91. * - Add openconnect_has_yubioath_support() and OC_TOKEN_MODE_YUBIOATH.
  92. * - Add openconnect_has_system_key_support().
  93. *
  94. * API version 4.1 (v7.00; 2014-11-27):
  95. * - Add openconnect_get_cstp_cipher(), openconnect_get_dtls_cipher(),
  96. * openconnect_set_system_trust(), openconnect_set_csd_environ().
  97. * - Change openconnect_init_ssl() to return int.
  98. *
  99. * API version 4.0 (v7.00; 2014-11-27):
  100. * - Change string handling to never transfer ownership of allocations.
  101. * - Add openconnect_set_option_value(), openconnect_free_cert_info().
  102. *
  103. * API version 3.4 (v7.00; 2014-11-27):
  104. * - Add openconnect_set_token_callbacks()
  105. *
  106. * API version 3.3 (v6.00; 2014-07-08):
  107. * - Add openconnect_set_pfs(), openconnect_set_dpd(),
  108. * openconnect_set_proxy_auth()
  109. *
  110. * API version 3.2 (v5.99; 2014-03-05):
  111. * - Add OC_TOKEN_MODE_HOTP and allow openconnect_has_oath_support() to
  112. * return 2 to indicate that it is present.
  113. *
  114. * API version 3.1 (v5.99; 2014-03-05):
  115. * - Add openconnect_setup_cmd_pipe(), openconnect_mainloop(),
  116. * openconnect_setup_tun_device(), openconnect_setup_tun_script(),
  117. * openconnect_setup_tun_fd(), openconnect_setup_dtls(),
  118. * openconnect_make_cstp_connection(), openconnect_set_server_cert_sha1(),
  119. * openconnect_get_ifname(), openconnect_set_reqmtu(),
  120. * openconnect_get_ip_info(), openconnect_set_protect_socket_handler(),
  121. * openconnect_set_mobile_info(), openconnect_set_xmlpost(),
  122. * openconnect_set_stats_handler()
  123. *
  124. * API version 3.0 (v5.99; 2014-03-05):
  125. * - Change oc_form_opt_select->choices to an array of pointers
  126. * - Add oc_form_opt->flags
  127. * - Add OC_FORM_RESULT_* and oc_auth_form->authgroup_*
  128. *
  129. * API version 2.2 (v5.00; 2013-05-16):
  130. * - Add openconnect_set_token_mode(), openconnect_has_oath_support()
  131. * - Deprecate openconnect_set_stoken_mode()
  132. *
  133. * API version 2.1 (v4.99; 2013-02-07):
  134. * - Add openconnect_set_reported_os()
  135. * - Add openconnect_set_stoken_mode(), openconnect_has_stoken_support()
  136. *
  137. * API version 2.0 (v3.99; 2012-06-13):
  138. * - OPENCONNECT_X509 is now an opaque type.
  139. * - Add openconnect_has_pkcs11_support(), openconnect_has_tss_blob_support()
  140. * - Rename openconnect_init_openssl() -> openconnect_init_ssl()
  141. * - Rename openconnect_vpninfo_new_with_cbdata() -> openconnect_vpninfo_new()
  142. * and kill the old openconnect_vpninfo_new() and its callback types.
  143. *
  144. * API version 1.5 (v3.99; 2012-06-13):
  145. * - Add openconnect_get_cert_details(), openconnect_get_cert_DER().
  146. *
  147. * API version 1.4 (v3.19; 2012-05-17):
  148. * - Add openconnect_set_cancel_fd()
  149. *
  150. * API version 1.3 (v3.13; 2011-09-30):
  151. * - Add openconnect_set_cert_expiry_warning() to change from default 60 days
  152. *
  153. * API version 1.2 (v3.10; 2011-06-30)
  154. * - Add openconnect_vpninfo_new_with_cbdata()
  155. *
  156. * API version 1.1 (v3.02; 2011-04-19):
  157. * - Add openconnect_vpninfo_free()
  158. *
  159. * API version 1.0 (v3.00; 2011-03-09):
  160. * - Initial version
  161. *
  162. * NEW LIBRARY FUNCTION CHECKLIST:
  163. *
  164. * 1) Bump the API version if the current API version has already appeared
  165. * in a release
  166. * 2) Add function to the above changelog
  167. * 3) Add function to libopenconnect.map.in
  168. * 4) Add declaration + comments in the latter part of this file
  169. * 5) Add function to jni.c, then test with ./configure --with-java && make
  170. * 6) Add declaration to LibOpenConnect.java, then run "cd java && ant" to test
  171. */
  172. /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
  173. * Somewhat ironic, that the API version check itself needs to be
  174. * conditionally used depending on the API version. A very simple way
  175. * for users to handle this with an approximately correct answer is
  176. * #include <openconnect.h>
  177. * #ifndef OPENCONNECT_CHECK_VER
  178. * #define OPENCONNECT_CHECK_VER(x,y) 0
  179. * #endif
  180. */
  181. #define OPENCONNECT_CHECK_VER(maj, min) \
  182. (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
  183. (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
  184. OPENCONNECT_API_VERSION_MINOR >= (min)))
  185. /****************************************************************************/
  186. /* Enumeration of supported VPN protocols */
  187. #define OC_PROTO_PROXY (1<<0)
  188. #define OC_PROTO_CSD (1<<1)
  189. #define OC_PROTO_AUTH_CERT (1<<2)
  190. #define OC_PROTO_AUTH_OTP (1<<3)
  191. #define OC_PROTO_AUTH_STOKEN (1<<4)
  192. #define OC_PROTO_PERIODIC_TROJAN (1<<5)
  193. #define OC_PROTO_HIDDEN (1<<6)
  194. #define OC_PROTO_AUTH_MCA (1<<7)
  195. struct oc_vpn_proto {
  196. const char *name;
  197. const char *pretty_name;
  198. const char *description;
  199. unsigned int flags;
  200. };
  201. /****************************************************************************/
  202. /* Authentication form processing */
  203. #define OC_FORM_OPT_TEXT 1
  204. #define OC_FORM_OPT_PASSWORD 2
  205. #define OC_FORM_OPT_SELECT 3
  206. #define OC_FORM_OPT_HIDDEN 4
  207. #define OC_FORM_OPT_TOKEN 5
  208. #define OC_FORM_OPT_SSO_TOKEN 6
  209. #define OC_FORM_OPT_SSO_USER 7
  210. #define OC_FORM_RESULT_ERR -1
  211. #define OC_FORM_RESULT_OK 0
  212. #define OC_FORM_RESULT_CANCELLED 1
  213. #define OC_FORM_RESULT_NEWGROUP 2
  214. #ifdef __OPENCONNECT_PRIVATE__
  215. #define OC_FORM_RESULT_LOGGEDIN 255
  216. #define OC_FORM_OPT_SECOND_AUTH 0x8000
  217. #endif
  218. #define OC_FORM_OPT_IGNORE 0x0001
  219. #define OC_FORM_OPT_NUMERIC 0x0002
  220. /* char * fields are static (owned by XML parser) and don't need to be
  221. freed by the form handling code — except for value, which for TEXT
  222. and PASSWORD options is allocated by openconnect_set_option_value()
  223. when process_form() interacts with the user and must be freed. */
  224. struct oc_form_opt {
  225. struct oc_form_opt *next;
  226. int type;
  227. char *name;
  228. char *label;
  229. char *_value; /* Use openconnect_set_option_value() to set this */
  230. unsigned int flags;
  231. void *reserved;
  232. };
  233. /* To set the value to a form use the following function */
  234. int openconnect_set_option_value(struct oc_form_opt *opt, const char *value);
  235. /* All fields are static, owned by the XML parser */
  236. struct oc_choice {
  237. char *name;
  238. char *label;
  239. char *auth_type;
  240. char *override_name;
  241. char *override_label;
  242. #ifdef __OPENCONNECT_PRIVATE__
  243. int second_auth;
  244. char *secondary_username;
  245. int secondary_username_editable;
  246. int noaaa;
  247. #endif
  248. };
  249. struct oc_form_opt_select {
  250. struct oc_form_opt form;
  251. int nr_choices;
  252. struct oc_choice **choices;
  253. };
  254. /* All char * fields are static, owned by the XML parser */
  255. struct oc_auth_form {
  256. char *banner;
  257. char *message;
  258. char *error;
  259. char *auth_id;
  260. char *method;
  261. char *action;
  262. struct oc_form_opt *opts;
  263. struct oc_form_opt_select *authgroup_opt;
  264. int authgroup_selection;
  265. };
  266. struct oc_split_include {
  267. const char *route;
  268. struct oc_split_include *next;
  269. };
  270. struct oc_ip_info {
  271. const char *addr;
  272. const char *netmask; /* Just the netmask, in dotted-quad form. */
  273. const char *addr6;
  274. const char *netmask6; /* This is the IPv6 address *and* netmask
  275. * e.g. "2001::dead:beef/128". */
  276. const char *dns[3];
  277. const char *nbns[3];
  278. const char *domain;
  279. const char *proxy_pac;
  280. int mtu;
  281. struct oc_split_include *split_dns;
  282. struct oc_split_include *split_includes;
  283. struct oc_split_include *split_excludes;
  284. /* The elements above this line come from server-provided CSTP headers,
  285. * so they should be handled with caution. gateway_addr is generated
  286. * locally from getnameinfo(). */
  287. char *gateway_addr;
  288. };
  289. struct oc_vpn_option {
  290. char *option;
  291. char *value;
  292. struct oc_vpn_option *next;
  293. };
  294. struct oc_stats {
  295. uint64_t tx_pkts;
  296. uint64_t tx_bytes;
  297. uint64_t rx_pkts;
  298. uint64_t rx_bytes;
  299. };
  300. struct oc_cert {
  301. int der_len;
  302. unsigned char *der_data;
  303. void *reserved;
  304. };
  305. /* Used by openconnect_webview_load_changed() to return data to OC.
  306. * The arrays must contain an even number of const strings, with names and
  307. * values, and a NULL to terminate. E.g.:
  308. * name0, val0, name1, val1, NULL
  309. */
  310. struct oc_webview_result {
  311. const char *uri;
  312. const char **cookies;
  313. const char **headers;
  314. };
  315. /****************************************************************************/
  316. #define PRG_ERR 0
  317. #define PRG_INFO 1
  318. #define PRG_DEBUG 2
  319. #define PRG_TRACE 3
  320. /* Byte commands to write into the cmd_fd:
  321. *
  322. * CANCEL closes network connections, logs off the session (cookie)
  323. * and shuts down the tun device.
  324. * PAUSE closes network connections and returns. The caller is expected
  325. * to call openconnect_mainloop() again soon.
  326. * DETACH closes network connections and shuts down the tun device.
  327. * It is not legal to call openconnect_mainloop() again after this,
  328. * but a new instance of openconnect can be started using the same
  329. * cookie.
  330. * STATS calls the stats_handler.
  331. */
  332. #define OC_CMD_CANCEL 'x'
  333. #define OC_CMD_PAUSE 'p'
  334. #define OC_CMD_DETACH 'd'
  335. #define OC_CMD_STATS 's'
  336. #define RECONNECT_INTERVAL_MIN 10
  337. #define RECONNECT_INTERVAL_MAX 100
  338. struct openconnect_info;
  339. typedef enum {
  340. OC_TOKEN_MODE_NONE,
  341. OC_TOKEN_MODE_STOKEN,
  342. OC_TOKEN_MODE_TOTP,
  343. OC_TOKEN_MODE_HOTP,
  344. OC_TOKEN_MODE_YUBIOATH,
  345. OC_TOKEN_MODE_OIDC,
  346. } oc_token_mode_t;
  347. typedef enum {
  348. OC_COMPRESSION_MODE_NONE,
  349. OC_COMPRESSION_MODE_STATELESS,
  350. OC_COMPRESSION_MODE_ALL,
  351. } oc_compression_mode_t;
  352. /* All strings are UTF-8. If operating in a legacy environment where
  353. nl_langinfo(CODESET) returns anything other than UTF-8, or on Windows,
  354. the library will take appropriate steps to convert back to the legacy
  355. character set (or UTF-16) for file handling and wherever else it is
  356. appropriate to do so. Library functions may (but probably don't yet)
  357. return -EILSEQ if passed invalid UTF-8 strings. */
  358. /* Unlike previous versions of openconnect, no functions will take ownership
  359. of the provided strings. */
  360. /* Provide environment variables to be set in the CSD trojan environment
  361. before spawning it. Some callers may need to set $TMPDIR, $PATH and
  362. other such things if not running from a standard UNIX-like environment.
  363. To ensure that a variable is unset, pass its name with value==NULL.
  364. To clear all settings and allow the CSD trojan to inherit an unmodified
  365. environment, call with name==NULL. */
  366. int openconnect_set_csd_environ(struct openconnect_info *vpninfo,
  367. const char *name, const char *value);
  368. /* This string is static, valid only while the connection lasts. If you
  369. * are going to cache this to remember which certs the user has accepted,
  370. * make sure you also store the host/port for which it was accepted and
  371. * don't just accept this cert from *anywhere*. Also use the check
  372. * function below instead of manually comparing. When this function
  373. * returns a string which *doesn't* match the previously-stored hash
  374. * matched with openconnect_check_peer_cert_hash(), you should store
  375. * the new result from this function in place of the old. It means
  376. * we have upgraded to a better hash function. */
  377. const char *openconnect_get_peer_cert_hash(struct openconnect_info *vpninfo);
  378. /* Check if the current peer certificate matches a hash previously
  379. * obtained from openconect_get_peer_cert_hash(). Clients should not
  380. * attempt to do this using strcmp() and the *current* result of
  381. * openconnect_get_peer_cert_hash() because it might use
  382. * a different hash function today. This function will get it right.
  383. * Returns 0 on match; 1 on mismatch, -errno on failure. */
  384. int openconnect_check_peer_cert_hash(struct openconnect_info *vpninfo,
  385. const char *old_hash);
  386. /* The buffers returned by these two functions must be freed with
  387. openconnect_free_cert_info(), especially on Windows. */
  388. char *openconnect_get_peer_cert_details(struct openconnect_info *vpninfo);
  389. /* Returns the length of the created DER output, in a newly-allocated buffer
  390. that will need to be freed by openconnect_free_cert_info(). */
  391. int openconnect_get_peer_cert_DER(struct openconnect_info *vpninfo,
  392. unsigned char **buf);
  393. void openconnect_free_cert_info(struct openconnect_info *vpninfo,
  394. void *buf);
  395. /* Creates a list of all certs in the peer's chain, returning the
  396. number of certs in the chain (or <0 on error). Only valid inside the
  397. validate_peer_cert callback. The caller should free the chain,
  398. but should not modify the contents. */
  399. int openconnect_get_peer_cert_chain(struct openconnect_info *vpninfo,
  400. struct oc_cert **chain);
  401. void openconnect_free_peer_cert_chain(struct openconnect_info *vpninfo,
  402. struct oc_cert *chain);
  403. /* Contains a comma-separated list of authentication methods to enabled.
  404. Currently supported: Negotiate,NTLM,Digest,Basic */
  405. int openconnect_set_http_auth(struct openconnect_info *vpninfo,
  406. const char *methods);
  407. int openconnect_set_proxy_auth(struct openconnect_info *vpninfo,
  408. const char *methods);
  409. int openconnect_set_http_proxy(struct openconnect_info *vpninfo,
  410. const char *proxy);
  411. /* Passing a useragent string to openconnect_vpninfo_new() will still
  412. * append the OpenConnect version number. This function allows the
  413. * full string to be set, for those cases where a VPN server might
  414. * require an *exact* match. */
  415. int openconnect_set_useragent(struct openconnect_info *vpninfo,
  416. const char *useragent);
  417. int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
  418. int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
  419. int openconnect_init_ssl(void);
  420. /* These are strictly cosmetic. The strings differ depending on
  421. * whether OpenSSL or GnuTLS is being used. And even depending on the
  422. * version of GnuTLS. Do *not* attempt to do anything meaningful based
  423. * on matching these strings; if you want to do something like that then
  424. * ask for an API that *does* offer you what you need. */
  425. const char *openconnect_get_cstp_cipher(struct openconnect_info *);
  426. const char *openconnect_get_dtls_cipher(struct openconnect_info *);
  427. /* These return a descriptive string of the compression algorithm
  428. * in use (LZS, LZ4, ...). If no compression then NULL is returned. */
  429. const char *openconnect_get_cstp_compression(struct openconnect_info *);
  430. const char *openconnect_get_dtls_compression(struct openconnect_info *);
  431. /*
  432. * Since authentication can run in a separate environment to the connection
  433. * itself, there is a simple set of information which needs to be passed
  434. * from one to the other. Basically it's just the server to connect to, and
  435. * the cookie we need for authentication. And luckily, as we've added more
  436. * and more protocols over the years, the "cookie" part has remained true
  437. * and we haven't needed to use client certificates for the *connection*.
  438. *
  439. * The *server* part is a little more complex. Firstly, the certificate
  440. * might not be valid and may have been accepted manually by the user,
  441. * so we pass the certificate fingerprint separately, as a second piece
  442. * of information.
  443. * In the beginning, we passed the server hostname as the third piece of
  444. * information, and all was well.
  445. *
  446. * Then we found servers on a non-standard port, so the authentication
  447. * dialogs would use openconnect_get_port() and just append it (":%d")
  448. * to the hostname string they passed on.
  449. *
  450. * Then we encountered servers with round-robin or geo-DNS, which gave
  451. * different IP addresses for a given hostname, and we switched the
  452. * openconnect_get_hostname() function to return the *IP* address instead,
  453. * since the actual name didn't matter when it wasn't being used to check
  454. * the server's certificate anyway.
  455. *
  456. * At some point later, openconnect_get_dnsname() was added to return the
  457. * actual hostname, during the authentication phase where the cert was
  458. * being presented to the user for manual acceptance. But that wasn't
  459. * really important for the authentication → connection handoff.
  460. *
  461. * Later still, Legacy IP addresses got scarce and SNI was invented, and
  462. * we started to see servers behind proxies that forward a connection
  463. * based on the SNI in the incoming ClientHello (TLS layer), or based on
  464. * the 'Host' header in the initial connection-phase request (HTTP[S]
  465. * layer). So returning just the IP address from openconnect_get_hostname()
  466. * now made things break.
  467. *
  468. * So... we need to pass *both* the actual hostname *and* the IP address
  469. * to the connecting openconnect invocation. As well as the port.
  470. *
  471. * In addition, the Pulse protocol introduced a new requirement for the
  472. * connection. Instead of connecting to a fixed endpoint on the server,
  473. * we must connect to the appropriate *path*, which varies. So in fact
  474. * it isn't just the "hostname" any more, but the full URL.
  475. *
  476. * So, now we have openconnect_get_connect_url() which gets the full URL
  477. * including the port and path, and the original hostname.
  478. *
  479. * Since we're now back to giving openconnect a hostname, we need to add
  480. * a '--resolve' argument to avoid the round robin DNS problem and ensure
  481. * that we actually connect to the same server we authenticated to. The
  482. * arguments for that can be obtained from openconnect_get_dnsname() and
  483. * openconnect_get_hostname() — the latter of which, as noted, was changed
  484. * years ago to return a numeric address. We end up invoking openconnect
  485. * to make the connection as follows:
  486. *
  487. * openconnect $CONNECT_URL --servercert $FINGERPRINT --cookie $COOKIE \
  488. * --resolve $DNSNAME:$HOSTNAME
  489. *
  490. * ... where '$HOSTNAME', as returned by openconnect_get_hostname(),
  491. * isn't actually a hostname (as noted above). Sorry.
  492. *
  493. * In fact, what you get back from openconnect_get_hostname() is the
  494. * IP literal in the form it would appear in a URL. So IPv6 addresses
  495. * are wrapped in [], and that needs to be *stripped* in order to pass
  496. * it to openconnect's --resolve argument. As I realise and type this,
  497. * it doesn't seem particularly useful to provide yet another function
  498. * that will return the non-[]-wrapped version, as we'd still need UI
  499. * tools to do it themselves for backward compatibility. Sorry again :)
  500. */
  501. const char *openconnect_get_connect_url(struct openconnect_info *);
  502. /* Returns the IP address of the exact host to which the connection
  503. * was made. In --cookieonly mode or in any other scenario involving
  504. * a "two stage" connection, it is important to reconnect by IP because
  505. * the server side may be using DNS trickery for load balancing.
  506. *
  507. * If the IP address is unavailable due to the use of a proxy, this will
  508. * fall back to returning the DNS name. */
  509. const char *openconnect_get_hostname(struct openconnect_info *);
  510. /* Returns the hostname parsed out of the server name URL. This is
  511. * intended to be used by the validate_peer_cert callback to check that
  512. * the certificate matches the server name. */
  513. const char *openconnect_get_dnsname(struct openconnect_info *);
  514. int openconnect_set_hostname(struct openconnect_info *, const char *);
  515. char *openconnect_get_urlpath(struct openconnect_info *);
  516. int openconnect_set_urlpath(struct openconnect_info *, const char *);
  517. int openconnect_set_localname(struct openconnect_info *, const char *);
  518. /* Some software tokens, such as HOTP tokens, include a counter which
  519. * needs to be stored in persistent storage.
  520. *
  521. * For such tokens, the lock function is first invoked to obtain a lock
  522. * on the storage because we're about to generate a new code. It is
  523. * permitted to call openconnect_set_token_mode() from the lock function,
  524. * if the token storage has been updated since it was first loaded. The
  525. * token mode must not change; only the token secret.
  526. *
  527. * The unlock function is called when a token code has been generated,
  528. * with a new token secret to be written to the persistent storage. The
  529. * secret will be in the same format as it was originally received by
  530. * openconnect_set_token_mode(). The new token may be NULL if an error
  531. * was encountered generating the code, in which case it is only
  532. * necessary for the callback function to unlock the storage.
  533. */
  534. typedef int (*openconnect_lock_token_vfn)(void *tokdata);
  535. typedef int (*openconnect_unlock_token_vfn)(void *tokdata, const char *new_tok);
  536. int openconnect_set_token_callbacks(struct openconnect_info *, void *tokdata,
  537. openconnect_lock_token_vfn,
  538. openconnect_unlock_token_vfn);
  539. int openconnect_set_token_mode(struct openconnect_info *,
  540. oc_token_mode_t, const char *token_str);
  541. /* Legacy stoken-only function; do not use */
  542. int openconnect_set_stoken_mode(struct openconnect_info *, int, const char *);
  543. int openconnect_set_compression_mode(struct openconnect_info *,
  544. oc_compression_mode_t);
  545. /* The size must be 41 bytes, since that's the size of a 20-byte SHA1
  546. represented as hex with a trailing NUL. */
  547. void openconnect_set_xmlsha1(struct openconnect_info *, const char *, int size);
  548. int openconnect_set_cafile(struct openconnect_info *, const char *);
  549. /* call this function to disable the system trust from being used to
  550. * verify the server certificate. @val is a boolean value.
  551. *
  552. * For backwards compatibility reasons this is enabled by default.
  553. */
  554. void openconnect_set_system_trust(struct openconnect_info *vpninfo, unsigned val);
  555. int openconnect_setup_csd(struct openconnect_info *, uid_t, int silent, const char *wrapper);
  556. void openconnect_set_xmlpost(struct openconnect_info *, int enable);
  557. /* Valid choices are: "linux", "linux-64", "win", "mac-intel",
  558. "android", and "apple-ios". This also selects the corresponding CSD
  559. trojan binary. */
  560. int openconnect_set_reported_os(struct openconnect_info *, const char *os);
  561. int openconnect_set_version_string(struct openconnect_info *vpninfo,
  562. const char *version_string);
  563. int openconnect_set_mobile_info(struct openconnect_info *vpninfo,
  564. const char *mobile_platform_version,
  565. const char *mobile_device_type,
  566. const char *mobile_device_uniqueid);
  567. int openconnect_set_client_cert(struct openconnect_info *, const char *cert,
  568. const char *sslkey);
  569. int openconnect_set_key_password(struct openconnect_info *vpninfo, const char *pass);
  570. /**
  571. * Multiple certificate authentication (MCA): the client cert _and_ the
  572. * mca_cert are used for authentication. The mca_cert is used to sign a
  573. * challenge sent by the server.
  574. */
  575. int openconnect_set_mca_cert(struct openconnect_info *, const char *cert,
  576. const char *key);
  577. int openconnect_set_mca_key_password(struct openconnect_info *vpninfo, const char *pass);
  578. const char *openconnect_get_ifname(struct openconnect_info *);
  579. void openconnect_set_reqmtu(struct openconnect_info *, int reqmtu);
  580. void openconnect_set_dpd(struct openconnect_info *, int min_seconds);
  581. void openconnect_set_trojan_interval(struct openconnect_info *, int seconds);
  582. int openconnect_get_idle_timeout(struct openconnect_info *);
  583. time_t openconnect_get_auth_expiration(struct openconnect_info *);
  584. /* The returned structures are owned by the library and may be freed/replaced
  585. due to rekey or reconnect. Assume that once the mainloop starts, the
  586. pointers are no longer valid. For similar reasons, it is unsafe to call
  587. this function from another thread. */
  588. int openconnect_get_ip_info(struct openconnect_info *,
  589. const struct oc_ip_info **info,
  590. const struct oc_vpn_option **cstp_options,
  591. const struct oc_vpn_option **dtls_options);
  592. int openconnect_get_port(struct openconnect_info *);
  593. const char *openconnect_get_cookie(struct openconnect_info *);
  594. int openconnect_set_cookie(struct openconnect_info *, const char *);
  595. void openconnect_clear_cookie(struct openconnect_info *);
  596. int openconnect_disable_ipv6(struct openconnect_info *vpninfo);
  597. int openconnect_disable_dtls(struct openconnect_info *vpninfo);
  598. void openconnect_reset_ssl(struct openconnect_info *vpninfo);
  599. int openconnect_parse_url(struct openconnect_info *vpninfo, const char *url);
  600. void openconnect_set_cert_expiry_warning(struct openconnect_info *vpninfo,
  601. int seconds);
  602. void openconnect_set_pfs(struct openconnect_info *vpninfo, unsigned val);
  603. int openconnect_set_allow_insecure_crypto(struct openconnect_info *vpninfo, unsigned val);
  604. /* If this is set, then openconnect_obtain_cookie() will abort and return
  605. failure if the file descriptor is readable. Typically a user may create
  606. a pair of pipes with the pipe(2) system call, hand the readable one to
  607. this function, and then write a byte to the other end if it ever wants
  608. to cancel the connection. This way, a multi-threaded UI (which will be
  609. running openconnect_obtain_cookie() in a separate thread since it blocks)
  610. has the ability to cancel that call, reap its thread and free the
  611. vpninfo structure (or retry). An 'fd' argument of -1 will render the
  612. cancellation mechanism inactive. */
  613. void openconnect_set_cancel_fd(struct openconnect_info *vpninfo, int fd);
  614. /* Create a nonblocking pipe used to send cancellations and other commands
  615. to the library. This returns a file descriptor to the write side of
  616. the pipe. Both sides will be closed by openconnect_vpninfo_free().
  617. This replaces openconnect_set_cancel_fd(). */
  618. #ifdef _WIN32
  619. SOCKET
  620. #else
  621. int
  622. #endif
  623. openconnect_setup_cmd_pipe(struct openconnect_info *vpninfo);
  624. const char *openconnect_get_version(void);
  625. /* Open CSTP connection; on success, IP information will be available. */
  626. int openconnect_make_cstp_connection(struct openconnect_info *vpninfo);
  627. /* Create a tun device through the OS kernel (typical use case). Both
  628. strings are optional and can be NULL if desired. */
  629. int openconnect_setup_tun_device(struct openconnect_info *vpninfo,
  630. const char *vpnc_script, const char *ifname);
  631. /* Pass traffic to a script program (no tun device). */
  632. int openconnect_setup_tun_script(struct openconnect_info *vpninfo,
  633. const char *tun_script);
  634. #ifdef _WIN32
  635. /* Caller will provide an overlap-capable handle for the tunnel traffic. */
  636. int openconnect_setup_tun_fd(struct openconnect_info *vpninfo, intptr_t tun_fd);
  637. #else
  638. /* Caller will provide a file descriptor for the tunnel traffic. */
  639. int openconnect_setup_tun_fd(struct openconnect_info *vpninfo, int tun_fd);
  640. #endif
  641. /* Optional call to enable DTLS on the connection. */
  642. int openconnect_setup_dtls(struct openconnect_info *vpninfo, int dtls_attempt_period);
  643. /* Start the main loop; exits if OC_CMD_CANCEL is received on cmd_fd or
  644. the remote site aborts. */
  645. int openconnect_mainloop(struct openconnect_info *vpninfo,
  646. int reconnect_timeout,
  647. int reconnect_interval);
  648. /* The first (privdata) argument to each of these functions is either
  649. the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
  650. or if that argument was NULL then it'll be the vpninfo itself. */
  651. /* When the server's certificate fails validation via the normal means,
  652. this function is called with the offending certificate along with
  653. a textual reason for the failure (which may not be translated, if
  654. it comes directly from OpenSSL, but will be if it is rejected for
  655. "certificate does not match hostname", because that check is done
  656. in OpenConnect and *is* translated). The function shall return zero
  657. if the certificate is (or has in the past been) explicitly accepted
  658. by the user, and non-zero to abort the connection. */
  659. typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
  660. const char *reason);
  661. /* On a successful connection, the server may provide us with a new XML
  662. configuration file. This contains the list of servers that can be
  663. chosen by the user to connect to, amongst other stuff that we mostly
  664. ignore. By "new", we mean that the SHA1 indicated by the server does
  665. not match the SHA1 set with the openconnect_set_xmlsha1() above. If
  666. they don't match, or openconnect_set_xmlsha1() has not been called,
  667. then the new XML is downloaded and this function is invoked. */
  668. typedef int (*openconnect_write_new_config_vfn) (void *privdata, const char *buf,
  669. int buflen);
  670. /* Handle an authentication form, requesting input from the user.
  671. * Return value:
  672. * < 0, on error
  673. * = 0, when form was parsed and POST required
  674. * = 1, when response was cancelled by user
  675. */
  676. typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
  677. struct oc_auth_form *form);
  678. /* Logging output which the user *may* want to see. */
  679. typedef void __attribute__ ((format(printf, 3, 4)))
  680. (*openconnect_progress_vfn) (void *privdata, int level,
  681. const char *fmt, ...);
  682. struct openconnect_info *openconnect_vpninfo_new(const char *useragent,
  683. openconnect_validate_peer_cert_vfn,
  684. openconnect_write_new_config_vfn,
  685. openconnect_process_auth_form_vfn,
  686. openconnect_progress_vfn,
  687. void *privdata);
  688. void openconnect_vpninfo_free(struct openconnect_info *vpninfo);
  689. typedef int (*openconnect_open_webview_vfn) (struct openconnect_info *,
  690. const char *uri,
  691. void *privdata);
  692. void openconnect_set_webview_callback(struct openconnect_info *vpninfo,
  693. openconnect_open_webview_vfn);
  694. int openconnect_webview_load_changed(struct openconnect_info *vpninfo,
  695. const struct oc_webview_result *result);
  696. void openconnect_set_external_browser_callback(struct openconnect_info *vpninfo,
  697. openconnect_open_webview_vfn);
  698. /* Callback to allow binding a newly created socket's file descriptor to
  699. a specific interface, e.g. with SO_BINDTODEVICE. This tells the kernel
  700. not to route the traffic in question over the VPN tunnel. */
  701. typedef void (*openconnect_protect_socket_vfn) (void *privdata, int fd);
  702. void openconnect_set_protect_socket_handler(struct openconnect_info *vpninfo,
  703. openconnect_protect_socket_vfn protect_socket);
  704. void openconnect_set_loglevel(struct openconnect_info *vpninfo, int level);
  705. void openconnect_set_pass_tos(struct openconnect_info *vpninfo, int enable);
  706. /* Callback for obtaining traffic stats via OC_CMD_STATS.
  707. */
  708. typedef void (*openconnect_stats_vfn) (void *privdata, const struct oc_stats *stats);
  709. void openconnect_set_stats_handler(struct openconnect_info *vpninfo,
  710. openconnect_stats_vfn stats_handler);
  711. /* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
  712. can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
  713. int openconnect_has_pkcs11_support(void);
  714. /* The OpenSSL TPM ENGINE stores keys in a PEM file labelled with the string
  715. -----BEGIN TSS KEY BLOB-----. */
  716. int openconnect_has_tss_blob_support(void);
  717. int openconnect_has_tss2_blob_support(void);
  718. /* Software token capabilities. */
  719. int openconnect_has_stoken_support(void);
  720. int openconnect_has_oath_support(void);
  721. int openconnect_has_yubioath_support(void);
  722. int openconnect_has_system_key_support(void);
  723. /* Query and select from among supported protocols */
  724. int openconnect_get_supported_protocols(struct oc_vpn_proto **protos);
  725. void openconnect_free_supported_protocols(struct oc_vpn_proto *protos);
  726. const char *openconnect_get_protocol(struct openconnect_info *vpninfo);
  727. int openconnect_set_protocol(struct openconnect_info *vpninfo, const char *protocol);
  728. struct addrinfo;
  729. typedef int (*openconnect_getaddrinfo_vfn) (void *privdata, const char *node, const char *service,
  730. const struct addrinfo *hints, struct addrinfo **res);
  731. void openconnect_override_getaddrinfo(struct openconnect_info *vpninfo, openconnect_getaddrinfo_vfn gai_fn);
  732. /* Callback for configuring the interface after tunnel is fully up. */
  733. typedef void (*openconnect_setup_tun_vfn) (void *privdata);
  734. void openconnect_set_setup_tun_handler(struct openconnect_info *vpninfo,
  735. openconnect_setup_tun_vfn setup_tun);
  736. /* Callback for indicating that a TCP reconnection succeeded. */
  737. typedef void (*openconnect_reconnected_vfn) (void *privdata);
  738. void openconnect_set_reconnected_handler(struct openconnect_info *vpninfo,
  739. openconnect_reconnected_vfn reconnected_fn);
  740. #ifdef __cplusplus
  741. }
  742. #endif
  743. #endif /* __OPENCONNECT_H__ */