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