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