s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[garming/samba-autobuild/.git] / source4 / torture / libnet / libnet_rpc.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 "lib/cmdline/popt_common.h"
23 #include "libnet/libnet.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/ndr_lsa.h"
26 #include "librpc/gen_ndr/ndr_samr.h"
27 #include "librpc/gen_ndr/ndr_srvsvc.h"
28 #include "torture/rpc/torture_rpc.h"
29 #include "param/param.h"
30
31
32 static bool test_connect_service(struct torture_context *tctx,
33                                  struct libnet_context *ctx,
34                                  const struct ndr_interface_table *iface,
35                                  const char *binding_string,
36                                  const char *hostname,
37                                  const enum libnet_RpcConnect_level level,
38                                  bool badcreds, NTSTATUS expected_status)
39 {
40         NTSTATUS status;
41         struct libnet_RpcConnect connect_r;
42         ZERO_STRUCT(connect_r);
43
44         connect_r.level            = level;
45         connect_r.in.binding       = binding_string;
46         connect_r.in.name          = hostname;
47         connect_r.in.dcerpc_iface  = iface;
48
49         /* if bad credentials are needed, set baduser%badpassword instead
50            of default commandline-passed credentials */
51         if (badcreds) {
52                 cli_credentials_set_username(ctx->cred, "baduser", CRED_SPECIFIED);
53                 cli_credentials_set_password(ctx->cred, "badpassword", CRED_SPECIFIED);
54         }
55
56         status = libnet_RpcConnect(ctx, ctx, &connect_r);
57
58         if (!NT_STATUS_EQUAL(status, expected_status)) {
59                 torture_comment(tctx, "Connecting to rpc service %s on %s.\n\tFAILED. Expected: %s."
60                        "Received: %s\n",
61                        connect_r.in.dcerpc_iface->name, connect_r.in.binding, nt_errstr(expected_status),
62                        nt_errstr(status));
63
64                 return false;
65         }
66
67         torture_comment(tctx, "PASSED. Expected: %s, received: %s\n", nt_errstr(expected_status),
68                nt_errstr(status));
69
70         if (connect_r.level == LIBNET_RPC_CONNECT_DC_INFO && NT_STATUS_IS_OK(status)) {
71                 torture_comment(tctx, "Domain Controller Info:\n");
72                 torture_comment(tctx, "\tDomain Name:\t %s\n", connect_r.out.domain_name);
73                 torture_comment(tctx, "\tDomain SID:\t %s\n", dom_sid_string(ctx, connect_r.out.domain_sid));
74                 torture_comment(tctx, "\tRealm:\t\t %s\n", connect_r.out.realm);
75                 torture_comment(tctx, "\tGUID:\t\t %s\n", GUID_string(ctx, connect_r.out.guid));
76
77         } else if (!NT_STATUS_IS_OK(status)) {
78                 torture_comment(tctx, "Error string: %s\n", connect_r.out.error_string);
79         }
80
81         return true;
82 }
83
84
85 static bool torture_rpc_connect(struct torture_context *torture,
86                                 const enum libnet_RpcConnect_level level,
87                                 const char *bindstr, const char *hostname)
88 {
89         struct libnet_context *ctx;
90
91         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
92         ctx->cred = cmdline_credentials;
93
94         torture_comment(torture, "Testing connection to LSA interface\n");
95
96         if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
97                                   hostname, level, false, NT_STATUS_OK)) {
98                 torture_comment(torture, "failed to connect LSA interface\n");
99                 return false;
100         }
101
102         torture_comment(torture, "Testing connection to SAMR interface\n");
103         if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
104                                   hostname, level, false, NT_STATUS_OK)) {
105                 torture_comment(torture, "failed to connect SAMR interface\n");
106                 return false;
107         }
108
109         torture_comment(torture, "Testing connection to SRVSVC interface\n");
110         if (!test_connect_service(torture, ctx, &ndr_table_srvsvc, bindstr,
111                                   hostname, level, false, NT_STATUS_OK)) {
112                 torture_comment(torture, "failed to connect SRVSVC interface\n");
113                 return false;
114         }
115
116         torture_comment(torture, "Testing connection to LSA interface with wrong credentials\n");
117         if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
118                                   hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
119                 torture_comment(torture, "failed to test wrong credentials on LSA interface\n");
120                 return false;
121         }
122
123         torture_comment(torture, "Testing connection to SAMR interface with wrong credentials\n");
124         if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
125                                   hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
126                 torture_comment(torture, "failed to test wrong credentials on SAMR interface\n");
127                 return false;
128         }
129
130         talloc_free(ctx);
131
132         return true;
133 }
134
135
136 bool torture_rpc_connect_srv(struct torture_context *torture)
137 {
138         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_SERVER;
139         NTSTATUS status;
140         struct dcerpc_binding *binding;
141
142         status = torture_rpc_binding(torture, &binding);
143         if (!NT_STATUS_IS_OK(status)) {
144                 return false;
145         }
146
147         return torture_rpc_connect(torture, level, NULL, binding->host);
148 }
149
150
151 bool torture_rpc_connect_pdc(struct torture_context *torture)
152 {
153         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
154         NTSTATUS status;
155         struct dcerpc_binding *binding;
156         const char *domain_name;
157
158         status = torture_rpc_binding(torture, &binding);
159         if (!NT_STATUS_IS_OK(status)) {
160                 return false;
161         }
162
163         /* we're accessing domain controller so the domain name should be
164            passed (it's going to be resolved to dc name and address) instead
165            of specific server name. */
166         domain_name = lpcfg_workgroup(torture->lp_ctx);
167         return torture_rpc_connect(torture, level, NULL, domain_name);
168 }
169
170
171 bool torture_rpc_connect_dc(struct torture_context *torture)
172 {
173         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC;
174         NTSTATUS status;
175         struct dcerpc_binding *binding;
176         const char *domain_name;
177
178         status = torture_rpc_binding(torture, &binding);
179         if (!NT_STATUS_IS_OK(status)) {
180                 return false;
181         }
182
183         /* we're accessing domain controller so the domain name should be
184            passed (it's going to be resolved to dc name and address) instead
185            of specific server name. */
186         domain_name = lpcfg_workgroup(torture->lp_ctx);
187         return torture_rpc_connect(torture, level, NULL, domain_name);
188 }
189
190
191 bool torture_rpc_connect_dc_info(struct torture_context *torture)
192 {
193         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC_INFO;
194         NTSTATUS status;
195         struct dcerpc_binding *binding;
196         const char *domain_name;
197
198         status = torture_rpc_binding(torture, &binding);
199         if (!NT_STATUS_IS_OK(status)) {
200                 return false;
201         }
202
203         /* we're accessing domain controller so the domain name should be
204            passed (it's going to be resolved to dc name and address) instead
205            of specific server name. */
206         domain_name = lpcfg_workgroup(torture->lp_ctx);
207         return torture_rpc_connect(torture, level, NULL, domain_name);
208 }
209
210
211 bool torture_rpc_connect_binding(struct torture_context *torture)
212 {
213         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
214         NTSTATUS status;
215         struct dcerpc_binding *binding;
216         const char *bindstr;
217
218         status = torture_rpc_binding(torture, &binding);
219         if (!NT_STATUS_IS_OK(status)) {
220                 return false;
221         }
222
223         bindstr = dcerpc_binding_string(torture, binding);
224
225         return torture_rpc_connect(torture, level, bindstr, NULL);
226 }