09b2549956e2ae3951faf54c6163fb4c37d3b7a3
[gd/samba-autobuild/.git] / source4 / nbt_server / packet.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    packet utility functions
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 "nbt_server/nbt_server.h"
24 #include "lib/socket/socket.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26
27 /*
28   we received a badly formed packet - log it
29 */
30 void nbtd_bad_packet(struct nbt_name_packet *packet, 
31                      const struct socket_address *src, const char *reason)
32 {
33         DEBUG(2,("nbtd: bad packet '%s' from %s:%d\n", reason, src->addr, src->port));
34         if (DEBUGLVL(5)) {
35                 NDR_PRINT_DEBUG(nbt_name_packet, packet);               
36         }
37 }
38
39
40 /*
41   see if an incoming packet is a broadcast packet from one of our own
42   interfaces
43 */
44 BOOL nbtd_self_packet_and_bcast(struct nbt_name_socket *nbtsock, 
45                                 struct nbt_name_packet *packet, 
46                                 const struct socket_address *src)
47 {
48         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
49                                                        struct nbtd_interface);
50
51         /* if its not a broadcast then its not considered a self packet */
52         if (!(packet->operation & NBT_FLAG_BROADCAST)) {
53                 return False;
54         }
55
56         /* 
57          * this uses the fact that iface->nbtsock is the unicast listen address
58          * if the interface isn't the global bcast interface
59          *
60          * so if the request was directed to the unicast address it isn't a broadcast
61          * message
62          */
63         if (iface->nbtsock == nbtsock &&
64             iface != iface->nbtsrv->bcast_interface) {
65                 return False;
66         }
67
68         return nbtd_self_packet(nbtsock, packet, src);
69 }
70
71 BOOL nbtd_self_packet(struct nbt_name_socket *nbtsock, 
72                       struct nbt_name_packet *packet, 
73                       const struct socket_address *src)
74 {
75         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
76                                                        struct nbtd_interface);
77         struct nbtd_server *nbtsrv = iface->nbtsrv;
78         
79         /* if its not from the nbt port, then it wasn't a broadcast from us */
80         if (src->port != lp_nbt_port()) {
81                 return False;
82         }
83
84         /* we have to loop over our interface list, seeing if its from
85            one of our own interfaces */
86         for (iface=nbtsrv->interfaces;iface;iface=iface->next) {
87                 if (strcmp(src->addr, iface->ip_address) == 0) {
88                         return True;
89                 }
90         }
91
92         return False;
93 }
94
95
96 /*
97   send a name query reply
98 */
99 void nbtd_name_query_reply(struct nbt_name_socket *nbtsock, 
100                            struct nbt_name_packet *request_packet, 
101                            struct socket_address *src,
102                            struct nbt_name *name, uint32_t ttl,
103                            uint16_t nb_flags, const char **addresses)
104 {
105         struct nbt_name_packet *packet;
106         size_t num_addresses = str_list_length(addresses);
107         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
108                                                        struct nbtd_interface);
109         struct nbtd_server *nbtsrv = iface->nbtsrv;
110         int i;
111
112         if (num_addresses == 0) {
113                 DEBUG(3,("No addresses in name query reply - failing\n"));
114                 return;
115         }
116
117         packet = talloc_zero(nbtsock, struct nbt_name_packet);
118         if (packet == NULL) return;
119
120         packet->name_trn_id = request_packet->name_trn_id;
121         packet->ancount = 1;
122         packet->operation = 
123                 NBT_FLAG_REPLY | 
124                 NBT_OPCODE_QUERY | 
125                 NBT_FLAG_AUTHORITIVE |
126                 NBT_FLAG_RECURSION_DESIRED |
127                 NBT_FLAG_RECURSION_AVAIL;
128
129         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
130         if (packet->answers == NULL) goto failed;
131
132         packet->answers[0].name     = *name;
133         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
134         packet->answers[0].rr_class = NBT_QCLASS_IP;
135         packet->answers[0].ttl      = ttl;
136         packet->answers[0].rdata.netbios.length = num_addresses*6;
137         packet->answers[0].rdata.netbios.addresses = 
138                 talloc_array(packet->answers, struct nbt_rdata_address, num_addresses);
139         if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
140
141         for (i=0;i<num_addresses;i++) {
142                 struct nbt_rdata_address *addr = 
143                         &packet->answers[0].rdata.netbios.addresses[i];
144                 addr->nb_flags = nb_flags;
145                 addr->ipaddr = talloc_strdup(packet->answers, addresses[i]);
146                 if (addr->ipaddr == NULL) goto failed;
147         }
148
149         DEBUG(7,("Sending name query reply for %s at %s to %s:%d\n", 
150                  nbt_name_string(packet, name), addresses[0], src->addr, src->port));
151         
152         nbtsrv->stats.total_sent++;
153         nbt_name_reply_send(nbtsock, src, packet);
154
155 failed:
156         talloc_free(packet);
157 }
158
159
160 /*
161   send a negative name query reply
162 */
163 void nbtd_negative_name_query_reply(struct nbt_name_socket *nbtsock, 
164                                     struct nbt_name_packet *request_packet, 
165                                     struct socket_address *src)
166 {
167         struct nbt_name_packet *packet;
168         struct nbt_name *name = &request_packet->questions[0].name;
169         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
170                                                        struct nbtd_interface);
171         struct nbtd_server *nbtsrv = iface->nbtsrv;
172
173         packet = talloc_zero(nbtsock, struct nbt_name_packet);
174         if (packet == NULL) return;
175
176         packet->name_trn_id = request_packet->name_trn_id;
177         packet->ancount = 1;
178         packet->operation = 
179                 NBT_FLAG_REPLY | 
180                 NBT_OPCODE_QUERY | 
181                 NBT_FLAG_AUTHORITIVE |
182                 NBT_RCODE_NAM;
183
184         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
185         if (packet->answers == NULL) goto failed;
186
187         packet->answers[0].name      = *name;
188         packet->answers[0].rr_type   = NBT_QTYPE_NULL;
189         packet->answers[0].rr_class  = NBT_QCLASS_IP;
190         packet->answers[0].ttl       = 0;
191         ZERO_STRUCT(packet->answers[0].rdata);
192
193         DEBUG(7,("Sending negative name query reply for %s to %s:%d\n", 
194                  nbt_name_string(packet, name), src->addr, src->port));
195         
196         nbtsrv->stats.total_sent++;
197         nbt_name_reply_send(nbtsock, src, packet);
198
199 failed:
200         talloc_free(packet);
201 }
202
203 /*
204   send a name registration reply
205 */
206 void nbtd_name_registration_reply(struct nbt_name_socket *nbtsock, 
207                                   struct nbt_name_packet *request_packet, 
208                                   struct socket_address *src,
209                                   uint8_t rcode)
210 {
211         struct nbt_name_packet *packet;
212         struct nbt_name *name = &request_packet->questions[0].name;
213         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
214                                                        struct nbtd_interface);
215         struct nbtd_server *nbtsrv = iface->nbtsrv;
216
217         packet = talloc_zero(nbtsock, struct nbt_name_packet);
218         if (packet == NULL) return;
219
220         packet->name_trn_id = request_packet->name_trn_id;
221         packet->ancount = 1;
222         packet->operation = 
223                 NBT_FLAG_REPLY | 
224                 NBT_OPCODE_REGISTER |
225                 NBT_FLAG_AUTHORITIVE |
226                 NBT_FLAG_RECURSION_DESIRED |
227                 NBT_FLAG_RECURSION_AVAIL |
228                 rcode;
229         
230         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
231         if (packet->answers == NULL) goto failed;
232
233         packet->answers[0].name     = *name;
234         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
235         packet->answers[0].rr_class = NBT_QCLASS_IP;
236         packet->answers[0].ttl      = request_packet->additional[0].ttl;
237         packet->answers[0].rdata    = request_packet->additional[0].rdata;
238
239         DEBUG(7,("Sending %s name registration reply for %s to %s:%d\n", 
240                  rcode==0?"positive":"negative",
241                  nbt_name_string(packet, name), src->addr, src->port));
242         
243         nbtsrv->stats.total_sent++;
244         nbt_name_reply_send(nbtsock, src, packet);
245
246 failed:
247         talloc_free(packet);
248 }
249
250
251 /*
252   send a name release reply
253 */
254 void nbtd_name_release_reply(struct nbt_name_socket *nbtsock, 
255                              struct nbt_name_packet *request_packet, 
256                              struct socket_address *src,
257                              uint8_t rcode)
258 {
259         struct nbt_name_packet *packet;
260         struct nbt_name *name = &request_packet->questions[0].name;
261         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
262                                                        struct nbtd_interface);
263         struct nbtd_server *nbtsrv = iface->nbtsrv;
264
265         packet = talloc_zero(nbtsock, struct nbt_name_packet);
266         if (packet == NULL) return;
267
268         packet->name_trn_id = request_packet->name_trn_id;
269         packet->ancount = 1;
270         packet->operation = 
271                 NBT_FLAG_REPLY | 
272                 NBT_OPCODE_RELEASE |
273                 NBT_FLAG_AUTHORITIVE |
274                 rcode;
275         
276         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
277         if (packet->answers == NULL) goto failed;
278
279         packet->answers[0].name     = *name;
280         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
281         packet->answers[0].rr_class = NBT_QCLASS_IP;
282         packet->answers[0].ttl      = request_packet->additional[0].ttl;
283         packet->answers[0].rdata    = request_packet->additional[0].rdata;
284
285         DEBUG(7,("Sending %s name release reply for %s to %s:%d\n", 
286                  rcode==0?"positive":"negative",
287                  nbt_name_string(packet, name), src->addr, src->port));
288         
289         nbtsrv->stats.total_sent++;
290         nbt_name_reply_send(nbtsock, src, packet);
291
292 failed:
293         talloc_free(packet);
294 }
295
296
297 /*
298   send a WACK reply
299 */
300 void nbtd_wack_reply(struct nbt_name_socket *nbtsock, 
301                      struct nbt_name_packet *request_packet, 
302                      struct socket_address *src,
303                      uint32_t ttl)
304 {
305         struct nbt_name_packet *packet;
306         struct nbt_name *name = &request_packet->questions[0].name;
307         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
308                                                        struct nbtd_interface);
309         struct nbtd_server *nbtsrv = iface->nbtsrv;
310
311         packet = talloc_zero(nbtsock, struct nbt_name_packet);
312         if (packet == NULL) return;
313
314         packet->name_trn_id = request_packet->name_trn_id;
315         packet->ancount = 1;
316         packet->operation = 
317                 NBT_FLAG_REPLY | 
318                 NBT_OPCODE_WACK |
319                 NBT_FLAG_AUTHORITIVE;
320         
321         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
322         if (packet->answers == NULL) goto failed;
323
324         packet->answers[0].name              = *name;
325         packet->answers[0].rr_type           = NBT_QTYPE_NETBIOS;
326         packet->answers[0].rr_class          = NBT_QCLASS_IP;
327         packet->answers[0].ttl               = ttl;
328         packet->answers[0].rdata.data.length = 2;
329         packet->answers[0].rdata.data.data   = talloc_size(packet, 2);
330         if (packet->answers[0].rdata.data.data == NULL) goto failed;
331         RSSVAL(packet->answers[0].rdata.data.data, 0, request_packet->operation);
332
333         DEBUG(7,("Sending WACK reply for %s to %s:%d\n", 
334                  nbt_name_string(packet, name), src->addr, src->port));
335         
336         nbtsrv->stats.total_sent++;
337         nbt_name_reply_send(nbtsock, src, packet);
338
339 failed:
340         talloc_free(packet);
341 }