9c5ae43d4076dd46314603f2db8b1a114106543c
[garming/samba-autobuild/.git] / source4 / libcli / nbt / nameregister.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    send out a name registration request
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 "libcli/nbt/libnbt.h"
24 #include "libcli/nbt/nbt_proto.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/socket/socket.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "param/param.h"
29
30 /*
31   send a nbt name registration request
32 */
33 struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock,
34                                                 struct nbt_name_register *io)
35 {
36         struct nbt_name_request *req;
37         struct nbt_name_packet *packet;
38         struct socket_address *dest;
39
40         packet = talloc_zero(nbtsock, struct nbt_name_packet);
41         if (packet == NULL) return NULL;
42
43         packet->qdcount = 1;
44         packet->arcount = 1;
45         if (io->in.multi_homed) {
46                 packet->operation = NBT_OPCODE_MULTI_HOME_REG;
47         } else {
48                 packet->operation = NBT_OPCODE_REGISTER;
49         }
50         if (io->in.broadcast) {
51                 packet->operation |= NBT_FLAG_BROADCAST;
52         }
53         if (io->in.register_demand) {
54                 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
55         }
56
57         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
58         if (packet->questions == NULL) goto failed;
59
60         packet->questions[0].name           = io->in.name;
61         packet->questions[0].question_type  = NBT_QTYPE_NETBIOS;
62         packet->questions[0].question_class = NBT_QCLASS_IP;
63
64         packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
65         if (packet->additional == NULL) goto failed;
66
67         packet->additional[0].name                   = io->in.name;
68         packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
69         packet->additional[0].rr_class               = NBT_QCLASS_IP;
70         packet->additional[0].ttl                    = io->in.ttl;
71         packet->additional[0].rdata.netbios.length   = 6;
72         packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
73                                                                      struct nbt_rdata_address, 1);
74         if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
75         packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
76         packet->additional[0].rdata.netbios.addresses[0].ipaddr = 
77                 talloc_strdup(packet->additional, io->in.address);
78         if (packet->additional[0].rdata.netbios.addresses[0].ipaddr == NULL) goto failed;
79
80         dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, 
81                                            io->in.dest_addr, io->in.dest_port);
82         if (dest == NULL) goto failed;
83         req = nbt_name_request_send(nbtsock, dest, packet,
84                                     io->in.timeout, io->in.retries, false);
85         if (req == NULL) goto failed;
86
87         talloc_free(packet);
88         return req;
89
90 failed:
91         talloc_free(packet);
92         return NULL;    
93 }
94
95 /*
96   wait for a registration reply
97 */
98 _PUBLIC_ NTSTATUS nbt_name_register_recv(struct nbt_name_request *req, 
99                                 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
100 {
101         NTSTATUS status;
102         struct nbt_name_packet *packet;
103
104         status = nbt_name_request_recv(req);
105         if (!NT_STATUS_IS_OK(status) ||
106             req->num_replies == 0) {
107                 talloc_free(req);
108                 return status;
109         }
110         
111         packet = req->replies[0].packet;
112         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
113
114         if (packet->ancount != 1 ||
115             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
116             packet->answers[0].rr_class != NBT_QCLASS_IP) {
117                 talloc_free(req);
118                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
119         }
120
121         io->out.rcode = packet->operation & NBT_RCODE;
122         io->out.name = packet->answers[0].name;
123         if (packet->answers[0].rdata.netbios.length < 6) {
124                 talloc_free(req);
125                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
126         }
127         io->out.reply_addr = talloc_steal(mem_ctx, 
128                                           packet->answers[0].rdata.netbios.addresses[0].ipaddr);
129         talloc_steal(mem_ctx, io->out.name.name);
130         talloc_steal(mem_ctx, io->out.name.scope);
131             
132         talloc_free(req);
133
134         return NT_STATUS_OK;
135 }
136
137 /*
138   synchronous name registration request
139 */
140 _PUBLIC_ NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock, 
141                            TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
142 {
143         struct nbt_name_request *req = nbt_name_register_send(nbtsock, io);
144         return nbt_name_register_recv(req, mem_ctx, io);
145 }
146
147
148 /*
149   a 4 step broadcast registration. 3 lots of name registration requests, followed by
150   a name registration demand
151 */
152 struct register_bcast_state {
153         struct nbt_name_socket *nbtsock;
154         struct nbt_name_register *io;
155         struct nbt_name_request *req;
156 };
157
158
159 /*
160   state handler for 4 stage name registration
161 */
162 static void name_register_bcast_handler(struct nbt_name_request *req)
163 {
164         struct composite_context *c = talloc_get_type(req->async.private, struct composite_context);
165         struct register_bcast_state *state = talloc_get_type(c->private_data, struct register_bcast_state);
166         NTSTATUS status;
167
168         status = nbt_name_register_recv(state->req, state, state->io);
169         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
170                 if (state->io->in.register_demand == true) {
171                         /* all done */
172                         c->state = COMPOSITE_STATE_DONE;
173                         c->status = NT_STATUS_OK;
174                         goto done;
175                 }
176
177                 /* the registration timed out - good, send the demand */
178                 state->io->in.register_demand = true;
179                 state->io->in.retries         = 0;
180                 state->req = nbt_name_register_send(state->nbtsock, state->io);
181                 if (state->req == NULL) {
182                         c->state = COMPOSITE_STATE_ERROR;
183                         c->status = NT_STATUS_NO_MEMORY;
184                 } else {
185                         state->req->async.fn      = name_register_bcast_handler;
186                         state->req->async.private = c;
187                 }
188         } else if (!NT_STATUS_IS_OK(status)) {
189                 c->state = COMPOSITE_STATE_ERROR;
190                 c->status = status;
191         } else {
192                 c->state = COMPOSITE_STATE_ERROR;
193                 c->status = NT_STATUS_CONFLICTING_ADDRESSES;
194                 DEBUG(3,("Name registration conflict from %s for %s with ip %s - rcode %d\n",
195                          state->io->out.reply_from, 
196                          nbt_name_string(state, &state->io->out.name),
197                          state->io->out.reply_addr,
198                          state->io->out.rcode));
199         }
200
201 done:
202         if (c->state >= COMPOSITE_STATE_DONE &&
203             c->async.fn) {
204                 c->async.fn(c);
205         }
206 }
207
208 /*
209   the async send call for a 4 stage name registration
210 */
211 _PUBLIC_ struct composite_context *nbt_name_register_bcast_send(struct nbt_name_socket *nbtsock,
212                                                        struct nbt_name_register_bcast *io)
213 {
214         struct composite_context *c;
215         struct register_bcast_state *state;
216
217         c = talloc_zero(nbtsock, struct composite_context);
218         if (c == NULL) goto failed;
219
220         state = talloc(c, struct register_bcast_state);
221         if (state == NULL) goto failed;
222
223         state->io = talloc(state, struct nbt_name_register);
224         if (state->io == NULL) goto failed;
225
226         state->io->in.name            = io->in.name;
227         state->io->in.dest_addr       = io->in.dest_addr;
228         state->io->in.dest_port       = io->in.dest_port;
229         state->io->in.address         = io->in.address;
230         state->io->in.nb_flags        = io->in.nb_flags;
231         state->io->in.register_demand = false;
232         state->io->in.broadcast       = true;
233         state->io->in.multi_homed     = false;
234         state->io->in.ttl             = io->in.ttl;
235         state->io->in.timeout         = 1;
236         state->io->in.retries         = 2;
237
238         state->nbtsock = nbtsock;
239
240         state->req = nbt_name_register_send(nbtsock, state->io);
241         if (state->req == NULL) goto failed;
242
243         state->req->async.fn      = name_register_bcast_handler;
244         state->req->async.private = c;
245
246         c->private_data = state;
247         c->state        = COMPOSITE_STATE_IN_PROGRESS;
248         c->event_ctx    = nbtsock->event_ctx;
249
250         return c;
251
252 failed:
253         talloc_free(c);
254         return NULL;
255 }
256
257 /*
258   broadcast 4 part name register - recv
259 */
260 _PUBLIC_ NTSTATUS nbt_name_register_bcast_recv(struct composite_context *c)
261 {
262         NTSTATUS status;
263         status = composite_wait(c);
264         talloc_free(c);
265         return status;
266 }
267
268 /*
269   broadcast 4 part name register - sync interface
270 */
271 NTSTATUS nbt_name_register_bcast(struct nbt_name_socket *nbtsock,
272                                  struct nbt_name_register_bcast *io)
273 {
274         struct composite_context *c = nbt_name_register_bcast_send(nbtsock, io);
275         return nbt_name_register_bcast_recv(c);
276 }
277
278
279 /*
280   a wins name register with multiple WINS servers and multiple
281   addresses to register. Try each WINS server in turn, until we get a
282   reply for each address
283 */
284 struct register_wins_state {
285         struct nbt_name_socket *nbtsock;
286         struct nbt_name_register *io;
287         const char **wins_servers;
288         uint16_t wins_port;
289         const char **addresses;
290         int address_idx;
291         struct nbt_name_request *req;
292 };
293
294
295 /*
296   state handler for WINS multi-homed multi-server name register
297 */
298 static void name_register_wins_handler(struct nbt_name_request *req)
299 {
300         struct composite_context *c = talloc_get_type(req->async.private, 
301                                                       struct composite_context);
302         struct register_wins_state *state = talloc_get_type(c->private_data, 
303                                                             struct register_wins_state);
304         NTSTATUS status;
305
306         status = nbt_name_register_recv(state->req, state, state->io);
307         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
308                 /* the register timed out - try the next WINS server */
309                 state->wins_servers++;
310                 state->address_idx = 0;
311                 if (state->wins_servers[0] == NULL) {
312                         c->state = COMPOSITE_STATE_ERROR;
313                         c->status = status;
314                         goto done;
315                 }
316                 state->io->in.dest_addr = state->wins_servers[0];
317                 state->io->in.dest_port = state->wins_port;
318                 state->io->in.address   = state->addresses[0];
319                 state->req = nbt_name_register_send(state->nbtsock, state->io);
320                 if (state->req == NULL) {
321                         c->state = COMPOSITE_STATE_ERROR;
322                         c->status = NT_STATUS_NO_MEMORY;
323                 } else {
324                         state->req->async.fn      = name_register_wins_handler;
325                         state->req->async.private = c;
326                 }
327         } else if (!NT_STATUS_IS_OK(status)) {
328                 c->state = COMPOSITE_STATE_ERROR;
329                 c->status = status;
330         } else {
331                 if (state->io->out.rcode == 0 &&
332                     state->addresses[state->address_idx+1] != NULL) {
333                         /* register our next address */
334                         state->io->in.address = state->addresses[++(state->address_idx)];
335                         state->req = nbt_name_register_send(state->nbtsock, state->io);
336                         if (state->req == NULL) {
337                                 c->state = COMPOSITE_STATE_ERROR;
338                                 c->status = NT_STATUS_NO_MEMORY;
339                         } else {
340                                 state->req->async.fn      = name_register_wins_handler;
341                                 state->req->async.private = c;
342                         }
343                 } else {
344                         c->state = COMPOSITE_STATE_DONE;
345                         c->status = NT_STATUS_OK;
346                 }
347         }
348
349 done:
350         if (c->state >= COMPOSITE_STATE_DONE &&
351             c->async.fn) {
352                 c->async.fn(c);
353         }
354 }
355
356 /*
357   the async send call for a multi-server WINS register
358 */
359 _PUBLIC_ struct composite_context *nbt_name_register_wins_send(struct nbt_name_socket *nbtsock,
360                                                       struct nbt_name_register_wins *io)
361 {
362         struct composite_context *c;
363         struct register_wins_state *state;
364
365         c = talloc_zero(nbtsock, struct composite_context);
366         if (c == NULL) goto failed;
367
368         state = talloc(c, struct register_wins_state);
369         if (state == NULL) goto failed;
370
371         state->io = talloc(state, struct nbt_name_register);
372         if (state->io == NULL) goto failed;
373
374         state->wins_port = io->in.wins_port;
375         state->wins_servers = str_list_copy(state, io->in.wins_servers);
376         if (state->wins_servers == NULL || 
377             state->wins_servers[0] == NULL) goto failed;
378
379         state->addresses = str_list_copy(state, io->in.addresses);
380         if (state->addresses == NULL || 
381             state->addresses[0] == NULL) goto failed;
382
383         state->io->in.name            = io->in.name;
384         state->io->in.dest_addr       = state->wins_servers[0];
385         state->io->in.dest_port       = state->wins_port;
386         state->io->in.address         = io->in.addresses[0];
387         state->io->in.nb_flags        = io->in.nb_flags;
388         state->io->in.broadcast       = false;
389         state->io->in.register_demand = false;
390         state->io->in.multi_homed     = (io->in.nb_flags & NBT_NM_GROUP)?false:true;
391         state->io->in.ttl             = io->in.ttl;
392         state->io->in.timeout         = 3;
393         state->io->in.retries         = 2;
394
395         state->nbtsock     = nbtsock;
396         state->address_idx = 0;
397
398         state->req = nbt_name_register_send(nbtsock, state->io);
399         if (state->req == NULL) goto failed;
400
401         state->req->async.fn      = name_register_wins_handler;
402         state->req->async.private = c;
403
404         c->private_data = state;
405         c->state        = COMPOSITE_STATE_IN_PROGRESS;
406         c->event_ctx    = nbtsock->event_ctx;
407
408         return c;
409
410 failed:
411         talloc_free(c);
412         return NULL;
413 }
414
415 /*
416   multi-homed WINS name register - recv side
417 */
418 _PUBLIC_ NTSTATUS nbt_name_register_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
419                                      struct nbt_name_register_wins *io)
420 {
421         NTSTATUS status;
422         status = composite_wait(c);
423         if (NT_STATUS_IS_OK(status)) {
424                 struct register_wins_state *state = 
425                         talloc_get_type(c->private_data, struct register_wins_state);
426                 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
427                 io->out.rcode = state->io->out.rcode;
428         }
429         talloc_free(c);
430         return status;
431 }
432
433 /*
434   multi-homed WINS register - sync interface
435 */
436 _PUBLIC_ NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock,
437                                 TALLOC_CTX *mem_ctx,
438                                 struct nbt_name_register_wins *io)
439 {
440         struct composite_context *c = nbt_name_register_wins_send(nbtsock, io);
441         return nbt_name_register_wins_recv(c, mem_ctx, io);
442 }