2 Unix SMB/CIFS implementation.
4 low level WINS replication client code
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "lib/socket/socket.h"
27 #include "libcli/wrepl/winsrepl.h"
30 mark all pending requests as dead - called when a socket error happens
32 static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket)
34 event_set_fd_flags(wrepl_socket->fde, 0);
36 while (wrepl_socket->send_queue) {
37 struct wrepl_request *req = wrepl_socket->send_queue;
38 DLIST_REMOVE(wrepl_socket->send_queue, req);
39 req->state = WREPL_REQUEST_ERROR;
40 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
45 while (wrepl_socket->recv_queue) {
46 struct wrepl_request *req = wrepl_socket->recv_queue;
47 DLIST_REMOVE(wrepl_socket->recv_queue, req);
48 req->state = WREPL_REQUEST_ERROR;
49 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
59 static void wrepl_handler_send(struct wrepl_socket *wrepl_socket)
61 while (wrepl_socket->send_queue) {
62 struct wrepl_request *req = wrepl_socket->send_queue;
66 status = socket_send(wrepl_socket->sock, &req->buffer, &nsent, 0);
67 if (NT_STATUS_IS_ERR(status)) {
68 wrepl_socket_dead(wrepl_socket);
71 if (!NT_STATUS_IS_OK(status) || nsent == 0) return;
73 req->buffer.data += nsent;
74 req->buffer.length -= nsent;
75 if (req->buffer.length != 0) {
79 DLIST_REMOVE(wrepl_socket->send_queue, req);
80 DLIST_ADD_END(wrepl_socket->recv_queue, req, struct wrepl_request *);
81 req->state = WREPL_REQUEST_RECV;
83 EVENT_FD_READABLE(wrepl_socket->fde);
86 EVENT_FD_NOT_WRITEABLE(wrepl_socket->fde);
93 static void wrepl_handler_recv(struct wrepl_socket *wrepl_socket)
96 struct wrepl_request *req = wrepl_socket->recv_queue;
100 EVENT_FD_NOT_READABLE(wrepl_socket->fde);
104 if (req->buffer.length == 0) {
105 req->buffer = data_blob_talloc(req, NULL, 4);
106 if (req->buffer.data == NULL) {
107 req->status = NT_STATUS_NO_MEMORY;
113 /* read in the packet length */
114 if (req->num_read < 4) {
117 req->status = socket_recv(wrepl_socket->sock,
118 req->buffer.data + req->num_read,
121 if (NT_STATUS_IS_ERR(req->status)) goto failed;
122 if (!NT_STATUS_IS_OK(req->status)) return;
124 req->num_read += nread;
125 if (req->num_read != 4) return;
127 req_length = RIVAL(req->buffer.data, 0) + 4;
129 req->buffer.data = talloc_realloc(req, req->buffer.data,
130 uint8_t, req_length);
131 if (req->buffer.data == NULL) {
132 req->status = NT_STATUS_NO_MEMORY;
135 req->buffer.length = req_length;
138 /* read in the body */
139 req->status = socket_recv(wrepl_socket->sock,
140 req->buffer.data + req->num_read,
141 req->buffer.length - req->num_read,
143 if (NT_STATUS_IS_ERR(req->status)) goto failed;
144 if (!NT_STATUS_IS_OK(req->status)) return;
146 req->num_read += nread;
147 if (req->num_read != req->buffer.length) return;
149 req->packet = talloc(req, struct wrepl_packet);
150 if (req->packet == NULL) {
151 req->status = NT_STATUS_NO_MEMORY;
155 blob.data = req->buffer.data + 4;
156 blob.length = req->buffer.length - 4;
158 /* we have a full request - parse it */
159 req->status = ndr_pull_struct_blob(&blob,
160 req->packet, req->packet,
161 (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
162 if (!NT_STATUS_IS_OK(req->status)) {
163 DEBUG(2,("Failed to parse incoming WINS packet - %s\n",
164 nt_errstr(req->status)));
165 DEBUG(10,("packet length %d\n", (int)req->buffer.length));
166 NDR_PRINT_DEBUG(wrepl_packet, req->packet);
171 DEBUG(10,("Received WINS packet of length %d\n", (int)req->buffer.length));
172 NDR_PRINT_DEBUG(wrepl_packet, req->packet);
175 DLIST_REMOVE(wrepl_socket->recv_queue, req);
176 req->state = WREPL_REQUEST_DONE;
183 if (req->state == WREPL_REQUEST_RECV) {
184 DLIST_REMOVE(wrepl_socket->recv_queue, req);
186 req->state = WREPL_REQUEST_ERROR;
194 handler for winrepl events
196 static void wrepl_handler(struct event_context *ev, struct fd_event *fde,
197 uint16_t flags, void *private)
199 struct wrepl_socket *wrepl_socket = talloc_get_type(private,
200 struct wrepl_socket);
201 if (flags & EVENT_FD_WRITE) {
202 wrepl_handler_send(wrepl_socket);
204 if (flags & EVENT_FD_READ) {
205 wrepl_handler_recv(wrepl_socket);
211 handler for winrepl connection completion
213 static void wrepl_connect_handler(struct event_context *ev, struct fd_event *fde,
214 uint16_t flags, void *private)
216 struct wrepl_socket *wrepl_socket = talloc_get_type(private,
217 struct wrepl_socket);
218 struct wrepl_request *req = wrepl_socket->recv_queue;
222 if (req == NULL) return;
224 req->status = socket_connect_complete(wrepl_socket->sock, 0);
225 if (NT_STATUS_IS_ERR(req->status)) goto failed;
227 if (!NT_STATUS_IS_OK(req->status)) return;
229 wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket,
230 socket_get_fd(wrepl_socket->sock),
232 wrepl_handler, wrepl_socket);
233 if (wrepl_socket->fde == NULL) {
234 req->status = NT_STATUS_NO_MEMORY;
239 DLIST_REMOVE(wrepl_socket->recv_queue, req);
240 if (!NT_STATUS_IS_OK(req->status)) {
241 req->state = WREPL_REQUEST_ERROR;
243 req->state = WREPL_REQUEST_DONE;
252 initialise a wrepl_socket. The event_ctx is optional, if provided then
253 operations will use that event context
255 struct wrepl_socket *wrepl_socket_init(TALLOC_CTX *mem_ctx,
256 struct event_context *event_ctx)
258 struct wrepl_socket *wrepl_socket;
261 wrepl_socket = talloc(mem_ctx, struct wrepl_socket);
262 if (wrepl_socket == NULL) goto failed;
264 if (event_ctx == NULL) {
265 wrepl_socket->event_ctx = event_context_init(wrepl_socket);
267 wrepl_socket->event_ctx = talloc_reference(wrepl_socket, event_ctx);
269 if (wrepl_socket->event_ctx == NULL) goto failed;
271 status = socket_create("ip", SOCKET_TYPE_STREAM, &wrepl_socket->sock, 0);
272 if (!NT_STATUS_IS_OK(status)) goto failed;
274 talloc_steal(wrepl_socket, wrepl_socket->sock);
276 wrepl_socket->send_queue = NULL;
277 wrepl_socket->recv_queue = NULL;
279 wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket,
280 socket_get_fd(wrepl_socket->sock),
282 wrepl_connect_handler, wrepl_socket);
284 set_blocking(socket_get_fd(wrepl_socket->sock), False);
289 talloc_free(wrepl_socket);
295 destroy a wrepl_request
297 static int wrepl_request_destructor(void *ptr)
299 struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
300 if (req->state == WREPL_REQUEST_SEND) {
301 DLIST_REMOVE(req->wrepl_socket->send_queue, req);
303 if (req->state == WREPL_REQUEST_RECV) {
304 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
306 req->state = WREPL_REQUEST_ERROR;
311 wait for a request to complete
313 static NTSTATUS wrepl_request_wait(struct wrepl_request *req)
315 NT_STATUS_HAVE_NO_MEMORY(req);
316 while (req->state < WREPL_REQUEST_DONE) {
317 event_loop_once(req->wrepl_socket->event_ctx);
324 connect a wrepl_socket to a WINS server
326 struct wrepl_request *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
329 struct wrepl_request *req;
332 req = talloc_zero(wrepl_socket, struct wrepl_request);
333 if (req == NULL) goto failed;
335 req->wrepl_socket = wrepl_socket;
336 req->state = WREPL_REQUEST_RECV;
338 DLIST_ADD(wrepl_socket->recv_queue, req);
340 talloc_set_destructor(req, wrepl_request_destructor);
342 status = socket_connect(wrepl_socket->sock, iface_best_ip(address), 0, address,
343 WINS_REPLICATION_PORT, 0);
344 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) goto failed;
354 connect a wrepl_socket to a WINS server - recv side
356 NTSTATUS wrepl_connect_recv(struct wrepl_request *req)
358 return wrepl_request_wait(req);
363 connect a wrepl_socket to a WINS server - sync API
365 NTSTATUS wrepl_connect(struct wrepl_socket *wrepl_socket, const char *address)
367 struct wrepl_request *req = wrepl_connect_send(wrepl_socket, address);
368 return wrepl_connect_recv(req);
373 send a generic wins replication request
375 struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
376 struct wrepl_packet *packet)
378 struct wrepl_request *req;
379 struct wrepl_wrap wrap;
381 req = talloc_zero(wrepl_socket, struct wrepl_request);
382 if (req == NULL) goto failed;
384 req->wrepl_socket = wrepl_socket;
385 req->state = WREPL_REQUEST_SEND;
387 wrap.packet = *packet;
388 req->status = ndr_push_struct_blob(&req->buffer, req, &wrap,
389 (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
390 if (!NT_STATUS_IS_OK(req->status)) goto failed;
393 DEBUG(10,("Sending WINS packet of length %d\n", (int)req->buffer.length));
394 NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
397 DLIST_ADD(wrepl_socket->send_queue, req);
399 talloc_set_destructor(req, wrepl_request_destructor);
401 EVENT_FD_WRITEABLE(wrepl_socket->fde);
411 receive a generic WINS replication reply
413 NTSTATUS wrepl_request_recv(struct wrepl_request *req,
415 struct wrepl_packet **packet)
417 NTSTATUS status = wrepl_request_wait(req);
418 if (NT_STATUS_IS_OK(status)) {
419 *packet = talloc_steal(mem_ctx, req->packet);
426 a full WINS replication request/response
428 NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket,
430 struct wrepl_packet *req_packet,
431 struct wrepl_packet **reply_packet)
433 struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet);
434 return wrepl_request_recv(req, mem_ctx, reply_packet);
439 setup an association - send
441 struct wrepl_request *wrepl_associate_send(struct wrepl_socket *wrepl_socket,
442 struct wrepl_associate *io)
444 struct wrepl_packet *packet;
445 struct wrepl_request *req;
447 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
448 if (packet == NULL) return NULL;
450 packet->opcode = WREPL_OPCODE_BITS;
451 packet->mess_type = WREPL_START_ASSOCIATION;
452 packet->message.start.minor_version = 2;
453 packet->message.start.major_version = 5;
455 req = wrepl_request_send(wrepl_socket, packet);
463 setup an association - recv
465 NTSTATUS wrepl_associate_recv(struct wrepl_request *req,
466 struct wrepl_associate *io)
468 struct wrepl_packet *packet=NULL;
470 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
471 if (packet->mess_type != WREPL_START_ASSOCIATION_REPLY) {
472 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
474 if (NT_STATUS_IS_OK(status)) {
475 io->out.assoc_ctx = packet->message.start_reply.assoc_ctx;
482 setup an association - sync api
484 NTSTATUS wrepl_associate(struct wrepl_socket *wrepl_socket,
485 struct wrepl_associate *io)
487 struct wrepl_request *req = wrepl_associate_send(wrepl_socket, io);
488 return wrepl_associate_recv(req, io);
493 fetch the partner tables - send
495 struct wrepl_request *wrepl_pull_table_send(struct wrepl_socket *wrepl_socket,
496 struct wrepl_pull_table *io)
498 struct wrepl_packet *packet;
499 struct wrepl_request *req;
501 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
502 if (packet == NULL) return NULL;
504 packet->opcode = WREPL_OPCODE_BITS;
505 packet->assoc_ctx = io->in.assoc_ctx;
506 packet->mess_type = WREPL_REPLICATION;
507 packet->message.replication.command = WREPL_REPL_TABLE_QUERY;
509 req = wrepl_request_send(wrepl_socket, packet);
518 fetch the partner tables - recv
520 NTSTATUS wrepl_pull_table_recv(struct wrepl_request *req,
522 struct wrepl_pull_table *io)
524 struct wrepl_packet *packet=NULL;
526 struct wrepl_table *table;
529 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
530 if (packet->mess_type != WREPL_REPLICATION) {
531 status = NT_STATUS_NETWORK_ACCESS_DENIED;
532 } else if (packet->message.replication.command != WREPL_REPL_TABLE_REPLY) {
533 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
535 if (!NT_STATUS_IS_OK(status)) goto failed;
537 table = &packet->message.replication.info.table;
538 io->out.num_partners = table->partner_count;
539 io->out.partners = talloc_steal(mem_ctx, table->partners);
540 for (i=0;i<io->out.num_partners;i++) {
541 talloc_steal(io->out.partners, io->out.partners[i].address);
551 fetch the partner table - sync api
553 NTSTATUS wrepl_pull_table(struct wrepl_socket *wrepl_socket,
555 struct wrepl_pull_table *io)
557 struct wrepl_request *req = wrepl_pull_table_send(wrepl_socket, io);
558 return wrepl_pull_table_recv(req, mem_ctx, io);
563 fetch the names for a WINS partner - send
565 struct wrepl_request *wrepl_pull_names_send(struct wrepl_socket *wrepl_socket,
566 struct wrepl_pull_names *io)
568 struct wrepl_packet *packet;
569 struct wrepl_request *req;
571 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
572 if (packet == NULL) return NULL;
574 packet->opcode = WREPL_OPCODE_BITS;
575 packet->assoc_ctx = io->in.assoc_ctx;
576 packet->mess_type = WREPL_REPLICATION;
577 packet->message.replication.command = WREPL_REPL_SEND_REQUEST;
578 packet->message.replication.info.owner = io->in.partner;
580 req = wrepl_request_send(wrepl_socket, packet);
589 extract a nbt_name from a WINS name buffer
591 static NTSTATUS wrepl_extract_name(struct nbt_name *name,
593 uint8_t *namebuf, uint32_t len)
597 /* oh wow, what a nasty bug in windows ... */
598 if (namebuf[0] == 0x1b && len >= 16) {
599 namebuf[0] = namebuf[15];
604 make_nbt_name_client(name, talloc_strndup(mem_ctx, namebuf, len));
608 s = talloc_strndup(mem_ctx, namebuf, 15);
609 trim_string(s, NULL, " ");
611 name->type = namebuf[15];
613 name->scope = talloc_strndup(mem_ctx, namebuf+17, len-17);
622 fetch the names for a WINS partner - recv
624 NTSTATUS wrepl_pull_names_recv(struct wrepl_request *req,
626 struct wrepl_pull_names *io)
628 struct wrepl_packet *packet=NULL;
632 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
633 if (packet->mess_type != WREPL_REPLICATION ||
634 packet->message.replication.command != WREPL_REPL_SEND_REPLY) {
635 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
637 if (!NT_STATUS_IS_OK(status)) goto failed;
639 io->out.num_names = packet->message.replication.info.reply.num_names;
641 status = NT_STATUS_NO_MEMORY;
643 io->out.names = talloc_array(packet, struct wrepl_name, io->out.num_names);
644 if (io->out.names == NULL) goto failed;
646 /* convert the list of names and addresses to a sane format */
647 for (i=0;i<io->out.num_names;i++) {
648 struct wrepl_wins_name *wname = &packet->message.replication.info.reply.names[i];
649 struct wrepl_name *name = &io->out.names[i];
650 status = wrepl_extract_name(&name->name, io->out.names,
651 wname->name, wname->name_len);
652 if (!NT_STATUS_IS_OK(status)) goto failed;
654 /* trying to save 1 or 2 bytes on the wire isn't a good idea */
655 if (wname->flags & 2) {
658 name->num_addresses = wname->addresses.addresses.num_ips;
659 name->addresses = talloc_array(io->out.names,
660 struct wrepl_address,
661 name->num_addresses);
662 if (name->addresses == NULL) goto failed;
663 for (j=0;j<name->num_addresses;j++) {
664 name->addresses[j].owner =
665 talloc_steal(name->addresses,
666 wname->addresses.addresses.ips[j].owner);
667 name->addresses[j].address =
668 talloc_steal(name->addresses,
669 wname->addresses.addresses.ips[j].ip);
672 name->num_addresses = 1;
673 name->addresses = talloc(io->out.names, struct wrepl_address);
674 if (name->addresses == NULL) goto failed;
675 name->addresses[0].owner = talloc_steal(name->addresses,
676 wname->addresses.address.owner);
677 name->addresses[0].address = talloc_steal(name->addresses,
678 wname->addresses.address.ip);
682 talloc_steal(mem_ctx, io->out.names);
683 status = NT_STATUS_OK;
693 fetch the names for a WINS partner - sync api
695 NTSTATUS wrepl_pull_names(struct wrepl_socket *wrepl_socket,
697 struct wrepl_pull_names *io)
699 struct wrepl_request *req = wrepl_pull_names_send(wrepl_socket, io);
700 return wrepl_pull_names_recv(req, mem_ctx, io);