4f42ec517d528f6bfd1caee677658f67fb983524
[bbaumbach/samba-autobuild/.git] / source4 / 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 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 #include "includes.h"
23 #include "lib/util/dlinklist.h"
24 #include "system/network.h"
25 #include "nbt_server/nbt_server.h"
26 #include "nbt_server/wins/winsserver.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "lib/socket/socket.h"
29
30 /*
31   answer a name query
32 */
33 void nbtd_request_query(struct nbt_name_socket *nbtsock, 
34                         struct nbt_name_packet *packet, 
35                         struct socket_address *src)
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         /* see if its a node status query */
43         if (packet->qdcount == 1 &&
44             packet->questions[0].question_type == NBT_QTYPE_STATUS) {
45                 nbtd_query_status(nbtsock, packet, src);
46                 return;
47         }
48
49         NBTD_ASSERT_PACKET(packet, src, packet->qdcount == 1);
50         NBTD_ASSERT_PACKET(packet, src, 
51                            packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
52         NBTD_ASSERT_PACKET(packet, src, 
53                            packet->questions[0].question_class == NBT_QCLASS_IP);
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, 0);
59         if (iname == NULL) {
60                 /* don't send negative replies to broadcast queries */
61                 if (packet->operation & NBT_FLAG_BROADCAST) {
62                         return;
63                 }
64
65                 if (packet->operation & NBT_FLAG_RECURSION_DESIRED) {
66                         nbtd_winsserver_request(nbtsock, packet, src);
67                         return;
68                 }
69
70                 /* otherwise send a negative reply */
71                 nbtd_negative_name_query_reply(nbtsock, packet, src);
72                 return;
73         }
74
75         /*
76          * normally we should forward all queries with the
77          * recursion desired flag to the wins server, but this
78          * breaks are winsclient code, when doing mhomed registrations
79          */
80         if (!(packet->operation & NBT_FLAG_BROADCAST) &&
81            (packet->operation & NBT_FLAG_RECURSION_DESIRED) &&
82            (iname->nb_flags & NBT_NM_GROUP) &&
83            lp_wins_support()) {
84                 nbtd_winsserver_request(nbtsock, packet, src);
85                 return;
86         }
87
88         /* if the name is not yet active and its a broadcast query then
89            ignore it for now */
90         if (!(iname->nb_flags & NBT_NM_ACTIVE) && 
91             (packet->operation & NBT_FLAG_BROADCAST)) {
92                 DEBUG(7,("Query for %s from %s - name not active yet on %s\n",
93                          nbt_name_string(packet, name), src->addr, iface->ip_address));
94                 return;
95         }
96
97         nbtd_name_query_reply(nbtsock, packet, src,
98                               &iname->name, iname->ttl, iname->nb_flags, 
99                               nbtd_address_list(iface, packet));
100 }