2 Unix SMB/CIFS implementation.
4 manipulate nbt name structures
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/>.
25 #include "lib/util/xfile.h"
26 #include "lib/util/util_net.h"
27 #include "system/filesys.h"
28 #include "system/network.h"
30 /********************************************************
31 Start parsing the lmhosts file.
32 *********************************************************/
34 XFILE *startlmhosts(const char *fname)
36 XFILE *fp = x_fopen(fname,O_RDONLY, 0);
38 DEBUG(4,("startlmhosts: Can't open lmhosts file %s. "
40 fname, strerror(errno)));
46 /********************************************************
47 Parse the next line in the lmhosts file.
48 *********************************************************/
50 bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type,
51 struct sockaddr_storage *pss)
57 while(!x_feof(fp) && !x_ferror(fp)) {
68 if (!fgets_slash(line,sizeof(line),fp)) {
78 if (next_token_talloc(ctx, &ptr, &ip, NULL))
80 if (next_token_talloc(ctx, &ptr, &name, NULL))
82 if (next_token_talloc(ctx, &ptr, &flags, NULL))
84 if (next_token_talloc(ctx, &ptr, &extra, NULL))
90 if (count > 0 && count < 2) {
91 DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",
97 DEBUG(0,("getlmhostsent: too many columns "
98 "in lmhosts file (obsolete syntax)\n"));
103 flags = talloc_strdup(ctx, "");
109 DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n",
112 if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
113 DEBUG(0,("getlmhostsent: group flag "
114 "in lmhosts ignored (obsolete)\n"));
118 if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) {
119 DEBUG(0,("getlmhostsent: invalid address "
123 /* Extra feature. If the name ends in '#XX',
124 * where XX is a hex number, then only add that name type. */
125 if((ptr1 = strchr_m(name, '#')) != NULL) {
129 *name_type = (int)strtol(ptr1, &endptr, 16);
130 if(!*ptr1 || (endptr == ptr1)) {
131 DEBUG(0,("getlmhostsent: invalid name "
132 "%s containing '#'.\n", name));
136 *(--ptr1) = '\0'; /* Truncate at the '#' */
139 *pp_name = talloc_strdup(ctx, name);
149 /********************************************************
150 Finish parsing the lmhosts file.
151 *********************************************************/
153 void endlmhosts(XFILE *fp)
158 /********************************************************
159 Resolve via "lmhosts" method.
160 *********************************************************/
162 NTSTATUS resolve_lmhosts_file_as_sockaddr(const char *lmhosts_file,
163 const char *name, int name_type,
165 struct sockaddr_storage **return_iplist,
169 * "lmhosts" means parse the local lmhosts file.
173 char *lmhost_name = NULL;
175 struct sockaddr_storage return_ss;
176 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
177 TALLOC_CTX *ctx = NULL;
179 *return_iplist = NULL;
182 DEBUG(3,("resolve_lmhosts: "
183 "Attempting lmhosts lookup for name %s<0x%x>\n",
186 fp = startlmhosts(lmhosts_file);
189 return NT_STATUS_NO_SUCH_FILE;
191 ctx = talloc_new(mem_ctx);
194 return NT_STATUS_NO_MEMORY;
197 while (getlmhostsent(ctx, fp, &lmhost_name, &name_type2, &return_ss)) {
199 if (!strequal(name, lmhost_name)) {
200 TALLOC_FREE(lmhost_name);
204 if ((name_type2 != -1) && (name_type != name_type2)) {
205 TALLOC_FREE(lmhost_name);
209 *return_iplist = talloc_realloc(ctx, (*return_iplist),
210 struct sockaddr_storage,
213 if ((*return_iplist) == NULL) {
216 DEBUG(3,("resolve_lmhosts: talloc_realloc fail !\n"));
217 return NT_STATUS_NO_MEMORY;
220 (*return_iplist)[*return_count] = return_ss;
223 /* we found something */
224 status = NT_STATUS_OK;
226 /* Multiple names only for DC lookup */
227 if (name_type != 0x1c)
231 talloc_steal(mem_ctx, *return_iplist);