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