r3830: unified the query/set security descriptor code with the rest of the
[jelmer/samba4-debian.git] / source / 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;
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_RIGHT_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                 OWNER_SECURITY_INFORMATION | 
75                 GROUP_SECURITY_INFORMATION | 
76                 DACL_SECURITY_INFORMATION;
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 = STD_RIGHT_ALL_ACCESS;
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    basic testing of security descriptor calls
139 */
140 BOOL torture_raw_acls(void)
141 {
142         struct smbcli_state *cli;
143         BOOL ret = True;
144         TALLOC_CTX *mem_ctx;
145
146         if (!torture_open_connection(&cli)) {
147                 return False;
148         }
149
150         mem_ctx = talloc_init("torture_raw_acls");
151
152         if (!torture_setup_dir(cli, BASEDIR)) {
153                 return False;
154         }
155
156         ret &= test_sd(cli, mem_ctx);
157
158         smb_raw_exit(cli->session);
159         smbcli_deltree(cli->tree, BASEDIR);
160
161         torture_close_connection(cli);
162         talloc_destroy(mem_ctx);
163         return ret;
164 }