Debian Codegraph @

source4/torture/unix/whoami.c

raw
1 /*
2 Test the SMB_WHOAMI Unix extension.
3
4 Copyright (C) 2007 James Peach
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 "includes.h"
21 #include "libcli/libcli.h"
22 #include "libcli/raw/raw_proto.h"
23 #include "torture/torture.h"
24 #include "torture/unix/proto.h"
25 #include "lib/cmdline/cmdline.h"
26 #include "auth/credentials/credentials.h"
27 #include "param/param.h"
28 #include "libcli/resolve/resolve.h"
29 #include <ldb.h>
30 #include "lib/util/util_ldb.h"
31 #include "ldb_wrap.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "../libcli/security/security.h"
34
35 #undef strcasecmp
36
37 /* Size (in bytes) of the required fields in the SMBwhoami response. */
38 #define WHOAMI_REQUIRED_SIZE 40
39
40 /*
41 SMBWhoami - Query the user mapping performed by the server for the
42 connected tree. This is a subcommand of the TRANS2_QFSINFO.
43
44 Returns:
45 4 bytes unsigned - mapping flags (smb_whoami_flags)
46 4 bytes unsigned - flags mask
47
48 8 bytes unsigned - primary UID
49 8 bytes unsigned - primary GID
50 4 bytes unsigned - number of supplementary GIDs
51 4 bytes unsigned - number of SIDs
52 4 bytes unsigned - SID list byte count
53 4 bytes - pad / reserved (must be zero)
54
55 8 bytes unsigned[] - list of GIDs (may be empty)
56 struct dom_sid[] - list of SIDs (may be empty)
57 */
58
59 struct smb_whoami
60 {
61 uint32_t mapping_flags;
62 uint32_t mapping_mask;
63 uint64_t server_uid;
64 uint64_t server_gid;
65 uint32_t num_gids;
66 uint32_t num_sids;
67 uint32_t num_sid_bytes;
68 uint32_t reserved; /* Must be zero */
69 uint64_t * gid_list;
70 struct dom_sid ** sid_list;
71 };
72
73 static struct smbcli_state *connect_to_server(struct torture_context *tctx,
74 struct cli_credentials *creds)
75 {
76 NTSTATUS status;
77 struct smbcli_state *cli;
78
79 const char *host = torture_setting_string(tctx, "host", NULL);
80 const char *share = torture_setting_string(tctx, "share", NULL);
81 struct smbcli_options options;
82 struct smbcli_session_options session_options;
83
84 lpcfg_smbcli_options(tctx->lp_ctx, &options);
85 lpcfg_smbcli_session_options(tctx->lp_ctx, &session_options);
86
87 status = smbcli_full_connection(tctx, &cli, host,
88 share, NULL, lpcfg_socket_options(tctx->lp_ctx),
89 creds,
90 tctx->lp_ctx,
91 lpcfg_resolve_context(tctx->lp_ctx),
92 tctx->ev, &options, &session_options,
93 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
94
95 if (!NT_STATUS_IS_OK(status)) {
96 torture_comment(tctx,
97 "FATAL: Failed to connect to //%s/%s "
98 "with %s - %s\n",
99 host,
100 share,
101 cli_credentials_get_username(creds),
102 nt_errstr(status));
103 return NULL;
104 }
105
106 return cli;
107 }
108
109 static bool whoami_sid_parse(void *mem_ctx,
110 struct torture_context *torture,
111 DATA_BLOB *data, size_t *offset,
112 struct dom_sid **psid)
113 {
114 size_t remain = data->length - *offset;
115 int i;
116
117 *psid = talloc_zero(mem_ctx, struct dom_sid);
118 torture_assert(torture, *psid != NULL, "out of memory");
119
120 torture_assert(torture, remain >= 8,
121 "invalid SID format");
122
123 (*psid)->sid_rev_num = CVAL(data->data, *offset);
124 (*psid)->num_auths = CVAL(data->data, *offset + 1);
125 memcpy((*psid)->id_auth, data->data + *offset + 2, 6);
126
127 (*offset) += 8;
128 remain = data->length - *offset;
129
130 torture_assert(torture, remain >= ((*psid)->num_auths * 4),
131 "invalid sub_auth byte count");
132 torture_assert(torture, (*psid)->num_auths >= 0,
133 "invalid sub_auth value");
134 torture_assert(torture, (*psid)->num_auths <= 15,
135 "invalid sub_auth value");
136
137 for (i = 0; i < (*psid)->num_auths; i++) {
138 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
139 (*offset) += 4;
140 }
141
142 return true;
143 }
144
145 static bool smb_raw_query_posix_whoami(void *mem_ctx,
146 struct torture_context *torture,
147 struct smbcli_state *cli,
148 struct smb_whoami *whoami,
149 unsigned max_data)
150 {
151 struct smb_trans2 tp;
152 NTSTATUS status;
153 size_t offset;
154 int i;
155
156 uint16_t setup = TRANSACT2_QFSINFO;
157 uint16_t info_level;
158
159 ZERO_STRUCTP(whoami);
160
161 tp.in.max_setup = 0;
162 tp.in.flags = 0;
163 tp.in.timeout = 0;
164 tp.in.setup_count = 1;
165 tp.in.max_param = 10;
166 tp.in.max_data = (uint16_t)max_data;
167 tp.in.setup = &setup;
168 tp.in.trans_name = NULL;
169 SSVAL(&info_level, 0, SMB_QFS_POSIX_WHOAMI);
170 tp.in.params = data_blob_talloc(mem_ctx, &info_level, 2);
171 tp.in.data = data_blob_talloc(mem_ctx, NULL, 0);
172
173 status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
174 torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
175 "doing SMB_QFS_POSIX_WHOAMI");
176
177 /* Make sure we got back all the required fields. */
178 torture_assert(torture, tp.out.params.length == 0,
179 "trans2 params should be empty");
180 torture_assert(torture, tp.out.data.length >= WHOAMI_REQUIRED_SIZE,
181 "checking for required response fields");
182
183 whoami->mapping_flags = IVAL(tp.out.data.data, 0);
184 whoami->mapping_mask = IVAL(tp.out.data.data, 4);
185 whoami->server_uid = BVAL(tp.out.data.data, 8);
186 whoami->server_gid = BVAL(tp.out.data.data, 16);
187 whoami->num_gids = IVAL(tp.out.data.data, 24);
188 whoami->num_sids = IVAL(tp.out.data.data, 28);
189 whoami->num_sid_bytes = IVAL(tp.out.data.data, 32);
190 whoami->reserved = IVAL(tp.out.data.data, 36);
191
192 /* The GID list and SID list are optional, depending on the count
193 * and length fields.
194 */
195 if (whoami->num_sids != 0) {
196 torture_assert(torture, whoami->num_sid_bytes != 0,
197 "SID count does not match byte count");
198 }
199
200 printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
201 whoami->mapping_flags, whoami->mapping_mask);
202 printf("\tserver UID=%llu GID=%llu\n",
203 (unsigned long long)whoami->server_uid, (unsigned long long)whoami->server_gid);
204 printf("\t%u GIDs, %u SIDs, %u SID bytes\n",
205 whoami->num_gids, whoami->num_sids,
206 whoami->num_sid_bytes);
207
208 offset = WHOAMI_REQUIRED_SIZE;
209
210 torture_assert_int_equal(torture, whoami->reserved, 0,
211 "invalid reserved field");
212
213 if (tp.out.data.length == offset) {
214 /* No SIDs or GIDs returned */
215 torture_assert_int_equal(torture, whoami->num_gids, 0,
216 "invalid GID count");
217 torture_assert_int_equal(torture, whoami->num_sids, 0,
218 "invalid SID count");
219 torture_assert_int_equal(torture, whoami->num_sid_bytes, 0,
220 "invalid SID byte count");
221 return true;
222 }
223
224 if (whoami->num_gids != 0) {
225 int remain = tp.out.data.length - offset;
226 int gid_bytes = whoami->num_gids * 8;
227
228 if (whoami->num_sids == 0) {
229 torture_assert_int_equal(torture, remain, gid_bytes,
230 "GID count does not match data length");
231 } else {
232 torture_assert(torture, remain > gid_bytes,
233 "invalid GID count");
234 }
235
236 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
237 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
238
239 torture_comment(torture, "\tGIDs:\n");
240
241 for (i = 0; i < whoami->num_gids; ++i) {
242 whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
243 offset += 8;
244 torture_comment(torture, "\t\t%u\n", (unsigned int)whoami->gid_list[i]);
245 }
246 }
247
248 /* Check if there should be data left for the SID list. */
249 if (tp.out.data.length == offset) {
250 torture_assert_int_equal(torture, whoami->num_sids, 0,
251 "invalid SID count");
252 return true;
253 }
254
255 /* All the remaining bytes must be the SID list. */
256 torture_assert_int_equal(torture,
257 whoami->num_sid_bytes, (tp.out.data.length - offset),
258 "invalid SID byte count");
259
260 if (whoami->num_sids != 0) {
261
262 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
263 whoami->num_sids);
264 torture_assert(torture, whoami->sid_list != NULL,
265 "out of memory");
266
267 torture_comment(torture, "\tSIDs:\n");
268
269 for (i = 0; i < whoami->num_sids; ++i) {
270 if (!whoami_sid_parse(mem_ctx, torture,
271 &tp.out.data, &offset,
272 &whoami->sid_list[i])) {
273 return false;
274 }
275
276 torture_comment(torture, "\t\t%s\n",
277 dom_sid_string(torture, whoami->sid_list[i]));
278 }
279 }
280
281 /* We should be at the end of the response now. */
282 torture_assert_int_equal(torture, tp.out.data.length, offset,
283 "trailing garbage bytes");
284
285 return true;
286 }
287
288 static bool test_against_ldap(struct torture_context *torture, struct ldb_context *ldb, bool is_dc,
289 struct smb_whoami *whoami)
290 {
291 struct ldb_message *msg;
292 struct ldb_message_element *el;
293
294 const char *attrs[] = { "tokenGroups", NULL };
295 int i;
296
297 torture_assert_int_equal(torture, dsdb_search_one(ldb, torture, &msg, NULL, LDB_SCOPE_BASE, attrs, 0, NULL), LDB_SUCCESS, "searching for tokenGroups");
298 el = ldb_msg_find_element(msg, "tokenGroups");
299 torture_assert(torture, el, "obtaining tokenGroups");
300 torture_assert(torture, el->num_values > 0, "Number of SIDs from LDAP needs to be more than 0");
301 torture_assert(torture, whoami->num_sids > 0, "Number of SIDs from LDAP needs to be more than 0");
302
303 if (is_dc) {
304 torture_assert_int_equal(torture, el->num_values, whoami->num_sids, "Number of SIDs from LDAP and number of SIDs from CIFS does not match!");
305
306 for (i = 0; i < el->num_values; i++) {
307 struct dom_sid *sid = talloc(torture, struct dom_sid);
308 ssize_t ret;
309 torture_assert(torture, sid != NULL, "talloc failed");
310
311 ret = sid_parse(el->values[i].data,
312 el->values[i].length, sid);
313 torture_assert(torture,
314 ret != -1,
315 "sid parse failed");
316 torture_assert_str_equal(torture, dom_sid_string(sid, sid), dom_sid_string(sid, whoami->sid_list[i]), "SID from LDAP and SID from CIFS does not match!");
317 talloc_free(sid);
318 }
319 } else {
320 unsigned int num_domain_sids_dc = 0, num_domain_sids_member = 0;
321 struct dom_sid *user_sid = talloc(torture, struct dom_sid);
322 struct dom_sid *dom_sid = talloc(torture, struct dom_sid);
323 struct dom_sid *dc_sids = talloc_array(torture, struct dom_sid, el->num_values);
324 struct dom_sid *member_sids = talloc_array(torture, struct dom_sid, whoami->num_sids);
325 ssize_t ret;
326 torture_assert(torture, user_sid != NULL, "talloc failed");
327 ret = sid_parse(el->values[0].data,
328 el->values[0].length,
329 user_sid);
330 torture_assert(torture,
331 ret != -1,
332 "sid parse failed");
333 torture_assert_ntstatus_equal(torture, dom_sid_split_rid(torture, user_sid, &dom_sid, NULL), NT_STATUS_OK, "failed to split domain SID from user SID");
334 for (i = 0; i < el->num_values; i++) {
335 struct dom_sid *sid = talloc(dc_sids, struct dom_sid);
336 torture_assert(torture, sid != NULL, "talloc failed");
337
338 ret = sid_parse(el->values[i].data,
339 el->values[i].length,
340 sid);
341 torture_assert(torture,
342 ret != -1,
343 "sid parse failed");
344 if (dom_sid_in_domain(dom_sid, sid)) {
345 dc_sids[num_domain_sids_dc] = *sid;
346 num_domain_sids_dc++;
347 }
348 talloc_free(sid);
349 }
350
351 for (i = 0; i < whoami->num_sids; i++) {
352 if (dom_sid_in_domain(dom_sid, whoami->sid_list[i])) {
353 member_sids[num_domain_sids_member] = *whoami->sid_list[i];
354 num_domain_sids_member++;
355 }
356 }
357
358 torture_assert_int_equal(torture, num_domain_sids_dc, num_domain_sids_member, "Number of Domain SIDs from LDAP DC and number of SIDs from CIFS member does not match!");
359 for (i = 0; i < num_domain_sids_dc; i++) {
360 torture_assert_str_equal(torture, dom_sid_string(dc_sids, &dc_sids[i]), dom_sid_string(member_sids, &member_sids[i]), "Domain SID from LDAP DC and SID from CIFS member server does not match!");
361 }
362 talloc_free(dc_sids);
363 talloc_free(member_sids);
364 }
365 return true;
366 }
367
368 bool torture_unix_whoami(struct torture_context *torture)
369 {
370 struct smbcli_state *cli;
371 struct smb_whoami whoami;
372 bool ret = false;
373 struct ldb_context *ldb;
374 const char *addc, *host;
375
376 cli = connect_to_server(torture, samba_cmdline_get_creds());
377 torture_assert(torture, cli, "connecting to server with authenticated credentials");
378
379 /* Test basic authenticated mapping. */
380 torture_assert_goto(torture, smb_raw_query_posix_whoami(torture, torture,
381 cli, &whoami, 0xFFFF), ret, fail,
382 "calling SMB_QFS_POSIX_WHOAMI on an authenticated connection");
383
384 /* Check that our anonymous login mapped us to guest on the server, but
385 * only if the server supports this.
386 */
387 if (whoami.mapping_mask & SMB_WHOAMI_GUEST) {
388 bool guest = whoami.mapping_flags & SMB_WHOAMI_GUEST;
389 torture_comment(torture, "checking whether we were logged in as guest... %s\n",
390 guest ? "YES" : "NO");
391 torture_assert(torture,
392 cli_credentials_is_anonymous(
393 samba_cmdline_get_creds()) == guest,
394 "login did not credentials map to guest");
395 } else {
396 torture_comment(torture, "server does not support SMB_WHOAMI_GUEST flag\n");
397 }
398
399 addc = torture_setting_string(torture, "addc", NULL);
400 host = torture_setting_string(torture, "host", NULL);
401
402 if (addc) {
403 ldb = ldb_wrap_connect(torture, torture->ev, torture->lp_ctx, talloc_asprintf(torture, "ldap://%s", addc),
404 NULL, samba_cmdline_get_creds(), 0);
405 torture_assert(torture, ldb, "ldb connect failed");
406
407 /* We skip this testing if we could not contact the LDAP server */
408 if (!test_against_ldap(torture, ldb, strcasecmp(addc, host) == 0, &whoami)) {
409 goto fail;
410 }
411 }
412
413 /* Test that the server drops the UID and GID list. */
414 torture_assert_goto(torture, smb_raw_query_posix_whoami(torture, torture,
415 cli, &whoami, 0x40), ret, fail,
416 "calling SMB_QFS_POSIX_WHOAMI with a small buffer\n");
417
418 torture_assert_int_equal(torture, whoami.num_gids, 0,
419 "invalid GID count");
420 torture_assert_int_equal(torture, whoami.num_sids, 0,
421 "invalid SID count");
422 torture_assert_int_equal(torture, whoami.num_sid_bytes, 0,
423 "invalid SID bytes count");
424
425 smbcli_tdis(cli);
426
427 return true;
428 fail:
429
430 smbcli_tdis(cli);
431 return ret;
432 }
433
434 /* vim: set sts=8 sw=8 : */