summaryrefslogtreecommitdiffstats
path: root/testsuite/smbd/se_access_check_printer.c
blob: e2645c68549460286dcdf9f933e743d26ba84f2f (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
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
/* 
   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 "se_access_check_utils.h"

/* Globals */

BOOL failed;
SEC_DESC *sd;

struct ace_entry acl_printer[] = {

	/* Everyone is allowed to print */

	{ SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_PRINT, "S-1-1-0" },

	/* Except for user0 who uses too much paper */

	{ SEC_ACE_TYPE_ACCESS_DENIED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_FULL_CONTROL, "user0" },

	/* Users 1 and 2 can manage documents */

	{ SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_MANAGE_DOCUMENTS, "user1" },

	{ SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_MANAGE_DOCUMENTS, "user2" },

	/* Domain Admins can also manage documents */

	{ SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_MANAGE_DOCUMENTS, "Domain Admins" },

	/* User 3 is da man */

	{ SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
	  PRINTER_ACE_FULL_CONTROL, "user3" },

	{ 0, 0, 0, NULL}
};

BOOL test_user(char *username, uint32 acc_desired, uint32 *acc_granted)
{
	struct passwd *pw;
	uint32 status;

	if (!(pw = getpwnam(username))) {
		printf("FAIL: could not lookup user info for %s\n",
		       username);
		exit(1);
	}

	return se_access_check(sd, pw->pw_uid, pw->pw_gid, 0, NULL,
			       acc_desired, acc_granted, &status);
}

static char *pace_str(uint32 ace_flags)
{
	if ((ace_flags & PRINTER_ACE_FULL_CONTROL) == 
	    PRINTER_ACE_FULL_CONTROL) return "full control";

	if ((ace_flags & PRINTER_ACE_MANAGE_DOCUMENTS) ==
	    PRINTER_ACE_MANAGE_DOCUMENTS) return "manage documents";

	if ((ace_flags & PRINTER_ACE_PRINT) == PRINTER_ACE_PRINT)
		return "print";

	return "UNKNOWN";
}

uint32 perms[] = {
	PRINTER_ACE_PRINT,
	PRINTER_ACE_FULL_CONTROL,
	PRINTER_ACE_MANAGE_DOCUMENTS,
	0
};

void runtest(void)
{
	uint32 acc_granted;
	BOOL result;
	int i, j;

	for (i = 0; perms[i]; i++) {

		/* Test 10 users */
		
		for (j = 0; j < 10; j++) {
			fstring name;

			/* Test user against ACL */

			snprintf(name, sizeof(fstring), "%s/user%d", 
				 getenv("TEST_WORKGROUP"), j);
			
			result = test_user(name, perms[i], &acc_granted);

			printf("%s: %s %s 0x%08x\n", name, 
			       pace_str(perms[i]), 
			       result ? "TRUE " : "FALSE", acc_granted);

			/* Check results */

			switch (perms[i]) {

			case PRINTER_ACE_PRINT: {
				if (!result || acc_granted !=
				    PRINTER_ACE_PRINT) {
					printf("FAIL: user %s can't print\n",
					       name);
					failed = True;
				}
				break;
			}

			case PRINTER_ACE_FULL_CONTROL: {
				if (j == 3) {
					if (!result || acc_granted !=
					    PRINTER_ACE_FULL_CONTROL) {
						printf("FAIL: user %s doesn't "
						       "have full control\n",
						       name);
						failed = True;
					}
				} else {
					if (result || acc_granted != 0) {
						printf("FAIL: user %s has full "
						       "control\n", name);
						failed = True;
					}
				}
				break;
			}
			case PRINTER_ACE_MANAGE_DOCUMENTS: {
				if (j == 1 || j == 2) {
					if (!result || acc_granted !=
					    PRINTER_ACE_MANAGE_DOCUMENTS) {
						printf("FAIL: user %s can't "
						       "manage documents\n",
						       name);
						failed = True;
					}
				} else {
					if (result || acc_granted != 0) {
						printf("FAIL: user %s can "
						       "manage documents\n",
						       name);
						failed = True;
					}
				}
				break;
			}

			default:
				printf("FAIL: internal error\n");
				exit(1);
			}
		}
	}
}

/* Main function */

int main(int argc, char **argv)
{
	/* Initialisation */

	generate_wellknown_sids();

	/* Create security descriptor */

	sd = build_sec_desc(acl_printer, NULL, NULL_SID, NULL_SID);

	if (!sd) {
		printf("FAIL: could not build security descriptor\n");
		return 1;
	}

	/* Run test */

	runtest();

	/* Return */

        if (!failed) {
		printf("PASS\n");
		return 0;
	} 

	return 1;
}