r5346: - a bit more preparation for the WINS server going in
[jelmer/samba4-debian.git] / source / nbt_server / defense.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    defend our names against name registration requests
5
6    Copyright (C) Andrew Tridgell        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 #include "includes.h"
24 #include "dlinklist.h"
25 #include "system/network.h"
26 #include "nbt_server/nbt_server.h"
27
28
29 /*
30   defend our registered names against registration or name refresh
31   requests
32 */
33 void nbtd_request_defense(struct nbt_name_socket *nbtsock, 
34                           struct nbt_name_packet *packet, 
35                           const char *src_address, int src_port)
36 {
37         struct nbtd_iface_name *iname;
38         struct nbt_name *name;
39         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
40                                                        struct nbtd_interface);
41
42         NBTD_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
43         NBTD_ASSERT_PACKET(packet, src_address, packet->arcount == 1);
44         NBTD_ASSERT_PACKET(packet, src_address, 
45                            packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
46         NBTD_ASSERT_PACKET(packet, src_address, 
47                            packet->questions[0].question_class == NBT_QCLASS_IP);
48         NBTD_ASSERT_PACKET(packet, src_address, 
49                           packet->additional[0].rr_type == NBT_QTYPE_NETBIOS);
50         NBTD_ASSERT_PACKET(packet, src_address, 
51                           packet->additional[0].rr_class == NBT_QCLASS_IP);
52         NBTD_ASSERT_PACKET(packet, src_address, 
53                           packet->additional[0].rdata.netbios.length == 6);
54
55         /* see if we have the requested name on this interface */
56         name = &packet->questions[0].name;
57
58         iname = nbtd_find_iname(iface, name, NBT_NM_ACTIVE);
59         if (iname != NULL && !(iname->nb_flags & NBT_NM_GROUP)) {
60                 DEBUG(2,("Defending name %s<%02x> on %s against %s\n",
61                          name->name, name->type, iface->bcast_address, src_address));
62                 nbtd_negative_name_registration_reply(nbtsock, packet, 
63                                                       src_address, src_port);
64         } else {
65                 nbtd_winsserver_request(nbtsock, packet, src_address, src_port);
66         }
67 }