1 | /*
|
---|
2 | Samba Unix/Linux SMB client library
|
---|
3 | net lookup command
|
---|
4 | Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "includes.h"
|
---|
20 | #include "utils/net.h"
|
---|
21 | #include "libads/sitename_cache.h"
|
---|
22 | #include "../lib/addns/dnsquery.h"
|
---|
23 | #include "../librpc/gen_ndr/ndr_netlogon.h"
|
---|
24 | #include "smb_krb5.h"
|
---|
25 | #include "../libcli/security/security.h"
|
---|
26 | #include "passdb/lookup_sid.h"
|
---|
27 |
|
---|
28 | int net_lookup_usage(struct net_context *c, int argc, const char **argv)
|
---|
29 | {
|
---|
30 | d_printf(_(
|
---|
31 | " net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
|
---|
32 | " net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
|
---|
33 | " net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
|
---|
34 | " net lookup pdc [domain|realm]\n\tgives IP of realm's kerberos KDC\n\n"
|
---|
35 | " net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n"
|
---|
36 | " net lookup master [domain|wg]\n\tgive IP of master browser\n\n"
|
---|
37 | " net lookup name [name]\n\tLookup name's sid and type\n\n"
|
---|
38 | " net lookup sid [sid]\n\tGive sid's name and type\n\n"
|
---|
39 | " net lookup dsgetdcname [name] [flags] [sitename]\n\n"
|
---|
40 | ));
|
---|
41 | return -1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* lookup a hostname giving an IP */
|
---|
45 | static int net_lookup_host(struct net_context *c, int argc, const char **argv)
|
---|
46 | {
|
---|
47 | struct sockaddr_storage ss;
|
---|
48 | int name_type = 0x20;
|
---|
49 | char addr[INET6_ADDRSTRLEN];
|
---|
50 | const char *name = argv[0];
|
---|
51 | char *p;
|
---|
52 |
|
---|
53 | if (argc == 0)
|
---|
54 | return net_lookup_usage(c, argc, argv);
|
---|
55 |
|
---|
56 | p = strchr_m(name,'#');
|
---|
57 | if (p) {
|
---|
58 | *p = '\0';
|
---|
59 | sscanf(++p,"%x",&name_type);
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (!resolve_name(name, &ss, name_type, false)) {
|
---|
63 | /* we deliberately use DEBUG() here to send it to stderr
|
---|
64 | so scripts aren't mucked up */
|
---|
65 | DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
|
---|
66 | return -1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
70 | d_printf("%s\n", addr);
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | #ifdef HAVE_ADS
|
---|
75 | static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
|
---|
76 | {
|
---|
77 | struct sockaddr_storage ss;
|
---|
78 | int i;
|
---|
79 |
|
---|
80 | for ( i=0; i<numdcs; i++ ) {
|
---|
81 | if (resolve_name(dclist[i].hostname, &ss, 0x20, true) ) {
|
---|
82 | char addr[INET6_ADDRSTRLEN];
|
---|
83 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
84 | #ifdef HAVE_IPV6
|
---|
85 | if (ss.ss_family == AF_INET6) {
|
---|
86 | d_printf("[%s]:%d\n", addr, dclist[i].port);
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 | if (ss.ss_family == AF_INET) {
|
---|
90 | d_printf("%s:%d\n", addr, dclist[i].port);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
|
---|
98 | {
|
---|
99 | #ifdef HAVE_ADS
|
---|
100 | const char *domain;
|
---|
101 | struct sockaddr_storage ss;
|
---|
102 | struct dns_rr_srv *dcs = NULL;
|
---|
103 | int numdcs = 0;
|
---|
104 | char *sitename;
|
---|
105 | TALLOC_CTX *ctx;
|
---|
106 | NTSTATUS status;
|
---|
107 | int ret;
|
---|
108 | char h_name[MAX_DNS_NAME_LENGTH];
|
---|
109 |
|
---|
110 | if (argc > 0)
|
---|
111 | domain = argv[0];
|
---|
112 | else
|
---|
113 | domain = c->opt_target_workgroup;
|
---|
114 |
|
---|
115 | if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
|
---|
116 | d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
|
---|
117 | _("failed"));
|
---|
118 | return -1;
|
---|
119 | }
|
---|
120 |
|
---|
121 | sitename = sitename_fetch(ctx, domain);
|
---|
122 |
|
---|
123 | DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
|
---|
124 |
|
---|
125 | status = ads_dns_query_dcs(ctx,
|
---|
126 | domain,
|
---|
127 | sitename,
|
---|
128 | &dcs,
|
---|
129 | &numdcs);
|
---|
130 | if ( NT_STATUS_IS_OK(status) && numdcs ) {
|
---|
131 | print_ldap_srvlist(dcs, numdcs);
|
---|
132 | TALLOC_FREE( ctx );
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | DEBUG(9, ("Looking up PDC for domain %s\n", domain));
|
---|
137 | if (!get_pdc_ip(domain, &ss)) {
|
---|
138 | TALLOC_FREE( ctx );
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | ret = sys_getnameinfo((struct sockaddr *)&ss,
|
---|
143 | sizeof(struct sockaddr_storage),
|
---|
144 | h_name, sizeof(h_name),
|
---|
145 | NULL, 0,
|
---|
146 | NI_NAMEREQD);
|
---|
147 |
|
---|
148 | if (ret) {
|
---|
149 | TALLOC_FREE( ctx );
|
---|
150 | return -1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
|
---|
154 | domain = strchr(h_name, '.');
|
---|
155 | if (!domain) {
|
---|
156 | TALLOC_FREE( ctx );
|
---|
157 | return -1;
|
---|
158 | }
|
---|
159 | domain++;
|
---|
160 |
|
---|
161 | DEBUG(9, ("Looking up ldap for domain %s\n", domain));
|
---|
162 |
|
---|
163 | status = ads_dns_query_dcs(ctx,
|
---|
164 | domain,
|
---|
165 | sitename,
|
---|
166 | &dcs,
|
---|
167 | &numdcs);
|
---|
168 | if ( NT_STATUS_IS_OK(status) && numdcs ) {
|
---|
169 | print_ldap_srvlist(dcs, numdcs);
|
---|
170 | TALLOC_FREE( ctx );
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | TALLOC_FREE( ctx );
|
---|
175 |
|
---|
176 | return -1;
|
---|
177 | #endif
|
---|
178 | DEBUG(1,("No ADS support\n"));
|
---|
179 | return -1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
|
---|
183 | {
|
---|
184 | struct ip_service *ip_list;
|
---|
185 | struct sockaddr_storage ss;
|
---|
186 | char *pdc_str = NULL;
|
---|
187 | const char *domain = NULL;
|
---|
188 | char *sitename = NULL;
|
---|
189 | int count, i;
|
---|
190 | char addr[INET6_ADDRSTRLEN];
|
---|
191 | bool sec_ads = (lp_security() == SEC_ADS);
|
---|
192 |
|
---|
193 | if (sec_ads) {
|
---|
194 | domain = lp_realm();
|
---|
195 | } else {
|
---|
196 | domain = c->opt_target_workgroup;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (argc > 0)
|
---|
200 | domain=argv[0];
|
---|
201 |
|
---|
202 | /* first get PDC */
|
---|
203 | if (!get_pdc_ip(domain, &ss))
|
---|
204 | return -1;
|
---|
205 |
|
---|
206 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
207 | if (asprintf(&pdc_str, "%s", addr) == -1) {
|
---|
208 | return -1;
|
---|
209 | }
|
---|
210 | d_printf("%s\n", pdc_str);
|
---|
211 |
|
---|
212 | sitename = sitename_fetch(talloc_tos(), domain);
|
---|
213 | if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
|
---|
214 | &ip_list, &count, sec_ads))) {
|
---|
215 | SAFE_FREE(pdc_str);
|
---|
216 | TALLOC_FREE(sitename);
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 | TALLOC_FREE(sitename);
|
---|
220 | for (i=0;i<count;i++) {
|
---|
221 | print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
|
---|
222 | if (!strequal(pdc_str, addr))
|
---|
223 | d_printf("%s\n", addr);
|
---|
224 | }
|
---|
225 | SAFE_FREE(pdc_str);
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
|
---|
230 | {
|
---|
231 | struct sockaddr_storage ss;
|
---|
232 | char *pdc_str = NULL;
|
---|
233 | const char *domain;
|
---|
234 | char addr[INET6_ADDRSTRLEN];
|
---|
235 |
|
---|
236 | if (lp_security() == SEC_ADS) {
|
---|
237 | domain = lp_realm();
|
---|
238 | } else {
|
---|
239 | domain = c->opt_target_workgroup;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (argc > 0)
|
---|
243 | domain=argv[0];
|
---|
244 |
|
---|
245 | /* first get PDC */
|
---|
246 | if (!get_pdc_ip(domain, &ss))
|
---|
247 | return -1;
|
---|
248 |
|
---|
249 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
250 | if (asprintf(&pdc_str, "%s", addr) == -1) {
|
---|
251 | return -1;
|
---|
252 | }
|
---|
253 | d_printf("%s\n", pdc_str);
|
---|
254 | SAFE_FREE(pdc_str);
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | static int net_lookup_master(struct net_context *c, int argc, const char **argv)
|
---|
260 | {
|
---|
261 | struct sockaddr_storage master_ss;
|
---|
262 | const char *domain = c->opt_target_workgroup;
|
---|
263 | char addr[INET6_ADDRSTRLEN];
|
---|
264 |
|
---|
265 | if (argc > 0)
|
---|
266 | domain=argv[0];
|
---|
267 |
|
---|
268 | if (!find_master_ip(domain, &master_ss))
|
---|
269 | return -1;
|
---|
270 | print_sockaddr(addr, sizeof(addr), &master_ss);
|
---|
271 | d_printf("%s\n", addr);
|
---|
272 | return 0;
|
---|
273 | }
|
---|
274 |
|
---|
275 | static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
|
---|
276 | {
|
---|
277 | #ifdef HAVE_KRB5
|
---|
278 | krb5_error_code rc;
|
---|
279 | krb5_context ctx;
|
---|
280 | struct ip_service *kdcs;
|
---|
281 | const char *realm;
|
---|
282 | int num_kdcs = 0;
|
---|
283 | int i;
|
---|
284 | NTSTATUS status;
|
---|
285 |
|
---|
286 | initialize_krb5_error_table();
|
---|
287 | rc = krb5_init_context(&ctx);
|
---|
288 | if (rc) {
|
---|
289 | DEBUG(1,("krb5_init_context failed (%s)\n",
|
---|
290 | error_message(rc)));
|
---|
291 | return -1;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (argc > 0) {
|
---|
295 | realm = argv[0];
|
---|
296 | } else if (lp_realm() && *lp_realm()) {
|
---|
297 | realm = lp_realm();
|
---|
298 | } else {
|
---|
299 | char **realms;
|
---|
300 |
|
---|
301 | rc = krb5_get_host_realm(ctx, NULL, &realms);
|
---|
302 | if (rc) {
|
---|
303 | DEBUG(1,("krb5_gethost_realm failed (%s)\n",
|
---|
304 | error_message(rc)));
|
---|
305 | return -1;
|
---|
306 | }
|
---|
307 | realm = (const char *) *realms;
|
---|
308 | }
|
---|
309 |
|
---|
310 | status = get_kdc_list(realm, NULL, &kdcs, &num_kdcs);
|
---|
311 | if (!NT_STATUS_IS_OK(status)) {
|
---|
312 | DEBUG(1,("get_kdc_list failed (%s)\n", nt_errstr(status)));
|
---|
313 | return -1;
|
---|
314 | }
|
---|
315 |
|
---|
316 | for (i = 0; i < num_kdcs; i++) {
|
---|
317 | char addr[INET6_ADDRSTRLEN];
|
---|
318 |
|
---|
319 | print_sockaddr(addr, sizeof(addr), &kdcs[i].ss);
|
---|
320 |
|
---|
321 | d_printf("%s:%u\n", addr, kdcs[i].port);
|
---|
322 | }
|
---|
323 |
|
---|
324 | return 0;
|
---|
325 | #endif
|
---|
326 | DEBUG(1, ("No kerberos support\n"));
|
---|
327 | return -1;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static int net_lookup_name(struct net_context *c, int argc, const char **argv)
|
---|
331 | {
|
---|
332 | const char *dom, *name;
|
---|
333 | struct dom_sid sid;
|
---|
334 | enum lsa_SidType type;
|
---|
335 |
|
---|
336 | if (argc != 1) {
|
---|
337 | d_printf("%s\n%s",
|
---|
338 | _("Usage:"),
|
---|
339 | _(" net lookup name <name>\n"));
|
---|
340 | return -1;
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
|
---|
344 | &dom, &name, &sid, &type)) {
|
---|
345 | d_printf(_("Could not lookup name %s\n"), argv[0]);
|
---|
346 | return -1;
|
---|
347 | }
|
---|
348 |
|
---|
349 | d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
|
---|
350 | type, sid_type_lookup(type), dom, name);
|
---|
351 | return 0;
|
---|
352 | }
|
---|
353 |
|
---|
354 | static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
|
---|
355 | {
|
---|
356 | const char *dom, *name;
|
---|
357 | struct dom_sid sid;
|
---|
358 | enum lsa_SidType type;
|
---|
359 |
|
---|
360 | if (argc != 1) {
|
---|
361 | d_printf("%s\n%s",
|
---|
362 | _("Usage:"),
|
---|
363 | _(" net lookup sid <sid>\n"));
|
---|
364 | return -1;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (!string_to_sid(&sid, argv[0])) {
|
---|
368 | d_printf(_("Could not convert %s to SID\n"), argv[0]);
|
---|
369 | return -1;
|
---|
370 | }
|
---|
371 |
|
---|
372 | if (!lookup_sid(talloc_tos(), &sid,
|
---|
373 | &dom, &name, &type)) {
|
---|
374 | d_printf(_("Could not lookup name %s\n"), argv[0]);
|
---|
375 | return -1;
|
---|
376 | }
|
---|
377 |
|
---|
378 | d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
|
---|
379 | type, sid_type_lookup(type), dom, name);
|
---|
380 | return 0;
|
---|
381 | }
|
---|
382 |
|
---|
383 | static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
|
---|
384 | {
|
---|
385 | NTSTATUS status;
|
---|
386 | const char *domain_name = NULL;
|
---|
387 | const char *site_name = NULL;
|
---|
388 | uint32_t flags = 0;
|
---|
389 | struct netr_DsRGetDCNameInfo *info = NULL;
|
---|
390 | TALLOC_CTX *mem_ctx;
|
---|
391 | char *s = NULL;
|
---|
392 |
|
---|
393 | if (argc < 1 || argc > 3) {
|
---|
394 | d_printf("%s\n%s",
|
---|
395 | _("Usage:"),
|
---|
396 | _(" net lookup dsgetdcname "
|
---|
397 | "<name> <flags> <sitename>\n"));
|
---|
398 | return -1;
|
---|
399 | }
|
---|
400 |
|
---|
401 | mem_ctx = talloc_init("net_lookup_dsgetdcname");
|
---|
402 | if (!mem_ctx) {
|
---|
403 | return -1;
|
---|
404 | }
|
---|
405 |
|
---|
406 | domain_name = argv[0];
|
---|
407 |
|
---|
408 | if (argc >= 2)
|
---|
409 | sscanf(argv[1], "%x", &flags);
|
---|
410 |
|
---|
411 | if (!flags) {
|
---|
412 | flags |= DS_DIRECTORY_SERVICE_REQUIRED;
|
---|
413 | }
|
---|
414 |
|
---|
415 | if (argc == 3) {
|
---|
416 | site_name = argv[2];
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (!c->msg_ctx) {
|
---|
420 | d_fprintf(stderr, _("Could not initialise message context. "
|
---|
421 | "Try running as root\n"));
|
---|
422 | return -1;
|
---|
423 | }
|
---|
424 |
|
---|
425 | status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
|
---|
426 | flags, &info);
|
---|
427 | if (!NT_STATUS_IS_OK(status)) {
|
---|
428 | d_printf(_("failed with: %s\n"), nt_errstr(status));
|
---|
429 | TALLOC_FREE(mem_ctx);
|
---|
430 | return -1;
|
---|
431 | }
|
---|
432 |
|
---|
433 | s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
|
---|
434 | printf("%s\n", s);
|
---|
435 | TALLOC_FREE(s);
|
---|
436 |
|
---|
437 | TALLOC_FREE(mem_ctx);
|
---|
438 | return 0;
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | /* lookup hosts or IP addresses using internal samba lookup fns */
|
---|
443 | int net_lookup(struct net_context *c, int argc, const char **argv)
|
---|
444 | {
|
---|
445 | int i;
|
---|
446 |
|
---|
447 | struct functable table[] = {
|
---|
448 | {"HOST", net_lookup_host},
|
---|
449 | {"LDAP", net_lookup_ldap},
|
---|
450 | {"DC", net_lookup_dc},
|
---|
451 | {"PDC", net_lookup_pdc},
|
---|
452 | {"MASTER", net_lookup_master},
|
---|
453 | {"KDC", net_lookup_kdc},
|
---|
454 | {"NAME", net_lookup_name},
|
---|
455 | {"SID", net_lookup_sid},
|
---|
456 | {"DSGETDCNAME", net_lookup_dsgetdcname},
|
---|
457 | {NULL, NULL}
|
---|
458 | };
|
---|
459 |
|
---|
460 | if (argc < 1) {
|
---|
461 | d_printf(_("\nUsage: \n"));
|
---|
462 | return net_lookup_usage(c, argc, argv);
|
---|
463 | }
|
---|
464 | for (i=0; table[i].funcname; i++) {
|
---|
465 | if (strcasecmp_m(argv[0], table[i].funcname) == 0)
|
---|
466 | return table[i].fn(c, argc-1, argv+1);
|
---|
467 | }
|
---|
468 |
|
---|
469 | /* Default to lookup a hostname so 'net lookup foo#1b' can be
|
---|
470 | used instead of 'net lookup host foo#1b'. The host syntax
|
---|
471 | is a bit confusing as non #00 names can't really be
|
---|
472 | considered hosts as such. */
|
---|
473 |
|
---|
474 | return net_lookup_host(c, argc, argv);
|
---|
475 | }
|
---|