summaryrefslogtreecommitdiff
path: root/source4/lib/samba3/winsdb.c
blob: 0a93dbb0e18c6a9c5d4c709ae465b5ef06438155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* 
   Unix SMB/CIFS implementation.
   Wins Database

   Copyright (C) Jeremy Allison 1994-2003
   Copyright (C) Jelmer Vernooij 2005

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
*/

#include "includes.h"
#include "system/filesys.h"
#include "lib/samba3/samba3.h"

#define WINS_VERSION 1

NTSTATUS samba3_read_winsdb( const char *fn, TALLOC_CTX *ctx, struct samba3_winsdb_entry **entries, uint32_t *count )
{
	XFILE *fp;
	char *line;

	if((fp = x_fopen(fn,O_RDONLY,0)) == NULL) {
		DEBUG(0,("initialise_wins: Can't open wins database file %s. Error was %s\n",
			fn, strerror(errno) ));
		return NT_STATUS_OPEN_FAILED;
	}

	*count = 0;
	*entries = NULL;

	while (!x_feof(fp)) {
		struct samba3_winsdb_entry entry;
		const char *name_str, *ttl_str, *nb_flags_str;
		const char **args;
		char *p;
		int i;
		unsigned int hash;
		int version;

		/* Read a line from the wins.dat file. Strips whitespace
			from the beginning and end of the line.  */
		line = fgets_slash(NULL,8,fp);
		if (!line) {
			return NT_STATUS_UNEXPECTED_IO_ERROR;
		}
      
		if (*line == '#') {
			SAFE_FREE(line);
			continue;
		}

		if (strncmp(line,"VERSION ", 8) == 0) {
			if (sscanf(line,"VERSION %d %u", &version, &hash) != 2 ||
						version != WINS_VERSION) {
				DEBUG(0,("Discarding invalid wins.dat file [%s]\n",line));
				SAFE_FREE(line);
				x_fclose(fp);
				return NT_STATUS_REVISION_MISMATCH;
			}
			SAFE_FREE(line);

			continue;
		}

		args = str_list_make_shell(ctx, line, NULL);

		/* 
		 * Now we handle multiple IP addresses per name we need
		 * to iterate over the line twice. The first time to
		 * determine how many IP addresses there are, the second
		 * time to actually parse them into the ip_list array.
		 */

		name_str = args[0];
		if (!name_str) {
			DEBUG(0,("initialise_wins: Failed to parse name when parsing line %s\n", line ));
			SAFE_FREE(line);
			continue;
		}

		ttl_str = args[1];
		if (!ttl_str) {
			DEBUG(0,("initialise_wins: Failed to parse time to live when parsing line %s\n", line ));
			SAFE_FREE(line);
			continue;
		}

		/*
		 * Determine the number of IP addresses per line.
		 */
		entry.ip_count = 0;
		for (i = 2; args[i] && strchr(args[i], '.'); i++) entry.ip_count++;

		if(entry.ip_count == 0) {
			DEBUG(0,("initialise_wins: Missing IP address when parsing line %s\n", line ));
			SAFE_FREE(line);
			continue;
		}

		/* Allocate the space for the ip_list. */
		if((entry.ips = talloc_array ( ctx, struct ipv4_addr, entry.ip_count)) == NULL) {
			DEBUG(0,("initialise_wins: Malloc fail !\n"));
			SAFE_FREE(line);
			return NT_STATUS_NO_MEMORY;
		}
 
		/* Reset and re-parse the line. */
		for(i = 0; i < entry.ip_count; i++) {
			entry.ips[i] = interpret_addr2(args[i+2]);
		}
		nb_flags_str = args[2 + entry.ip_count];

		SMB_ASSERT(nb_flags_str);

		/* 
		 * Deal with SELF or REGISTER name encoding. Default is REGISTER
		 * for compatibility with old nmbds.
		 */

		if(nb_flags_str[strlen(nb_flags_str)-1] == 'S') {
			DEBUG(5,("initialise_wins: Ignoring SELF name %s\n", line));
			talloc_free(entry.ips);
			SAFE_FREE(line);
			continue;
		}
      
		/* Netbios name. # divides the name from the type (hex): netbios#xx */
		entry.name = talloc_strdup(ctx, name_str);
      
		if((p = strchr(entry.name,'#')) != NULL) {
			*p = 0;
			sscanf(p+1,"%x",&entry.type);
		}
      
		/* Decode the netbios flags (hex) and the time-to-live (in seconds). */
		sscanf(nb_flags_str,"%x",&entry.nb_flags);
		entry.ttl = atol(ttl_str);

		*entries = talloc_realloc(ctx, *entries, struct samba3_winsdb_entry, (*count)+1);
		(*entries)[*count] = entry;

		(*count)++;
	} 
    
	x_fclose(fp);
	return NT_STATUS_OK;
}