r17915: Saturn fixes
[Samba/gbeck.git] / source / utils / net_lookup.c
blob68097aa9f7fe5343e3cf86db486720c74de2417a
1 /*
2 Samba Unix/Linux SMB client library
3 net lookup command
4 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
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.
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.
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. */
20 #include "includes.h"
21 #include "utils/net.h"
23 int net_lookup_usage(int argc, const char **argv)
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"
34 return -1;
37 /* lookup a hostname giving an IP */
38 static int net_lookup_host(int argc, const char **argv)
40 struct in_addr ip;
41 int name_type = 0x20;
42 const char *name = argv[0];
43 char *p;
45 if (argc == 0)
46 return net_lookup_usage(argc, argv);
48 p = strchr_m(name,'#');
49 if (p) {
50 *p = '\0';
51 sscanf(++p,"%x",&name_type);
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;
61 d_printf("%s\n", inet_ntoa(ip));
62 return 0;
65 #ifdef HAVE_ADS
66 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
68 struct in_addr ip;
69 int i;
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);
77 #endif
79 static int net_lookup_ldap(int argc, const char **argv)
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 TALLOC_CTX *ctx;
88 NTSTATUS status;
90 if (argc > 0)
91 domain = argv[0];
92 else
93 domain = opt_target_workgroup;
95 if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
96 d_fprintf(stderr, "net_lookup_ldap: talloc_inti() failed!\n");
97 return -1;
100 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
102 status = ads_dns_query_dcs( ctx, domain, &dcs, &numdcs );
103 if ( NT_STATUS_IS_OK(status) && numdcs ) {
104 print_ldap_srvlist(dcs, numdcs);
105 TALLOC_FREE( ctx );
107 return 0;
110 DEBUG(9, ("Looking up DC for domain %s\n", domain));
111 if (!get_pdc_ip(domain, &addr)) {
112 TALLOC_FREE( ctx );
113 return -1;
116 hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
117 AF_INET);
118 if (!hostent) {
119 TALLOC_FREE( ctx );
120 return -1;
123 DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
124 domain = strchr(hostent->h_name, '.');
125 if (!domain) {
126 TALLOC_FREE( ctx );
127 return -1;
129 domain++;
131 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
133 status = ads_dns_query_dcs( ctx, domain, &dcs, &numdcs );
134 if ( NT_STATUS_IS_OK(status) && numdcs ) {
135 print_ldap_srvlist(dcs, numdcs);
136 TALLOC_FREE( ctx );
138 return 0;
141 TALLOC_FREE( ctx );
144 return -1;
145 #endif
146 DEBUG(1,("No ADS support\n"));
147 return -1;
150 static int net_lookup_dc(int argc, const char **argv)
152 struct ip_service *ip_list;
153 struct in_addr addr;
154 char *pdc_str = NULL;
155 const char *domain=opt_target_workgroup;
156 int count, i;
158 if (argc > 0)
159 domain=argv[0];
161 /* first get PDC */
162 if (!get_pdc_ip(domain, &addr))
163 return -1;
165 asprintf(&pdc_str, "%s", inet_ntoa(addr));
166 d_printf("%s\n", pdc_str);
168 if (!get_sorted_dc_list(domain, &ip_list, &count, False)) {
169 SAFE_FREE(pdc_str);
170 return 0;
172 for (i=0;i<count;i++) {
173 char *dc_str = inet_ntoa(ip_list[i].ip);
174 if (!strequal(pdc_str, dc_str))
175 d_printf("%s\n", dc_str);
177 SAFE_FREE(pdc_str);
178 return 0;
181 static int net_lookup_master(int argc, const char **argv)
183 struct in_addr master_ip;
184 const char *domain=opt_target_workgroup;
186 if (argc > 0)
187 domain=argv[0];
189 if (!find_master_ip(domain, &master_ip))
190 return -1;
191 d_printf("%s\n", inet_ntoa(master_ip));
192 return 0;
195 static int net_lookup_kdc(int argc, const char **argv)
197 #ifdef HAVE_KRB5
198 krb5_error_code rc;
199 krb5_context ctx;
200 struct sockaddr_in *addrs;
201 int num_kdcs,i;
202 krb5_data realm;
203 char **realms;
205 initialize_krb5_error_table();
206 rc = krb5_init_context(&ctx);
207 if (rc) {
208 DEBUG(1,("krb5_init_context failed (%s)\n",
209 error_message(rc)));
210 return -1;
213 if (argc>0) {
214 realm.data = CONST_DISCARD(krb5_pointer, argv[0]);
215 realm.length = strlen(argv[0]);
216 } else if (lp_realm() && *lp_realm()) {
217 realm.data = (krb5_pointer) lp_realm();
218 realm.length = strlen(realm.data);
219 } else {
220 rc = krb5_get_host_realm(ctx, NULL, &realms);
221 if (rc) {
222 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
223 error_message(rc)));
224 return -1;
226 realm.data = (krb5_pointer) *realms;
227 realm.length = strlen(realm.data);
230 rc = krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
231 if (rc) {
232 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
233 return -1;
235 for (i=0;i<num_kdcs;i++)
236 if (addrs[i].sin_family == AF_INET)
237 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
238 ntohs(addrs[i].sin_port));
239 return 0;
241 #endif
242 DEBUG(1, ("No kerberos support\n"));
243 return -1;
246 static int net_lookup_name(int argc, const char **argv)
248 const char *dom, *name;
249 DOM_SID sid;
250 enum SID_NAME_USE type;
252 if (argc != 1) {
253 d_printf("usage: net lookup name <name>\n");
254 return -1;
257 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ALL,
258 &dom, &name, &sid, &type)) {
259 d_printf("Could not lookup name %s\n", argv[0]);
260 return -1;
263 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
264 type, sid_type_lookup(type), dom, name);
265 return 0;
268 static int net_lookup_sid(int argc, const char **argv)
270 const char *dom, *name;
271 DOM_SID sid;
272 enum SID_NAME_USE type;
274 if (argc != 1) {
275 d_printf("usage: net lookup sid <sid>\n");
276 return -1;
279 if (!string_to_sid(&sid, argv[0])) {
280 d_printf("Could not convert %s to SID\n", argv[0]);
281 return -1;
284 if (!lookup_sid(tmp_talloc_ctx(), &sid,
285 &dom, &name, &type)) {
286 d_printf("Could not lookup name %s\n", argv[0]);
287 return -1;
290 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
291 type, sid_type_lookup(type), dom, name);
292 return 0;
295 /* lookup hosts or IP addresses using internal samba lookup fns */
296 int net_lookup(int argc, const char **argv)
298 int i;
300 struct functable table[] = {
301 {"HOST", net_lookup_host},
302 {"LDAP", net_lookup_ldap},
303 {"DC", net_lookup_dc},
304 {"MASTER", net_lookup_master},
305 {"KDC", net_lookup_kdc},
306 {"NAME", net_lookup_name},
307 {"SID", net_lookup_sid},
308 {NULL, NULL}
311 if (argc < 1) {
312 d_printf("\nUsage: \n");
313 return net_lookup_usage(argc, argv);
315 for (i=0; table[i].funcname; i++) {
316 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
317 return table[i].fn(argc-1, argv+1);
320 /* Default to lookup a hostname so 'net lookup foo#1b' can be
321 used instead of 'net lookup host foo#1b'. The host syntax
322 is a bit confusing as non #00 names can't really be
323 considered hosts as such. */
325 return net_lookup_host(argc, argv);