b53965b91ec385c168a02b777517dc8c74066a41
[samba.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 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 #include "includes.h"
22 #include "torture/rpc/rpc.h"
23 #include "libnet/libnet.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "param/param.h"
27 #include "torture/libnet/utils.h"
28
29
30 #define TEST_USERNAME  "libnetuserinfotest"
31
32
33 static BOOL test_userinfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
34                           struct policy_handle *domain_handle,
35                           struct dom_sid2 *domain_sid, const char* user_name,
36                           uint32_t *rid)
37 {
38         const uint16_t level = 5;
39         NTSTATUS status;
40         struct libnet_rpc_userinfo user;
41         struct dom_sid *user_sid;
42         
43         user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
44         
45         user.in.domain_handle = *domain_handle;
46         user.in.sid           = dom_sid_string(mem_ctx, user_sid);
47         user.in.level         = level;       /* this should be extended */
48
49         printf("Testing sync libnet_rpc_userinfo (SID argument)\n");
50         status = libnet_rpc_userinfo(p, mem_ctx, &user);
51         if (!NT_STATUS_IS_OK(status)) {
52                 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
53                 return False;
54         }
55
56         ZERO_STRUCT(user);
57
58         user.in.domain_handle = *domain_handle;
59         user.in.sid           = NULL;
60         user.in.username      = TEST_USERNAME;
61         user.in.level         = level;
62
63         printf("Testing sync libnet_rpc_userinfo (username argument)\n");
64         status = libnet_rpc_userinfo(p, mem_ctx, &user);
65         if (!NT_STATUS_IS_OK(status)) {
66                 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
67                 return False;
68         }
69
70         return True;
71 }
72
73
74 static BOOL test_userinfo_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
75                                 struct policy_handle *domain_handle,
76                                 struct dom_sid2 *domain_sid, const char* user_name,
77                                 uint32_t *rid)
78 {
79         const uint16_t level = 10;
80         NTSTATUS status;
81         struct composite_context *c;
82         struct libnet_rpc_userinfo user;
83         struct dom_sid *user_sid;
84
85         user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
86
87         user.in.domain_handle = *domain_handle;
88         user.in.sid           = dom_sid_string(mem_ctx, user_sid);
89         user.in.level         = level;       /* this should be extended */
90
91         printf("Testing async libnet_rpc_userinfo (SID argument)\n");
92
93         c = libnet_rpc_userinfo_send(p, &user, msg_handler);
94         if (!c) {
95                 printf("Failed to call sync libnet_rpc_userinfo_send\n");
96                 return False;
97         }
98
99         status = libnet_rpc_userinfo_recv(c, mem_ctx, &user);
100         if (!NT_STATUS_IS_OK(status)) {
101                 printf("Calling async libnet_rpc_userinfo failed - %s\n", nt_errstr(status));
102                 return False;
103         }
104
105         ZERO_STRUCT(user);
106
107         user.in.domain_handle = *domain_handle;
108         user.in.sid           = NULL;
109         user.in.username      = TEST_USERNAME;
110         user.in.level         = level;
111
112         printf("Testing async libnet_rpc_userinfo (username argument)\n");
113
114         c = libnet_rpc_userinfo_send(p, &user, msg_handler);
115         if (!c) {
116                 printf("Failed to call sync libnet_rpc_userinfo_send\n");
117                 return False;
118         }
119
120         status = libnet_rpc_userinfo_recv(c, mem_ctx, &user);
121         if (!NT_STATUS_IS_OK(status)) {
122                 printf("Calling async libnet_rpc_userinfo failed - %s\n", nt_errstr(status));
123                 return False;
124         }
125
126         return True;
127 }
128
129
130 bool torture_userinfo(struct torture_context *torture)
131 {
132         NTSTATUS status;
133         struct dcerpc_pipe *p;
134         TALLOC_CTX *mem_ctx;
135         BOOL ret = True;
136         struct policy_handle h;
137         struct lsa_String name;
138         struct dom_sid2 sid;
139         uint32_t rid;
140
141         mem_ctx = talloc_init("test_userinfo");
142
143         status = torture_rpc_connection(torture, 
144                                         &p,
145                                         &ndr_table_samr);
146         
147         if (!NT_STATUS_IS_OK(status)) {
148                 return False;
149         }
150
151         name.string = lp_workgroup();
152
153         /*
154          * Testing synchronous version
155          */
156         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
157                 ret = False;
158                 goto done;
159         }
160
161         if (!test_user_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
162                 ret = False;
163                 goto done;
164         }
165
166         if (!test_userinfo(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
167                 ret = False;
168                 goto done;
169         }
170
171         if (!test_user_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
172                 ret = False;
173                 goto done;
174         }
175
176         /*
177          * Testing asynchronous version and monitor messages
178          */
179         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
180                 ret = False;
181                 goto done;
182         }
183
184         if (!test_user_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
185                 ret = False;
186                 goto done;
187         }
188
189         if (!test_userinfo_async(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
190                 ret = False;
191                 goto done;
192         }
193
194         if (!test_user_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
195                 ret = False;
196                 goto done;
197         }
198
199 done:
200         talloc_free(mem_ctx);
201
202         return ret;
203 }