summaryrefslogtreecommitdiff
path: root/source4/torture/libnet/libnet_rpc.c
blob: 4a394c58d297e0c0a368e54a76d3315068ed2a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* 
   Unix SMB/CIFS implementation.
   Test suite for libnet calls.

   Copyright (C) Rafal Szczesniak 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/cmdline/popt_common.h"
#include "auth/credentials/credentials.h"
#include "libnet/libnet.h"
#include "libcli/security/security.h"
#include "librpc/ndr/libndr.h"
#include "librpc/gen_ndr/ndr_lsa.h"
#include "librpc/gen_ndr/ndr_samr.h"
#include "librpc/gen_ndr/ndr_srvsvc.h"
#include "librpc/rpc/dcerpc.h"
#include "torture/torture.h"


static BOOL test_connect_service(struct libnet_context *ctx,
				 const struct dcerpc_interface_table *iface,
				 const char *binding_string,
				 const char *hostname,
				 const enum libnet_RpcConnect_level level,
				 BOOL badcreds, NTSTATUS expected_status)
{
	NTSTATUS status;
	struct libnet_RpcConnect connect;
	connect.level            = level;
	connect.in.binding       = binding_string;
	connect.in.name          = hostname;
	connect.in.dcerpc_iface  = iface;

	/* if bad credentials are needed, set baduser%badpassword instead
	   of default commandline-passed credentials */
	if (badcreds) {
		cli_credentials_set_username(ctx->cred, "baduser", CRED_SPECIFIED);
		cli_credentials_set_password(ctx->cred, "badpassword", CRED_SPECIFIED);
	}

	status = libnet_RpcConnect(ctx, ctx, &connect);

	if (!NT_STATUS_EQUAL(status, expected_status)) {
		printf("Connecting to rpc service %s on %s.\n\tFAILED. Expected: %s."
		       "Received: %s\n",
		       connect.in.dcerpc_iface->name, connect.in.binding, nt_errstr(expected_status),
		       nt_errstr(status));

		return False;
	}

	printf("PASSED. Expected: %s, received: %s\n", nt_errstr(expected_status),
	       nt_errstr(status));

	if (connect.level == LIBNET_RPC_CONNECT_DC_INFO && NT_STATUS_IS_OK(status)) {
		printf("Domain Controller Info:\n");
		printf("\tDomain Name:\t %s\n", connect.out.domain_name);
		printf("\tDomain SID:\t %s\n", dom_sid_string(ctx, connect.out.domain_sid));
		printf("\tRealm:\t\t %s\n", connect.out.realm);
		printf("\tGUID:\t\t %s\n", GUID_string(ctx, connect.out.guid));

	} else if (!NT_STATUS_IS_OK(status)) {
		printf("Error string: %s\n", connect.out.error_string);
	}

	return True;
}


static BOOL torture_rpc_connect(struct torture_context *torture,
				const enum libnet_RpcConnect_level level,
				const char *bindstr, const char *hostname)
{
	struct libnet_context *ctx;

	ctx = libnet_context_init(NULL);
	ctx->cred = cmdline_credentials;
	
	printf("Testing connection to LSA interface\n");
	if (!test_connect_service(ctx, &dcerpc_table_lsarpc, bindstr,
				  hostname, level, False, NT_STATUS_OK)) {
		printf("failed to connect LSA interface\n");
		return False;
	}

	printf("Testing connection to SAMR interface\n");
	if (!test_connect_service(ctx, &dcerpc_table_samr, bindstr,
				  hostname, level, False, NT_STATUS_OK)) {
		printf("failed to connect SAMR interface\n");
		return False;
	}

	printf("Testing connection to SRVSVC interface\n");
	if (!test_connect_service(ctx, &dcerpc_table_srvsvc, bindstr,
				  hostname, level, False, NT_STATUS_OK)) {
		printf("failed to connect SRVSVC interface\n");
		return False;
	}

	printf("Testing connection to LSA interface with wrong credentials\n");
	if (!test_connect_service(ctx, &dcerpc_table_lsarpc, bindstr,
				  hostname, level, True, NT_STATUS_LOGON_FAILURE)) {
		printf("failed to test wrong credentials on LSA interface\n");
		return False;
	}

	printf("Testing connection to SAMR interface with wrong credentials\n");
	if (!test_connect_service(ctx, &dcerpc_table_samr, bindstr,
				  hostname, level, True, NT_STATUS_LOGON_FAILURE)) {
		printf("failed to test wrong credentials on SAMR interface\n");
		return False;
	}

	talloc_free(ctx);

	return True;
}


BOOL torture_rpc_connect_srv(struct torture_context *torture)
{
	const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_SERVER;
	NTSTATUS status;
	struct dcerpc_binding *binding;
	const char *bindstr;;

	bindstr = lp_parm_string(-1, "torture", "binding");
	status = dcerpc_parse_binding(torture, bindstr, &binding);
	if (!NT_STATUS_IS_OK(status)) {
		printf("failed to parse binding string\n");
		return False;
	}

	return torture_rpc_connect(torture, level, NULL, binding->host);
}


BOOL torture_rpc_connect_pdc(struct torture_context *torture)
{
	const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
	NTSTATUS status;
	struct dcerpc_binding *binding;
	const char *bindstr;
	
	bindstr = lp_parm_string(-1, "torture", "binding");
	status = dcerpc_parse_binding(torture, bindstr, &binding);
	if (!NT_STATUS_IS_OK(status)) {
		printf("failed to parse binding string\n");
		return False;
	}

	return torture_rpc_connect(torture, level, NULL, binding->host);
}


BOOL torture_rpc_connect_dc(struct torture_context *torture)
{
	const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC;
	NTSTATUS status;
	struct dcerpc_binding *binding;
	const char *bindstr;
	
	bindstr = lp_parm_string(-1, "torture", "binding");
	status = dcerpc_parse_binding(torture, bindstr, &binding);
	if (!NT_STATUS_IS_OK(status)) {
		printf("failed to parse binding string\n");
		return False;
	}

	return torture_rpc_connect(torture, level, NULL, binding->host);
}


BOOL torture_rpc_connect_dc_info(struct torture_context *torture)
{
	const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC_INFO;
	NTSTATUS status;
	struct dcerpc_binding *binding;
	const char *bindstr;
	
	bindstr = lp_parm_string(-1, "torture", "binding");
	status = dcerpc_parse_binding(torture, bindstr, &binding);
	if (!NT_STATUS_IS_OK(status)) {
		printf("failed to parse binding string\n");
		return False;
	}

	return torture_rpc_connect(torture, level, NULL, binding->host);
}


BOOL torture_rpc_connect_binding(struct torture_context *torture)
{
	const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
	NTSTATUS status;
	struct dcerpc_binding *binding;
	const char *bindstr;
	
	bindstr = lp_parm_string(-1, "torture", "binding");
	status = dcerpc_parse_binding(torture, bindstr, &binding);
	if (!NT_STATUS_IS_OK(status)) {
		printf("failed to parse binding string\n");
		return False;
	}

	return torture_rpc_connect(torture, level, bindstr, NULL);
}