r9621: Bunch of bug fixes. Add 'format' option to samba3dump (text,summary,ldif)
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / winsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Wins Database
4
5    Copyright (C) Jeremy Allison 1994-2003
6    Copyright (C) Jelmer Vernooij 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "pstring.h"
27 #include "lib/samba3/samba3.h"
28
29 #define WINS_VERSION 1
30
31 NTSTATUS samba3_read_winsdb( const char *fn, TALLOC_CTX *ctx, struct samba3_winsdb_entry **entries, uint32_t *count )
32 {
33         XFILE *fp;
34         char *line;
35
36         if((fp = x_fopen(fn,O_RDONLY,0)) == NULL) {
37                 DEBUG(0,("initialise_wins: Can't open wins database file %s. Error was %s\n",
38                         fn, strerror(errno) ));
39                 return NT_STATUS_UNSUCCESSFUL;
40         }
41
42         *count = 0;
43         *entries = NULL;
44
45         while (!x_feof(fp)) {
46                 struct samba3_winsdb_entry entry;
47                 pstring name_str, ip_str, ttl_str, nb_flags_str;
48                 const char *ptr;
49                 char *p;
50                 BOOL got_token;
51                 BOOL was_ip;
52                 int i;
53                 unsigned int hash;
54                 int version;
55
56                 /* Read a line from the wins.dat file. Strips whitespace
57                         from the beginning and end of the line.  */
58                 line = fgets_slash(NULL,-1,fp);
59                 if (!line) 
60                         return NT_STATUS_UNSUCCESSFUL;
61       
62                 if (*line == '#') {
63                         SAFE_FREE(line);
64                         continue;
65                 }
66
67                 if (strncmp(line,"VERSION ", 8) == 0) {
68                         if (sscanf(line,"VERSION %d %u", &version, &hash) != 2 ||
69                                                 version != WINS_VERSION) {
70                                 DEBUG(0,("Discarding invalid wins.dat file [%s]\n",line));
71                                 SAFE_FREE(line);
72                                 x_fclose(fp);
73                                 return NT_STATUS_UNSUCCESSFUL;
74                         }
75                         SAFE_FREE(line);
76
77                         continue;
78                 }
79
80                 ptr = line;
81
82                 /* 
83                  * Now we handle multiple IP addresses per name we need
84                  * to iterate over the line twice. The first time to
85                  * determine how many IP addresses there are, the second
86                  * time to actually parse them into the ip_list array.
87                  */
88
89                 if (!next_token(&ptr,name_str,NULL,sizeof(name_str))) {
90                         DEBUG(0,("initialise_wins: Failed to parse name when parsing line %s\n", line ));
91                         SAFE_FREE(line);
92                         continue;
93                 }
94
95                 if (!next_token(&ptr,ttl_str,NULL,sizeof(ttl_str))) {
96                         DEBUG(0,("initialise_wins: Failed to parse time to live when parsing line %s\n", line ));
97                         SAFE_FREE(line);
98                         continue;
99                 }
100
101                 /*
102                  * Determine the number of IP addresses per line.
103                  */
104                 entry.ip_count = 0;
105                 do {
106                         got_token = next_token(&ptr,ip_str,NULL,sizeof(ip_str));
107                         was_ip = False;
108
109                         if(got_token && strchr(ip_str, '.')) {
110                                 entry.ip_count++;
111                                 was_ip = True;
112                         }
113                 } while( got_token && was_ip);
114
115                 if(entry.ip_count == 0) {
116                         DEBUG(0,("initialise_wins: Missing IP address when parsing line %s\n", line ));
117                         SAFE_FREE(line);
118                         continue;
119                 }
120
121                 if(!got_token) {
122                         DEBUG(0,("initialise_wins: Missing nb_flags when parsing line %s\n", line ));
123                         SAFE_FREE(line);
124                         continue;
125                 }
126
127                 /* Allocate the space for the ip_list. */
128                 if((entry.ips = talloc_array ( ctx, struct ipv4_addr, entry.ip_count)) == NULL) {
129                         DEBUG(0,("initialise_wins: Malloc fail !\n"));
130                         SAFE_FREE(line);
131                         return NT_STATUS_NO_MEMORY;
132                 }
133  
134                 /* Reset and re-parse the line. */
135                 ptr = line;
136                 next_token(&ptr,name_str,NULL,sizeof(name_str)); 
137                 next_token(&ptr,ttl_str,NULL,sizeof(ttl_str));
138                 for(i = 0; i < entry.ip_count; i++) {
139                         next_token(&ptr, ip_str, NULL, sizeof(ip_str));
140                         entry.ips[i] = interpret_addr2(ip_str);
141                 }
142                 next_token(&ptr,nb_flags_str,NULL, sizeof(nb_flags_str));
143
144                 /* 
145                  * Deal with SELF or REGISTER name encoding. Default is REGISTER
146                  * for compatibility with old nmbds.
147                  */
148
149                 if(nb_flags_str[strlen(nb_flags_str)-1] == 'S') {
150                         DEBUG(5,("initialise_wins: Ignoring SELF name %s\n", line));
151                         talloc_free(entry.ips);
152                         SAFE_FREE(line);
153                         continue;
154                 }
155       
156                 if(nb_flags_str[strlen(nb_flags_str)-1] == 'R')
157                         nb_flags_str[strlen(nb_flags_str)-1] = '\0';
158       
159                 /* Netbios name. # divides the name from the type (hex): netbios#xx */
160                 entry.name = talloc_strdup(ctx, name_str);
161       
162                 if((p = strchr(entry.name,'#')) != NULL) {
163                         *p = 0;
164                         sscanf(p+1,"%x",&entry.type);
165                 }
166       
167                 /* Decode the netbios flags (hex) and the time-to-live (in seconds). */
168                 sscanf(nb_flags_str,"%x",&entry.nb_flags);
169                 entry.ttl = atol(ttl_str);
170
171                 *entries = talloc_realloc(ctx, *entries, struct samba3_winsdb_entry, (*count)+1);
172                 *entries[*count] = entry;
173
174                 (*count)++;
175         } 
176     
177         x_fclose(fp);
178         return NT_STATUS_OK;
179 }