Fix bug #9085 - NMB registration for a duplicate workstation fails with registration...
[Samba/gebeck_regimport.git] / source4 / torture / unix / whoami.c
blob7778776442da1bbba413ef532f52594227bdb966
1 /*
2 Test the SMB_WHOAMI Unix extension.
4 Copyright (C) 2007 James Peach
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.
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.
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/>.
20 #include "includes.h"
21 #include "libcli/libcli.h"
22 #include "libcli/raw/raw_proto.h"
23 #include "torture/torture.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "auth/credentials/credentials.h"
26 #include "param/param.h"
27 #include "libcli/resolve/resolve.h"
28 #include <ldb.h>
29 #include "lib/util/util_ldb.h"
30 #include "ldb_wrap.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "../libcli/security/security.h"
35 /* Size (in bytes) of the required fields in the SMBwhoami response. */
36 #define WHOAMI_REQUIRED_SIZE 40
39 SMBWhoami - Query the user mapping performed by the server for the
40 connected tree. This is a subcommand of the TRANS2_QFSINFO.
42 Returns:
43 4 bytes unsigned - mapping flags (smb_whoami_flags)
44 4 bytes unsigned - flags mask
46 8 bytes unsigned - primary UID
47 8 bytes unsigned - primary GID
48 4 bytes unsigned - number of supplementary GIDs
49 4 bytes unsigned - number of SIDs
50 4 bytes unsigned - SID list byte count
51 4 bytes - pad / reserved (must be zero)
53 8 bytes unsigned[] - list of GIDs (may be empty)
54 struct dom_sid[] - list of SIDs (may be empty)
57 struct smb_whoami
59 uint32_t mapping_flags;
60 uint32_t mapping_mask;
61 uint64_t server_uid;
62 uint64_t server_gid;
63 uint32_t num_gids;
64 uint32_t num_sids;
65 uint32_t num_sid_bytes;
66 uint32_t reserved; /* Must be zero */
67 uint64_t * gid_list;
68 struct dom_sid ** sid_list;
71 static struct smbcli_state *connect_to_server(struct torture_context *tctx,
72 struct cli_credentials *creds)
74 NTSTATUS status;
75 struct smbcli_state *cli;
77 const char *host = torture_setting_string(tctx, "host", NULL);
78 const char *share = torture_setting_string(tctx, "share", NULL);
79 struct smbcli_options options;
80 struct smbcli_session_options session_options;
82 lpcfg_smbcli_options(tctx->lp_ctx, &options);
83 lpcfg_smbcli_session_options(tctx->lp_ctx, &session_options);
85 status = smbcli_full_connection(tctx, &cli, host,
86 lpcfg_smb_ports(tctx->lp_ctx),
87 share, NULL, lpcfg_socket_options(tctx->lp_ctx),
88 creds, lpcfg_resolve_context(tctx->lp_ctx),
89 tctx->ev, &options, &session_options,
90 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
92 if (!NT_STATUS_IS_OK(status)) {
93 printf("failed to connect to //%s/%s: %s\n",
94 host, share, nt_errstr(status));
95 return NULL;
98 return cli;
101 static bool whoami_sid_parse(void *mem_ctx,
102 struct torture_context *torture,
103 DATA_BLOB *data, size_t *offset,
104 struct dom_sid **psid)
106 size_t remain = data->length - *offset;
107 int i;
109 *psid = talloc_zero(mem_ctx, struct dom_sid);
110 torture_assert(torture, *psid != NULL, "out of memory");
112 torture_assert(torture, remain >= 8,
113 "invalid SID format");
115 (*psid)->sid_rev_num = CVAL(data->data, *offset);
116 (*psid)->num_auths = CVAL(data->data, *offset + 1);
117 memcpy((*psid)->id_auth, data->data + *offset + 2, 6);
119 (*offset) += 8;
120 remain = data->length - *offset;
122 torture_assert(torture, remain >= ((*psid)->num_auths * 4),
123 "invalid sub_auth byte count");
124 torture_assert(torture, (*psid)->num_auths >= 0,
125 "invalid sub_auth value");
126 torture_assert(torture, (*psid)->num_auths <= 15,
127 "invalid sub_auth value");
129 for (i = 0; i < (*psid)->num_auths; i++) {
130 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
131 (*offset) += 4;
134 return true;
137 static bool smb_raw_query_posix_whoami(void *mem_ctx,
138 struct torture_context *torture,
139 struct smbcli_state *cli,
140 struct smb_whoami *whoami,
141 unsigned max_data)
143 struct smb_trans2 tp;
144 NTSTATUS status;
145 size_t offset;
146 int i;
148 uint16_t setup = TRANSACT2_QFSINFO;
149 uint16_t info_level;
151 ZERO_STRUCTP(whoami);
153 tp.in.max_setup = 0;
154 tp.in.flags = 0;
155 tp.in.timeout = 0;
156 tp.in.setup_count = 1;
157 tp.in.max_param = 10;
158 tp.in.max_data = (uint16_t)max_data;
159 tp.in.setup = &setup;
160 tp.in.trans_name = NULL;
161 SSVAL(&info_level, 0, SMB_QFS_POSIX_WHOAMI);
162 tp.in.params = data_blob_talloc(mem_ctx, &info_level, 2);
163 tp.in.data = data_blob_talloc(mem_ctx, NULL, 0);
165 status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
166 torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
167 "doing SMB_QFS_POSIX_WHOAMI");
169 /* Make sure we got back all the required fields. */
170 torture_assert(torture, tp.out.params.length == 0,
171 "trans2 params should be empty");
172 torture_assert(torture, tp.out.data.length >= WHOAMI_REQUIRED_SIZE,
173 "checking for required response fields");
175 whoami->mapping_flags = IVAL(tp.out.data.data, 0);
176 whoami->mapping_mask = IVAL(tp.out.data.data, 4);
177 whoami->server_uid = BVAL(tp.out.data.data, 8);
178 whoami->server_gid = BVAL(tp.out.data.data, 16);
179 whoami->num_gids = IVAL(tp.out.data.data, 24);
180 whoami->num_sids = IVAL(tp.out.data.data, 28);
181 whoami->num_sid_bytes = IVAL(tp.out.data.data, 32);
182 whoami->reserved = IVAL(tp.out.data.data, 36);
184 /* The GID list and SID list are optional, depending on the count
185 * and length fields.
187 if (whoami->num_sids != 0) {
188 torture_assert(torture, whoami->num_sid_bytes != 0,
189 "SID count does not match byte count");
192 printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
193 whoami->mapping_flags, whoami->mapping_mask);
194 printf("\tserver UID=%llu GID=%llu\n",
195 (unsigned long long)whoami->server_uid, (unsigned long long)whoami->server_gid);
196 printf("\t%u GIDs, %u SIDs, %u SID bytes\n",
197 whoami->num_gids, whoami->num_sids,
198 whoami->num_sid_bytes);
200 offset = WHOAMI_REQUIRED_SIZE;
202 torture_assert_int_equal(torture, whoami->reserved, 0,
203 "invalid reserved field");
205 if (tp.out.data.length == offset) {
206 /* No SIDs or GIDs returned */
207 torture_assert_int_equal(torture, whoami->num_gids, 0,
208 "invalid GID count");
209 torture_assert_int_equal(torture, whoami->num_sids, 0,
210 "invalid SID count");
211 torture_assert_int_equal(torture, whoami->num_sid_bytes, 0,
212 "invalid SID byte count");
213 return true;
216 if (whoami->num_gids != 0) {
217 int remain = tp.out.data.length - offset;
218 int gid_bytes = whoami->num_gids * 8;
220 if (whoami->num_sids == 0) {
221 torture_assert_int_equal(torture, remain, gid_bytes,
222 "GID count does not match data length");
223 } else {
224 torture_assert(torture, remain > gid_bytes,
225 "invalid GID count");
228 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
229 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
231 torture_comment(torture, "\tGIDs:\n");
233 for (i = 0; i < whoami->num_gids; ++i) {
234 whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
235 offset += 8;
236 torture_comment(torture, "\t\t%u\n", (unsigned int)whoami->gid_list[i]);
240 /* Check if there should be data left for the SID list. */
241 if (tp.out.data.length == offset) {
242 torture_assert_int_equal(torture, whoami->num_sids, 0,
243 "invalid SID count");
244 return true;
247 /* All the remaining bytes must be the SID list. */
248 torture_assert_int_equal(torture,
249 whoami->num_sid_bytes, (tp.out.data.length - offset),
250 "invalid SID byte count");
252 if (whoami->num_sids != 0) {
254 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
255 whoami->num_sids);
256 torture_assert(torture, whoami->sid_list != NULL,
257 "out of memory");
259 torture_comment(torture, "\tSIDs:\n");
261 for (i = 0; i < whoami->num_sids; ++i) {
262 if (!whoami_sid_parse(mem_ctx, torture,
263 &tp.out.data, &offset,
264 &whoami->sid_list[i])) {
265 return false;
268 torture_comment(torture, "\t\t%s\n",
269 dom_sid_string(torture, whoami->sid_list[i]));
273 /* We should be at the end of the response now. */
274 torture_assert_int_equal(torture, tp.out.data.length, offset,
275 "trailing garbage bytes");
277 return true;
280 static bool test_against_ldap(struct torture_context *torture, struct ldb_context *ldb, bool is_dc,
281 struct smb_whoami *whoami)
283 struct ldb_message *msg;
284 struct ldb_message_element *el;
286 const char *attrs[] = { "tokenGroups", NULL };
287 int i;
289 torture_assert_int_equal(torture, dsdb_search_one(ldb, torture, &msg, NULL, LDB_SCOPE_BASE, attrs, 0, NULL), LDB_SUCCESS, "searching for tokenGroups");
290 el = ldb_msg_find_element(msg, "tokenGroups");
291 torture_assert(torture, el, "obtaining tokenGroups");
292 torture_assert(torture, el->num_values > 0, "Number of SIDs from LDAP needs to be more than 0");
293 torture_assert(torture, whoami->num_sids > 0, "Number of SIDs from LDAP needs to be more than 0");
295 if (is_dc) {
296 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!");
298 for (i = 0; i < el->num_values; i++) {
299 struct dom_sid *sid = talloc(torture, struct dom_sid);
300 torture_assert(torture, sid != NULL, "talloc failed");
302 torture_assert(torture, sid_blob_parse(el->values[i], sid), "sid parse failed");
303 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!");
304 talloc_free(sid);
306 } else {
307 unsigned int num_domain_sids_dc = 0, num_domain_sids_member = 0;
308 struct dom_sid *user_sid = talloc(torture, struct dom_sid);
309 struct dom_sid *dom_sid = talloc(torture, struct dom_sid);
310 struct dom_sid *dc_sids = talloc_array(torture, struct dom_sid, el->num_values);
311 struct dom_sid *member_sids = talloc_array(torture, struct dom_sid, whoami->num_sids);
312 torture_assert(torture, user_sid != NULL, "talloc failed");
313 torture_assert(torture, sid_blob_parse(el->values[0], user_sid), "sid parse failed");
314 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");
315 for (i = 0; i < el->num_values; i++) {
316 struct dom_sid *sid = talloc(dc_sids, struct dom_sid);
317 torture_assert(torture, sid != NULL, "talloc failed");
319 torture_assert(torture, sid_blob_parse(el->values[i], sid), "sid parse failed");
320 if (dom_sid_in_domain(dom_sid, sid)) {
321 dc_sids[num_domain_sids_dc] = *sid;
322 num_domain_sids_dc++;
324 talloc_free(sid);
327 for (i = 0; i < whoami->num_sids; i++) {
328 if (dom_sid_in_domain(dom_sid, whoami->sid_list[i])) {
329 member_sids[num_domain_sids_member] = *whoami->sid_list[i];
330 num_domain_sids_member++;
334 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!");
335 for (i = 0; i < num_domain_sids_dc; i++) {
336 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!");
338 talloc_free(dc_sids);
339 talloc_free(member_sids);
341 return true;
344 bool torture_unix_whoami(struct torture_context *torture)
346 struct smbcli_state *cli;
347 struct smb_whoami whoami;
348 bool ret;
349 struct ldb_context *ldb;
350 const char *addc, *host;
352 cli = connect_to_server(torture, cmdline_credentials);
353 torture_assert(torture, cli, "connecting to server with authenticated credentials");
355 /* Test basic authenticated mapping. */
356 torture_assert_goto(torture, smb_raw_query_posix_whoami(torture, torture,
357 cli, &whoami, 0xFFFF), ret, fail,
358 "calling SMB_QFS_POSIX_WHOAMI on an authenticated connection");
360 /* Check that our anonymous login mapped us to guest on the server, but
361 * only if the server supports this.
363 if (whoami.mapping_mask & SMB_WHOAMI_GUEST) {
364 bool guest = whoami.mapping_flags & SMB_WHOAMI_GUEST;
365 torture_comment(torture, "checking whether we were logged in as guest... %s\n",
366 guest ? "YES" : "NO");
367 torture_assert(torture, cli_credentials_is_anonymous(cmdline_credentials) == guest,
368 "login did not credentials map to guest");
369 } else {
370 torture_comment(torture, "server does not support SMB_WHOAMI_GUEST flag\n");
373 addc = torture_setting_string(torture, "addc", NULL);
374 host = torture_setting_string(torture, "host", NULL);
376 if (addc) {
377 ldb = ldb_wrap_connect(torture, torture->ev, torture->lp_ctx, talloc_asprintf(torture, "ldap://%s", addc),
378 NULL, cmdline_credentials, 0);
379 torture_assert(torture, ldb, "ldb connect failed");
381 /* We skip this testing if we could not contact the LDAP server */
382 if (!test_against_ldap(torture, ldb, strcasecmp(addc, host) == 0, &whoami)) {
383 goto fail;
387 /* Test that the server drops the UID and GID list. */
388 torture_assert_goto(torture, smb_raw_query_posix_whoami(torture, torture,
389 cli, &whoami, 0x40), ret, fail,
390 "calling SMB_QFS_POSIX_WHOAMI with a small buffer\n");
392 torture_assert_int_equal(torture, whoami.num_gids, 0,
393 "invalid GID count");
394 torture_assert_int_equal(torture, whoami.num_sids, 0,
395 "invalid SID count");
396 torture_assert_int_equal(torture, whoami.num_sid_bytes, 0,
397 "invalid SID bytes count");
399 smbcli_tdis(cli);
401 return true;
402 fail:
404 smbcli_tdis(cli);
405 return ret;
408 /* vim: set sts=8 sw=8 : */