1 | /*
|
---|
2 | * Unix SMB/CIFS implementation.
|
---|
3 | * NetGroup testsuite
|
---|
4 | * Copyright (C) Guenther Deschner 2008
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <sys/types.h>
|
---|
21 | #include <inttypes.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #include <netapi.h>
|
---|
27 |
|
---|
28 | #include "common.h"
|
---|
29 |
|
---|
30 | static NET_API_STATUS test_netgroupenum(const char *hostname,
|
---|
31 | uint32_t level,
|
---|
32 | const char *groupname)
|
---|
33 | {
|
---|
34 | NET_API_STATUS status;
|
---|
35 | uint32_t entries_read = 0;
|
---|
36 | uint32_t total_entries = 0;
|
---|
37 | uint32_t resume_handle = 0;
|
---|
38 | int found_group = 0;
|
---|
39 | const char *current_name = NULL;
|
---|
40 | uint8_t *buffer = NULL;
|
---|
41 | int i;
|
---|
42 |
|
---|
43 | struct GROUP_INFO_0 *info0 = NULL;
|
---|
44 | struct GROUP_INFO_1 *info1 = NULL;
|
---|
45 | struct GROUP_INFO_2 *info2 = NULL;
|
---|
46 | struct GROUP_INFO_3 *info3 = NULL;
|
---|
47 |
|
---|
48 | printf("testing NetGroupEnum level %d\n", level);
|
---|
49 |
|
---|
50 | do {
|
---|
51 | status = NetGroupEnum(hostname,
|
---|
52 | level,
|
---|
53 | &buffer,
|
---|
54 | 120, //(uint32_t)-1,
|
---|
55 | &entries_read,
|
---|
56 | &total_entries,
|
---|
57 | &resume_handle);
|
---|
58 | if (status == 0 || status == ERROR_MORE_DATA) {
|
---|
59 | switch (level) {
|
---|
60 | case 0:
|
---|
61 | info0 = (struct GROUP_INFO_0 *)buffer;
|
---|
62 | break;
|
---|
63 | case 1:
|
---|
64 | info1 = (struct GROUP_INFO_1 *)buffer;
|
---|
65 | break;
|
---|
66 | case 2:
|
---|
67 | info2 = (struct GROUP_INFO_2 *)buffer;
|
---|
68 | break;
|
---|
69 | case 3:
|
---|
70 | info3 = (struct GROUP_INFO_3 *)buffer;
|
---|
71 | break;
|
---|
72 | default:
|
---|
73 | return -1;
|
---|
74 | }
|
---|
75 |
|
---|
76 | for (i=0; i<entries_read; i++) {
|
---|
77 |
|
---|
78 | switch (level) {
|
---|
79 | case 0:
|
---|
80 | current_name = info0->grpi0_name;
|
---|
81 | break;
|
---|
82 | case 1:
|
---|
83 | current_name = info1->grpi1_name;
|
---|
84 | break;
|
---|
85 | case 2:
|
---|
86 | current_name = info2->grpi2_name;
|
---|
87 | break;
|
---|
88 | case 3:
|
---|
89 | current_name = info3->grpi3_name;
|
---|
90 | break;
|
---|
91 | default:
|
---|
92 | break;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (strcasecmp(current_name, groupname) == 0) {
|
---|
96 | found_group = 1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | switch (level) {
|
---|
100 | case 0:
|
---|
101 | info0++;
|
---|
102 | break;
|
---|
103 | case 1:
|
---|
104 | info1++;
|
---|
105 | break;
|
---|
106 | case 2:
|
---|
107 | info2++;
|
---|
108 | break;
|
---|
109 | case 3:
|
---|
110 | info3++;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | NetApiBufferFree(buffer);
|
---|
115 | }
|
---|
116 | } while (status == ERROR_MORE_DATA);
|
---|
117 |
|
---|
118 | if (status) {
|
---|
119 | return status;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (!found_group) {
|
---|
123 | printf("failed to get group\n");
|
---|
124 | return -1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | NET_API_STATUS netapitest_group(struct libnetapi_ctx *ctx,
|
---|
131 | const char *hostname)
|
---|
132 | {
|
---|
133 | NET_API_STATUS status = 0;
|
---|
134 | const char *username, *groupname, *groupname2;
|
---|
135 | uint8_t *buffer = NULL;
|
---|
136 | struct GROUP_INFO_0 g0;
|
---|
137 | uint32_t parm_err = 0;
|
---|
138 | uint32_t levels[] = { 0, 1, 2, 3};
|
---|
139 | uint32_t enum_levels[] = { 0, 1, 2, 3};
|
---|
140 | int i;
|
---|
141 |
|
---|
142 | printf("NetGroup tests\n");
|
---|
143 |
|
---|
144 | username = "torture_test_user";
|
---|
145 | groupname = "torture_test_group";
|
---|
146 | groupname2 = "torture_test_group2";
|
---|
147 |
|
---|
148 | /* cleanup */
|
---|
149 | NetGroupDel(hostname, groupname);
|
---|
150 | NetGroupDel(hostname, groupname2);
|
---|
151 | NetUserDel(hostname, username);
|
---|
152 |
|
---|
153 | /* add a group */
|
---|
154 |
|
---|
155 | g0.grpi0_name = groupname;
|
---|
156 |
|
---|
157 | printf("testing NetGroupAdd\n");
|
---|
158 |
|
---|
159 | status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
|
---|
160 | if (status) {
|
---|
161 | NETAPI_STATUS(ctx, status, "NetGroupAdd");
|
---|
162 | goto out;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* 2nd add must fail */
|
---|
166 |
|
---|
167 | status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
|
---|
168 | if (status == 0) {
|
---|
169 | NETAPI_STATUS(ctx, status, "NetGroupAdd");
|
---|
170 | goto out;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* test enum */
|
---|
174 |
|
---|
175 | for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
|
---|
176 |
|
---|
177 | status = test_netgroupenum(hostname, enum_levels[i], groupname);
|
---|
178 | if (status) {
|
---|
179 | NETAPI_STATUS(ctx, status, "NetGroupEnum");
|
---|
180 | goto out;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* basic queries */
|
---|
185 |
|
---|
186 | for (i=0; i<ARRAY_SIZE(levels); i++) {
|
---|
187 |
|
---|
188 | printf("testing NetGroupGetInfo level %d\n", levels[i]);
|
---|
189 |
|
---|
190 | status = NetGroupGetInfo(hostname, groupname, levels[i], &buffer);
|
---|
191 | if (status && status != 124) {
|
---|
192 | NETAPI_STATUS(ctx, status, "NetGroupGetInfo");
|
---|
193 | goto out;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* group rename */
|
---|
198 |
|
---|
199 | g0.grpi0_name = groupname2;
|
---|
200 |
|
---|
201 | printf("testing NetGroupSetInfo level 0\n");
|
---|
202 |
|
---|
203 | status = NetGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err);
|
---|
204 | if (status) {
|
---|
205 | NETAPI_STATUS(ctx, status, "NetGroupSetInfo");
|
---|
206 | goto out;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /* should not exist anymore */
|
---|
210 |
|
---|
211 | status = NetGroupDel(hostname, groupname);
|
---|
212 | if (status == 0) {
|
---|
213 | NETAPI_STATUS(ctx, status, "NetGroupDel");
|
---|
214 | goto out;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* query info */
|
---|
218 |
|
---|
219 | for (i=0; i<ARRAY_SIZE(levels); i++) {
|
---|
220 |
|
---|
221 | status = NetGroupGetInfo(hostname, groupname2, levels[i], &buffer);
|
---|
222 | if (status && status != 124) {
|
---|
223 | NETAPI_STATUS(ctx, status, "NetGroupGetInfo");
|
---|
224 | goto out;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* add user to group */
|
---|
229 |
|
---|
230 | status = test_netuseradd(hostname, username);
|
---|
231 | if (status) {
|
---|
232 | NETAPI_STATUS(ctx, status, "NetUserAdd");
|
---|
233 | goto out;
|
---|
234 | }
|
---|
235 |
|
---|
236 | printf("testing NetGroupAddUser\n");
|
---|
237 |
|
---|
238 | status = NetGroupAddUser(hostname, groupname2, username);
|
---|
239 | if (status) {
|
---|
240 | NETAPI_STATUS(ctx, status, "NetGroupAddUser");
|
---|
241 | goto out;
|
---|
242 | }
|
---|
243 |
|
---|
244 | printf("testing NetGroupDelUser\n");
|
---|
245 |
|
---|
246 | status = NetGroupDelUser(hostname, groupname2, username);
|
---|
247 | if (status) {
|
---|
248 | NETAPI_STATUS(ctx, status, "NetGroupDelUser");
|
---|
249 | goto out;
|
---|
250 | }
|
---|
251 |
|
---|
252 | status = NetUserDel(hostname, username);
|
---|
253 | if (status) {
|
---|
254 | NETAPI_STATUS(ctx, status, "NetUserDel");
|
---|
255 | goto out;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /* delete */
|
---|
259 |
|
---|
260 | printf("testing NetGroupDel\n");
|
---|
261 |
|
---|
262 | status = NetGroupDel(hostname, groupname2);
|
---|
263 | if (status) {
|
---|
264 | NETAPI_STATUS(ctx, status, "NetGroupDel");
|
---|
265 | goto out;
|
---|
266 | };
|
---|
267 |
|
---|
268 | /* should not exist anymore */
|
---|
269 |
|
---|
270 | status = NetGroupGetInfo(hostname, groupname2, 0, &buffer);
|
---|
271 | if (status == 0) {
|
---|
272 | NETAPI_STATUS_MSG(ctx, status, "NetGroupGetInfo", "expected failure and error code");
|
---|
273 | goto out;
|
---|
274 | };
|
---|
275 |
|
---|
276 | status = 0;
|
---|
277 |
|
---|
278 | printf("NetGroup tests succeeded\n");
|
---|
279 | out:
|
---|
280 | if (status != 0) {
|
---|
281 | printf("NetGroup testsuite failed with: %s\n",
|
---|
282 | libnetapi_get_error_string(ctx, status));
|
---|
283 | }
|
---|
284 |
|
---|
285 | return status;
|
---|
286 | }
|
---|