summaryrefslogtreecommitdiffstats
path: root/testsuite/smbd/sec_ctx_groups.c
blob: 63109634278351f7368cdaf5159d61bf90b856c9 (plain)
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
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Security context tests
   Copyright (C) Tim Potter 2000
   
   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 "includes.h"
#include "sec_ctx_utils.h"

int main (int argc, char **argv)
{
	int ngroups, initial_ngroups, check_ngroups, final_ngroups;
	gid_t *groups, *initial_groups, *check_groups, *final_groups;
	int i;

	init_sec_ctx();

	/* Save current groups */

	initial_ngroups = sys_getgroups(0, NULL);
	initial_groups = malloc(sizeof(gid_t) * initial_ngroups);
	sys_getgroups(initial_ngroups, initial_groups);

	printf("Initial groups are: ");
	for (i = 0; i < initial_ngroups; i++) {
		printf("%d, ", initial_groups[i]);
	}
	printf("\n");

	/* Push a context plus groups */

	get_random_grouplist(&ngroups, &groups);

	printf("Random groups are: ");
	for (i = 0; i < ngroups; i++) {
		printf("%d, ", groups[i]);
	}
	printf("\n");

	if (!push_sec_ctx()) {
		printf("FAIL: push_sec_ctx\n");
		return 1;
	}

	set_sec_ctx(1, 2, ngroups, groups);

	/* Check grouplist stuck */

	check_ngroups = sys_getgroups(0, NULL);
	check_groups = malloc(sizeof(gid_t) * check_ngroups);
	sys_getgroups(check_ngroups, check_groups);

	printf("Actual groups are: ");
	for (i = 0; i < check_ngroups; i++) {
		printf("%d, ", check_groups[i]);
	}
	printf("\n");

	if (ngroups != check_ngroups) {
		printf("FAIL: number of groups differs\n");
		return 1;
	}

	for (i = 0; i < ngroups; i++) {
		if (groups[i] != check_groups[i]) {
			printf("FAIL: group %d differs\n", i);
			return 1;
		}
	}

	safe_free(groups);
	safe_free(check_groups);

	/* Pop and check initial groups are back */

	if (!pop_sec_ctx()) {
		printf("FAIL: pop_sec_ctx\n");
		return 1;
	}

	final_ngroups = sys_getgroups(0, NULL);
	final_groups = malloc(sizeof(gid_t) * final_ngroups);
	sys_getgroups(final_ngroups, final_groups);
	
	printf("Final groups are: ");
	for (i = 0; i < final_ngroups; i++) {
		printf("%d, ", final_groups[i]);
	}
	printf("\n");

	if (initial_ngroups != final_ngroups) {
		printf("FAIL: final number of groups differ\n");
		return 1;
	}

	for (i = 0; i < initial_ngroups; i++) {
		if (initial_groups[i] != final_groups[i]) {
			printf("FAIL: final group %d differs\n", i);
			return 1;
		}
	}

	printf("Final groups are: ");
	for (i = 0; i < final_ngroups; i++) {
		printf("%d, ", final_groups[i]);
	}
	printf("\n");

	safe_free(initial_groups);
	safe_free(final_groups);

	/* Everything's cool */

	printf("PASS\n");
	return 0;
}