/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- net_lookup_usage
- net_lookup_host
- print_ldap_srvlist
- net_lookup_ldap
- net_lookup_dc
- net_lookup_pdc
- net_lookup_master
- net_lookup_adskdc
- net_lookup_kdc
- net_lookup_name
- net_lookup_sid
- net_lookup_dsgetdcname
- net_lookup
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
22 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
23 {
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;
36 }
37
38 /* lookup a hostname giving an IP */
39 static int net_lookup_host(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
40 {
41 struct sockaddr_storage ss;
42 int name_type = 0x20;
43 char addr[INET6_ADDRSTRLEN];
44 const char *name = argv[0];
45 char *p;
46
47 if (argc == 0)
48 return net_lookup_usage(c, argc, argv);
49
50 p = strchr_m(name,'#');
51 if (p) {
52 *p = '\0';
53 sscanf(++p,"%x",&name_type);
54 }
55
56 if (!resolve_name(name, &ss, name_type)) {
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;
61 }
62
63 print_sockaddr(addr, sizeof(addr), &ss);
64 d_printf("%s\n", addr);
65 return 0;
66 }
67
68 #ifdef HAVE_ADS
69 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
/* [<][>][^][v][top][bottom][index][help] */
70 {
71 struct sockaddr_storage ss;
72 int i;
73
74 for ( i=0; i<numdcs; i++ ) {
75 if (resolve_name(dclist[i].hostname, &ss, 0x20) ) {
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);
81 }
82 #endif
83 if (ss.ss_family == AF_INET) {
84 d_printf("%s:%d\n", addr, dclist[i].port);
85 }
86 }
87 }
88 }
89 #endif
90
91 static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
92 {
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];
103
104 if (argc > 0)
105 domain = argv[0];
106 else
107 domain = c->opt_target_workgroup;
108
109 sitename = sitename_fetch(domain);
110
111 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
112 d_fprintf(stderr, "net_lookup_ldap: talloc_init() failed!\n");
113 SAFE_FREE(sitename);
114 return -1;
115 }
116
117 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
118
119 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
120 if ( NT_STATUS_IS_OK(status) && numdcs ) {
121 print_ldap_srvlist(dcs, numdcs);
122 TALLOC_FREE( ctx );
123 SAFE_FREE(sitename);
124 return 0;
125 }
126
127 DEBUG(9, ("Looking up PDC for domain %s\n", domain));
128 if (!get_pdc_ip(domain, &ss)) {
129 TALLOC_FREE( ctx );
130 SAFE_FREE(sitename);
131 return -1;
132 }
133
134 ret = sys_getnameinfo((struct sockaddr *)&ss,
135 sizeof(struct sockaddr_storage),
136 h_name, sizeof(h_name),
137 NULL, 0,
138 NI_NAMEREQD);
139
140 if (ret) {
141 TALLOC_FREE( ctx );
142 SAFE_FREE(sitename);
143 return -1;
144 }
145
146 DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
147 domain = strchr(h_name, '.');
148 if (!domain) {
149 TALLOC_FREE( ctx );
150 SAFE_FREE(sitename);
151 return -1;
152 }
153 domain++;
154
155 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
156
157 status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
158 if ( NT_STATUS_IS_OK(status) && numdcs ) {
159 print_ldap_srvlist(dcs, numdcs);
160 TALLOC_FREE( ctx );
161 SAFE_FREE(sitename);
162 return 0;
163 }
164
165 TALLOC_FREE( ctx );
166 SAFE_FREE(sitename);
167
168 return -1;
169 #endif
170 DEBUG(1,("No ADS support\n"));
171 return -1;
172 }
173
174 static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
175 {
176 struct ip_service *ip_list;
177 struct sockaddr_storage ss;
178 char *pdc_str = NULL;
179 const char *domain = NULL;
180 char *sitename = NULL;
181 int count, i;
182 char addr[INET6_ADDRSTRLEN];
183 bool sec_ads = (lp_security() == SEC_ADS);
184
185 if (sec_ads) {
186 domain = lp_realm();
187 } else {
188 domain = c->opt_target_workgroup;
189 }
190
191 if (argc > 0)
192 domain=argv[0];
193
194 /* first get PDC */
195 if (!get_pdc_ip(domain, &ss))
196 return -1;
197
198 print_sockaddr(addr, sizeof(addr), &ss);
199 if (asprintf(&pdc_str, "%s", addr) == -1) {
200 return -1;
201 }
202 d_printf("%s\n", pdc_str);
203
204 sitename = sitename_fetch(domain);
205 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
206 &ip_list, &count, sec_ads))) {
207 SAFE_FREE(pdc_str);
208 SAFE_FREE(sitename);
209 return 0;
210 }
211 SAFE_FREE(sitename);
212 for (i=0;i<count;i++) {
213 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
214 if (!strequal(pdc_str, addr))
215 d_printf("%s\n", addr);
216 }
217 SAFE_FREE(pdc_str);
218 return 0;
219 }
220
221 static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
222 {
223 struct sockaddr_storage ss;
224 char *pdc_str = NULL;
225 const char *domain;
226 char addr[INET6_ADDRSTRLEN];
227
228 if (lp_security() == SEC_ADS) {
229 domain = lp_realm();
230 } else {
231 domain = c->opt_target_workgroup;
232 }
233
234 if (argc > 0)
235 domain=argv[0];
236
237 /* first get PDC */
238 if (!get_pdc_ip(domain, &ss))
239 return -1;
240
241 print_sockaddr(addr, sizeof(addr), &ss);
242 if (asprintf(&pdc_str, "%s", addr) == -1) {
243 return -1;
244 }
245 d_printf("%s\n", pdc_str);
246 SAFE_FREE(pdc_str);
247 return 0;
248 }
249
250
251 static int net_lookup_master(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
252 {
253 struct sockaddr_storage master_ss;
254 const char *domain = c->opt_target_workgroup;
255 char addr[INET6_ADDRSTRLEN];
256
257 if (argc > 0)
258 domain=argv[0];
259
260 if (!find_master_ip(domain, &master_ss))
261 return -1;
262 print_sockaddr(addr, sizeof(addr), &master_ss);
263 d_printf("%s\n", addr);
264 return 0;
265 }
266
267
268 /* Look up a KDC using the documented AD SRV records
269 only. Does not need ADS configured or even built */
270
271 static int net_lookup_adskdc(int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
272 {
273 char *realm=NULL;
274 char *sitename=NULL;
275 struct dns_rr_srv *dclist;
276 int numdcs, i;
277 NTSTATUS status;
278 struct addrinfo hints;
279
280 /* First, see if the realm and sitename were
281 explicitly specified */
282
283 if (argc > 0)
284 realm = talloc_strdup(NULL, argv[0]);
285 if (argc > 1)
286 sitename = talloc_strdup(realm, argv[1]);
287
288 #ifdef HAVE_ADS
289 if (!realm && (lp_security() == SEC_ADS)) {
290 /* auto-detect realm and site if we can */
291 }
292 #endif
293
294 if (!realm) {
295 d_printf("No realm specified!\n");
296 return -1;
297 }
298
299 status = ads_dns_query_kdcs(realm, realm,
300 sitename,
301 &dclist, &numdcs);
302
303 if (!NT_STATUS_IS_OK(status)) {
304 d_printf("No KDC's found.\n");
305 TALLOC_FREE(realm);
306 return -1;
307 }
308
309 ZERO_STRUCT(hints);
310 /* only request IPv[46] address if
311 we have one configured - for the future
312 will require better logic on displaying
313 the returned addresses.
314 */
315 hints.ai_flags = AI_ADDRCONFIG;
316 hints.ai_socktype = SOCK_DGRAM;
317
318 for(i=0; i<numdcs; i++) {
319 struct addrinfo *out;
320 int ret;
321 char *fq_hostname = talloc_asprintf(realm, "%s.",
322 dclist[i].hostname);
323
324 ret = getaddrinfo(fq_hostname,
325 NULL, &hints, &out);
326 if (ret == 0) {
327 struct addrinfo *cur = out;
328 char *num_host = talloc_array(realm, char, NI_MAXHOST);
329
330 while(cur) {
331 if (cur->ai_family == AF_INET ||
332 cur->ai_family == AF_INET6) {
333 getnameinfo((struct sockaddr *) cur->ai_addr,
334 cur->ai_addrlen, num_host, NI_MAXHOST,
335 NULL, 0, NI_NUMERICHOST);
336 d_printf("%s \t %s\n",
337 num_host,
338 dclist[i].hostname);
339 }
340 cur = cur->ai_next;
341 }
342 freeaddrinfo(out);
343 }
344 }
345 TALLOC_FREE(realm);
346 return 0;
347 }
348
349
350 static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
351 {
352 #ifdef HAVE_KRB5
353 krb5_error_code rc;
354 krb5_context ctx;
355 struct sockaddr_in *addrs;
356 int num_kdcs,i;
357 krb5_data realm;
358 char **realms;
359
360 initialize_krb5_error_table();
361 rc = krb5_init_context(&ctx);
362 if (rc) {
363 DEBUG(1,("krb5_init_context failed (%s)\n",
364 error_message(rc)));
365 return -1;
366 }
367
368 if (argc>0) {
369 realm.data = CONST_DISCARD(char *, argv[0]);
370 realm.length = strlen(argv[0]);
371 } else if (lp_realm() && *lp_realm()) {
372 realm.data = lp_realm();
373 realm.length = strlen((const char *)realm.data);
374 } else {
375 rc = krb5_get_host_realm(ctx, NULL, &realms);
376 if (rc) {
377 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
378 error_message(rc)));
379 return -1;
380 }
381 realm.data = (char *) *realms;
382 realm.length = strlen((const char *)realm.data);
383 }
384
385 rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
386 if (rc) {
387 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
388 return -1;
389 }
390 for (i=0;i<num_kdcs;i++)
391 if (addrs[i].sin_family == AF_INET)
392 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
393 ntohs(addrs[i].sin_port));
394 return 0;
395
396 #endif
397 DEBUG(1, ("No kerberos support\n"));
398 return -1;
399 }
400
401 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
402 {
403 const char *dom, *name;
404 DOM_SID sid;
405 enum lsa_SidType type;
406
407 if (argc != 1) {
408 d_printf("usage: net lookup name <name>\n");
409 return -1;
410 }
411
412 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
413 &dom, &name, &sid, &type)) {
414 d_printf("Could not lookup name %s\n", argv[0]);
415 return -1;
416 }
417
418 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
419 type, sid_type_lookup(type), dom, name);
420 return 0;
421 }
422
423 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
424 {
425 const char *dom, *name;
426 DOM_SID sid;
427 enum lsa_SidType type;
428
429 if (argc != 1) {
430 d_printf("usage: net lookup sid <sid>\n");
431 return -1;
432 }
433
434 if (!string_to_sid(&sid, argv[0])) {
435 d_printf("Could not convert %s to SID\n", argv[0]);
436 return -1;
437 }
438
439 if (!lookup_sid(talloc_tos(), &sid,
440 &dom, &name, &type)) {
441 d_printf("Could not lookup name %s\n", argv[0]);
442 return -1;
443 }
444
445 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
446 type, sid_type_lookup(type), dom, name);
447 return 0;
448 }
449
450 static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
451 {
452 NTSTATUS status;
453 const char *domain_name = NULL;
454 const char *site_name = NULL;
455 uint32_t flags = 0;
456 struct netr_DsRGetDCNameInfo *info = NULL;
457 TALLOC_CTX *mem_ctx;
458 char *s = NULL;
459
460 if (argc < 1 || argc > 3) {
461 d_printf("usage: net lookup dsgetdcname "
462 "<name> <flags> <sitename>\n");
463 return -1;
464 }
465
466 mem_ctx = talloc_init("net_lookup_dsgetdcname");
467 if (!mem_ctx) {
468 return -1;
469 }
470
471 domain_name = argv[0];
472
473 if (argc >= 2)
474 sscanf(argv[1], "%x", &flags);
475
476 if (!flags) {
477 flags |= DS_DIRECTORY_SERVICE_REQUIRED;
478 }
479
480 if (argc == 3) {
481 site_name = argv[2];
482 }
483
484 status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, site_name,
485 flags, &info);
486 if (!NT_STATUS_IS_OK(status)) {
487 d_printf("failed with: %s\n", nt_errstr(status));
488 TALLOC_FREE(mem_ctx);
489 return -1;
490 }
491
492 s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
493 printf("%s\n", s);
494 TALLOC_FREE(s);
495
496 TALLOC_FREE(mem_ctx);
497 return 0;
498 }
499
500
501 /* lookup hosts or IP addresses using internal samba lookup fns */
502 int net_lookup(struct net_context *c, int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
503 {
504 int i;
505
506 struct functable table[] = {
507 {"HOST", net_lookup_host},
508 {"LDAP", net_lookup_ldap},
509 {"DC", net_lookup_dc},
510 {"PDC", net_lookup_pdc},
511 {"MASTER", net_lookup_master},
512 {"ADSKDC", net_lookup_adskdc},
513 {"KDC", net_lookup_kdc},
514 {"NAME", net_lookup_name},
515 {"SID", net_lookup_sid},
516 {"DSGETDCNAME", net_lookup_dsgetdcname},
517 {NULL, NULL}
518 };
519
520 if (argc < 1) {
521 d_printf("\nUsage: \n");
522 return net_lookup_usage(c, argc, argv);
523 }
524 for (i=0; table[i].funcname; i++) {
525 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
526 return table[i].fn(c, argc-1, argv+1);
527 }
528
529 /* Default to lookup a hostname so 'net lookup foo#1b' can be
530 used instead of 'net lookup host foo#1b'. The host syntax
531 is a bit confusing as non #00 names can't really be
532 considered hosts as such. */
533
534 return net_lookup_host(c, argc, argv);
535 }