nmbd_elections.c: Removed force elections code to bring into line with 1.9.18.
[samba.git] / source / nmbd / nmbd_subnetdb.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7    Copyright (C) Jeremy Allison 1994-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23    Revision History:
24
25 */
26
27 #include "includes.h"
28 #include "smb.h"
29
30 extern int ClientNMB;
31 extern int ClientDGRAM;
32 extern int global_nmb_port;
33
34 extern int DEBUGLEVEL;
35
36 extern fstring myworkgroup;
37 extern char **my_netbios_names;
38 extern struct in_addr ipzero;
39
40 /* This is the broadcast subnets database. */
41 struct subnet_record *subnetlist = NULL;
42
43 /* Extra subnets - keep these separate so enumeration code doesn't
44    run onto it by mistake. */
45
46 struct subnet_record *unicast_subnet = NULL;
47 struct subnet_record *remote_broadcast_subnet = NULL;
48 struct subnet_record *wins_server_subnet = NULL;
49
50 extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
51
52 /****************************************************************************
53   Add a subnet into the list.
54   **************************************************************************/
55
56 static void add_subnet(struct subnet_record *subrec)
57 {
58   struct subnet_record *subrec2;
59
60   if (!subnetlist)
61   {
62     subnetlist = subrec;
63     subrec->prev = NULL;
64     subrec->next = NULL;
65     return;
66   }
67
68   for (subrec2 = subnetlist; subrec2->next; subrec2 = subrec2->next)
69     ;
70
71   subrec2->next = subrec;
72   subrec->next = NULL;
73   subrec->prev = subrec2;
74 }
75
76 /* CRH!!! */
77 #if 0
78 /* ************************************************************************** ** * This will go away when we move to a "real" database back-end.
79   Note that we cannot use memcmp here as we have no control
80   over how the struct nmb_name structures are packed in memory. JRA.
81  * ************************************************************************** ** */
82 int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
83   {
84   struct name_record *NR = (struct name_record *)Node;
85   struct nmb_name *nmbname = (struct nmb_name *)Item;
86   int ret;
87
88   if(nmb_name_equal( &NR->name, nmbname))
89     return 0;
90
91   ret = strcmp( nmbname->name, NR->name.name);
92   if(ret)
93     return ret;
94
95   ret = strcmp( nmbname->scope, NR->name.scope);
96   if(ret)
97     return ret;
98
99   return nmbname->name_type - NR->name.name_type;
100   } /* namelist_entry_compare */
101 #else
102 /* ************************************************************************** **
103  * This will go away when we move to a "real" database back-end.
104  * ************************************************************************** **
105  */
106 int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
107   {
108   struct name_record *NR = (struct name_record *)Node;
109
110   struct nmb_name *Iname = (struct nmb_name *)Item;
111   DEBUG(10, ("namelist_entry_compare: %d == memcmp( \"%s\", \"%s\", %d )\n",
112           memcmp( Item, &(NR->name), sizeof(struct nmb_name) ),
113           namestr(Iname), namestr(&NR->name), sizeof(struct nmb_name)) );
114
115   return( memcmp( Item, &(NR->name), sizeof(struct nmb_name) ) ); 
116   } /* namelist_entry_compare */
117
118 #endif
119 /* CRH!!! */   
120
121 /****************************************************************************
122   Create a subnet entry.
123   ****************************************************************************/
124
125 static struct subnet_record *make_subnet(char *name, enum subnet_type type,
126                                          struct in_addr myip, struct in_addr bcast_ip, 
127                                          struct in_addr mask_ip)
128 {
129   struct subnet_record *subrec = NULL;
130   int nmb_sock, dgram_sock;
131
132   /* Check if we are creating a non broadcast subnet - if so don't create
133      sockets.
134    */
135
136   if(type != NORMAL_SUBNET)
137   {
138     nmb_sock = -1;
139     dgram_sock = -1;
140   }
141   else
142   {
143     /*
144      * Attempt to open the sockets on port 137/138 for this interface
145      * and bind them.
146      * Fail the subnet creation if this fails.
147      */
148
149     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
150     {
151       DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
152 for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
153       return NULL;
154     }
155
156     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
157     {
158       DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
159 for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
160       return NULL;
161     }
162
163     /* Make sure we can broadcast from these sockets. */
164     set_socket_options(nmb_sock,"SO_BROADCAST");
165     set_socket_options(dgram_sock,"SO_BROADCAST");
166
167   }
168
169   subrec = (struct subnet_record *)malloc(sizeof(*subrec));
170   
171   if (!subrec) 
172   {
173     DEBUG(0,("make_subnet: malloc fail !\n"));
174     close(nmb_sock);
175     close(dgram_sock);
176     return(NULL);
177   }
178   
179   bzero( (char *)subrec, sizeof(*subrec) );
180   (void)ubi_trInitTree( subrec->namelist,
181                         namelist_entry_compare,
182                         ubi_trOVERWRITE );
183
184   if((subrec->subnet_name = strdup(name)) == NULL)
185   {
186     DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
187     close(nmb_sock);
188     close(dgram_sock);
189     free((char *)subrec);
190     return(NULL);
191   }
192
193   DEBUG(2, ("making subnet name:%s ", name ));
194   DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
195   DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
196  
197   subrec->namelist_changed = False;
198   subrec->work_changed = False;
199  
200   subrec->bcast_ip = bcast_ip;
201   subrec->mask_ip  = mask_ip;
202   subrec->myip = myip;
203   subrec->type = type;
204   subrec->nmb_sock = nmb_sock;
205   subrec->dgram_sock = dgram_sock;
206   
207   return subrec;
208 }
209
210 /****************************************************************************
211   Create subnet entries.
212 **************************************************************************/
213
214 BOOL create_subnets(void)
215 {    
216   int num_interfaces = iface_count();
217   int i;
218   struct in_addr unicast_ip;
219
220   if(num_interfaces == 0)
221   {
222     DEBUG(0,("create_subnets: No local interfaces !\n"));
223     return False;
224   }
225
226   /* 
227    * Create subnets from all the local interfaces and thread them onto
228    * the linked list. 
229    */
230
231   for (i = 0 ; i < num_interfaces; i++)
232   {
233     struct subnet_record *subrec;
234     struct interface *iface = get_interface(i);
235
236     if((subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
237                  iface->ip, iface->bcast,iface->nmask)) == NULL)
238       return False;
239     add_subnet(subrec);
240   }
241
242   /* 
243    * If we have been configured to use a WINS server, then try and
244    * get the ip address of it here. If we are the WINS server then
245    * set the unicast subnet address to be the first of our own real
246    * addresses.
247    */
248
249   if(*lp_wins_server())
250   {
251     struct in_addr real_wins_ip;
252     real_wins_ip = *interpret_addr2(lp_wins_server());
253
254     if (!zero_ip(real_wins_ip))
255     {
256       unicast_ip = real_wins_ip;
257     }
258     else
259     {
260       /* The smb.conf's wins server parameter MUST be a host_name
261          or an ip_address. */
262       DEBUG(0,("invalid smb.conf parameter 'wins server'\n"));
263       return False;
264     }
265   } 
266   else if(lp_we_are_a_wins_server())
267   {
268     /* Pick the first interface ip address as the WINS server ip. */
269     unicast_ip = *iface_n_ip(0);
270   }
271   else
272   {
273     /* We should not be using a WINS server at all. Set the
274       ip address of the subnet to be zero. */
275     unicast_ip = ipzero;
276   }
277
278   /*
279    * Create the unicast and remote broadcast subnets.
280    * Don't put these onto the linked list.
281    * The ip address of the unicast subnet is set to be
282    * the WINS server address, if it exists, or ipzero if not.
283    */
284
285   unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
286                                  unicast_ip, unicast_ip, unicast_ip);
287
288   remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
289                                          REMOTE_BROADCAST_SUBNET,
290                                          ipzero, ipzero, ipzero);
291
292   if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
293     return False;
294
295   /* 
296    * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
297    * the linked list.
298    */
299
300   if (lp_we_are_a_wins_server())
301   {
302     if((wins_server_subnet = make_subnet("WINS_SERVER_SUBNET",
303                                        WINS_SERVER_SUBNET, 
304                                        ipzero, ipzero, ipzero)) == NULL)
305       return False;
306   }
307
308   return True;
309 }
310
311 /*******************************************************************
312 Function to tell us if we can use the unicast subnet.
313 ******************************************************************/
314
315 BOOL we_are_a_wins_client(void)
316 {
317   static int cache_we_are_a_wins_client = -1;
318
319   if(cache_we_are_a_wins_client == -1)
320     cache_we_are_a_wins_client = (ip_equal(ipzero, unicast_subnet->myip) ? 
321                                   False : True);
322
323   return cache_we_are_a_wins_client;
324 }
325
326 /*******************************************************************
327 Access function used by NEXT_SUBNET_INCLUDING_UNICAST
328 ******************************************************************/
329
330 struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec)
331 {
332   if(subrec == unicast_subnet)
333     return NULL;
334   else if((subrec->next == NULL) && we_are_a_wins_client())
335     return unicast_subnet;
336   else
337     return subrec->next;
338 }
339
340 /*******************************************************************
341  Access function used by retransmit_or_expire_response_records() in
342  nmbd_packets.c. Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>
343  Needed when we need to enumerate all the broadcast, unicast and
344  WINS subnets.
345 ******************************************************************/
346
347 struct subnet_record *get_next_subnet_maybe_unicast_or_wins_server(struct subnet_record *subrec)
348 {
349   if(subrec == unicast_subnet)
350   {
351     if(wins_server_subnet)
352       return wins_server_subnet;
353     else
354       return NULL;
355   }
356
357   if(wins_server_subnet && subrec == wins_server_subnet)
358     return NULL;
359
360   if((subrec->next == NULL) && we_are_a_wins_client())
361     return unicast_subnet;
362   else
363     return subrec->next;
364 }