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 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. */
21 #include "utils/net.h"
23 int net_lookup_usage(int argc, const char **argv)
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"
35 /* lookup a hostname giving an IP */
36 static int net_lookup_host(int argc, const char **argv)
40 const char *name = argv[0];
44 return net_lookup_usage(argc, argv);
46 p = strchr_m(name,'#');
49 sscanf(++p,"%x",&name_type);
52 if (!resolve_name(name, &ip, name_type)) {
53 /* we deliberately use DEBUG() here to send it to stderr
54 so scripts aren't mucked up */
55 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
59 d_printf("%s\n", inet_ntoa(ip));
63 static void print_ldap_srvlist(char *srvlist)
71 next = strchr(cur,':');
72 if (next) *next++='\0';
73 printit = resolve_name(cur, &ip, 0x20);
75 next=cur ? strchr(cur,' ') :NULL;
79 d_printf("%s:%s\n", inet_ntoa(ip), cur?cur:"");
85 static int net_lookup_ldap(int argc, const char **argv)
92 struct hostent *hostent;
97 domain = opt_target_workgroup;
99 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
100 rc = ldap_domain2hostlist(domain, &srvlist);
101 if ((rc == LDAP_SUCCESS) && srvlist) {
102 print_ldap_srvlist(srvlist);
106 DEBUG(9, ("Looking up DC for domain %s\n", domain));
107 if (!get_pdc_ip(domain, &addr))
110 hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
115 DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
116 domain = strchr(hostent->h_name, '.');
121 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
122 rc = ldap_domain2hostlist(domain, &srvlist);
123 if ((rc == LDAP_SUCCESS) && srvlist) {
124 print_ldap_srvlist(srvlist);
129 DEBUG(1,("No LDAP support\n"));
133 static int net_lookup_dc(int argc, const char **argv)
135 struct ip_service *ip_list;
137 char *pdc_str = NULL;
138 const char *domain=opt_target_workgroup;
145 if (!get_pdc_ip(domain, &addr))
148 asprintf(&pdc_str, "%s", inet_ntoa(addr));
149 d_printf("%s\n", pdc_str);
151 if (!get_sorted_dc_list(domain, &ip_list, &count, False)) {
155 for (i=0;i<count;i++) {
156 char *dc_str = inet_ntoa(ip_list[i].ip);
157 if (!strequal(pdc_str, dc_str))
158 d_printf("%s\n", dc_str);
164 static int net_lookup_master(int argc, const char **argv)
166 struct in_addr master_ip;
167 const char *domain=opt_target_workgroup;
172 if (!find_master_ip(domain, &master_ip))
174 d_printf("%s\n", inet_ntoa(master_ip));
178 static int net_lookup_kdc(int argc, const char **argv)
183 struct sockaddr_in *addrs;
188 rc = krb5_init_context(&ctx);
190 DEBUG(1,("krb5_init_context failed (%s)\n",
196 realm.data = CONST_DISCARD(krb5_pointer, argv[0]);
197 realm.length = strlen(argv[0]);
198 } else if (lp_realm() && *lp_realm()) {
199 realm.data = (krb5_pointer) lp_realm();
200 realm.length = strlen(realm.data);
202 rc = krb5_get_host_realm(ctx, NULL, &realms);
204 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
208 realm.data = (krb5_pointer) *realms;
209 realm.length = strlen(realm.data);
212 rc = krb5_locate_kdc(ctx, &realm, (struct sockaddr **) &addrs, &num_kdcs, 0);
214 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
217 for (i=0;i<num_kdcs;i++)
218 if (addrs[i].sin_family == AF_INET)
219 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
220 ntohs(addrs[i].sin_port));
224 DEBUG(1, ("No kerberos support\n"));
229 /* lookup hosts or IP addresses using internal samba lookup fns */
230 int net_lookup(int argc, const char **argv)
234 struct functable table[] = {
235 {"HOST", net_lookup_host},
236 {"LDAP", net_lookup_ldap},
237 {"DC", net_lookup_dc},
238 {"MASTER", net_lookup_master},
239 {"KDC", net_lookup_kdc},
244 d_printf("\nUsage: \n");
245 return net_lookup_usage(argc, argv);
247 for (i=0; table[i].funcname; i++) {
248 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
249 return table[i].fn(argc-1, argv+1);
252 /* Default to lookup a hostname so 'net lookup foo#1b' can be
253 used instead of 'net lookup host foo#1b'. The host syntax
254 is a bit confusing as non #00 names can't really be
255 considered hosts as such. */
257 return net_lookup_host(argc, argv);