updated the 3.0 branch from the head branch - ready for alpha18
[obnox/samba/samba-obnox.git] / source3 / utils / net_lookup.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    net lookup command
4    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
5
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.
10    
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.
15    
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.  */
19
20 #include "includes.h"
21 #include "../utils/net.h"
22
23 int net_lookup_usage(int argc, const char **argv)
24 {
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 );
32         return -1;
33 }
34
35 /* lookup a hostname giving an IP */
36 static int net_lookup_host(int argc, const char **argv)
37 {
38         struct in_addr ip;
39         int name_type = 0x20;
40
41         if (argc == 0) return net_lookup_usage(argc, argv);
42         if (argc > 1) name_type = strtol(argv[1], NULL, 0);
43
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;
49         }
50
51         d_printf("%s\n", inet_ntoa(ip));
52         return 0;
53 }
54
55 static void print_ldap_srvlist(char *srvlist)
56 {
57         char *cur, *next;
58         struct in_addr ip;
59         BOOL printit;
60
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);
74 }
75                 
76
77 static int net_lookup_ldap(int argc, const char **argv)
78 {
79 #ifdef HAVE_LDAP
80         char *srvlist, *domain;
81         int rc, count;
82         struct in_addr *addr;
83         struct hostent *hostent;
84
85         if (argc > 0)
86                 domain = argv[0];
87         else
88                 domain = opt_target_workgroup;
89
90         DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
91         rc = ldap_domain2hostlist(domain, &srvlist);
92         if ((rc == LDAP_SUCCESS) && srvlist) {
93                 print_ldap_srvlist(srvlist);
94                 return 0;
95         }
96
97         DEBUG(9, ("Looking up DC for domain %s\n", domain));
98         if (!get_dc_list(True, domain, &addr, &count))
99                 return -1;
100
101         hostent = gethostbyaddr((char *) &addr->s_addr, sizeof(addr->s_addr),
102                                 AF_INET);
103         if (!hostent)
104                 return -1;
105
106         DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
107         domain = strchr(hostent->h_name, '.');
108         if (!domain)
109                 return -1;
110         domain++;
111
112         DEBUG(9, ("Looking up ldap for domain %s\n", domain));
113         rc = ldap_domain2hostlist(domain, &srvlist);
114         if ((rc == LDAP_SUCCESS) && srvlist) {
115                 print_ldap_srvlist(srvlist);
116                 return 0;
117         }
118         return -1;
119 #endif
120         DEBUG(1,("No LDAP support\n"));
121         return -1;
122 }
123
124 static int net_lookup_dc(int argc, const char **argv)
125 {
126         struct in_addr *ip_list;
127         char *pdc_str = NULL;
128         char *domain=opt_target_workgroup;
129         int count, i;
130
131         if (argc > 0)
132                 domain=argv[0];
133
134         /* first get PDC */
135         if (!get_dc_list(True, domain, &ip_list, &count))
136                 return -1;
137
138         asprintf(&pdc_str, "%s", inet_ntoa(*ip_list));
139         d_printf("%s\n", pdc_str);
140
141         if (!get_dc_list(False, domain, &ip_list, &count)) {
142                 SAFE_FREE(pdc_str);
143                 return 0;
144         }
145         for (i=0;i<count;i++) {
146                 char *dc_str = inet_ntoa(ip_list[i]);
147                 if (!strequal(pdc_str, dc_str))
148                         d_printf("%s\n", dc_str);
149         }
150         SAFE_FREE(pdc_str);
151         return 0;
152 }
153
154 static int net_lookup_master(int argc, const char **argv)
155 {
156         struct in_addr master_ip;
157         char *domain=opt_target_workgroup;
158
159         if (argc > 0)
160                 domain=argv[0];
161
162         if (!find_master_ip(domain, &master_ip))
163                 return -1;
164         d_printf("%s\n", inet_ntoa(master_ip));
165         return 0;
166 }
167
168 static int net_lookup_kdc(int argc, const char **argv)
169 {
170 #ifdef HAVE_KRB5
171         krb5_error_code rc;
172         krb5_context ctx;
173         struct sockaddr_in *addrs;
174         int num_kdcs,i;
175         krb5_data realm;
176         char **realms;
177
178         rc = krb5_init_context(&ctx);
179         if (rc) {
180                 DEBUG(1,("krb5_init_context failed (%s)\n", 
181                          error_message(rc)));
182                 return -1;
183         }
184
185         if (argc>0) {
186                 realm.data = (krb5_pointer) argv[0];
187                 realm.length = strlen(argv[0]);
188         } else if (lp_realm() && *lp_realm()) {
189                 realm.data = (krb5_pointer) lp_realm();
190                 realm.length = strlen(realm.data);
191         } else {
192                 rc = krb5_get_host_realm(ctx, NULL, &realms);
193                 if (rc) {
194                         DEBUG(1,("krb5_gethost_realm failed (%s)\n",
195                                  error_message(rc)));
196                         return -1;
197                 }
198                 realm.data = (krb5_pointer) *realms;
199                 realm.length = strlen(realm.data);
200         }
201
202         rc = krb5_locate_kdc(ctx, &realm, &addrs, &num_kdcs, 0);
203         if (rc) {
204                 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
205                 return -1;
206         }
207         for (i=0;i<num_kdcs;i++)
208                 if (addrs[i].sin_family == AF_INET) 
209                         d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
210                                  ntohs(addrs[i].sin_port));
211         return 0;
212
213 #endif  
214         DEBUG(1, ("No kerberos support\n"));
215         return -1;
216 }
217 /* lookup hosts or IP addresses using internal samba lookup fns */
218 int net_lookup(int argc, const char **argv)
219 {
220         struct functable func[] = {
221                 {"HOST", net_lookup_host},
222                 {"LDAP", net_lookup_ldap},
223                 {"DC", net_lookup_dc},
224                 {"MASTER", net_lookup_master},
225                 {"KDC", net_lookup_kdc},
226                 {NULL, NULL}
227         };
228
229         return net_run_function(argc, argv, func, net_lookup_usage);
230 }