r20857: Silence gives assent :-). Checking in the fix for
[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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "includes.h"
21 #include "utils/net.h"
22
23 int net_lookup_usage(int argc, const char **argv)
24 {
25         d_printf(
26 "  net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
27 "  net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
28 "  net lookup kdc [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 );
34         return -1;
35 }
36
37 /* lookup a hostname giving an IP */
38 static int net_lookup_host(int argc, const char **argv)
39 {
40         struct in_addr ip;
41         int name_type = 0x20;
42         const char *name = argv[0];
43         char *p;
44
45         if (argc == 0) 
46                 return net_lookup_usage(argc, argv);
47
48         p = strchr_m(name,'#');
49         if (p) {
50                 *p = '\0';
51                 sscanf(++p,"%x",&name_type);
52         }
53         
54         if (!resolve_name(name, &ip, name_type)) {
55                 /* we deliberately use DEBUG() here to send it to stderr 
56                    so scripts aren't mucked up */
57                 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
58                 return -1;
59         }
60
61         d_printf("%s\n", inet_ntoa(ip));
62         return 0;
63 }
64
65 #ifdef HAVE_ADS
66 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
67 {
68         struct in_addr ip;
69         int i;
70
71         for ( i=0; i<numdcs; i++ ) {
72                 if ( resolve_name(dclist[i].hostname, &ip, 0x20) ) {
73                         d_printf("%s:%d\n", inet_ntoa(ip), dclist[i].port); 
74                 }
75         }
76 }
77 #endif
78
79 static int net_lookup_ldap(int argc, const char **argv)
80 {
81 #ifdef HAVE_ADS
82         const char *domain;
83         struct in_addr addr;
84         struct hostent *hostent;
85         struct dns_rr_srv *dcs = NULL;
86         int numdcs = 0;
87         char *sitename = sitename_fetch();
88         TALLOC_CTX *ctx;
89         NTSTATUS status;
90
91         if (argc > 0)
92                 domain = argv[0];
93         else
94                 domain = opt_target_workgroup;
95
96         if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
97                 d_fprintf(stderr, "net_lookup_ldap: talloc_inti() failed!\n");
98                 SAFE_FREE(sitename);
99                 return -1;
100         }
101
102         DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
103
104         status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
105         if ( NT_STATUS_IS_OK(status) && numdcs ) {
106                 print_ldap_srvlist(dcs, numdcs);
107                 TALLOC_FREE( ctx );
108                 SAFE_FREE(sitename);
109                 return 0;
110         }
111
112         DEBUG(9, ("Looking up DC for domain %s\n", domain));
113         if (!get_pdc_ip(domain, &addr)) {
114                 TALLOC_FREE( ctx );
115                 SAFE_FREE(sitename);
116                 return -1;
117         }
118
119         hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
120                                 AF_INET);
121         if (!hostent) {
122                 TALLOC_FREE( ctx );
123                 SAFE_FREE(sitename);
124                 return -1;
125         }
126
127         DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
128         domain = strchr(hostent->h_name, '.');
129         if (!domain) {
130                 TALLOC_FREE( ctx );
131                 SAFE_FREE(sitename);
132                 return -1;
133         }
134         domain++;
135
136         DEBUG(9, ("Looking up ldap for domain %s\n", domain));
137
138         status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
139         if ( NT_STATUS_IS_OK(status) && numdcs ) {
140                 print_ldap_srvlist(dcs, numdcs);
141                 TALLOC_FREE( ctx );
142                 SAFE_FREE(sitename);
143                 return 0;
144         }
145
146         TALLOC_FREE( ctx );
147         SAFE_FREE(sitename);
148
149         return -1;
150 #endif
151         DEBUG(1,("No ADS support\n"));
152         return -1;
153 }
154
155 static int net_lookup_dc(int argc, const char **argv)
156 {
157         struct ip_service *ip_list;
158         struct in_addr addr;
159         char *pdc_str = NULL;
160         const char *domain=opt_target_workgroup;
161         char *sitename = NULL;
162         int count, i;
163
164         if (argc > 0)
165                 domain=argv[0];
166
167         /* first get PDC */
168         if (!get_pdc_ip(domain, &addr))
169                 return -1;
170
171         asprintf(&pdc_str, "%s", inet_ntoa(addr));
172         d_printf("%s\n", pdc_str);
173
174         sitename = sitename_fetch();
175         if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename, &ip_list, &count, False))) {
176                 SAFE_FREE(pdc_str);
177                 SAFE_FREE(sitename);
178                 return 0;
179         }
180         SAFE_FREE(sitename);
181         for (i=0;i<count;i++) {
182                 char *dc_str = inet_ntoa(ip_list[i].ip);
183                 if (!strequal(pdc_str, dc_str))
184                         d_printf("%s\n", dc_str);
185         }
186         SAFE_FREE(pdc_str);
187         return 0;
188 }
189
190 static int net_lookup_master(int argc, const char **argv)
191 {
192         struct in_addr master_ip;
193         const char *domain=opt_target_workgroup;
194
195         if (argc > 0)
196                 domain=argv[0];
197
198         if (!find_master_ip(domain, &master_ip))
199                 return -1;
200         d_printf("%s\n", inet_ntoa(master_ip));
201         return 0;
202 }
203
204 static int net_lookup_kdc(int argc, const char **argv)
205 {
206 #ifdef HAVE_KRB5
207         krb5_error_code rc;
208         krb5_context ctx;
209         struct sockaddr_in *addrs;
210         int num_kdcs,i;
211         krb5_data realm;
212         char **realms;
213
214         initialize_krb5_error_table();
215         rc = krb5_init_context(&ctx);
216         if (rc) {
217                 DEBUG(1,("krb5_init_context failed (%s)\n", 
218                          error_message(rc)));
219                 return -1;
220         }
221
222         if (argc>0) {
223                 realm.data = CONST_DISCARD(char *, argv[0]);
224                 realm.length = strlen(argv[0]);
225         } else if (lp_realm() && *lp_realm()) {
226                 realm.data = lp_realm();
227                 realm.length = strlen((const char *)realm.data);
228         } else {
229                 rc = krb5_get_host_realm(ctx, NULL, &realms);
230                 if (rc) {
231                         DEBUG(1,("krb5_gethost_realm failed (%s)\n",
232                                  error_message(rc)));
233                         return -1;
234                 }
235                 realm.data = (char *) *realms;
236                 realm.length = strlen((const char *)realm.data);
237         }
238
239         rc = krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
240         if (rc) {
241                 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
242                 return -1;
243         }
244         for (i=0;i<num_kdcs;i++)
245                 if (addrs[i].sin_family == AF_INET) 
246                         d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
247                                  ntohs(addrs[i].sin_port));
248         return 0;
249
250 #endif  
251         DEBUG(1, ("No kerberos support\n"));
252         return -1;
253 }
254
255 static int net_lookup_name(int argc, const char **argv)
256 {
257         const char *dom, *name;
258         DOM_SID sid;
259         enum lsa_SidType type;
260
261         if (argc != 1) {
262                 d_printf("usage: net lookup name <name>\n");
263                 return -1;
264         }
265
266         if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ALL,
267                          &dom, &name, &sid, &type)) {
268                 d_printf("Could not lookup name %s\n", argv[0]);
269                 return -1;
270         }
271
272         d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
273                  type, sid_type_lookup(type), dom, name);
274         return 0;
275 }
276
277 static int net_lookup_sid(int argc, const char **argv)
278 {
279         const char *dom, *name;
280         DOM_SID sid;
281         enum lsa_SidType type;
282
283         if (argc != 1) {
284                 d_printf("usage: net lookup sid <sid>\n");
285                 return -1;
286         }
287
288         if (!string_to_sid(&sid, argv[0])) {
289                 d_printf("Could not convert %s to SID\n", argv[0]);
290                 return -1;
291         }
292
293         if (!lookup_sid(tmp_talloc_ctx(), &sid,
294                         &dom, &name, &type)) {
295                 d_printf("Could not lookup name %s\n", argv[0]);
296                 return -1;
297         }
298
299         d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
300                  type, sid_type_lookup(type), dom, name);
301         return 0;
302 }
303
304 /* lookup hosts or IP addresses using internal samba lookup fns */
305 int net_lookup(int argc, const char **argv)
306 {
307         int i;
308
309         struct functable table[] = {
310                 {"HOST", net_lookup_host},
311                 {"LDAP", net_lookup_ldap},
312                 {"DC", net_lookup_dc},
313                 {"MASTER", net_lookup_master},
314                 {"KDC", net_lookup_kdc},
315                 {"NAME", net_lookup_name},
316                 {"SID", net_lookup_sid},
317                 {NULL, NULL}
318         };
319
320         if (argc < 1) {
321                 d_printf("\nUsage: \n");
322                 return net_lookup_usage(argc, argv);
323         }
324         for (i=0; table[i].funcname; i++) {
325                 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
326                         return table[i].fn(argc-1, argv+1);
327         }
328
329         /* Default to lookup a hostname so 'net lookup foo#1b' can be 
330            used instead of 'net lookup host foo#1b'.  The host syntax
331            is a bit confusing as non #00 names can't really be 
332            considered hosts as such. */
333
334         return net_lookup_host(argc, argv);
335 }