2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2005
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.
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.
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.
23 #include "librpc/gen_ndr/ndr_samr.h"
24 #include "libnet/composite.h"
26 #define TEST_USERNAME "libnetuserinfotest"
28 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
29 struct policy_handle *handle, struct samr_String *domname,
33 struct policy_handle h, domain_handle;
34 struct samr_Connect r1;
35 struct samr_LookupDomain r2;
36 struct samr_OpenDomain r3;
38 printf("connecting\n");
40 r1.in.system_name = 0;
41 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
42 r1.out.connect_handle = &h;
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));
50 r2.in.connect_handle = &h;
51 r2.in.domain_name = domname;
53 printf("domain lookup on %s\n", domname->string);
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));
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;
66 printf("opening domain\n");
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));
73 *handle = domain_handle;
81 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
82 struct policy_handle *domain_handle, const char *username)
85 struct samr_LookupNames r1;
86 struct samr_OpenUser r2;
87 struct samr_DeleteUser r3;
88 struct samr_String names[2];
90 struct policy_handle user_handle;
92 names[0].string = username;
94 r1.in.domain_handle = domain_handle;
98 printf("user account lookup '%s'\n", username);
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));
106 rid = r1.out.rids.ids[0];
108 r2.in.domain_handle = domain_handle;
109 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
111 r2.out.user_handle = &user_handle;
113 printf("opening user account\n");
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));
121 r3.in.user_handle = &user_handle;
122 r3.out.user_handle = &user_handle;
124 printf("deleting user account\n");
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));
136 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
137 struct policy_handle *handle, const char *name, uint32_t *rid)
140 struct samr_String username;
141 struct samr_CreateUser r;
142 struct policy_handle user_handle;
144 username.string = name;
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;
152 printf("creating user account %s\n", name);
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));
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)) {
164 printf("creating user account\n");
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));
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,
186 struct rpc_composite_userinfo user;
187 struct dom_sid *user_sid;
189 user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
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 */
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));
206 BOOL torture_userinfo(void)
210 struct dcerpc_pipe *p;
211 struct dcerpc_binding *b;
214 struct policy_handle h;
215 struct samr_String name;
219 mem_ctx = talloc_init("test_userinfo");
220 binding = lp_parm_string(-1, "torture", "binding");
222 status = torture_rpc_connection(mem_ctx,
226 DCERPC_SAMR_VERSION);
228 if (!NT_STATUS_IS_OK(status)) {
232 name.string = lp_workgroup();
234 if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
239 if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
244 if (!test_userinfo(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
249 if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
254 talloc_free(mem_ctx);