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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "torture/torture.h"
23 #include "torture/basic/proto.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/interfaces.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "auth/credentials/credentials.h"
29 #define SMB_QUERY_POSIX_WHOAMI 0x202
31 /* Size (in bytes) of the required fields in the SMBwhoami response. */
32 #define WHOAMI_REQUIRED_SIZE 40
34 enum smb_whoami_flags {
35 SMB_WHOAMI_GUEST = 0x1 /* Logged in as (or squashed to) guest */
39 SMBWhoami - Query the user mapping performed by the server for the
40 connected tree. This is a subcommand of the TRANS2_QFSINFO.
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 DOM_SID[] - list of SIDs (may be empty)
59 uint32_t mapping_flags;
60 uint32_t mapping_mask;
65 uint32_t num_sid_bytes;
66 uint32_t reserved; /* Must be zero */
68 struct dom_sid ** sid_list;
71 static struct smbcli_state *connect_to_server(void *mem_ctx)
74 struct smbcli_state *cli;
76 const char *host = lp_parm_string(-1, "torture", "host");
77 const char *share = lp_parm_string(-1, "torture", "share");
79 status = smbcli_full_connection(mem_ctx, &cli,
81 cmdline_credentials, NULL);
83 if (!NT_STATUS_IS_OK(status)) {
84 printf("failed to connect to //%s/%s: %s\n",
85 host, share, nt_errstr(status));
92 static BOOL sid_parse(void *mem_ctx,
93 struct torture_context *torture,
94 DATA_BLOB *data, size_t *offset,
95 struct dom_sid **psid)
97 size_t remain = data->length - *offset;
100 *psid = talloc_zero(mem_ctx, struct dom_sid);
101 torture_assert(torture, *psid != NULL, "out of memory");
103 torture_assert(torture, remain >= 8,
104 "invalid SID format");
106 (*psid)->sid_rev_num = CVAL(data->data, *offset);
107 (*psid)->num_auths = CVAL(data->data, *offset + 1);
108 memcpy((*psid)->id_auth, data->data + *offset + 2, 6);
111 remain = data->length - *offset;
113 torture_assert(torture, remain >= ((*psid)->num_auths * 4),
114 "invalid sub_auth byte count");
115 torture_assert(torture, (*psid)->num_auths >= 0,
116 "invalid sub_auth value");
117 torture_assert(torture, (*psid)->num_auths <= 15,
118 "invalid sub_auth value");
120 (*psid)->sub_auths = talloc_array(mem_ctx, uint32_t,
122 torture_assert(torture, (*psid)->sub_auths != NULL,
125 for (i = 0; i < (*psid)->num_auths; i++) {
126 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
133 static BOOL smb_raw_query_posix_whoami(void *mem_ctx,
134 struct torture_context *torture,
135 struct smbcli_state *cli,
136 struct smb_whoami *whoami,
139 struct smb_trans2 tp;
144 uint16_t setup = TRANSACT2_QFSINFO;
147 ZERO_STRUCTP(whoami);
152 tp.in.setup_count = 1;
153 tp.in.max_param = 10;
154 tp.in.max_data = (uint16_t)max_data;
155 tp.in.setup = &setup;
156 tp.in.trans_name = NULL;
157 SSVAL(&info_level, 0, SMB_QUERY_POSIX_WHOAMI);
158 tp.in.params = data_blob_talloc(mem_ctx, &info_level, 2);
159 tp.in.data = data_blob_talloc(mem_ctx, NULL, 0);
161 status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
162 torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
163 "doing SMB_QUERY_POSIX_WHOAMI");
165 /* Make sure we got back all the required fields. */
166 torture_assert(torture, tp.out.params.length == 0,
167 "trans2 params should be empty");
168 torture_assert(torture, tp.out.data.length >= WHOAMI_REQUIRED_SIZE,
169 "checking for required response fields");
171 whoami->mapping_flags = IVAL(tp.out.data.data, 0);
172 whoami->mapping_mask = IVAL(tp.out.data.data, 4);
173 whoami->server_uid = BVAL(tp.out.data.data, 8);
174 whoami->server_gid = BVAL(tp.out.data.data, 16);
175 whoami->num_gids = IVAL(tp.out.data.data, 24);
176 whoami->num_sids = IVAL(tp.out.data.data, 28);
177 whoami->num_sid_bytes = IVAL(tp.out.data.data, 32);
178 whoami->reserved = IVAL(tp.out.data.data, 36);
180 /* The GID list and SID list are optional, depending on the count
183 if (whoami->num_sids != 0) {
184 torture_assert(torture, whoami->num_sid_bytes != 0,
185 "SID count does not match byte count");
188 printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
189 whoami->mapping_flags, whoami->mapping_mask);
190 printf("\tserver UID=%lld GID=%lld\n",
191 whoami->server_uid, whoami->server_gid);
192 printf("\t%u GIDs, %u SIDs, %u SID bytes\n",
193 whoami->num_gids, whoami->num_sids,
194 whoami->num_sid_bytes);
196 offset = WHOAMI_REQUIRED_SIZE;
198 torture_assert_int_equal(torture, whoami->reserved, 0,
199 "invalid reserved field");
201 if (tp.out.data.length == offset) {
202 /* No SIDs or GIDs returned */
203 torture_assert_int_equal(torture, whoami->num_gids, 0,
204 "invalid GID count");
205 torture_assert_int_equal(torture, whoami->num_sids, 0,
206 "invalid SID count");
207 torture_assert_int_equal(torture, whoami->num_sid_bytes, 0,
208 "invalid SID byte count");
212 if (whoami->num_gids != 0) {
213 int remain = tp.out.data.length - offset;
214 int gid_bytes = whoami->num_gids * 8;
216 if (whoami->num_sids == 0) {
217 torture_assert_int_equal(torture, remain, gid_bytes,
218 "GID count does not match data length");
220 torture_assert(torture, remain > gid_bytes,
221 "invalid GID count");
224 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
225 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
227 for (i = 0; i < whoami->num_gids; ++i) {
228 whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
233 /* Check if there should be data left for the SID list. */
234 if (tp.out.data.length == offset) {
235 torture_assert_int_equal(torture, whoami->num_sids, 0,
236 "invalid SID count");
240 /* All the remaining bytes must be the SID list. */
241 torture_assert_int_equal(torture,
242 whoami->num_sid_bytes, (tp.out.data.length - offset),
243 "invalid SID byte count");
245 if (whoami->num_sids != 0) {
247 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
249 torture_assert(torture, whoami->sid_list != NULL,
252 for (i = 0; i < whoami->num_sids; ++i) {
253 if (!sid_parse(mem_ctx, torture,
254 &tp.out.data, &offset,
255 &whoami->sid_list[i])) {
262 /* We should be at the end of the response now. */
263 torture_assert_int_equal(torture, tp.out.data.length, offset,
264 "trailing garbage bytes");
269 BOOL torture_unix_whoami(struct torture_context *torture)
271 struct smbcli_state *cli;
272 struct smb_whoami whoami;
275 mem_ctx = talloc_init("smb_query_posix_whoami");
276 torture_assert(torture, mem_ctx != NULL, "malloc failed");
278 if (!(cli = connect_to_server(mem_ctx))) {
282 /* Test basic authenticated mapping. */
283 printf("calling SMB_QUERY_POSIX_WHOAMI on an authenticated connection\n");
284 if (!smb_raw_query_posix_whoami(mem_ctx, torture,
285 cli, &whoami, 0xFFFF)) {
290 /* Test that the server drops the UID and GID list. */
291 printf("calling SMB_QUERY_POSIX_WHOAMI with a small buffer\n");
292 if (!smb_raw_query_posix_whoami(mem_ctx, torture,
293 cli, &whoami, 0x40)) {
298 torture_assert_int_equal(torture, whoami.num_gids, 0,
299 "invalid GID count");
300 torture_assert_int_equal(torture, whoami.num_sids, 0,
301 "invalid SID count");
302 torture_assert_int_equal(torture, whoami.num_sid_bytes, 0,
303 "invalid SID bytes count");
306 cli_credentials_set_anonymous(cmdline_credentials);
308 if (!(cli = connect_to_server(mem_ctx))) {
312 printf("calling SMB_QUERY_POSIX_WHOAMI on an anonymous connection\n");
313 if (!smb_raw_query_posix_whoami(mem_ctx, torture,
314 cli, &whoami, 0xFFFF)) {
321 /* Check that our anonymous login mapped us to guest on the server, but
322 * only if the server supports this.
324 if (whoami.mapping_mask & SMB_WHOAMI_GUEST) {
325 printf("checking whether we were logged in as guest... %s\n",
326 whoami.mapping_flags & SMB_WHOAMI_GUEST ? "YES" : "NO");
327 torture_assert(torture, whoami.mapping_flags & SMB_WHOAMI_GUEST,
328 "anonymous login did not map to guest");
330 printf("server does not support SMB_WHOAMI_GUEST flag\n");
333 talloc_free(mem_ctx);
337 talloc_free(mem_ctx);
342 /* vim: set sts=8 sw=8 : */