r26644: Janitorial: Pass resolve_context explicitly to various SMB functions, should...
[jelmer/samba4-debian.git] / source / 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 #include "libcli/resolve/resolve.h"
29
30 /* Size (in bytes) of the required fields in the SMBwhoami response. */
31 #define WHOAMI_REQUIRED_SIZE    40
32
33 enum smb_whoami_flags {
34     SMB_WHOAMI_GUEST = 0x1 /* Logged in as (or squashed to) guest */
35 };
36
37 /*
38    SMBWhoami - Query the user mapping performed by the server for the
39    connected tree. This is a subcommand of the TRANS2_QFSINFO.
40
41    Returns:
42        4 bytes unsigned -      mapping flags (smb_whoami_flags)
43        4 bytes unsigned -      flags mask
44
45        8 bytes unsigned -      primary UID
46        8 bytes unsigned -      primary GID
47        4 bytes unsigned -      number of supplementary GIDs
48        4 bytes unsigned -      number of SIDs
49        4 bytes unsigned -      SID list byte count
50        4 bytes -               pad / reserved (must be zero)
51
52        8 bytes unsigned[] -    list of GIDs (may be empty)
53        DOM_SID[] -             list of SIDs (may be empty)
54 */
55
56 struct smb_whoami
57 {
58         uint32_t        mapping_flags;
59         uint32_t        mapping_mask;
60         uint64_t        server_uid;
61         uint64_t        server_gid;
62         uint32_t        num_gids;
63         uint32_t        num_sids;
64         uint32_t        num_sid_bytes;
65         uint32_t        reserved; /* Must be zero */
66         uint64_t *      gid_list;
67         struct dom_sid ** sid_list;
68 };
69
70 static struct smbcli_state *connect_to_server(struct torture_context *tctx,
71                 struct cli_credentials *creds)
72 {
73         NTSTATUS status;
74         struct smbcli_state *cli;
75
76         const char *host = torture_setting_string(tctx, "host", NULL);
77         const char *share = torture_setting_string(tctx, "share", NULL);
78
79         status = smbcli_full_connection(tctx, &cli, host, 
80                                         lp_smb_ports(tctx->lp_ctx),
81                                         share, NULL,
82                                         creds, lp_resolve_context(tctx->lp_ctx),
83                                         NULL);
84
85         if (!NT_STATUS_IS_OK(status)) {
86                 printf("failed to connect to //%s/%s: %s\n",
87                         host, share, nt_errstr(status));
88                 return NULL;
89         }
90
91         return cli;
92 }
93
94 static bool sid_parse(void *mem_ctx,
95                 struct torture_context *torture,
96                 DATA_BLOB *data, size_t *offset,
97                 struct dom_sid **psid)
98 {
99         size_t remain = data->length - *offset;
100         int i;
101
102         *psid = talloc_zero(mem_ctx, struct dom_sid);
103         torture_assert(torture, *psid != NULL, "out of memory");
104
105         torture_assert(torture, remain >= 8,
106                         "invalid SID format");
107
108         (*psid)->sid_rev_num = CVAL(data->data, *offset);
109         (*psid)->num_auths = CVAL(data->data, *offset + 1);
110         memcpy((*psid)->id_auth, data->data + *offset + 2, 6);
111
112         (*offset) += 8;
113         remain = data->length - *offset;
114
115         torture_assert(torture, remain >= ((*psid)->num_auths * 4),
116                         "invalid sub_auth byte count");
117         torture_assert(torture, (*psid)->num_auths >= 0,
118                         "invalid sub_auth value");
119         torture_assert(torture, (*psid)->num_auths <= 15,
120                         "invalid sub_auth value");
121
122         (*psid)->sub_auths = talloc_array(mem_ctx, uint32_t,
123                         (*psid)->num_auths);
124         torture_assert(torture, (*psid)->sub_auths != NULL,
125                         "out of memory");
126
127         for (i = 0; i < (*psid)->num_auths; i++) {
128                 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
129                 (*offset) += 4;
130         }
131
132         return true;
133 }
134
135 static bool smb_raw_query_posix_whoami(void *mem_ctx,
136                                 struct torture_context *torture,
137                                 struct smbcli_state *cli,
138                                 struct smb_whoami *whoami,
139                                 unsigned max_data)
140 {
141         struct smb_trans2 tp;
142         NTSTATUS status;
143         size_t offset;
144         int i;
145
146         uint16_t setup = TRANSACT2_QFSINFO;
147         uint16_t info_level;
148
149         ZERO_STRUCTP(whoami);
150
151         tp.in.max_setup = 0;
152         tp.in.flags = 0;
153         tp.in.timeout = 0;
154         tp.in.setup_count = 1;
155         tp.in.max_param = 10;
156         tp.in.max_data = (uint16_t)max_data;
157         tp.in.setup = &setup;
158         tp.in.trans_name = NULL;
159         SSVAL(&info_level, 0, SMB_QFS_POSIX_WHOAMI);
160         tp.in.params = data_blob_talloc(mem_ctx, &info_level, 2);
161         tp.in.data = data_blob_talloc(mem_ctx, NULL, 0);
162
163         status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
164         torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
165                         "doing SMB_QFS_POSIX_WHOAMI");
166
167         /* Make sure we got back all the required fields. */
168         torture_assert(torture, tp.out.params.length == 0,
169                         "trans2 params should be empty");
170         torture_assert(torture, tp.out.data.length >= WHOAMI_REQUIRED_SIZE,
171                         "checking for required response fields");
172
173         whoami->mapping_flags = IVAL(tp.out.data.data, 0);
174         whoami->mapping_mask = IVAL(tp.out.data.data, 4);
175         whoami->server_uid = BVAL(tp.out.data.data, 8);
176         whoami->server_gid = BVAL(tp.out.data.data, 16);
177         whoami->num_gids = IVAL(tp.out.data.data, 24);
178         whoami->num_sids = IVAL(tp.out.data.data, 28);
179         whoami->num_sid_bytes = IVAL(tp.out.data.data, 32);
180         whoami->reserved = IVAL(tp.out.data.data, 36);
181
182         /* The GID list and SID list are optional, depending on the count
183          * and length fields.
184          */
185         if (whoami->num_sids != 0) {
186                 torture_assert(torture, whoami->num_sid_bytes != 0,
187                                 "SID count does not match byte count");
188         }
189
190         printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
191                         whoami->mapping_flags, whoami->mapping_mask);
192         printf("\tserver UID=%llu GID=%llu\n",
193                (unsigned long long)whoami->server_uid, (unsigned long long)whoami->server_gid);
194         printf("\t%u GIDs, %u SIDs, %u SID bytes\n",
195                         whoami->num_gids, whoami->num_sids,
196                         whoami->num_sid_bytes);
197
198         offset = WHOAMI_REQUIRED_SIZE;
199
200         torture_assert_int_equal(torture, whoami->reserved, 0,
201                         "invalid reserved field");
202
203         if (tp.out.data.length == offset) {
204                 /* No SIDs or GIDs returned */
205                 torture_assert_int_equal(torture, whoami->num_gids, 0,
206                                 "invalid GID count");
207                 torture_assert_int_equal(torture, whoami->num_sids, 0,
208                                 "invalid SID count");
209                 torture_assert_int_equal(torture, whoami->num_sid_bytes, 0,
210                                 "invalid SID byte count");
211                 return true;
212         }
213
214         if (whoami->num_gids != 0) {
215                 int remain = tp.out.data.length - offset;
216                 int gid_bytes = whoami->num_gids * 8;
217
218                 if (whoami->num_sids == 0) {
219                         torture_assert_int_equal(torture, remain, gid_bytes,
220                                         "GID count does not match data length");
221                 } else {
222                         torture_assert(torture, remain > gid_bytes,
223                                                 "invalid GID count");
224                 }
225
226                 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
227                 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
228
229                 for (i = 0; i < whoami->num_gids; ++i) {
230                         whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
231                         offset += 8;
232                 }
233         }
234
235         /* Check if there should be data left for the SID list. */
236         if (tp.out.data.length == offset) {
237                 torture_assert_int_equal(torture, whoami->num_sids, 0,
238                                 "invalid SID count");
239                 return true;
240         }
241
242         /* All the remaining bytes must be the SID list. */
243         torture_assert_int_equal(torture,
244                 whoami->num_sid_bytes, (tp.out.data.length - offset),
245                 "invalid SID byte count");
246
247         if (whoami->num_sids != 0) {
248
249                 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
250                                 whoami->num_sids);
251                 torture_assert(torture, whoami->sid_list != NULL,
252                                 "out of memory");
253
254                 for (i = 0; i < whoami->num_sids; ++i) {
255                         if (!sid_parse(mem_ctx, torture,
256                                         &tp.out.data, &offset,
257                                         &whoami->sid_list[i])) {
258                                 return false;
259                         }
260
261                 }
262         }
263
264         /* We should be at the end of the response now. */
265         torture_assert_int_equal(torture, tp.out.data.length, offset,
266                         "trailing garbage bytes");
267
268         return true;
269 }
270
271 bool torture_unix_whoami(struct torture_context *torture)
272 {
273         struct smbcli_state *cli;
274         struct cli_credentials *anon_credentials;
275         struct smb_whoami whoami;
276
277         if (!(cli = connect_to_server(torture, cmdline_credentials))) {
278                 return false;
279         }
280
281         /* Test basic authenticated mapping. */
282         printf("calling SMB_QFS_POSIX_WHOAMI on an authenticated connection\n");
283         if (!smb_raw_query_posix_whoami(torture, torture,
284                                 cli, &whoami, 0xFFFF)) {
285                 smbcli_tdis(cli);
286                 return false;
287         }
288
289         /* Test that the server drops the UID and GID list. */
290         printf("calling SMB_QFS_POSIX_WHOAMI with a small buffer\n");
291         if (!smb_raw_query_posix_whoami(torture, torture,
292                                 cli, &whoami, 0x40)) {
293                 smbcli_tdis(cli);
294                 return false;
295         }
296
297         torture_assert_int_equal(torture, whoami.num_gids, 0,
298                         "invalid GID count");
299         torture_assert_int_equal(torture, whoami.num_sids, 0,
300                         "invalid SID count");
301         torture_assert_int_equal(torture, whoami.num_sid_bytes, 0,
302                         "invalid SID bytes count");
303
304         smbcli_tdis(cli);
305
306         printf("calling SMB_QFS_POSIX_WHOAMI on an anonymous connection\n");
307         anon_credentials = cli_credentials_init_anon(torture);
308
309         if (!(cli = connect_to_server(torture, anon_credentials))) {
310                 return false;
311         }
312
313         if (!smb_raw_query_posix_whoami(torture, torture,
314                                 cli, &whoami, 0xFFFF)) {
315                 smbcli_tdis(cli);
316                 return false;
317         }
318
319         smbcli_tdis(cli);
320
321         /* Check that our anonymous login mapped us to guest on the server, but
322          * only if the server supports this.
323          */
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");
329         } else {
330                 printf("server does not support SMB_WHOAMI_GUEST flag\n");
331         }
332
333         return true;
334 }
335
336 /* vim: set sts=8 sw=8 : */