2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2011 Intel Corporation.
6 * Authors: David Woodhouse <dwmw2@infradead.org>
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
25 #include "openconnect-internal.h"
27 struct openconnect_info *openconnect_vpninfo_new_with_cbdata (char *useragent,
28 openconnect_validate_peer_cert_vfn validate_peer_cert,
29 openconnect_write_new_config_vfn write_new_config,
30 openconnect_process_auth_form_vfn process_auth_form,
31 openconnect_progress_vfn progress,
34 struct openconnect_info *vpninfo = calloc (sizeof(*vpninfo), 1);
38 vpninfo->useragent = openconnect_create_useragent (useragent);
39 vpninfo->validate_peer_cert = validate_peer_cert;
40 vpninfo->write_new_config = write_new_config;
41 vpninfo->process_auth_form = process_auth_form;
42 vpninfo->progress = progress;
43 vpninfo->cbdata = privdata?:vpninfo;
48 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
49 openconnect_validate_peer_cert_fn validate_peer_cert,
50 openconnect_write_new_config_fn write_new_config,
51 openconnect_process_auth_form_fn process_auth_form,
52 openconnect_progress_fn progress)
54 return openconnect_vpninfo_new_with_cbdata (useragent,
55 (void *)validate_peer_cert,
56 (void *)write_new_config,
57 (void *)process_auth_form,
58 (void *)progress, NULL);
61 static void free_optlist (struct vpn_option *opt)
63 struct vpn_option *next;
65 for (; opt; opt = next) {
73 void openconnect_vpninfo_free (struct openconnect_info *vpninfo)
75 openconnect_reset_ssl(vpninfo);
76 free_optlist(vpninfo->cookies);
77 free_optlist(vpninfo->cstp_options);
78 free_optlist(vpninfo->dtls_options);
79 free(vpninfo->hostname);
80 free(vpninfo->urlpath);
81 free(vpninfo->redirect_url);
82 free(vpninfo->proxy_type);
84 free(vpninfo->csd_scriptname);
85 free(vpninfo->csd_stuburl);
86 /* These are const in openconnect itself, but for consistency of
87 the library API we do take ownership of the strings we're given,
88 and thus we have to free them too. */
89 free((void *)vpninfo->cafile);
90 if (vpninfo->cert != vpninfo->sslkey)
91 free((void *)vpninfo->sslkey);
92 free((void *)vpninfo->cert);
93 /* No need to free deflate streams; they weren't initialised */
97 char *openconnect_get_hostname (struct openconnect_info *vpninfo)
99 return vpninfo->hostname;
102 void openconnect_set_hostname (struct openconnect_info *vpninfo, char *hostname)
104 vpninfo->hostname = hostname;
107 char *openconnect_get_urlpath (struct openconnect_info *vpninfo)
109 return vpninfo->urlpath;
112 void openconnect_set_urlpath (struct openconnect_info *vpninfo, char *urlpath)
114 vpninfo->urlpath = urlpath;
117 void openconnect_set_xmlsha1 (struct openconnect_info *vpninfo, char *xmlsha1, int size)
119 if (size != sizeof (vpninfo->xmlsha1))
122 memcpy (&vpninfo->xmlsha1, xmlsha1, size);
125 void openconnect_set_cafile (struct openconnect_info *vpninfo, char *cafile)
127 vpninfo->cafile = cafile;
130 void openconnect_setup_csd (struct openconnect_info *vpninfo, uid_t uid, int silent, char *wrapper)
132 vpninfo->uid_csd = uid;
133 vpninfo->uid_csd_given = silent?2:1;
134 vpninfo->csd_wrapper = wrapper;
137 void openconnect_set_client_cert (struct openconnect_info *vpninfo, char *cert, char *sslkey)
139 vpninfo->cert = cert;
141 vpninfo->sslkey = sslkey;
143 vpninfo->sslkey = cert;
146 struct x509_st *openconnect_get_peer_cert (struct openconnect_info *vpninfo)
148 return SSL_get_peer_certificate(vpninfo->https_ssl);
151 int openconnect_get_port (struct openconnect_info *vpninfo)
153 return vpninfo->port;
156 char *openconnect_get_cookie (struct openconnect_info *vpninfo)
158 return vpninfo->cookie;
161 void openconnect_clear_cookie (struct openconnect_info *vpninfo)
164 memset(vpninfo->cookie, 0, strlen(vpninfo->cookie));
167 void openconnect_reset_ssl (struct openconnect_info *vpninfo)
169 if (vpninfo->https_ssl) {
170 openconnect_close_https(vpninfo);
172 if (vpninfo->peer_addr) {
173 free(vpninfo->peer_addr);
174 vpninfo->peer_addr = NULL;
176 if (vpninfo->https_ctx) {
177 SSL_CTX_free(vpninfo->https_ctx);
178 vpninfo->https_ctx = NULL;
182 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url)
184 if (vpninfo->peer_addr) {
185 free(vpninfo->peer_addr);
186 vpninfo->peer_addr = NULL;
189 return internal_parse_url (url, NULL, &vpninfo->hostname,
190 &vpninfo->port, &vpninfo->urlpath, 443);
193 const char *openconnect_get_version (void)
195 return openconnect_version;