Until the new ldb changes land, make ldb_wait set the error string.
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
20    
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "lib/samba3/samba3.h"
26 #include "system/network.h"
27
28 #define WINS_VERSION 1
29
30 NTSTATUS samba3_read_winsdb( const char *fn, TALLOC_CTX *ctx, struct samba3_winsdb_entry **entries, uint32_t *count )
31 {
32         XFILE *fp;
33         char *line;
34
35         if((fp = x_fopen(fn,O_RDONLY,0)) == NULL) {
36                 DEBUG(0,("initialise_wins: Can't open wins database file %s. Error was %s\n",
37                         fn, strerror(errno) ));
38                 return NT_STATUS_OPEN_FAILED;
39         }
40
41         *count = 0;
42         *entries = NULL;
43
44         while (!x_feof(fp)) {
45                 struct samba3_winsdb_entry entry;
46                 const char *name_str, *ttl_str, *nb_flags_str;
47                 const char **args;
48                 char *p;
49                 int i;
50                 unsigned int hash;
51                 int version;
52
53                 /* Read a line from the wins.dat file. Strips whitespace
54                         from the beginning and end of the line.  */
55                 line = fgets_slash(NULL,8,fp);
56                 if (!line) {
57                         return NT_STATUS_UNEXPECTED_IO_ERROR;
58                 }
59       
60                 if (*line == '#') {
61                         SAFE_FREE(line);
62                         continue;
63                 }
64
65                 if (strncmp(line,"VERSION ", 8) == 0) {
66                         if (sscanf(line,"VERSION %d %u", &version, &hash) != 2 ||
67                                                 version != WINS_VERSION) {
68                                 DEBUG(0,("Discarding invalid wins.dat file [%s]\n",line));
69                                 SAFE_FREE(line);
70                                 x_fclose(fp);
71                                 return NT_STATUS_REVISION_MISMATCH;
72                         }
73                         SAFE_FREE(line);
74
75                         continue;
76                 }
77
78                 args = str_list_make_shell(ctx, line, NULL);
79
80                 /* 
81                  * Now we handle multiple IP addresses per name we need
82                  * to iterate over the line twice. The first time to
83                  * determine how many IP addresses there are, the second
84                  * time to actually parse them into the ip_list array.
85                  */
86
87                 name_str = args[0];
88                 if (!name_str) {
89                         DEBUG(0,("initialise_wins: Failed to parse name when parsing line %s\n", line ));
90                         SAFE_FREE(line);
91                         continue;
92                 }
93
94                 ttl_str = args[1];
95                 if (!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                 for (i = 2; args[i] && strchr(args[i], '.'); i++) entry.ip_count++;
106
107                 if(entry.ip_count == 0) {
108                         DEBUG(0,("initialise_wins: Missing IP address when parsing line %s\n", line ));
109                         SAFE_FREE(line);
110                         continue;
111                 }
112
113                 /* Allocate the space for the ip_list. */
114                 if((entry.ips = talloc_array ( ctx, struct in_addr, entry.ip_count)) == NULL) {
115                         DEBUG(0,("initialise_wins: Malloc fail !\n"));
116                         SAFE_FREE(line);
117                         return NT_STATUS_NO_MEMORY;
118                 }
119  
120                 /* Reset and re-parse the line. */
121                 for(i = 0; i < entry.ip_count; i++) {
122                         entry.ips[i] = interpret_addr2(args[i+2]);
123                 }
124                 nb_flags_str = args[2 + entry.ip_count];
125
126                 SMB_ASSERT(nb_flags_str);
127
128                 /* 
129                  * Deal with SELF or REGISTER name encoding. Default is REGISTER
130                  * for compatibility with old nmbds.
131                  */
132
133                 if(nb_flags_str[strlen(nb_flags_str)-1] == 'S') {
134                         DEBUG(5,("initialise_wins: Ignoring SELF name %s\n", line));
135                         talloc_free(entry.ips);
136                         SAFE_FREE(line);
137                         continue;
138                 }
139       
140                 /* Netbios name. # divides the name from the type (hex): netbios#xx */
141                 entry.name = talloc_strdup(ctx, name_str);
142       
143                 if((p = strchr(entry.name,'#')) != NULL) {
144                         *p = 0;
145                         sscanf(p+1,"%x",&entry.type);
146                 }
147       
148                 /* Decode the netbios flags (hex) and the time-to-live (in seconds). */
149                 sscanf(nb_flags_str,"%x",&entry.nb_flags);
150                 entry.ttl = atol(ttl_str);
151
152                 *entries = talloc_realloc(ctx, *entries, struct samba3_winsdb_entry, (*count)+1);
153                 (*entries)[*count] = entry;
154
155                 (*count)++;
156         } 
157     
158         x_fclose(fp);
159         return NT_STATUS_OK;
160 }