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