source: trunk/server/libcli/nbt/dns_hosts_file.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 8.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 read a file containing DNS names, types and IP addresses
5
6 Copyright (C) Andrew Tridgell 1994-1998
7 Copyright (C) Jeremy Allison 2007
8 Copyright (C) Andrew Bartlett 2009.
9
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.
14
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.
19
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/>.
22*/
23
24/* The purpose of this file is to read the file generated by the samba_dnsupdate script */
25
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"
31#include "libcli/nbt/libnbt.h"
32
33/********************************************************
34 Start parsing the dns_hosts_file file.
35*********************************************************/
36
37static XFILE *startdns_hosts_file(const char *fname)
38{
39 XFILE *fp = x_fopen(fname,O_RDONLY, 0);
40 if (!fp) {
41 DEBUG(4,("startdns_hosts_file: Can't open dns_hosts_file file %s. "
42 "Error was %s\n",
43 fname, strerror(errno)));
44 return NULL;
45 }
46 return fp;
47}
48
49/********************************************************
50 Parse the next line in the dns_hosts_file file.
51*********************************************************/
52
53static bool getdns_hosts_fileent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, char **pp_name_type,
54 char **pp_next_name,
55 struct sockaddr_storage *pss, uint32_t *p_port)
56{
57 char line[1024];
58
59 *pp_name = NULL;
60 *pp_name_type = NULL;
61 *pp_next_name = NULL;
62 *p_port = 0;
63
64 while(!x_feof(fp) && !x_ferror(fp)) {
65 char *name_type = NULL;
66 char *name = NULL;
67 char *next_name = NULL;
68 char *ip = NULL;
69 char *port = NULL;
70
71 const char *ptr;
72 int count = 0;
73
74 if (!fgets_slash(line,sizeof(line),fp)) {
75 continue;
76 }
77
78 if (*line == '#') {
79 continue;
80 }
81
82 ptr = line;
83
84 if (next_token_talloc(ctx, &ptr, &name_type, NULL))
85 ++count;
86 if (next_token_talloc(ctx, &ptr, &name, NULL))
87 ++count;
88 if (strcasecmp(name_type, "A") == 0) {
89 if (next_token_talloc(ctx, &ptr, &ip, NULL))
90 ++count;
91 } else if (strcasecmp(name_type, "SRV") == 0) {
92 if (next_token_talloc(ctx, &ptr, &next_name, NULL))
93 ++count;
94 if (next_token_talloc(ctx, &ptr, &port, NULL))
95 ++count;
96 } else if (strcasecmp(name_type, "CNAME") == 0) {
97 if (next_token_talloc(ctx, &ptr, &next_name, NULL))
98 ++count;
99 }
100 if (count <= 0)
101 continue;
102
103 if (strcasecmp(name_type, "A") == 0) {
104 if (count != 3) {
105 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts A record [%s]\n",
106 line));
107 continue;
108 }
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 "
113 "%s.\n", ip));
114 }
115
116 } else if (strcasecmp(name_type, "SRV") == 0) {
117 if (count != 4) {
118 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts SRV record [%s]\n",
119 line));
120 continue;
121 }
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",
125 line, port));
126 continue;
127 }
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) {
132 return false;
133 }
134 } else if (strcasecmp(name_type, "CNAME") == 0) {
135 if (count != 3) {
136 DEBUG(0,("getdns_hosts_fileent: Ill formed hosts CNAME record [%s]\n",
137 line));
138 continue;
139 }
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) {
144 return false;
145 }
146 } else {
147 DEBUG(0,("getdns_hosts_fileent: unknown type %s\n", name_type));
148 continue;
149 }
150
151 *pp_name = talloc_strdup(ctx, name);
152 if (!*pp_name) {
153 return false;
154 }
155
156 *pp_name_type = talloc_strdup(ctx, name_type);
157 if (!*pp_name_type) {
158 return false;
159 }
160 return true;
161 }
162
163 return false;
164}
165
166/********************************************************
167 Finish parsing the dns_hosts_file file.
168*********************************************************/
169
170static void enddns_hosts_file(XFILE *fp)
171{
172 x_fclose(fp);
173}
174
175/********************************************************
176 Resolve via "dns_hosts" method.
177*********************************************************/
178
179static 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,
182 TALLOC_CTX *mem_ctx,
183 struct sockaddr_storage **return_iplist,
184 int *return_count)
185{
186 /*
187 * "dns_hosts" means parse the local dns_hosts file.
188 */
189
190 XFILE *fp;
191 char *host_name = NULL;
192 char *name_type = NULL;
193 char *next_name = NULL;
194 struct sockaddr_storage return_ss;
195 uint32_t srv_port;
196 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
197 TALLOC_CTX *ctx = NULL;
198 TALLOC_CTX *ip_list_ctx = NULL;
199
200 /* Don't recurse forever, even on our own flat files */
201 if (level > 11) {
202
203 }
204
205 *return_iplist = NULL;
206 *return_count = 0;
207
208 DEBUG(3,("resolve_dns_hosts: "
209 "Attempting dns_hosts lookup for name %s\n",
210 name));
211
212 fp = startdns_hosts_file(dns_hosts_file);
213
214 if ( fp == NULL )
215 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
216
217 ip_list_ctx = talloc_new(mem_ctx);
218 if (!ip_list_ctx) {
219 enddns_hosts_file(fp);
220 return NT_STATUS_NO_MEMORY;
221 }
222
223 ctx = talloc_new(ip_list_ctx);
224 if (!ctx) {
225 talloc_free(ip_list_ctx);
226 enddns_hosts_file(fp);
227 return NT_STATUS_NO_MEMORY;
228 }
229
230 while (getdns_hosts_fileent(ctx, fp, &host_name, &name_type, &next_name, &return_ss, &srv_port)) {
231 if (!strequal(name, host_name)) {
232 TALLOC_FREE(ctx);
233 ctx = talloc_new(mem_ctx);
234 if (!ctx) {
235 enddns_hosts_file(fp);
236 return NT_STATUS_NO_MEMORY;
237 }
238
239 continue;
240 }
241
242 if (srv_lookup) {
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,
247 false,
248 level + 1, srv_port,
249 mem_ctx, return_iplist,
250 return_count);
251 talloc_free(ip_list_ctx);
252 return status;
253 } else {
254 continue;
255 }
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,
260 level + 1, port,
261 mem_ctx, return_iplist, return_count);
262 talloc_free(ip_list_ctx);
263 return status;
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);
267
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,
271 (*return_count)+1);
272
273 if ((*return_iplist) == NULL) {
274 TALLOC_FREE(ctx);
275 enddns_hosts_file(fp);
276 DEBUG(3,("resolve_dns_hosts: talloc_realloc fail !\n"));
277 return NT_STATUS_NO_MEMORY;
278 }
279
280 (*return_iplist)[*return_count] = return_ss;
281 *return_count += 1;
282
283 /* we found something */
284 status = NT_STATUS_OK;
285 }
286 }
287
288 talloc_steal(mem_ctx, *return_iplist);
289 TALLOC_FREE(ip_list_ctx);
290 enddns_hosts_file(fp);
291 return status;
292}
293
294/********************************************************
295 Resolve via "dns_hosts" method.
296*********************************************************/
297
298NTSTATUS resolve_dns_hosts_file_as_sockaddr(const char *dns_hosts_file,
299 const char *name, bool srv_lookup,
300 TALLOC_CTX *mem_ctx,
301 struct sockaddr_storage **return_iplist,
302 int *return_count)
303{
304 return resolve_dns_hosts_file_as_sockaddr_recurse(dns_hosts_file, name, srv_lookup,
305 0, 0,
306 mem_ctx, return_iplist, return_count);
307}
Note: See TracBrowser for help on using the repository browser.