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