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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
SSSD
System Database - certificate mapping rules related calls
Copyright (C) 2017 Sumit Bose <sbose@redhat.com>
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 "util/util.h"
#include "db/sysdb_private.h"
#include "lib/certmap/sss_certmap.h"
static errno_t sysdb_create_certmap_container(struct sysdb_ctx *sysdb,
bool user_name_hint)
{
struct ldb_message *msg = NULL;
errno_t ret;
msg = ldb_msg_new(sysdb);
if (msg == NULL) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(msg, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
if (msg->dn == NULL) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "cn", "certmap");
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
ret = ldb_msg_add_string(msg, SYSDB_CERTMAP_USER_NAME_HINT,
user_name_hint ? "TRUE" : "FALSE");
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* do a synchronous add */
ret = ldb_add(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_FATAL_FAILURE,
"Failed to add certmap container (%d, [%s])!\n",
ret, ldb_errstring(sysdb->ldb));
ret = EIO;
goto done;
}
ret = EOK;
done:
talloc_free(msg);
return ret;
}
static struct ldb_dn *sysdb_certmap_dn(TALLOC_CTX *mem_ctx,
struct sysdb_ctx *sysdb,
const char *name)
{
int ret;
char *clean_name;
struct ldb_dn *dn = NULL;
ret = sysdb_dn_sanitize(mem_ctx, name, &clean_name);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_dn_sanitize failed.\n");
return NULL;
}
dn = ldb_dn_new_fmt(mem_ctx, sysdb->ldb, SYSDB_TMPL_CERTMAP, clean_name);
talloc_free(clean_name);
if (dn == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new_fmt failed.\n");
return NULL;
}
return dn;
}
static errno_t sysdb_certmap_add(struct sysdb_ctx *sysdb,
struct certmap_info *certmap)
{
struct ldb_message *msg;
struct ldb_message_element *el;
int ret;
TALLOC_CTX *tmp_ctx;
size_t c;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed");
return ENOMEM;
}
msg = ldb_msg_new(tmp_ctx);
if (msg == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_new failed.\n");
ret = ENOMEM;
goto done;
}
msg->dn = sysdb_certmap_dn(tmp_ctx, sysdb, certmap->name);
if (msg->dn == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_certmap_dn failed.\n");
ret = ENOMEM;
goto done;
}
ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_CERTMAP_CLASS);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
goto done;
}
ret = sysdb_add_string(msg, SYSDB_NAME, certmap->name);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
goto done;
}
if (certmap->map_rule != NULL) {
ret = sysdb_add_string(msg, SYSDB_CERTMAP_MAPPING_RULE,
certmap->map_rule);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
goto done;
}
}
if (certmap->match_rule != NULL) {
ret = sysdb_add_string(msg, SYSDB_CERTMAP_MATCHING_RULE,
certmap->match_rule);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
goto done;
}
}
if (certmap->domains != NULL && certmap->domains[0] != NULL) {
for (c = 0; certmap->domains[c] != NULL; c++);
el = talloc_zero(tmp_ctx, struct ldb_message_element);
if (el == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_zero failed.\n");
ret = ENOMEM;
goto done;
}
el->name = talloc_strdup(el, SYSDB_CERTMAP_DOMAINS);
if(el->name == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ret = ENOMEM;
goto done;
}
el->num_values = c;
el->values = talloc_zero_array(el, struct ldb_val, c + 1);
if (el->values == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ret = ENOMEM;
goto done;
}
for (c = 0; certmap->domains[c] != NULL; c++) {
el->values[c].data = (uint8_t *) talloc_strdup(el->values,
certmap->domains[c]);
if (el->values[c].data == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ret = ENOMEM;
goto done;
}
el->values[c].length = strlen(certmap->domains[c]);
}
ret = ldb_msg_add(msg, el, LDB_FLAG_MOD_ADD);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add failed.\n");
ret = sysdb_error_to_errno(ret);
goto done;
}
}
ret = sysdb_add_ulong(msg, SYSDB_CERTMAP_PRIORITY,
(unsigned long)certmap->priority);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_ulong failed.\n");
goto done;
}
ret = ldb_add(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_add failed.\n");
ret = sysdb_error_to_errno(ret);
goto done;
}
ret = EOK;
done:
if (ret) {
DEBUG(SSSDBG_TRACE_FUNC, "Error: %d (%s)\n", ret, sss_strerror(ret));
}
talloc_zfree(tmp_ctx);
return ret;
}
errno_t sysdb_update_certmap(struct sysdb_ctx *sysdb,
struct certmap_info **certmaps,
bool user_name_hint)
{
size_t c;
struct ldb_dn *container_dn = NULL;
bool in_transaction = false;
int ret;
int sret;
if (certmaps == NULL) {
return EINVAL;
}
container_dn = ldb_dn_new(sysdb, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
if (container_dn == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new failed.\n");
return ENOMEM;
}
ret = sysdb_transaction_start(sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_transaction_start failed.\n");
goto done;
}
in_transaction = true;
ret = sysdb_delete_recursive(sysdb, container_dn, true);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_recursive failed.\n");
goto done;
}
ret = sysdb_create_certmap_container(sysdb, user_name_hint);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_create_certmap_container failed.\n");
goto done;
}
for (c = 0; certmaps[c] != NULL; c++) {
ret = sysdb_certmap_add(sysdb, certmaps[c]);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_certmap_add failed.\n");
goto done;
}
}
ret = sysdb_transaction_commit(sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "sysdb_transaction_commit failed.\n");
goto done;
}
in_transaction = false;
done:
if (in_transaction) {
sret = sysdb_transaction_cancel(sysdb);
if (sret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Could not cancel transaction.\n");
}
}
talloc_free(container_dn);
return ret;
}
enum certmap_info_member {
SSS_CMIM_NAME = 0,
SSS_CMIM_MAPPING_RULE,
SSS_CMIM_MATCHING_RULE,
SSS_CMIM_PRIORITY,
SSS_CMIM_DOMAINS,
SSS_CMIM_SENTINEL
};
errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
struct ldb_message *msg,
const char **attr_map,
struct certmap_info **certmap)
{
int ret;
size_t d;
size_t num_values;
struct certmap_info *map = NULL;
const char *tmp_str;
uint64_t tmp_uint;
struct ldb_message_element *tmp_el;
if (msg == NULL || attr_map == NULL || certmap == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid input.\n");
return EINVAL;
}
for (d = 0; d < SSS_CMIM_SENTINEL; d++) {
if (attr_map[d] == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid attribute map");
return EINVAL;
}
}
map = talloc_zero(mem_ctx, struct certmap_info);
if (map == NULL) {
return ENOMEM;
}
tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_NAME], NULL);
if (tmp_str == NULL) {
DEBUG(SSSDBG_MINOR_FAILURE, "The object [%s] doesn't have a name.\n",
ldb_dn_get_linearized(msg->dn));
ret = EINVAL;
goto done;
}
map->name = talloc_strdup(map, tmp_str);
if (map->name == NULL) {
ret = ENOMEM;
goto done;
}
tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_MAPPING_RULE],
NULL);
if (tmp_str != NULL) {
map->map_rule = talloc_strdup(map, tmp_str);
if (map->map_rule == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ret = ENOMEM;
goto done;
}
}
tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_MATCHING_RULE],
NULL);
if (tmp_str != NULL) {
map->match_rule = talloc_strdup(map, tmp_str);
if (map->match_rule == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ret = ENOMEM;
goto done;
}
}
tmp_uint = ldb_msg_find_attr_as_uint64(msg, attr_map[SSS_CMIM_PRIORITY],
(uint64_t) -1);
if (tmp_uint != (uint64_t) -1) {
if (tmp_uint > UINT32_MAX) {
DEBUG(SSSDBG_OP_FAILURE, "Priority value [%lu] too large.\n",
(unsigned long) tmp_uint);
ret = EINVAL;
goto done;
}
map->priority = (uint32_t) tmp_uint;
} else {
map->priority = SSS_CERTMAP_MIN_PRIO;
}
tmp_el = ldb_msg_find_element(msg, attr_map[SSS_CMIM_DOMAINS]);
if (tmp_el != NULL) {
num_values = tmp_el->num_values;
} else {
num_values = 0;
}
map->domains = talloc_zero_array(map, const char *, num_values + 1);
if (map->domains == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ret = ENOMEM;
goto done;
}
for (d = 0; d < num_values; d++) {
map->domains[d] = talloc_strndup(map->domains,
(char *) tmp_el->values[d].data,
tmp_el->values[d].length);
if (map->domains[d] == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_strndup failed.\n");
ret = ENOMEM;
goto done;
}
}
*certmap = map;
ret = EOK;
done:
if (ret != EOK) {
talloc_free(map);
}
return ret;
}
errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
struct certmap_info ***certmaps, bool *user_name_hint)
{
size_t c;
struct ldb_dn *container_dn = NULL;
int ret;
struct certmap_info **maps = NULL;
TALLOC_CTX *tmp_ctx = NULL;
struct ldb_result *res;
const char *attrs[] = {SYSDB_NAME,
SYSDB_CERTMAP_MAPPING_RULE,
SYSDB_CERTMAP_MATCHING_RULE,
SYSDB_CERTMAP_PRIORITY,
SYSDB_CERTMAP_DOMAINS,
NULL};
const char *config_attrs[] = {SYSDB_CERTMAP_USER_NAME_HINT,
NULL};
bool hint = false;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
ret = ENOMEM;
goto done;
}
container_dn = ldb_dn_new(tmp_ctx, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
if (container_dn == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new failed.\n");
ret = ENOMEM;
goto done;
}
ret = ldb_search(sysdb->ldb, tmp_ctx, &res, container_dn, LDB_SCOPE_BASE,
config_attrs, SYSDB_CERTMAP_USER_NAME_HINT"=*");
if (ret != LDB_SUCCESS || res->count != 1) {
DEBUG(SSSDBG_TRACE_INTERNAL,
"Failed to read certmap config, skipping.\n");
} else {
hint = ldb_msg_find_attr_as_bool(res->msgs[0],
SYSDB_CERTMAP_USER_NAME_HINT, false);
}
ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
container_dn, LDB_SCOPE_SUBTREE,
attrs, "objectclass=%s", SYSDB_CERTMAP_CLASS);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_search failed.\n");
ret = EIO;
goto done;
}
if (res->count == 0) {
DEBUG(SSSDBG_TRACE_FUNC, "No certificate maps found.\n");
ret = EOK;
goto done;
}
maps = talloc_zero_array(tmp_ctx, struct certmap_info *, res->count + 1);
if (maps == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ret = ENOMEM;
goto done;
}
for (c = 0; c < res->count; c++) {
ret = sysdb_ldb_msg_attr_to_certmap_info(maps, res->msgs[c], attrs,
&maps[c]);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"sysdb_ldb_msg_attr_to_certmap_info failed.\n");
goto done;
}
}
ret = EOK;
done:
if (ret == EOK) {
*certmaps = talloc_steal(mem_ctx, maps);
*user_name_hint = hint;
}
talloc_free(tmp_ctx);
return ret;
}
|