r6674: Fix some compiler warnings.
[samba.git] / source / torture / libnet / userman.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2005
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 #include "librpc/gen_ndr/ndr_samr.h"
24 #include "libnet/composite.h"
25
26 #define TEST_USERNAME  "libnetusermantest"
27
28
29 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
30                             struct policy_handle *handle, struct samr_String *domname)
31 {
32         NTSTATUS status;
33         struct policy_handle h, domain_handle;
34         struct samr_Connect r1;
35         struct samr_LookupDomain r2;
36         struct samr_OpenDomain r3;
37         
38         printf("connecting\n");
39         
40         r1.in.system_name = 0;
41         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
42         r1.out.connect_handle = &h;
43         
44         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
45         if (!NT_STATUS_IS_OK(status)) {
46                 printf("Connect failed - %s\n", nt_errstr(status));
47                 return False;
48         }
49         
50         r2.in.connect_handle = &h;
51         r2.in.domain_name = domname;
52
53         printf("domain lookup on %s\n", domname->string);
54
55         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
56         if (!NT_STATUS_IS_OK(status)) {
57                 printf("LookupDomain failed - %s\n", nt_errstr(status));
58                 return False;
59         }
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         printf("opening domain\n");
67
68         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
69         if (!NT_STATUS_IS_OK(status)) {
70                 printf("OpenDomain failed - %s\n", nt_errstr(status));
71                 return False;
72         } else {
73                 *handle = domain_handle;
74         }
75
76         return True;
77 }
78
79
80 static BOOL test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
81                          struct policy_handle *domain_handle,
82                          const char *name)
83 {
84         NTSTATUS status;
85         BOOL ret = True;
86         struct rpc_composite_useradd user;
87         
88         user.in.domain_handle = *domain_handle;
89         user.in.username      = name;
90
91         status = rpc_composite_useradd(p, mem_ctx, &user);
92         if (!NT_STATUS_IS_OK(status)) {
93                 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
94                 return False;
95         }
96         
97         return ret;
98 }
99
100
101 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
102                          struct policy_handle *domain_handle, const char *username)
103 {
104         NTSTATUS status;
105         struct samr_LookupNames r1;
106         struct samr_OpenUser r2;
107         struct samr_DeleteUser r3;
108         struct samr_String names[2];
109         uint32_t rid;
110         struct policy_handle user_handle;
111
112         names[0].string = username;
113
114         r1.in.domain_handle  = domain_handle;
115         r1.in.num_names      = 1;
116         r1.in.names          = names;
117         
118         printf("user account lookup '%s'\n", username);
119
120         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
121         if (!NT_STATUS_IS_OK(status)) {
122                 printf("LookupNames failed - %s\n", nt_errstr(status));
123                 return False;
124         }
125
126         rid = r1.out.rids.ids[0];
127         
128         r2.in.domain_handle  = domain_handle;
129         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
130         r2.in.rid            = rid;
131         r2.out.user_handle   = &user_handle;
132
133         printf("opening user account\n");
134
135         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
136         if (!NT_STATUS_IS_OK(status)) {
137                 printf("OpenUser failed - %s\n", nt_errstr(status));
138                 return False;
139         }
140
141         r3.in.user_handle  = &user_handle;
142         r3.out.user_handle = &user_handle;
143
144         printf("deleting user account\n");
145         
146         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
147         if (!NT_STATUS_IS_OK(status)) {
148                 printf("DeleteUser failed - %s\n", nt_errstr(status));
149                 return False;
150         }
151         
152         return True;
153 }
154
155
156 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
157                             struct policy_handle *handle, const char* user)
158 {
159         NTSTATUS status;
160         struct policy_handle user_handle;
161         struct samr_String username;
162         struct samr_CreateUser r1;
163         struct samr_Close r2;
164         uint32_t user_rid;
165
166         username.string = user;
167         
168         r1.in.domain_handle = handle;
169         r1.in.account_name = &username;
170         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
171         r1.out.user_handle = &user_handle;
172         r1.out.rid = &user_rid;
173
174         printf("creating user '%s'\n", username.string);
175         
176         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
177         if (!NT_STATUS_IS_OK(status)) {
178                 printf("CreateUser failed - %s\n", nt_errstr(status));
179
180                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
181                         printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
182                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
183                                 return False;
184                         }
185
186                         printf("creating user account\n");
187                         
188                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
189                         if (!NT_STATUS_IS_OK(status)) {
190                                 printf("CreateUser failed - %s\n", nt_errstr(status));
191                                 return False;
192                         }
193                         return True;
194                 }               
195                 return False;
196         }
197
198         r2.in.handle = &user_handle;
199         r2.out.handle = &user_handle;
200         
201         printf("closing user '%s'\n", username.string);
202
203         status = dcerpc_samr_Close(p, mem_ctx, &r2);
204         if (!NT_STATUS_IS_OK(status)) {
205                 printf("Close failed - %s\n", nt_errstr(status));
206                 return False;
207         }
208
209         return True;
210 }
211
212
213 static BOOL test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
214                          struct policy_handle *handle, const char *username)
215 {
216         NTSTATUS status;
217         struct rpc_composite_userdel user;
218         
219         user.in.domain_handle = *handle;
220         user.in.username = username;
221         
222         status = rpc_composite_userdel(p, mem_ctx, &user);
223         if (!NT_STATUS_IS_OK(status)) {
224                 printf("Failed to call sync rpc_composite_userdel - %s\n", nt_errstr(status));
225                 return False;
226         }
227
228         return True;
229 }
230
231
232 BOOL torture_useradd(void)
233 {
234         NTSTATUS status;
235         const char *binding;
236         struct dcerpc_pipe *p;
237         struct policy_handle h;
238         struct samr_String domain_name;
239         char* name = TEST_USERNAME;
240         TALLOC_CTX *mem_ctx;
241         BOOL ret = True;
242
243         mem_ctx = talloc_init("test_useradd");
244         binding = lp_parm_string(-1, "torture", "binding");
245
246         status = torture_rpc_connection(mem_ctx, 
247                                         &p,
248                                         DCERPC_SAMR_NAME,
249                                         DCERPC_SAMR_UUID,
250                                         DCERPC_SAMR_VERSION);
251         
252         if (!NT_STATUS_IS_OK(status)) {
253                 return False;
254         }
255
256         domain_name.string = lp_workgroup();
257         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
258                 ret = False;
259                 goto done;
260         }
261
262         if (!test_useradd(p, mem_ctx, &h, name)) {
263                 ret = False;
264                 goto done;
265         }
266
267         if (!test_cleanup(p, mem_ctx, &h, name)) {
268                 ret = False;
269                 goto done;
270         }
271
272 done:
273         talloc_free(mem_ctx);
274         return ret;
275 }
276
277
278 BOOL torture_userdel(void)
279 {
280         NTSTATUS status;
281         const char *binding;
282         struct dcerpc_pipe *p;
283         struct policy_handle h;
284         struct samr_String domain_name;
285         char* name = TEST_USERNAME;
286         TALLOC_CTX *mem_ctx;
287         BOOL ret = True;
288
289         mem_ctx = talloc_init("test_userdel");
290         binding = lp_parm_string(-1, "torture", "binding");
291
292         status = torture_rpc_connection(mem_ctx, 
293                                         &p,
294                                         DCERPC_SAMR_NAME,
295                                         DCERPC_SAMR_UUID,
296                                         DCERPC_SAMR_VERSION);
297         
298         if (!NT_STATUS_IS_OK(status)) {
299                 return False;
300         }
301
302         domain_name.string = lp_workgroup();
303         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
304                 ret = False;
305                 goto done;
306         }
307
308         if (!test_createuser(p, mem_ctx, &h, name)) {
309                 ret = False;
310                 goto done;
311         }
312         
313         if (!test_userdel(p, mem_ctx, &h, name)) {
314                 ret = False;
315                 goto done;
316         }
317         
318 done:
319         talloc_free(mem_ctx);
320         return ret;
321 }