- corrected some lsa idl
[samba.git] / source4 / torture / rpc / lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   these really shouldn't be here ....
26 */
27 static char *lsa_sid_string_talloc(TALLOC_CTX *mem_ctx, struct dom_sid *sid)
28 {
29         int i, ofs, maxlen;
30         uint32 ia;
31         char *ret;
32         
33         if (!sid) {
34                 return talloc_asprintf(mem_ctx, "(NULL SID)");
35         }
36
37         maxlen = sid->num_auths * 11 + 25;
38         ret = talloc(mem_ctx, maxlen);
39         if (!ret) return NULL;
40
41         ia = (sid->id_auth[5]) +
42                 (sid->id_auth[4] << 8 ) +
43                 (sid->id_auth[3] << 16) +
44                 (sid->id_auth[2] << 24);
45
46         ofs = snprintf(ret, maxlen, "S-%u-%lu", 
47                        (unsigned int)sid->sid_rev_num, (unsigned long)ia);
48
49         for (i = 0; i < sid->num_auths; i++) {
50                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
51         }
52
53         return ret;
54 }
55
56 static int dom_sid_compare(struct dom_sid *sid1, struct dom_sid *sid2)
57 {
58         int i;
59
60         if (sid1 == sid2) return 0;
61         if (!sid1) return -1;
62         if (!sid2) return 1;
63
64         /* Compare most likely different rids, first: i.e start at end */
65         if (sid1->num_auths != sid2->num_auths)
66                 return sid1->num_auths - sid2->num_auths;
67
68         for (i = sid1->num_auths-1; i >= 0; --i)
69                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
70                         return sid1->sub_auths[i] - sid2->sub_auths[i];
71
72         if (sid1->sid_rev_num != sid2->sid_rev_num)
73                 return sid1->sid_rev_num - sid2->sid_rev_num;
74
75         for (i = 0; i < 6; i++)
76                 if (sid1->id_auth[i] != sid2->id_auth[i])
77                         return sid1->id_auth[i] - sid2->id_auth[i];
78
79         return 0;
80 }
81
82 static BOOL test_OpenPolicy(struct dcerpc_pipe *p)
83 {
84         struct lsa_ObjectAttribute attr;
85         struct policy_handle handle;
86         struct lsa_QosInfo qos;
87         NTSTATUS status;
88
89         printf("\ntesting OpenPolicy\n");
90
91         qos.impersonation_level = 2;
92         qos.context_mode = 1;
93         qos.effective_only = 0;
94
95         attr.root_dir = NULL;
96         attr.object_name = NULL;
97         attr.attributes = 0;
98         attr.sec_desc = NULL;
99         attr.sec_qos = &qos;
100
101         status = dcerpc_lsa_OpenPolicy(p, 
102                                        "\\",
103                                        &attr,
104                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
105                                        &handle);
106         if (!NT_STATUS_IS_OK(status)) {
107                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
108                 return False;
109         }
110
111         return True;
112 }
113
114
115 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, struct policy_handle *handle)
116 {
117         struct lsa_ObjectAttribute attr;
118         struct lsa_QosInfo qos;
119         NTSTATUS status;
120
121         printf("\ntesting OpenPolicy2\n");
122
123         qos.impersonation_level = 2;
124         qos.context_mode = 1;
125         qos.effective_only = 0;
126
127         attr.root_dir = NULL;
128         attr.object_name = NULL;
129         attr.attributes = 0;
130         attr.sec_desc = NULL;
131         attr.sec_qos = &qos;
132
133         status = dcerpc_lsa_OpenPolicy2(p, 
134                                         "\\",
135                                         &attr,
136                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
137                                         handle);
138         if (!NT_STATUS_IS_OK(status)) {
139                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
140                 return False;
141         }
142
143         return True;
144 }
145
146
147 static BOOL test_EnumSids(struct dcerpc_pipe *p, 
148                           TALLOC_CTX *mem_ctx, 
149                           struct policy_handle *handle)
150 {
151         NTSTATUS status;
152         struct lsa_SidArray sids1, sids2;
153         uint32 resume_handle;
154         int i;
155
156         printf("\ntesting EnumSids\n");
157
158         resume_handle = 0;
159         status = dcerpc_lsa_EnumSids(p, mem_ctx, handle, &resume_handle, 100, &sids1);
160         if (!NT_STATUS_IS_OK(status)) {
161                 printf("EnumSids failed - %s\n", nt_errstr(status));
162                 return False;
163         }
164
165         printf("Got %d sids resume_handle=%u\n", sids1.num_sids, resume_handle);
166
167         for (i=0;i<sids1.num_sids;i++) {
168                 printf("%s\n", lsa_sid_string_talloc(mem_ctx, sids1.sids[i].sid));
169         }
170
171         if (sids1.num_sids < 3) {
172                 return True;
173         }
174         
175         printf("trying partial listing (asking for 1 at 2)\n");
176         resume_handle = 2;
177         status = dcerpc_lsa_EnumSids(p, mem_ctx, handle, &resume_handle, 1, &sids2);
178         if (!NT_STATUS_IS_OK(status)) {
179                 printf("EnumSids failed - %s\n", nt_errstr(status));
180                 return False;
181         }
182
183         if (sids2.num_sids != 1) {
184                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
185                 return False;
186         }
187
188         return True;
189 }
190
191 BOOL torture_rpc_lsa(int dummy)
192 {
193         NTSTATUS status;
194         struct dcerpc_pipe *p;
195         TALLOC_CTX *mem_ctx;
196         BOOL ret = True;
197         struct policy_handle handle;
198
199         mem_ctx = talloc_init("torture_rpc_lsa");
200
201         status = torture_rpc_connection(&p, "lsarpc");
202         if (!NT_STATUS_IS_OK(status)) {
203                 return False;
204         }
205         
206         if (!test_OpenPolicy(p)) {
207                 ret = False;
208         }
209
210         if (!test_OpenPolicy2(p, &handle)) {
211                 ret = False;
212         }
213
214         if (!test_EnumSids(p, mem_ctx, &handle)) {
215                 ret = False;
216         }
217         
218         torture_rpc_close(p);
219
220         return ret;
221 }