provision: BIND 9.15.x is not supported
[samba.git] / source3 / utils / net_lookup.c
blob030296aba371525813781de6cc964d06cc378bc3
1 /*
2 Samba Unix/Linux SMB client library
3 net lookup command
4 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
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.
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.
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/>. */
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "libsmb/namequery.h"
22 #include "libads/sitename_cache.h"
23 #include "../lib/addns/dnsquery.h"
24 #include "../librpc/gen_ndr/ndr_netlogon.h"
25 #include "smb_krb5.h"
26 #include "../libcli/security/security.h"
27 #include "passdb/lookup_sid.h"
28 #include "libsmb/dsgetdcname.h"
30 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
32 d_printf(_(
33 " net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
34 " net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
35 " net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
36 " net lookup pdc [domain|realm]\n\tgives IP of realm's kerberos KDC\n\n"
37 " net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n"
38 " net lookup master [domain|wg]\n\tgive IP of master browser\n\n"
39 " net lookup name [name]\n\tLookup name's sid and type\n\n"
40 " net lookup sid [sid]\n\tGive sid's name and type\n\n"
41 " net lookup dsgetdcname [name] [flags] [sitename]\n\n"
42 ));
43 return -1;
46 /* lookup a hostname giving an IP */
47 static int net_lookup_host(struct net_context *c, int argc, const char **argv)
49 struct sockaddr_storage ss;
50 int name_type = 0x20;
51 char addr[INET6_ADDRSTRLEN];
52 const char *name = argv[0];
53 char *p;
55 if (argc == 0)
56 return net_lookup_usage(c, argc, argv);
58 p = strchr_m(name,'#');
59 if (p) {
60 *p = '\0';
61 sscanf(++p,"%x",&name_type);
64 if (!resolve_name(name, &ss, name_type, false)) {
65 /* we deliberately use DEBUG() here to send it to stderr
66 so scripts aren't mucked up */
67 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
68 return -1;
71 print_sockaddr(addr, sizeof(addr), &ss);
72 d_printf("%s\n", addr);
73 return 0;
76 #ifdef HAVE_ADS
77 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
79 struct sockaddr_storage ss;
80 int i;
82 for ( i=0; i<numdcs; i++ ) {
83 if (resolve_name(dclist[i].hostname, &ss, 0x20, true) ) {
84 char addr[INET6_ADDRSTRLEN];
85 print_sockaddr(addr, sizeof(addr), &ss);
86 #ifdef HAVE_IPV6
87 if (ss.ss_family == AF_INET6) {
88 d_printf("[%s]:%d\n", addr, dclist[i].port);
90 #endif
91 if (ss.ss_family == AF_INET) {
92 d_printf("%s:%d\n", addr, dclist[i].port);
97 #endif
99 static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
101 #ifdef HAVE_ADS
102 const char *domain;
103 struct sockaddr_storage ss;
104 struct dns_rr_srv *dcs = NULL;
105 size_t numdcs = 0;
106 char *sitename;
107 TALLOC_CTX *ctx;
108 NTSTATUS status;
109 int ret;
110 char h_name[MAX_DNS_NAME_LENGTH];
112 if (argc > 0)
113 domain = argv[0];
114 else
115 domain = c->opt_target_workgroup;
117 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
118 d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
119 _("failed"));
120 return -1;
123 sitename = sitename_fetch(ctx, domain);
125 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
127 status = ads_dns_query_dcs(ctx,
128 domain,
129 sitename,
130 &dcs,
131 &numdcs);
132 if ( NT_STATUS_IS_OK(status) && numdcs ) {
133 print_ldap_srvlist(dcs, numdcs);
134 TALLOC_FREE( ctx );
135 return 0;
138 DEBUG(9, ("Looking up PDC for domain %s\n", domain));
139 if (!get_pdc_ip(domain, &ss)) {
140 TALLOC_FREE( ctx );
141 return -1;
144 ret = sys_getnameinfo((struct sockaddr *)&ss,
145 sizeof(struct sockaddr_storage),
146 h_name, sizeof(h_name),
147 NULL, 0,
148 NI_NAMEREQD);
150 if (ret) {
151 TALLOC_FREE( ctx );
152 return -1;
155 DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
156 domain = strchr(h_name, '.');
157 if (!domain) {
158 TALLOC_FREE( ctx );
159 return -1;
161 domain++;
163 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
165 status = ads_dns_query_dcs(ctx,
166 domain,
167 sitename,
168 &dcs,
169 &numdcs);
170 if ( NT_STATUS_IS_OK(status) && numdcs ) {
171 print_ldap_srvlist(dcs, numdcs);
172 TALLOC_FREE( ctx );
173 return 0;
176 TALLOC_FREE( ctx );
178 return -1;
179 #endif
180 DEBUG(1,("No ADS support\n"));
181 return -1;
184 static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
186 struct samba_sockaddr *sa_list = NULL;
187 struct sockaddr_storage ss;
188 char *pdc_str = NULL;
189 const char *domain = NULL;
190 char *sitename = NULL;
191 size_t count = 0;
192 size_t i;
193 char addr[INET6_ADDRSTRLEN];
194 bool sec_ads = (lp_security() == SEC_ADS);
195 NTSTATUS status;
197 if (sec_ads) {
198 domain = lp_realm();
199 } else {
200 domain = c->opt_target_workgroup;
203 if (argc > 0)
204 domain=argv[0];
206 /* first get PDC */
207 if (!get_pdc_ip(domain, &ss))
208 return -1;
210 print_sockaddr(addr, sizeof(addr), &ss);
211 if (asprintf(&pdc_str, "%s", addr) == -1) {
212 return -1;
214 d_printf("%s\n", pdc_str);
216 sitename = sitename_fetch(talloc_tos(), domain);
217 status = get_sorted_dc_list(talloc_tos(),
218 domain,
219 sitename,
220 &sa_list,
221 &count,
222 sec_ads);
223 if (!NT_STATUS_IS_OK(status)) {
224 SAFE_FREE(pdc_str);
225 TALLOC_FREE(sitename);
226 return 0;
228 TALLOC_FREE(sitename);
229 for (i=0;i<count;i++) {
230 print_sockaddr(addr, sizeof(addr), &sa_list[i].u.ss);
231 if (!strequal(pdc_str, addr))
232 d_printf("%s\n", addr);
234 TALLOC_FREE(sa_list);
235 SAFE_FREE(pdc_str);
236 return 0;
239 static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
241 struct sockaddr_storage ss;
242 char *pdc_str = NULL;
243 const char *domain;
244 char addr[INET6_ADDRSTRLEN];
246 if (lp_security() == SEC_ADS) {
247 domain = lp_realm();
248 } else {
249 domain = c->opt_target_workgroup;
252 if (argc > 0)
253 domain=argv[0];
255 /* first get PDC */
256 if (!get_pdc_ip(domain, &ss))
257 return -1;
259 print_sockaddr(addr, sizeof(addr), &ss);
260 if (asprintf(&pdc_str, "%s", addr) == -1) {
261 return -1;
263 d_printf("%s\n", pdc_str);
264 SAFE_FREE(pdc_str);
265 return 0;
269 static int net_lookup_master(struct net_context *c, int argc, const char **argv)
271 struct sockaddr_storage master_ss;
272 const char *domain = c->opt_target_workgroup;
273 char addr[INET6_ADDRSTRLEN];
275 if (argc > 0)
276 domain=argv[0];
278 if (!find_master_ip(domain, &master_ss))
279 return -1;
280 print_sockaddr(addr, sizeof(addr), &master_ss);
281 d_printf("%s\n", addr);
282 return 0;
285 static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
287 #ifdef HAVE_KRB5
288 krb5_error_code rc;
289 krb5_context ctx;
290 struct samba_sockaddr *kdcs = NULL;
291 const char *realm;
292 char **get_host_realms = NULL;
293 size_t num_kdcs = 0;
294 size_t i;
295 NTSTATUS status;
297 rc = smb_krb5_init_context_common(&ctx);
298 if (rc) {
299 DBG_ERR("kerberos init context failed (%s)\n",
300 error_message(rc));
301 return -1;
304 if (argc > 0) {
305 realm = argv[0];
306 } else if (lp_realm() && *lp_realm()) {
307 realm = lp_realm();
308 } else {
309 rc = krb5_get_host_realm(ctx, NULL, &get_host_realms);
310 if (rc) {
311 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
312 error_message(rc)));
313 krb5_free_context(ctx);
314 return -1;
316 realm = (const char *) *get_host_realms;
319 status = get_kdc_list(talloc_tos(),
320 realm,
321 NULL,
322 &kdcs,
323 &num_kdcs);
324 if (!NT_STATUS_IS_OK(status)) {
325 DBG_WARNING("get_kdc_list failed (%s)\n",
326 nt_errstr(status));
327 krb5_free_host_realm(ctx, get_host_realms);
328 krb5_free_context(ctx);
329 return -1;
332 for (i = 0; i < num_kdcs; i++) {
333 char addr[INET6_ADDRSTRLEN];
335 print_sockaddr(addr, sizeof(addr), &kdcs[i].u.ss);
337 d_printf("%s:88\n", addr);
340 krb5_free_host_realm(ctx, get_host_realms);
341 krb5_free_context(ctx);
342 TALLOC_FREE(kdcs);
343 return 0;
344 #endif
345 DEBUG(1, ("No kerberos support\n"));
346 return -1;
349 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
351 const char *dom, *name;
352 struct dom_sid sid;
353 struct dom_sid_buf buf;
354 enum lsa_SidType type;
356 if (argc != 1) {
357 d_printf("%s\n%s",
358 _("Usage:"),
359 _(" net lookup name <name>\n"));
360 return -1;
363 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
364 &dom, &name, &sid, &type)) {
365 d_printf(_("Could not lookup name %s\n"), argv[0]);
366 return -1;
369 d_printf("%s %d (%s) %s\\%s\n", dom_sid_str_buf(&sid, &buf),
370 type, sid_type_lookup(type), dom, name);
371 return 0;
374 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
376 const char *dom, *name;
377 struct dom_sid sid;
378 struct dom_sid_buf buf;
379 enum lsa_SidType type;
381 if (argc != 1) {
382 d_printf("%s\n%s",
383 _("Usage:"),
384 _(" net lookup sid <sid>\n"));
385 return -1;
388 if (!string_to_sid(&sid, argv[0])) {
389 d_printf(_("Could not convert %s to SID\n"), argv[0]);
390 return -1;
393 if (!lookup_sid(talloc_tos(), &sid,
394 &dom, &name, &type)) {
395 d_printf(_("Could not lookup name %s\n"), argv[0]);
396 return -1;
399 d_printf("%s %d (%s) %s\\%s\n", dom_sid_str_buf(&sid, &buf),
400 type, sid_type_lookup(type), dom, name);
401 return 0;
404 static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
406 NTSTATUS status;
407 const char *domain_name = NULL;
408 const char *site_name = NULL;
409 uint32_t flags = 0;
410 struct netr_DsRGetDCNameInfo *info = NULL;
411 TALLOC_CTX *mem_ctx;
412 char *s = NULL;
414 if (argc < 1 || argc > 3) {
415 d_printf("%s\n%s",
416 _("Usage:"),
417 _(" net lookup dsgetdcname "
418 "<name> <flags> <sitename>\n"));
419 return -1;
422 mem_ctx = talloc_init("net_lookup_dsgetdcname");
423 if (!mem_ctx) {
424 return -1;
427 domain_name = argv[0];
429 if (argc >= 2) {
430 sscanf(argv[1], "%x", &flags);
433 if (flags == 0) {
434 flags = DS_DIRECTORY_SERVICE_REQUIRED;
437 if (argc == 3) {
438 site_name = argv[2];
441 if (!c->msg_ctx) {
442 d_fprintf(stderr, _("Could not initialise message context. "
443 "Try running as root\n"));
444 return -1;
447 status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
448 flags, &info);
449 if (!NT_STATUS_IS_OK(status)) {
450 d_printf(_("failed with: %s\n"), nt_errstr(status));
451 TALLOC_FREE(mem_ctx);
452 return -1;
455 s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
456 printf("%s\n", s);
457 TALLOC_FREE(s);
459 TALLOC_FREE(mem_ctx);
460 return 0;
464 /* lookup hosts or IP addresses using internal samba lookup fns */
465 int net_lookup(struct net_context *c, int argc, const char **argv)
467 int i;
469 struct functable table[] = {
471 .funcname = "HOST",
472 .fn = net_lookup_host,
475 .funcname = "LDAP",
476 .fn = net_lookup_ldap,
479 .funcname = "DC",
480 .fn = net_lookup_dc,
483 .funcname = "PDC",
484 .fn = net_lookup_pdc,
487 .funcname = "MASTER",
488 .fn = net_lookup_master,
491 .funcname = "KDC",
492 .fn = net_lookup_kdc,
495 .funcname = "NAME",
496 .fn = net_lookup_name,
499 .funcname = "SID",
500 .fn = net_lookup_sid,
503 .funcname = "DSGETDCNAME",
504 .fn = net_lookup_dsgetdcname,
507 .funcname = NULL,
511 if (argc < 1) {
512 d_printf(_("\nUsage: \n"));
513 return net_lookup_usage(c, argc, argv);
515 for (i=0; table[i].funcname; i++) {
516 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
517 return table[i].fn(c, argc-1, argv+1);
520 /* Default to lookup a hostname so 'net lookup foo#1b' can be
521 used instead of 'net lookup host foo#1b'. The host syntax
522 is a bit confusing as non #00 names can't really be
523 considered hosts as such. */
525 return net_lookup_host(c, argc, argv);