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