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 | #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"
|
---|
26 | #include "passdb/lookup_sid.h"
|
---|
27 |
|
---|
28 | int net_lookup_usage(struct net_context *c, int argc, const char **argv)
|
---|
29 | {
|
---|
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;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* lookup a hostname giving an IP */
|
---|
45 | static int net_lookup_host(struct net_context *c, int argc, const char **argv)
|
---|
46 | {
|
---|
47 | struct sockaddr_storage ss;
|
---|
48 | int name_type = 0x20;
|
---|
49 | char addr[INET6_ADDRSTRLEN];
|
---|
50 | const char *name = argv[0];
|
---|
51 | char *p;
|
---|
52 |
|
---|
53 | if (argc == 0)
|
---|
54 | return net_lookup_usage(c, argc, argv);
|
---|
55 |
|
---|
56 | p = strchr_m(name,'#');
|
---|
57 | if (p) {
|
---|
58 | *p = '\0';
|
---|
59 | sscanf(++p,"%x",&name_type);
|
---|
60 | }
|
---|
61 |
|
---|
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;
|
---|
67 | }
|
---|
68 |
|
---|
69 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
70 | d_printf("%s\n", addr);
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | #ifdef HAVE_ADS
|
---|
75 | static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
|
---|
76 | {
|
---|
77 | struct sockaddr_storage ss;
|
---|
78 | int i;
|
---|
79 |
|
---|
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);
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 | if (ss.ss_family == AF_INET) {
|
---|
90 | d_printf("%s:%d\n", addr, dclist[i].port);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
|
---|
98 | {
|
---|
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 |
|
---|
110 | if (argc > 0)
|
---|
111 | domain = argv[0];
|
---|
112 | else
|
---|
113 | domain = c->opt_target_workgroup;
|
---|
114 |
|
---|
115 | sitename = sitename_fetch(domain);
|
---|
116 |
|
---|
117 | if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
|
---|
118 | d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
|
---|
119 | _("failed"));
|
---|
120 | SAFE_FREE(sitename);
|
---|
121 | return -1;
|
---|
122 | }
|
---|
123 |
|
---|
124 | DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
|
---|
125 |
|
---|
126 | status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
|
---|
127 | if ( NT_STATUS_IS_OK(status) && numdcs ) {
|
---|
128 | print_ldap_srvlist(dcs, numdcs);
|
---|
129 | TALLOC_FREE( ctx );
|
---|
130 | SAFE_FREE(sitename);
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | DEBUG(9, ("Looking up PDC for domain %s\n", domain));
|
---|
135 | if (!get_pdc_ip(domain, &ss)) {
|
---|
136 | TALLOC_FREE( ctx );
|
---|
137 | SAFE_FREE(sitename);
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | ret = sys_getnameinfo((struct sockaddr *)&ss,
|
---|
142 | sizeof(struct sockaddr_storage),
|
---|
143 | h_name, sizeof(h_name),
|
---|
144 | NULL, 0,
|
---|
145 | NI_NAMEREQD);
|
---|
146 |
|
---|
147 | if (ret) {
|
---|
148 | TALLOC_FREE( ctx );
|
---|
149 | SAFE_FREE(sitename);
|
---|
150 | return -1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | DEBUG(9, ("Found PDC with DNS name %s\n", h_name));
|
---|
154 | domain = strchr(h_name, '.');
|
---|
155 | if (!domain) {
|
---|
156 | TALLOC_FREE( ctx );
|
---|
157 | SAFE_FREE(sitename);
|
---|
158 | return -1;
|
---|
159 | }
|
---|
160 | domain++;
|
---|
161 |
|
---|
162 | DEBUG(9, ("Looking up ldap for domain %s\n", domain));
|
---|
163 |
|
---|
164 | status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
|
---|
165 | if ( NT_STATUS_IS_OK(status) && numdcs ) {
|
---|
166 | print_ldap_srvlist(dcs, numdcs);
|
---|
167 | TALLOC_FREE( ctx );
|
---|
168 | SAFE_FREE(sitename);
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | TALLOC_FREE( ctx );
|
---|
173 | SAFE_FREE(sitename);
|
---|
174 |
|
---|
175 | return -1;
|
---|
176 | #endif
|
---|
177 | DEBUG(1,("No ADS support\n"));
|
---|
178 | return -1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
|
---|
182 | {
|
---|
183 | struct ip_service *ip_list;
|
---|
184 | struct sockaddr_storage ss;
|
---|
185 | char *pdc_str = NULL;
|
---|
186 | const char *domain = NULL;
|
---|
187 | char *sitename = NULL;
|
---|
188 | int count, i;
|
---|
189 | char addr[INET6_ADDRSTRLEN];
|
---|
190 | bool sec_ads = (lp_security() == SEC_ADS);
|
---|
191 |
|
---|
192 | if (sec_ads) {
|
---|
193 | domain = lp_realm();
|
---|
194 | } else {
|
---|
195 | domain = c->opt_target_workgroup;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (argc > 0)
|
---|
199 | domain=argv[0];
|
---|
200 |
|
---|
201 | /* first get PDC */
|
---|
202 | if (!get_pdc_ip(domain, &ss))
|
---|
203 | return -1;
|
---|
204 |
|
---|
205 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
206 | if (asprintf(&pdc_str, "%s", addr) == -1) {
|
---|
207 | return -1;
|
---|
208 | }
|
---|
209 | d_printf("%s\n", pdc_str);
|
---|
210 |
|
---|
211 | sitename = sitename_fetch(domain);
|
---|
212 | if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
|
---|
213 | &ip_list, &count, sec_ads))) {
|
---|
214 | SAFE_FREE(pdc_str);
|
---|
215 | SAFE_FREE(sitename);
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 | SAFE_FREE(sitename);
|
---|
219 | for (i=0;i<count;i++) {
|
---|
220 | print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
|
---|
221 | if (!strequal(pdc_str, addr))
|
---|
222 | d_printf("%s\n", addr);
|
---|
223 | }
|
---|
224 | SAFE_FREE(pdc_str);
|
---|
225 | return 0;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
|
---|
229 | {
|
---|
230 | struct sockaddr_storage ss;
|
---|
231 | char *pdc_str = NULL;
|
---|
232 | const char *domain;
|
---|
233 | char addr[INET6_ADDRSTRLEN];
|
---|
234 |
|
---|
235 | if (lp_security() == SEC_ADS) {
|
---|
236 | domain = lp_realm();
|
---|
237 | } else {
|
---|
238 | domain = c->opt_target_workgroup;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (argc > 0)
|
---|
242 | domain=argv[0];
|
---|
243 |
|
---|
244 | /* first get PDC */
|
---|
245 | if (!get_pdc_ip(domain, &ss))
|
---|
246 | return -1;
|
---|
247 |
|
---|
248 | print_sockaddr(addr, sizeof(addr), &ss);
|
---|
249 | if (asprintf(&pdc_str, "%s", addr) == -1) {
|
---|
250 | return -1;
|
---|
251 | }
|
---|
252 | d_printf("%s\n", pdc_str);
|
---|
253 | SAFE_FREE(pdc_str);
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | static int net_lookup_master(struct net_context *c, int argc, const char **argv)
|
---|
259 | {
|
---|
260 | struct sockaddr_storage master_ss;
|
---|
261 | const char *domain = c->opt_target_workgroup;
|
---|
262 | char addr[INET6_ADDRSTRLEN];
|
---|
263 |
|
---|
264 | if (argc > 0)
|
---|
265 | domain=argv[0];
|
---|
266 |
|
---|
267 | if (!find_master_ip(domain, &master_ss))
|
---|
268 | return -1;
|
---|
269 | print_sockaddr(addr, sizeof(addr), &master_ss);
|
---|
270 | d_printf("%s\n", addr);
|
---|
271 | return 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
|
---|
275 | {
|
---|
276 | #ifdef HAVE_KRB5
|
---|
277 | krb5_error_code rc;
|
---|
278 | krb5_context ctx;
|
---|
279 | struct sockaddr_in *addrs;
|
---|
280 | int num_kdcs,i;
|
---|
281 | krb5_data realm;
|
---|
282 | char **realms;
|
---|
283 |
|
---|
284 | initialize_krb5_error_table();
|
---|
285 | rc = krb5_init_context(&ctx);
|
---|
286 | if (rc) {
|
---|
287 | DEBUG(1,("krb5_init_context failed (%s)\n",
|
---|
288 | error_message(rc)));
|
---|
289 | return -1;
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (argc>0) {
|
---|
293 | realm.data = CONST_DISCARD(char *, argv[0]);
|
---|
294 | realm.length = strlen(argv[0]);
|
---|
295 | } else if (lp_realm() && *lp_realm()) {
|
---|
296 | realm.data = lp_realm();
|
---|
297 | realm.length = strlen((const char *)realm.data);
|
---|
298 | } else {
|
---|
299 | rc = krb5_get_host_realm(ctx, NULL, &realms);
|
---|
300 | if (rc) {
|
---|
301 | DEBUG(1,("krb5_gethost_realm failed (%s)\n",
|
---|
302 | error_message(rc)));
|
---|
303 | return -1;
|
---|
304 | }
|
---|
305 | realm.data = (char *) *realms;
|
---|
306 | realm.length = strlen((const char *)realm.data);
|
---|
307 | }
|
---|
308 |
|
---|
309 | rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
|
---|
310 | if (rc) {
|
---|
311 | DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
|
---|
312 | return -1;
|
---|
313 | }
|
---|
314 | for (i=0;i<num_kdcs;i++)
|
---|
315 | if (addrs[i].sin_family == AF_INET)
|
---|
316 | d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
|
---|
317 | ntohs(addrs[i].sin_port));
|
---|
318 | return 0;
|
---|
319 |
|
---|
320 | #endif
|
---|
321 | DEBUG(1, ("No kerberos support\n"));
|
---|
322 | return -1;
|
---|
323 | }
|
---|
324 |
|
---|
325 | static int net_lookup_name(struct net_context *c, int argc, const char **argv)
|
---|
326 | {
|
---|
327 | const char *dom, *name;
|
---|
328 | struct dom_sid sid;
|
---|
329 | enum lsa_SidType type;
|
---|
330 |
|
---|
331 | if (argc != 1) {
|
---|
332 | d_printf("%s\n%s",
|
---|
333 | _("Usage:"),
|
---|
334 | _(" net lookup name <name>\n"));
|
---|
335 | return -1;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
|
---|
339 | &dom, &name, &sid, &type)) {
|
---|
340 | d_printf(_("Could not lookup name %s\n"), argv[0]);
|
---|
341 | return -1;
|
---|
342 | }
|
---|
343 |
|
---|
344 | d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
|
---|
345 | type, sid_type_lookup(type), dom, name);
|
---|
346 | return 0;
|
---|
347 | }
|
---|
348 |
|
---|
349 | static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
|
---|
350 | {
|
---|
351 | const char *dom, *name;
|
---|
352 | struct dom_sid sid;
|
---|
353 | enum lsa_SidType type;
|
---|
354 |
|
---|
355 | if (argc != 1) {
|
---|
356 | d_printf("%s\n%s",
|
---|
357 | _("Usage:"),
|
---|
358 | _(" net lookup sid <sid>\n"));
|
---|
359 | return -1;
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (!string_to_sid(&sid, argv[0])) {
|
---|
363 | d_printf(_("Could not convert %s to SID\n"), argv[0]);
|
---|
364 | return -1;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (!lookup_sid(talloc_tos(), &sid,
|
---|
368 | &dom, &name, &type)) {
|
---|
369 | d_printf(_("Could not lookup name %s\n"), argv[0]);
|
---|
370 | return -1;
|
---|
371 | }
|
---|
372 |
|
---|
373 | d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid),
|
---|
374 | type, sid_type_lookup(type), dom, name);
|
---|
375 | return 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
|
---|
379 | {
|
---|
380 | NTSTATUS status;
|
---|
381 | const char *domain_name = NULL;
|
---|
382 | const char *site_name = NULL;
|
---|
383 | uint32_t flags = 0;
|
---|
384 | struct netr_DsRGetDCNameInfo *info = NULL;
|
---|
385 | TALLOC_CTX *mem_ctx;
|
---|
386 | char *s = NULL;
|
---|
387 |
|
---|
388 | if (argc < 1 || argc > 3) {
|
---|
389 | d_printf("%s\n%s",
|
---|
390 | _("Usage:"),
|
---|
391 | _(" net lookup dsgetdcname "
|
---|
392 | "<name> <flags> <sitename>\n"));
|
---|
393 | return -1;
|
---|
394 | }
|
---|
395 |
|
---|
396 | mem_ctx = talloc_init("net_lookup_dsgetdcname");
|
---|
397 | if (!mem_ctx) {
|
---|
398 | return -1;
|
---|
399 | }
|
---|
400 |
|
---|
401 | domain_name = argv[0];
|
---|
402 |
|
---|
403 | if (argc >= 2)
|
---|
404 | sscanf(argv[1], "%x", &flags);
|
---|
405 |
|
---|
406 | if (!flags) {
|
---|
407 | flags |= DS_DIRECTORY_SERVICE_REQUIRED;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (argc == 3) {
|
---|
411 | site_name = argv[2];
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (!c->msg_ctx) {
|
---|
415 | d_fprintf(stderr, _("Could not initialise message context. "
|
---|
416 | "Try running as root\n"));
|
---|
417 | return -1;
|
---|
418 | }
|
---|
419 |
|
---|
420 | status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
|
---|
421 | flags, &info);
|
---|
422 | if (!NT_STATUS_IS_OK(status)) {
|
---|
423 | d_printf(_("failed with: %s\n"), nt_errstr(status));
|
---|
424 | TALLOC_FREE(mem_ctx);
|
---|
425 | return -1;
|
---|
426 | }
|
---|
427 |
|
---|
428 | s = NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info);
|
---|
429 | printf("%s\n", s);
|
---|
430 | TALLOC_FREE(s);
|
---|
431 |
|
---|
432 | TALLOC_FREE(mem_ctx);
|
---|
433 | return 0;
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | /* lookup hosts or IP addresses using internal samba lookup fns */
|
---|
438 | int net_lookup(struct net_context *c, int argc, const char **argv)
|
---|
439 | {
|
---|
440 | int i;
|
---|
441 |
|
---|
442 | struct functable table[] = {
|
---|
443 | {"HOST", net_lookup_host},
|
---|
444 | {"LDAP", net_lookup_ldap},
|
---|
445 | {"DC", net_lookup_dc},
|
---|
446 | {"PDC", net_lookup_pdc},
|
---|
447 | {"MASTER", net_lookup_master},
|
---|
448 | {"KDC", net_lookup_kdc},
|
---|
449 | {"NAME", net_lookup_name},
|
---|
450 | {"SID", net_lookup_sid},
|
---|
451 | {"DSGETDCNAME", net_lookup_dsgetdcname},
|
---|
452 | {NULL, NULL}
|
---|
453 | };
|
---|
454 |
|
---|
455 | if (argc < 1) {
|
---|
456 | d_printf(_("\nUsage: \n"));
|
---|
457 | return net_lookup_usage(c, argc, argv);
|
---|
458 | }
|
---|
459 | for (i=0; table[i].funcname; i++) {
|
---|
460 | if (StrCaseCmp(argv[0], table[i].funcname) == 0)
|
---|
461 | return table[i].fn(c, argc-1, argv+1);
|
---|
462 | }
|
---|
463 |
|
---|
464 | /* Default to lookup a hostname so 'net lookup foo#1b' can be
|
---|
465 | used instead of 'net lookup host foo#1b'. The host syntax
|
---|
466 | is a bit confusing as non #00 names can't really be
|
---|
467 | considered hosts as such. */
|
---|
468 |
|
---|
469 | return net_lookup_host(c, argc, argv);
|
---|
470 | }
|
---|