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