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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
Authors:
Pavel Březina <pbrezina@redhat.com>
Copyright (C) 2021 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include <arpa/inet.h>
#include <krb5/preauth_plugin.h>
#include "krb5_plugin/common/utils.h"
#include "krb5_plugin/idp/idp.h"
void
sss_idp_config_free(struct sss_idp_config *idpcfg)
{
if (idpcfg == NULL) {
return;
}
if (idpcfg->type != NULL) {
free(idpcfg->type);
}
sss_string_array_free(idpcfg->indicators);
free(idpcfg);
}
/**
* [{
* "type": "oauth2",
* "indicators": ["..."] (optional)
* }]
*
* Note: array and type is used for future extensibility.
*/
krb5_error_code
sss_idp_config_init(const char *config,
struct sss_idp_config **_idpcfg)
{
struct sss_idp_config *idpcfg;
json_t *jindicators = NULL;
json_error_t jret;
json_t *jroot;
krb5_error_code ret;
idpcfg = malloc(sizeof(struct sss_idp_config));
if (idpcfg == NULL) {
return ENOMEM;
}
memset(idpcfg, 0, sizeof(struct sss_idp_config));
jroot = json_loads(config, 0, &jret);
if (jroot == NULL) {
ret = EINVAL;
goto done;
}
/* Only one item is supported at the moment. The rest is ignored. */
ret = json_unpack(jroot, "[{s:s, s?:o}]",
"type", &idpcfg->type,
"indicators", &jindicators);
if (ret != 0) {
ret = EINVAL;
goto done;
}
/* Only oauth2 type is supported at the moment. */
if (strcmp(idpcfg->type, "oauth2") != 0) {
ret = EINVAL;
goto done;
}
idpcfg->type = strdup(idpcfg->type);
if (idpcfg->type == NULL) {
ret = ENOMEM;
goto done;
}
/* Are indicators set? */
if (jindicators != NULL) {
idpcfg->indicators = sss_json_array_to_strings(jindicators);
if (idpcfg->indicators == NULL) {
ret = EINVAL;
goto done;
}
}
*_idpcfg = idpcfg;
ret = 0;
done:
if (ret != 0) {
sss_idp_config_free(idpcfg);
}
if (jroot != NULL) {
json_decref(jroot);
}
return ret;
}
void
sss_idp_oauth2_free(struct sss_idp_oauth2 *data)
{
if (data == NULL) {
return;
}
free(data->verification_uri);
free(data->verification_uri_complete);
free(data->user_code);
free(data);
}
static struct sss_idp_oauth2 *
sss_idp_oauth2_init(const char *verification_uri,
const char *verification_uri_complete,
const char *user_code)
{
struct sss_idp_oauth2 *data;
/* These are required fields. */
if (is_empty(verification_uri) || is_empty(user_code)) {
return NULL;
}
data = malloc(sizeof(struct sss_idp_oauth2));
if (data == NULL) {
return NULL;
}
memset(data, 0, sizeof(struct sss_idp_oauth2));
data->verification_uri = strdup(verification_uri);
data->user_code = strdup(user_code);
if (data->verification_uri == NULL || data->user_code == NULL) {
sss_idp_oauth2_free(data);
return NULL;
}
if (!is_empty(verification_uri_complete)) {
data->verification_uri_complete = strdup(verification_uri_complete);
if (data->verification_uri_complete == NULL) {
sss_idp_oauth2_free(data);
return NULL;
}
}
return data;
}
static struct sss_idp_oauth2 *
sss_idp_oauth2_from_json(const char *json_str)
{
struct sss_idp_oauth2 jdata = {0};
struct sss_idp_oauth2 *data;
json_error_t jret;
json_t *jroot;
int ret;
jroot = json_loads(json_str, 0, &jret);
if (jroot == NULL) {
return NULL;
}
ret = json_unpack(jroot, "{s:s, s?:s, s:s}",
"verification_uri", &jdata.verification_uri,
"verification_uri_complete", &jdata.verification_uri_complete,
"user_code", &jdata.user_code);
if (ret != 0) {
json_decref(jroot);
return NULL;
}
data = sss_idp_oauth2_init(jdata.verification_uri,
jdata.verification_uri_complete,
jdata.user_code);
json_decref(jroot);
return data;
}
static char *
sss_idp_oauth2_to_json(const struct sss_idp_oauth2 *data)
{
json_t *jroot;
char *str;
if (data == NULL) {
return NULL;
}
/* These are required fields. */
if (data->verification_uri == NULL || data->user_code == NULL) {
return NULL;
}
jroot = json_pack("{s:s?, s:s*, s:s?}",
"verification_uri", data->verification_uri,
"verification_uri_complete", data->verification_uri_complete,
"user_code", data->user_code);
if (jroot == NULL) {
return NULL;
}
str = json_dumps(jroot, JSON_COMPACT);
json_decref(jroot);
return str;
}
static struct sss_idp_oauth2 *
sss_idp_oauth2_decode(const char *str)
{
return sss_radius_message_decode(SSSD_IDP_OAUTH2_PREFIX,
(sss_radius_message_decode_fn)sss_idp_oauth2_from_json, str);
}
static char *
sss_idp_oauth2_encode(struct sss_idp_oauth2 *data)
{
return sss_radius_message_encode(SSSD_IDP_OAUTH2_PREFIX,
(sss_radius_message_encode_fn)sss_idp_oauth2_to_json, data);
}
krb5_pa_data *
sss_idp_oauth2_encode_padata(struct sss_idp_oauth2 *data)
{
return sss_radius_encode_padata(SSSD_IDP_OAUTH2_PADATA,
(sss_radius_message_encode_fn)sss_idp_oauth2_encode, data);
}
struct sss_idp_oauth2 *
sss_idp_oauth2_decode_padata(krb5_pa_data *padata)
{
return sss_radius_decode_padata(
(sss_radius_message_decode_fn)sss_idp_oauth2_decode, padata);
}
char *
sss_idp_oauth2_encode_challenge(struct sss_idp_oauth2 *data)
{
return sss_idp_oauth2_encode(data);
}
struct sss_idp_oauth2 *
sss_idp_oauth2_decode_challenge(const char *str)
{
return sss_idp_oauth2_decode(str);
}
|