r6432: Restorin previous construction of conditions after a little discussion
[sfrench/samba-autobuild/.git] / source4 / torture / libnet / userinfo.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  "libnetuserinfotest"
27
28 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
29                             struct policy_handle *handle, struct samr_String *domname,
30                             struct dom_sid2 *sid)
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         *sid = *r2.out.sid;
77         return True;
78 }
79
80
81 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
82                          struct policy_handle *domain_handle, const char *username)
83 {
84         NTSTATUS status;
85         struct samr_LookupNames r1;
86         struct samr_OpenUser r2;
87         struct samr_DeleteUser r3;
88         struct samr_String names[2];
89         uint32_t rid;
90         struct policy_handle user_handle;
91
92         names[0].string = username;
93
94         r1.in.domain_handle  = domain_handle;
95         r1.in.num_names      = 1;
96         r1.in.names          = names;
97         
98         printf("user account lookup '%s'\n", username);
99
100         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
101         if (!NT_STATUS_IS_OK(status)) {
102                 printf("LookupNames failed - %s\n", nt_errstr(status));
103                 return False;
104         }
105
106         rid = r1.out.rids.ids[0];
107         
108         r2.in.domain_handle  = domain_handle;
109         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
110         r2.in.rid            = rid;
111         r2.out.user_handle   = &user_handle;
112
113         printf("opening user account\n");
114
115         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
116         if (!NT_STATUS_IS_OK(status)) {
117                 printf("OpenUser failed - %s\n", nt_errstr(status));
118                 return False;
119         }
120
121         r3.in.user_handle  = &user_handle;
122         r3.out.user_handle = &user_handle;
123
124         printf("deleting user account\n");
125         
126         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
127         if (!NT_STATUS_IS_OK(status)) {
128                 printf("DeleteUser failed - %s\n", nt_errstr(status));
129                 return False;
130         }
131         
132         return True;
133 }
134
135
136 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
137                         struct policy_handle *handle, const char *name, uint32_t *rid)
138 {
139         NTSTATUS status;
140         struct samr_String username;
141         struct samr_CreateUser r;
142         struct policy_handle user_handle;
143         
144         username.string = name;
145         
146         r.in.domain_handle = handle;
147         r.in.account_name  = &username;
148         r.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
149         r.out.user_handle  = &user_handle;
150         r.out.rid          = rid;
151
152         printf("creating user account %s\n", name);
153
154         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
155         if (!NT_STATUS_IS_OK(status)) {
156                 printf("CreateUser failed - %s\n", nt_errstr(status));
157
158                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
159                         printf("User (%s) already exists - attempting to delete and recreate account again\n", name);
160                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
161                                 return False;
162                         }
163
164                         printf("creating user account\n");
165                         
166                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
167                         if (!NT_STATUS_IS_OK(status)) {
168                                 printf("CreateUser failed - %s\n", nt_errstr(status));
169                                 return False;
170                         }
171                         return True;
172                 }
173                 return False;
174         }
175
176         return True;
177 }
178
179
180 static BOOL test_userinfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
181                           struct policy_handle *domain_handle,
182                           struct dom_sid2 *domain_sid, const char* user_name,
183                           uint32_t *rid)
184 {
185         NTSTATUS status;
186         struct rpc_composite_userinfo user;
187         struct dom_sid *user_sid;
188
189         user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
190
191         user.in.domain_handle = *domain_handle;
192         user.in.sid           = dom_sid_string(mem_ctx, user_sid);
193         user.in.level         = 5;       /* this should be extended */
194
195         printf("Testing sync rpc_composite_userinfo\n");
196         status = rpc_composite_userinfo(p, mem_ctx, &user);
197         if (!NT_STATUS_IS_OK(status)) {
198                 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
199                 return False;
200         }
201
202         return True;
203 }
204
205
206 BOOL torture_userinfo(void)
207 {
208         NTSTATUS status;
209         const char *binding;
210         struct dcerpc_pipe *p;
211         struct dcerpc_binding *b;
212         TALLOC_CTX *mem_ctx;
213         BOOL ret = True;
214         struct policy_handle h;
215         struct samr_String name;
216         struct dom_sid2 sid;
217         uint32_t rid;
218
219         mem_ctx = talloc_init("test_userinfo");
220         binding = lp_parm_string(-1, "torture", "binding");
221
222         status = torture_rpc_connection(mem_ctx, 
223                                         &p,
224                                         DCERPC_SAMR_NAME,
225                                         DCERPC_SAMR_UUID,
226                                         DCERPC_SAMR_VERSION);
227         
228         if (!NT_STATUS_IS_OK(status)) {
229                 return False;
230         }
231
232         name.string = lp_workgroup();
233
234         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
235                 ret = False;
236                 goto done;
237         }
238
239         if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
240                 ret = False;
241                 goto done;
242         }
243
244         if (!test_userinfo(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
245                 ret = False;
246                 goto done;
247         }
248
249         if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
250                 ret = False;
251                 goto done;
252         }
253 done:
254         talloc_free(mem_ctx);
255
256         return ret;
257 }