r12608: Remove some unused #include lines.
[kai/samba.git] / source4 / torture / libnet / libnet_user.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 "lib/cmdline/popt_common.h"
24 #include "libnet/libnet.h"
25
26
27 #define TEST_USERNAME  "libnetusertest"
28
29
30 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
31                          struct policy_handle *domain_handle, const char *username)
32 {
33         NTSTATUS status;
34         struct samr_LookupNames r1;
35         struct samr_OpenUser r2;
36         struct samr_DeleteUser r3;
37         struct samr_Close r4;
38         struct lsa_String names[2];
39         uint32_t rid;
40         struct policy_handle user_handle;
41
42         names[0].string = username;
43
44         r1.in.domain_handle  = domain_handle;
45         r1.in.num_names      = 1;
46         r1.in.names          = names;
47         
48         printf("user account lookup '%s'\n", username);
49
50         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
51         if (!NT_STATUS_IS_OK(status)) {
52                 printf("LookupNames failed - %s\n", nt_errstr(status));
53                 return False;
54         }
55
56         rid = r1.out.rids.ids[0];
57         
58         r2.in.domain_handle  = domain_handle;
59         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
60         r2.in.rid            = rid;
61         r2.out.user_handle   = &user_handle;
62
63         printf("opening user account\n");
64
65         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
66         if (!NT_STATUS_IS_OK(status)) {
67                 printf("OpenUser failed - %s\n", nt_errstr(status));
68                 return False;
69         }
70
71         r3.in.user_handle  = &user_handle;
72         r3.out.user_handle = &user_handle;
73
74         printf("deleting user account\n");
75         
76         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
77         if (!NT_STATUS_IS_OK(status)) {
78                 printf("DeleteUser failed - %s\n", nt_errstr(status));
79                 return False;
80         }
81
82         r4.in.handle = domain_handle;
83         r4.out.handle = domain_handle;
84
85         status = dcerpc_samr_Close(p, mem_ctx, &r4);
86         if (!NT_STATUS_IS_OK(status)) {
87                 printf("Close failed - %s\n", nt_errstr(status));
88                 return False;
89         }
90         
91         return True;
92 }
93
94
95 BOOL torture_createuser(void)
96 {
97         NTSTATUS status;
98         const char *binding;
99         TALLOC_CTX *mem_ctx;
100         struct libnet_context *ctx;
101         struct libnet_CreateUser req;
102
103         mem_ctx = talloc_init("test_createuser");
104         binding = lp_parm_string(-1, "torture", "binding");
105
106         ctx = libnet_context_init(NULL);
107         ctx->cred = cmdline_credentials;
108
109         req.in.user_name = TEST_USERNAME;
110         req.in.domain_name = lp_workgroup();
111
112         status = libnet_CreateUser(ctx, mem_ctx, &req);
113         if (!NT_STATUS_IS_OK(status)) {
114                 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
115                 return False;
116         }
117
118         if (!test_cleanup(ctx->pipe, mem_ctx, &ctx->domain_handle, TEST_USERNAME)) {
119                 printf("cleanup failed\n");
120                 return False;
121         }
122
123         return True;
124 }