dbwrap: Use tdb_null in db_ctdb_delete
[Samba/gbeck.git] / source3 / utils / net_lookup.c
blobb0d6fe51f01c65ccecb832a64b113042fe6a7c53
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 "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"
28 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
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;
44 /* lookup a hostname giving an IP */
45 static int net_lookup_host(struct net_context *c, int argc, const char **argv)
47 struct sockaddr_storage ss;
48 int name_type = 0x20;
49 char addr[INET6_ADDRSTRLEN];
50 const char *name = argv[0];
51 char *p;
53 if (argc == 0)
54 return net_lookup_usage(c, argc, argv);
56 p = strchr_m(name,'#');
57 if (p) {
58 *p = '\0';
59 sscanf(++p,"%x",&name_type);
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;
69 print_sockaddr(addr, sizeof(addr), &ss);
70 d_printf("%s\n", addr);
71 return 0;
74 #ifdef HAVE_ADS
75 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
77 struct sockaddr_storage ss;
78 int i;
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);
88 #endif
89 if (ss.ss_family == AF_INET) {
90 d_printf("%s:%d\n", addr, dclist[i].port);
95 #endif
97 static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
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 const char *dns_hosts_file;
111 if (argc > 0)
112 domain = argv[0];
113 else
114 domain = c->opt_target_workgroup;
116 sitename = sitename_fetch(domain);
118 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
119 d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
120 _("failed"));
121 SAFE_FREE(sitename);
122 return -1;
125 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
127 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
128 status = ads_dns_query_dcs(ctx, dns_hosts_file, domain, sitename,
129 &dcs, &numdcs);
130 if ( NT_STATUS_IS_OK(status) && numdcs ) {
131 print_ldap_srvlist(dcs, numdcs);
132 TALLOC_FREE( ctx );
133 SAFE_FREE(sitename);
134 return 0;
137 DEBUG(9, ("Looking up PDC for domain %s\n", domain));
138 if (!get_pdc_ip(domain, &ss)) {
139 TALLOC_FREE( ctx );
140 SAFE_FREE(sitename);
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 SAFE_FREE(sitename);
153 return -1;
156 DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
157 domain = strchr(h_name, '.');
158 if (!domain) {
159 TALLOC_FREE( ctx );
160 SAFE_FREE(sitename);
161 return -1;
163 domain++;
165 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
167 status = ads_dns_query_dcs(ctx, dns_hosts_file, domain, sitename,
168 &dcs, &numdcs);
169 if ( NT_STATUS_IS_OK(status) && numdcs ) {
170 print_ldap_srvlist(dcs, numdcs);
171 TALLOC_FREE( ctx );
172 SAFE_FREE(sitename);
173 return 0;
176 TALLOC_FREE( ctx );
177 SAFE_FREE(sitename);
179 return -1;
180 #endif
181 DEBUG(1,("No ADS support\n"));
182 return -1;
185 static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
187 struct ip_service *ip_list;
188 struct sockaddr_storage ss;
189 char *pdc_str = NULL;
190 const char *domain = NULL;
191 char *sitename = NULL;
192 int count, i;
193 char addr[INET6_ADDRSTRLEN];
194 bool sec_ads = (lp_security() == SEC_ADS);
196 if (sec_ads) {
197 domain = lp_realm();
198 } else {
199 domain = c->opt_target_workgroup;
202 if (argc > 0)
203 domain=argv[0];
205 /* first get PDC */
206 if (!get_pdc_ip(domain, &ss))
207 return -1;
209 print_sockaddr(addr, sizeof(addr), &ss);
210 if (asprintf(&pdc_str, "%s", addr) == -1) {
211 return -1;
213 d_printf("%s\n", pdc_str);
215 sitename = sitename_fetch(domain);
216 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
217 &ip_list, &count, sec_ads))) {
218 SAFE_FREE(pdc_str);
219 SAFE_FREE(sitename);
220 return 0;
222 SAFE_FREE(sitename);
223 for (i=0;i<count;i++) {
224 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
225 if (!strequal(pdc_str, addr))
226 d_printf("%s\n", addr);
228 SAFE_FREE(pdc_str);
229 return 0;
232 static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
234 struct sockaddr_storage ss;
235 char *pdc_str = NULL;
236 const char *domain;
237 char addr[INET6_ADDRSTRLEN];
239 if (lp_security() == SEC_ADS) {
240 domain = lp_realm();
241 } else {
242 domain = c->opt_target_workgroup;
245 if (argc > 0)
246 domain=argv[0];
248 /* first get PDC */
249 if (!get_pdc_ip(domain, &ss))
250 return -1;
252 print_sockaddr(addr, sizeof(addr), &ss);
253 if (asprintf(&pdc_str, "%s", addr) == -1) {
254 return -1;
256 d_printf("%s\n", pdc_str);
257 SAFE_FREE(pdc_str);
258 return 0;
262 static int net_lookup_master(struct net_context *c, int argc, const char **argv)
264 struct sockaddr_storage master_ss;
265 const char *domain = c->opt_target_workgroup;
266 char addr[INET6_ADDRSTRLEN];
268 if (argc > 0)
269 domain=argv[0];
271 if (!find_master_ip(domain, &master_ss))
272 return -1;
273 print_sockaddr(addr, sizeof(addr), &master_ss);
274 d_printf("%s\n", addr);
275 return 0;
278 static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
280 #ifdef HAVE_KRB5
281 krb5_error_code rc;
282 krb5_context ctx;
283 struct ip_service *kdcs;
284 const char *realm;
285 int num_kdcs = 0;
286 int i;
287 NTSTATUS status;
289 initialize_krb5_error_table();
290 rc = krb5_init_context(&ctx);
291 if (rc) {
292 DEBUG(1,("krb5_init_context failed (%s)\n",
293 error_message(rc)));
294 return -1;
297 if (argc > 0) {
298 realm = argv[0];
299 } else if (lp_realm() && *lp_realm()) {
300 realm = lp_realm();
301 } else {
302 char **realms;
304 rc = krb5_get_host_realm(ctx, NULL, &realms);
305 if (rc) {
306 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
307 error_message(rc)));
308 return -1;
310 realm = (const char *) *realms;
313 status = get_kdc_list(realm, NULL, &kdcs, &num_kdcs);
314 if (!NT_STATUS_IS_OK(status)) {
315 DEBUG(1,("get_kdc_list failed (%s)\n", nt_errstr(status)));
316 return -1;
319 for (i = 0; i < num_kdcs; i++) {
320 char addr[INET6_ADDRSTRLEN];
322 print_sockaddr(addr, sizeof(addr), &kdcs[i].ss);
324 d_printf("%s:%hd\n", addr, kdcs[i].port);
327 return 0;
328 #endif
329 DEBUG(1, ("No kerberos support\n"));
330 return -1;
333 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
335 const char *dom, *name;
336 struct dom_sid sid;
337 enum lsa_SidType type;
339 if (argc != 1) {
340 d_printf("%s\n%s",
341 _("Usage:"),
342 _(" net lookup name <name>\n"));
343 return -1;
346 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
347 &dom, &name, &sid, &type)) {
348 d_printf(_("Could not lookup name %s\n"), argv[0]);
349 return -1;
352 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
353 type, sid_type_lookup(type), dom, name);
354 return 0;
357 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
359 const char *dom, *name;
360 struct dom_sid sid;
361 enum lsa_SidType type;
363 if (argc != 1) {
364 d_printf("%s\n%s",
365 _("Usage:"),
366 _(" net lookup sid <sid>\n"));
367 return -1;
370 if (!string_to_sid(&sid, argv[0])) {
371 d_printf(_("Could not convert %s to SID\n"), argv[0]);
372 return -1;
375 if (!lookup_sid(talloc_tos(), &sid,
376 &dom, &name, &type)) {
377 d_printf(_("Could not lookup name %s\n"), argv[0]);
378 return -1;
381 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
382 type, sid_type_lookup(type), dom, name);
383 return 0;
386 static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
388 NTSTATUS status;
389 const char *domain_name = NULL;
390 const char *site_name = NULL;
391 uint32_t flags = 0;
392 struct netr_DsRGetDCNameInfo *info = NULL;
393 TALLOC_CTX *mem_ctx;
394 char *s = NULL;
396 if (argc < 1 || argc > 3) {
397 d_printf("%s\n%s",
398 _("Usage:"),
399 _(" net lookup dsgetdcname "
400 "<name> <flags> <sitename>\n"));
401 return -1;
404 mem_ctx = talloc_init("net_lookup_dsgetdcname");
405 if (!mem_ctx) {
406 return -1;
409 domain_name = argv[0];
411 if (argc >= 2)
412 sscanf(argv[1], "%x", &flags);
414 if (!flags) {
415 flags |= DS_DIRECTORY_SERVICE_REQUIRED;
418 if (argc == 3) {
419 site_name = argv[2];
422 if (!c->msg_ctx) {
423 d_fprintf(stderr, _("Could not initialise message context. "
424 "Try running as root\n"));
425 return -1;
428 status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
429 flags, &info);
430 if (!NT_STATUS_IS_OK(status)) {
431 d_printf(_("failed with: %s\n"), nt_errstr(status));
432 TALLOC_FREE(mem_ctx);
433 return -1;
436 s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
437 printf("%s\n", s);
438 TALLOC_FREE(s);
440 TALLOC_FREE(mem_ctx);
441 return 0;
445 /* lookup hosts or IP addresses using internal samba lookup fns */
446 int net_lookup(struct net_context *c, int argc, const char **argv)
448 int i;
450 struct functable table[] = {
451 {"HOST", net_lookup_host},
452 {"LDAP", net_lookup_ldap},
453 {"DC", net_lookup_dc},
454 {"PDC", net_lookup_pdc},
455 {"MASTER", net_lookup_master},
456 {"KDC", net_lookup_kdc},
457 {"NAME", net_lookup_name},
458 {"SID", net_lookup_sid},
459 {"DSGETDCNAME", net_lookup_dsgetdcname},
460 {NULL, NULL}
463 if (argc < 1) {
464 d_printf(_("\nUsage: \n"));
465 return net_lookup_usage(c, argc, argv);
467 for (i=0; table[i].funcname; i++) {
468 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
469 return table[i].fn(c, argc-1, argv+1);
472 /* Default to lookup a hostname so 'net lookup foo#1b' can be
473 used instead of 'net lookup host foo#1b'. The host syntax
474 is a bit confusing as non #00 names can't really be
475 considered hosts as such. */
477 return net_lookup_host(c, argc, argv);