Use torture_setting_* rather than lp_parm_* where possible.
[samba-svnmirror.git] / source / 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 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 "auth/credentials/credentials.h"
25 #include "libnet/libnet.h"
26 #include "libcli/security/security.h"
27 #include "librpc/ndr/libndr.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "librpc/gen_ndr/ndr_srvsvc.h"
31 #include "librpc/rpc/dcerpc.h"
32 #include "torture/torture.h"
33
34
35 static BOOL test_connect_service(struct libnet_context *ctx,
36                                  const struct dcerpc_interface_table *iface,
37                                  const char *binding_string,
38                                  const char *hostname,
39                                  const enum libnet_RpcConnect_level level,
40                                  BOOL badcreds, NTSTATUS expected_status)
41 {
42         NTSTATUS status;
43         struct libnet_RpcConnect connect;
44         connect.level            = level;
45         connect.in.binding       = binding_string;
46         connect.in.name          = hostname;
47         connect.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);
57
58         if (!NT_STATUS_EQUAL(status, expected_status)) {
59                 d_printf("Connecting to rpc service %s on %s.\n\tFAILED. Expected: %s."
60                        "Received: %s\n",
61                        connect.in.dcerpc_iface->name, connect.in.binding, nt_errstr(expected_status),
62                        nt_errstr(status));
63
64                 return False;
65         }
66
67         d_printf("PASSED. Expected: %s, received: %s\n", nt_errstr(expected_status),
68                nt_errstr(status));
69
70         if (connect.level == LIBNET_RPC_CONNECT_DC_INFO && NT_STATUS_IS_OK(status)) {
71                 d_printf("Domain Controller Info:\n");
72                 d_printf("\tDomain Name:\t %s\n", connect.out.domain_name);
73                 d_printf("\tDomain SID:\t %s\n", dom_sid_string(ctx, connect.out.domain_sid));
74                 d_printf("\tRealm:\t\t %s\n", connect.out.realm);
75                 d_printf("\tGUID:\t\t %s\n", GUID_string(ctx, connect.out.guid));
76
77         } else if (!NT_STATUS_IS_OK(status)) {
78                 d_printf("Error string: %s\n", connect.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(NULL);
92         ctx->cred = cmdline_credentials;
93         
94         d_printf("Testing connection to LSA interface\n");
95         if (!test_connect_service(ctx, &dcerpc_table_lsarpc, bindstr,
96                                   hostname, level, False, NT_STATUS_OK)) {
97                 d_printf("failed to connect LSA interface\n");
98                 return False;
99         }
100
101         d_printf("Testing connection to SAMR interface\n");
102         if (!test_connect_service(ctx, &dcerpc_table_samr, bindstr,
103                                   hostname, level, False, NT_STATUS_OK)) {
104                 d_printf("failed to connect SAMR interface\n");
105                 return False;
106         }
107
108         d_printf("Testing connection to SRVSVC interface\n");
109         if (!test_connect_service(ctx, &dcerpc_table_srvsvc, bindstr,
110                                   hostname, level, False, NT_STATUS_OK)) {
111                 d_printf("failed to connect SRVSVC interface\n");
112                 return False;
113         }
114
115         d_printf("Testing connection to LSA interface with wrong credentials\n");
116         if (!test_connect_service(ctx, &dcerpc_table_lsarpc, bindstr,
117                                   hostname, level, True, NT_STATUS_LOGON_FAILURE)) {
118                 d_printf("failed to test wrong credentials on LSA interface\n");
119                 return False;
120         }
121
122         d_printf("Testing connection to SAMR interface with wrong credentials\n");
123         if (!test_connect_service(ctx, &dcerpc_table_samr, bindstr,
124                                   hostname, level, True, NT_STATUS_LOGON_FAILURE)) {
125                 d_printf("failed to test wrong credentials on SAMR interface\n");
126                 return False;
127         }
128
129         talloc_free(ctx);
130
131         return True;
132 }
133
134
135 BOOL torture_rpc_connect_srv(struct torture_context *torture)
136 {
137         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_SERVER;
138         NTSTATUS status;
139         struct dcerpc_binding *binding;
140         const char *bindstr;;
141
142         bindstr = torture_setting_string(torture, "binding", NULL);
143         status = dcerpc_parse_binding(torture, bindstr, &binding);
144         if (!NT_STATUS_IS_OK(status)) {
145                 d_printf("failed to parse binding string\n");
146                 return False;
147         }
148
149         return torture_rpc_connect(torture, level, NULL, binding->host);
150 }
151
152
153 BOOL torture_rpc_connect_pdc(struct torture_context *torture)
154 {
155         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
156         NTSTATUS status;
157         struct dcerpc_binding *binding;
158         const char *bindstr;
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         return torture_rpc_connect(torture, level, NULL, binding->host);
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 *bindstr;
177         
178         bindstr = torture_setting_string(torture, "binding", NULL);
179         status = dcerpc_parse_binding(torture, bindstr, &binding);
180         if (!NT_STATUS_IS_OK(status)) {
181                 d_printf("failed to parse binding string\n");
182                 return False;
183         }
184
185         return torture_rpc_connect(torture, level, NULL, binding->host);
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 *bindstr;
195         
196         bindstr = torture_setting_string(torture, "binding", NULL);
197         status = dcerpc_parse_binding(torture, bindstr, &binding);
198         if (!NT_STATUS_IS_OK(status)) {
199                 d_printf("failed to parse binding string\n");
200                 return False;
201         }
202
203         return torture_rpc_connect(torture, level, NULL, binding->host);
204 }
205
206
207 BOOL torture_rpc_connect_binding(struct torture_context *torture)
208 {
209         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
210         NTSTATUS status;
211         struct dcerpc_binding *binding;
212         const char *bindstr;
213         
214         bindstr = torture_setting_string(torture, "binding", NULL);
215         status = dcerpc_parse_binding(torture, bindstr, &binding);
216         if (!NT_STATUS_IS_OK(status)) {
217                 d_printf("failed to parse binding string\n");
218                 return False;
219         }
220
221         return torture_rpc_connect(torture, level, bindstr, NULL);
222 }