From 42d6a4c4f0d87e8e1bdd1e3152e592822bf95337 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 18 Feb 2005 23:13:51 +0000 Subject: r5451: - added separate wrepl_associate(), wrepl_pull_table() and wrepl_pull_names() functions, with reasonable parameters, so callers don't need to deal directly with wins replication packet structures - converted the NBT-WINSREPLICATION torture test to use the new APIs (This used to be commit cec1672662b7e5b1bdf843e9dee317aa4b03f719) --- source4/libcli/wins/winsrepl.c | 268 +++++++++++++++++++++++++++++++++++++++++ source4/libcli/wins/winsrepl.h | 45 +++++++ 2 files changed, 313 insertions(+) (limited to 'source4/libcli') diff --git a/source4/libcli/wins/winsrepl.c b/source4/libcli/wins/winsrepl.c index 6ee948202f..bf3593bdf8 100644 --- a/source4/libcli/wins/winsrepl.c +++ b/source4/libcli/wins/winsrepl.c @@ -434,3 +434,271 @@ NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket, struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet); return wrepl_request_recv(req, mem_ctx, reply_packet); } + + +/* + setup an association - send +*/ +struct wrepl_request *wrepl_associate_send(struct wrepl_socket *wrepl_socket, + struct wrepl_associate *io) +{ + struct wrepl_packet *packet; + struct wrepl_request *req; + + packet = talloc_zero(wrepl_socket, struct wrepl_packet); + if (packet == NULL) return NULL; + + packet->opcode = WREPL_OPCODE_BITS; + packet->mess_type = WREPL_START_ASSOCIATION; + packet->message.start.minor_version = 2; + packet->message.start.major_version = 5; + + req = wrepl_request_send(wrepl_socket, packet); + + talloc_free(packet); + + return req; +} + +/* + setup an association - recv +*/ +NTSTATUS wrepl_associate_recv(struct wrepl_request *req, + struct wrepl_associate *io) +{ + struct wrepl_packet *packet=NULL; + NTSTATUS status; + status = wrepl_request_recv(req, req->wrepl_socket, &packet); + if (packet->mess_type != WREPL_START_ASSOCIATION_REPLY) { + status = NT_STATUS_UNEXPECTED_NETWORK_ERROR; + } + if (NT_STATUS_IS_OK(status)) { + io->out.assoc_ctx = packet->message.start_reply.assoc_ctx; + } + talloc_free(packet); + return status; +} + +/* + setup an association - sync api +*/ +NTSTATUS wrepl_associate(struct wrepl_socket *wrepl_socket, + struct wrepl_associate *io) +{ + struct wrepl_request *req = wrepl_associate_send(wrepl_socket, io); + return wrepl_associate_recv(req, io); +} + + +/* + fetch the partner tables - send +*/ +struct wrepl_request *wrepl_pull_table_send(struct wrepl_socket *wrepl_socket, + struct wrepl_pull_table *io) +{ + struct wrepl_packet *packet; + struct wrepl_request *req; + + packet = talloc_zero(wrepl_socket, struct wrepl_packet); + if (packet == NULL) return NULL; + + packet->opcode = WREPL_OPCODE_BITS; + packet->assoc_ctx = io->in.assoc_ctx; + packet->mess_type = WREPL_REPLICATION; + packet->message.replication.command = WREPL_REPL_TABLE_QUERY; + + req = wrepl_request_send(wrepl_socket, packet); + + talloc_free(packet); + + return req; +} + + +/* + fetch the partner tables - recv +*/ +NTSTATUS wrepl_pull_table_recv(struct wrepl_request *req, + TALLOC_CTX *mem_ctx, + struct wrepl_pull_table *io) +{ + struct wrepl_packet *packet=NULL; + NTSTATUS status; + struct wrepl_table *table; + int i; + + status = wrepl_request_recv(req, req->wrepl_socket, &packet); + if (packet->mess_type != WREPL_REPLICATION) { + status = NT_STATUS_NETWORK_ACCESS_DENIED; + } else if (packet->message.replication.command != WREPL_REPL_TABLE_REPLY) { + status = NT_STATUS_UNEXPECTED_NETWORK_ERROR; + } + if (!NT_STATUS_IS_OK(status)) goto failed; + + table = &packet->message.replication.info.table; + io->out.num_partners = table->partner_count; + io->out.partners = talloc_steal(mem_ctx, table->partners); + for (i=0;iout.num_partners;i++) { + talloc_steal(io->out.partners, io->out.partners[i].address); + } + +failed: + talloc_free(packet); + return status; +} + + +/* + fetch the partner table - sync api +*/ +NTSTATUS wrepl_pull_table(struct wrepl_socket *wrepl_socket, + TALLOC_CTX *mem_ctx, + struct wrepl_pull_table *io) +{ + struct wrepl_request *req = wrepl_pull_table_send(wrepl_socket, io); + return wrepl_pull_table_recv(req, mem_ctx, io); +} + + +/* + fetch the names for a WINS partner - send +*/ +struct wrepl_request *wrepl_pull_names_send(struct wrepl_socket *wrepl_socket, + struct wrepl_pull_names *io) +{ + struct wrepl_packet *packet; + struct wrepl_request *req; + + packet = talloc_zero(wrepl_socket, struct wrepl_packet); + if (packet == NULL) return NULL; + + packet->opcode = WREPL_OPCODE_BITS; + packet->assoc_ctx = io->in.assoc_ctx; + packet->mess_type = WREPL_REPLICATION; + packet->message.replication.command = WREPL_REPL_SEND_REQUEST; + packet->message.replication.info.owner = io->in.partner; + + req = wrepl_request_send(wrepl_socket, packet); + + talloc_free(packet); + + return req; +} + + +/* + extract a nbt_name from a WINS name buffer +*/ +static NTSTATUS wrepl_extract_name(struct nbt_name *name, + TALLOC_CTX *mem_ctx, + uint8_t *namebuf, uint32_t len) +{ + char *s; + + /* oh wow, what a nasty bug in windows ... */ + if (namebuf[0] == 0x1b && len >= 16) { + namebuf[0] = namebuf[15]; + namebuf[15] = 0x1b; + } + + if (len < 17) { + name->name = talloc_strndup(mem_ctx, namebuf, len); + name->type = 0; + name->scope = NULL; + return NT_STATUS_OK; + } + + s = talloc_strndup(mem_ctx, namebuf, 15); + trim_string(s, NULL, " "); + name->name = s; + name->type = namebuf[15]; + if (len > 18) { + name->scope = talloc_strndup(mem_ctx, namebuf+17, len-17); + } else { + name->scope = NULL; + } + + return NT_STATUS_OK; +} + +/* + fetch the names for a WINS partner - recv +*/ +NTSTATUS wrepl_pull_names_recv(struct wrepl_request *req, + TALLOC_CTX *mem_ctx, + struct wrepl_pull_names *io) +{ + struct wrepl_packet *packet=NULL; + NTSTATUS status; + int i; + + status = wrepl_request_recv(req, req->wrepl_socket, &packet); + if (packet->mess_type != WREPL_REPLICATION || + packet->message.replication.command != WREPL_REPL_SEND_REPLY) { + status = NT_STATUS_UNEXPECTED_NETWORK_ERROR; + } + if (!NT_STATUS_IS_OK(status)) goto failed; + + io->out.num_names = packet->message.replication.info.reply.num_names; + + status = NT_STATUS_NO_MEMORY; + + io->out.names = talloc_array(packet, struct wrepl_name, io->out.num_names); + if (io->out.names == NULL) goto failed; + + /* convert the list of names and addresses to a sane format */ + for (i=0;iout.num_names;i++) { + struct wrepl_wins_name *wname = &packet->message.replication.info.reply.names[i]; + struct wrepl_name *name = &io->out.names[i]; + status = wrepl_extract_name(&name->name, io->out.names, + wname->name, wname->name_len); + if (!NT_STATUS_IS_OK(status)) goto failed; + + /* trying to save 1 or 2 bytes on the wire isn't a good idea */ + if (wname->flags & 2) { + int j; + + name->num_addresses = wname->addresses.addresses.num_ips; + name->addresses = talloc_array(io->out.names, + struct wrepl_address, + name->num_addresses); + if (name->addresses == NULL) goto failed; + for (j=0;jnum_addresses;j++) { + name->addresses[j].owner = + talloc_steal(name->addresses, + wname->addresses.addresses.ips[j].owner); + name->addresses[j].address = + talloc_steal(name->addresses, + wname->addresses.addresses.ips[j].ip); + } + } else { + name->num_addresses = 1; + name->addresses = talloc(io->out.names, struct wrepl_address); + if (name->addresses == NULL) goto failed; + name->addresses[0].owner = talloc_steal(name->addresses, + wname->addresses.address.owner); + name->addresses[0].address = talloc_steal(name->addresses, + wname->addresses.address.ip); + } + } + + talloc_steal(mem_ctx, io->out.names); + status = NT_STATUS_OK; + +failed: + talloc_free(packet); + return status; +} + + + +/* + fetch the names for a WINS partner - sync api +*/ +NTSTATUS wrepl_pull_names(struct wrepl_socket *wrepl_socket, + TALLOC_CTX *mem_ctx, + struct wrepl_pull_names *io) +{ + struct wrepl_request *req = wrepl_pull_names_send(wrepl_socket, io); + return wrepl_pull_names_recv(req, mem_ctx, io); +} diff --git a/source4/libcli/wins/winsrepl.h b/source4/libcli/wins/winsrepl.h index 3fd1e5406c..79b7f1fd70 100644 --- a/source4/libcli/wins/winsrepl.h +++ b/source4/libcli/wins/winsrepl.h @@ -20,6 +20,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include "librpc/gen_ndr/ndr_nbt.h" #include "librpc/gen_ndr/ndr_winsrepl.h" /* @@ -67,3 +68,47 @@ struct wrepl_request { void *private; } async; }; + + +/* + setup an association +*/ +struct wrepl_associate { + struct { + uint32_t assoc_ctx; + } out; +}; + +/* + pull the partner table +*/ +struct wrepl_pull_table { + struct { + uint32_t assoc_ctx; + } in; + struct { + uint32_t num_partners; + struct wrepl_wins_owner *partners; + } out; +}; + +/* + a full pull replication +*/ +struct wrepl_pull_names { + struct { + uint32_t assoc_ctx; + struct wrepl_wins_owner partner; + } in; + struct { + uint32_t num_names; + struct wrepl_name { + struct nbt_name name; + uint32_t num_addresses; + struct wrepl_address { + const char *owner; + const char *address; + } *addresses; + } *names; + } out; +}; -- cgit