r5251: - renamed the nbtd server side structures to have a nbtd_ prefix, to
[garming/samba-autobuild/.git] / source4 / 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   send a name defense reply
30 */
31 static void nbtd_name_defense_reply(struct nbt_name_socket *nbtsock, 
32                                     struct nbt_name_packet *request_packet, 
33                                     const char *src_address, int src_port,
34                                     struct nbt_name *name, uint32_t ttl,
35                                     uint16_t nb_flags, const char *address)
36 {
37         struct nbt_name_packet *packet;
38
39         packet = talloc_zero(nbtsock, struct nbt_name_packet);
40         if (packet == NULL) return;
41
42         packet->name_trn_id = request_packet->name_trn_id;
43         packet->ancount = 1;
44         packet->operation = 
45                 NBT_FLAG_REPLY | 
46                 NBT_OPCODE_REGISTER |
47                 NBT_FLAG_AUTHORITIVE |
48                 NBT_FLAG_RECURSION_DESIRED |
49                 NBT_FLAG_RECURSION_AVAIL |
50                 NBT_RCODE_ACT;
51         
52         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
53         if (packet->answers == NULL) goto failed;
54
55         packet->answers[0].name     = *name;
56         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
57         packet->answers[0].rr_class = NBT_QCLASS_IP;
58         packet->answers[0].ttl      = ttl;
59         packet->answers[0].rdata.netbios.length = 6;
60         packet->answers[0].rdata.netbios.addresses = 
61                 talloc_array(packet->answers,
62                              struct nbt_rdata_address, 1);
63         if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
64         packet->answers[0].rdata.netbios.addresses[0].nb_flags = nb_flags;
65         packet->answers[0].rdata.netbios.addresses[0].ipaddr = 
66                 talloc_strdup(packet->answers, address);
67         if (packet->answers[0].rdata.netbios.addresses[0].ipaddr == NULL) goto failed;
68
69         DEBUG(7,("Sending name defense reply for %s<%02x> at %s to %s:%d\n", 
70                  name->name, name->type, src_address, address, src_port));
71         
72         nbt_name_reply_send(nbtsock, src_address, src_port, packet);
73
74 failed:
75         talloc_free(packet);
76 }
77
78
79 /*
80   defend our registered names against registration or name refresh
81   requests
82 */
83 void nbtd_request_defense(struct nbt_name_socket *nbtsock, 
84                           struct nbt_name_packet *packet, 
85                           const char *src_address, int src_port)
86 {
87         struct nbtd_iface_name *iname;
88         struct nbt_name *name;
89         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
90                                                        struct nbtd_interface);
91
92         NBT_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
93         NBT_ASSERT_PACKET(packet, src_address, packet->arcount == 1);
94         NBT_ASSERT_PACKET(packet, src_address, 
95                           packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
96         NBT_ASSERT_PACKET(packet, src_address, 
97                           packet->questions[0].question_class == NBT_QCLASS_IP);
98
99         /* see if we have the requested name on this interface */
100         name = &packet->questions[0].name;
101
102         iname = nbtd_find_iname(iface, name, NBT_NM_ACTIVE);
103         if (iname != NULL && !(iname->nb_flags & NBT_NM_GROUP)) {
104                 DEBUG(2,("Defending name %s<%02x> on %s against %s\n",
105                          name->name, name->type, iface->bcast_address, src_address));
106                 nbtd_name_defense_reply(nbtsock, packet, src_address, src_port,
107                                         &iname->name, iname->ttl, iname->nb_flags, 
108                                         iface->ip_address);
109         }
110 }