r26409: Pass smb ports along.
[gd/samba-autobuild/.git] / source4 / torture / unix / whoami.c
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 "torture/torture.h"
22 #include "torture/basic/proto.h"
23 #include "libcli/libcli.h"
24 #include "libcli/raw/interfaces.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "auth/credentials/credentials.h"
27 #include "param/param.h"
28
29 /* Size (in bytes) of the required fields in the SMBwhoami response. */
30 #define WHOAMI_REQUIRED_SIZE    40
31
32 enum smb_whoami_flags {
33     SMB_WHOAMI_GUEST = 0x1 /* Logged in as (or squashed to) guest */
34 };
35
36 /*
37    SMBWhoami - Query the user mapping performed by the server for the
38    connected tree. This is a subcommand of the TRANS2_QFSINFO.
39
40    Returns:
41        4 bytes unsigned -      mapping flags (smb_whoami_flags)
42        4 bytes unsigned -      flags mask
43
44        8 bytes unsigned -      primary UID
45        8 bytes unsigned -      primary GID
46        4 bytes unsigned -      number of supplementary GIDs
47        4 bytes unsigned -      number of SIDs
48        4 bytes unsigned -      SID list byte count
49        4 bytes -               pad / reserved (must be zero)
50
51        8 bytes unsigned[] -    list of GIDs (may be empty)
52        DOM_SID[] -             list of SIDs (may be empty)
53 */
54
55 struct smb_whoami
56 {
57         uint32_t        mapping_flags;
58         uint32_t        mapping_mask;
59         uint64_t        server_uid;
60         uint64_t        server_gid;
61         uint32_t        num_gids;
62         uint32_t        num_sids;
63         uint32_t        num_sid_bytes;
64         uint32_t        reserved; /* Must be zero */
65         uint64_t *      gid_list;
66         struct dom_sid ** sid_list;
67 };
68
69 static struct smbcli_state *connect_to_server(struct torture_context *tctx,
70                 struct cli_credentials *creds)
71 {
72         NTSTATUS status;
73         struct smbcli_state *cli;
74
75         const char *host = torture_setting_string(tctx, "host", NULL);
76         const char *share = torture_setting_string(tctx, "share", NULL);
77
78         status = smbcli_full_connection(tctx, &cli, host, 
79                                         lp_smb_ports(tctx->lp_ctx),
80                                         share, NULL,
81                                         creds, NULL);
82
83         if (!NT_STATUS_IS_OK(status)) {
84                 printf("failed to connect to //%s/%s: %s\n",
85                         host, share, nt_errstr(status));
86                 return NULL;
87         }
88
89         return cli;
90 }
91
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)
96 {
97         size_t remain = data->length - *offset;
98         int i;
99
100         *psid = talloc_zero(mem_ctx, struct dom_sid);
101         torture_assert(torture, *psid != NULL, "out of memory");
102
103         torture_assert(torture, remain >= 8,
104                         "invalid SID format");
105
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);
109
110         (*offset) += 8;
111         remain = data->length - *offset;
112
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");
119
120         (*psid)->sub_auths = talloc_array(mem_ctx, uint32_t,
121                         (*psid)->num_auths);
122         torture_assert(torture, (*psid)->sub_auths != NULL,
123                         "out of memory");
124
125         for (i = 0; i < (*psid)->num_auths; i++) {
126                 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
127                 (*offset) += 4;
128         }
129
130         return true;
131 }
132
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,
137                                 unsigned max_data)
138 {
139         struct smb_trans2 tp;
140         NTSTATUS status;
141         size_t offset;
142         int i;
143
144         uint16_t setup = TRANSACT2_QFSINFO;
145         uint16_t info_level;
146
147         ZERO_STRUCTP(whoami);
148
149         tp.in.max_setup = 0;
150         tp.in.flags = 0;
151         tp.in.timeout = 0;
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_QFS_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);
160
161         status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
162         torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
163                         "doing SMB_QFS_POSIX_WHOAMI");
164
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");
170
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);
179
180         /* The GID list and SID list are optional, depending on the count
181          * and length fields.
182          */
183         if (whoami->num_sids != 0) {
184                 torture_assert(torture, whoami->num_sid_bytes != 0,
185                                 "SID count does not match byte count");
186         }
187
188         printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
189                         whoami->mapping_flags, whoami->mapping_mask);
190         printf("\tserver UID=%llu GID=%llu\n",
191                (unsigned long long)whoami->server_uid, (unsigned long long)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);
195
196         offset = WHOAMI_REQUIRED_SIZE;
197
198         torture_assert_int_equal(torture, whoami->reserved, 0,
199                         "invalid reserved field");
200
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");
209                 return true;
210         }
211
212         if (whoami->num_gids != 0) {
213                 int remain = tp.out.data.length - offset;
214                 int gid_bytes = whoami->num_gids * 8;
215
216                 if (whoami->num_sids == 0) {
217                         torture_assert_int_equal(torture, remain, gid_bytes,
218                                         "GID count does not match data length");
219                 } else {
220                         torture_assert(torture, remain > gid_bytes,
221                                                 "invalid GID count");
222                 }
223
224                 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
225                 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
226
227                 for (i = 0; i < whoami->num_gids; ++i) {
228                         whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
229                         offset += 8;
230                 }
231         }
232
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");
237                 return true;
238         }
239
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");
244
245         if (whoami->num_sids != 0) {
246
247                 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
248                                 whoami->num_sids);
249                 torture_assert(torture, whoami->sid_list != NULL,
250                                 "out of memory");
251
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])) {
256                                 return false;
257                         }
258
259                 }
260         }
261
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");
265
266         return true;
267 }
268
269 bool torture_unix_whoami(struct torture_context *torture)
270 {
271         struct smbcli_state *cli;
272         struct cli_credentials *anon_credentials;
273         struct smb_whoami whoami;
274
275         if (!(cli = connect_to_server(torture, cmdline_credentials))) {
276                 return false;
277         }
278
279         /* Test basic authenticated mapping. */
280         printf("calling SMB_QFS_POSIX_WHOAMI on an authenticated connection\n");
281         if (!smb_raw_query_posix_whoami(torture, torture,
282                                 cli, &whoami, 0xFFFF)) {
283                 smbcli_tdis(cli);
284                 return false;
285         }
286
287         /* Test that the server drops the UID and GID list. */
288         printf("calling SMB_QFS_POSIX_WHOAMI with a small buffer\n");
289         if (!smb_raw_query_posix_whoami(torture, torture,
290                                 cli, &whoami, 0x40)) {
291                 smbcli_tdis(cli);
292                 return false;
293         }
294
295         torture_assert_int_equal(torture, whoami.num_gids, 0,
296                         "invalid GID count");
297         torture_assert_int_equal(torture, whoami.num_sids, 0,
298                         "invalid SID count");
299         torture_assert_int_equal(torture, whoami.num_sid_bytes, 0,
300                         "invalid SID bytes count");
301
302         smbcli_tdis(cli);
303
304         printf("calling SMB_QFS_POSIX_WHOAMI on an anonymous connection\n");
305         anon_credentials = cli_credentials_init_anon(torture);
306
307         if (!(cli = connect_to_server(torture, anon_credentials))) {
308                 return false;
309         }
310
311         if (!smb_raw_query_posix_whoami(torture, torture,
312                                 cli, &whoami, 0xFFFF)) {
313                 smbcli_tdis(cli);
314                 return false;
315         }
316
317         smbcli_tdis(cli);
318
319         /* Check that our anonymous login mapped us to guest on the server, but
320          * only if the server supports this.
321          */
322         if (whoami.mapping_mask & SMB_WHOAMI_GUEST) {
323                 printf("checking whether we were logged in as guest... %s\n",
324                         whoami.mapping_flags & SMB_WHOAMI_GUEST ? "YES" : "NO");
325                 torture_assert(torture, whoami.mapping_flags & SMB_WHOAMI_GUEST,
326                                 "anonymous login did not map to guest");
327         } else {
328                 printf("server does not support SMB_WHOAMI_GUEST flag\n");
329         }
330
331         return true;
332 }
333
334 /* vim: set sts=8 sw=8 : */