r23792: convert Samba4 to GPLv3
[samba.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 "auth/credentials/credentials.h"
24 #include "libnet/libnet.h"
25 #include "libcli/security/security.h"
26 #include "librpc/ndr/libndr.h"
27 #include "librpc/gen_ndr/ndr_lsa.h"
28 #include "librpc/gen_ndr/ndr_samr.h"
29 #include "librpc/gen_ndr/ndr_srvsvc.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "torture/torture.h"
32
33
34 static BOOL test_connect_service(struct libnet_context *ctx,
35                                  const struct dcerpc_interface_table *iface,
36                                  const char *binding_string,
37                                  const char *hostname,
38                                  const enum libnet_RpcConnect_level level,
39                                  BOOL badcreds, NTSTATUS expected_status)
40 {
41         NTSTATUS status;
42         struct libnet_RpcConnect connect;
43         connect.level            = level;
44         connect.in.binding       = binding_string;
45         connect.in.name          = hostname;
46         connect.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);
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.in.dcerpc_iface->name, connect.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.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.out.domain_name);
72                 d_printf("\tDomain SID:\t %s\n", dom_sid_string(ctx, connect.out.domain_sid));
73                 d_printf("\tRealm:\t\t %s\n", connect.out.realm);
74                 d_printf("\tGUID:\t\t %s\n", GUID_string(ctx, connect.out.guid));
75
76         } else if (!NT_STATUS_IS_OK(status)) {
77                 d_printf("Error string: %s\n", connect.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(NULL);
91         ctx->cred = cmdline_credentials;
92         
93         d_printf("Testing connection to LSA interface\n");
94         if (!test_connect_service(ctx, &dcerpc_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, &dcerpc_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, &dcerpc_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, &dcerpc_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, &dcerpc_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         const char *bindstr;;
140
141         bindstr = torture_setting_string(torture, "binding", NULL);
142         status = dcerpc_parse_binding(torture, bindstr, &binding);
143         if (!NT_STATUS_IS_OK(status)) {
144                 d_printf("failed to parse binding string\n");
145                 return False;
146         }
147
148         return torture_rpc_connect(torture, level, NULL, binding->host);
149 }
150
151
152 BOOL torture_rpc_connect_pdc(struct torture_context *torture)
153 {
154         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
155         NTSTATUS status;
156         struct dcerpc_binding *binding;
157         const char *bindstr;
158         const char *domain_name;
159         
160         bindstr = torture_setting_string(torture, "binding", NULL);
161         status = dcerpc_parse_binding(torture, bindstr, &binding);
162         if (!NT_STATUS_IS_OK(status)) {
163                 d_printf("failed to parse binding string\n");
164                 return False;
165         }
166
167         /* we're accessing domain controller so the domain name should be
168            passed (it's going to be resolved to dc name and address) instead
169            of specific server name. */
170         domain_name = lp_workgroup();
171         return torture_rpc_connect(torture, level, NULL, domain_name);
172 }
173
174
175 BOOL torture_rpc_connect_dc(struct torture_context *torture)
176 {
177         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC;
178         NTSTATUS status;
179         struct dcerpc_binding *binding;
180         const char *bindstr;
181         const char *domain_name;
182         
183         bindstr = torture_setting_string(torture, "binding", NULL);
184         status = dcerpc_parse_binding(torture, bindstr, &binding);
185         if (!NT_STATUS_IS_OK(status)) {
186                 d_printf("failed to parse binding string\n");
187                 return False;
188         }
189
190         /* we're accessing domain controller so the domain name should be
191            passed (it's going to be resolved to dc name and address) instead
192            of specific server name. */
193         domain_name = lp_workgroup();
194         return torture_rpc_connect(torture, level, NULL, domain_name);
195 }
196
197
198 BOOL torture_rpc_connect_dc_info(struct torture_context *torture)
199 {
200         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC_INFO;
201         NTSTATUS status;
202         struct dcerpc_binding *binding;
203         const char *bindstr;
204         const char *domain_name;
205         
206         bindstr = torture_setting_string(torture, "binding", NULL);
207         status = dcerpc_parse_binding(torture, bindstr, &binding);
208         if (!NT_STATUS_IS_OK(status)) {
209                 d_printf("failed to parse binding string\n");
210                 return False;
211         }
212
213         /* we're accessing domain controller so the domain name should be
214            passed (it's going to be resolved to dc name and address) instead
215            of specific server name. */
216         domain_name = lp_workgroup();
217         return torture_rpc_connect(torture, level, NULL, domain_name);
218 }
219
220
221 BOOL torture_rpc_connect_binding(struct torture_context *torture)
222 {
223         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
224         NTSTATUS status;
225         struct dcerpc_binding *binding;
226         const char *bindstr;
227         
228         bindstr = torture_setting_string(torture, "binding", NULL);
229         status = dcerpc_parse_binding(torture, bindstr, &binding);
230         if (!NT_STATUS_IS_OK(status)) {
231                 d_printf("failed to parse binding string\n");
232                 return False;
233         }
234
235         return torture_rpc_connect(torture, level, bindstr, NULL);
236 }