s3: Fix uninitialized memory read in talloc_free()
[Samba.git] / source3 / utils / net_lookup.c
blob658e904f59bb6dd2ea7c315bf274cc4acabc1a29
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"
22 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
24 d_printf(_(
25 " net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
26 " net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
27 " net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
28 " net lookup pdc [domain|realm]\n\tgives IP of realm's kerberos KDC\n\n"
29 " net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n"
30 " net lookup master [domain|wg]\n\tgive IP of master browser\n\n"
31 " net lookup name [name]\n\tLookup name's sid and type\n\n"
32 " net lookup sid [sid]\n\tGive sid's name and type\n\n"
33 " net lookup dsgetdcname [name] [flags] [sitename]\n\n"
34 ));
35 return -1;
38 /* lookup a hostname giving an IP */
39 static int net_lookup_host(struct net_context *c, int argc, const char **argv)
41 struct sockaddr_storage ss;
42 int name_type = 0x20;
43 char addr[INET6_ADDRSTRLEN];
44 const char *name = argv[0];
45 char *p;
47 if (argc == 0)
48 return net_lookup_usage(c, argc, argv);
50 p = strchr_m(name,'#');
51 if (p) {
52 *p = '\0';
53 sscanf(++p,"%x",&name_type);
56 if (!resolve_name(name, &ss, name_type, false)) {
57 /* we deliberately use DEBUG() here to send it to stderr
58 so scripts aren't mucked up */
59 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
60 return -1;
63 print_sockaddr(addr, sizeof(addr), &ss);
64 d_printf("%s\n", addr);
65 return 0;
68 #ifdef HAVE_ADS
69 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
71 struct sockaddr_storage ss;
72 int i;
74 for ( i=0; i<numdcs; i++ ) {
75 if (resolve_name(dclist[i].hostname, &ss, 0x20, true) ) {
76 char addr[INET6_ADDRSTRLEN];
77 print_sockaddr(addr, sizeof(addr), &ss);
78 #ifdef HAVE_IPV6
79 if (ss.ss_family == AF_INET6) {
80 d_printf("[%s]:%d\n", addr, dclist[i].port);
82 #endif
83 if (ss.ss_family == AF_INET) {
84 d_printf("%s:%d\n", addr, dclist[i].port);
89 #endif
91 static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
93 #ifdef HAVE_ADS
94 const char *domain;
95 struct sockaddr_storage ss;
96 struct dns_rr_srv *dcs = NULL;
97 int numdcs = 0;
98 char *sitename;
99 TALLOC_CTX *ctx;
100 NTSTATUS status;
101 int ret;
102 char h_name[MAX_DNS_NAME_LENGTH];
104 if (argc > 0)
105 domain = argv[0];
106 else
107 domain = c->opt_target_workgroup;
109 sitename = sitename_fetch(domain);
111 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
112 d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
113 _("failed"));
114 SAFE_FREE(sitename);
115 return -1;
118 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
120 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
121 if ( NT_STATUS_IS_OK(status) && numdcs ) {
122 print_ldap_srvlist(dcs, numdcs);
123 TALLOC_FREE( ctx );
124 SAFE_FREE(sitename);
125 return 0;
128 DEBUG(9, ("Looking up PDC for domain %s\n", domain));
129 if (!get_pdc_ip(domain, &ss)) {
130 TALLOC_FREE( ctx );
131 SAFE_FREE(sitename);
132 return -1;
135 ret = sys_getnameinfo((struct sockaddr *)&ss,
136 sizeof(struct sockaddr_storage),
137 h_name, sizeof(h_name),
138 NULL, 0,
139 NI_NAMEREQD);
141 if (ret) {
142 TALLOC_FREE( ctx );
143 SAFE_FREE(sitename);
144 return -1;
147 DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
148 domain = strchr(h_name, '.');
149 if (!domain) {
150 TALLOC_FREE( ctx );
151 SAFE_FREE(sitename);
152 return -1;
154 domain++;
156 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
158 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
159 if ( NT_STATUS_IS_OK(status) && numdcs ) {
160 print_ldap_srvlist(dcs, numdcs);
161 TALLOC_FREE( ctx );
162 SAFE_FREE(sitename);
163 return 0;
166 TALLOC_FREE( ctx );
167 SAFE_FREE(sitename);
169 return -1;
170 #endif
171 DEBUG(1,("No ADS support\n"));
172 return -1;
175 static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
177 struct ip_service *ip_list;
178 struct sockaddr_storage ss;
179 char *pdc_str = NULL;
180 const char *domain = NULL;
181 char *sitename = NULL;
182 int count, i;
183 char addr[INET6_ADDRSTRLEN];
184 bool sec_ads = (lp_security() == SEC_ADS);
186 if (sec_ads) {
187 domain = lp_realm();
188 } else {
189 domain = c->opt_target_workgroup;
192 if (argc > 0)
193 domain=argv[0];
195 /* first get PDC */
196 if (!get_pdc_ip(domain, &ss))
197 return -1;
199 print_sockaddr(addr, sizeof(addr), &ss);
200 if (asprintf(&pdc_str, "%s", addr) == -1) {
201 return -1;
203 d_printf("%s\n", pdc_str);
205 sitename = sitename_fetch(domain);
206 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
207 &ip_list, &count, sec_ads))) {
208 SAFE_FREE(pdc_str);
209 SAFE_FREE(sitename);
210 return 0;
212 SAFE_FREE(sitename);
213 for (i=0;i<count;i++) {
214 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
215 if (!strequal(pdc_str, addr))
216 d_printf("%s\n", addr);
218 SAFE_FREE(pdc_str);
219 return 0;
222 static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
224 struct sockaddr_storage ss;
225 char *pdc_str = NULL;
226 const char *domain;
227 char addr[INET6_ADDRSTRLEN];
229 if (lp_security() == SEC_ADS) {
230 domain = lp_realm();
231 } else {
232 domain = c->opt_target_workgroup;
235 if (argc > 0)
236 domain=argv[0];
238 /* first get PDC */
239 if (!get_pdc_ip(domain, &ss))
240 return -1;
242 print_sockaddr(addr, sizeof(addr), &ss);
243 if (asprintf(&pdc_str, "%s", addr) == -1) {
244 return -1;
246 d_printf("%s\n", pdc_str);
247 SAFE_FREE(pdc_str);
248 return 0;
252 static int net_lookup_master(struct net_context *c, int argc, const char **argv)
254 struct sockaddr_storage master_ss;
255 const char *domain = c->opt_target_workgroup;
256 char addr[INET6_ADDRSTRLEN];
258 if (argc > 0)
259 domain=argv[0];
261 if (!find_master_ip(domain, &master_ss))
262 return -1;
263 print_sockaddr(addr, sizeof(addr), &master_ss);
264 d_printf("%s\n", addr);
265 return 0;
268 static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
270 #ifdef HAVE_KRB5
271 krb5_error_code rc;
272 krb5_context ctx;
273 struct sockaddr_in *addrs;
274 int num_kdcs,i;
275 krb5_data realm;
276 char **realms;
278 initialize_krb5_error_table();
279 rc = krb5_init_context(&ctx);
280 if (rc) {
281 DEBUG(1,("krb5_init_context failed (%s)\n",
282 error_message(rc)));
283 return -1;
286 if (argc>0) {
287 realm.data = CONST_DISCARD(char *, argv[0]);
288 realm.length = strlen(argv[0]);
289 } else if (lp_realm() && *lp_realm()) {
290 realm.data = lp_realm();
291 realm.length = strlen((const char *)realm.data);
292 } else {
293 rc = krb5_get_host_realm(ctx, NULL, &realms);
294 if (rc) {
295 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
296 error_message(rc)));
297 return -1;
299 realm.data = (char *) *realms;
300 realm.length = strlen((const char *)realm.data);
303 rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
304 if (rc) {
305 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
306 return -1;
308 for (i=0;i<num_kdcs;i++)
309 if (addrs[i].sin_family == AF_INET)
310 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
311 ntohs(addrs[i].sin_port));
312 return 0;
314 #endif
315 DEBUG(1, ("No kerberos support\n"));
316 return -1;
319 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
321 const char *dom, *name;
322 DOM_SID sid;
323 enum lsa_SidType type;
325 if (argc != 1) {
326 d_printf("%s\n%s",
327 _("Usage:"),
328 _(" net lookup name <name>\n"));
329 return -1;
332 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
333 &dom, &name, &sid, &type)) {
334 d_printf(_("Could not lookup name %s\n"), argv[0]);
335 return -1;
338 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
339 type, sid_type_lookup(type), dom, name);
340 return 0;
343 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
345 const char *dom, *name;
346 DOM_SID sid;
347 enum lsa_SidType type;
349 if (argc != 1) {
350 d_printf("%s\n%s",
351 _("Usage:"),
352 _(" net lookup sid <sid>\n"));
353 return -1;
356 if (!string_to_sid(&sid, argv[0])) {
357 d_printf(_("Could not convert %s to SID\n"), argv[0]);
358 return -1;
361 if (!lookup_sid(talloc_tos(), &sid,
362 &dom, &name, &type)) {
363 d_printf(_("Could not lookup name %s\n"), argv[0]);
364 return -1;
367 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
368 type, sid_type_lookup(type), dom, name);
369 return 0;
372 static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
374 NTSTATUS status;
375 const char *domain_name = NULL;
376 const char *site_name = NULL;
377 uint32_t flags = 0;
378 struct netr_DsRGetDCNameInfo *info = NULL;
379 TALLOC_CTX *mem_ctx;
380 char *s = NULL;
382 if (argc < 1 || argc > 3) {
383 d_printf("%s\n%s",
384 _("Usage:"),
385 _(" net lookup dsgetdcname "
386 "<name> <flags> <sitename>\n"));
387 return -1;
390 mem_ctx = talloc_init("net_lookup_dsgetdcname");
391 if (!mem_ctx) {
392 return -1;
395 domain_name = argv[0];
397 if (argc >= 2)
398 sscanf(argv[1], "%x", &flags);
400 if (!flags) {
401 flags |= DS_DIRECTORY_SERVICE_REQUIRED;
404 if (argc == 3) {
405 site_name = argv[2];
408 status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, site_name,
409 flags, &info);
410 if (!NT_STATUS_IS_OK(status)) {
411 d_printf(_("failed with: %s\n"), nt_errstr(status));
412 TALLOC_FREE(mem_ctx);
413 return -1;
416 s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
417 printf("%s\n", s);
418 TALLOC_FREE(s);
420 TALLOC_FREE(mem_ctx);
421 return 0;
425 /* lookup hosts or IP addresses using internal samba lookup fns */
426 int net_lookup(struct net_context *c, int argc, const char **argv)
428 int i;
430 struct functable table[] = {
431 {"HOST", net_lookup_host},
432 {"LDAP", net_lookup_ldap},
433 {"DC", net_lookup_dc},
434 {"PDC", net_lookup_pdc},
435 {"MASTER", net_lookup_master},
436 {"KDC", net_lookup_kdc},
437 {"NAME", net_lookup_name},
438 {"SID", net_lookup_sid},
439 {"DSGETDCNAME", net_lookup_dsgetdcname},
440 {NULL, NULL}
443 if (argc < 1) {
444 d_printf(_("\nUsage: \n"));
445 return net_lookup_usage(c, argc, argv);
447 for (i=0; table[i].funcname; i++) {
448 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
449 return table[i].fn(c, argc-1, argv+1);
452 /* Default to lookup a hostname so 'net lookup foo#1b' can be
453 used instead of 'net lookup host foo#1b'. The host syntax
454 is a bit confusing as non #00 names can't really be
455 considered hosts as such. */
457 return net_lookup_host(c, argc, argv);