ccbdc2b933726b77a747da172691f7c430da0389
[garming/samba-autobuild/.git] / source4 / nbt_server / nodestatus.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    answer node status 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 status reply
30 */
31 static void nbtd_node_status_reply(struct nbt_name_socket *nbtsock, 
32                                    struct nbt_name_packet *request_packet, 
33                                    const struct nbt_peer_socket *src,
34                                    struct nbt_name *name, 
35                                    struct nbtd_interface *iface)
36 {
37         struct nbt_name_packet *packet;
38         uint32_t name_count;
39         struct nbtd_iface_name *iname;
40         struct nbtd_server *nbtsrv = iface->nbtsrv;
41         
42         /* work out how many names to send */
43         name_count = 0;
44         for (iname=iface->names;iname;iname=iname->next) {
45                 if ((iname->nb_flags & NBT_NM_ACTIVE) && 
46                     strcmp(iname->name.name, "*") != 0) {
47                         name_count++;
48                 }
49         }
50
51         packet = talloc_zero(nbtsock, struct nbt_name_packet);
52         if (packet == NULL) return;
53
54         packet->name_trn_id = request_packet->name_trn_id;
55         packet->ancount = 1;
56         packet->operation = NBT_OPCODE_QUERY | NBT_FLAG_REPLY | NBT_FLAG_AUTHORITIVE;
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_STATUS;
63         packet->answers[0].rr_class = NBT_QCLASS_IP;
64         packet->answers[0].ttl      = 0;
65         packet->answers[0].rdata.status.num_names = name_count;
66         packet->answers[0].rdata.status.names = talloc_array(packet->answers,
67                                                              struct nbt_status_name, name_count);
68         if (packet->answers[0].rdata.status.names == NULL) goto failed;
69
70         name_count = 0;
71         for (iname=iface->names;iname;iname=iname->next) {
72                 if ((iname->nb_flags & NBT_NM_ACTIVE) && 
73                     strcmp(iname->name.name, "*") != 0) {
74                         struct nbt_status_name *n = &packet->answers[0].rdata.status.names[name_count];
75                         n->name = talloc_asprintf(packet->answers, "%-15s", iname->name.name);
76                         if (n->name == NULL) goto failed;
77                         n->type     = iname->name.type;
78                         n->nb_flags = iname->nb_flags;
79                         name_count++;
80                 }
81         }
82         /* we deliberately don't fill in the statistics structure as
83            it could lead to giving attackers too much information */
84         ZERO_STRUCT(packet->answers[0].rdata.status.statistics);
85
86         DEBUG(7,("Sending node status reply for %s to %s:%d\n", 
87                  nbt_name_string(packet, name), src->addr, src->port));
88         
89         nbtsrv->stats.total_sent++;
90         nbt_name_reply_send(nbtsock, src, packet);
91
92 failed:
93         talloc_free(packet);
94 }
95
96
97 /*
98   answer a node status query
99 */
100 void nbtd_query_status(struct nbt_name_socket *nbtsock, 
101                        struct nbt_name_packet *packet, 
102                        const struct nbt_peer_socket *src)
103 {
104         struct nbt_name *name;
105         struct nbtd_iface_name *iname;
106         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
107                                                        struct nbtd_interface);
108
109         NBTD_ASSERT_PACKET(packet, src->addr, packet->qdcount == 1);
110         NBTD_ASSERT_PACKET(packet, src->addr, packet->questions[0].question_type == NBT_QTYPE_STATUS);
111         NBTD_ASSERT_PACKET(packet, src->addr, packet->questions[0].question_class == NBT_QCLASS_IP);
112
113         /* see if we have the requested name on this interface */
114         name = &packet->questions[0].name;
115
116         iname = nbtd_find_iname(iface, name, NBT_NM_ACTIVE);
117         if (iname == NULL) {
118                 DEBUG(7,("Node status query for %s from %s - not found on %s\n",
119                          nbt_name_string(packet, name), src->addr, iface->ip_address));
120                 return;
121         }
122
123         nbtd_node_status_reply(nbtsock, packet, src, 
124                                &iname->name, iface);
125 }