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