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