2 Samba Unix/Linux SMB client library
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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "utils/net.h"
22 int net_lookup_usage(int argc
, const char **argv
)
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"
38 /* lookup a hostname giving an IP */
39 static int net_lookup_host(int argc
, const char **argv
)
43 const char *name
= argv
[0];
47 return net_lookup_usage(argc
, argv
);
49 p
= strchr_m(name
,'#');
52 sscanf(++p
,"%x",&name_type
);
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
));
62 d_printf("%s\n", inet_ntoa(ip
));
67 static void print_ldap_srvlist(struct dns_rr_srv
*dclist
, int numdcs
)
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
);
80 static int net_lookup_ldap(int argc
, const char **argv
)
85 struct hostent
*hostent
;
86 struct dns_rr_srv
*dcs
= NULL
;
95 domain
= opt_target_workgroup
;
97 sitename
= sitename_fetch(domain
);
99 if ( (ctx
= talloc_init("net_lookup_ldap")) == NULL
) {
100 d_fprintf(stderr
, "net_lookup_ldap: talloc_init() failed!\n");
105 DEBUG(9, ("Lookup up ldap for domain %s\n", domain
));
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
);
115 DEBUG(9, ("Looking up PDC for domain %s\n", domain
));
116 if (!get_pdc_ip(domain
, &addr
)) {
122 hostent
= gethostbyaddr((char *) &addr
.s_addr
, sizeof(addr
.s_addr
),
130 DEBUG(9, ("Found PDC with DNS name %s\n", hostent
->h_name
));
131 domain
= strchr(hostent
->h_name
, '.');
139 DEBUG(9, ("Looking up ldap for domain %s\n", domain
));
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
);
154 DEBUG(1,("No ADS support\n"));
158 static int net_lookup_dc(int argc
, const char **argv
)
160 struct ip_service
*ip_list
;
162 char *pdc_str
= NULL
;
163 const char *domain
= NULL
;
164 char *sitename
= NULL
;
166 BOOL sec_ads
= (lp_security() == SEC_ADS
);
171 domain
= opt_target_workgroup
;
178 if (!get_pdc_ip(domain
, &addr
))
181 asprintf(&pdc_str
, "%s", inet_ntoa(addr
));
182 d_printf("%s\n", pdc_str
);
184 sitename
= sitename_fetch(domain
);
185 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain
, sitename
, &ip_list
, &count
, sec_ads
))) {
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
);
200 static int net_lookup_pdc(int argc
, const char **argv
)
203 char *pdc_str
= NULL
;
206 if (lp_security() == SEC_ADS
) {
209 domain
= opt_target_workgroup
;
216 if (!get_pdc_ip(domain
, &addr
))
219 asprintf(&pdc_str
, "%s", inet_ntoa(addr
));
220 d_printf("%s\n", pdc_str
);
226 static int net_lookup_master(int argc
, const char **argv
)
228 struct in_addr master_ip
;
229 const char *domain
=opt_target_workgroup
;
234 if (!find_master_ip(domain
, &master_ip
))
236 d_printf("%s\n", inet_ntoa(master_ip
));
240 static int net_lookup_kdc(int argc
, const char **argv
)
245 struct sockaddr_in
*addrs
;
250 initialize_krb5_error_table();
251 rc
= krb5_init_context(&ctx
);
253 DEBUG(1,("krb5_init_context failed (%s)\n",
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
);
265 rc
= krb5_get_host_realm(ctx
, NULL
, &realms
);
267 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
271 realm
.data
= (char *) *realms
;
272 realm
.length
= strlen((const char *)realm
.data
);
275 rc
= smb_krb5_locate_kdc(ctx
, &realm
, (struct sockaddr
**)(void *)&addrs
, &num_kdcs
, 0);
277 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc
)));
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
));
287 DEBUG(1, ("No kerberos support\n"));
291 static int net_lookup_name(int argc
, const char **argv
)
293 const char *dom
, *name
;
295 enum lsa_SidType type
;
298 d_printf("usage: net lookup name <name>\n");
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]);
308 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid
),
309 type
, sid_type_lookup(type
), dom
, name
);
313 static int net_lookup_sid(int argc
, const char **argv
)
315 const char *dom
, *name
;
317 enum lsa_SidType type
;
320 d_printf("usage: net lookup sid <sid>\n");
324 if (!string_to_sid(&sid
, argv
[0])) {
325 d_printf("Could not convert %s to SID\n", argv
[0]);
329 if (!lookup_sid(talloc_tos(), &sid
,
330 &dom
, &name
, &type
)) {
331 d_printf("Could not lookup name %s\n", argv
[0]);
335 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid
),
336 type
, sid_type_lookup(type
), dom
, name
);
340 static int net_lookup_dsgetdcname(int argc
, const char **argv
)
343 const char *domain_name
= NULL
;
344 char *site_name
= NULL
;
346 struct DS_DOMAIN_CONTROLLER_INFO
*info
= NULL
;
349 if (argc
< 1 || argc
> 3) {
350 d_printf("usage: net lookup dsgetdcname "
351 "<name> <flags> <sitename>\n");
355 mem_ctx
= talloc_init("net_lookup_dsgetdcname");
360 domain_name
= argv
[0];
363 sscanf(argv
[1], "%x", &flags
);
366 flags
|= DS_DIRECTORY_SERVICE_REQUIRED
;
370 site_name
= SMB_STRDUP(argv
[2]);
372 TALLOC_FREE(mem_ctx
);
378 site_name
= sitename_fetch(domain_name
);
381 status
= DsGetDcName(mem_ctx
, NULL
, domain_name
, NULL
, site_name
,
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
);
390 display_ds_domain_controller_info(mem_ctx
, info
);
392 SAFE_FREE(site_name
);
393 TALLOC_FREE(mem_ctx
);
398 /* lookup hosts or IP addresses using internal samba lookup fns */
399 int net_lookup(int argc
, const char **argv
)
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
},
417 d_printf("\nUsage: \n");
418 return net_lookup_usage(argc
, argv
);
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);
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. */
430 return net_lookup_host(argc
, argv
);