libcli:nbt make the lmhosts parsing code and dependicies common
[samba.git] / libcli / nbt / lmhosts.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    manipulate nbt name structures
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 #include "includes.h"
25 #include "lib/util/xfile.h"
26 #include "system/filesys.h"
27 #include "system/network.h"
28
29 /********************************************************
30  Start parsing the lmhosts file.
31 *********************************************************/
32
33 XFILE *startlmhosts(const char *fname)
34 {
35         XFILE *fp = x_fopen(fname,O_RDONLY, 0);
36         if (!fp) {
37                 DEBUG(4,("startlmhosts: Can't open lmhosts file %s. "
38                         "Error was %s\n",
39                         fname, strerror(errno)));
40                 return NULL;
41         }
42         return fp;
43 }
44
45 /********************************************************
46  Parse the next line in the lmhosts file.
47 *********************************************************/
48
49 bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type,
50                 struct sockaddr_storage *pss)
51 {
52         char line[1024];
53
54         *pp_name = NULL;
55
56         while(!x_feof(fp) && !x_ferror(fp)) {
57                 char *ip = NULL;
58                 char *flags = NULL;
59                 char *extra = NULL;
60                 char *name = NULL;
61                 const char *ptr;
62                 char *ptr1 = NULL;
63                 int count = 0;
64
65                 *name_type = -1;
66
67                 if (!fgets_slash(line,sizeof(line),fp)) {
68                         continue;
69                 }
70
71                 if (*line == '#') {
72                         continue;
73                 }
74
75                 ptr = line;
76
77                 if (next_token_talloc(ctx, &ptr, &ip, NULL))
78                         ++count;
79                 if (next_token_talloc(ctx, &ptr, &name, NULL))
80                         ++count;
81                 if (next_token_talloc(ctx, &ptr, &flags, NULL))
82                         ++count;
83                 if (next_token_talloc(ctx, &ptr, &extra, NULL))
84                         ++count;
85
86                 if (count <= 0)
87                         continue;
88
89                 if (count > 0 && count < 2) {
90                         DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",
91                                                 line));
92                         continue;
93                 }
94
95                 if (count >= 4) {
96                         DEBUG(0,("getlmhostsent: too many columns "
97                                 "in lmhosts file (obsolete syntax)\n"));
98                         continue;
99                 }
100
101                 if (!flags) {
102                         flags = talloc_strdup(ctx, "");
103                         if (!flags) {
104                                 continue;
105                         }
106                 }
107
108                 DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n",
109                                         ip, name, flags));
110
111                 if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
112                         DEBUG(0,("getlmhostsent: group flag "
113                                 "in lmhosts ignored (obsolete)\n"));
114                         continue;
115                 }
116
117                 if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) {
118                         DEBUG(0,("getlmhostsent: invalid address "
119                                 "%s.\n", ip));
120                 }
121
122                 /* Extra feature. If the name ends in '#XX',
123                  * where XX is a hex number, then only add that name type. */
124                 if((ptr1 = strchr_m(name, '#')) != NULL) {
125                         char *endptr;
126                         ptr1++;
127
128                         *name_type = (int)strtol(ptr1, &endptr, 16);
129                         if(!*ptr1 || (endptr == ptr1)) {
130                                 DEBUG(0,("getlmhostsent: invalid name "
131                                         "%s containing '#'.\n", name));
132                                 continue;
133                         }
134
135                         *(--ptr1) = '\0'; /* Truncate at the '#' */
136                 }
137
138                 *pp_name = talloc_strdup(ctx, name);
139                 if (!*pp_name) {
140                         return false;
141                 }
142                 return true;
143         }
144
145         return false;
146 }
147
148 /********************************************************
149  Finish parsing the lmhosts file.
150 *********************************************************/
151
152 void endlmhosts(XFILE *fp)
153 {
154         x_fclose(fp);
155 }
156