Skip to content

Latest commit

 

History

History
242 lines (207 loc) · 5.07 KB

auth-common.c

File metadata and controls

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