summaryrefslogtreecommitdiff
path: root/source4/torture/libnet/libnet_lookup.c
blob: 832a7e5f848c5ad7be74986678d2e4b5c849f878 (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
/*
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/cmdline/popt_common.h"
#include "libnet/libnet.h"
#include "libcli/libcli.h"
#include "torture/rpc/rpc.h"
#include "param/param.h"


bool torture_lookup(struct torture_context *torture)
{
	bool ret;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	struct libnet_context *ctx;
	struct libnet_Lookup lookup;
	struct dcerpc_binding *binding;

	mem_ctx = talloc_init("test_lookup");

	ctx = libnet_context_init(torture->ev, torture->lp_ctx);
	ctx->cred = cmdline_credentials;

	lookup.in.hostname = torture_setting_string(torture, "host", NULL);
	if (lookup.in.hostname == NULL) {
		status = torture_rpc_binding(torture, &binding);
		if (NT_STATUS_IS_OK(status)) {
			lookup.in.hostname = binding->host;
		}
	}

	lookup.in.type     = NBT_NAME_CLIENT;
	lookup.in.resolve_ctx = NULL;
	lookup.out.address = NULL;

	status = libnet_Lookup(ctx, mem_ctx, &lookup);

	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "Couldn't lookup name %s: %s\n", lookup.in.hostname, nt_errstr(status));
		ret = false;
		goto done;
	}

	ret = true;

	torture_comment(torture, "Name [%s] found at address: %s.\n", lookup.in.hostname, *lookup.out.address);

done:
	talloc_free(mem_ctx);
	return ret;
}


bool torture_lookup_host(struct torture_context *torture)
{
	bool ret;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	struct libnet_context *ctx;
	struct libnet_Lookup lookup;
	struct dcerpc_binding *binding;

	mem_ctx = talloc_init("test_lookup_host");

	ctx = libnet_context_init(torture->ev, torture->lp_ctx);
	ctx->cred = cmdline_credentials;

	lookup.in.hostname = torture_setting_string(torture, "host", NULL);
	if (lookup.in.hostname == NULL) {
		status = torture_rpc_binding(torture, &binding);
		if (NT_STATUS_IS_OK(status)) {
			lookup.in.hostname = binding->host;
		}
	}

	lookup.in.resolve_ctx = NULL;
	lookup.out.address = NULL;

	status = libnet_LookupHost(ctx, mem_ctx, &lookup);

	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "Couldn't lookup host %s: %s\n", lookup.in.hostname, nt_errstr(status));
		ret = false;
		goto done;
	}

	ret = true;

	torture_comment(torture, "Host [%s] found at address: %s.\n", lookup.in.hostname, *lookup.out.address);

done:
	talloc_free(mem_ctx);
	return ret;
}


bool torture_lookup_pdc(struct torture_context *torture)
{
	bool ret;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	struct libnet_context *ctx;
	struct libnet_LookupDCs *lookup;
	int i;

	mem_ctx = talloc_init("test_lookup_pdc");

	ctx = libnet_context_init(torture->ev, torture->lp_ctx);
	ctx->cred = cmdline_credentials;

	talloc_steal(ctx, mem_ctx);

	lookup = talloc(mem_ctx, struct libnet_LookupDCs);
	if (!lookup) {
		ret = false;
		goto done;
	}

	lookup->in.domain_name = lp_workgroup(torture->lp_ctx);
	lookup->in.name_type   = NBT_NAME_PDC;

	status = libnet_LookupDCs(ctx, mem_ctx, lookup);

	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "Couldn't lookup pdc %s: %s\n", lookup->in.domain_name,
		       nt_errstr(status));
		ret = false;
		goto done;
	}

	ret = true;

	torture_comment(torture, "DCs of domain [%s] found.\n", lookup->in.domain_name);
	for (i = 0; i < lookup->out.num_dcs; i++) {
		torture_comment(torture, "\tDC[%d]: name=%s, address=%s\n", i, lookup->out.dcs[i].name,
		       lookup->out.dcs[i].address);
	}

done:
	talloc_free(mem_ctx);
	return ret;
}


bool torture_lookup_sam_name(struct torture_context *torture)
{
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	struct libnet_context *ctx;
	struct libnet_LookupName r;

	ctx = libnet_context_init(torture->ev, torture->lp_ctx);
	ctx->cred = cmdline_credentials;

	mem_ctx = talloc_init("torture lookup sam name");
	if (mem_ctx == NULL) return false;

	r.in.name = "Administrator";
	r.in.domain_name = lp_workgroup(torture->lp_ctx);

	status = libnet_LookupName(ctx, mem_ctx, &r);

	talloc_free(mem_ctx);
	talloc_free(ctx);

	return true;
}