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"
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);
40 DEBUG(4,("startdns_hosts_file: Can't open dns_hosts_file file %s. "
42 fname
, strerror(errno
)));
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
,
54 struct sockaddr_storage
*pss
, uint32_t *p_port
)
63 while(!x_feof(fp
) && !x_ferror(fp
)) {
64 char *name_type
= NULL
;
66 char *next_name
= NULL
;
73 if (!fgets_slash(line
,sizeof(line
),fp
)) {
83 if (next_token_talloc(ctx
, &ptr
, &name_type
, NULL
))
85 if (next_token_talloc(ctx
, &ptr
, &name
, NULL
))
87 if (strcasecmp(name_type
, "A") == 0) {
88 if (next_token_talloc(ctx
, &ptr
, &ip
, NULL
))
90 } else if (strcasecmp(name_type
, "SRV") == 0) {
91 if (next_token_talloc(ctx
, &ptr
, &next_name
, NULL
))
93 if (next_token_talloc(ctx
, &ptr
, &port
, NULL
))
95 } else if (strcasecmp(name_type
, "CNAME") == 0) {
96 if (next_token_talloc(ctx
, &ptr
, &next_name
, NULL
))
102 if (strcasecmp(name_type
, "A") == 0) {
104 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts A record [%s]\n",
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 "
115 } else if (strcasecmp(name_type
, "SRV") == 0) {
117 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts SRV record [%s]\n",
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",
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
) {
133 } else if (strcasecmp(name_type
, "CNAME") == 0) {
135 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts CNAME record [%s]\n",
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
) {
146 DEBUG(0,("getdns_hosts_fileent: unknown type %s\n", name_type
));
150 *pp_name
= talloc_strdup(ctx
, name
);
155 *pp_name_type
= talloc_strdup(ctx
, name_type
);
156 if (!*pp_name_type
) {
165 /********************************************************
166 Finish parsing the dns_hosts_file file.
167 *********************************************************/
169 static void enddns_hosts_file(XFILE
*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
,
182 struct sockaddr_storage
**return_iplist
,
186 * "dns_hosts" means parse the local dns_hosts file.
190 char *host_name
= NULL
;
191 char *name_type
= NULL
;
192 char *next_name
= NULL
;
193 struct sockaddr_storage return_ss
;
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 */
204 *return_iplist
= NULL
;
207 DEBUG(3,("resolve_dns_hosts: "
208 "Attempting dns_hosts lookup for name %s\n",
211 fp
= startdns_hosts_file(dns_hosts_file
);
214 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
216 ip_list_ctx
= talloc_new(mem_ctx
);
218 enddns_hosts_file(fp
);
219 return NT_STATUS_NO_MEMORY
;
222 ctx
= talloc_new(ip_list_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
)) {
232 ctx
= talloc_new(mem_ctx
);
234 enddns_hosts_file(fp
);
235 return NT_STATUS_NO_MEMORY
;
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
,
248 mem_ctx
, return_iplist
,
250 talloc_free(ip_list_ctx
);
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,
260 mem_ctx
, return_iplist
, return_count
);
261 talloc_free(ip_list_ctx
);
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
,
272 if ((*return_iplist
) == NULL
) {
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
;
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
);
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
,
300 struct sockaddr_storage
**return_iplist
,
303 return resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file
, name
, srv_lookup
,
305 mem_ctx
, return_iplist
, return_count
);