r14156: Fix coverity #114: free storage alloc'ed by sstring_sub()
[Samba/nascimento.git] / source3 / utils / net_lookup.c
blobdd2d666d5a0b0f9f7043a59cf8560d4ded1f919d
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"
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"
34 return -1;
37 /* lookup a hostname giving an IP */
38 static int net_lookup_host(int argc, const char **argv)
40 struct in_addr ip;
41 int name_type = 0x20;
42 const char *name = argv[0];
43 char *p;
45 if (argc == 0)
46 return net_lookup_usage(argc, argv);
48 p = strchr_m(name,'#');
49 if (p) {
50 *p = '\0';
51 sscanf(++p,"%x",&name_type);
54 if (!resolve_name(name, &ip, name_type)) {
55 /* we deliberately use DEBUG() here to send it to stderr
56 so scripts aren't mucked up */
57 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
58 return -1;
61 d_printf("%s\n", inet_ntoa(ip));
62 return 0;
65 #ifdef HAVE_LDAP
66 static void print_ldap_srvlist(char *srvlist)
68 char *cur, *next;
69 struct in_addr ip;
70 BOOL printit;
72 cur = srvlist;
73 do {
74 next = strchr(cur,':');
75 if (next) *next++='\0';
76 printit = resolve_name(cur, &ip, 0x20);
77 cur=next;
78 next=cur ? strchr(cur,' ') :NULL;
79 if (next)
80 *next++='\0';
81 if (printit)
82 d_printf("%s:%s\n", inet_ntoa(ip), cur?cur:"");
83 cur = next;
84 } while (next);
86 #endif
88 static int net_lookup_ldap(int argc, const char **argv)
90 #ifdef HAVE_ADS
91 char *srvlist;
92 const char *domain;
93 int rc;
94 struct in_addr addr;
95 struct hostent *hostent;
97 if (argc > 0)
98 domain = argv[0];
99 else
100 domain = opt_target_workgroup;
102 DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
103 rc = ldap_domain2hostlist(domain, &srvlist);
104 if ((rc == LDAP_SUCCESS) && srvlist) {
105 print_ldap_srvlist(srvlist);
106 return 0;
109 DEBUG(9, ("Looking up DC for domain %s\n", domain));
110 if (!get_pdc_ip(domain, &addr))
111 return -1;
113 hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
114 AF_INET);
115 if (!hostent)
116 return -1;
118 DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
119 domain = strchr(hostent->h_name, '.');
120 if (!domain)
121 return -1;
122 domain++;
124 DEBUG(9, ("Looking up ldap for domain %s\n", domain));
125 rc = ldap_domain2hostlist(domain, &srvlist);
126 if ((rc == LDAP_SUCCESS) && srvlist) {
127 print_ldap_srvlist(srvlist);
128 return 0;
130 return -1;
131 #endif
132 DEBUG(1,("No ADS support\n"));
133 return -1;
136 static int net_lookup_dc(int argc, const char **argv)
138 struct ip_service *ip_list;
139 struct in_addr addr;
140 char *pdc_str = NULL;
141 const char *domain=opt_target_workgroup;
142 int count, i;
144 if (argc > 0)
145 domain=argv[0];
147 /* first get PDC */
148 if (!get_pdc_ip(domain, &addr))
149 return -1;
151 asprintf(&pdc_str, "%s", inet_ntoa(addr));
152 d_printf("%s\n", pdc_str);
154 if (!get_sorted_dc_list(domain, &ip_list, &count, False)) {
155 SAFE_FREE(pdc_str);
156 return 0;
158 for (i=0;i<count;i++) {
159 char *dc_str = inet_ntoa(ip_list[i].ip);
160 if (!strequal(pdc_str, dc_str))
161 d_printf("%s\n", dc_str);
163 SAFE_FREE(pdc_str);
164 return 0;
167 static int net_lookup_master(int argc, const char **argv)
169 struct in_addr master_ip;
170 const char *domain=opt_target_workgroup;
172 if (argc > 0)
173 domain=argv[0];
175 if (!find_master_ip(domain, &master_ip))
176 return -1;
177 d_printf("%s\n", inet_ntoa(master_ip));
178 return 0;
181 static int net_lookup_kdc(int argc, const char **argv)
183 #ifdef HAVE_KRB5
184 krb5_error_code rc;
185 krb5_context ctx;
186 struct sockaddr_in *addrs;
187 int num_kdcs,i;
188 krb5_data realm;
189 char **realms;
191 initialize_krb5_error_table();
192 rc = krb5_init_context(&ctx);
193 if (rc) {
194 DEBUG(1,("krb5_init_context failed (%s)\n",
195 error_message(rc)));
196 return -1;
199 if (argc>0) {
200 realm.data = CONST_DISCARD(krb5_pointer, argv[0]);
201 realm.length = strlen(argv[0]);
202 } else if (lp_realm() && *lp_realm()) {
203 realm.data = (krb5_pointer) lp_realm();
204 realm.length = strlen(realm.data);
205 } else {
206 rc = krb5_get_host_realm(ctx, NULL, &realms);
207 if (rc) {
208 DEBUG(1,("krb5_gethost_realm failed (%s)\n",
209 error_message(rc)));
210 return -1;
212 realm.data = (krb5_pointer) *realms;
213 realm.length = strlen(realm.data);
216 rc = krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
217 if (rc) {
218 DEBUG(1, ("krb5_locate_kdc failed (%s)\n", error_message(rc)));
219 return -1;
221 for (i=0;i<num_kdcs;i++)
222 if (addrs[i].sin_family == AF_INET)
223 d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
224 ntohs(addrs[i].sin_port));
225 return 0;
227 #endif
228 DEBUG(1, ("No kerberos support\n"));
229 return -1;
232 static int net_lookup_name(int argc, const char **argv)
234 const char *dom, *name;
235 DOM_SID sid;
236 enum SID_NAME_USE type;
238 if (argc != 1) {
239 d_printf("usage: net lookup name <name>\n");
240 return -1;
243 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ALL,
244 &dom, &name, &sid, &type)) {
245 d_printf("Could not lookup name %s\n", argv[0]);
246 return -1;
249 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
250 type, sid_type_lookup(type), dom, name);
251 return 0;
254 static int net_lookup_sid(int argc, const char **argv)
256 const char *dom, *name;
257 DOM_SID sid;
258 enum SID_NAME_USE type;
260 if (argc != 1) {
261 d_printf("usage: net lookup sid <sid>\n");
262 return -1;
265 if (!string_to_sid(&sid, argv[0])) {
266 d_printf("Could not convert %s to SID\n", argv[0]);
267 return -1;
270 if (!lookup_sid(tmp_talloc_ctx(), &sid,
271 &dom, &name, &type)) {
272 d_printf("Could not lookup name %s\n", argv[0]);
273 return -1;
276 d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
277 type, sid_type_lookup(type), dom, name);
278 return 0;
281 /* lookup hosts or IP addresses using internal samba lookup fns */
282 int net_lookup(int argc, const char **argv)
284 int i;
286 struct functable table[] = {
287 {"HOST", net_lookup_host},
288 {"LDAP", net_lookup_ldap},
289 {"DC", net_lookup_dc},
290 {"MASTER", net_lookup_master},
291 {"KDC", net_lookup_kdc},
292 {"NAME", net_lookup_name},
293 {"SID", net_lookup_sid},
294 {NULL, NULL}
297 if (argc < 1) {
298 d_printf("\nUsage: \n");
299 return net_lookup_usage(argc, argv);
301 for (i=0; table[i].funcname; i++) {
302 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
303 return table[i].fn(argc-1, argv+1);
306 /* Default to lookup a hostname so 'net lookup foo#1b' can be
307 used instead of 'net lookup host foo#1b'. The host syntax
308 is a bit confusing as non #00 names can't really be
309 considered hosts as such. */
311 return net_lookup_host(argc, argv);