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 */
27 #include "lib/util/xfile.h"
28 #include "lib/util/util_net.h"
29 #include "system/filesys.h"
30 #include "system/network.h"
31 #include "libcli/nbt/libnbt.h"
33 /********************************************************
34 Start parsing the dns_hosts_file file.
35 *********************************************************/
37 static XFILE
*startdns_hosts_file(const char *fname
)
39 XFILE
*fp
= x_fopen(fname
,O_RDONLY
, 0);
41 DEBUG(4,("startdns_hosts_file: Can't open dns_hosts_file file %s. "
43 fname
, strerror(errno
)));
49 /********************************************************
50 Parse the next line in the dns_hosts_file file.
51 *********************************************************/
53 static bool getdns_hosts_fileent(TALLOC_CTX
*ctx
, XFILE
*fp
, char **pp_name
, char **pp_name_type
,
55 struct sockaddr_storage
*pss
, uint32_t *p_port
)
64 while(!x_feof(fp
) && !x_ferror(fp
)) {
65 char *name_type
= NULL
;
67 char *next_name
= NULL
;
74 if (!fgets_slash(line
,sizeof(line
),fp
)) {
84 if (next_token_talloc(ctx
, &ptr
, &name_type
, NULL
))
86 if (next_token_talloc(ctx
, &ptr
, &name
, NULL
))
88 if (strcasecmp(name_type
, "A") == 0) {
89 if (next_token_talloc(ctx
, &ptr
, &ip
, NULL
))
91 } else if (strcasecmp(name_type
, "SRV") == 0) {
92 if (next_token_talloc(ctx
, &ptr
, &next_name
, NULL
))
94 if (next_token_talloc(ctx
, &ptr
, &port
, NULL
))
96 } else if (strcasecmp(name_type
, "CNAME") == 0) {
97 if (next_token_talloc(ctx
, &ptr
, &next_name
, NULL
))
103 if (strcasecmp(name_type
, "A") == 0) {
105 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts A record [%s]\n",
109 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s\n",
110 name_type
, name
, ip
));
111 if (!interpret_string_addr(pss
, ip
, AI_NUMERICHOST
)) {
112 DEBUG(0,("getdns_hosts_fileent: invalid address "
116 } else if (strcasecmp(name_type
, "SRV") == 0) {
118 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts SRV record [%s]\n",
122 *p_port
= strtoul(port
, NULL
, 10);
123 if (*p_port
== UINT32_MAX
) {
124 DEBUG(0, ("getdns_hosts_fileent: Ill formed hosts SRV record [%s] (invalid port: %s)\n",
128 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s %u\n",
129 name_type
, name
, next_name
, (unsigned int)*p_port
));
130 *pp_next_name
= talloc_strdup(ctx
, next_name
);
131 if (!*pp_next_name
) {
134 } else if (strcasecmp(name_type
, "CNAME") == 0) {
136 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts CNAME record [%s]\n",
140 DEBUG(4, ("getdns_hosts_fileent: host entry: %s %s %s\n",
141 name_type
, name
, next_name
));
142 *pp_next_name
= talloc_strdup(ctx
, next_name
);
143 if (!*pp_next_name
) {
147 DEBUG(0,("getdns_hosts_fileent: unknown type %s\n", name_type
));
151 *pp_name
= talloc_strdup(ctx
, name
);
156 *pp_name_type
= talloc_strdup(ctx
, name_type
);
157 if (!*pp_name_type
) {
166 /********************************************************
167 Finish parsing the dns_hosts_file file.
168 *********************************************************/
170 static void enddns_hosts_file(XFILE
*fp
)
175 /********************************************************
176 Resolve via "dns_hosts" method.
177 *********************************************************/
179 static NTSTATUS
resolve_dns_hosts_file_as_sockaddr_recurse(const char *dns_hosts_file
,
180 const char *name
, bool srv_lookup
,
181 int level
, uint32_t port
,
183 struct sockaddr_storage
**return_iplist
,
187 * "dns_hosts" means parse the local dns_hosts file.
191 char *host_name
= NULL
;
192 char *name_type
= NULL
;
193 char *next_name
= NULL
;
194 struct sockaddr_storage return_ss
;
196 NTSTATUS status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
197 TALLOC_CTX
*ctx
= NULL
;
198 TALLOC_CTX
*ip_list_ctx
= NULL
;
200 /* Don't recurse forever, even on our own flat files */
205 *return_iplist
= NULL
;
208 DEBUG(3,("resolve_dns_hosts: "
209 "Attempting dns_hosts lookup for name %s\n",
212 fp
= startdns_hosts_file(dns_hosts_file
);
215 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
217 ip_list_ctx
= talloc_new(mem_ctx
);
219 enddns_hosts_file(fp
);
220 return NT_STATUS_NO_MEMORY
;
223 ctx
= talloc_new(ip_list_ctx
);
225 talloc_free(ip_list_ctx
);
226 enddns_hosts_file(fp
);
227 return NT_STATUS_NO_MEMORY
;
230 while (getdns_hosts_fileent(ctx
, fp
, &host_name
, &name_type
, &next_name
, &return_ss
, &srv_port
)) {
231 if (!strequal(name
, host_name
)) {
233 ctx
= talloc_new(mem_ctx
);
235 enddns_hosts_file(fp
);
236 return NT_STATUS_NO_MEMORY
;
243 if (strcasecmp(name_type
, "SRV") == 0) {
244 /* we only accept one host name per SRV entry */
245 enddns_hosts_file(fp
);
246 status
= resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file
, next_name
,
249 mem_ctx
, return_iplist
,
251 talloc_free(ip_list_ctx
);
256 } else if (strcasecmp(name_type
, "CNAME") == 0) {
257 /* we only accept one host name per CNAME */
258 enddns_hosts_file(fp
);
259 status
= resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file
, next_name
, false,
261 mem_ctx
, return_iplist
, return_count
);
262 talloc_free(ip_list_ctx
);
264 } else if (strcasecmp(name_type
, "A") == 0) {
265 /* Set the specified port (possibly from a SRV lookup) into the structure we return */
266 set_sockaddr_port((struct sockaddr
*)&return_ss
, port
);
268 /* We are happy to keep looking for other possible A record matches */
269 *return_iplist
= talloc_realloc(ip_list_ctx
, (*return_iplist
),
270 struct sockaddr_storage
,
273 if ((*return_iplist
) == NULL
) {
275 enddns_hosts_file(fp
);
276 DEBUG(3,("resolve_dns_hosts: talloc_realloc fail !\n"));
277 return NT_STATUS_NO_MEMORY
;
280 (*return_iplist
)[*return_count
] = return_ss
;
283 /* we found something */
284 status
= NT_STATUS_OK
;
288 talloc_steal(mem_ctx
, *return_iplist
);
289 TALLOC_FREE(ip_list_ctx
);
290 enddns_hosts_file(fp
);
294 /********************************************************
295 Resolve via "dns_hosts" method.
296 *********************************************************/
298 NTSTATUS
resolve_dns_hosts_file_as_sockaddr(const char *dns_hosts_file
,
299 const char *name
, bool srv_lookup
,
301 struct sockaddr_storage
**return_iplist
,
304 return resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file
, name
, srv_lookup
,
306 mem_ctx
, return_iplist
, return_count
);