s3:libsmb: add tstream_cli_np_use_trans() and the needed logic
[Samba/gebeck_regimport.git] / libcli / nbt / dns_hosts_file.c
blob810e4ebb0c91e25fc939a9326713bea5097405ba
1 /*
2 Unix SMB/CIFS implementation.
4 read a file containing DNS names, types and IP addresses
6 Copyright (C) Andrew Tridgell 1994-1998
7 Copyright (C) Jeremy Allison 2007
8 Copyright (C) Andrew Bartlett 2009.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* The purpose of this file is to read the file generated by the samba_dnsupdate script */
26 #include "includes.h"
27 #include "lib/util/xfile.h"
28 #include "lib/util/util_net.h"
29 #include "system/filesys.h"
30 #include "system/network.h"
32 /********************************************************
33 Start parsing the dns_hosts_file file.
34 *********************************************************/
36 static XFILE *startdns_hosts_file(const char *fname)
38 XFILE *fp = x_fopen(fname,O_RDONLY, 0);
39 if (!fp) {
40 DEBUG(4,("startdns_hosts_file: Can't open dns_hosts_file file %s. "
41 "Error was %s\n",
42 fname, strerror(errno)));
43 return NULL;
45 return fp;
48 /********************************************************
49 Parse the next line in the dns_hosts_file file.
50 *********************************************************/
52 static bool getdns_hosts_fileent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, char **pp_name_type,
53 char **pp_next_name,
54 struct sockaddr_storage *pss, uint32_t *p_port)
56 char line[1024];
58 *pp_name = NULL;
59 *pp_name_type = NULL;
60 *pp_next_name = NULL;
61 *p_port = 0;
63 while(!x_feof(fp) && !x_ferror(fp)) {
64 char *name_type = NULL;
65 char *name = NULL;
66 char *next_name = NULL;
67 char *ip = NULL;
68 char *port = NULL;
70 const char *ptr;
71 int count = 0;
73 if (!fgets_slash(line,sizeof(line),fp)) {
74 continue;
77 if (*line == '#') {
78 continue;
81 ptr = line;
83 if (next_token_talloc(ctx, &ptr, &name_type, NULL))
84 ++count;
85 if (next_token_talloc(ctx, &ptr, &name, NULL))
86 ++count;
87 if (strcasecmp(name_type, "A") == 0) {
88 if (next_token_talloc(ctx, &ptr, &ip, NULL))
89 ++count;
90 } else if (strcasecmp(name_type, "SRV") == 0) {
91 if (next_token_talloc(ctx, &ptr, &next_name, NULL))
92 ++count;
93 if (next_token_talloc(ctx, &ptr, &port, NULL))
94 ++count;
95 } else if (strcasecmp(name_type, "CNAME") == 0) {
96 if (next_token_talloc(ctx, &ptr, &next_name, NULL))
97 ++count;
99 if (count <= 0)
100 continue;
102 if (strcasecmp(name_type, "A") == 0) {
103 if (count != 3) {
104 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts A record [%s]\n",
105 line));
106 continue;
108 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s\n",
109 name_type, name, ip));
110 if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) {
111 DEBUG(0,("getdns_hosts_fileent: invalid address "
112 "%s.\n", ip));
115 } else if (strcasecmp(name_type, "SRV") == 0) {
116 if (count != 4) {
117 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts SRV record [%s]\n",
118 line));
119 continue;
121 *p_port = strtoul(port, NULL, 10);
122 if (*p_port == UINT32_MAX) {
123 DEBUG(0, ("getdns_hosts_fileent: Ill formed hosts SRV record [%s] (invalid port: %s)\n",
124 line, port));
125 continue;
127 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s %u\n",
128 name_type, name, next_name, (unsigned int)*p_port));
129 *pp_next_name = talloc_strdup(ctx, next_name);
130 if (!*pp_next_name) {
131 return false;
133 } else if (strcasecmp(name_type, "CNAME") == 0) {
134 if (count != 3) {
135 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts CNAME record [%s]\n",
136 line));
137 continue;
139 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s\n",
140 name_type, name, next_name));
141 *pp_next_name = talloc_strdup(ctx, next_name);
142 if (!*pp_next_name) {
143 return false;
145 } else {
146 DEBUG(0,("getdns_hosts_fileent: unknown type %s\n", name_type));
147 continue;
150 *pp_name = talloc_strdup(ctx, name);
151 if (!*pp_name) {
152 return false;
155 *pp_name_type = talloc_strdup(ctx, name_type);
156 if (!*pp_name_type) {
157 return false;
159 return true;
162 return false;
165 /********************************************************
166 Finish parsing the dns_hosts_file file.
167 *********************************************************/
169 static void enddns_hosts_file(XFILE *fp)
171 x_fclose(fp);
174 /********************************************************
175 Resolve via "dns_hosts" method.
176 *********************************************************/
178 static NTSTATUS resolve_dns_hosts_file_as_sockaddr_recurse(const char *dns_hosts_file,
179 const char *name, bool srv_lookup,
180 int level, uint32_t port,
181 TALLOC_CTX *mem_ctx,
182 struct sockaddr_storage **return_iplist,
183 int *return_count)
186 * "dns_hosts" means parse the local dns_hosts file.
189 XFILE *fp;
190 char *host_name = NULL;
191 char *name_type = NULL;
192 char *next_name = NULL;
193 struct sockaddr_storage return_ss;
194 uint32_t srv_port;
195 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
196 TALLOC_CTX *ctx = NULL;
197 TALLOC_CTX *ip_list_ctx = NULL;
199 /* Don't recurse forever, even on our own flat files */
200 if (level > 11) {
204 *return_iplist = NULL;
205 *return_count = 0;
207 DEBUG(3,("resolve_dns_hosts: "
208 "Attempting dns_hosts lookup for name %s\n",
209 name));
211 fp = startdns_hosts_file(dns_hosts_file);
213 if ( fp == NULL )
214 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
216 ip_list_ctx = talloc_new(mem_ctx);
217 if (!ip_list_ctx) {
218 enddns_hosts_file(fp);
219 return NT_STATUS_NO_MEMORY;
222 ctx = talloc_new(ip_list_ctx);
223 if (!ctx) {
224 talloc_free(ip_list_ctx);
225 enddns_hosts_file(fp);
226 return NT_STATUS_NO_MEMORY;
229 while (getdns_hosts_fileent(ctx, fp, &host_name, &name_type, &next_name, &return_ss, &srv_port)) {
230 if (!strequal(name, host_name)) {
231 TALLOC_FREE(ctx);
232 ctx = talloc_new(mem_ctx);
233 if (!ctx) {
234 enddns_hosts_file(fp);
235 return NT_STATUS_NO_MEMORY;
238 continue;
241 if (srv_lookup) {
242 if (strcasecmp(name_type, "SRV") == 0) {
243 /* we only accept one host name per SRV entry */
244 enddns_hosts_file(fp);
245 status = resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file, next_name,
246 false,
247 level + 1, srv_port,
248 mem_ctx, return_iplist,
249 return_count);
250 talloc_free(ip_list_ctx);
251 return status;
252 } else {
253 continue;
255 } else if (strcasecmp(name_type, "CNAME") == 0) {
256 /* we only accept one host name per CNAME */
257 enddns_hosts_file(fp);
258 status = resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file, next_name, false,
259 level + 1, port,
260 mem_ctx, return_iplist, return_count);
261 talloc_free(ip_list_ctx);
262 return status;
263 } else if (strcasecmp(name_type, "A") == 0) {
264 /* Set the specified port (possibly from a SRV lookup) into the structure we return */
265 set_sockaddr_port((struct sockaddr *)&return_ss, port);
267 /* We are happy to keep looking for other possible A record matches */
268 *return_iplist = talloc_realloc(ip_list_ctx, (*return_iplist),
269 struct sockaddr_storage,
270 (*return_count)+1);
272 if ((*return_iplist) == NULL) {
273 TALLOC_FREE(ctx);
274 enddns_hosts_file(fp);
275 DEBUG(3,("resolve_dns_hosts: talloc_realloc fail !\n"));
276 return NT_STATUS_NO_MEMORY;
279 (*return_iplist)[*return_count] = return_ss;
280 *return_count += 1;
282 /* we found something */
283 status = NT_STATUS_OK;
287 talloc_steal(mem_ctx, *return_iplist);
288 TALLOC_FREE(ip_list_ctx);
289 enddns_hosts_file(fp);
290 return status;
293 /********************************************************
294 Resolve via "dns_hosts" method.
295 *********************************************************/
297 NTSTATUS resolve_dns_hosts_file_as_sockaddr(const char *dns_hosts_file,
298 const char *name, bool srv_lookup,
299 TALLOC_CTX *mem_ctx,
300 struct sockaddr_storage **return_iplist,
301 int *return_count)
303 return resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file, name, srv_lookup,
304 0, 0,
305 mem_ctx, return_iplist, return_count);