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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
SSSD
Simple access module -- Tests
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2010 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 <stdlib.h>
#include <popt.h>
#include <check.h>
#include "confdb/confdb.h"
#include "providers/simple/simple_access.h"
#include "tests/common.h"
#define TESTS_PATH "tests_simple_access"
#define TEST_CONF_FILE "tests_conf.ldb"
const char *ulist_1[] = {"u1", "u2", NULL};
const char *glist_1[] = {"g1", "g2", NULL};
const char *glist_1_case[] = {"G1", "G2", NULL};
struct simple_test_ctx *test_ctx = NULL;
struct simple_test_ctx {
struct sysdb_ctx *sysdb;
struct confdb_ctx *confdb;
struct tevent_context *ev;
bool done;
int error;
bool access_granted;
struct simple_ctx *ctx;
};
static int test_loop(struct simple_test_ctx *tctx)
{
while (!tctx->done)
tevent_loop_once(tctx->ev);
return tctx->error;
}
static void simple_access_check_done(struct tevent_req *req)
{
struct simple_test_ctx *tctx =
tevent_req_callback_data(req, struct simple_test_ctx);
tctx->error = simple_access_check_recv(req, &tctx->access_granted);
talloc_free(req);
tctx->done = true;
}
void setup_simple(void)
{
errno_t ret;
char *conf_db;
const char *val[2];
val[1] = NULL;
fail_unless(test_ctx == NULL, "Simple context already initialized.");
test_ctx = talloc_zero(NULL, struct simple_test_ctx);
fail_unless(test_ctx != NULL, "Cannot create simple test context.");
test_ctx->ev = tevent_context_init(test_ctx);
fail_unless(test_ctx->ev != NULL, "Cannot create tevent context.");
test_ctx->ctx = talloc_zero(test_ctx, struct simple_ctx);
fail_unless(test_ctx->ctx != NULL, "Cannot create simple context.");
/* Create tests directory if it doesn't exist */
/* (relative to current dir) */
ret = mkdir(TESTS_PATH, 0775);
fail_if(ret == -1 && errno != EEXIST,
"Could not create %s directory", TESTS_PATH);
conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
fail_if(conf_db == NULL, "Out of memory, aborting!");
DEBUG(SSSDBG_TRACE_LIBS, ("CONFDB: %s\n", conf_db));
/* Connect to the conf db */
ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
fail_if(ret != EOK, "Could not initialize connection to the confdb");
val[0] = "LOCAL";
ret = confdb_add_param(test_ctx->confdb, true,
"config/sssd", "domains", val);
fail_if(ret != EOK, "Could not initialize domains placeholder");
val[0] = "local";
ret = confdb_add_param(test_ctx->confdb, true,
"config/domain/LOCAL", "id_provider", val);
fail_if(ret != EOK, "Could not initialize provider");
val[0] = "TRUE";
ret = confdb_add_param(test_ctx->confdb, true,
"config/domain/LOCAL", "enumerate", val);
fail_if(ret != EOK, "Could not initialize LOCAL domain");
val[0] = "TRUE";
ret = confdb_add_param(test_ctx->confdb, true,
"config/domain/LOCAL", "cache_credentials", val);
fail_if(ret != EOK, "Could not initialize LOCAL domain");
ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
TESTS_PATH, &test_ctx->ctx->domain);
fail_if(ret != EOK, "Could not initialize connection to the sysdb (%d)", ret);
test_ctx->sysdb = test_ctx->ctx->domain->sysdb;
test_ctx->ctx->domain->case_sensitive = true;
test_ctx->ctx->domain->mpg = false; /* Simulate an LDAP domain better */
}
void teardown_simple(void)
{
int ret;
fail_unless(test_ctx != NULL, "Simple context already freed.");
ret = talloc_free(test_ctx);
test_ctx = NULL;
fail_unless(ret == 0, "Connot free simple context.");
}
void setup_simple_group(void)
{
errno_t ret;
setup_simple();
/* Add test users u1 and u2 that would be members of test groups
* g1 and g2 respectively */
ret = sysdb_add_group(test_ctx->sysdb, test_ctx->ctx->domain,
"pvt", 999, NULL, 0, 0);
fail_if(ret != EOK, "Could not add private group");
ret = sysdb_store_user(test_ctx->sysdb, test_ctx->ctx->domain,
"u1", NULL, 123, 999, "u1", "/home/u1",
"/bin/bash", NULL, NULL, NULL, -1, 0);
fail_if(ret != EOK, "Could not add u1");
ret = sysdb_store_user(test_ctx->sysdb, test_ctx->ctx->domain,
"u2", NULL, 456, 999, "u1", "/home/u1",
"/bin/bash", NULL, NULL, NULL, -1, 0);
fail_if(ret != EOK, "Could not add u2");
ret = sysdb_store_user(test_ctx->sysdb, test_ctx->ctx->domain,
"u3", NULL, 789, 999, "u1", "/home/u1",
"/bin/bash", NULL, NULL, NULL, -1, 0);
fail_if(ret != EOK, "Could not add u3");
ret = sysdb_add_group(test_ctx->sysdb, test_ctx->ctx->domain,
"g1", 321, NULL, 0, 0);
fail_if(ret != EOK, "Could not add g1");
ret = sysdb_add_group(test_ctx->sysdb, test_ctx->ctx->domain,
"g2", 654, NULL, 0, 0);
fail_if(ret != EOK, "Could not add g2");
ret = sysdb_add_group_member(test_ctx->sysdb, test_ctx->ctx->domain,
"g1", "u1", SYSDB_MEMBER_USER);
fail_if(ret != EOK, "Could not add u1 to g1");
ret = sysdb_add_group_member(test_ctx->sysdb, test_ctx->ctx->domain,
"g2", "u2", SYSDB_MEMBER_USER);
fail_if(ret != EOK, "Could not add u2 to g2");
}
void teardown_simple_group(void)
{
errno_t ret;
ret = sysdb_delete_user(test_ctx->sysdb, test_ctx->ctx->domain, "u1", 0);
fail_if(ret != EOK, "Could not delete u1");
ret = sysdb_delete_user(test_ctx->sysdb, test_ctx->ctx->domain, "u2", 0);
fail_if(ret != EOK, "Could not delete u2");
ret = sysdb_delete_user(test_ctx->sysdb, test_ctx->ctx->domain, "u3", 0);
fail_if(ret != EOK, "Could not delete u3");
ret = sysdb_delete_group(test_ctx->sysdb, test_ctx->ctx->domain, "g1", 0);
fail_if(ret != EOK, "Could not delete g1");
ret = sysdb_delete_group(test_ctx->sysdb, test_ctx->ctx->domain, "g2", 0);
fail_if(ret != EOK, "Could not delete g2");
ret = sysdb_delete_group(test_ctx->sysdb, test_ctx->ctx->domain, "pvt", 0);
fail_if(ret != EOK, "Could not delete pvt");
teardown_simple();
}
START_TEST(test_both_empty)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = NULL;
test_ctx->ctx->deny_users = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied while both lists are empty.");
}
END_TEST
START_TEST(test_allow_empty)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = NULL;
test_ctx->ctx->deny_users = discard_const(ulist_1);
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is in deny list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied while user is not in deny list.");
}
END_TEST
START_TEST(test_deny_empty)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = discard_const(ulist_1);
test_ctx->ctx->deny_users = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied while user is in allow list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is not in allow list.");
}
END_TEST
START_TEST(test_both_set)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = discard_const(ulist_1);
test_ctx->ctx->deny_users = discard_const(ulist_1);
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is in deny list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is not in allow list.");
}
END_TEST
START_TEST(test_case)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = discard_const(ulist_1);
test_ctx->ctx->deny_users = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "U1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted for user with different case "
"in case-sensitive domain");
test_ctx->ctx->domain->case_sensitive = false;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "U1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied for user with different case "
"in case-sensitive domain");
}
END_TEST
START_TEST(test_unknown_user)
{
struct tevent_req *req;
test_ctx->ctx->allow_users = discard_const(ulist_1);
test_ctx->ctx->deny_users = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "foo");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted for user not present in domain");
}
END_TEST
START_TEST(test_group_allow_empty)
{
struct tevent_req *req;
test_ctx->ctx->allow_groups = NULL;
test_ctx->ctx->deny_groups = discard_const(glist_1);
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while group is in deny list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied while group is not in deny list.");
}
END_TEST
START_TEST(test_group_deny_empty)
{
struct tevent_req *req;
test_ctx->ctx->allow_groups = discard_const(glist_1);
test_ctx->ctx->deny_groups = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied while user is in allow list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is not in allow list.");
}
END_TEST
START_TEST(test_group_both_set)
{
struct tevent_req *req;
test_ctx->ctx->allow_groups = discard_const(ulist_1);
test_ctx->ctx->deny_groups = discard_const(ulist_1);
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is in deny list.");
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "u3");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted while user is not in allow list.");
}
END_TEST
START_TEST(test_group_case)
{
struct tevent_req *req;
test_ctx->ctx->allow_groups = discard_const(glist_1_case);
test_ctx->ctx->deny_groups = NULL;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "U1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == false,
"Access granted for user with different case "
"in case-sensitive domain");
test_ctx->ctx->domain->case_sensitive = false;
req = simple_access_check_send(test_ctx, test_ctx->ev,
test_ctx->ctx, "U1");
fail_unless(test_ctx != NULL, "Cannot create request\n");
tevent_req_set_callback(req, simple_access_check_done, test_ctx);
test_loop(test_ctx);
test_ctx->done = false;
fail_unless(test_ctx->error == EOK, "access_simple_check failed.");
fail_unless(test_ctx->access_granted == true,
"Access denied for user with different case "
"in case-sensitive domain");
}
END_TEST
Suite *access_simple_suite (void)
{
Suite *s = suite_create("access_simple");
TCase *tc_allow_deny = tcase_create("user allow/deny");
tcase_add_checked_fixture(tc_allow_deny, setup_simple, teardown_simple);
tcase_add_test(tc_allow_deny, test_both_empty);
tcase_add_test(tc_allow_deny, test_allow_empty);
tcase_add_test(tc_allow_deny, test_deny_empty);
tcase_add_test(tc_allow_deny, test_both_set);
tcase_add_test(tc_allow_deny, test_case);
tcase_add_test(tc_allow_deny, test_unknown_user);
suite_add_tcase(s, tc_allow_deny);
TCase *tc_grp_allow_deny = tcase_create("group allow/deny");
tcase_add_checked_fixture(tc_grp_allow_deny,
setup_simple_group, teardown_simple_group);
tcase_add_test(tc_grp_allow_deny, test_group_allow_empty);
tcase_add_test(tc_grp_allow_deny, test_group_deny_empty);
tcase_add_test(tc_grp_allow_deny, test_group_both_set);
tcase_add_test(tc_grp_allow_deny, test_group_case);
suite_add_tcase(s, tc_grp_allow_deny);
return s;
}
int main(int argc, const char *argv[])
{
int opt;
poptContext pc;
int number_failed;
int ret;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_MAIN_OPTS
POPT_TABLEEND
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
debug_level = SSSDBG_INVALID;
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
while((opt = poptGetNextOpt(pc)) != -1) {
switch(opt) {
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 1;
}
}
poptFreeContext(pc);
DEBUG_INIT(debug_level);
tests_set_cwd();
Suite *s = access_simple_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
ret = unlink(TESTS_PATH"/"TEST_CONF_FILE);
if (ret != EOK) {
fprintf(stderr, "Could not delete the test config ldb file (%d) (%s)\n",
errno, strerror(errno));
return EXIT_FAILURE;
}
ret = unlink(TESTS_PATH"/"LOCAL_SYSDB_FILE);
if (ret != EOK) {
fprintf(stderr, "Could not delete the test config ldb file (%d) (%s)\n",
errno, strerror(errno));
return EXIT_FAILURE;
}
return (number_failed==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
|