r5259: make sure we give the ip of the interface that a name query comes in
[jelmer/samba4-debian.git] / source / nbt_server / query.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    answer name queries
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 query reply
30 */
31 static void nbtd_name_query_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 **addresses)
36 {
37         struct nbt_name_packet *packet;
38         size_t num_addresses = str_list_length(addresses);
39         int i;
40
41         if (num_addresses == 0) {
42                 DEBUG(3,("No addresses in name query reply - failing\n"));
43                 return;
44         }
45
46         packet = talloc_zero(nbtsock, struct nbt_name_packet);
47         if (packet == NULL) return;
48
49         packet->name_trn_id = request_packet->name_trn_id;
50         packet->ancount = 1;
51         packet->operation = 
52                 NBT_FLAG_REPLY | 
53                 NBT_OPCODE_QUERY | 
54                 NBT_FLAG_AUTHORITIVE |
55                 NBT_FLAG_RECURSION_DESIRED |
56                 NBT_FLAG_RECURSION_AVAIL;
57
58         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
59         if (packet->answers == NULL) goto failed;
60
61         packet->answers[0].name     = *name;
62         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
63         packet->answers[0].rr_class = NBT_QCLASS_IP;
64         packet->answers[0].ttl      = ttl;
65         packet->answers[0].rdata.netbios.length = num_addresses*6;
66         packet->answers[0].rdata.netbios.addresses = 
67                 talloc_array(packet->answers, struct nbt_rdata_address, num_addresses);
68         if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
69
70         for (i=0;i<num_addresses;i++) {
71                 struct nbt_rdata_address *addr = 
72                         &packet->answers[0].rdata.netbios.addresses[i];
73                 addr->nb_flags = nb_flags;
74                 addr->ipaddr = talloc_strdup(packet->answers, addresses[i]);
75                 if (addr->ipaddr == NULL) goto failed;
76         }
77
78         DEBUG(7,("Sending name query reply for %s<%02x> at %s to %s:%d\n", 
79                  name->name, name->type, src_address, addresses[0], src_port));
80         
81         nbt_name_reply_send(nbtsock, src_address, src_port, packet);
82
83 failed:
84         talloc_free(packet);
85 }
86
87
88 /*
89   answer a name query
90 */
91 void nbtd_request_query(struct nbt_name_socket *nbtsock, 
92                         struct nbt_name_packet *packet, 
93                         const char *src_address, int src_port)
94 {
95         struct nbtd_iface_name *iname;
96         struct nbt_name *name;
97         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
98                                                        struct nbtd_interface);
99
100         /* see if its a node status query */
101         if (packet->qdcount == 1 &&
102             packet->questions[0].question_type == NBT_QTYPE_STATUS) {
103                 nbtd_query_status(nbtsock, packet, src_address, src_port);
104                 return;
105         }
106
107         NBT_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
108         NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
109         NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_class == NBT_QCLASS_IP);
110
111         /* see if we have the requested name on this interface */
112         name = &packet->questions[0].name;
113
114         iname = nbtd_find_iname(iface, name, 0);
115         if (iname == NULL) {
116                 DEBUG(7,("Query for %s<%02x> from %s - not found on %s\n",
117                          name->name, name->type, src_address, iface->ip_address));
118                 return;
119         }
120
121         /* if the name is not yet active and its a broadcast query then
122            ignore it for now */
123         if (!(iname->nb_flags & NBT_NM_ACTIVE) && 
124             (packet->operation & NBT_FLAG_BROADCAST)) {
125                 DEBUG(7,("Query for %s<%02x> from %s - name not active yet on %s\n",
126                          name->name, name->type, src_address, iface->ip_address));
127                 return;
128         }
129
130         nbtd_name_query_reply(nbtsock, packet, src_address, src_port,
131                               &iname->name, iname->ttl, iname->nb_flags, 
132                               nbtd_address_list(iface, packet));
133 }