r4061: more additions to the RAW-ACLS test, to help me work out some details for...
[metze/samba/wip.git] / source4 / torture / raw / acls.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test security descriptor operations
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26
27 #define BASEDIR "\\testsd"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%s) Incorrect status %s - should be %s\n", \
32                        __location__, nt_errstr(status), nt_errstr(correct)); \
33                 ret = False; \
34                 goto done; \
35         }} while (0)
36
37
38 static BOOL test_sd(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
39 {
40         NTSTATUS status;
41         union smb_open io;
42         const char *fname = BASEDIR "\\sd.txt";
43         BOOL ret = True;
44         int fnum = -1;
45         union smb_fileinfo q;
46         union smb_setfileinfo set;
47         struct security_ace ace;
48         struct security_descriptor *sd;
49         struct dom_sid *test_sid;
50
51         printf("TESTING SETFILEINFO EA_SET\n");
52
53         io.generic.level = RAW_OPEN_NTCREATEX;
54         io.ntcreatex.in.root_fid = 0;
55         io.ntcreatex.in.flags = 0;
56         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
57         io.ntcreatex.in.create_options = 0;
58         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
59         io.ntcreatex.in.share_access = 
60                 NTCREATEX_SHARE_ACCESS_READ | 
61                 NTCREATEX_SHARE_ACCESS_WRITE;
62         io.ntcreatex.in.alloc_size = 0;
63         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
64         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
65         io.ntcreatex.in.security_flags = 0;
66         io.ntcreatex.in.fname = fname;
67         status = smb_raw_open(cli->tree, mem_ctx, &io);
68         CHECK_STATUS(status, NT_STATUS_OK);
69         fnum = io.ntcreatex.out.fnum;
70         
71         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
72         q.query_secdesc.in.fnum = fnum;
73         q.query_secdesc.in.secinfo_flags = 
74                 SECINFO_OWNER |
75                 SECINFO_GROUP |
76                 SECINFO_DACL;
77         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
78         CHECK_STATUS(status, NT_STATUS_OK);
79         sd = q.query_secdesc.out.sd;
80
81         printf("add a new ACE to the DACL\n");
82
83         test_sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-1234-5432");
84
85         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
86         ace.flags = 0;
87         ace.access_mask = SEC_STD_ALL;
88         ace.trustee = *test_sid;
89
90         status = security_descriptor_dacl_add(sd, &ace);
91         CHECK_STATUS(status, NT_STATUS_OK);
92
93         set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
94         set.set_secdesc.file.fnum = fnum;
95         set.set_secdesc.in.secinfo_flags = q.query_secdesc.in.secinfo_flags;
96         set.set_secdesc.in.sd = sd;
97
98         status = smb_raw_setfileinfo(cli->tree, &set);
99         CHECK_STATUS(status, NT_STATUS_OK);
100
101         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
102         CHECK_STATUS(status, NT_STATUS_OK);
103
104         if (!security_descriptor_equal(q.query_secdesc.out.sd, sd)) {
105                 printf("security descriptors don't match!\n");
106                 printf("got:\n");
107                 NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
108                 printf("expected:\n");
109                 NDR_PRINT_DEBUG(security_descriptor, sd);
110         }
111
112         printf("remove it again\n");
113
114         status = security_descriptor_dacl_del(sd, test_sid);
115         CHECK_STATUS(status, NT_STATUS_OK);
116
117         status = smb_raw_setfileinfo(cli->tree, &set);
118         CHECK_STATUS(status, NT_STATUS_OK);
119
120         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
121         CHECK_STATUS(status, NT_STATUS_OK);
122
123         if (!security_descriptor_equal(q.query_secdesc.out.sd, sd)) {
124                 printf("security descriptors don't match!\n");
125                 printf("got:\n");
126                 NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
127                 printf("expected:\n");
128                 NDR_PRINT_DEBUG(security_descriptor, sd);
129         }
130
131 done:
132         smbcli_close(cli->tree, fnum);
133         return ret;
134 }
135
136
137 /*
138   test using NTTRANS CREATE to create a file with an initial ACL set
139 */
140 static BOOL test_nttrans_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
141 {
142         NTSTATUS status;
143         union smb_open io;
144         const char *fname = BASEDIR "\\acl2.txt";
145         BOOL ret = True;
146         int fnum = -1;
147         union smb_fileinfo q;
148         struct security_ace ace;
149         struct security_descriptor *sd;
150         struct dom_sid *test_sid;
151
152         printf("TESTING NTTRANS CREATE WITH SEC_DESC\n");
153
154         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
155         io.ntcreatex.in.root_fid = 0;
156         io.ntcreatex.in.flags = 0;
157         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
158         io.ntcreatex.in.create_options = 0;
159         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
160         io.ntcreatex.in.share_access = 
161                 NTCREATEX_SHARE_ACCESS_READ | 
162                 NTCREATEX_SHARE_ACCESS_WRITE;
163         io.ntcreatex.in.alloc_size = 0;
164         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
165         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
166         io.ntcreatex.in.security_flags = 0;
167         io.ntcreatex.in.fname = fname;
168         io.ntcreatex.in.sec_desc = NULL;
169         io.ntcreatex.in.ea_list = NULL;
170
171         printf("creating normal file\n");
172
173         status = smb_raw_open(cli->tree, mem_ctx, &io);
174         CHECK_STATUS(status, NT_STATUS_OK);
175         fnum = io.ntcreatex.out.fnum;
176
177         printf("querying ACL\n");
178
179         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
180         q.query_secdesc.in.fnum = fnum;
181         q.query_secdesc.in.secinfo_flags = 
182                 SECINFO_OWNER |
183                 SECINFO_GROUP |
184                 SECINFO_DACL;
185         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
186         CHECK_STATUS(status, NT_STATUS_OK);
187         sd = q.query_secdesc.out.sd;
188
189         smbcli_close(cli->tree, fnum);
190         smbcli_unlink(cli->tree, fname);
191
192         printf("adding a new ACE\n");
193         test_sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-1234-54321");
194
195         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
196         ace.flags = 0;
197         ace.access_mask = SEC_STD_ALL;
198         ace.trustee = *test_sid;
199
200         status = security_descriptor_dacl_add(sd, &ace);
201         CHECK_STATUS(status, NT_STATUS_OK);
202         
203         printf("creating a file with an initial ACL\n");
204
205         io.ntcreatex.in.sec_desc = sd;
206         status = smb_raw_open(cli->tree, mem_ctx, &io);
207         CHECK_STATUS(status, NT_STATUS_OK);
208         fnum = io.ntcreatex.out.fnum;
209         
210         q.query_secdesc.in.fnum = fnum;
211         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
212         CHECK_STATUS(status, NT_STATUS_OK);
213
214         if (!security_descriptor_equal(q.query_secdesc.out.sd, sd)) {
215                 printf("security descriptors don't match!\n");
216                 printf("got:\n");
217                 NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
218                 printf("expected:\n");
219                 NDR_PRINT_DEBUG(security_descriptor, sd);
220         }
221
222 done:
223         smbcli_close(cli->tree, fnum);
224         return ret;
225 }
226
227 #define CHECK_ACCESS_FLAGS(_fnum, flags) do { \
228         union smb_fileinfo _q; \
229         _q.access_information.level = RAW_FILEINFO_ACCESS_INFORMATION; \
230         _q.access_information.in.fnum = (_fnum); \
231         status = smb_raw_fileinfo(cli->tree, mem_ctx, &_q); \
232         CHECK_STATUS(status, NT_STATUS_OK); \
233         if (_q.access_information.out.access_flags != (flags)) { \
234                 printf("(%s) Incorrect access_flags 0x%08x - should be 0x%08x\n", \
235                        __location__, _q.access_information.out.access_flags, (flags)); \
236                 ret = False; \
237                 goto done; \
238         } \
239 } while (0)
240
241
242 /*
243   test the behaviour of the well known SID_CREATOR_OWNER sid, and some generic
244   mapping bits
245 */
246 static BOOL test_creator_sid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
247 {
248         NTSTATUS status;
249         union smb_open io;
250         const char *fname = BASEDIR "\\creator.txt";
251         BOOL ret = True;
252         int fnum = -1;
253         union smb_fileinfo q;
254         union smb_setfileinfo set;
255         struct security_descriptor *sd, *sd_orig, *sd2;
256         const char *owner_sid;
257
258         printf("TESTING SID_CREATOR_OWNER\n");
259
260         io.generic.level = RAW_OPEN_NTCREATEX;
261         io.ntcreatex.in.root_fid = 0;
262         io.ntcreatex.in.flags = 0;
263         io.ntcreatex.in.access_mask = SEC_STD_READ_CONTROL | SEC_STD_WRITE_DAC | SEC_STD_WRITE_OWNER;
264         io.ntcreatex.in.create_options = 0;
265         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
266         io.ntcreatex.in.share_access = 
267                 NTCREATEX_SHARE_ACCESS_READ | 
268                 NTCREATEX_SHARE_ACCESS_WRITE;
269         io.ntcreatex.in.alloc_size = 0;
270         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
271         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
272         io.ntcreatex.in.security_flags = 0;
273         io.ntcreatex.in.fname = fname;
274         status = smb_raw_open(cli->tree, mem_ctx, &io);
275         CHECK_STATUS(status, NT_STATUS_OK);
276         fnum = io.ntcreatex.out.fnum;
277
278         printf("get the original sd\n");
279         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
280         q.query_secdesc.in.fnum = fnum;
281         q.query_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
282         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
283         CHECK_STATUS(status, NT_STATUS_OK);
284         sd_orig = q.query_secdesc.out.sd;
285
286         owner_sid = dom_sid_string(mem_ctx, sd_orig->owner_sid);
287
288         printf("set a sec desc allowing no write by CREATOR_OWNER\n");
289         sd = security_descriptor_create(mem_ctx,
290                                         NULL, NULL,
291                                         SID_CREATOR_OWNER,
292                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
293                                         SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
294                                         NULL);
295
296         set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
297         set.set_secdesc.file.fnum = fnum;
298         set.set_secdesc.in.secinfo_flags = SECINFO_DACL;
299         set.set_secdesc.in.sd = sd;
300
301         status = smb_raw_setfileinfo(cli->tree, &set);
302         CHECK_STATUS(status, NT_STATUS_OK);
303
304         printf("try open for write\n");
305         io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
306         status = smb_raw_open(cli->tree, mem_ctx, &io);
307         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
308
309         printf("try open for read\n");
310         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
311         status = smb_raw_open(cli->tree, mem_ctx, &io);
312         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
313
314         printf("try open for generic write\n");
315         io.ntcreatex.in.access_mask = SEC_GENERIC_WRITE;
316         status = smb_raw_open(cli->tree, mem_ctx, &io);
317         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
318
319         printf("try open for generic read\n");
320         io.ntcreatex.in.access_mask = SEC_GENERIC_READ;
321         status = smb_raw_open(cli->tree, mem_ctx, &io);
322         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
323
324         printf("set a sec desc allowing no write by owner\n");
325         sd = security_descriptor_create(mem_ctx,
326                                         owner_sid, NULL,
327                                         owner_sid,
328                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
329                                         SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
330                                         NULL);
331
332         set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
333         set.set_secdesc.file.fnum = fnum;
334         set.set_secdesc.in.secinfo_flags = SECINFO_DACL;
335         set.set_secdesc.in.sd = sd;
336         status = smb_raw_setfileinfo(cli->tree, &set);
337         CHECK_STATUS(status, NT_STATUS_OK);
338
339         printf("check that sd has been mapped correctly\n");
340         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
341         CHECK_STATUS(status, NT_STATUS_OK);
342         if (!security_descriptor_equal(q.query_secdesc.out.sd, sd)) {
343                 printf("security descriptors don't match!\n");
344                 printf("got:\n");
345                 NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
346                 printf("expected:\n");
347                 NDR_PRINT_DEBUG(security_descriptor, sd);
348         }
349
350         printf("try open for write\n");
351         io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
352         status = smb_raw_open(cli->tree, mem_ctx, &io);
353         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
354
355         printf("try open for read\n");
356         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
357         status = smb_raw_open(cli->tree, mem_ctx, &io);
358         CHECK_STATUS(status, NT_STATUS_OK);
359         CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
360                            SEC_FILE_READ_DATA|
361                            SEC_FILE_READ_ATTRIBUTE);
362         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
363
364         printf("try open for generic write\n");
365         io.ntcreatex.in.access_mask = SEC_GENERIC_WRITE;
366         status = smb_raw_open(cli->tree, mem_ctx, &io);
367         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
368
369         printf("try open for generic read\n");
370         io.ntcreatex.in.access_mask = SEC_GENERIC_READ;
371         status = smb_raw_open(cli->tree, mem_ctx, &io);
372         CHECK_STATUS(status, NT_STATUS_OK);
373         CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
374                            SEC_RIGHTS_FILE_READ);
375         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
376
377         printf("set a sec desc allowing generic read by owner\n");
378         sd = security_descriptor_create(mem_ctx,
379                                         NULL, NULL,
380                                         owner_sid,
381                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
382                                         SEC_GENERIC_READ | SEC_STD_ALL,
383                                         NULL);
384
385         set.set_secdesc.in.sd = sd;
386         status = smb_raw_setfileinfo(cli->tree, &set);
387         CHECK_STATUS(status, NT_STATUS_OK);
388
389         printf("check that generic read has been mapped correctly\n");
390         sd2 = security_descriptor_create(mem_ctx,
391                                          owner_sid, NULL,
392                                          owner_sid,
393                                          SEC_ACE_TYPE_ACCESS_ALLOWED,
394                                          SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
395                                          NULL);
396
397         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
398         CHECK_STATUS(status, NT_STATUS_OK);
399         if (!security_descriptor_equal(q.query_secdesc.out.sd, sd2)) {
400                 printf("security descriptors don't match!\n");
401                 printf("got:\n");
402                 NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
403                 printf("expected:\n");
404                 NDR_PRINT_DEBUG(security_descriptor, sd2);
405         }
406         
407
408         printf("try open for write\n");
409         io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
410         status = smb_raw_open(cli->tree, mem_ctx, &io);
411         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
412
413         printf("try open for read\n");
414         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
415         status = smb_raw_open(cli->tree, mem_ctx, &io);
416         CHECK_STATUS(status, NT_STATUS_OK);
417         CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
418                            SEC_FILE_READ_DATA | 
419                            SEC_FILE_READ_ATTRIBUTE);
420         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
421
422         printf("try open for generic write\n");
423         io.ntcreatex.in.access_mask = SEC_GENERIC_WRITE;
424         status = smb_raw_open(cli->tree, mem_ctx, &io);
425         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
426
427         printf("try open for generic read\n");
428         io.ntcreatex.in.access_mask = SEC_GENERIC_READ;
429         status = smb_raw_open(cli->tree, mem_ctx, &io);
430         CHECK_STATUS(status, NT_STATUS_OK);
431         CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, SEC_RIGHTS_FILE_READ);
432         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
433
434
435         printf("put back original sd\n");
436         set.set_secdesc.in.sd = sd_orig;
437         status = smb_raw_setfileinfo(cli->tree, &set);
438         CHECK_STATUS(status, NT_STATUS_OK);
439
440
441 done:
442         smbcli_close(cli->tree, fnum);
443         return ret;
444 }
445
446
447 /*
448   test the mapping of the SEC_GENERIC_xx bits to SEC_STD_xx and
449   SEC_FILE_xx bits
450 */
451 static BOOL test_generic_bits(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
452 {
453         NTSTATUS status;
454         union smb_open io;
455         const char *fname = BASEDIR "\\generic.txt";
456         BOOL ret = True;
457         int fnum = -1, i;
458         union smb_fileinfo q;
459         union smb_setfileinfo set;
460         struct security_descriptor *sd, *sd_orig, *sd2;
461         const char *owner_sid;
462         const struct {
463                 uint32_t gen_bits;
464                 uint32_t specific_bits;
465         } file_mappings[] = {
466                 { 0,                       0 },
467                 { SEC_GENERIC_READ,        SEC_RIGHTS_FILE_READ },
468                 { SEC_GENERIC_WRITE,       SEC_RIGHTS_FILE_WRITE },
469                 { SEC_GENERIC_EXECUTE,     SEC_RIGHTS_FILE_EXECUTE },
470                 { SEC_GENERIC_ALL,         SEC_RIGHTS_FILE_ALL },
471                 { SEC_FILE_READ_DATA,      SEC_FILE_READ_DATA },
472                 { SEC_FILE_READ_ATTRIBUTE, SEC_FILE_READ_ATTRIBUTE }
473         };
474         const struct {
475                 uint32_t gen_bits;
476                 uint32_t specific_bits;
477         } dir_mappings[] = {
478                 { 0,                   0 },
479                 { SEC_GENERIC_READ,    SEC_RIGHTS_DIR_READ },
480                 { SEC_GENERIC_WRITE,   SEC_RIGHTS_DIR_WRITE },
481                 { SEC_GENERIC_EXECUTE, SEC_RIGHTS_DIR_EXECUTE },
482                 { SEC_GENERIC_ALL,     SEC_RIGHTS_DIR_ALL }
483         };
484
485         printf("TESTING FILE GENERIC BITS\n");
486
487         io.generic.level = RAW_OPEN_NTCREATEX;
488         io.ntcreatex.in.root_fid = 0;
489         io.ntcreatex.in.flags = 0;
490         io.ntcreatex.in.access_mask = 
491                 SEC_STD_READ_CONTROL | 
492                 SEC_STD_WRITE_DAC | 
493                 SEC_STD_WRITE_OWNER;
494         io.ntcreatex.in.create_options = 0;
495         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
496         io.ntcreatex.in.share_access = 
497                 NTCREATEX_SHARE_ACCESS_READ | 
498                 NTCREATEX_SHARE_ACCESS_WRITE;
499         io.ntcreatex.in.alloc_size = 0;
500         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
501         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
502         io.ntcreatex.in.security_flags = 0;
503         io.ntcreatex.in.fname = fname;
504         status = smb_raw_open(cli->tree, mem_ctx, &io);
505         CHECK_STATUS(status, NT_STATUS_OK);
506         fnum = io.ntcreatex.out.fnum;
507
508         printf("get the original sd\n");
509         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
510         q.query_secdesc.in.fnum = fnum;
511         q.query_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
512         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
513         CHECK_STATUS(status, NT_STATUS_OK);
514         sd_orig = q.query_secdesc.out.sd;
515
516         owner_sid = dom_sid_string(mem_ctx, sd_orig->owner_sid);
517
518
519         for (i=0;i<ARRAY_SIZE(file_mappings);i++) {
520
521                 printf("testing generic bits 0x%08x\n", 
522                        file_mappings[i].gen_bits);
523                 sd = security_descriptor_create(mem_ctx,
524                                                 owner_sid, NULL,
525                                                 owner_sid,
526                                                 SEC_ACE_TYPE_ACCESS_ALLOWED,
527                                                 file_mappings[i].gen_bits,
528                                                 NULL);
529
530                 set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
531                 set.set_secdesc.file.fnum = fnum;
532                 set.set_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
533                 set.set_secdesc.in.sd = sd;
534
535                 status = smb_raw_setfileinfo(cli->tree, &set);
536                 CHECK_STATUS(status, NT_STATUS_OK);
537
538                 sd2 = security_descriptor_create(mem_ctx,
539                                                  owner_sid, NULL,
540                                                  owner_sid,
541                                                  SEC_ACE_TYPE_ACCESS_ALLOWED,
542                                                  file_mappings[i].specific_bits,
543                                                  NULL);
544
545                 status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
546                 CHECK_STATUS(status, NT_STATUS_OK);
547                 if (!security_descriptor_equal(q.query_secdesc.out.sd, sd2)) {
548                         printf("security descriptors don't match!\n");
549                         printf("got:\n");
550                         NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
551                         printf("expected:\n");
552                         NDR_PRINT_DEBUG(security_descriptor, sd2);
553                 }
554
555                 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
556                 status = smb_raw_open(cli->tree, mem_ctx, &io);
557                 CHECK_STATUS(status, NT_STATUS_OK);
558                 CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
559                                    SEC_STD_READ_CONTROL | 
560                                    SEC_STD_WRITE_DAC | 
561                                    SEC_STD_DELETE | 
562                                    SEC_FILE_READ_ATTRIBUTE |
563                                    file_mappings[i].specific_bits);
564                 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
565
566
567                 printf("testing generic bits 0x%08x (anonymous)\n", 
568                        file_mappings[i].gen_bits);
569                 sd = security_descriptor_create(mem_ctx,
570                                                 SID_ANONYMOUS, NULL,
571                                                 owner_sid,
572                                                 SEC_ACE_TYPE_ACCESS_ALLOWED,
573                                                 file_mappings[i].gen_bits,
574                                                 NULL);
575
576                 set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
577                 set.set_secdesc.file.fnum = fnum;
578                 set.set_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
579                 set.set_secdesc.in.sd = sd;
580
581                 status = smb_raw_setfileinfo(cli->tree, &set);
582                 CHECK_STATUS(status, NT_STATUS_OK);
583
584                 sd2 = security_descriptor_create(mem_ctx,
585                                                  SID_ANONYMOUS, NULL,
586                                                  owner_sid,
587                                                  SEC_ACE_TYPE_ACCESS_ALLOWED,
588                                                  file_mappings[i].specific_bits,
589                                                  NULL);
590
591                 status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
592                 CHECK_STATUS(status, NT_STATUS_OK);
593                 if (!security_descriptor_equal(q.query_secdesc.out.sd, sd2)) {
594                         printf("security descriptors don't match!\n");
595                         printf("got:\n");
596                         NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
597                         printf("expected:\n");
598                         NDR_PRINT_DEBUG(security_descriptor, sd2);
599                 }
600
601                 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
602                 status = smb_raw_open(cli->tree, mem_ctx, &io);
603                 CHECK_STATUS(status, NT_STATUS_OK);
604                 CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
605                                    SEC_STD_DELETE | 
606                                    SEC_FILE_READ_ATTRIBUTE |
607                                    file_mappings[i].specific_bits);
608                 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
609         }
610
611         printf("put back original sd\n");
612         set.set_secdesc.in.sd = sd_orig;
613         status = smb_raw_setfileinfo(cli->tree, &set);
614         CHECK_STATUS(status, NT_STATUS_OK);
615
616         smbcli_close(cli->tree, fnum);
617         smbcli_unlink(cli->tree, fname);
618
619
620         printf("TESTING DIR GENERIC BITS\n");
621
622         io.generic.level = RAW_OPEN_NTCREATEX;
623         io.ntcreatex.in.root_fid = 0;
624         io.ntcreatex.in.flags = 0;
625         io.ntcreatex.in.access_mask = SEC_STD_READ_CONTROL | SEC_STD_WRITE_DAC;
626         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
627         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
628         io.ntcreatex.in.share_access = 
629                 NTCREATEX_SHARE_ACCESS_READ | 
630                 NTCREATEX_SHARE_ACCESS_WRITE;
631         io.ntcreatex.in.alloc_size = 0;
632         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
633         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
634         io.ntcreatex.in.security_flags = 0;
635         io.ntcreatex.in.fname = fname;
636         status = smb_raw_open(cli->tree, mem_ctx, &io);
637         CHECK_STATUS(status, NT_STATUS_OK);
638         fnum = io.ntcreatex.out.fnum;
639
640         printf("get the original sd\n");
641         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
642         q.query_secdesc.in.fnum = fnum;
643         q.query_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
644         status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
645         CHECK_STATUS(status, NT_STATUS_OK);
646         sd_orig = q.query_secdesc.out.sd;
647
648         owner_sid = dom_sid_string(mem_ctx, sd_orig->owner_sid);
649
650
651         for (i=0;i<ARRAY_SIZE(dir_mappings);i++) {
652
653                 printf("testing generic bits 0x%08x\n", 
654                        file_mappings[i].gen_bits);
655                 sd = security_descriptor_create(mem_ctx,
656                                                 NULL, NULL,
657                                                 owner_sid,
658                                                 SEC_ACE_TYPE_ACCESS_ALLOWED,
659                                                 dir_mappings[i].gen_bits,
660                                                 NULL);
661
662                 set.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
663                 set.set_secdesc.file.fnum = fnum;
664                 set.set_secdesc.in.secinfo_flags = SECINFO_DACL;
665                 set.set_secdesc.in.sd = sd;
666
667                 status = smb_raw_setfileinfo(cli->tree, &set);
668                 CHECK_STATUS(status, NT_STATUS_OK);
669
670                 sd2 = security_descriptor_create(mem_ctx,
671                                                  owner_sid, NULL,
672                                                  owner_sid,
673                                                  SEC_ACE_TYPE_ACCESS_ALLOWED,
674                                                  dir_mappings[i].specific_bits,
675                                                  NULL);
676
677                 status = smb_raw_fileinfo(cli->tree, mem_ctx, &q);
678                 CHECK_STATUS(status, NT_STATUS_OK);
679                 if (!security_descriptor_equal(q.query_secdesc.out.sd, sd2)) {
680                         printf("security descriptors don't match!\n");
681                         printf("got:\n");
682                         NDR_PRINT_DEBUG(security_descriptor, q.query_secdesc.out.sd);
683                         printf("expected:\n");
684                         NDR_PRINT_DEBUG(security_descriptor, sd2);
685                 }
686
687                 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
688                 status = smb_raw_open(cli->tree, mem_ctx, &io);
689                 CHECK_STATUS(status, NT_STATUS_OK);
690                 CHECK_ACCESS_FLAGS(io.ntcreatex.out.fnum, 
691                                    SEC_STD_WRITE_DAC | 
692                                    SEC_STD_READ_CONTROL | 
693                                    SEC_STD_DELETE | 
694                                    SEC_FILE_READ_ATTRIBUTE |
695                                    dir_mappings[i].specific_bits);
696                 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
697
698         }
699         printf("put back original sd\n");
700         set.set_secdesc.in.sd = sd_orig;
701         status = smb_raw_setfileinfo(cli->tree, &set);
702         CHECK_STATUS(status, NT_STATUS_OK);
703
704         smbcli_close(cli->tree, fnum);
705         smbcli_unlink(cli->tree, fname);
706
707 done:
708         smbcli_close(cli->tree, fnum);
709         return ret;
710 }
711
712
713 /* 
714    basic testing of security descriptor calls
715 */
716 BOOL torture_raw_acls(void)
717 {
718         struct smbcli_state *cli;
719         BOOL ret = True;
720         TALLOC_CTX *mem_ctx;
721
722         if (!torture_open_connection(&cli)) {
723                 return False;
724         }
725
726         mem_ctx = talloc_init("torture_raw_acls");
727
728         if (!torture_setup_dir(cli, BASEDIR)) {
729                 return False;
730         }
731
732         ret &= test_sd(cli, mem_ctx);
733         ret &= test_nttrans_create(cli, mem_ctx);
734         ret &= test_creator_sid(cli, mem_ctx);
735         ret &= test_generic_bits(cli, mem_ctx);
736
737         smb_raw_exit(cli->session);
738         smbcli_deltree(cli->tree, BASEDIR);
739
740         torture_close_connection(cli);
741         talloc_destroy(mem_ctx);
742         return ret;
743 }