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