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