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(struct net_context
*c
, 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(struct net_context
*c
, int argc
, const char **argv
)
41 struct sockaddr_storage ss
;
43 char addr
[INET6_ADDRSTRLEN
];
44 const char *name
= argv
[0];
48 return net_lookup_usage(c
, argc
, argv
);
50 p
= strchr_m(name
,'#');
53 sscanf(++p
,"%x",&name_type
);
56 if (!resolve_name(name
, &ss
, name_type
, false)) {
57 /* we deliberately use DEBUG() here to send it to stderr
58 so scripts aren't mucked up */
59 DEBUG(0,("Didn't find %s#%02x\n", name
, name_type
));
63 print_sockaddr(addr
, sizeof(addr
), &ss
);
64 d_printf("%s\n", addr
);
69 static void print_ldap_srvlist(struct dns_rr_srv
*dclist
, int numdcs
)
71 struct sockaddr_storage ss
;
74 for ( i
=0; i
<numdcs
; i
++ ) {
75 if (resolve_name(dclist
[i
].hostname
, &ss
, 0x20, true) ) {
76 char addr
[INET6_ADDRSTRLEN
];
77 print_sockaddr(addr
, sizeof(addr
), &ss
);
79 if (ss
.ss_family
== AF_INET6
) {
80 d_printf("[%s]:%d\n", addr
, dclist
[i
].port
);
83 if (ss
.ss_family
== AF_INET
) {
84 d_printf("%s:%d\n", addr
, dclist
[i
].port
);
91 static int net_lookup_ldap(struct net_context
*c
, int argc
, const char **argv
)
95 struct sockaddr_storage ss
;
96 struct dns_rr_srv
*dcs
= NULL
;
102 char h_name
[MAX_DNS_NAME_LENGTH
];
107 domain
= c
->opt_target_workgroup
;
109 sitename
= sitename_fetch(domain
);
111 if ( (ctx
= talloc_init("net_lookup_ldap")) == NULL
) {
112 d_fprintf(stderr
,_("net_lookup_ldap: talloc_init() failed!\n"));
117 DEBUG(9, ("Lookup up ldap for domain %s\n", domain
));
119 status
= ads_dns_query_dcs( ctx
, domain
, sitename
, &dcs
, &numdcs
);
120 if ( NT_STATUS_IS_OK(status
) && numdcs
) {
121 print_ldap_srvlist(dcs
, numdcs
);
127 DEBUG(9, ("Looking up PDC for domain %s\n", domain
));
128 if (!get_pdc_ip(domain
, &ss
)) {
134 ret
= sys_getnameinfo((struct sockaddr
*)&ss
,
135 sizeof(struct sockaddr_storage
),
136 h_name
, sizeof(h_name
),
146 DEBUG(9, ("Found PDC with DNS name %s\n", h_name
));
147 domain
= strchr(h_name
, '.');
155 DEBUG(9, ("Looking up ldap for domain %s\n", domain
));
157 status
= ads_dns_query_dcs( ctx
, domain
, sitename
, &dcs
, &numdcs
);
158 if ( NT_STATUS_IS_OK(status
) && numdcs
) {
159 print_ldap_srvlist(dcs
, numdcs
);
170 DEBUG(1,("No ADS support\n"));
174 static int net_lookup_dc(struct net_context
*c
, int argc
, const char **argv
)
176 struct ip_service
*ip_list
;
177 struct sockaddr_storage ss
;
178 char *pdc_str
= NULL
;
179 const char *domain
= NULL
;
180 char *sitename
= NULL
;
182 char addr
[INET6_ADDRSTRLEN
];
183 bool sec_ads
= (lp_security() == SEC_ADS
);
188 domain
= c
->opt_target_workgroup
;
195 if (!get_pdc_ip(domain
, &ss
))
198 print_sockaddr(addr
, sizeof(addr
), &ss
);
199 if (asprintf(&pdc_str
, "%s", addr
) == -1) {
202 d_printf("%s\n", pdc_str
);
204 sitename
= sitename_fetch(domain
);
205 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain
, sitename
,
206 &ip_list
, &count
, sec_ads
))) {
212 for (i
=0;i
<count
;i
++) {
213 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
214 if (!strequal(pdc_str
, addr
))
215 d_printf("%s\n", addr
);
221 static int net_lookup_pdc(struct net_context
*c
, int argc
, const char **argv
)
223 struct sockaddr_storage ss
;
224 char *pdc_str
= NULL
;
226 char addr
[INET6_ADDRSTRLEN
];
228 if (lp_security() == SEC_ADS
) {
231 domain
= c
->opt_target_workgroup
;
238 if (!get_pdc_ip(domain
, &ss
))
241 print_sockaddr(addr
, sizeof(addr
), &ss
);
242 if (asprintf(&pdc_str
, "%s", addr
) == -1) {
245 d_printf("%s\n", pdc_str
);
251 static int net_lookup_master(struct net_context
*c
, int argc
, const char **argv
)
253 struct sockaddr_storage master_ss
;
254 const char *domain
= c
->opt_target_workgroup
;
255 char addr
[INET6_ADDRSTRLEN
];
260 if (!find_master_ip(domain
, &master_ss
))
262 print_sockaddr(addr
, sizeof(addr
), &master_ss
);
263 d_printf("%s\n", addr
);
267 static int net_lookup_kdc(struct net_context
*c
, int argc
, const char **argv
)
272 struct sockaddr_in
*addrs
;
277 initialize_krb5_error_table();
278 rc
= krb5_init_context(&ctx
);
280 DEBUG(1,("krb5_init_context failed (%s)\n",
286 realm
.data
= CONST_DISCARD(char *, argv
[0]);
287 realm
.length
= strlen(argv
[0]);
288 } else if (lp_realm() && *lp_realm()) {
289 realm
.data
= lp_realm();
290 realm
.length
= strlen((const char *)realm
.data
);
292 rc
= krb5_get_host_realm(ctx
, NULL
, &realms
);
294 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
298 realm
.data
= (char *) *realms
;
299 realm
.length
= strlen((const char *)realm
.data
);
302 rc
= smb_krb5_locate_kdc(ctx
, &realm
, (struct sockaddr
**)(void *)&addrs
, &num_kdcs
, 0);
304 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc
)));
307 for (i
=0;i
<num_kdcs
;i
++)
308 if (addrs
[i
].sin_family
== AF_INET
)
309 d_printf("%s:%hd\n", inet_ntoa(addrs
[i
].sin_addr
),
310 ntohs(addrs
[i
].sin_port
));
314 DEBUG(1, ("No kerberos support\n"));
318 static int net_lookup_name(struct net_context
*c
, int argc
, const char **argv
)
320 const char *dom
, *name
;
322 enum lsa_SidType type
;
325 d_printf(_("usage: net lookup name <name>\n"));
329 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_ALL
,
330 &dom
, &name
, &sid
, &type
)) {
331 d_printf(_("Could not lookup name %s\n"), argv
[0]);
335 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid
),
336 type
, sid_type_lookup(type
), dom
, name
);
340 static int net_lookup_sid(struct net_context
*c
, int argc
, const char **argv
)
342 const char *dom
, *name
;
344 enum lsa_SidType type
;
347 d_printf(_("usage: net lookup sid <sid>\n"));
351 if (!string_to_sid(&sid
, argv
[0])) {
352 d_printf(_("Could not convert %s to SID\n"), argv
[0]);
356 if (!lookup_sid(talloc_tos(), &sid
,
357 &dom
, &name
, &type
)) {
358 d_printf(_("Could not lookup name %s\n"), argv
[0]);
362 d_printf("%s %d (%s) %s\\%s\n", sid_string_tos(&sid
),
363 type
, sid_type_lookup(type
), dom
, name
);
367 static int net_lookup_dsgetdcname(struct net_context
*c
, int argc
, const char **argv
)
370 const char *domain_name
= NULL
;
371 const char *site_name
= NULL
;
373 struct netr_DsRGetDCNameInfo
*info
= NULL
;
377 if (argc
< 1 || argc
> 3) {
378 d_printf(_("usage: net lookup dsgetdcname "
379 "<name> <flags> <sitename>\n"));
383 mem_ctx
= talloc_init("net_lookup_dsgetdcname");
388 domain_name
= argv
[0];
391 sscanf(argv
[1], "%x", &flags
);
394 flags
|= DS_DIRECTORY_SERVICE_REQUIRED
;
401 status
= dsgetdcname(mem_ctx
, NULL
, domain_name
, NULL
, site_name
,
403 if (!NT_STATUS_IS_OK(status
)) {
404 d_printf(_("failed with: %s\n"), nt_errstr(status
));
405 TALLOC_FREE(mem_ctx
);
409 s
= NDR_PRINT_STRUCT_STRING(mem_ctx
, netr_DsRGetDCNameInfo
, info
);
413 TALLOC_FREE(mem_ctx
);
418 /* lookup hosts or IP addresses using internal samba lookup fns */
419 int net_lookup(struct net_context
*c
, int argc
, const char **argv
)
423 struct functable table
[] = {
424 {"HOST", net_lookup_host
},
425 {"LDAP", net_lookup_ldap
},
426 {"DC", net_lookup_dc
},
427 {"PDC", net_lookup_pdc
},
428 {"MASTER", net_lookup_master
},
429 {"KDC", net_lookup_kdc
},
430 {"NAME", net_lookup_name
},
431 {"SID", net_lookup_sid
},
432 {"DSGETDCNAME", net_lookup_dsgetdcname
},
437 d_printf(_("\nUsage: \n"));
438 return net_lookup_usage(c
, argc
, argv
);
440 for (i
=0; table
[i
].funcname
; i
++) {
441 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
442 return table
[i
].fn(c
, argc
-1, argv
+1);
445 /* Default to lookup a hostname so 'net lookup foo#1b' can be
446 used instead of 'net lookup host foo#1b'. The host syntax
447 is a bit confusing as non #00 names can't really be
448 considered hosts as such. */
450 return net_lookup_host(c
, argc
, argv
);