s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[metze/samba/wip.git] / source4 / torture / libnet / libnet_lookup.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/libcli.h"
25 #include "torture/rpc/torture_rpc.h"
26 #include "param/param.h"
27
28
29 bool torture_lookup(struct torture_context *torture)
30 {
31         bool ret;
32         NTSTATUS status;
33         TALLOC_CTX *mem_ctx;
34         struct libnet_context *ctx;
35         struct libnet_Lookup lookup;
36         struct dcerpc_binding *binding;
37
38         mem_ctx = talloc_init("test_lookup");
39
40         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
41         ctx->cred = cmdline_credentials;
42
43         lookup.in.hostname = torture_setting_string(torture, "host", NULL);
44         if (lookup.in.hostname == NULL) {
45                 status = torture_rpc_binding(torture, &binding);
46                 if (NT_STATUS_IS_OK(status)) {
47                         lookup.in.hostname = binding->host;
48                 }
49         }
50
51         lookup.in.type     = NBT_NAME_CLIENT;
52         lookup.in.resolve_ctx = NULL;
53         lookup.out.address = NULL;
54
55         status = libnet_Lookup(ctx, mem_ctx, &lookup);
56
57         if (!NT_STATUS_IS_OK(status)) {
58                 torture_comment(torture, "Couldn't lookup name %s: %s\n", lookup.in.hostname, nt_errstr(status));
59                 ret = false;
60                 goto done;
61         }
62
63         ret = true;
64
65         torture_comment(torture, "Name [%s] found at address: %s.\n", lookup.in.hostname, *lookup.out.address);
66
67 done:
68         talloc_free(mem_ctx);
69         return ret;
70 }
71
72
73 bool torture_lookup_host(struct torture_context *torture)
74 {
75         bool ret;
76         NTSTATUS status;
77         TALLOC_CTX *mem_ctx;
78         struct libnet_context *ctx;
79         struct libnet_Lookup lookup;
80         struct dcerpc_binding *binding;
81
82         mem_ctx = talloc_init("test_lookup_host");
83
84         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
85         ctx->cred = cmdline_credentials;
86
87         lookup.in.hostname = torture_setting_string(torture, "host", NULL);
88         if (lookup.in.hostname == NULL) {
89                 status = torture_rpc_binding(torture, &binding);
90                 if (NT_STATUS_IS_OK(status)) {
91                         lookup.in.hostname = binding->host;
92                 }
93         }
94
95         lookup.in.resolve_ctx = NULL;
96         lookup.out.address = NULL;
97
98         status = libnet_LookupHost(ctx, mem_ctx, &lookup);
99
100         if (!NT_STATUS_IS_OK(status)) {
101                 torture_comment(torture, "Couldn't lookup host %s: %s\n", lookup.in.hostname, nt_errstr(status));
102                 ret = false;
103                 goto done;
104         }
105
106         ret = true;
107
108         torture_comment(torture, "Host [%s] found at address: %s.\n", lookup.in.hostname, *lookup.out.address);
109
110 done:
111         talloc_free(mem_ctx);
112         return ret;
113 }
114
115
116 bool torture_lookup_pdc(struct torture_context *torture)
117 {
118         bool ret;
119         NTSTATUS status;
120         TALLOC_CTX *mem_ctx;
121         struct libnet_context *ctx;
122         struct libnet_LookupDCs *lookup;
123         int i;
124
125         mem_ctx = talloc_init("test_lookup_pdc");
126
127         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
128         ctx->cred = cmdline_credentials;
129
130         talloc_steal(ctx, mem_ctx);
131
132         lookup = talloc(mem_ctx, struct libnet_LookupDCs);
133         if (!lookup) {
134                 ret = false;
135                 goto done;
136         }
137
138         lookup->in.domain_name = lpcfg_workgroup(torture->lp_ctx);
139         lookup->in.name_type   = NBT_NAME_PDC;
140
141         status = libnet_LookupDCs(ctx, mem_ctx, lookup);
142
143         if (!NT_STATUS_IS_OK(status)) {
144                 torture_comment(torture, "Couldn't lookup pdc %s: %s\n", lookup->in.domain_name,
145                        nt_errstr(status));
146                 ret = false;
147                 goto done;
148         }
149
150         ret = true;
151
152         torture_comment(torture, "DCs of domain [%s] found.\n", lookup->in.domain_name);
153         for (i = 0; i < lookup->out.num_dcs; i++) {
154                 torture_comment(torture, "\tDC[%d]: name=%s, address=%s\n", i, lookup->out.dcs[i].name,
155                        lookup->out.dcs[i].address);
156         }
157
158 done:
159         talloc_free(mem_ctx);
160         return ret;
161 }
162
163
164 bool torture_lookup_sam_name(struct torture_context *torture)
165 {
166         NTSTATUS status;
167         TALLOC_CTX *mem_ctx;
168         struct libnet_context *ctx;
169         struct libnet_LookupName r;
170
171         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
172         ctx->cred = cmdline_credentials;
173
174         mem_ctx = talloc_init("torture lookup sam name");
175         if (mem_ctx == NULL) return false;
176
177         r.in.name = "Administrator";
178         r.in.domain_name = lpcfg_workgroup(torture->lp_ctx);
179
180         status = libnet_LookupName(ctx, mem_ctx, &r);
181
182         talloc_free(mem_ctx);
183         talloc_free(ctx);
184
185         return true;
186 }