Skip to content

Latest commit

 

History

History
364 lines (316 loc) · 8.56 KB

library.c

File metadata and controls

364 lines (316 loc) · 8.56 KB
 
1
2
3
/*
* OpenConnect (SSL + DTLS) VPN client
*
May 13, 2012
May 13, 2012
4
* Copyright © 2008-2012 Intel Corporation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*
* Authors: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
Apr 11, 2012
Apr 11, 2012
25
#include <string.h>
May 13, 2012
May 13, 2012
26
#include <errno.h>
May 29, 2012
May 29, 2012
27
#include <stdlib.h>
Apr 11, 2012
Apr 11, 2012
28
Oct 15, 2012
Oct 15, 2012
29
30
31
32
#ifdef LIBSTOKEN_HDR
#include LIBSTOKEN_HDR
#endif
Oct 28, 2012
Oct 28, 2012
33
34
#include <libxml/tree.h>
35
36
#include "openconnect-internal.h"
Jun 8, 2012
Jun 8, 2012
37
struct openconnect_info *openconnect_vpninfo_new (char *useragent,
Jun 27, 2011
Jun 27, 2011
38
39
40
41
42
openconnect_validate_peer_cert_vfn validate_peer_cert,
openconnect_write_new_config_vfn write_new_config,
openconnect_process_auth_form_vfn process_auth_form,
openconnect_progress_vfn progress,
void *privdata)
43
44
45
46
{
struct openconnect_info *vpninfo = calloc (sizeof(*vpninfo), 1);
vpninfo->ssl_fd = -1;
Sep 29, 2011
Sep 29, 2011
47
vpninfo->cert_expire_warning = 60 * 86400;
48
49
50
51
52
vpninfo->useragent = openconnect_create_useragent (useragent);
vpninfo->validate_peer_cert = validate_peer_cert;
vpninfo->write_new_config = write_new_config;
vpninfo->process_auth_form = process_auth_form;
vpninfo->progress = progress;
Jun 27, 2011
Jun 27, 2011
53
vpninfo->cbdata = privdata?:vpninfo;
May 12, 2012
May 12, 2012
54
vpninfo->cancel_fd = -1;
Oct 28, 2012
Oct 28, 2012
55
openconnect_set_reported_os(vpninfo, NULL);
Nov 24, 2011
Nov 24, 2011
57
58
59
60
#ifdef ENABLE_NLS
bindtextdomain("openconnect", LOCALEDIR);
#endif
Oct 28, 2012
Oct 28, 2012
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int openconnect_set_reported_os (struct openconnect_info *vpninfo, const char *os)
{
if (!os) {
#if defined(__APPLE__)
os = "mac";
#else
os = sizeof(long) > 4 ? "linux-64" : "linux";
#endif
}
/* FIXME: is there a special platname for 64-bit Windows? */
if (!strcmp(os, "mac"))
vpninfo->csd_xmltag = "csdMac";
else if (!strcmp(os, "linux") || !strcmp(os, "linux-64"))
vpninfo->csd_xmltag = "csdLinux";
else if (!strcmp(os, "win"))
vpninfo->csd_xmltag = "csd";
else
return -EINVAL;
vpninfo->platname = os;
return 0;
}
Mar 17, 2011
Mar 17, 2011
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static void free_optlist (struct vpn_option *opt)
{
struct vpn_option *next;
for (; opt; opt = next) {
next = opt->next;
free(opt->option);
free(opt->value);
free(opt);
}
}
void openconnect_vpninfo_free (struct openconnect_info *vpninfo)
{
Jul 10, 2012
Jul 10, 2012
102
103
openconnect_close_https(vpninfo, 1);
free(vpninfo->peer_addr);
Mar 17, 2011
Mar 17, 2011
104
105
106
107
108
109
110
111
free_optlist(vpninfo->cookies);
free_optlist(vpninfo->cstp_options);
free_optlist(vpninfo->dtls_options);
free(vpninfo->hostname);
free(vpninfo->urlpath);
free(vpninfo->redirect_url);
free(vpninfo->proxy_type);
free(vpninfo->proxy);
Oct 28, 2012
Oct 28, 2012
112
Jul 12, 2012
Jul 12, 2012
113
114
115
116
if (vpninfo->csd_scriptname) {
unlink(vpninfo->csd_scriptname);
free(vpninfo->csd_scriptname);
}
Oct 28, 2012
Oct 28, 2012
117
118
free(vpninfo->csd_token);
free(vpninfo->csd_ticket);
Mar 17, 2011
Mar 17, 2011
119
free(vpninfo->csd_stuburl);
Oct 28, 2012
Oct 28, 2012
120
121
122
free(vpninfo->csd_starturl);
free(vpninfo->csd_waiturl);
free(vpninfo->csd_preurl);
Oct 28, 2012
Oct 28, 2012
123
124
if (vpninfo->opaque_srvdata)
xmlFreeNode(vpninfo->opaque_srvdata);
Oct 28, 2012
Oct 28, 2012
125
Mar 17, 2011
Mar 17, 2011
126
127
128
129
130
131
/* These are const in openconnect itself, but for consistency of
the library API we do take ownership of the strings we're given,
and thus we have to free them too. */
free((void *)vpninfo->cafile);
if (vpninfo->cert != vpninfo->sslkey)
free((void *)vpninfo->sslkey);
Mar 17, 2011
Mar 17, 2011
132
free((void *)vpninfo->cert);
May 29, 2012
May 29, 2012
133
134
if (vpninfo->peer_cert) {
#if defined (OPENCONNECT_OPENSSL)
May 17, 2012
May 17, 2012
135
X509_free(vpninfo->peer_cert);
May 29, 2012
May 29, 2012
136
137
138
139
140
#elif defined (OPENCONNECT_GNUTLS)
gnutls_x509_crt_deinit(vpninfo->peer_cert);
#endif
vpninfo->peer_cert = NULL;
}
Jun 12, 2012
Jun 12, 2012
141
free(vpninfo->useragent);
Oct 15, 2012
Oct 15, 2012
142
143
144
145
146
147
#ifdef LIBSTOKEN_HDR
if (vpninfo->stoken_pin)
free(vpninfo->stoken_pin);
if (vpninfo->stoken_ctx)
stoken_destroy(vpninfo->stoken_ctx);
#endif
Mar 17, 2011
Mar 17, 2011
148
149
150
151
/* No need to free deflate streams; they weren't initialised */
free(vpninfo);
}
152
153
char *openconnect_get_hostname (struct openconnect_info *vpninfo)
{
Feb 22, 2013
Feb 22, 2013
154
return vpninfo->unique_hostname?:vpninfo->hostname;
155
156
157
158
}
void openconnect_set_hostname (struct openconnect_info *vpninfo, char *hostname)
{
Feb 22, 2013
Feb 22, 2013
159
free(vpninfo->hostname);
160
vpninfo->hostname = hostname;
Feb 22, 2013
Feb 22, 2013
161
162
free(vpninfo->unique_hostname);
vpninfo->unique_hostname = NULL;
163
164
165
166
167
168
169
170
171
172
173
174
}
char *openconnect_get_urlpath (struct openconnect_info *vpninfo)
{
return vpninfo->urlpath;
}
void openconnect_set_urlpath (struct openconnect_info *vpninfo, char *urlpath)
{
vpninfo->urlpath = urlpath;
}
Jun 1, 2012
Jun 1, 2012
175
void openconnect_set_xmlsha1 (struct openconnect_info *vpninfo, const char *xmlsha1, int size)
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
if (size != sizeof (vpninfo->xmlsha1))
return;
memcpy (&vpninfo->xmlsha1, xmlsha1, size);
}
void openconnect_set_cafile (struct openconnect_info *vpninfo, char *cafile)
{
vpninfo->cafile = cafile;
}
void openconnect_setup_csd (struct openconnect_info *vpninfo, uid_t uid, int silent, char *wrapper)
{
vpninfo->uid_csd = uid;
vpninfo->uid_csd_given = silent?2:1;
vpninfo->csd_wrapper = wrapper;
}
void openconnect_set_client_cert (struct openconnect_info *vpninfo, char *cert, char *sslkey)
{
vpninfo->cert = cert;
if (sslkey)
vpninfo->sslkey = sslkey;
else
vpninfo->sslkey = cert;
}
May 29, 2012
May 29, 2012
204
OPENCONNECT_X509 *openconnect_get_peer_cert (struct openconnect_info *vpninfo)
May 17, 2012
May 17, 2012
206
return vpninfo->peer_cert;
207
208
209
210
211
212
213
214
215
216
217
218
219
220
}
int openconnect_get_port (struct openconnect_info *vpninfo)
{
return vpninfo->port;
}
char *openconnect_get_cookie (struct openconnect_info *vpninfo)
{
return vpninfo->cookie;
}
void openconnect_clear_cookie (struct openconnect_info *vpninfo)
{
Mar 17, 2011
Mar 17, 2011
221
222
if (vpninfo->cookie)
memset(vpninfo->cookie, 0, strlen(vpninfo->cookie));
223
224
225
226
}
void openconnect_reset_ssl (struct openconnect_info *vpninfo)
{
Jul 10, 2012
Jul 10, 2012
227
openconnect_close_https(vpninfo, 0);
Apr 19, 2011
Apr 19, 2011
228
if (vpninfo->peer_addr) {
229
230
231
232
233
234
235
free(vpninfo->peer_addr);
vpninfo->peer_addr = NULL;
}
}
int openconnect_parse_url (struct openconnect_info *vpninfo, char *url)
{
May 13, 2012
May 13, 2012
236
237
238
char *scheme = NULL;
int ret;
Apr 19, 2011
Apr 19, 2011
239
240
241
242
243
if (vpninfo->peer_addr) {
free(vpninfo->peer_addr);
vpninfo->peer_addr = NULL;
}
May 13, 2012
May 13, 2012
244
245
free(vpninfo->hostname);
vpninfo->hostname = NULL;
Feb 22, 2013
Feb 22, 2013
246
247
free(vpninfo->unique_hostname);
vpninfo->unique_hostname = NULL;
May 13, 2012
May 13, 2012
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
free(vpninfo->urlpath);
vpninfo->urlpath = NULL;
ret = internal_parse_url (url, &scheme, &vpninfo->hostname,
&vpninfo->port, &vpninfo->urlpath, 443);
if (ret) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse server URL '%s'\n"),
url);
return ret;
}
if (scheme && strcmp(scheme, "https")) {
vpn_progress(vpninfo, PRG_ERR,
_("Only https:// permitted for server URL\n"));
ret = -EINVAL;
}
free(scheme);
return ret;
Mar 9, 2011
Mar 9, 2011
268
Sep 30, 2011
Sep 30, 2011
269
270
271
272
273
274
void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
int seconds)
{
vpninfo->cert_expire_warning = seconds;
}
May 12, 2012
May 12, 2012
275
276
277
278
279
void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd)
{
vpninfo->cancel_fd = fd;
}
Mar 9, 2011
Mar 9, 2011
280
281
const char *openconnect_get_version (void)
{
May 12, 2012
May 12, 2012
282
return openconnect_version_str;
Mar 9, 2011
Mar 9, 2011
283
}
Jun 11, 2012
Jun 11, 2012
284
285
286
287
288
289
290
291
292
int openconnect_has_pkcs11_support(void)
{
#if defined (OPENCONNECT_GNUTLS) && defined (HAVE_P11KIT)
return 1;
#else
return 0;
#endif
}
Jun 11, 2012
Jun 11, 2012
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#if defined (OPENCONNECT_OPENSSL) && defined (HAVE_ENGINE)
#include <openssl/engine.h>
#endif
int openconnect_has_tss_blob_support(void)
{
#if defined (OPENCONNECT_OPENSSL) && defined (HAVE_ENGINE)
ENGINE *e;
ENGINE_load_builtin_engines();
e = ENGINE_by_id("tpm");
if (e) {
ENGINE_free(e);
return 1;
}
Jun 13, 2012
Jun 13, 2012
309
310
#elif defined (OPENCONNECT_GNUTLS) && defined (HAVE_TROUSERS)
return 1;
Jun 11, 2012
Jun 11, 2012
311
312
313
#endif
return 0;
}
Oct 15, 2012
Oct 15, 2012
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
int openconnect_has_stoken_support(void)
{
#ifdef LIBSTOKEN_HDR
return 1;
#else
return 0;
#endif
}
/*
* Enable software token generation if use_stoken == 1.
*
* If token_str is not NULL, try to parse the string. Otherwise, try to read
* the token data from ~/.stokenrc
*
* Return value:
* = -EOPNOTSUPP, if libstoken is not available
* = -EINVAL, if the token string is invalid (token_str was provided)
* = -ENOENT, if ~/.stokenrc is missing (token_str was NULL)
* = -EIO, for other libstoken failures
* = 0, on success
*/
int openconnect_set_stoken_mode (struct openconnect_info *vpninfo,
int use_stoken, const char *token_str)
{
#ifdef LIBSTOKEN_HDR
int ret;
vpninfo->use_stoken = 0;
if (!use_stoken)
return 0;
if (!vpninfo->stoken_ctx) {
vpninfo->stoken_ctx = stoken_new();
if (!vpninfo->stoken_ctx)
return -EIO;
}
ret = token_str ?
stoken_import_string(vpninfo->stoken_ctx, token_str) :
stoken_import_rcfile(vpninfo->stoken_ctx, NULL);
if (ret)
return ret;
vpninfo->use_stoken = 1;
return 0;
#else
return -EOPNOTSUPP;
#endif
}