Make sure prototypes are always included, make some functions static and
[kamenim/samba.git] / source4 / torture / libnet / utils.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2007
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  * These are more general use functions shared among the tests.
23  */
24
25 #include "includes.h"
26 #include "torture/rpc/rpc.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "param/param.h"
30 #include "torture/libnet/utils.h"
31
32
33 bool test_opendomain(struct torture_context *tctx, 
34                      struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
35                      struct policy_handle *handle, struct lsa_String *domname,
36                      struct dom_sid2 *sid)
37 {
38         NTSTATUS status;
39         struct policy_handle h, domain_handle;
40         struct samr_Connect r1;
41         struct samr_LookupDomain r2;
42         struct samr_OpenDomain r3;
43         
44         torture_comment(tctx, "connecting\n");
45         
46         r1.in.system_name = 0;
47         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
48         r1.out.connect_handle = &h;
49         
50         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
51         torture_assert_ntstatus_ok(tctx, status, "Connect failed");
52         
53         r2.in.connect_handle = &h;
54         r2.in.domain_name = domname;
55
56         torture_comment(tctx, "domain lookup on %s\n", domname->string);
57
58         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
59         torture_assert_ntstatus_ok(tctx, status, "LookupDomain failed");
60
61         r3.in.connect_handle = &h;
62         r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63         r3.in.sid = r2.out.sid;
64         r3.out.domain_handle = &domain_handle;
65
66         torture_comment(tctx, "opening domain\n");
67
68         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
69         torture_assert_ntstatus_ok(tctx, status, "OpenDomain failed");
70         *handle = domain_handle;
71
72         *sid = *r2.out.sid;
73         return true;
74 }
75
76
77 bool test_user_cleanup(struct torture_context *tctx, struct dcerpc_pipe *p, 
78                        TALLOC_CTX *mem_ctx, struct policy_handle *domain_handle,
79                        const char *name)
80 {
81         NTSTATUS status;
82         struct samr_LookupNames r1;
83         struct samr_OpenUser r2;
84         struct samr_DeleteUser r3;
85         struct lsa_String names[2];
86         uint32_t rid;
87         struct policy_handle user_handle;
88
89         names[0].string = name;
90
91         r1.in.domain_handle  = domain_handle;
92         r1.in.num_names      = 1;
93         r1.in.names          = names;
94         
95         torture_comment(tctx, "user account lookup '%s'\n", name);
96
97         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
98         torture_assert_ntstatus_ok(tctx, status, "LookupNames failed");
99
100         rid = r1.out.rids.ids[0];
101         
102         r2.in.domain_handle  = domain_handle;
103         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
104         r2.in.rid            = rid;
105         r2.out.user_handle   = &user_handle;
106
107         torture_comment(tctx, "opening user account\n");
108
109         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
110         torture_assert_ntstatus_ok(tctx, status, "OpenUser failed");
111
112         r3.in.user_handle  = &user_handle;
113         r3.out.user_handle = &user_handle;
114
115         torture_comment(tctx, "deleting user account\n");
116         
117         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
118         torture_assert_ntstatus_ok(tctx, status, "DeleteUser failed");
119         
120         return true;
121 }
122
123
124 bool test_user_create(struct torture_context *tctx, 
125                       struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
126                       struct policy_handle *handle, const char *name,
127                       uint32_t *rid)
128 {
129         NTSTATUS status;
130         struct lsa_String username;
131         struct samr_CreateUser r;
132         struct policy_handle user_handle;
133         
134         username.string = name;
135         
136         r.in.domain_handle = handle;
137         r.in.account_name  = &username;
138         r.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
139         r.out.user_handle  = &user_handle;
140         r.out.rid          = rid;
141
142         torture_comment(tctx, "creating user account %s\n", name);
143
144         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
145         if (!NT_STATUS_IS_OK(status)) {
146                 printf("CreateUser failed - %s\n", nt_errstr(status));
147
148                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
149                         torture_comment(tctx, "User (%s) already exists - attempting to delete and recreate account again\n", name);
150                         if (!test_user_cleanup(tctx, p, mem_ctx, handle, name)) {
151                                 return false;
152                         }
153
154                         torture_comment(tctx, "creating user account\n");
155                         
156                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
157                         torture_assert_ntstatus_ok(tctx, status, "CreateUser failed");
158                         return true;
159                 }
160                 return false;
161         }
162
163         return true;
164 }
165
166
167 bool test_group_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
168                         struct policy_handle *domain_handle,
169                         const char *name)
170 {
171         NTSTATUS status;
172         struct samr_LookupNames r1;
173         struct samr_OpenGroup r2;
174         struct samr_DeleteDomainGroup r3;
175         struct lsa_String names[2];
176         uint32_t rid;
177         struct policy_handle group_handle;
178
179         names[0].string = name;
180
181         r1.in.domain_handle  = domain_handle;
182         r1.in.num_names      = 1;
183         r1.in.names          = names;
184         
185         printf("group account lookup '%s'\n", name);
186
187         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
188         if (!NT_STATUS_IS_OK(status)) {
189                 printf("LookupNames failed - %s\n", nt_errstr(status));
190                 return false;
191         }
192
193         rid = r1.out.rids.ids[0];
194         
195         r2.in.domain_handle  = domain_handle;
196         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
197         r2.in.rid            = rid;
198         r2.out.group_handle  = &group_handle;
199
200         printf("opening group account\n");
201
202         status = dcerpc_samr_OpenGroup(p, mem_ctx, &r2);
203         if (!NT_STATUS_IS_OK(status)) {
204                 printf("OpenGroup failed - %s\n", nt_errstr(status));
205                 return false;
206         }
207
208         r3.in.group_handle  = &group_handle;
209         r3.out.group_handle = &group_handle;
210
211         printf("deleting group account\n");
212         
213         status = dcerpc_samr_DeleteDomainGroup(p, mem_ctx, &r3);
214         if (!NT_STATUS_IS_OK(status)) {
215                 printf("DeleteGroup failed - %s\n", nt_errstr(status));
216                 return false;
217         }
218         
219         return true;
220 }
221
222
223 bool test_group_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
224                        struct policy_handle *handle, const char *name,
225                        uint32_t *rid)
226 {
227         NTSTATUS status;
228         struct lsa_String groupname;
229         struct samr_CreateDomainGroup r;
230         struct policy_handle group_handle;
231         
232         groupname.string = name;
233         
234         r.in.domain_handle  = handle;
235         r.in.name           = &groupname;
236         r.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
237         r.out.group_handle  = &group_handle;
238         r.out.rid           = rid;
239
240         printf("creating group account %s\n", name);
241
242         status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
243         if (!NT_STATUS_IS_OK(status)) {
244                 printf("CreateGroup failed - %s\n", nt_errstr(status));
245
246                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
247                         printf("Group (%s) already exists - attempting to delete and recreate account again\n", name);
248                         if (!test_group_cleanup(p, mem_ctx, handle, name)) {
249                                 return false;
250                         }
251
252                         printf("creating group account\n");
253                         
254                         status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
255                         if (!NT_STATUS_IS_OK(status)) {
256                                 printf("CreateGroup failed - %s\n", nt_errstr(status));
257                                 return false;
258                         }
259                         return true;
260                 }
261                 return false;
262         }
263
264         return true;
265 }
266
267
268 void msg_handler(struct monitor_msg *m)
269 {
270         struct msg_rpc_open_user *msg_open;
271         struct msg_rpc_query_user *msg_query;
272         struct msg_rpc_close_user *msg_close;
273         struct msg_rpc_create_user *msg_create;
274
275         switch (m->type) {
276         case mon_SamrOpenUser:
277                 msg_open = (struct msg_rpc_open_user*)m->data;
278                 printf("monitor_msg: user opened (rid=%d, access_mask=0x%08x)\n",
279                        msg_open->rid, msg_open->access_mask);
280                 break;
281         case mon_SamrQueryUser:
282                 msg_query = (struct msg_rpc_query_user*)m->data;
283                 printf("monitor_msg: user queried (level=%d)\n", msg_query->level);
284                 break;
285         case mon_SamrCloseUser:
286                 msg_close = (struct msg_rpc_close_user*)m->data;
287                 printf("monitor_msg: user closed (rid=%d)\n", msg_close->rid);
288                 break;
289         case mon_SamrCreateUser:
290                 msg_create = (struct msg_rpc_create_user*)m->data;
291                 printf("monitor_msg: user created (rid=%d)\n", msg_create->rid);
292                 break;
293         }
294 }