docs: Update man 7 samba.
[Samba.git] / libcli / nbt / lmhosts.c
blob3e746f01f27c4a0297f149e49c212e5e686e69a0
1 /*
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/>.
24 #include "includes.h"
25 #include "lib/util/xfile.h"
26 #include "lib/util/util_net.h"
27 #include "system/filesys.h"
28 #include "system/network.h"
29 #include "../libcli/nbt/libnbt.h"
31 /********************************************************
32 Start parsing the lmhosts file.
33 *********************************************************/
35 XFILE *startlmhosts(const char *fname)
37 XFILE *fp = x_fopen(fname,O_RDONLY, 0);
38 if (!fp) {
39 DEBUG(4,("startlmhosts: Can't open lmhosts file %s. "
40 "Error was %s\n",
41 fname, strerror(errno)));
42 return NULL;
44 return fp;
47 /********************************************************
48 Parse the next line in the lmhosts file.
49 *********************************************************/
51 bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type,
52 struct sockaddr_storage *pss)
54 char line[1024];
56 *pp_name = NULL;
58 while(!x_feof(fp) && !x_ferror(fp)) {
59 char *ip = NULL;
60 char *flags = NULL;
61 char *extra = NULL;
62 char *name = NULL;
63 const char *ptr;
64 char *ptr1 = NULL;
65 int count = 0;
67 *name_type = -1;
69 if (!fgets_slash(line,sizeof(line),fp)) {
70 continue;
73 if (*line == '#') {
74 continue;
77 ptr = line;
79 if (next_token_talloc(ctx, &ptr, &ip, NULL))
80 ++count;
81 if (next_token_talloc(ctx, &ptr, &name, NULL))
82 ++count;
83 if (next_token_talloc(ctx, &ptr, &flags, NULL))
84 ++count;
85 if (next_token_talloc(ctx, &ptr, &extra, NULL))
86 ++count;
88 if (count <= 0)
89 continue;
91 if (count > 0 && count < 2) {
92 DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",
93 line));
94 continue;
97 if (count >= 4) {
98 DEBUG(0,("getlmhostsent: too many columns "
99 "in lmhosts file (obsolete syntax)\n"));
100 continue;
103 if (!flags) {
104 flags = talloc_strdup(ctx, "");
105 if (!flags) {
106 continue;
110 DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n",
111 ip, name, flags));
113 if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
114 DEBUG(0,("getlmhostsent: group flag "
115 "in lmhosts ignored (obsolete)\n"));
116 continue;
119 if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) {
120 DEBUG(0,("getlmhostsent: invalid address "
121 "%s.\n", ip));
124 /* Extra feature. If the name ends in '#XX',
125 * where XX is a hex number, then only add that name type. */
126 if((ptr1 = strchr_m(name, '#')) != NULL) {
127 char *endptr;
128 ptr1++;
130 *name_type = (int)strtol(ptr1, &endptr, 16);
131 if(!*ptr1 || (endptr == ptr1)) {
132 DEBUG(0,("getlmhostsent: invalid name "
133 "%s containing '#'.\n", name));
134 continue;
137 *(--ptr1) = '\0'; /* Truncate at the '#' */
140 *pp_name = talloc_strdup(ctx, name);
141 if (!*pp_name) {
142 return false;
144 return true;
147 return false;
150 /********************************************************
151 Finish parsing the lmhosts file.
152 *********************************************************/
154 void endlmhosts(XFILE *fp)
156 x_fclose(fp);
159 /********************************************************
160 Resolve via "lmhosts" method.
161 *********************************************************/
163 NTSTATUS resolve_lmhosts_file_as_sockaddr(const char *lmhosts_file,
164 const char *name, int name_type,
165 TALLOC_CTX *mem_ctx,
166 struct sockaddr_storage **return_iplist,
167 int *return_count)
170 * "lmhosts" means parse the local lmhosts file.
173 XFILE *fp;
174 char *lmhost_name = NULL;
175 int name_type2;
176 struct sockaddr_storage return_ss;
177 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
178 TALLOC_CTX *ctx = NULL;
180 *return_iplist = NULL;
181 *return_count = 0;
183 DEBUG(3,("resolve_lmhosts: "
184 "Attempting lmhosts lookup for name %s<0x%x>\n",
185 name, name_type));
187 fp = startlmhosts(lmhosts_file);
189 if ( fp == NULL )
190 return NT_STATUS_NO_SUCH_FILE;
192 ctx = talloc_new(mem_ctx);
193 if (!ctx) {
194 endlmhosts(fp);
195 return NT_STATUS_NO_MEMORY;
198 while (getlmhostsent(ctx, fp, &lmhost_name, &name_type2, &return_ss)) {
200 if (!strequal(name, lmhost_name)) {
201 TALLOC_FREE(lmhost_name);
202 continue;
205 if ((name_type2 != -1) && (name_type != name_type2)) {
206 TALLOC_FREE(lmhost_name);
207 continue;
210 *return_iplist = talloc_realloc(ctx, (*return_iplist),
211 struct sockaddr_storage,
212 (*return_count)+1);
214 if ((*return_iplist) == NULL) {
215 TALLOC_FREE(ctx);
216 endlmhosts(fp);
217 DEBUG(3,("resolve_lmhosts: talloc_realloc fail !\n"));
218 return NT_STATUS_NO_MEMORY;
221 (*return_iplist)[*return_count] = return_ss;
222 *return_count += 1;
224 /* we found something */
225 status = NT_STATUS_OK;
227 /* Multiple names only for DC lookup */
228 if (name_type != 0x1c)
229 break;
232 talloc_steal(mem_ctx, *return_iplist);
233 TALLOC_FREE(ctx);
234 endlmhosts(fp);
235 return status;