Debian Codegraph @

src/openconnect_helper.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * ConnMan GTK GUI
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Author: Jaakko Hannikainen <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <glib.h>
22 #include <gio/gio.h>
23 #include "config.h"
24
25 #ifdef USE_OPENCONNECT
26
27 #include <openconnect.h>
28 #include <glib/gi18n.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "openconnect_helper.h"
34 #include "../src/configurator.h"
35 #include "../src/main.h"
36 #include "../src/util.h"
37 #include "../src/dialog.h"
38
39 #ifdef USE_OPENCONNECT_DYNAMIC
40 #include <dlfcn.h>
41 #endif /* USE_OPENCONNECT_DYNAMIC */
42
43 typedef int (*process_form_fn) (void *privdata, struct oc_auth_form *form);
44 typedef void (*show_progress_fn) (void *privdata, int level,
45 const char *fmt, ...);
46
47 typedef void (*vpninfo_free_fn)(struct openconnect_info *vpninfo);
48 typedef int (*obtain_cookie_fn)(struct openconnect_info *vpninfo);
49 typedef int (*passphrase_from_fsid_fn)(struct openconnect_info *vpninfo);
50
51 #if OPENCONNECT_CHECK_VER(5, 0)
52 typedef int (*validate_cert_fn) (void *privdata, const char *reason);
53 typedef const char *(*get_peer_cert_hash_fn)(struct openconnect_info *vpninfo);
54 #else /* !OPENCONNECT_CHECK_VER(5, 0) */
55 typedef int (*validate_cert_fn) (void *privdata, OPENCONNECT_X509 *cert,
56 const char *reason);
57 typedef OPENCONNECT_X509 *(*get_peer_cert_fn)(struct openconnect_info *);
58 typedef int (*get_cert_sha1_fn)(struct openconnect_info *vpninfo,
59 OPENCONNECT_X509 *cert, char *buf);
60 #endif /* !OPENCONNECT_CHECK_VER(5, 0) */
61
62 #if OPENCONNECT_CHECK_VER(4, 1)
63 typedef int (*init_ssl_fn)(void);
64 #else
65 typedef void (*init_ssl_fn)(void);
66 #endif
67
68 #if OPENCONNECT_CHECK_VER(4, 0)
69 typedef int (*write_config_fn) (void *privdata, const char *buf, int buflen);
70
71 typedef struct openconnect_info *(*vpninfo_new_fn)(const char *useragent,
72 validate_cert_fn,
73 write_config_fn,
74 process_form_fn,
75 show_progress_fn,
76 void *privdata);
77 typedef const char *(*get_cookie_fn)(struct openconnect_info *);
78 typedef const char *(*get_hostname_fn)(struct openconnect_info *);
79
80 typedef int (*set_client_cert_fn)(struct openconnect_info *, const char *cert,
81 const char *sslkey);
82 typedef int (*set_cafile_fn)(struct openconnect_info *, const char *);
83 typedef int (*parse_url_fn)(struct openconnect_info *vpninfo, const char *url);
84 typedef int (*set_option_value_fn)(struct oc_form_opt *opt, const char *value);
85 #else /* !OPENCONNECT_CHECK_VERSION(4, 0) */
86 typedef int (*write_config_fn) (void *privdata, char *buf, int buflen);
87
88 typedef struct openconnect_info *(*vpninfo_new_fn)(char *useragent,
89 validate_cert_fn,
90 write_config_fn,
91 process_form_fn,
92 show_progress_fn,
93 void *privdata);
94 typedef char *(*get_cookie_fn)(struct openconnect_info *);
95 typedef char *(*get_hostname_fn)(struct openconnect_info *);
96
97 typedef void (*set_client_cert_fn)(struct openconnect_info *, char *cert,
98 char *sslkey);
99 typedef void (*set_cafile_fn)(struct openconnect_info *, char *);
100 typedef int (*parse_url_fn)(struct openconnect_info *vpninfo, char *url);
101 #endif /* !OPENCONNECT_CHECK_VERSION(4, 0) */
102
103 static init_ssl_fn init_ssl;
104 static vpninfo_free_fn vpninfo_free;
105 static obtain_cookie_fn obtain_cookie;
106 static passphrase_from_fsid_fn passphrase_from_fsid;
107
108 static vpninfo_new_fn _vpninfo_new;
109 static get_cookie_fn _get_cookie;
110 static get_hostname_fn _get_hostname;
111 static set_client_cert_fn _set_client_cert;
112 static set_cafile_fn _set_cafile;
113 static parse_url_fn _parse_url;
114
115 #if OPENCONNECT_CHECK_VER(5, 0)
116 static get_peer_cert_hash_fn _get_peer_cert_hash;
117 #else
118 static get_peer_cert_fn _get_peer_cert;
119 static get_cert_sha1_fn _get_cert_sha1;
120 #endif
121
122 #if OPENCONNECT_CHECK_VER(4, 0)
123 static set_option_value_fn _set_option_value;
124 #endif
125
126 static gboolean init_lib(void) {
127 static gboolean initialized = FALSE;
128 static gboolean loaded = FALSE;
129 if(initialized)
130 return loaded;
131 initialized = TRUE;
132 #ifndef USE_OPENCONNECT_DYNAMIC
133 init_ssl = openconnect_init_ssl;
134 vpninfo_free = openconnect_vpninfo_free;
135 obtain_cookie = openconnect_obtain_cookie;
136
137 _vpninfo_new = openconnect_vpninfo_new;
138 _get_cookie = openconnect_get_cookie;
139 _get_hostname = openconnect_get_hostname;
140 _set_client_cert = openconnect_set_client_cert;
141 passphrase_from_fsid = openconnect_passphrase_from_fsid;
142 _set_cafile = openconnect_set_cafile;
143 _parse_url = openconnect_parse_url;
144 #if OPENCONNECT_CHECK_VER(5, 0)
145 _get_peer_cert_hash = openconnect_get_peer_cert_hash;
146 #else
147 _get_peer_cert = openconnect_get_peer_cert;
148 _get_cert_sha1 = openconnect_get_cert_sha1;
149 #endif
150
151 #if OPENCONNECT_CHECK_VER(4, 0)
152 _set_option_value = openconnect_set_option_value;
153 #endif
154
155 #else
156 void *lib = dlopen("libopenconnect.so", RTLD_NOW);
157 /* technically this shouldn't fail since getting here depends
158 * on ConnMan having openconnect support */
159 if(!lib)
160 return FALSE;
161 init_ssl = dlsym(lib, "openconnect_init_ssl");
162 vpninfo_free = dlsym(lib, "openconnect_vpninfo_free");
163 obtain_cookie = dlsym(lib, "openconnect_obtain_cookie");
164
165 _vpninfo_new = dlsym(lib, "openconnect_vpninfo_new");
166 _get_cookie = dlsym(lib, "openconnect_get_cookie");
167 _get_hostname = dlsym(lib, "openconnect_get_hostname");
168 _set_client_cert = dlsym(lib, "openconnect_set_client_cert");
169 passphrase_from_fsid = dlsym(lib, "openconnect_passphrase_from_fsid");
170 _set_cafile = dlsym(lib, "openconnect_set_cafile");
171 _parse_url = dlsym(lib, "openconnect_parse_url");
172 #if OPENCONNECT_CHECK_VER(5, 0)
173 _get_peer_cert_hash = dlsym(lib, "openconnect_get_peer_cert_hash");
174 #else
175 _get_peer_cert = dlsym(lib, "openconnect_get_peer_cert");
176 _get_cert_sha1 = dlsym(lib, "openconnect_get_cert_sha1");
177 #endif
178
179 #if OPENCONNECT_CHECK_VER(4, 0)
180 _set_option_value = dlsym(lib, "openconnect_set_option_value");
181 #endif
182
183 #endif /* USE_OPENCONNECT_DYNAMIC */
184 loaded = TRUE;
185 return TRUE;
186 };
187
188 #if OPENCONNECT_CHECK_VER(4, 0)
189 struct openconnect_info *vpninfo_new(const char *useragent,
190 validate_cert_fn validate,
191 write_config_fn new_config,
192 process_form_fn process_form,
193 show_progress_fn show_progress,
194 void *privdata)
195 {
196 return _vpninfo_new(useragent, validate, new_config,
197 process_form, show_progress, privdata);
198 }
199
200 void set_client_cert(struct openconnect_info *info, const char *key,
201 const char *sslkey)
202 {
203 _set_client_cert(info, key, sslkey);
204 }
205
206 void set_cafile(struct openconnect_info *info, const char *file)
207 {
208 _set_cafile(info, file);
209 }
210
211 void parse_url(struct openconnect_info *info, const char *url)
212 {
213 _parse_url(info, url);
214 }
215
216 static char *get_peer_cert_hash(struct openconnect_info *info)
217 {
218 return g_strdup(_get_peer_cert_hash(info));
219 }
220
221 static void set_option_value(struct oc_form_opt *opt, char *value)
222 {
223 _set_option_value(opt, value);
224 g_free(value);
225 }
226
227 #else
228
229 struct openconnect_info *vpninfo_new(const char *useragent,
230 validate_cert_fn validate,
231 write_config_fn new_config,
232 process_form_fn process_form,
233 show_progress_fn show_progress,
234 void *privdata)
235 {
236 return _vpninfo_new(g_strdup(useragent), validate, new_config,
237 process_form, show_progress, privdata);
238 }
239
240 void set_client_cert(struct openconnect_info *info, const char *key,
241 const char *sslkey)
242 {
243 _set_client_cert(info, g_strdup(key), g_strdup(sslkey));
244 }
245
246 void set_cafile(struct openconnect_info *info, const char *file)
247 {
248 _set_cafile(info, g_strdup(file));
249 }
250
251 void parse_url(struct openconnect_info *info, const char *url)
252 {
253 _parse_url(info, g_strdup(url));
254 }
255
256 static char *get_peer_cert_hash(struct openconnect_info *info)
257 {
258 char buf[41] = {0, };
259 char *out;
260 OPENCONNECT_X509 *cert = _get_peer_cert(info);
261
262 if(!cert)
263 return g_strdup("");
264
265 _get_cert_sha1(info, cert, buf);
266 out = g_strdup(buf);
267 /* openconnect's documentation states that cert should be now freed
268 * with X509_free... */
269 return out;
270 }
271
272 static void set_option_value(struct oc_form_opt *opt, char *value)
273 {
274 opt->value = value;
275 }
276
277 #endif
278
279 static const char *get_cookie(struct openconnect_info *info)
280 {
281 return _get_cookie(info);
282 }
283
284 static const char *get_hostname(struct openconnect_info *info)
285 {
286 return _get_hostname(info);
287 }
288
289 GString *progress;
290
291 static int check_cert(const char *reason)
292 {
293 GPtrArray *tokens;
294 gboolean status;
295 const gchar *message;
296
297 tokens = g_ptr_array_new_full(0, (GDestroyNotify)free_token_element);
298
299 message = _("Certificate from VPN server failed verification. Reason:");
300 g_ptr_array_add(tokens, token_new_text(message, NULL));
301 g_ptr_array_add(tokens, token_new_text(reason, NULL));
302 message = _("Continue connecting?");
303 g_ptr_array_add(tokens, token_new_text(message, NULL));
304 status = dialog_ask_tokens(_("Certificate failure"), tokens);
305 g_ptr_array_free(tokens, TRUE);
306 return !status;
307 }
308
309 #if OPENCONNECT_CHECK_VER(4, 0)
310 static int invalid_cert(void *data, const char *reason)
311 {
312 printf("%s\n", reason);
313 return check_cert(reason);
314 }
315
316 static int new_config(void *data, const char *buf, int buflen)
317 {
318 return 0;
319 }
320 #else /* !OPENCONNECT_CHECK_VER(4, 0) */
321 static int new_config(void *data, char *buf, int buflen)
322 {
323 return 0;
324 }
325
326 static int invalid_cert(void *data, OPENCONNECT_X509 *cert, const char *reason)
327 {
328 printf("%s\n", reason);
329 return check_cert(reason);
330 }
331 #endif /* !OPENCONNECT_CHECK_VER(4, 0) */
332
333 static int ask_pass(void *data, struct oc_auth_form *form)
334 {
335 struct oc_form_opt *opt;
336 GPtrArray *tokens;
337 int i;
338
339 tokens = g_ptr_array_new_full(0, (GDestroyNotify)free_token_element);
340
341 for(opt = form->opts; opt; opt = opt->next) {
342 struct token_element *elem = NULL;
343
344 if(opt->flags & OC_FORM_OPT_IGNORE)
345 continue;
346
347 switch(opt->type) {
348 case OC_FORM_OPT_SELECT: {
349 struct oc_form_opt_select *select;
350 GPtrArray *options = g_ptr_array_new();
351 int i;
352
353 select = (struct oc_form_opt_select *)opt;
354 for(i = 0; i < select->nr_choices; i++)
355 g_ptr_array_add(options,
356 select->choices[i]->label);
357 elem = token_new_list(select->form.label, options);
358 break;
359 }
360 case OC_FORM_OPT_TEXT:
361 elem = token_new_entry(opt->label, FALSE);
362 break;
363 case OC_FORM_OPT_PASSWORD:
364 elem = token_new_entry(opt->label, TRUE);
365 break;
366 case OC_FORM_OPT_TOKEN:
367 break;
368 }
369 if(elem)
370 g_ptr_array_add(tokens, elem);
371 }
372
•373 if(!dialog_ask_tokens(_("Enter AnyConnect credentials"), tokens)) {
374 g_ptr_array_free(tokens, TRUE);
375 return OC_FORM_RESULT_CANCELLED;
376 }
377
378 i = 0;
379 for(opt = form->opts; opt; opt = opt->next) {
380 struct token_element *elem = NULL;
381
382 if(opt->flags & OC_FORM_OPT_IGNORE)
383 continue;
384
385 switch(opt->type) {
386 case OC_FORM_OPT_SELECT:
387 case OC_FORM_OPT_TEXT:
388 case OC_FORM_OPT_PASSWORD:
389 elem = tokens->pdata[i++];
390 set_option_value(opt, elem->value);
391 elem->value = NULL;
392 break;
393 case OC_FORM_OPT_TOKEN:
394 break;
395 }
396 }
397
398 g_ptr_array_free(tokens, TRUE);
399
400 return OC_FORM_RESULT_OK;
401 }
402
403 static void show_progress(void *data, int level, const char *fmt, ...)
404 {
405 gchar *msg;
406 va_list argp;
407
408 va_start(argp, fmt);
409 msg = g_strdup_vprintf(fmt, argp);
410 va_end(argp);
411 printf("%s", msg);
412 g_string_append(progress, msg);
413 g_free(msg);
414 }
415
416 static GVariantDict *get_tokens(GHashTable *info)
417 {
418 GVariantDict *tokens = NULL;
419 gchar *host, *cert, *hash;
420 const gchar *cookie, *hostname;
421 struct openconnect_info *vpninfo;
422 int status;
423
424 init_ssl();
425 progress = g_string_new(NULL);
426 vpninfo = vpninfo_new("linux-64", invalid_cert, new_config,
427 ask_pass, show_progress, NULL);
428
429 host = g_hash_table_lookup(info, "Host");
430
431 cert = g_hash_table_lookup(info, "OpenConnect.ClientCert");
432 if(cert)
433 set_client_cert(vpninfo, cert, NULL);
434
435 cert = g_hash_table_lookup(info, "OpenConnect.CACert");
436 if(cert)
437 set_cafile(vpninfo, cert);
438
439 if(use_fsid)
440 passphrase_from_fsid(vpninfo);
441
442 parse_url(vpninfo, host);
443
444 status = obtain_cookie(vpninfo);
445 if(status) {
446 if(status != OC_FORM_RESULT_CANCELLED)
447 show_error(_("Connecting to VPN failed."),
448 progress->str);
449 goto out;
450 }
451
452 tokens = g_variant_dict_new(NULL);
453
454 hash = get_peer_cert_hash(vpninfo);
455 cookie = get_cookie(vpninfo);
456 hostname = get_hostname(vpninfo);
457 g_variant_dict_insert(tokens, "OpenConnect.ServerCert", "s", hash);
458 g_variant_dict_insert(tokens, "OpenConnect.Cookie", "s", cookie);
459 g_variant_dict_insert(tokens, "OpenConnect.VPNHost", "s", hostname);
460 g_free(hash);
461 out:
462 vpninfo_free(vpninfo);
463 g_string_free(progress, TRUE);
464 return tokens;
465 }
466
467 GVariantDict *openconnect_handle(GDBusMethodInvocation *invocation,
468 GVariant *args)
469 {
470 GHashTable *info, *required;
471 GVariantDict *out;
472 GVariantIter *iter;
473 gchar *key;
474 GVariant *value;
475 GVariant *parameters;
476
477 if(!init_lib())
478 return NULL;
479
480 parameters = g_variant_get_child_value(args, 1);
481
482 info = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
483 required = g_hash_table_new_full(g_str_hash, g_str_equal,
484 g_free, g_free);
485
486 iter = g_variant_iter_new(parameters);
487 while(g_variant_iter_loop(iter, "{sv}", &key, &value)) {
488 GVariantDict *dict = g_variant_dict_new(value);
489 GVariant *req_v, *val_v;
490 const gchar *req;
491 gchar *val;
492
493 req_v = g_variant_dict_lookup_value(dict, "Requirement", NULL);
494 val_v = g_variant_dict_lookup_value(dict, "Value", NULL);
495 req = g_variant_get_string(req_v, NULL);
496 if(val_v) {
497 val = g_variant_dup_string(val_v, NULL);
498 if(!strcmp(req, "informational"))
499 g_hash_table_insert(info, g_strdup(key), val);
500 g_variant_unref(val_v);
501 } else if(!strcmp(req, "mandatory"))
502 g_hash_table_insert(required, g_strdup(key), NULL);
503 g_variant_unref(req_v);
504 g_variant_dict_unref(dict);
505 }
506 g_variant_iter_free(iter);
507
508 out = get_tokens(info);
509
510 g_hash_table_unref(required);
511 g_hash_table_unref(info);
512
513 return out;
514 }
515
516 gboolean is_openconnect(GVariant *args)
517 {
518 GVariantIter *iter;
519 gchar *path;
520 GVariant *value, *service, *parameters;
521 gboolean openconnect = FALSE;
522
523 service = g_variant_get_child_value(args, 0);
524 parameters = g_variant_get_child_value(args, 1);
525
526 iter = g_variant_iter_new(parameters);
527
528 while(g_variant_iter_loop(iter, "{sv}", &path, &value))
529 openconnect = openconnect || strstr(path, "OpenConnect");
530 g_variant_iter_free(iter);
531
532 g_variant_unref(service);
533 g_variant_unref(parameters);
534
535 return openconnect && init_lib();
536 }
537
538 #else
539
540 GVariantDict *openconnect_handle(GDBusMethodInvocation *invocation,
541 GVariant *args)
542 {
543 return NULL;
544 }
545
546 gboolean is_openconnect(GVariant *args)
547 {
548 return FALSE;
549 }
550
551 #endif /* USE_OPENCONNECT */