[merge from APP_HEAD]
[Samba/gbeck.git] / source3 / utils / net_lookup.c
blob271094480c312c30c488e86c0a39cee817d147c6
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"
32 return -1;
35 /* lookup a hostname giving an IP */
36 static int net_lookup_host(int argc, const char **argv)
38 struct in_addr ip;
39 int name_type = 0x20;
41 if (argc == 0) return net_lookup_usage(argc, argv);
42 if (argc > 1) name_type = strtol(argv[1], NULL, 0);
44 if (!resolve_name(argv[0], &ip, name_type)) {
45 /* we deliberately use DEBUG() here to send it to stderr
46 so scripts aren't mucked up */
47 DEBUG(0,("Didn't find %s#%02x\n", argv[0], name_type));
48 return -1;
51 d_printf("%s\n", inet_ntoa(ip));
52 return 0;
55 static void print_ldap_srvlist(char *srvlist)
57 char *cur, *next;
58 struct in_addr ip;
59 BOOL printit;
61 cur = srvlist;
62 do {
63 next = strchr(cur,':');
64 if (next) *next++='\0';
65 printit = resolve_name(cur, &ip, 0x20);
66 cur=next;
67 next=cur ? strchr(cur,' ') :NULL;
68 if (next)
69 *next++='\0';
70 if (printit)
71 d_printf("%s:%s\n", inet_ntoa(ip), cur?cur:"");
72 cur = next;
73 } while (next);
77 static int net_lookup_ldap(int argc, const char **argv)
79 #ifdef HAVE_LDAP
80 char *srvlist;
81 const char *domain;
82 int rc;
83 struct in_addr addr;
84 struct hostent *hostent;
86 if (argc > 0)
87 domain = argv[0];
88 else
89 domain = opt_target_workgroup;
91 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
92 rc = ldap_domain2hostlist(domain, &srvlist);
93 if ((rc == LDAP_SUCCESS) && srvlist) {
94 print_ldap_srvlist(srvlist);
95 return 0;
98 DEBUG(9, ("Looking up DC for domain %s\n", domain));
99 if (!get_pdc_ip(domain, &addr))
100 return -1;
102 hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
103 AF_INET);
104 if (!hostent)
105 return -1;
107 DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
108 domain = strchr(hostent->h_name, '.');
109 if (!domain)
110 return -1;
111 domain++;
113 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
114 rc = ldap_domain2hostlist(domain, &srvlist);
115 if ((rc == LDAP_SUCCESS) && srvlist) {
116 print_ldap_srvlist(srvlist);
117 return 0;
119 return -1;
120 #endif
121 DEBUG(1,("No LDAP support\n"));
122 return -1;
125 static int net_lookup_dc(int argc, const char **argv)
127 struct in_addr *ip_list, addr;
128 char *pdc_str = NULL;
129 const char *domain=opt_target_workgroup;
130 int count, i;
131 BOOL list_ordered;
133 if (argc > 0)
134 domain=argv[0];
136 /* first get PDC */
137 if (!get_pdc_ip(domain, &addr))
138 return -1;
140 asprintf(&pdc_str, "%s", inet_ntoa(addr));
141 d_printf("%s\n", pdc_str);
143 if (!get_dc_list(domain, &ip_list, &count, &list_ordered)) {
144 SAFE_FREE(pdc_str);
145 return 0;
147 for (i=0;i<count;i++) {
148 char *dc_str = inet_ntoa(ip_list[i]);
149 if (!strequal(pdc_str, dc_str))
150 d_printf("%s\n", dc_str);
152 SAFE_FREE(pdc_str);
153 return 0;
156 static int net_lookup_master(int argc, const char **argv)
158 struct in_addr master_ip;
159 const char *domain=opt_target_workgroup;
161 if (argc > 0)
162 domain=argv[0];
164 if (!find_master_ip(domain, &master_ip))
165 return -1;
166 d_printf("%s\n", inet_ntoa(master_ip));
167 return 0;
170 static int net_lookup_kdc(int argc, const char **argv)
172 #ifdef HAVE_KRB5
173 krb5_error_code rc;
174 krb5_context ctx;
175 struct sockaddr_in *addrs;
176 int num_kdcs,i;
177 krb5_data realm;
178 char **realms;
180 rc = krb5_init_context(&ctx);
181 if (rc) {
182 DEBUG(1,("krb5_init_context failed (%s)\n",
183 error_message(rc)));
184 return -1;
187 if (argc>0) {
188 realm.data = (krb5_pointer) argv[0];
189 realm.length = strlen(argv[0]);
190 } else if (lp_realm() && *lp_realm()) {
191 realm.data = (krb5_pointer) lp_realm();
192 realm.length = strlen(realm.data);
193 } else {
194 rc = krb5_get_host_realm(ctx, NULL, &realms);
195 if (rc) {
196 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
197 error_message(rc)));
198 return -1;
200 realm.data = (krb5_pointer) *realms;
201 realm.length = strlen(realm.data);
204 rc = krb5_locate_kdc(ctx, &realm, &addrs, &num_kdcs, 0);
205 if (rc) {
206 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
207 return -1;
209 for (i=0;i<num_kdcs;i++)
210 if (addrs[i].sin_family == AF_INET)
211 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
212 ntohs(addrs[i].sin_port));
213 return 0;
215 #endif
216 DEBUG(1, ("No kerberos support\n"));
217 return -1;
221 /* lookup hosts or IP addresses using internal samba lookup fns */
222 int net_lookup(int argc, const char **argv)
224 struct functable func[] = {
225 {"HOST", net_lookup_host},
226 {"LDAP", net_lookup_ldap},
227 {"DC", net_lookup_dc},
228 {"MASTER", net_lookup_master},
229 {"KDC", net_lookup_kdc},
230 {NULL, NULL}
233 return net_run_function(argc, argv, func, net_lookup_usage);