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