s4-torture: ran minimal_includes.pl over source4/torture
[samba.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 "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
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         struct smbcli_options options;
78         struct smbcli_session_options session_options;
79
80         lp_smbcli_options(tctx->lp_ctx, &options);
81         lp_smbcli_session_options(tctx->lp_ctx, &session_options);
82
83         status = smbcli_full_connection(tctx, &cli, host, 
84                                         lp_smb_ports(tctx->lp_ctx),
85                                         share, NULL, lp_socket_options(tctx->lp_ctx),
86                                         creds, lp_resolve_context(tctx->lp_ctx),
87                                         tctx->ev, &options, &session_options,
88                                         lp_iconv_convenience(tctx->lp_ctx),
89                                         lp_gensec_settings(tctx, tctx->lp_ctx));
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 printf("failed to connect to //%s/%s: %s\n",
93                         host, share, nt_errstr(status));
94                 return NULL;
95         }
96
97         return cli;
98 }
99
100 static bool sid_parse(void *mem_ctx,
101                 struct torture_context *torture,
102                 DATA_BLOB *data, size_t *offset,
103                 struct dom_sid **psid)
104 {
105         size_t remain = data->length - *offset;
106         int i;
107
108         *psid = talloc_zero(mem_ctx, struct dom_sid);
109         torture_assert(torture, *psid != NULL, "out of memory");
110
111         torture_assert(torture, remain >= 8,
112                         "invalid SID format");
113
114         (*psid)->sid_rev_num = CVAL(data->data, *offset);
115         (*psid)->num_auths = CVAL(data->data, *offset + 1);
116         memcpy((*psid)->id_auth, data->data + *offset + 2, 6);
117
118         (*offset) += 8;
119         remain = data->length - *offset;
120
121         torture_assert(torture, remain >= ((*psid)->num_auths * 4),
122                         "invalid sub_auth byte count");
123         torture_assert(torture, (*psid)->num_auths >= 0,
124                         "invalid sub_auth value");
125         torture_assert(torture, (*psid)->num_auths <= 15,
126                         "invalid sub_auth value");
127
128         for (i = 0; i < (*psid)->num_auths; i++) {
129                 (*psid)->sub_auths[i] = IVAL(data->data, *offset);
130                 (*offset) += 4;
131         }
132
133         return true;
134 }
135
136 static bool smb_raw_query_posix_whoami(void *mem_ctx,
137                                 struct torture_context *torture,
138                                 struct smbcli_state *cli,
139                                 struct smb_whoami *whoami,
140                                 unsigned max_data)
141 {
142         struct smb_trans2 tp;
143         NTSTATUS status;
144         size_t offset;
145         int i;
146
147         uint16_t setup = TRANSACT2_QFSINFO;
148         uint16_t info_level;
149
150         ZERO_STRUCTP(whoami);
151
152         tp.in.max_setup = 0;
153         tp.in.flags = 0;
154         tp.in.timeout = 0;
155         tp.in.setup_count = 1;
156         tp.in.max_param = 10;
157         tp.in.max_data = (uint16_t)max_data;
158         tp.in.setup = &setup;
159         tp.in.trans_name = NULL;
160         SSVAL(&info_level, 0, SMB_QFS_POSIX_WHOAMI);
161         tp.in.params = data_blob_talloc(mem_ctx, &info_level, 2);
162         tp.in.data = data_blob_talloc(mem_ctx, NULL, 0);
163
164         status = smb_raw_trans2(cli->tree, mem_ctx, &tp);
165         torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
166                         "doing SMB_QFS_POSIX_WHOAMI");
167
168         /* Make sure we got back all the required fields. */
169         torture_assert(torture, tp.out.params.length == 0,
170                         "trans2 params should be empty");
171         torture_assert(torture, tp.out.data.length >= WHOAMI_REQUIRED_SIZE,
172                         "checking for required response fields");
173
174         whoami->mapping_flags = IVAL(tp.out.data.data, 0);
175         whoami->mapping_mask = IVAL(tp.out.data.data, 4);
176         whoami->server_uid = BVAL(tp.out.data.data, 8);
177         whoami->server_gid = BVAL(tp.out.data.data, 16);
178         whoami->num_gids = IVAL(tp.out.data.data, 24);
179         whoami->num_sids = IVAL(tp.out.data.data, 28);
180         whoami->num_sid_bytes = IVAL(tp.out.data.data, 32);
181         whoami->reserved = IVAL(tp.out.data.data, 36);
182
183         /* The GID list and SID list are optional, depending on the count
184          * and length fields.
185          */
186         if (whoami->num_sids != 0) {
187                 torture_assert(torture, whoami->num_sid_bytes != 0,
188                                 "SID count does not match byte count");
189         }
190
191         printf("\tmapping_flags=0x%08x mapping_mask=0x%08x\n",
192                         whoami->mapping_flags, whoami->mapping_mask);
193         printf("\tserver UID=%llu GID=%llu\n",
194                (unsigned long long)whoami->server_uid, (unsigned long long)whoami->server_gid);
195         printf("\t%u GIDs, %u SIDs, %u SID bytes\n",
196                         whoami->num_gids, whoami->num_sids,
197                         whoami->num_sid_bytes);
198
199         offset = WHOAMI_REQUIRED_SIZE;
200
201         torture_assert_int_equal(torture, whoami->reserved, 0,
202                         "invalid reserved field");
203
204         if (tp.out.data.length == offset) {
205                 /* No SIDs or GIDs returned */
206                 torture_assert_int_equal(torture, whoami->num_gids, 0,
207                                 "invalid GID count");
208                 torture_assert_int_equal(torture, whoami->num_sids, 0,
209                                 "invalid SID count");
210                 torture_assert_int_equal(torture, whoami->num_sid_bytes, 0,
211                                 "invalid SID byte count");
212                 return true;
213         }
214
215         if (whoami->num_gids != 0) {
216                 int remain = tp.out.data.length - offset;
217                 int gid_bytes = whoami->num_gids * 8;
218
219                 if (whoami->num_sids == 0) {
220                         torture_assert_int_equal(torture, remain, gid_bytes,
221                                         "GID count does not match data length");
222                 } else {
223                         torture_assert(torture, remain > gid_bytes,
224                                                 "invalid GID count");
225                 }
226
227                 whoami->gid_list = talloc_array(mem_ctx, uint64_t, whoami->num_gids);
228                 torture_assert(torture, whoami->gid_list != NULL, "out of memory");
229
230                 for (i = 0; i < whoami->num_gids; ++i) {
231                         whoami->gid_list[i] = BVAL(tp.out.data.data, offset);
232                         offset += 8;
233                 }
234         }
235
236         /* Check if there should be data left for the SID list. */
237         if (tp.out.data.length == offset) {
238                 torture_assert_int_equal(torture, whoami->num_sids, 0,
239                                 "invalid SID count");
240                 return true;
241         }
242
243         /* All the remaining bytes must be the SID list. */
244         torture_assert_int_equal(torture,
245                 whoami->num_sid_bytes, (tp.out.data.length - offset),
246                 "invalid SID byte count");
247
248         if (whoami->num_sids != 0) {
249
250                 whoami->sid_list = talloc_array(mem_ctx, struct dom_sid *,
251                                 whoami->num_sids);
252                 torture_assert(torture, whoami->sid_list != NULL,
253                                 "out of memory");
254
255                 for (i = 0; i < whoami->num_sids; ++i) {
256                         if (!sid_parse(mem_ctx, torture,
257                                         &tp.out.data, &offset,
258                                         &whoami->sid_list[i])) {
259                                 return false;
260                         }
261
262                 }
263         }
264
265         /* We should be at the end of the response now. */
266         torture_assert_int_equal(torture, tp.out.data.length, offset,
267                         "trailing garbage bytes");
268
269         return true;
270 }
271
272 bool torture_unix_whoami(struct torture_context *torture)
273 {
274         struct smbcli_state *cli;
275         struct cli_credentials *anon_credentials;
276         struct smb_whoami whoami;
277
278         if (!(cli = connect_to_server(torture, cmdline_credentials))) {
279                 return false;
280         }
281
282         /* Test basic authenticated mapping. */
283         printf("calling SMB_QFS_POSIX_WHOAMI on an authenticated connection\n");
284         if (!smb_raw_query_posix_whoami(torture, torture,
285                                 cli, &whoami, 0xFFFF)) {
286                 smbcli_tdis(cli);
287                 return false;
288         }
289
290         /* Test that the server drops the UID and GID list. */
291         printf("calling SMB_QFS_POSIX_WHOAMI with a small buffer\n");
292         if (!smb_raw_query_posix_whoami(torture, torture,
293                                 cli, &whoami, 0x40)) {
294                 smbcli_tdis(cli);
295                 return false;
296         }
297
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");
304
305         smbcli_tdis(cli);
306
307         printf("calling SMB_QFS_POSIX_WHOAMI on an anonymous connection\n");
308         anon_credentials = cli_credentials_init_anon(torture);
309
310         if (!(cli = connect_to_server(torture, anon_credentials))) {
311                 return false;
312         }
313
314         if (!smb_raw_query_posix_whoami(torture, torture,
315                                 cli, &whoami, 0xFFFF)) {
316                 smbcli_tdis(cli);
317                 return false;
318         }
319
320         smbcli_tdis(cli);
321
322         /* Check that our anonymous login mapped us to guest on the server, but
323          * only if the server supports this.
324          */
325         if (whoami.mapping_mask & SMB_WHOAMI_GUEST) {
326                 printf("checking whether we were logged in as guest... %s\n",
327                         whoami.mapping_flags & SMB_WHOAMI_GUEST ? "YES" : "NO");
328                 torture_assert(torture, whoami.mapping_flags & SMB_WHOAMI_GUEST,
329                                 "anonymous login did not map to guest");
330         } else {
331                 printf("server does not support SMB_WHOAMI_GUEST flag\n");
332         }
333
334         return true;
335 }
336
337 /* vim: set sts=8 sw=8 : */