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