diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 17:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 17:47:29 +0000 |
commit | 4f5791ebd03eaec1c7da0865a383175b05102712 (patch) | |
tree | 8ce7b00f7a76baa386372422adebbe64510812d4 /source4/nbt_server/wins | |
parent | Initial commit. (diff) | |
download | samba-4f5791ebd03eaec1c7da0865a383175b05102712.tar.xz samba-4f5791ebd03eaec1c7da0865a383175b05102712.zip |
Adding upstream version 2:4.17.12+dfsg.upstream/2%4.17.12+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'source4/nbt_server/wins')
-rw-r--r-- | source4/nbt_server/wins/wins_dns_proxy.c | 99 | ||||
-rw-r--r-- | source4/nbt_server/wins/wins_hook.c | 94 | ||||
-rw-r--r-- | source4/nbt_server/wins/wins_ldb.c | 127 | ||||
-rw-r--r-- | source4/nbt_server/wins/winsclient.c | 284 | ||||
-rw-r--r-- | source4/nbt_server/wins/winsdb.c | 1027 | ||||
-rw-r--r-- | source4/nbt_server/wins/winsdb.h | 81 | ||||
-rw-r--r-- | source4/nbt_server/wins/winsserver.c | 1074 | ||||
-rw-r--r-- | source4/nbt_server/wins/winsserver.h | 67 | ||||
-rw-r--r-- | source4/nbt_server/wins/winswack.c | 387 |
9 files changed, 3240 insertions, 0 deletions
diff --git a/source4/nbt_server/wins/wins_dns_proxy.c b/source4/nbt_server/wins/wins_dns_proxy.c new file mode 100644 index 0000000..95ceb21 --- /dev/null +++ b/source4/nbt_server/wins/wins_dns_proxy.c @@ -0,0 +1,99 @@ +/* + Unix SMB/CIFS implementation. + + wins server dns proxy + + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include "nbt_server/wins/winsserver.h" +#include "system/time.h" +#include "libcli/composite/composite.h" +#include "samba/service_task.h" +#include "libcli/resolve/resolve.h" +#include "lib/socket/socket.h" + +struct wins_dns_proxy_state { + struct nbt_name_socket *nbtsock; + struct nbt_name_packet *packet; + struct socket_address *src; +}; + +static void nbtd_wins_dns_proxy_handler(struct composite_context *creq) +{ + NTSTATUS status; + struct wins_dns_proxy_state *s = talloc_get_type(creq->async.private_data, + struct wins_dns_proxy_state); + struct nbt_name *name = &s->packet->questions[0].name; + const char *address; + const char **addresses; + uint16_t nb_flags = 0; /* TODO: ... */ + + status = resolve_name_recv(creq, s->packet, &address); + if (!NT_STATUS_IS_OK(status)) { + goto notfound; + } + + addresses = str_list_add(NULL, address); + talloc_steal(s->packet, addresses); + if (!addresses) goto notfound; + + nbtd_name_query_reply(s->nbtsock, s->packet, s->src, name, + 0, nb_flags, addresses); + return; +notfound: + nbtd_negative_name_query_reply(s->nbtsock, s->packet, s->src); +} + +/* + dns proxy query a name +*/ +void nbtd_wins_dns_proxy_query(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + struct nbt_name *name = &packet->questions[0].name; + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_dns_proxy_state *s; + struct composite_context *creq; + struct resolve_context *resolve_ctx; + + s = talloc(nbtsock, struct wins_dns_proxy_state); + if (!s) goto failed; + s->nbtsock = nbtsock; + s->packet = talloc_steal(s, packet); + s->src = socket_address_copy(s, src); + if (s->src == NULL) { + goto failed; + } + + resolve_ctx = resolve_context_init(s); + if (resolve_ctx == NULL) goto failed; + resolve_context_add_host_method(resolve_ctx); + + creq = resolve_name_send(resolve_ctx, s, name, iface->nbtsrv->task->event_ctx); + if (!creq) goto failed; + + creq->async.fn = nbtd_wins_dns_proxy_handler; + creq->async.private_data= s; + return; +failed: + nbtd_negative_name_query_reply(nbtsock, packet, src); +} diff --git a/source4/nbt_server/wins/wins_hook.c b/source4/nbt_server/wins/wins_hook.c new file mode 100644 index 0000000..1af471b --- /dev/null +++ b/source4/nbt_server/wins/wins_hook.c @@ -0,0 +1,94 @@ +/* + Unix SMB/CIFS implementation. + + wins hook feature, we run a specified script + which can then do some custom actions + + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include "system/filesys.h" + +static const char *wins_hook_action_string(enum wins_hook_action action) +{ + switch (action) { + case WINS_HOOK_ADD: return "add"; + case WINS_HOOK_MODIFY: return "refresh"; + case WINS_HOOK_DELETE: return "delete"; + } + + return "unknown"; +} + +void wins_hook(struct winsdb_handle *h, const struct winsdb_record *rec, + enum wins_hook_action action, const char *wins_hook_script) +{ + uint32_t i, length; + int child; + char *cmd = NULL; + TALLOC_CTX *tmp_mem = NULL; + + if (!wins_hook_script || !wins_hook_script[0]) return; + + tmp_mem = talloc_new(h); + if (!tmp_mem) goto failed; + + length = winsdb_addr_list_length(rec->addresses); + + if (action == WINS_HOOK_MODIFY && length < 1) { + action = WINS_HOOK_DELETE; + } + + cmd = talloc_asprintf(tmp_mem, + "%s %s %s %02x %ld", + wins_hook_script, + wins_hook_action_string(action), + rec->name->name, + rec->name->type, + (long int) rec->expire_time); + if (!cmd) goto failed; + + for (i=0; rec->addresses[i]; i++) { + cmd = talloc_asprintf_append_buffer(cmd, " %s", rec->addresses[i]->address); + if (!cmd) goto failed; + } + + DEBUG(10,("call wins hook '%s'\n", cmd)); + + /* signal handling in posix really sucks - doing this in a library + affects the whole app, but what else to do?? */ + signal(SIGCHLD, SIG_IGN); + + child = fork(); + if (child == (pid_t)-1) { + goto failed; + } + + if (child == 0) { +/* TODO: close file handles */ + execl("/bin/sh", "sh", "-c", cmd, NULL); + _exit(0); + } + + talloc_free(tmp_mem); + return; +failed: + talloc_free(tmp_mem); + DEBUG(0,("FAILED: calling wins hook '%s'\n", wins_hook_script)); +} diff --git a/source4/nbt_server/wins/wins_ldb.c b/source4/nbt_server/wins/wins_ldb.c new file mode 100644 index 0000000..304c98d --- /dev/null +++ b/source4/nbt_server/wins/wins_ldb.c @@ -0,0 +1,127 @@ +/* + ldb database module + + Copyright (C) Stefan Metzmacher 2006 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* + * Name: ldb + * + * Component: ldb winsdb module + * + * Description: verify winsdb records before they're written to disk + * + * Author: Stefan Metzmacher + */ + +#include "includes.h" +#include "lib/events/events.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include <ldb_module.h> +#include "system/network.h" +#include "lib/socket/netif.h" +#include "param/param.h" + +static int wins_ldb_verify(struct ldb_module *module, struct ldb_request *req) +{ + struct ldb_context *ldb = ldb_module_get_ctx(module); + struct winsdb_handle *h = talloc_get_type(ldb_get_opaque(ldb, "winsdb_handle"), + struct winsdb_handle); + const struct ldb_message *msg; + + switch (req->operation) { + case LDB_ADD: + msg = req->op.add.message; + break; + + case LDB_MODIFY: + msg = req->op.mod.message; + break; + + default: + return ldb_next_request(module, req); + } + + /* do not manipulate our control entries */ + if (ldb_dn_is_special(msg->dn)) { + return ldb_next_request(module, req); + } + + if (!h) { + ldb_debug_set(ldb, LDB_DEBUG_FATAL, "%s", "WINS_LDB: INTERNAL ERROR: no winsdb_handle present!"); + return LDB_ERR_OTHER; + } + + switch (h->caller) { + case WINSDB_HANDLE_CALLER_NBTD: + case WINSDB_HANDLE_CALLER_WREPL: + /* we trust our nbt and wrepl code ... */ + return ldb_next_request(module, req); + + case WINSDB_HANDLE_CALLER_ADMIN: + ldb_debug(ldb, LDB_DEBUG_WARNING, "%s\n", "WINS_LDB: TODO verify add/modify for WINSDB_HANDLE_CALLER_ADMIN"); + return ldb_next_request(module, req); + } + + return LDB_ERR_OTHER; +} + +static int wins_ldb_init(struct ldb_module *module) +{ + struct ldb_context *ldb = ldb_module_get_ctx(module); + struct winsdb_handle *h; + const char *owner; + struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm"); + + ldb_module_set_private(module, NULL); + + owner = lpcfg_parm_string(lp_ctx, NULL, "winsdb", "local_owner"); + if (!owner) { + struct interface *ifaces; + load_interface_list(module, lp_ctx, &ifaces); + owner = iface_list_first_v4(ifaces); + if (!owner) { + owner = "0.0.0.0"; + } + } + + h = talloc_zero(module, struct winsdb_handle); + if (!h) goto failed; + h->ldb = ldb; + h->caller = WINSDB_HANDLE_CALLER_ADMIN; + h->local_owner = talloc_strdup(h, owner); + if (!h->local_owner) goto failed; + + return ldb_set_opaque(ldb, "winsdb_handle", h); + +failed: + talloc_free(h); + return LDB_ERR_OTHER; +} + +static const struct ldb_module_ops ldb_wins_ldb_module_ops = { + .name = "wins_ldb", + .add = wins_ldb_verify, + .modify = wins_ldb_verify, + .init_context = wins_ldb_init +}; + +int ldb_wins_ldb_module_init(const char *version) +{ + LDB_MODULE_CHECK_VERSION(version); + return ldb_register_module(&ldb_wins_ldb_module_ops); +} diff --git a/source4/nbt_server/wins/winsclient.c b/source4/nbt_server/wins/winsclient.c new file mode 100644 index 0000000..c1e7f9a --- /dev/null +++ b/source4/nbt_server/wins/winsclient.c @@ -0,0 +1,284 @@ +/* + Unix SMB/CIFS implementation. + + wins client name registration and refresh + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsserver.h" +#include "libcli/composite/composite.h" +#include "lib/events/events.h" +#include "librpc/gen_ndr/ndr_nbt.h" +#include "samba/service_task.h" +#include "param/param.h" + +/* we send WINS client requests using our primary network interface +*/ +static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface) +{ + struct nbtd_server *nbtsrv = iface->nbtsrv; + return nbtsrv->interfaces->nbtsock; +} + + +static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te, + struct timeval t, void *private_data); + +/* + retry a WINS name registration +*/ +static void nbtd_wins_register_retry(struct tevent_context *ev, struct tevent_timer *te, + struct timeval t, void *private_data) +{ + struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name); + nbtd_winsclient_register(iname); +} + +/* + start a timer to refresh this name +*/ +static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname) +{ + uint32_t refresh_time; + uint32_t max_refresh_time = lpcfg_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "max_refresh_time", 7200); + + refresh_time = MIN(max_refresh_time, iname->ttl/2); + + tevent_add_timer(iname->iface->nbtsrv->task->event_ctx, + iname, + timeval_add(&iname->registration_time, refresh_time, 0), + nbtd_wins_refresh, iname); +} + +struct nbtd_wins_refresh_state { + struct nbtd_iface_name *iname; + struct nbt_name_refresh_wins io; +}; + +/* + called when a wins name refresh has completed +*/ +static void nbtd_wins_refresh_handler(struct tevent_req *subreq) +{ + NTSTATUS status; + struct nbtd_wins_refresh_state *state = + tevent_req_callback_data(subreq, + struct nbtd_wins_refresh_state); + struct nbtd_iface_name *iname = state->iname; + + status = nbt_name_refresh_wins_recv(subreq, state, &state->io); + TALLOC_FREE(subreq); + if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { + /* our WINS server is dead - start registration over + from scratch */ + DEBUG(2,("Failed to refresh %s with WINS server %s\n", + nbt_name_string(state, &iname->name), iname->wins_server)); + talloc_free(state); + nbtd_winsclient_register(iname); + return; + } + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1,("Name refresh failure with WINS for %s - %s\n", + nbt_name_string(state, &iname->name), nt_errstr(status))); + talloc_free(state); + return; + } + + if (state->io.out.rcode != 0) { + DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n", + state->io.out.wins_server, + nbt_name_string(state, &iname->name), + nt_errstr(nbt_rcode_to_ntstatus(state->io.out.rcode)))); + iname->nb_flags |= NBT_NM_CONFLICT; + talloc_free(state); + return; + } + + DEBUG(4,("Refreshed name %s with WINS server %s\n", + nbt_name_string(state, &iname->name), iname->wins_server)); + /* success - start a periodic name refresh */ + iname->nb_flags |= NBT_NM_ACTIVE; + if (iname->wins_server) { + /* + * talloc_free() would generate a warning, + * so steal it into the tmp context + */ + talloc_steal(state, iname->wins_server); + } + iname->wins_server = talloc_move(iname, &state->io.out.wins_server); + iname->registration_time = timeval_current(); + + talloc_free(state); + + nbtd_wins_start_refresh_timer(iname); +} + + +/* + refresh a WINS name registration +*/ +static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te, + struct timeval t, void *private_data) +{ + struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name); + struct nbtd_interface *iface = iname->iface; + struct nbt_name_socket *nbtsock = wins_socket(iface); + struct tevent_req *subreq; + struct nbtd_wins_refresh_state *state; + char **l; + + state = talloc_zero(iname, struct nbtd_wins_refresh_state); + if (state == NULL) { + return; + } + + state->iname = iname; + + /* setup a wins name refresh request */ + state->io.in.name = iname->name; + l = str_list_make_single(state, iname->wins_server); + state->io.in.wins_servers = discard_const_p(const char *, l); + state->io.in.wins_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx); + state->io.in.addresses = nbtd_address_list(iface, state); + state->io.in.nb_flags = iname->nb_flags; + state->io.in.ttl = iname->ttl; + + if (!state->io.in.addresses) { + talloc_free(state); + return; + } + + subreq = nbt_name_refresh_wins_send(state, ev, nbtsock, &state->io); + if (subreq == NULL) { + talloc_free(state); + return; + } + + tevent_req_set_callback(subreq, nbtd_wins_refresh_handler, state); +} + +struct nbtd_wins_register_state { + struct nbtd_iface_name *iname; + struct nbt_name_register_wins io; +}; + +/* + called when a wins name register has completed +*/ +static void nbtd_wins_register_handler(struct tevent_req *subreq) +{ + NTSTATUS status; + struct nbtd_wins_register_state *state = + tevent_req_callback_data(subreq, + struct nbtd_wins_register_state); + struct nbtd_iface_name *iname = state->iname; + + status = nbt_name_register_wins_recv(subreq, state, &state->io); + TALLOC_FREE(subreq); + if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { + /* none of the WINS servers responded - try again + periodically */ + int wins_retry_time = lpcfg_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "wins_retry", 300); + tevent_add_timer(iname->iface->nbtsrv->task->event_ctx, + iname, + timeval_current_ofs(wins_retry_time, 0), + nbtd_wins_register_retry, + iname); + talloc_free(state); + return; + } + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1,("Name register failure with WINS for %s - %s\n", + nbt_name_string(state, &iname->name), nt_errstr(status))); + talloc_free(state); + return; + } + + if (state->io.out.rcode != 0) { + DEBUG(1,("WINS server %s rejected name register of %s - %s\n", + state->io.out.wins_server, + nbt_name_string(state, &iname->name), + nt_errstr(nbt_rcode_to_ntstatus(state->io.out.rcode)))); + iname->nb_flags |= NBT_NM_CONFLICT; + talloc_free(state); + return; + } + + /* success - start a periodic name refresh */ + iname->nb_flags |= NBT_NM_ACTIVE; + if (iname->wins_server) { + /* + * talloc_free() would generate a warning, + * so steal it into the tmp context + */ + talloc_steal(state, iname->wins_server); + } + iname->wins_server = talloc_move(iname, &state->io.out.wins_server); + + iname->registration_time = timeval_current(); + + DEBUG(3,("Registered %s with WINS server %s\n", + nbt_name_string(state, &iname->name), iname->wins_server)); + + talloc_free(state); + + nbtd_wins_start_refresh_timer(iname); +} + +/* + register a name with our WINS servers +*/ +void nbtd_winsclient_register(struct nbtd_iface_name *iname) +{ + struct nbtd_interface *iface = iname->iface; + struct nbt_name_socket *nbtsock = wins_socket(iface); + struct nbtd_wins_register_state *state; + struct tevent_req *subreq; + + state = talloc_zero(iname, struct nbtd_wins_register_state); + if (state == NULL) { + return; + } + + state->iname = iname; + + /* setup a wins name register request */ + state->io.in.name = iname->name; + state->io.in.wins_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx); + state->io.in.wins_servers = lpcfg_wins_server_list(iface->nbtsrv->task->lp_ctx); + state->io.in.addresses = nbtd_address_list(iface, state); + state->io.in.nb_flags = iname->nb_flags; + state->io.in.ttl = iname->ttl; + + if (state->io.in.addresses == NULL) { + talloc_free(state); + return; + } + + subreq = nbt_name_register_wins_send(state, iface->nbtsrv->task->event_ctx, + nbtsock, &state->io); + if (subreq == NULL) { + talloc_free(state); + return; + } + + tevent_req_set_callback(subreq, nbtd_wins_register_handler, state); +} diff --git a/source4/nbt_server/wins/winsdb.c b/source4/nbt_server/wins/winsdb.c new file mode 100644 index 0000000..2a05e96 --- /dev/null +++ b/source4/nbt_server/wins/winsdb.c @@ -0,0 +1,1027 @@ +/* + Unix SMB/CIFS implementation. + + WINS database routines + + Copyright (C) Andrew Tridgell 2005 + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include <ldb.h> +#include <ldb_errors.h> +#include "librpc/gen_ndr/ndr_nbt.h" +#include "system/time.h" +#include "ldb_wrap.h" +#include "system/network.h" +#include "lib/socket/netif.h" +#include "param/param.h" +#include "lib/util/smb_strtox.h" + +#undef strcasecmp + +uint64_t winsdb_get_maxVersion(struct winsdb_handle *h) +{ + int ret; + struct ldb_context *ldb = h->ldb; + struct ldb_dn *dn; + struct ldb_result *res = NULL; + TALLOC_CTX *tmp_ctx = talloc_new(ldb); + uint64_t maxVersion = 0; + + dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION"); + if (!dn) goto failed; + + /* find the record in the WINS database */ + ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL); + if (ret != LDB_SUCCESS) goto failed; + if (res->count > 1) goto failed; + + if (res->count == 1) { + maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0); + } + +failed: + talloc_free(tmp_ctx); + return maxVersion; +} + +/* + if newVersion == 0 return the old maxVersion + 1 and save it + if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it +*/ +uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion) +{ + int trans; + int ret; + struct ldb_dn *dn; + struct ldb_result *res = NULL; + struct ldb_message *msg = NULL; + struct ldb_context *wins_db = h->ldb; + TALLOC_CTX *tmp_ctx = talloc_new(wins_db); + uint64_t oldMaxVersion = 0; + + trans = ldb_transaction_start(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + dn = ldb_dn_new(tmp_ctx, wins_db, "CN=VERSION"); + if (!dn) goto failed; + + /* find the record in the WINS database */ + ret = ldb_search(wins_db, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL); + if (ret != LDB_SUCCESS) goto failed; + if (res->count > 1) goto failed; + + if (res->count == 1) { + oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0); + } + + if (newMaxVersion == 0) { + newMaxVersion = oldMaxVersion + 1; + } else { + newMaxVersion = MAX(oldMaxVersion, newMaxVersion); + } + + msg = ldb_msg_new(tmp_ctx); + if (!msg) goto failed; + msg->dn = dn; + + + ret = ldb_msg_append_string(msg, "objectClass", "winsMaxVersion", + LDB_FLAG_MOD_REPLACE); + if (ret != LDB_SUCCESS) goto failed; + ret = ldb_msg_append_fmt(msg, LDB_FLAG_MOD_REPLACE, + "maxVersion", "%llu", (long long)newMaxVersion); + if (ret != LDB_SUCCESS) goto failed; + + ret = ldb_modify(wins_db, msg); + if (ret != LDB_SUCCESS) ret = ldb_add(wins_db, msg); + if (ret != LDB_SUCCESS) goto failed; + + trans = ldb_transaction_commit(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + talloc_free(tmp_ctx); + return newMaxVersion; + +failed: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db); + talloc_free(tmp_ctx); + return 0; +} + +/* + return a DN for a nbt_name +*/ +static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, + const struct nbt_name *name) +{ + struct ldb_dn *dn; + + dn = ldb_dn_new_fmt(mem_ctx, ldb, "type=0x%02X", name->type); + if (ldb_dn_is_valid(dn) && name->name && *name->name) { + ldb_dn_add_child_fmt(dn, "name=%s", name->name); + } + if (ldb_dn_is_valid(dn) && name->scope && *name->scope) { + ldb_dn_add_child_fmt(dn, "scope=%s", name->scope); + } + return dn; +} + +static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name) +{ + NTSTATUS status; + struct nbt_name *name; + unsigned int comp_num; + uint32_t cur = 0; + int error = 0; + + name = talloc(mem_ctx, struct nbt_name); + if (!name) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + comp_num = ldb_dn_get_comp_num(dn); + + if (comp_num > 3) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + if (comp_num > cur && strcasecmp("scope", ldb_dn_get_component_name(dn, cur)) == 0) { + name->scope = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data); + cur++; + } else { + name->scope = NULL; + } + + if (comp_num > cur && strcasecmp("name", ldb_dn_get_component_name(dn, cur)) == 0) { + name->name = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data); + cur++; + } else { + name->name = talloc_strdup(name, ""); + if (!name->name) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + } + + if (comp_num > cur && strcasecmp("type", ldb_dn_get_component_name(dn, cur)) == 0) { + name->type = + smb_strtoul( + (char *)ldb_dn_get_component_val(dn, cur)->data, + NULL, + 0, + &error, + SMB_STR_STANDARD); + if (error != 0) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + cur++; + } else { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + *_name = name; + return NT_STATUS_OK; +failed: + talloc_free(name); + return status; +} + +/* + decode the winsdb_addr("address") attribute: + "172.31.1.1" or + "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;" + are valid records +*/ +static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val, + TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr) +{ + NTSTATUS status; + struct winsdb_addr *addr; + const char *address; + const char *wins_owner; + const char *expire_time; + char *p; + + addr = talloc(mem_ctx, struct winsdb_addr); + if (!addr) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + address = (char *)val->data; + + p = strchr(address, ';'); + if (!p) { + /* support old entries, with only the address */ + addr->address = (const char *)talloc_steal(addr, val->data); + addr->wins_owner = talloc_strdup(addr, rec->wins_owner); + if (!addr->wins_owner) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + addr->expire_time = rec->expire_time; + *_addr = addr; + return NT_STATUS_OK; + } + + *p = '\0'; p++; + addr->address = talloc_strdup(addr, address); + if (!addr->address) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + if (strncmp("winsOwner:", p, 10) != 0) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + wins_owner = p + 10; + p = strchr(wins_owner, ';'); + if (!p) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + *p = '\0';p++; + if (strcmp(wins_owner, "0.0.0.0") == 0) { + wins_owner = h->local_owner; + } + addr->wins_owner = talloc_strdup(addr, wins_owner); + if (!addr->wins_owner) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + if (strncmp("expireTime:", p, 11) != 0) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + expire_time = p + 11; + p = strchr(expire_time, ';'); + if (!p) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + *p = '\0';p++; + addr->expire_time = ldb_string_to_time(expire_time); + + *_addr = addr; + return NT_STATUS_OK; +failed: + talloc_free(addr); + return status; +} + +/* + encode the winsdb_addr("address") attribute like this: + non-static record: + "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;" + static record: + "172.31.1.1" +*/ +static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec, + const char *attr_name, struct winsdb_addr *addr) +{ + const char *str; + + if (rec->is_static) { + str = talloc_strdup(msg, addr->address); + if (!str) return LDB_ERR_OPERATIONS_ERROR; + } else { + char *expire_time; + expire_time = ldb_timestring(msg, addr->expire_time); + if (!expire_time) return LDB_ERR_OPERATIONS_ERROR; + str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;", + addr->address, addr->wins_owner, + expire_time); + talloc_free(expire_time); + if (!str) return LDB_ERR_OPERATIONS_ERROR; + } + + return ldb_msg_add_string(msg, attr_name, str); +} + +struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx) +{ + struct winsdb_addr **addresses; + + addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1); + if (!addresses) return NULL; + + addresses[0] = NULL; + + return addresses; +} + +static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque) +{ + struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr); + struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr); + struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle); + bool a1_owned = false; + bool a2_owned = false; + + /* + * first the owned addresses with the newest to the oldest address + * then the replica addresses with the newest to the oldest address + */ + if (a2->expire_time != a1->expire_time) { + return a2->expire_time - a1->expire_time; + } + + if (strcmp(a2->wins_owner, h->local_owner) == 0) { + a2_owned = true; + } + + if (strcmp(a1->wins_owner, h->local_owner) == 0) { + a1_owned = true; + } + + return a2_owned - a1_owned; +} + +struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec, + struct winsdb_addr **addresses, const char *address, + const char *wins_owner, time_t expire_time, + bool is_name_registration) +{ + struct winsdb_addr *old_addr = NULL; + size_t len = 0; + size_t i; + bool found_old_replica = false; + + /* + * count the addresses and maybe + * find an old entry for the new address + */ + for (i=0; addresses[i]; i++) { + if (old_addr) continue; + if (strcmp(addresses[i]->address, address) == 0) { + old_addr = addresses[i]; + } + } + len = i; + + /* + * the address is already there + * and we can replace it + */ + if (old_addr) { + goto remove_old_addr; + } + + /* + * if we don't have 25 addresses already, + * we can just add the new address + */ + if (len < 25) { + goto add_new_addr; + } + + /* + * if we haven't found the address, + * and we have already have 25 addresses + * if so then we need to do the following: + * - if it isn't a name registration, then just ignore the new address + * - if it is a name registration, then first search for + * the oldest replica and if there's no replica address + * search the oldest owned address + */ + if (!is_name_registration) { + return addresses; + } + + /* + * find the oldest replica address, if there's no replica + * record at all, find the oldest owned address + */ + for (i=0; addresses[i]; i++) { + bool cur_is_replica = false; + /* find out if the current address is a replica */ + if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) { + cur_is_replica = true; + } + + /* + * if we already found a replica address and the current address + * is not a replica, then skip it + */ + if (found_old_replica && !cur_is_replica) continue; + + /* + * if we found the first replica address, reset the address + * that would be replaced + */ + if (!found_old_replica && cur_is_replica) { + found_old_replica = true; + old_addr = addresses[i]; + continue; + } + + /* + * if the first address isn't a replica, just start with + * the first one + */ + if (!old_addr) { + old_addr = addresses[i]; + continue; + } + + /* + * see if we find an older address + */ + if (addresses[i]->expire_time < old_addr->expire_time) { + old_addr = addresses[i]; + continue; + } + } + +remove_old_addr: + winsdb_addr_list_remove(addresses, old_addr->address); + len --; + +add_new_addr: + addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2); + if (!addresses) return NULL; + + addresses[len] = talloc(addresses, struct winsdb_addr); + if (!addresses[len]) { + talloc_free(addresses); + return NULL; + } + + addresses[len]->address = talloc_strdup(addresses[len], address); + if (!addresses[len]->address) { + talloc_free(addresses); + return NULL; + } + + addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner); + if (!addresses[len]->wins_owner) { + talloc_free(addresses); + return NULL; + } + + addresses[len]->expire_time = expire_time; + + addresses[len+1] = NULL; + + LDB_TYPESAFE_QSORT(addresses, len+1, h, winsdb_addr_sort_list); + + return addresses; +} + +void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address) +{ + size_t i; + + for (i=0; addresses[i]; i++) { + if (strcmp(addresses[i]->address, address) == 0) { + break; + } + } + + for (; addresses[i]; i++) { + addresses[i] = addresses[i+1]; + } + + return; +} + +struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address) +{ + size_t i; + + for (i=0; addresses[i]; i++) { + if (strcmp(addresses[i]->address, address) == 0) { + return addresses[i]; + } + } + + return NULL; +} + +size_t winsdb_addr_list_length(struct winsdb_addr **addresses) +{ + size_t i; + for (i=0; addresses[i]; i++); + return i; +} + +const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses) +{ + size_t len = winsdb_addr_list_length(addresses); + const char **str_list=NULL; + size_t i; + + for (i=0; i < len; i++) { + str_list = str_list_add(str_list, addresses[i]->address); + if (!str_list[i]) { + return NULL; + } + } + talloc_steal(mem_ctx, str_list); + return str_list; +} + +/* + load a WINS entry from the database +*/ +NTSTATUS winsdb_lookup(struct winsdb_handle *h, + const struct nbt_name *name, + TALLOC_CTX *mem_ctx, + struct winsdb_record **_rec) +{ + NTSTATUS status; + struct ldb_result *res = NULL; + int ret; + struct winsdb_record *rec; + struct ldb_context *wins_db = h->ldb; + TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx); + time_t now = time(NULL); + + /* find the record in the WINS database */ + ret = ldb_search(wins_db, tmp_ctx, &res, + winsdb_dn(tmp_ctx, wins_db, name), + LDB_SCOPE_BASE, NULL, NULL); + + if (ret != LDB_SUCCESS || res->count > 1) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } else if (res->count== 0) { + status = NT_STATUS_OBJECT_NAME_NOT_FOUND; + goto failed; + } + + status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec); + if (!NT_STATUS_IS_OK(status)) goto failed; + + talloc_steal(mem_ctx, rec); + talloc_free(tmp_ctx); + *_rec = rec; + return NT_STATUS_OK; + +failed: + talloc_free(tmp_ctx); + return status; +} + +NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec) +{ + NTSTATUS status; + struct winsdb_record *rec; + struct ldb_message_element *el; + struct nbt_name *name; + uint32_t i, j, num_values; + + rec = talloc(mem_ctx, struct winsdb_record); + if (rec == NULL) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + status = winsdb_nbt_name(rec, msg->dn, &name); + if (!NT_STATUS_IS_OK(status)) goto failed; + + if (strlen(name->name) > 15) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + if (name->scope && strlen(name->scope) > 238) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + /* parse it into a more convenient winsdb_record structure */ + rec->name = name; + rec->type = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE); + rec->state = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED); + rec->node = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B); + rec->is_static = ldb_msg_find_attr_as_int(msg, "isStatic", 0); + rec->expire_time = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL)); + rec->version = ldb_msg_find_attr_as_uint64(msg, "versionID", 0); + rec->wins_owner = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL); + rec->registered_by = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL); + talloc_steal(rec, rec->wins_owner); + talloc_steal(rec, rec->registered_by); + + if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) { + rec->wins_owner = h->local_owner; + } + + el = ldb_msg_find_element(msg, "address"); + if (el) { + num_values = el->num_values; + } else { + num_values = 0; + } + + if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) { + if (num_values != 1) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + } + if (rec->state == WREPL_STATE_ACTIVE) { + if (num_values < 1) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + } + if (num_values > 25) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + + rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1); + if (rec->addresses == NULL) { + status = NT_STATUS_NO_MEMORY; + goto failed; + } + + for (i=0,j=0;i<num_values;i++) { + bool we_are_owner = false; + + status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]); + if (!NT_STATUS_IS_OK(status)) goto failed; + + if (strcmp(rec->addresses[j]->wins_owner, h->local_owner) == 0) { + we_are_owner = true; + } + + /* + * the record isn't static and is active + * then don't add the address if it's expired, + * but only if we're the owner of the address + * + * This is important for SGROUP records, + * because each server thinks he's the owner of the + * record and the record isn't replicated on a + * name_refresh. So addresses owned by another owner + * could expire, but we still need to return them + * (as windows does). + */ + if (!rec->is_static && + rec->addresses[j]->expire_time <= now && + rec->state == WREPL_STATE_ACTIVE && + we_are_owner) { + DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n", + rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name), + timestring(rec->addresses[j], rec->addresses[j]->expire_time))); + talloc_free(rec->addresses[j]); + rec->addresses[j] = NULL; + continue; + } + j++; + } + rec->addresses[j] = NULL; + num_values = j; + + if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) { + rec->expire_time = get_time_t_max(); + for (i=0;rec->addresses[i];i++) { + rec->addresses[i]->expire_time = rec->expire_time; + } + } + + if (rec->state == WREPL_STATE_ACTIVE) { + if (num_values < 1) { + DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n", + nbt_name_string(mem_ctx, rec->name))); + rec->state = WREPL_STATE_RELEASED; + } + } + + *_rec = rec; + return NT_STATUS_OK; +failed: + if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) { + DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg->dn))); + } + talloc_free(rec); + return status; +} + +/* + form a ldb_message from a winsdb_record +*/ +static struct ldb_message *winsdb_message(struct ldb_context *ldb, + struct winsdb_record *rec, + TALLOC_CTX *mem_ctx) +{ + int i, ret; + size_t addr_count; + const char *expire_time; + struct ldb_message *msg = ldb_msg_new(mem_ctx); + if (msg == NULL) goto failed; + + /* make sure we don't put in corrupted records */ + addr_count = winsdb_addr_list_length(rec->addresses); + if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) { + rec->state = WREPL_STATE_RELEASED; + } + if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) { + rec->type = WREPL_TYPE_MHOMED; + } + + expire_time = ldb_timestring(msg, rec->expire_time); + if (!expire_time) { + goto failed; + } + + msg->dn = winsdb_dn(msg, ldb, rec->name); + if (msg->dn == NULL) goto failed; + ret = ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type); + if (rec->name->name && *rec->name->name) { + ret |= ldb_msg_add_string(msg, "name", rec->name->name); + } + if (rec->name->scope && *rec->name->scope) { + ret |= ldb_msg_add_string(msg, "scope", rec->name->scope); + } + ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord"); + ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type); + ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state); + ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node); + ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static); + ret |= ldb_msg_add_empty(msg, "expireTime", 0, NULL); + if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) { + ret |= ldb_msg_add_string(msg, "expireTime", expire_time); + } + ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version); + ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner); + ret |= ldb_msg_add_empty(msg, "address", 0, NULL); + for (i=0;rec->addresses[i];i++) { + ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]); + } + if (rec->registered_by) { + ret |= ldb_msg_append_string(msg, "registeredBy", rec->registered_by, 0); + } + if (ret != LDB_SUCCESS) goto failed; + return msg; + +failed: + talloc_free(msg); + return NULL; +} + +/* + save a WINS record into the database +*/ +uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags) +{ + struct ldb_message *msg; + struct ldb_context *wins_db = h->ldb; + TALLOC_CTX *tmp_ctx = talloc_new(wins_db); + int trans = -1; + int ret; + + trans = ldb_transaction_start(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + if (flags & WINSDB_FLAG_ALLOC_VERSION) { + /* passing '0' means auto-allocate a new one */ + rec->version = winsdb_set_maxVersion(h, 0); + if (rec->version == 0) goto failed; + } + if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) { + rec->wins_owner = h->local_owner; + } + + msg = winsdb_message(wins_db, rec, tmp_ctx); + if (msg == NULL) goto failed; + ret = ldb_add(wins_db, msg); + if (ret != LDB_SUCCESS) goto failed; + + trans = ldb_transaction_commit(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + wins_hook(h, rec, WINS_HOOK_ADD, h->hook_script); + + talloc_free(tmp_ctx); + return NBT_RCODE_OK; + +failed: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db); + talloc_free(tmp_ctx); + return NBT_RCODE_SVR; +} + + +/* + modify a WINS record in the database +*/ +uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags) +{ + struct ldb_message *msg; + struct ldb_context *wins_db = h->ldb; + TALLOC_CTX *tmp_ctx = talloc_new(wins_db); + int trans; + int ret; + unsigned int i; + + trans = ldb_transaction_start(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + if (flags & WINSDB_FLAG_ALLOC_VERSION) { + /* passing '0' means auto-allocate a new one */ + rec->version = winsdb_set_maxVersion(h, 0); + if (rec->version == 0) goto failed; + } + if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) { + rec->wins_owner = h->local_owner; + } + + msg = winsdb_message(wins_db, rec, tmp_ctx); + if (msg == NULL) goto failed; + + for (i=0;i<msg->num_elements;i++) { + msg->elements[i].flags = LDB_FLAG_MOD_REPLACE; + } + + ret = ldb_modify(wins_db, msg); + if (ret != LDB_SUCCESS) goto failed; + + trans = ldb_transaction_commit(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + wins_hook(h, rec, WINS_HOOK_MODIFY, h->hook_script); + + talloc_free(tmp_ctx); + return NBT_RCODE_OK; + +failed: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db); + talloc_free(tmp_ctx); + return NBT_RCODE_SVR; +} + + +/* + delete a WINS record from the database +*/ +uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec) +{ + struct ldb_context *wins_db = h->ldb; + TALLOC_CTX *tmp_ctx = talloc_new(wins_db); + struct ldb_dn *dn; + int trans; + int ret; + + trans = ldb_transaction_start(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + dn = winsdb_dn(tmp_ctx, wins_db, rec->name); + if (dn == NULL) goto failed; + + ret = ldb_delete(wins_db, dn); + if (ret != LDB_SUCCESS) goto failed; + + trans = ldb_transaction_commit(wins_db); + if (trans != LDB_SUCCESS) goto failed; + + wins_hook(h, rec, WINS_HOOK_DELETE, h->hook_script); + + talloc_free(tmp_ctx); + return NBT_RCODE_OK; + +failed: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db); + talloc_free(tmp_ctx); + return NBT_RCODE_SVR; +} + +static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx, + struct loadparm_context *lp_ctx, struct winsdb_handle *h, + const char *wins_path) +{ + int trans; + int ret; + struct ldb_dn *dn; + struct ldb_result *res = NULL; + struct ldb_message *msg = NULL; + TALLOC_CTX *tmp_ctx = talloc_new(h); + unsigned int flags = 0; + + trans = ldb_transaction_start(h->ldb); + if (trans != LDB_SUCCESS) goto failed; + + /* check if we have a special @MODULES record already */ + dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES"); + if (!dn) goto failed; + + /* find the record in the WINS database */ + ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL); + if (ret != LDB_SUCCESS) goto failed; + + if (res->count > 0) goto skip; + + /* if there's no record, add one */ + msg = ldb_msg_new(tmp_ctx); + if (!msg) goto failed; + msg->dn = dn; + + ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb"); + if (ret != LDB_SUCCESS) goto failed; + + ret = ldb_add(h->ldb, msg); + if (ret != LDB_SUCCESS) goto failed; + + trans = ldb_transaction_commit(h->ldb); + if (trans != LDB_SUCCESS) goto failed; + + /* close and reopen the database, with the modules */ + trans = LDB_ERR_OTHER; + talloc_free(h->ldb); + h->ldb = NULL; + + if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) { + flags |= LDB_FLG_NOSYNC; + } + + h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path, + NULL, NULL, flags); + if (!h->ldb) goto failed; + + talloc_free(tmp_ctx); + return true; + +skip: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb); + talloc_free(tmp_ctx); + return true; + +failed: + if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb); + talloc_free(tmp_ctx); + return false; +} + +struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, + struct tevent_context *ev_ctx, + struct loadparm_context *lp_ctx, + const char *owner, + enum winsdb_handle_caller caller) +{ + const struct loadparm_substitution *lp_sub = + lpcfg_noop_substitution(); + struct winsdb_handle *h = NULL; + unsigned int flags = 0; + bool ret; + int ldb_err; + char *wins_path; + + h = talloc_zero(mem_ctx, struct winsdb_handle); + if (!h) return NULL; + + wins_path = lpcfg_state_path(h, lp_ctx, "wins.ldb"); + + if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) { + flags |= LDB_FLG_NOSYNC; + } + + h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path, + NULL, NULL, flags); + if (!h->ldb) goto failed; + + h->caller = caller; + h->hook_script = lpcfg_wins_hook(lp_ctx, lp_sub, h); + + h->local_owner = talloc_strdup(h, owner); + if (!h->local_owner) goto failed; + + /* make sure the module list is available and used */ + ret = winsdb_check_or_add_module_list(ev_ctx, lp_ctx, h, wins_path); + if (!ret) goto failed; + + ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h); + if (ldb_err != LDB_SUCCESS) goto failed; + + return h; +failed: + talloc_free(h); + return NULL; +} + diff --git a/source4/nbt_server/wins/winsdb.h b/source4/nbt_server/wins/winsdb.h new file mode 100644 index 0000000..194bcc0 --- /dev/null +++ b/source4/nbt_server/wins/winsdb.h @@ -0,0 +1,81 @@ +/* + Unix SMB/CIFS implementation. + + WINS server structures + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#define WINSDB_FLAG_ALLOC_VERSION (1<<0) +#define WINSDB_FLAG_TAKE_OWNERSHIP (1<<1) + +struct winsdb_addr { + const char *address; + const char *wins_owner; + time_t expire_time; +}; + +/* + each record in the database contains the following information +*/ +struct winsdb_record { + struct nbt_name *name; + enum wrepl_name_type type; + enum wrepl_name_state state; + enum wrepl_name_node node; + bool is_static; + time_t expire_time; + uint64_t version; + const char *wins_owner; + struct winsdb_addr **addresses; + + /* only needed for debugging problems */ + const char *registered_by; +}; + +enum winsdb_handle_caller { + WINSDB_HANDLE_CALLER_ADMIN = 0, + WINSDB_HANDLE_CALLER_NBTD = 1, + WINSDB_HANDLE_CALLER_WREPL = 2 +}; + +struct winsdb_handle { + /* wins server database handle */ + struct ldb_context *ldb; + + /* + * the type of the caller, as we pass this to the + * 'wins_ldb' ldb module can decide if it needs to verify the + * the records before they're written to disk + */ + enum winsdb_handle_caller caller; + + /* local owner address */ + const char *local_owner; + + /* wins hook script */ + const char *hook_script; +}; + +enum wins_hook_action { + WINS_HOOK_ADD = 0, + WINS_HOOK_MODIFY = 1, + WINS_HOOK_DELETE = 2 +}; + +struct ldb_message; +struct tevent_context; +#include "nbt_server/wins/winsdb_proto.h" diff --git a/source4/nbt_server/wins/winsserver.c b/source4/nbt_server/wins/winsserver.c new file mode 100644 index 0000000..a9f3ecd --- /dev/null +++ b/source4/nbt_server/wins/winsserver.c @@ -0,0 +1,1074 @@ +/* + Unix SMB/CIFS implementation. + + core wins server handling + + Copyright (C) Andrew Tridgell 2005 + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "lib/util/dlinklist.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include "nbt_server/wins/winsserver.h" +#include "librpc/gen_ndr/ndr_nbt.h" +#include "system/time.h" +#include "libcli/composite/composite.h" +#include "samba/service_task.h" +#include "system/network.h" +#include "lib/socket/socket.h" +#include "lib/socket/netif.h" +#include <ldb.h> +#include "param/param.h" +#include "libcli/resolve/resolve.h" +#include "lib/util/util_net.h" + +/* + work out the ttl we will use given a client requested ttl +*/ +uint32_t wins_server_ttl(struct wins_server *winssrv, uint32_t ttl) +{ + ttl = MIN(ttl, winssrv->config.max_renew_interval); + ttl = MAX(ttl, winssrv->config.min_renew_interval); + return ttl; +} + +static enum wrepl_name_type wrepl_type(uint16_t nb_flags, struct nbt_name *name, bool mhomed) +{ + /* this copes with the nasty hack that is the type 0x1c name */ + if (name->type == NBT_NAME_LOGON) { + return WREPL_TYPE_SGROUP; + } + if (nb_flags & NBT_NM_GROUP) { + return WREPL_TYPE_GROUP; + } + if (mhomed) { + return WREPL_TYPE_MHOMED; + } + return WREPL_TYPE_UNIQUE; +} + +/* + register a new name with WINS +*/ +static uint8_t wins_register_new(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + const struct socket_address *src, + enum wrepl_name_type type) +{ + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + struct nbt_name *name = &packet->questions[0].name; + uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl); + uint16_t nb_flags = packet->additional[0].rdata.netbios.addresses[0].nb_flags; + const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr; + struct winsdb_record rec; + enum wrepl_name_node node; + +#define WREPL_NODE_NBT_FLAGS(nb_flags) \ + ((nb_flags & NBT_NM_OWNER_TYPE)>>13) + + node = WREPL_NODE_NBT_FLAGS(nb_flags); + + rec.name = name; + rec.type = type; + rec.state = WREPL_STATE_ACTIVE; + rec.node = node; + rec.is_static = false; + rec.expire_time = time(NULL) + ttl; + rec.version = 0; /* will be allocated later */ + rec.wins_owner = NULL; /* will be set later */ + rec.registered_by = src->addr; + rec.addresses = winsdb_addr_list_make(packet); + if (rec.addresses == NULL) return NBT_RCODE_SVR; + + rec.addresses = winsdb_addr_list_add(winssrv->wins_db, + &rec, rec.addresses, + address, + winssrv->wins_db->local_owner, + rec.expire_time, + true); + if (rec.addresses == NULL) return NBT_RCODE_SVR; + + DEBUG(4,("WINS: accepted registration of %s with address %s\n", + nbt_name_string(packet, name), rec.addresses[0]->address)); + + return winsdb_add(winssrv->wins_db, &rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP); +} + + +/* + update the ttl on an existing record +*/ +static uint8_t wins_update_ttl(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct winsdb_record *rec, + struct winsdb_addr *winsdb_addr, + const struct socket_address *src) +{ + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl); + const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr; + uint32_t modify_flags = 0; + + rec->expire_time = time(NULL) + ttl; + rec->registered_by = src->addr; + + if (winsdb_addr) { + rec->addresses = winsdb_addr_list_add(winssrv->wins_db, + rec, rec->addresses, + winsdb_addr->address, + winssrv->wins_db->local_owner, + rec->expire_time, + true); + if (rec->addresses == NULL) return NBT_RCODE_SVR; + } + + if (strcmp(winssrv->wins_db->local_owner, rec->wins_owner) != 0) { + modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP; + } + + DEBUG(5,("WINS: refreshed registration of %s at %s\n", + nbt_name_string(packet, rec->name), address)); + + return winsdb_modify(winssrv->wins_db, rec, modify_flags); +} + +/* + do a sgroup merge +*/ +static uint8_t wins_sgroup_merge(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct winsdb_record *rec, + const char *address, + const struct socket_address *src) +{ + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl); + + rec->expire_time = time(NULL) + ttl; + rec->registered_by = src->addr; + + rec->addresses = winsdb_addr_list_add(winssrv->wins_db, + rec, rec->addresses, + address, + winssrv->wins_db->local_owner, + rec->expire_time, + true); + if (rec->addresses == NULL) return NBT_RCODE_SVR; + + DEBUG(5,("WINS: sgroup merge of %s at %s\n", + nbt_name_string(packet, rec->name), address)); + + return winsdb_modify(winssrv->wins_db, rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP); +} + +struct nbtd_wins_wack_state { + struct nbtd_wins_wack_state *prev, *next; + struct wins_server *winssrv; + struct nbt_name_socket *nbtsock; + struct nbtd_interface *iface; + struct nbt_name_packet *request_packet; + struct winsdb_record *rec; + struct socket_address *src; + const char *reg_address; + enum wrepl_name_type new_type; + struct wins_challenge_io io; + NTSTATUS status; +}; + +static int nbtd_wins_wack_state_destructor(struct nbtd_wins_wack_state *s) +{ + DLIST_REMOVE(s->iface->wack_queue, s); + return 0; +} + +static bool wins_check_wack_queue(struct nbtd_interface *iface, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + struct nbtd_wins_wack_state *s; + + for (s= iface->wack_queue; s; s = s->next) { + if (packet->name_trn_id != s->request_packet->name_trn_id) { + continue; + } + if (packet->operation != s->request_packet->operation) { + continue; + } + if (src->port != s->src->port) { + continue; + } + if (strcmp(src->addr, s->src->addr) != 0) { + continue; + } + + return true; + } + + return false; +} + +/* + deny a registration request +*/ +static void wins_wack_deny(struct nbtd_wins_wack_state *s) +{ + nbtd_name_registration_reply(s->nbtsock, s->request_packet, + s->src, NBT_RCODE_ACT); + DEBUG(4,("WINS: denied name registration request for %s from %s:%d\n", + nbt_name_string(s, s->rec->name), s->src->addr, s->src->port)); + talloc_free(s); +} + +/* + allow a registration request +*/ +static void wins_wack_allow(struct nbtd_wins_wack_state *s) +{ + NTSTATUS status; + uint32_t ttl = wins_server_ttl(s->winssrv, s->request_packet->additional[0].ttl); + struct winsdb_record *rec = s->rec, *rec2; + uint32_t i,j; + + status = winsdb_lookup(s->winssrv->wins_db, rec->name, s, &rec2); + if (!NT_STATUS_IS_OK(status) || + rec2->version != rec->version || + strcmp(rec2->wins_owner, rec->wins_owner) != 0) { + DEBUG(5,("WINS: record %s changed during WACK - failing registration\n", + nbt_name_string(s, rec->name))); + wins_wack_deny(s); + return; + } + + /* + * if the old name owner doesn't hold the name anymore + * handle the request as new registration for the new name owner + */ + if (!NT_STATUS_IS_OK(s->status)) { + uint8_t rcode; + + winsdb_delete(s->winssrv->wins_db, rec); + rcode = wins_register_new(s->nbtsock, s->request_packet, s->src, s->new_type); + if (rcode != NBT_RCODE_OK) { + DEBUG(1,("WINS: record %s failed to register as new during WACK\n", + nbt_name_string(s, rec->name))); + wins_wack_deny(s); + return; + } + goto done; + } + + rec->expire_time = time(NULL) + ttl; + rec->registered_by = s->src->addr; + + /* + * now remove all addresses that the client doesn't hold anymore + * and update the time stamp and owner for the ones that are still there + */ + for (i=0; rec->addresses[i]; i++) { + bool found = false; + for (j=0; j < s->io.out.num_addresses; j++) { + if (strcmp(rec->addresses[i]->address, s->io.out.addresses[j]) != 0) continue; + + found = true; + break; + } + if (found) { + rec->addresses = winsdb_addr_list_add(s->winssrv->wins_db, + rec, rec->addresses, + s->reg_address, + s->winssrv->wins_db->local_owner, + rec->expire_time, + true); + if (rec->addresses == NULL) goto failed; + continue; + } + + winsdb_addr_list_remove(rec->addresses, rec->addresses[i]->address); + } + + rec->addresses = winsdb_addr_list_add(s->winssrv->wins_db, + rec, rec->addresses, + s->reg_address, + s->winssrv->wins_db->local_owner, + rec->expire_time, + true); + if (rec->addresses == NULL) goto failed; + + /* if we have more than one address, this becomes implicit a MHOMED record */ + if (winsdb_addr_list_length(rec->addresses) > 1) { + rec->type = WREPL_TYPE_MHOMED; + } + + winsdb_modify(s->winssrv->wins_db, rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP); + + DEBUG(4,("WINS: accepted registration of %s with address %s\n", + nbt_name_string(s, rec->name), s->reg_address)); + +done: + nbtd_name_registration_reply(s->nbtsock, s->request_packet, + s->src, NBT_RCODE_OK); +failed: + talloc_free(s); +} + +/* + called when a name query to a current owner completes +*/ +static void wack_wins_challenge_handler(struct composite_context *c_req) +{ + struct nbtd_wins_wack_state *s = talloc_get_type(c_req->async.private_data, + struct nbtd_wins_wack_state); + bool found; + uint32_t i; + + s->status = wins_challenge_recv(c_req, s, &s->io); + + /* + * if the owner denies it holds the name, then allow + * the registration + */ + if (!NT_STATUS_IS_OK(s->status)) { + wins_wack_allow(s); + return; + } + + if (s->new_type == WREPL_TYPE_GROUP || s->new_type == WREPL_TYPE_SGROUP) { + DEBUG(1,("WINS: record %s failed to register as group type(%u) during WACK, it's still type(%u)\n", + nbt_name_string(s, s->rec->name), s->new_type, s->rec->type)); + wins_wack_deny(s); + return; + } + + /* + * if the owner still wants the name and doesn't reply + * with the address trying to be registered, then deny + * the registration + */ + found = false; + for (i=0; i < s->io.out.num_addresses; i++) { + if (strcmp(s->reg_address, s->io.out.addresses[i]) != 0) continue; + + found = true; + break; + } + if (!found) { + wins_wack_deny(s); + return; + } + + wins_wack_allow(s); + return; +} + + +/* + a client has asked to register a unique name that someone else owns. We + need to ask each of the current owners if they still want it. If they do + then reject the registration, otherwise allow it +*/ +static void wins_register_wack(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct winsdb_record *rec, + struct socket_address *src, + enum wrepl_name_type new_type) +{ + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + struct nbtd_wins_wack_state *s; + struct composite_context *c_req; + uint32_t ttl; + + s = talloc_zero(nbtsock, struct nbtd_wins_wack_state); + if (s == NULL) goto failed; + + /* package up the state variables for this wack request */ + s->winssrv = winssrv; + s->nbtsock = nbtsock; + s->iface = iface; + s->request_packet = talloc_steal(s, packet); + s->rec = talloc_steal(s, rec); + s->reg_address = packet->additional[0].rdata.netbios.addresses[0].ipaddr; + s->new_type = new_type; + s->src = socket_address_copy(s, src); + if (s->src == NULL) goto failed; + + s->io.in.nbtd_server = iface->nbtsrv; + s->io.in.nbt_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx); + s->io.in.event_ctx = iface->nbtsrv->task->event_ctx; + s->io.in.name = rec->name; + s->io.in.num_addresses = winsdb_addr_list_length(rec->addresses); + s->io.in.addresses = winsdb_addr_string_list(s, rec->addresses); + if (s->io.in.addresses == NULL) goto failed; + + DLIST_ADD_END(iface->wack_queue, s); + + talloc_set_destructor(s, nbtd_wins_wack_state_destructor); + + /* + * send a WACK to the client, specifying the maximum time it could + * take to check with the owner, plus some slack + */ + ttl = 5 + 4 * winsdb_addr_list_length(rec->addresses); + nbtd_wack_reply(nbtsock, packet, src, ttl); + + /* + * send the challenge to the old addresses + */ + c_req = wins_challenge_send(s, &s->io); + if (c_req == NULL) goto failed; + + c_req->async.fn = wack_wins_challenge_handler; + c_req->async.private_data = s; + return; + +failed: + talloc_free(s); + nbtd_name_registration_reply(nbtsock, packet, src, NBT_RCODE_SVR); +} + +/* + register a name +*/ +static void nbtd_winsserver_register(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + NTSTATUS status; + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + struct nbt_name *name = &packet->questions[0].name; + struct winsdb_record *rec; + uint8_t rcode = NBT_RCODE_OK; + uint16_t nb_flags = packet->additional[0].rdata.netbios.addresses[0].nb_flags; + const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr; + bool mhomed = ((packet->operation & NBT_OPCODE) == NBT_OPCODE_MULTI_HOME_REG); + enum wrepl_name_type new_type = wrepl_type(nb_flags, name, mhomed); + struct winsdb_addr *winsdb_addr = NULL; + bool duplicate_packet; + + /* + * as a special case, the local master browser name is always accepted + * for registration, but never stored, but w2k3 stores it if it's registered + * as a group name, (but a query for the 0x1D name still returns not found!) + */ + if (name->type == NBT_NAME_MASTER && !(nb_flags & NBT_NM_GROUP)) { + rcode = NBT_RCODE_OK; + goto done; + } + + /* w2k3 refuses 0x1B names with marked as group */ + if (name->type == NBT_NAME_PDC && (nb_flags & NBT_NM_GROUP)) { + rcode = NBT_RCODE_RFS; + goto done; + } + + /* w2k3 refuses 0x1C names with out marked as group */ + if (name->type == NBT_NAME_LOGON && !(nb_flags & NBT_NM_GROUP)) { + rcode = NBT_RCODE_RFS; + goto done; + } + + /* w2k3 refuses 0x1E names with out marked as group */ + if (name->type == NBT_NAME_BROWSER && !(nb_flags & NBT_NM_GROUP)) { + rcode = NBT_RCODE_RFS; + goto done; + } + + if (name->scope && strlen(name->scope) > 237) { + rcode = NBT_RCODE_SVR; + goto done; + } + + duplicate_packet = wins_check_wack_queue(iface, packet, src); + if (duplicate_packet) { + /* just ignore the packet */ + DEBUG(5,("Ignoring duplicate packet while WACK is pending from %s:%d\n", + src->addr, src->port)); + return; + } + + status = winsdb_lookup(winssrv->wins_db, name, packet, &rec); + if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) { + rcode = wins_register_new(nbtsock, packet, src, new_type); + goto done; + } else if (!NT_STATUS_IS_OK(status)) { + rcode = NBT_RCODE_SVR; + goto done; + } else if (rec->is_static) { + if (rec->type == WREPL_TYPE_GROUP || rec->type == WREPL_TYPE_SGROUP) { + rcode = NBT_RCODE_OK; + goto done; + } + rcode = NBT_RCODE_ACT; + goto done; + } + + if (rec->type == WREPL_TYPE_GROUP) { + if (new_type != WREPL_TYPE_GROUP) { + DEBUG(2,("WINS: Attempt to register name %s as non normal group(%u)" + " while a normal group is already there\n", + nbt_name_string(packet, name), new_type)); + rcode = NBT_RCODE_ACT; + goto done; + } + + if (rec->state == WREPL_STATE_ACTIVE) { + /* TODO: is this correct? */ + rcode = wins_update_ttl(nbtsock, packet, rec, NULL, src); + goto done; + } + + /* TODO: is this correct? */ + winsdb_delete(winssrv->wins_db, rec); + rcode = wins_register_new(nbtsock, packet, src, new_type); + goto done; + } + + if (rec->state != WREPL_STATE_ACTIVE) { + winsdb_delete(winssrv->wins_db, rec); + rcode = wins_register_new(nbtsock, packet, src, new_type); + goto done; + } + + switch (rec->type) { + case WREPL_TYPE_UNIQUE: + case WREPL_TYPE_MHOMED: + /* + * if its an active unique name, and the registration is for a group, then + * see if the unique name owner still wants the name + * TODO: is this correct? + */ + if (new_type == WREPL_TYPE_GROUP || new_type == WREPL_TYPE_GROUP) { + wins_register_wack(nbtsock, packet, rec, src, new_type); + return; + } + + /* + * if the registration is for an address that is currently active, then + * just update the expiry time of the record and the address + */ + winsdb_addr = winsdb_addr_list_check(rec->addresses, address); + if (winsdb_addr) { + rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src); + goto done; + } + + /* + * we have to do a WACK to see if the current owner is willing + * to give up its claim + */ + wins_register_wack(nbtsock, packet, rec, src, new_type); + return; + + case WREPL_TYPE_GROUP: + /* this should not be reached as normal groups are handled above */ + DEBUG(0,("BUG at %s\n",__location__)); + rcode = NBT_RCODE_ACT; + goto done; + + case WREPL_TYPE_SGROUP: + /* if the new record isn't also a special group, refuse the registration */ + if (new_type != WREPL_TYPE_SGROUP) { + DEBUG(2,("WINS: Attempt to register name %s as non special group(%u)" + " while a special group is already there\n", + nbt_name_string(packet, name), new_type)); + rcode = NBT_RCODE_ACT; + goto done; + } + + /* + * if the registration is for an address that is currently active, then + * just update the expiry time of the record and the address + */ + winsdb_addr = winsdb_addr_list_check(rec->addresses, address); + if (winsdb_addr) { + rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src); + goto done; + } + + rcode = wins_sgroup_merge(nbtsock, packet, rec, address, src); + goto done; + } + +done: + nbtd_name_registration_reply(nbtsock, packet, src, rcode); +} + +static uint32_t ipv4_match_bits(struct in_addr ip1, struct in_addr ip2) +{ + uint32_t i, j, match=0; + uint8_t *p1, *p2; + + p1 = (uint8_t *)&ip1.s_addr; + p2 = (uint8_t *)&ip2.s_addr; + + for (i=0; i<4; i++) { + if (p1[i] != p2[i]) break; + match += 8; + } + + if (i==4) return match; + + for (j=0; j<8; j++) { + if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j)))) + break; + match++; + } + + return match; +} + +static int nbtd_wins_randomize1Clist_sort(void *p1,/* (const char **) */ + void *p2,/* (const char **) */ + struct socket_address *src) +{ + const char *a1 = (const char *)*(const char **)p1; + const char *a2 = (const char *)*(const char **)p2; + uint32_t match_bits1; + uint32_t match_bits2; + + match_bits1 = ipv4_match_bits(interpret_addr2(a1), interpret_addr2(src->addr)); + match_bits2 = ipv4_match_bits(interpret_addr2(a2), interpret_addr2(src->addr)); + + return match_bits2 - match_bits1; +} + +static void nbtd_wins_randomize1Clist(struct loadparm_context *lp_ctx, + const char **addresses, struct socket_address *src) +{ + const char *mask; + const char *tmp; + uint32_t num_addrs; + uint32_t idx, sidx; + int r; + + for (num_addrs=0; addresses[num_addrs]; num_addrs++) { /* noop */ } + + if (num_addrs <= 1) return; /* nothing to do */ + + /* first sort the addresses depending on the matching to the client */ + LDB_TYPESAFE_QSORT(addresses, num_addrs, src, nbtd_wins_randomize1Clist_sort); + + mask = lpcfg_parm_string(lp_ctx, NULL, "nbtd", "wins_randomize1Clist_mask"); + if (!mask) { + mask = "255.255.255.0"; + } + + /* + * choose a random address to be the first in the response to the client, + * prefer the addresses inside the nbtd:wins_randomize1Clist_mask netmask + */ + r = random(); + idx = sidx = r % num_addrs; + + while (1) { + bool same; + + /* if the current one is in the same subnet, use it */ + same = iface_list_same_net(addresses[idx], src->addr, mask); + if (same) { + sidx = idx; + break; + } + + /* we need to check for idx == 0, after checking for the same net */ + if (idx == 0) break; + /* + * if we haven't found an address in the same subnet, search in ones + * which match the client more + * + * some notes: + * + * it's not "idx = idx % r" but "idx = r % idx" + * because in "a % b" b is the allowed range + * and b-1 is the maximum possible result, so it must be decreasing + * and the above idx == 0 check breaks the while(1) loop. + */ + idx = r % idx; + } + + /* note sidx == 0 is also valid here ... */ + tmp = addresses[0]; + addresses[0] = addresses[sidx]; + addresses[sidx] = tmp; +} + +/* + query a name +*/ +static void nbtd_winsserver_query(struct loadparm_context *lp_ctx, + struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + NTSTATUS status; + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + struct nbt_name *name = &packet->questions[0].name; + struct winsdb_record *rec; + struct winsdb_record *rec_1b = NULL; + const char **addresses; + const char **addresses_1b = NULL; + uint16_t nb_flags = 0; + + if (name->type == NBT_NAME_MASTER) { + goto notfound; + } + + /* + * w2k3 returns the first address of the 0x1B record as first address + * to a 0x1C query + * + * since Windows 2000 Service Pack 2 there's on option to trigger this behavior: + * + * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Prepend1BTo1CQueries + * Typ: Daten REG_DWORD + * Value: 0 = deactivated, 1 = activated + */ + if (name->type == NBT_NAME_LOGON && + lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_prepend1Bto1Cqueries", true)) { + struct nbt_name name_1b; + + name_1b = *name; + name_1b.type = NBT_NAME_PDC; + + status = winsdb_lookup(winssrv->wins_db, &name_1b, packet, &rec_1b); + if (NT_STATUS_IS_OK(status)) { + addresses_1b = winsdb_addr_string_list(packet, rec_1b->addresses); + } + } + + status = winsdb_lookup(winssrv->wins_db, name, packet, &rec); + if (!NT_STATUS_IS_OK(status)) { + if (!lpcfg_wins_dns_proxy(lp_ctx)) { + goto notfound; + } + + if (name->type != NBT_NAME_CLIENT && name->type != NBT_NAME_SERVER) { + goto notfound; + } + + nbtd_wins_dns_proxy_query(nbtsock, packet, src); + return; + } + + /* + * for group's we always reply with + * 255.255.255.255 as address, even if + * the record is released or tombstoned + */ + if (rec->type == WREPL_TYPE_GROUP) { + addresses = str_list_add(NULL, "255.255.255.255"); + talloc_steal(packet, addresses); + if (!addresses) { + goto notfound; + } + nb_flags |= NBT_NM_GROUP; + goto found; + } + + if (rec->state != WREPL_STATE_ACTIVE) { + goto notfound; + } + + addresses = winsdb_addr_string_list(packet, rec->addresses); + if (!addresses) { + goto notfound; + } + + /* + * if addresses_1b isn't NULL, we have a 0x1C query and need to return the + * first 0x1B address as first address + */ + if (addresses_1b && addresses_1b[0]) { + const char **addresses_1c = addresses; + uint32_t i; + uint32_t num_addrs; + + addresses = str_list_add(NULL, addresses_1b[0]); + if (!addresses) { + goto notfound; + } + talloc_steal(packet, addresses); + num_addrs = 1; + + for (i=0; addresses_1c[i]; i++) { + if (strcmp(addresses_1b[0], addresses_1c[i]) == 0) continue; + + /* + * stop when we already have 25 addresses + */ + if (num_addrs >= 25) break; + + num_addrs++; + addresses = str_list_add(addresses, addresses_1c[i]); + if (!addresses) { + goto notfound; + } + } + } + + if (rec->type == WREPL_TYPE_SGROUP) { + nb_flags |= NBT_NM_GROUP; + } else { + nb_flags |= (rec->node <<13); + } + + /* + * since Windows 2000 Service Pack 2 there's on option to trigger this behavior: + * + * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Randomize1CList + * Typ: Daten REG_DWORD + * Value: 0 = deactivated, 1 = activated + */ + if (name->type == NBT_NAME_LOGON && + lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_randomize1Clist", false)) { + nbtd_wins_randomize1Clist(lp_ctx, addresses, src); + } + +found: + nbtd_name_query_reply(nbtsock, packet, src, name, + 0, nb_flags, addresses); + return; + +notfound: + nbtd_negative_name_query_reply(nbtsock, packet, src); +} + +/* + release a name +*/ +static void nbtd_winsserver_release(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + NTSTATUS status; + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + struct nbt_name *name = &packet->questions[0].name; + struct winsdb_record *rec; + uint32_t modify_flags = 0; + uint8_t ret; + + if (name->type == NBT_NAME_MASTER) { + goto done; + } + + if (name->scope && strlen(name->scope) > 237) { + goto done; + } + + status = winsdb_lookup(winssrv->wins_db, name, packet, &rec); + if (!NT_STATUS_IS_OK(status)) { + goto done; + } + + if (rec->is_static) { + if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_MHOMED) { + goto done; + } + nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_ACT); + return; + } + + if (rec->state != WREPL_STATE_ACTIVE) { + goto done; + } + + /* + * TODO: do we need to check if + * src->addr matches packet->additional[0].rdata.netbios.addresses[0].ipaddr + * here? + */ + + /* + * we only allow releases from an owner - other releases are + * silently ignored + */ + if (!winsdb_addr_list_check(rec->addresses, src->addr)) { + int i; + DEBUG(4,("WINS: silently ignoring attempted name release on %s from %s\n", nbt_name_string(rec, rec->name), src->addr)); + DEBUGADD(4, ("Registered Addresses: \n")); + for (i=0; rec->addresses && rec->addresses[i]; i++) { + DEBUGADD(4, ("%s\n", rec->addresses[i]->address)); + } + goto done; + } + + DEBUG(4,("WINS: released name %s from %s\n", nbt_name_string(rec, rec->name), src->addr)); + + switch (rec->type) { + case WREPL_TYPE_UNIQUE: + rec->state = WREPL_STATE_RELEASED; + break; + + case WREPL_TYPE_GROUP: + rec->state = WREPL_STATE_RELEASED; + break; + + case WREPL_TYPE_SGROUP: + winsdb_addr_list_remove(rec->addresses, src->addr); + /* TODO: do we need to take the ownership here? */ + if (winsdb_addr_list_length(rec->addresses) == 0) { + rec->state = WREPL_STATE_RELEASED; + } + break; + + case WREPL_TYPE_MHOMED: + winsdb_addr_list_remove(rec->addresses, src->addr); + /* TODO: do we need to take the ownership here? */ + if (winsdb_addr_list_length(rec->addresses) == 0) { + rec->state = WREPL_STATE_RELEASED; + } + break; + } + + if (rec->state == WREPL_STATE_ACTIVE) { + /* + * If the record is still active, we need to update the + * expire_time. + * + * if we're not the owner, we need to take the ownership. + */ + rec->expire_time= time(NULL) + winssrv->config.max_renew_interval; + if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) != 0) { + modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP; + } + if (lpcfg_parm_bool(iface->nbtsrv->task->lp_ctx, NULL, "wreplsrv", "propagate name releases", false)) { + /* + * We have an option to propagate every name release, + * this is off by default to match windows servers + */ + modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP; + } + } else if (rec->state == WREPL_STATE_RELEASED) { + /* + * if we're not the owner, we need to take the owner ship + * and make the record tombstone, but expire after + * tombstone_interval + tombstone_timeout and not only after tombstone_timeout + * like for normal tombstone records. + * This is to replicate the record directly to the original owner, + * where the record is still active + */ + if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) == 0) { + rec->expire_time= time(NULL) + winssrv->config.tombstone_interval; + } else { + rec->state = WREPL_STATE_TOMBSTONE; + rec->expire_time= time(NULL) + + winssrv->config.tombstone_interval + + winssrv->config.tombstone_timeout; + modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP; + } + } + + ret = winsdb_modify(winssrv->wins_db, rec, modify_flags); + if (ret != NBT_RCODE_OK) { + DEBUG(1,("WINS: FAILED: released name %s at %s: error:%u\n", + nbt_name_string(rec, rec->name), src->addr, ret)); + } +done: + /* we match w2k3 by always giving a positive reply to name releases. */ + nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_OK); +} + + +/* + answer a name query +*/ +void nbtd_winsserver_request(struct nbt_name_socket *nbtsock, + struct nbt_name_packet *packet, + struct socket_address *src) +{ + struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data, + struct nbtd_interface); + struct wins_server *winssrv = iface->nbtsrv->winssrv; + if ((packet->operation & NBT_FLAG_BROADCAST) || winssrv == NULL) { + return; + } + + switch (packet->operation & NBT_OPCODE) { + case NBT_OPCODE_QUERY: + nbtd_winsserver_query(iface->nbtsrv->task->lp_ctx, nbtsock, packet, src); + break; + + case NBT_OPCODE_REGISTER: + case NBT_OPCODE_REFRESH: + case NBT_OPCODE_REFRESH2: + case NBT_OPCODE_MULTI_HOME_REG: + nbtd_winsserver_register(nbtsock, packet, src); + break; + + case NBT_OPCODE_RELEASE: + nbtd_winsserver_release(nbtsock, packet, src); + break; + } + +} + +/* + startup the WINS server, if configured +*/ +NTSTATUS nbtd_winsserver_init(struct nbtd_server *nbtsrv) +{ + uint32_t tmp; + const char *owner; + + if (!lpcfg_we_are_a_wins_server(nbtsrv->task->lp_ctx)) { + nbtsrv->winssrv = NULL; + return NT_STATUS_OK; + } + + nbtsrv->winssrv = talloc_zero(nbtsrv, struct wins_server); + NT_STATUS_HAVE_NO_MEMORY(nbtsrv->winssrv); + + nbtsrv->winssrv->config.max_renew_interval = lpcfg_max_wins_ttl(nbtsrv->task->lp_ctx); + nbtsrv->winssrv->config.min_renew_interval = lpcfg_min_wins_ttl(nbtsrv->task->lp_ctx); + tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv", "tombstone_interval", 6*24*60*60); + nbtsrv->winssrv->config.tombstone_interval = tmp; + tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv"," tombstone_timeout", 1*24*60*60); + nbtsrv->winssrv->config.tombstone_timeout = tmp; + + owner = lpcfg_parm_string(nbtsrv->task->lp_ctx, NULL, "winsdb", "local_owner"); + + if (owner == NULL) { + struct interface *ifaces; + load_interface_list(nbtsrv->task, nbtsrv->task->lp_ctx, &ifaces); + owner = iface_list_first_v4(ifaces); + } + + nbtsrv->winssrv->wins_db = winsdb_connect(nbtsrv->winssrv, nbtsrv->task->event_ctx, + nbtsrv->task->lp_ctx, + owner, WINSDB_HANDLE_CALLER_NBTD); + if (!nbtsrv->winssrv->wins_db) { + return NT_STATUS_INTERNAL_DB_ERROR; + } + + irpc_add_name(nbtsrv->task->msg_ctx, "wins_server"); + + return NT_STATUS_OK; +} diff --git a/source4/nbt_server/wins/winsserver.h b/source4/nbt_server/wins/winsserver.h new file mode 100644 index 0000000..803a9e2 --- /dev/null +++ b/source4/nbt_server/wins/winsserver.h @@ -0,0 +1,67 @@ +/* + Unix SMB/CIFS implementation. + + wins server WACK processing + + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +struct wins_server { + /* wins server database handle */ + struct winsdb_handle *wins_db; + + /* some configuration */ + struct { + /* + * the interval (in secs) till an active record will be marked as RELEASED + */ + uint32_t min_renew_interval; + uint32_t max_renew_interval; + + /* + * the interval (in secs) a record remains in RELEASED state, + * before it will be marked as TOMBSTONE + * (also known as extinction interval) + */ + uint32_t tombstone_interval; + + /* + * the interval (in secs) a record remains in TOMBSTONE state, + * before it will be removed from the database. + * See also 'tombstone_extra_timeout'. + * (also known as extinction timeout) + */ + uint32_t tombstone_timeout; + } config; +}; + +struct wins_challenge_io { + struct { + struct nbtd_server *nbtd_server; + uint16_t nbt_port; + struct tevent_context *event_ctx; + struct nbt_name *name; + uint32_t num_addresses; + const char **addresses; + } in; + struct { + uint32_t num_addresses; + const char **addresses; + } out; +}; + +struct composite_context; +#include "nbt_server/wins/winsserver_proto.h" diff --git a/source4/nbt_server/wins/winswack.c b/source4/nbt_server/wins/winswack.c new file mode 100644 index 0000000..735a4e6 --- /dev/null +++ b/source4/nbt_server/wins/winswack.c @@ -0,0 +1,387 @@ +/* + Unix SMB/CIFS implementation. + + "secure" wins server WACK processing + + Copyright (C) Andrew Tridgell 2005 + Copyright (C) Stefan Metzmacher 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "nbt_server/nbt_server.h" +#include "nbt_server/wins/winsdb.h" +#include "nbt_server/wins/winsserver.h" +#include "system/time.h" +#include "libcli/composite/composite.h" +#include "param/param.h" +#include "samba/service_task.h" + +struct wins_challenge_state { + struct wins_challenge_io *io; + uint32_t current_address; + struct nbt_name_query query; +}; + +static void wins_challenge_handler(struct nbt_name_request *req) +{ + struct composite_context *ctx = talloc_get_type(req->async.private_data, struct composite_context); + struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state); + + ctx->status = nbt_name_query_recv(req, state, &state->query); + + /* if we timed out then try the next owner address, if any */ + if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_IO_TIMEOUT)) { + state->current_address++; + if (state->current_address < state->io->in.num_addresses) { + struct nbtd_interface *iface; + + state->query.in.dest_port = state->io->in.nbt_port; + state->query.in.dest_addr = state->io->in.addresses[state->current_address]; + + iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->query.in.dest_addr, true); + if (!iface) { + composite_error(ctx, NT_STATUS_INTERNAL_ERROR); + return; + } + + ZERO_STRUCT(state->query.out); + req = nbt_name_query_send(iface->nbtsock, &state->query); + composite_continue_nbt(ctx, req, wins_challenge_handler, ctx); + return; + } + } + + composite_done(ctx); +} + +NTSTATUS wins_challenge_recv(struct composite_context *ctx, TALLOC_CTX *mem_ctx, struct wins_challenge_io *io) +{ + NTSTATUS status = ctx->status; + struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state); + + if (NT_STATUS_IS_OK(status)) { + io->out.num_addresses = state->query.out.num_addrs; + io->out.addresses = state->query.out.reply_addrs; + talloc_steal(mem_ctx, io->out.addresses); + } else { + ZERO_STRUCT(io->out); + } + + talloc_free(ctx); + return status; +} + +struct composite_context *wins_challenge_send(TALLOC_CTX *mem_ctx, struct wins_challenge_io *io) +{ + struct composite_context *result; + struct wins_challenge_state *state; + struct nbt_name_request *req; + struct nbtd_interface *iface; + + result = talloc_zero(mem_ctx, struct composite_context); + if (result == NULL) return NULL; + result->state = COMPOSITE_STATE_IN_PROGRESS; + result->event_ctx = io->in.event_ctx; + + state = talloc_zero(result, struct wins_challenge_state); + if (state == NULL) goto failed; + result->private_data = state; + + /* package up the state variables for this wack request */ + state->io = io; + state->current_address = 0; + + /* setup a name query to the first address */ + state->query.in.name = *state->io->in.name; + state->query.in.dest_port = state->io->in.nbt_port; + state->query.in.dest_addr = state->io->in.addresses[state->current_address]; + state->query.in.broadcast = false; + state->query.in.wins_lookup = true; + state->query.in.timeout = 1; + state->query.in.retries = 2; + ZERO_STRUCT(state->query.out); + + iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->query.in.dest_addr, true); + if (!iface) { + goto failed; + } + + req = nbt_name_query_send(iface->nbtsock, &state->query); + if (req == NULL) goto failed; + + req->async.fn = wins_challenge_handler; + req->async.private_data = result; + + return result; +failed: + talloc_free(result); + return NULL; +} + +struct wins_release_demand_io { + struct { + struct nbtd_server *nbtd_server; + struct tevent_context *event_ctx; + struct nbt_name *name; + uint16_t nb_flags; + uint32_t num_addresses; + const char **addresses; + } in; +}; + +struct wins_release_demand_state { + struct wins_release_demand_io *io; + uint32_t current_address; + uint32_t addresses_left; + struct nbt_name_release release; +}; + +static void wins_release_demand_handler(struct nbt_name_request *req) +{ + struct composite_context *ctx = talloc_get_type(req->async.private_data, struct composite_context); + struct wins_release_demand_state *state = talloc_get_type(ctx->private_data, struct wins_release_demand_state); + + ctx->status = nbt_name_release_recv(req, state, &state->release); + + /* if we timed out then try the next owner address, if any */ + if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_IO_TIMEOUT)) { + state->current_address++; + state->addresses_left--; + if (state->current_address < state->io->in.num_addresses) { + struct nbtd_interface *iface; + + state->release.in.dest_port = lpcfg_nbt_port(state->io->in.nbtd_server->task->lp_ctx); + state->release.in.dest_addr = state->io->in.addresses[state->current_address]; + state->release.in.address = state->release.in.dest_addr; + state->release.in.timeout = (state->addresses_left > 1 ? 2 : 1); + state->release.in.retries = (state->addresses_left > 1 ? 0 : 2); + + iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->release.in.dest_addr, true); + if (!iface) { + composite_error(ctx, NT_STATUS_INTERNAL_ERROR); + return; + } + + ZERO_STRUCT(state->release.out); + req = nbt_name_release_send(iface->nbtsock, &state->release); + composite_continue_nbt(ctx, req, wins_release_demand_handler, ctx); + return; + } + } + + composite_done(ctx); +} + +static NTSTATUS wins_release_demand_recv(struct composite_context *ctx, + TALLOC_CTX *mem_ctx, + struct wins_release_demand_io *io) +{ + NTSTATUS status = ctx->status; + talloc_free(ctx); + return status; +} + +static struct composite_context *wins_release_demand_send(TALLOC_CTX *mem_ctx, struct wins_release_demand_io *io) +{ + struct composite_context *result; + struct wins_release_demand_state *state; + struct nbt_name_request *req; + struct nbtd_interface *iface; + + result = talloc_zero(mem_ctx, struct composite_context); + if (result == NULL) return NULL; + result->state = COMPOSITE_STATE_IN_PROGRESS; + result->event_ctx = io->in.event_ctx; + + state = talloc_zero(result, struct wins_release_demand_state); + if (state == NULL) goto failed; + result->private_data = state; + + /* package up the state variables for this wack request */ + state->io = io; + state->current_address = 0; + state->addresses_left = state->io->in.num_addresses; + + /* + * setup a name query to the first address + * - if we have more than one address try the first + * with 2 secs timeout and no retry + * - otherwise use 1 sec timeout (w2k3 uses 0.5 sec here) + * with 2 retries + */ + state->release.in.name = *state->io->in.name; + state->release.in.dest_port = lpcfg_nbt_port(state->io->in.nbtd_server->task->lp_ctx); + state->release.in.dest_addr = state->io->in.addresses[state->current_address]; + state->release.in.address = state->release.in.dest_addr; + state->release.in.broadcast = false; + state->release.in.timeout = (state->addresses_left > 1 ? 2 : 1); + state->release.in.retries = (state->addresses_left > 1 ? 0 : 2); + ZERO_STRUCT(state->release.out); + + iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->release.in.dest_addr, true); + if (!iface) { + goto failed; + } + + req = nbt_name_release_send(iface->nbtsock, &state->release); + if (req == NULL) goto failed; + + req->async.fn = wins_release_demand_handler; + req->async.private_data = result; + + return result; +failed: + talloc_free(result); + return NULL; +} + +/* + wrepl_server needs to be able to do a name query request, but some windows + servers always send the reply to port 137, regardless of the request + port. To cope with this we use a irpc request to the NBT server + which has port 137 open, and thus can receive the replies +*/ +struct proxy_wins_challenge_state { + struct irpc_message *msg; + struct nbtd_proxy_wins_challenge *req; + struct wins_challenge_io io; + struct composite_context *c_req; +}; + +static void proxy_wins_challenge_handler(struct composite_context *c_req) +{ + NTSTATUS status; + uint32_t i; + struct proxy_wins_challenge_state *s = talloc_get_type(c_req->async.private_data, + struct proxy_wins_challenge_state); + + status = wins_challenge_recv(s->c_req, s, &s->io); + if (!NT_STATUS_IS_OK(status)) { + ZERO_STRUCT(s->req->out); + irpc_send_reply(s->msg, status); + return; + } + + s->req->out.num_addrs = s->io.out.num_addresses; + /* TODO: fix pidl to handle inline ipv4address arrays */ + s->req->out.addrs = talloc_array(s->msg, struct nbtd_proxy_wins_addr, + s->io.out.num_addresses); + if (!s->req->out.addrs) { + ZERO_STRUCT(s->req->out); + irpc_send_reply(s->msg, NT_STATUS_NO_MEMORY); + return; + } + for (i=0; i < s->io.out.num_addresses; i++) { + s->req->out.addrs[i].addr = talloc_steal(s->req->out.addrs, s->io.out.addresses[i]); + } + + irpc_send_reply(s->msg, status); +} + +NTSTATUS nbtd_proxy_wins_challenge(struct irpc_message *msg, + struct nbtd_proxy_wins_challenge *req) +{ + struct nbtd_server *nbtd_server = + talloc_get_type(msg->private_data, struct nbtd_server); + struct proxy_wins_challenge_state *s; + uint32_t i; + + s = talloc(msg, struct proxy_wins_challenge_state); + NT_STATUS_HAVE_NO_MEMORY(s); + + s->msg = msg; + s->req = req; + + s->io.in.nbtd_server = nbtd_server; + s->io.in.nbt_port = lpcfg_nbt_port(nbtd_server->task->lp_ctx); + s->io.in.event_ctx = nbtd_server->task->event_ctx; + s->io.in.name = &req->in.name; + s->io.in.num_addresses = req->in.num_addrs; + s->io.in.addresses = talloc_array(s, const char *, req->in.num_addrs); + NT_STATUS_HAVE_NO_MEMORY(s->io.in.addresses); + /* TODO: fix pidl to handle inline ipv4address arrays */ + for (i=0; i < req->in.num_addrs; i++) { + s->io.in.addresses[i] = talloc_steal(s->io.in.addresses, req->in.addrs[i].addr); + } + + s->c_req = wins_challenge_send(s, &s->io); + NT_STATUS_HAVE_NO_MEMORY(s->c_req); + + s->c_req->async.fn = proxy_wins_challenge_handler; + s->c_req->async.private_data = s; + + msg->defer_reply = true; + return NT_STATUS_OK; +} + +/* + wrepl_server needs to be able to do a name release demands, but some windows + servers always send the reply to port 137, regardless of the request + port. To cope with this we use a irpc request to the NBT server + which has port 137 open, and thus can receive the replies +*/ +struct proxy_wins_release_demand_state { + struct irpc_message *msg; + struct nbtd_proxy_wins_release_demand *req; + struct wins_release_demand_io io; + struct composite_context *c_req; +}; + +static void proxy_wins_release_demand_handler(struct composite_context *c_req) +{ + NTSTATUS status; + struct proxy_wins_release_demand_state *s = talloc_get_type(c_req->async.private_data, + struct proxy_wins_release_demand_state); + + status = wins_release_demand_recv(s->c_req, s, &s->io); + + irpc_send_reply(s->msg, status); +} + +NTSTATUS nbtd_proxy_wins_release_demand(struct irpc_message *msg, + struct nbtd_proxy_wins_release_demand *req) +{ + struct nbtd_server *nbtd_server = + talloc_get_type(msg->private_data, struct nbtd_server); + struct proxy_wins_release_demand_state *s; + uint32_t i; + + s = talloc(msg, struct proxy_wins_release_demand_state); + NT_STATUS_HAVE_NO_MEMORY(s); + + s->msg = msg; + s->req = req; + + s->io.in.nbtd_server = nbtd_server; + s->io.in.event_ctx = nbtd_server->task->event_ctx; + s->io.in.name = &req->in.name; + s->io.in.num_addresses = req->in.num_addrs; + s->io.in.addresses = talloc_array(s, const char *, req->in.num_addrs); + NT_STATUS_HAVE_NO_MEMORY(s->io.in.addresses); + /* TODO: fix pidl to handle inline ipv4address arrays */ + for (i=0; i < req->in.num_addrs; i++) { + s->io.in.addresses[i] = talloc_steal(s->io.in.addresses, req->in.addrs[i].addr); + } + + s->c_req = wins_release_demand_send(s, &s->io); + NT_STATUS_HAVE_NO_MEMORY(s->c_req); + + s->c_req->async.fn = proxy_wins_release_demand_handler; + s->c_req->async.private_data = s; + + msg->defer_reply = true; + return NT_STATUS_OK; +} |