s4-python: Add missing prototypes.
[Samba.git] / source3 / utils / net_lookup.c
blob2afa75c609e2c1e9a14287cf2de44e8b694cb3fd
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 "libads/dns.h"
23 #include "../librpc/gen_ndr/ndr_netlogon.h"
24 #include "smb_krb5.h"
25 #include "../libcli/security/security.h"
27 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
29 d_printf(_(
30 " net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
31 " net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
32 " net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
33 " net lookup pdc [domain|realm]\n\tgives IP of realm's kerberos KDC\n\n"
34 " net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n"
35 " net lookup master [domain|wg]\n\tgive IP of master browser\n\n"
36 " net lookup name [name]\n\tLookup name's sid and type\n\n"
37 " net lookup sid [sid]\n\tGive sid's name and type\n\n"
38 " net lookup dsgetdcname [name] [flags] [sitename]\n\n"
39 ));
40 return -1;
43 /* lookup a hostname giving an IP */
44 static int net_lookup_host(struct net_context *c, int argc, const char **argv)
46 struct sockaddr_storage ss;
47 int name_type = 0x20;
48 char addr[INET6_ADDRSTRLEN];
49 const char *name = argv[0];
50 char *p;
52 if (argc == 0)
53 return net_lookup_usage(c, argc, argv);
55 p = strchr_m(name,'#');
56 if (p) {
57 *p = '\0';
58 sscanf(++p,"%x",&name_type);
61 if (!resolve_name(name, &ss, name_type, false)) {
62 /* we deliberately use DEBUG() here to send it to stderr
63 so scripts aren't mucked up */
64 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
65 return -1;
68 print_sockaddr(addr, sizeof(addr), &ss);
69 d_printf("%s\n", addr);
70 return 0;
73 #ifdef HAVE_ADS
74 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
76 struct sockaddr_storage ss;
77 int i;
79 for ( i=0; i<numdcs; i++ ) {
80 if (resolve_name(dclist[i].hostname, &ss, 0x20, true) ) {
81 char addr[INET6_ADDRSTRLEN];
82 print_sockaddr(addr, sizeof(addr), &ss);
83 #ifdef HAVE_IPV6
84 if (ss.ss_family == AF_INET6) {
85 d_printf("[%s]:%d\n", addr, dclist[i].port);
87 #endif
88 if (ss.ss_family == AF_INET) {
89 d_printf("%s:%d\n", addr, dclist[i].port);
94 #endif
96 static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
98 #ifdef HAVE_ADS
99 const char *domain;
100 struct sockaddr_storage ss;
101 struct dns_rr_srv *dcs = NULL;
102 int numdcs = 0;
103 char *sitename;
104 TALLOC_CTX *ctx;
105 NTSTATUS status;
106 int ret;
107 char h_name[MAX_DNS_NAME_LENGTH];
109 if (argc > 0)
110 domain = argv[0];
111 else
112 domain = c->opt_target_workgroup;
114 sitename = sitename_fetch(domain);
116 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
117 d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
118 _("failed"));
119 SAFE_FREE(sitename);
120 return -1;
123 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
125 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
126 if ( NT_STATUS_IS_OK(status) && numdcs ) {
127 print_ldap_srvlist(dcs, numdcs);
128 TALLOC_FREE( ctx );
129 SAFE_FREE(sitename);
130 return 0;
133 DEBUG(9, ("Looking up PDC for domain %s\n", domain));
134 if (!get_pdc_ip(domain, &ss)) {
135 TALLOC_FREE( ctx );
136 SAFE_FREE(sitename);
137 return -1;
140 ret = sys_getnameinfo((struct sockaddr *)&ss,
141 sizeof(struct sockaddr_storage),
142 h_name, sizeof(h_name),
143 NULL, 0,
144 NI_NAMEREQD);
146 if (ret) {
147 TALLOC_FREE( ctx );
148 SAFE_FREE(sitename);
149 return -1;
152 DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
153 domain = strchr(h_name, '.');
154 if (!domain) {
155 TALLOC_FREE( ctx );
156 SAFE_FREE(sitename);
157 return -1;
159 domain++;
161 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
163 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
164 if ( NT_STATUS_IS_OK(status) && numdcs ) {
165 print_ldap_srvlist(dcs, numdcs);
166 TALLOC_FREE( ctx );
167 SAFE_FREE(sitename);
168 return 0;
171 TALLOC_FREE( ctx );
172 SAFE_FREE(sitename);
174 return -1;
175 #endif
176 DEBUG(1,("No ADS support\n"));
177 return -1;
180 static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
182 struct ip_service *ip_list;
183 struct sockaddr_storage ss;
184 char *pdc_str = NULL;
185 const char *domain = NULL;
186 char *sitename = NULL;
187 int count, i;
188 char addr[INET6_ADDRSTRLEN];
189 bool sec_ads = (lp_security() == SEC_ADS);
191 if (sec_ads) {
192 domain = lp_realm();
193 } else {
194 domain = c->opt_target_workgroup;
197 if (argc > 0)
198 domain=argv[0];
200 /* first get PDC */
201 if (!get_pdc_ip(domain, &ss))
202 return -1;
204 print_sockaddr(addr, sizeof(addr), &ss);
205 if (asprintf(&pdc_str, "%s", addr) == -1) {
206 return -1;
208 d_printf("%s\n", pdc_str);
210 sitename = sitename_fetch(domain);
211 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
212 &ip_list, &count, sec_ads))) {
213 SAFE_FREE(pdc_str);
214 SAFE_FREE(sitename);
215 return 0;
217 SAFE_FREE(sitename);
218 for (i=0;i<count;i++) {
219 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
220 if (!strequal(pdc_str, addr))
221 d_printf("%s\n", addr);
223 SAFE_FREE(pdc_str);
224 return 0;
227 static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
229 struct sockaddr_storage ss;
230 char *pdc_str = NULL;
231 const char *domain;
232 char addr[INET6_ADDRSTRLEN];
234 if (lp_security() == SEC_ADS) {
235 domain = lp_realm();
236 } else {
237 domain = c->opt_target_workgroup;
240 if (argc > 0)
241 domain=argv[0];
243 /* first get PDC */
244 if (!get_pdc_ip(domain, &ss))
245 return -1;
247 print_sockaddr(addr, sizeof(addr), &ss);
248 if (asprintf(&pdc_str, "%s", addr) == -1) {
249 return -1;
251 d_printf("%s\n", pdc_str);
252 SAFE_FREE(pdc_str);
253 return 0;
257 static int net_lookup_master(struct net_context *c, int argc, const char **argv)
259 struct sockaddr_storage master_ss;
260 const char *domain = c->opt_target_workgroup;
261 char addr[INET6_ADDRSTRLEN];
263 if (argc > 0)
264 domain=argv[0];
266 if (!find_master_ip(domain, &master_ss))
267 return -1;
268 print_sockaddr(addr, sizeof(addr), &master_ss);
269 d_printf("%s\n", addr);
270 return 0;
273 static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
275 #ifdef HAVE_KRB5
276 krb5_error_code rc;
277 krb5_context ctx;
278 struct sockaddr_in *addrs;
279 int num_kdcs,i;
280 krb5_data realm;
281 char **realms;
283 initialize_krb5_error_table();
284 rc = krb5_init_context(&ctx);
285 if (rc) {
286 DEBUG(1,("krb5_init_context failed (%s)\n",
287 error_message(rc)));
288 return -1;
291 if (argc>0) {
292 realm.data = CONST_DISCARD(char *, argv[0]);
293 realm.length = strlen(argv[0]);
294 } else if (lp_realm() && *lp_realm()) {
295 realm.data = lp_realm();
296 realm.length = strlen((const char *)realm.data);
297 } else {
298 rc = krb5_get_host_realm(ctx, NULL, &realms);
299 if (rc) {
300 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
301 error_message(rc)));
302 return -1;
304 realm.data = (char *) *realms;
305 realm.length = strlen((const char *)realm.data);
308 rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
309 if (rc) {
310 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
311 return -1;
313 for (i=0;i<num_kdcs;i++)
314 if (addrs[i].sin_family == AF_INET)
315 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
316 ntohs(addrs[i].sin_port));
317 return 0;
319 #endif
320 DEBUG(1, ("No kerberos support\n"));
321 return -1;
324 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
326 const char *dom, *name;
327 struct dom_sid sid;
328 enum lsa_SidType type;
330 if (argc != 1) {
331 d_printf("%s\n%s",
332 _("Usage:"),
333 _(" net lookup name <name>\n"));
334 return -1;
337 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
338 &dom, &name, &sid, &type)) {
339 d_printf(_("Could not lookup name %s\n"), argv[0]);
340 return -1;
343 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
344 type, sid_type_lookup(type), dom, name);
345 return 0;
348 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
350 const char *dom, *name;
351 struct dom_sid sid;
352 enum lsa_SidType type;
354 if (argc != 1) {
355 d_printf("%s\n%s",
356 _("Usage:"),
357 _(" net lookup sid <sid>\n"));
358 return -1;
361 if (!string_to_sid(&sid, argv[0])) {
362 d_printf(_("Could not convert %s to SID\n"), argv[0]);
363 return -1;
366 if (!lookup_sid(talloc_tos(), &sid,
367 &dom, &name, &type)) {
368 d_printf(_("Could not lookup name %s\n"), argv[0]);
369 return -1;
372 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
373 type, sid_type_lookup(type), dom, name);
374 return 0;
377 static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
379 NTSTATUS status;
380 const char *domain_name = NULL;
381 const char *site_name = NULL;
382 uint32_t flags = 0;
383 struct netr_DsRGetDCNameInfo *info = NULL;
384 TALLOC_CTX *mem_ctx;
385 char *s = NULL;
387 if (argc < 1 || argc > 3) {
388 d_printf("%s\n%s",
389 _("Usage:"),
390 _(" net lookup dsgetdcname "
391 "<name> <flags> <sitename>\n"));
392 return -1;
395 mem_ctx = talloc_init("net_lookup_dsgetdcname");
396 if (!mem_ctx) {
397 return -1;
400 domain_name = argv[0];
402 if (argc >= 2)
403 sscanf(argv[1], "%x", &flags);
405 if (!flags) {
406 flags |= DS_DIRECTORY_SERVICE_REQUIRED;
409 if (argc == 3) {
410 site_name = argv[2];
413 if (!c->msg_ctx) {
414 d_fprintf(stderr, _("Could not initialise message context. "
415 "Try running as root\n"));
416 return -1;
419 status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
420 flags, &info);
421 if (!NT_STATUS_IS_OK(status)) {
422 d_printf(_("failed with: %s\n"), nt_errstr(status));
423 TALLOC_FREE(mem_ctx);
424 return -1;
427 s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
428 printf("%s\n", s);
429 TALLOC_FREE(s);
431 TALLOC_FREE(mem_ctx);
432 return 0;
436 /* lookup hosts or IP addresses using internal samba lookup fns */
437 int net_lookup(struct net_context *c, int argc, const char **argv)
439 int i;
441 struct functable table[] = {
442 {"HOST", net_lookup_host},
443 {"LDAP", net_lookup_ldap},
444 {"DC", net_lookup_dc},
445 {"PDC", net_lookup_pdc},
446 {"MASTER", net_lookup_master},
447 {"KDC", net_lookup_kdc},
448 {"NAME", net_lookup_name},
449 {"SID", net_lookup_sid},
450 {"DSGETDCNAME", net_lookup_dsgetdcname},
451 {NULL, NULL}
454 if (argc < 1) {
455 d_printf(_("\nUsage: \n"));
456 return net_lookup_usage(c, argc, argv);
458 for (i=0; table[i].funcname; i++) {
459 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
460 return table[i].fn(c, argc-1, argv+1);
463 /* Default to lookup a hostname so 'net lookup foo#1b' can be
464 used instead of 'net lookup host foo#1b'. The host syntax
465 is a bit confusing as non #00 names can't really be
466 considered hosts as such. */
468 return net_lookup_host(c, argc, argv);