diff options
author | Kai Blin <kai@samba.org> | 2010-10-23 19:27:26 -0700 |
---|---|---|
committer | Kai Blin <kai@samba.org> | 2010-10-28 07:25:16 +0000 |
commit | b73a05e4e184238614d73c1519f2880cd946908d (patch) | |
tree | 5405be11f54e857eeb9b74ca46121ea0b2aea34e /source4/samba_tool/drs | |
parent | 5634f55cc8d9eb0cca01f621939cee724271ecb0 (diff) | |
download | samba-b73a05e4e184238614d73c1519f2880cd946908d.tar.gz samba-b73a05e4e184238614d73c1519f2880cd946908d.tar.bz2 samba-b73a05e4e184238614d73c1519f2880cd946908d.zip |
s4 net: rename to samba-tool in order to not clash with s3 net
Autobuild-User: Kai Blin <kai@samba.org>
Autobuild-Date: Thu Oct 28 07:25:16 UTC 2010 on sn-devel-104
Diffstat (limited to 'source4/samba_tool/drs')
-rw-r--r-- | source4/samba_tool/drs/drs.c | 361 | ||||
-rw-r--r-- | source4/samba_tool/drs/drs.h | 87 | ||||
-rw-r--r-- | source4/samba_tool/drs/drs_bind.c | 136 | ||||
-rw-r--r-- | source4/samba_tool/drs/drs_kcc.c | 170 | ||||
-rw-r--r-- | source4/samba_tool/drs/drs_replicate.c | 252 | ||||
-rw-r--r-- | source4/samba_tool/drs/drs_showrepl.c | 605 |
6 files changed, 1611 insertions, 0 deletions
diff --git a/source4/samba_tool/drs/drs.c b/source4/samba_tool/drs/drs.c new file mode 100644 index 0000000000..78f8c144c2 --- /dev/null +++ b/source4/samba_tool/drs/drs.c @@ -0,0 +1,361 @@ +/* + Samba Unix/Linux SMB client library + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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 "samba_tool/samba_tool.h" +#include "librpc/gen_ndr/ndr_drsuapi_c.h" +#include "samba_tool/drs/drs.h" +#include "lib/ldb/include/ldb.h" +#include "ldb_wrap.h" +#include "system/filesys.h" + + +/** + * 'samba-tool drs' supported sub-commands + */ +static const struct net_functable net_drs_functable[] = { + { "bind", "Display replication features for a domain controller\n", net_drs_bind_cmd, net_drs_bind_usage }, + { "kcc", "Forces the KCC to recalculate replication topology for a specified domain controller\n", + net_drs_kcc_cmd, net_drs_kcc_usage }, + { "replicate", "Triggers replication event for the specified naming context between the source and destination domain controllers.\n", + net_drs_replicate_cmd, net_drs_replicate_usage }, + { "showrepl", "Displays the replication partners for each directory partition on the specified domain controller.\n", + net_drs_showrepl_cmd, net_drs_showrepl_usage }, + { NULL, NULL } +}; + +/** + * 'samba-tool drs' entry point + */ +int net_drs(struct net_context *ctx, int argc, const char **argv) +{ + return net_run_function(ctx, argc, argv, net_drs_functable, net_drs_usage); +} + +/** + * 'samba-tool drs' usage message + */ +int net_drs_usage(struct net_context *ctx, int argc, const char **argv) +{ + d_printf("samba-tool drs <command> [options]\n"); + d_printf("\n"); + d_printf("Currently implemented commands:\n"); + d_printf(" bind - Display DC replication features\n"); + d_printf(" kcc - Forces the KCC to recalculate replication topology for a specified domain controller\n"); + d_printf(" replicate - Triggers replication event for the specified naming context between the source and destination domain controllers.\n"); + d_printf(" showrepl - Displays the replication partners for each directory partition on the specified domain controller.\n"); + return 0; +} + +/** + * Create drsuapi connection to remote DC + * and fill-in DC capabilities + */ +static bool net_drs_DsBind(struct net_drs_context *drs_ctx, struct net_drs_connection *conn) +{ + NTSTATUS status; + struct GUID bind_guid; + struct drsuapi_DsBind req; + struct drsuapi_DsBindInfoCtr in_bind_ctr; + union drsuapi_DsBindInfo *bind_info; + + SMB_ASSERT(conn->binding != NULL); + + status = dcerpc_pipe_connect_b(conn, + &conn->drs_pipe, + conn->binding, + &ndr_table_drsuapi, + drs_ctx->net_ctx->credentials, + drs_ctx->net_ctx->event_ctx, + drs_ctx->net_ctx->lp_ctx); + if (!NT_STATUS_IS_OK(status)) { + d_printf("Failed to connect to server: %s\n", nt_errstr(status)); + return false; + } + conn->drs_handle = conn->drs_pipe->binding_handle; + + ZERO_STRUCT(in_bind_ctr); + in_bind_ctr.length = 48; + in_bind_ctr.info.info48.pid = (uint32_t)getpid(); + GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid); + req.in.bind_guid = &bind_guid; + req.in.bind_info = &in_bind_ctr; + req.out.bind_handle = &conn->bind_handle; + + status = dcerpc_drsuapi_DsBind_r(conn->drs_handle, conn, &req); + if (!NT_STATUS_IS_OK(status)) { + const char *errstr = nt_errstr(status); + d_printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr); + return false; + } else if (!W_ERROR_IS_OK(req.out.result)) { + d_printf("DsBind failed - %s\n", win_errstr(req.out.result)); + return false; + } + + /* fill-in remote DC capabilities */ + ZERO_STRUCT(conn->info48); + bind_info = &req.out.bind_info->info; + conn->bind_info_len = req.out.bind_info->length; + switch (conn->bind_info_len) { + case 48: + conn->info48.supported_extensions_ext = bind_info->info48.supported_extensions_ext; + conn->info48.config_dn_guid = bind_info->info48.config_dn_guid; + case 28: + conn->info48.repl_epoch = bind_info->info28.repl_epoch; + case 24: + conn->info48.supported_extensions = bind_info->info24.supported_extensions; + conn->info48.site_guid = bind_info->info24.site_guid; + conn->info48.pid = bind_info->info24.pid; + break; + default: + d_printf("Error: server returned BindInfo length %d", req.out.bind_info->length); + return false; + } + + return true; +} + +/** + * Close DRSUAPI connection to remote DC + */ +static bool net_drs_DsUnbind(struct net_drs_connection *conn) +{ + struct drsuapi_DsUnbind r; + struct policy_handle bind_handle; + + SMB_ASSERT(conn->drs_pipe); + + ZERO_STRUCT(r); + r.out.bind_handle = &bind_handle; + + r.in.bind_handle = &conn->bind_handle; + dcerpc_drsuapi_DsUnbind_r(conn->drs_handle, conn, &r); + + /* free dcerpc pipe in case we get called more than once */ + talloc_free(conn->drs_pipe); + conn->drs_pipe = NULL; + conn->drs_handle = NULL; + + return true; +} + +/** + * Destroy drsuapi connection + */ +static int net_drs_connection_destructor(struct net_drs_connection *conn) +{ + if (conn->drs_pipe) { + net_drs_DsUnbind(conn); + } + return 0; +} + +/** + * Create DRSUAPI connection to target DC + * @return ptr to net_drs_connection or NULL on failure + */ +struct net_drs_connection * net_drs_connect_dc(struct net_drs_context *drs_ctx, const char *dc_name) +{ + struct net_drs_connection *conn = NULL; + + conn = talloc_zero(drs_ctx, struct net_drs_connection); + NET_DRS_NOMEM_GOTO(conn, failed); + + /* init binding */ + conn->binding = talloc_zero(conn, struct dcerpc_binding); + conn->binding->transport = NCACN_IP_TCP; + conn->binding->flags = drs_ctx->drs_conn->binding->flags; + conn->binding->host = talloc_strdup(conn, dc_name); + conn->binding->target_hostname = conn->binding->host; + + if (!net_drs_DsBind(drs_ctx, conn)) { + goto failed; + } + + talloc_set_destructor(conn, net_drs_connection_destructor); + + return conn; + +failed: + talloc_free(conn); + return NULL; +} + +/** + * Open secured LDAP connection to remote DC + */ +static bool net_drs_ldap_connect(struct net_drs_context *drs_ctx) +{ + char *url; + bool bret = true; + + url = talloc_asprintf(drs_ctx, "ldap://%s/", drs_ctx->dc_name); + if (!url) { + d_printf(__location__ ": Have no memory"); + return false; + } + + drs_ctx->ldap.ldb = ldb_wrap_connect(drs_ctx, + drs_ctx->net_ctx->event_ctx, drs_ctx->net_ctx->lp_ctx, + url, + NULL, + drs_ctx->net_ctx->credentials, + 0); + if (drs_ctx->ldap.ldb == NULL) { + d_printf("Unable to connect to LDAP %s", url); + bret = false; + } + + talloc_free(url); + + return bret; +} + +/** + * fetch RootDSE record + */ +static bool net_drs_ldap_rootdse(struct net_drs_context *drs_ctx) +{ + int ret; + struct ldb_result *r; + struct ldb_dn *basedn; + static const char *attrs[] = { + "*", + NULL + }; + + SMB_ASSERT(drs_ctx->ldap.ldb != NULL); + + basedn = ldb_dn_new(drs_ctx, drs_ctx->ldap.ldb, NULL); + if (!basedn) { + d_printf(__location__ ": No memory"); + return false; + } + + ret = ldb_search(drs_ctx->ldap.ldb, drs_ctx, &r, + basedn, LDB_SCOPE_BASE, attrs, + "(objectClass=*)"); + talloc_free(basedn); + if (ret != LDB_SUCCESS) { + d_printf("RootDSE search failed: %s", ldb_errstring(drs_ctx->ldap.ldb)); + talloc_free(r); + return false; + } else if (r->count != 1) { + d_printf("RootDSE search returned more than one record!"); + talloc_free(r); + return false; + } + + drs_ctx->ldap.rootdse = r->msgs[0]; + + return true; +} + +/** + * parses binding from command line + * and gets target DC name + */ +static bool net_drs_parse_binding(struct net_drs_context *drs_ctx, const char *dc_binding) +{ + NTSTATUS status; + struct dcerpc_binding *b; + + status = dcerpc_parse_binding(drs_ctx->drs_conn, dc_binding, &b); + if (!NT_STATUS_IS_OK(status)) { + d_printf("Bad binding supplied %s\n", dc_binding); + return false; + } + + b->transport = NCACN_IP_TCP; + b->flags |= DCERPC_SIGN | DCERPC_SEAL; + + /* cache target DC name */ + drs_ctx->dc_name = b->target_hostname; + + drs_ctx->drs_conn->binding = b; + + return true; +} + +/** + * Free DRSUAPI connection upon net_drs_context + * destruction + */ +static int net_drs_context_destructor(struct net_drs_context *drs_ctx) +{ + if (drs_ctx->drs_conn && drs_ctx->drs_conn->drs_pipe) { + net_drs_DsUnbind(drs_ctx->drs_conn); + } + return 0; +} + +/** + * Create net_drs_context context to be used + * by 'samba-tool drs' sub-commands + */ +bool net_drs_create_context(struct net_context *net_ctx, + const char *dc_binding, + struct net_drs_context **_drs_ctx) +{ + struct net_drs_context *drs_ctx; + + drs_ctx = talloc_zero(net_ctx, struct net_drs_context); + if (!drs_ctx) { + d_printf(__location__ ": No memory"); + return false; + } + + drs_ctx->drs_conn = talloc_zero(drs_ctx, struct net_drs_connection); + if (!drs_ctx->drs_conn) { + d_printf(__location__ ": No memory"); + return false; + } + + drs_ctx->net_ctx = net_ctx; + + if (!net_drs_parse_binding(drs_ctx, dc_binding)) { + goto failed; + } + + /* LDAP connect */ + if (!net_drs_ldap_connect(drs_ctx)) { + goto failed; + } + /* fetch RootDSE */ + if (!net_drs_ldap_rootdse(drs_ctx)) { + goto failed; + } + + /* DRSUAPI connection */ + if (!net_drs_DsBind(drs_ctx, drs_ctx->drs_conn)) { + goto failed; + } + + /* set destructor to free any open connections */ + talloc_set_destructor(drs_ctx, net_drs_context_destructor); + + *_drs_ctx = drs_ctx; + return true; + +failed: + talloc_free(drs_ctx); + return false; +} diff --git a/source4/samba_tool/drs/drs.h b/source4/samba_tool/drs/drs.h new file mode 100644 index 0000000000..1e4cccc538 --- /dev/null +++ b/source4/samba_tool/drs/drs.h @@ -0,0 +1,87 @@ +/* + Unix SMB/CIFS implementation. + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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/>. +*/ + +#ifndef SAMBA_TOOL_DRS_H_ +#define SAMBA_TOOL_DRS_H_ + +#include "librpc/gen_ndr/ndr_drsuapi_c.h" + + +/** + * Check for critical error + */ +#define NET_DRS_CHECK_GOTO(_condition,_label,_msg) \ + do { \ + if (!(_condition)) { \ + d_printf(__location__": "#_condition" - %s\n", _msg); \ + goto _label; \ + } \ + } while (0) + +/** + * check allocated memory macro + */ +#define NET_DRS_NOMEM_GOTO(_ptr,_label) \ + NET_DRS_CHECK_GOTO(_ptr, _label, "Not enough memory!") + + +/** + * DRSUAPI binding context + */ +struct net_drs_connection { + /* DRSUAPI connection context */ + struct dcerpc_binding *binding; + struct dcerpc_pipe *drs_pipe; + struct dcerpc_binding_handle *drs_handle; + struct policy_handle bind_handle; + + /* length of bind info structure returned by remote DC + * 'net drs bind' command make use of this value */ + uint32_t bind_info_len; + + /* remote DC DRSUAPI capabilities */ + struct drsuapi_DsBindInfo48 info48; +}; + + +/** + * net drs commands context + */ +struct net_drs_context { + struct net_context *net_ctx; + + /* remote DC name supplied from command line */ + const char *dc_name; + + /* DRSUAPI connection to target DC */ + struct net_drs_connection *drs_conn; + + /* LDAP connection to DC */ + struct net_drs_ldap { + struct ldb_context *ldb; + const struct ldb_message *rootdse; + } ldap; +}; + + +#include "samba_tool/drs/drs_proto.h" + +#endif /* SAMBA_TOOL_DRS_H_ */ diff --git a/source4/samba_tool/drs/drs_bind.c b/source4/samba_tool/drs/drs_bind.c new file mode 100644 index 0000000000..cc2ad87df4 --- /dev/null +++ b/source4/samba_tool/drs/drs_bind.c @@ -0,0 +1,136 @@ +/* + Unix SMB/CIFS implementation. + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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 "samba_tool/samba_tool.h" +#include "samba_tool/drs/drs.h" + + +#define DEFINE_FLAG(_flag, _win_name) {_flag, #_flag, _win_name} + +struct drs_extension_flag { + uint32_t flag; + const char *samba_name; + const char *win_name; +}; + +static const struct drs_extension_flag drs_repl_flags[] = { + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_BASE, "DRS_EXT_BASE"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION, "DRS_EXT_ASYNCREPL"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI, "DRS_EXT_REMOVEAPI"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2, "DRS_EXT_MOVEREQ_V2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS, "DRS_EXT_GETCHG_DEFLATE"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1, "DRS_EXT_DCINFO_V1"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION, "DRS_EXT_RESTORE_USN_OPTIMIZATION"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY, "DRS_EXT_ADDENTRY"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE, "DRS_EXT_KCC_EXECUTE"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2, "DRS_EXT_ADDENTRY_V2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION, "DRS_EXT_LINKED_VALUE_REPLICATION"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2, "DRS_EXT_DCINFO_V2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD, "DRS_EXT_INSTANCE_TYPE_NOT_REQ_ON_MOD"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND, "DRS_EXT_CRYPTO_BIND"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO, "DRS_EXT_GET_REPL_INFO"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION, "DRS_EXT_STRONG_ENCRYPTION"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01, "DRS_EXT_DCINFO_VFFFFFFFF"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP, "DRS_EXT_TRANSITIVE_MEMBERSHIP"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY, "DRS_EXT_ADD_SID_HISTORY"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3, "DRS_EXT_POST_BETA3"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5, "DRS_EXT_GETCHGREQ_V5"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2, "DRS_EXT_GETMEMBERSHIPS2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6, "DRS_EXT_GETCHGREQ_V6"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS, "DRS_EXT_NONDOMAIN_NCS"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8, "DRS_EXT_GETCHGREQ_V8"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5, "DRS_EXT_GETCHGREPLY_V5"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6, "DRS_EXT_GETCHGREPLY_V6"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3, "DRS_EXT_WHISTLER_BETA3"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7, "DRS_EXT_WHISTLER_BETA3"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT, "DRS_EXT_WHISTLER_BETA3"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS, "DRS_EXT_W2K3_DEFLATE"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10, "DRS_EXT_GETCHGREQ_V10"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_RESERVED_PART2, "DRS_EXT_RESERVED_FOR_WIN2K_OR_DOTNET_PART2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_RESERVED_PART3, "DRS_EXT_RESERVED_FOR_WIN2K_OR_DOTNET_PART3") +}; + +static const struct drs_extension_flag drs_repl_flags_ex[] = { + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_ADAM, "DRS_EXT_ADAM"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_LH_BETA2, "DRS_EXT_LH_BETA2"), + DEFINE_FLAG(DRSUAPI_SUPPORTED_EXTENSION_RECYCLE_BIN, "DRS_EXT_RECYCLE_BIN") +}; + + + +int net_drs_bind_cmd(struct net_context *ctx, int argc, const char **argv) +{ + int i; + struct net_drs_context *drs_ctx; + struct drsuapi_DsBindInfo48 *info48; + + /* only one arg expected */ + if (argc != 1) { + return net_drs_bind_usage(ctx, argc, argv); + } + + if (!net_drs_create_context(ctx, argv[0], &drs_ctx)) { + return -1; + } + + d_printf("Bind to %s succeeded.\n", drs_ctx->dc_name); + d_printf("Extensions supported (cb=%d):\n", drs_ctx->drs_conn->bind_info_len); + + /* Print standard flags */ + info48 = &drs_ctx->drs_conn->info48; + for (i = 0; i < ARRAY_SIZE(drs_repl_flags); i++) { + const struct drs_extension_flag *repl_flag = &drs_repl_flags[i]; + d_printf(" %-60s: %-3s (%s)\n", repl_flag->samba_name, + info48->supported_extensions & repl_flag->flag ? "Yes" : "No", + repl_flag->win_name); + } + + /* Print Extended flags */ + d_printf("\n"); + d_printf("Extended Extensions supported:\n"); + for (i = 0; i < ARRAY_SIZE(drs_repl_flags_ex); i++) { + const struct drs_extension_flag *repl_flag_ex = &drs_repl_flags_ex[i]; + d_printf(" %-60s: %-3s (%s)\n", repl_flag_ex->samba_name, + info48->supported_extensions_ext & repl_flag_ex->flag ? "Yes" : "No", + repl_flag_ex->win_name); + } + + /* print additional info */ + d_printf("\n"); + d_printf("Site GUID: %s\n", GUID_string(drs_ctx, &info48->site_guid)); + d_printf("Repl epoch: %d\n", info48->repl_epoch); + if (GUID_all_zero(&info48->config_dn_guid)) { + d_printf("Forest GUID: (none)\n"); + } else { + d_printf("Forest GUID: %s\n", GUID_string(drs_ctx, &info48->config_dn_guid)); + } + + talloc_free(drs_ctx); + + return 0; +} + +int net_drs_bind_usage(struct net_context *ctx, int argc, const char **argv) +{ + d_printf("samba-tool drs bind <DC_NAME>\n"); + return 0; +} diff --git a/source4/samba_tool/drs/drs_kcc.c b/source4/samba_tool/drs/drs_kcc.c new file mode 100644 index 0000000000..fa8ea407df --- /dev/null +++ b/source4/samba_tool/drs/drs_kcc.c @@ -0,0 +1,170 @@ +/* + Unix SMB/CIFS implementation. + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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 "samba_tool/samba_tool.h" +#include "samba_tool/drs/drs.h" +#include "lib/ldb/include/ldb.h" + + +static bool net_drs_kcc_site_info(struct net_drs_context *drs_ctx, + const char **site_name, + uint32_t *site_options) +{ + struct ldb_dn *dn; + const struct ldb_val *ldb_val; + TALLOC_CTX *mem_ctx; + int ret; + struct ldb_result *ldb_res; + static const char *attrs[] = { + "options", + "whenChanged", + NULL + }; + + mem_ctx = talloc_new(drs_ctx); + + /* get dsServiceName, which is NTDS Settings + * object for the server + * e.g.: CN=NTDS Settings,CN=<DC_NAME>,CN=Servers,CN=<SITE_NAME>,CN=Sites,<CONFIG> */ + dn = ldb_msg_find_attr_as_dn(drs_ctx->ldap.ldb, mem_ctx, drs_ctx->ldap.rootdse, "dsServiceName"); + if (!dn) { + d_printf("No dsServiceName value in RootDSE!\n"); + goto failed; + } + + /* work out site name */ + if (!ldb_dn_remove_child_components(dn, 3)) { + d_printf("Failed to find Site DN.\n"); + goto failed; + } + + ldb_val = ldb_dn_get_rdn_val(dn); + if (!ldb_val) { + d_printf("Failed to get site name.\n"); + goto failed; + } + *site_name = talloc_strndup(drs_ctx, (const char*)ldb_val->data, ldb_val->length); + + /* get 'NTDS Site Settings' */ + if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Site Settings")) { + d_printf("Failed to create NTDS Site Settings DN.\n"); + goto failed; + } + + ret = ldb_search(drs_ctx->ldap.ldb, mem_ctx, &ldb_res, + dn, LDB_SCOPE_BASE, attrs, "(objectClass=*)"); + if (ret != LDB_SUCCESS) { + d_printf("Failed to get Site object\n"); + goto failed; + } + if (ldb_res->count != 1) { + d_printf("Error: returned %d messages for Site object.\n", ldb_res->count); + goto failed; + } + *site_options = ldb_msg_find_attr_as_uint(ldb_res->msgs[0], "options", 0); + + talloc_free(mem_ctx); + return true; + +failed: + talloc_free(mem_ctx); + return false; +} + +/** + * 'samba-tool drs kcc' command entry point + */ +int net_drs_kcc_cmd(struct net_context *ctx, int argc, const char **argv) +{ + NTSTATUS status; + struct net_drs_context *drs_ctx; + struct net_drs_connection *drs_conn; + struct drsuapi_DsBindInfo48 *info48; + struct drsuapi_DsExecuteKCC req; + union drsuapi_DsExecuteKCCRequest kcc_req; + const char *site_name; + uint32_t site_options; + + /* only one arg expected */ + if (argc != 1) { + return net_drs_kcc_usage(ctx, argc, argv); + } + + if (!net_drs_create_context(ctx, argv[0], &drs_ctx)) { + return -1; + } + drs_conn = drs_ctx->drs_conn; + info48 = &drs_conn->info48; + + /* check if target DC supports ExecuteKCC */ + if (!(info48->supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE)) { + d_printf("%s does not support EXECUTE_KCC extension.\n", drs_ctx->dc_name); + goto failed; + } + + /* gather some Site info */ + if (!net_drs_kcc_site_info(drs_ctx, &site_name, &site_options)) { + goto failed; + } + + d_printf("%s\n", site_name); + if (site_options) { + /* TODO: print meaningfull site options here */ + d_printf("Current Site Options: 0x%X\n", site_options); + } else { + d_printf("Current Site Options: (none)\n"); + } + + /* execute KCC */ + ZERO_STRUCT(req); + ZERO_STRUCT(kcc_req); + req.in.bind_handle = &drs_conn->bind_handle; + req.in.level = 1; + req.in.req = &kcc_req; + status = dcerpc_drsuapi_DsExecuteKCC_r(drs_conn->drs_handle, drs_ctx, &req); + if (!NT_STATUS_IS_OK(status)) { + const char *errstr = nt_errstr(status); + d_printf("dcerpc_drsuapi_DsExecuteKCC failed - %s.\n", errstr); + goto failed; + } else if (!W_ERROR_IS_OK(req.out.result)) { + d_printf("DsExecuteKCC failed - %s.\n", win_errstr(req.out.result)); + goto failed; + } + + d_printf("Consistency check on %s successful.\n", drs_ctx->dc_name); + + talloc_free(drs_ctx); + return 0; + +failed: + talloc_free(drs_ctx); + return -1; +} + +/** + * 'samba-tool drs kcc' usage + */ +int net_drs_kcc_usage(struct net_context *ctx, int argc, const char **argv) +{ + d_printf("samba-tool drs kcc <DC_NAME>\n"); + return 0; +} diff --git a/source4/samba_tool/drs/drs_replicate.c b/source4/samba_tool/drs/drs_replicate.c new file mode 100644 index 0000000000..36e0792e95 --- /dev/null +++ b/source4/samba_tool/drs/drs_replicate.c @@ -0,0 +1,252 @@ +/* + Unix SMB/CIFS implementation. + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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 "samba_tool/samba_tool.h" +#include "samba_tool/drs/drs.h" +#include "lib/ldb/include/ldb.h" +#include "dsdb/samdb/samdb.h" + + +/** + * Figure out what is the NDTS Settings objectGUID + * when DC_NAME is given + */ +static struct ldb_dn * +net_drs_server_dn_from_dc_name(struct net_drs_context *drs_ctx, + const char *dc_name) +{ + int ldb_err; + char *filter; + struct ldb_dn *dn; + struct ldb_dn *server_dn = NULL; + struct ldb_result *ldb_res; + static const char *attrs[] = { + "objectGUID", + "name", + "dNSHostName", + NULL + }; + TALLOC_CTX *mem_ctx; + + mem_ctx = talloc_new(drs_ctx); + + /* Make DN for Sites container */ + dn = ldb_msg_find_attr_as_dn(drs_ctx->ldap.ldb, mem_ctx, drs_ctx->ldap.rootdse, "dsServiceName"); + NET_DRS_CHECK_GOTO(dn != NULL, failed, "RootDSE doesn't have dsServiceName!?\n"); + if (!ldb_dn_remove_child_components(dn, 4)) { + d_printf("Failed to make DN for Sites container.\n"); + goto failed; + } + + /* search for Server in Sites container */ + filter = talloc_asprintf(mem_ctx, + "(&(objectCategory=server)(|(name=%1$s)(dNSHostName=%1$s)))", + dc_name); + ldb_err = ldb_search(drs_ctx->ldap.ldb, mem_ctx, &ldb_res, + dn, LDB_SCOPE_SUBTREE, attrs, + "%s", + filter); + if (ldb_err != LDB_SUCCESS) { + d_printf("ldb_seach(base=%s, filter=%s) failed: %d (%s).\n", + ldb_dn_get_linearized(dn), + filter, + ldb_err, ldb_errstring(drs_ctx->ldap.ldb)); + goto failed; + } + if (ldb_res->count != 1) { + d_printf("ldb_search(base=%s, filter=%s) returned %d records, expected 1!\n", + ldb_dn_get_linearized(dn), + filter, + ldb_res->count); + goto failed; + } + + server_dn = talloc_steal(drs_ctx, ldb_res->msgs[0]->dn); + +failed: + talloc_free(mem_ctx); + return server_dn; +} + + +/** + * Figure out what is the NDTS Settings objectGUID + * when DC_NAME is given + */ +static bool net_drs_ntds_guid_from_dc_name(struct net_drs_context *drs_ctx, + const char *dc_name, + struct GUID *_ntds_guid) +{ + int ldb_err; + struct ldb_dn *server_dn; + struct ldb_result *ldb_res; + static const char *attrs[] = { + "objectGUID", + "msDS-portLDAP", + "name", + "objectCategory", + NULL + }; + TALLOC_CTX *mem_ctx; + + mem_ctx = talloc_new(drs_ctx); + + /* resolve Server_DN for dc_name */ + server_dn = net_drs_server_dn_from_dc_name(drs_ctx, dc_name); + if (!server_dn) { + d_printf("DSA object for %s could not be found.\n", dc_name); + goto failed; + } + + /* move server_dn mem to local context */ + server_dn = talloc_steal(mem_ctx, server_dn); + + /* search ntdsDsa object under Server container */ + ldb_err = ldb_search(drs_ctx->ldap.ldb, mem_ctx, &ldb_res, + server_dn, LDB_SCOPE_ONELEVEL, attrs, + "%s", "(|(objectCategory=nTDSDSA)(objectCategory=nTDSDSARO))"); + if (ldb_err != LDB_SUCCESS) { + d_printf("ldb_seach() failed with err: %d (%s).\n", + ldb_err, ldb_errstring(drs_ctx->ldap.ldb)); + goto failed; + } + if (ldb_res->count != 1) { + d_printf("ldb_search() should return exactly one record!\n"); + goto failed; + } + + *_ntds_guid = samdb_result_guid(ldb_res->msgs[0], "objectGUID"); + + talloc_free(mem_ctx); + return true; + +failed: + talloc_free(mem_ctx); + return false; +} + +/** + * Sends DsReplicaSync to dc_name_dest to + * replicate naming context nc_dn_str from + * server with ntds_guid_src GUID + */ +static bool net_drs_replicate_sync_nc(struct net_drs_context *drs_ctx, + struct GUID ntds_guid_src, + const char *nc_dn_str, + uint32_t options) +{ + NTSTATUS status; + struct net_drs_connection *drs_conn; + struct drsuapi_DsReplicaSync req; + union drsuapi_DsReplicaSyncRequest sync_req; + struct drsuapi_DsReplicaObjectIdentifier nc; + + /* use already opened connection */ + drs_conn = drs_ctx->drs_conn; + + /* construct naming context object */ + ZERO_STRUCT(nc); + nc.dn = nc_dn_str; + + /* construct request object for DsReplicaSync */ + req.in.bind_handle = &drs_conn->bind_handle; + req.in.level = 1; + req.in.req = &sync_req; + req.in.req->req1.naming_context = &nc; + req.in.req->req1.options = options; + req.in.req->req1.source_dsa_dns = NULL; + req.in.req->req1.source_dsa_guid = ntds_guid_src; + + /* send DsReplicaSync request */ + status = dcerpc_drsuapi_DsReplicaSync_r(drs_conn->drs_handle, drs_ctx, &req); + if (!NT_STATUS_IS_OK(status)) { + const char *errstr = nt_errstr(status); + d_printf("DsReplicaSync RPC failed - %s.\n", errstr); + return false; + } else if (!W_ERROR_IS_OK(req.out.result)) { + d_printf("DsReplicaSync failed - %s (nc=[%s], dsa_guid=[%s]).\n", + win_errstr(req.out.result), + nc.dn, GUID_string(drs_ctx, &ntds_guid_src)); + return false; + } + + return true; +} + +/** + * 'samba-tool drs replicate' command entry point + */ +int net_drs_replicate_cmd(struct net_context *ctx, int argc, const char **argv) +{ + bool bret; + struct net_drs_context *drs_ctx; + struct GUID ntds_guid_src; + const char *dc_name_dest; + const char *dc_name_src; + const char *nc_dn_str; + + /* only one arg expected */ + if (argc != 3) { + return net_drs_replicate_usage(ctx, argc, argv); + } + + dc_name_dest = argv[0]; + dc_name_src = argv[1]; + nc_dn_str = argv[2]; + + if (!net_drs_create_context(ctx, dc_name_dest, &drs_ctx)) { + return -1; + } + + /* Resolve source DC_NAME to its NDTS Settings GUID */ + if (!net_drs_ntds_guid_from_dc_name(drs_ctx, dc_name_src, &ntds_guid_src)) { + d_printf("Error: DSA object for %s could not be found.\n", dc_name_src); + goto failed; + } + + /* Synchronize given Naming Context */ + bret = net_drs_replicate_sync_nc(drs_ctx, + ntds_guid_src, nc_dn_str, + DRSUAPI_DRS_WRIT_REP); + if (!bret) { + goto failed; + } + + d_printf("Replicate from %s to %s was successful.\n", dc_name_src, drs_ctx->dc_name); + + talloc_free(drs_ctx); + return 0; + +failed: + d_printf("Replicate terminated with errors.\n"); + talloc_free(drs_ctx); + return -1; +} + +/** + * 'samba-tool drs replicate' usage + */ +int net_drs_replicate_usage(struct net_context *ctx, int argc, const char **argv) +{ + d_printf("samba-tool drs replicate <Dest_DC_NAME> <Src_DC_NAME> <Naming Context>\n"); + return 0; +} diff --git a/source4/samba_tool/drs/drs_showrepl.c b/source4/samba_tool/drs/drs_showrepl.c new file mode 100644 index 0000000000..f399097008 --- /dev/null +++ b/source4/samba_tool/drs/drs_showrepl.c @@ -0,0 +1,605 @@ +/* + Unix SMB/CIFS implementation. + + Implements functions offered by repadmin.exe tool under Windows + + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010 + + 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 "samba_tool/samba_tool.h" +#include "samba_tool/drs/drs.h" +#include "lib/ldb/include/ldb.h" +#include "dsdb/samdb/samdb.h" +#include "lib/util/util_ldb.h" + + +/** + * Parses NTDS Settings DN to find out: + * - DC name + * - Site name + * - Domain DNS name + */ +static bool net_drs_parse_ntds_dn(struct ldb_dn *ntds_dn, + TALLOC_CTX *mem_ctx, + const char **_dc_name, + const char **_site_name, + const char **_domain_dns_name) +{ + struct ldb_dn *dn = NULL; + const struct ldb_val *val; + + dn = ldb_dn_copy(mem_ctx, ntds_dn); + NET_DRS_NOMEM_GOTO(dn, failed); + + /* remove NTDS Settings component */ + ldb_dn_remove_child_components(dn, 1); + if (_dc_name) { + val = ldb_dn_get_rdn_val(dn); + *_dc_name = talloc_strdup(mem_ctx, (const char *)val->data); + } + + /* remove DC and Servers components */ + ldb_dn_remove_child_components(dn, 2); + if (_site_name) { + val = ldb_dn_get_rdn_val(dn); + *_site_name = talloc_strdup(mem_ctx, (const char *)val->data); + } + + if (_domain_dns_name) { + char *pstr; + char *dns_name; + + dns_name = ldb_dn_canonical_string(mem_ctx, dn); + NET_DRS_NOMEM_GOTO(dns_name, failed); + + pstr = strchr(dns_name, '/'); + if (pstr) { + *pstr = '\0'; + } + + *_domain_dns_name = dns_name; + } + + talloc_free(dn); + return true; + +failed: + talloc_free(dn); + return false; +} + +static char * net_drs_dc_canonical_string(struct ldb_dn *ntds_dn, TALLOC_CTX *mem_ctx) +{ + const char *dc_name; + const char *site_name; + char *canonical_name; + + if (!net_drs_parse_ntds_dn(ntds_dn, mem_ctx, &dc_name, &site_name, NULL)) { + return NULL; + } + + canonical_name = talloc_asprintf(mem_ctx, "%s\\%s", site_name, dc_name); + + talloc_free(discard_const(dc_name)); + talloc_free(discard_const(site_name)); + + return canonical_name; +} + +/** + * Prints DC information for showrepl command + */ +static bool net_drs_showrepl_print_dc_info(struct net_drs_context *drs_ctx) +{ + int ret; + const char *dc_name; + const char *site_name; + struct ldb_dn *dn; + struct ldb_message **ntds_msgs; + struct ldb_message **site_msgs; + uint32_t options; + struct GUID guid; + TALLOC_CTX *mem_ctx; + const char *ntds_attr[] = {"options", "objectGuid", "invocationId", NULL}; + const char *site_ntds_attr[] = {"options", "whenChanged", NULL}; + + mem_ctx = talloc_new(drs_ctx); + + /* Get NTDS Settings DN string for the DC */ + dn = samdb_result_dn(drs_ctx->ldap.ldb, mem_ctx, + drs_ctx->ldap.rootdse, "dsServiceName", NULL); + NET_DRS_CHECK_GOTO(dn, failed, "No dsServiceName value in RootDSE!\n"); + + /* parse NTDS Settings DN */ + if (!net_drs_parse_ntds_dn(dn, mem_ctx, &dc_name, &site_name, NULL)) { + d_printf("Unexpected: Failed to parse %s DN!\n", + ldb_dn_get_linearized(dn)); + goto failed; + } + + /* Query DC record for DSA's NTDS Settings DN */ + ret = gendb_search_dn(drs_ctx->ldap.ldb, mem_ctx, dn, &ntds_msgs, ntds_attr); + if (ret != 1) { + d_printf("Error while fetching %s, Possible error: %s\n", + ldb_dn_get_linearized(dn), + ldb_errstring(drs_ctx->ldap.ldb)); + goto failed; + } + + /* find out NTDS Site Settings DN */ + if (!ldb_dn_remove_child_components(dn, 3)) { + d_printf("Unexpected: ldb_dn_remove_child_components() failed!\n"); + goto failed; + } + if (!ldb_dn_add_child_fmt(dn, "CN=%s", "NTDS Site Settings")) { + d_printf("Unexpected: ldb_dn_add_child_fmt() failed!\n"); + goto failed; + } + /* Query Site record for DSA's NTDS Settings DN */ + ret = gendb_search_dn(drs_ctx->ldap.ldb, mem_ctx, dn, &site_msgs, site_ntds_attr); + if (ret != 1) { + d_printf("Error while fetching %s, Possible error: %s\n", + ldb_dn_get_linearized(dn), + ldb_errstring(drs_ctx->ldap.ldb)); + goto failed; + } + + /* Site-name\DC-name */ + d_printf("%s\\%s\n", site_name, dc_name); + /* DSA Options */ + options = ldb_msg_find_attr_as_uint(ntds_msgs[0], "options", 0); + if (options) { + /* TODO: Print options as string in IS_GC... etc form */ + d_printf("DSA Options: 0x%08X\n", options); + } else { + d_printf("DSA Options: (none)\n"); + } + /* Site Options */ + options = ldb_msg_find_attr_as_uint(site_msgs[0], "options", 0); + if (options) { + /* TODO: Print options in string */ + d_printf("DSA Options: 0x%08X\n", options); + } else { + d_printf("Site Options: (none)\n"); + } + /* DSA GUID */ + guid = samdb_result_guid(ntds_msgs[0], "objectGUID"); + d_printf("DSA object GUID: %s\n", GUID_string(mem_ctx, &guid)); + /* DSA invocationId */ + guid = samdb_result_guid(ntds_msgs[0], "invocationId"); + d_printf("DSA invocationID: %s\n", GUID_string(mem_ctx, &guid)); + + talloc_free(mem_ctx); + return true; + +failed: + talloc_free(mem_ctx); + return false; +} + +/** + * Convenience function to call DsReplicaGetInfo + */ +static bool net_drs_exec_DsReplicaGetInfo(struct net_drs_context *drs_ctx, + enum drsuapi_DsReplicaInfoType info_type, + union drsuapi_DsReplicaInfo *_replica_info) +{ + NTSTATUS status; + struct drsuapi_DsReplicaGetInfo r; + union drsuapi_DsReplicaGetInfoRequest req; + enum drsuapi_DsReplicaInfoType info_type_got; + struct net_drs_connection *drs_conn = drs_ctx->drs_conn; + + ZERO_STRUCT(req); + req.req1.info_type = info_type; + + r.in.bind_handle = &drs_conn->bind_handle; + r.in.level = 1; + r.in.req = &req; + r.out.info = _replica_info; + r.out.info_type = &info_type_got; + + status = dcerpc_drsuapi_DsReplicaGetInfo_r(drs_conn->drs_handle, drs_ctx, &r); + if (!NT_STATUS_IS_OK(status)) { + const char *errstr = nt_errstr(status); + d_printf("DsReplicaGetInfo failed - %s.\n", errstr); + return false; + } else if (!W_ERROR_IS_OK(r.out.result)) { + d_printf("DsReplicaGetInfo failed - %s.\n", win_errstr(r.out.result)); + return false; + } + + if (info_type != info_type_got) { + d_printf("DsReplicaGetInfo: Error requested info %d, got info %d.\n", + info_type, info_type_got); + return false; + } + + return true; +} + +/** + * Return transport type string for given transport object DN. + * Currently always return 'RPC'. + * + * TODO: Implement getting transport type for all kind of transports + */ +static const char * +net_drs_transport_type_str(struct net_drs_context *drs_ctx, const char *transport_obj_dn) +{ + return "RPC"; +} + +/** + * Prints most of the info we got about + * a replication partner + */ +static bool net_drs_showrepl_print_heighbor(struct net_drs_context *drs_ctx, + struct drsuapi_DsReplicaNeighbour *neighbor) +{ + struct ldb_dn *ntds_dn; + TALLOC_CTX *mem_ctx; + + mem_ctx = talloc_new(drs_ctx); + + ntds_dn = ldb_dn_new(drs_ctx, drs_ctx->ldap.ldb, neighbor->source_dsa_obj_dn); + NET_DRS_NOMEM_GOTO(ntds_dn, failed); + + d_printf("%s\n", neighbor->naming_context_dn); + /* TODO: Determine connection type */ + d_printf("\t%s via %s\n", + net_drs_dc_canonical_string(ntds_dn, mem_ctx), + net_drs_transport_type_str(drs_ctx, neighbor->transport_obj_dn)); + d_printf("\t\tDSA object GUID: %s\n", GUID_string(mem_ctx, &neighbor->source_dsa_obj_guid)); + if (W_ERROR_IS_OK(neighbor->result_last_attempt)) { + d_printf("\t\tLast attempt @ %s was successful.\n", + nt_time_string(mem_ctx, neighbor->last_attempt)); + } else { + d_printf("\t\tLast attempt @ %s failed, result %d (%s):\n", + nt_time_string(mem_ctx, neighbor->last_attempt), + W_ERROR_V(neighbor->result_last_attempt), + win_errstr(neighbor->result_last_attempt)); + d_printf("\t\t\t%s\n", get_friendly_werror_msg(neighbor->result_last_attempt)); + } + d_printf("\t\t%d consecutive failure(s).\n", neighbor->consecutive_sync_failures); + d_printf("\t\tLast success @ %s\n", nt_time_string(mem_ctx, neighbor->last_success)); + + talloc_free(mem_ctx); + return true; + +failed: + talloc_free(mem_ctx); + return false; +} + +/** + * Prints list of all servers that target DC + * replicates from + */ +static bool net_drs_showrepl_print_inbound_neihbors(struct net_drs_context *drs_ctx) +{ + int i; + bool bret; + struct drsuapi_DsReplicaNeighbourCtr *reps_from; + union drsuapi_DsReplicaInfo replica_info; + + d_printf("\n==== INBOUND NEIGHBORS ====\n"); + + bret = net_drs_exec_DsReplicaGetInfo(drs_ctx, + DRSUAPI_DS_REPLICA_INFO_NEIGHBORS, &replica_info); + if (!bret) { + d_printf("DsReplicaGetInfo() failed for DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES.\n"); + return false; + } + reps_from = replica_info.neighbours; + + for (i = 0; i < reps_from->count; i++) { + d_printf("\n"); + net_drs_showrepl_print_heighbor(drs_ctx, &reps_from->array[i]); + } + + return true; +} + +/** + * Prints list of all servers that target DC + * notifies for changes + */ +static bool net_drs_showrepl_print_outbound_neihbors(struct net_drs_context *drs_ctx) +{ + int i; + bool bret; + struct drsuapi_DsReplicaNeighbourCtr *reps_to; + union drsuapi_DsReplicaInfo replica_info; + + d_printf("\n==== OUTBOUND NEIGHBORS ====\n"); + + bret = net_drs_exec_DsReplicaGetInfo(drs_ctx, + DRSUAPI_DS_REPLICA_INFO_REPSTO, &replica_info); + if (!bret) { + d_printf("DsReplicaGetInfo() failed for DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES.\n"); + return false; + } + reps_to = replica_info.repsto; + + for (i = 0; i < reps_to->count; i++) { + d_printf("\n"); + net_drs_showrepl_print_heighbor(drs_ctx, &reps_to->array[i]); + } + + return true; +} + +/** + * Prints all connections under + * NTDS Settings for target DC. + * + * NOTE: All connections are printed + * no matter what their status is + */ +static bool net_drs_showrepl_print_connection_objects(struct net_drs_context *drs_ctx) +{ + int i; + int conn_count; + struct ldb_message **conn_msgs; + struct ldb_dn *dn; + uint32_t options; + const char *dc_dns_name; + TALLOC_CTX *mem_ctx; + const char *conn_attr[] = { + "name", + "enabledConnection", + "fromServer", + "mS-DS-ReplicatesNCReason", + "options", + "schedule", + "transportType", + "whenChanged", + "whenCreated", + NULL + }; + + mem_ctx = talloc_new(drs_ctx); + + d_printf("\n==== KCC CONNECTION OBJECTS ====\n"); + + /* Get NTDS Settings DN string for the DC */ + dn = samdb_result_dn(drs_ctx->ldap.ldb, mem_ctx, + drs_ctx->ldap.rootdse, "dsServiceName", NULL); + NET_DRS_CHECK_GOTO(dn, failed, "No dsServiceName value in RootDSE!\n"); + + /* DNS host name for target DC */ + dc_dns_name = ldb_msg_find_attr_as_string(drs_ctx->ldap.rootdse , "dnsHostName", NULL); + NET_DRS_CHECK_GOTO(dc_dns_name, failed, "No dsServiceName value in dnsHostName!\n"); + + /* Enum. Connection objects under NTDS Settings */ + conn_count = gendb_search(drs_ctx->ldap.ldb, mem_ctx, dn, + &conn_msgs, conn_attr, "(objectClass=nTDSConnection)"); + if (conn_count == -1) { + d_printf("Error searching Connections for %s, Possible error: %s\n", + ldb_dn_get_linearized(dn), + ldb_errstring(drs_ctx->ldap.ldb)); + goto failed; + } + + for (i = 0; i < conn_count; i++) { + int k; + const char *transport_type; + struct ldb_message_element *msg_elem; + struct ldb_message *conn_msg = conn_msgs[i]; + + d_printf("Connection --\n"); + d_printf("\tConnection name : %s\n", + ldb_msg_find_attr_as_string(conn_msg, "name", NULL)); + d_printf("\tEnabled : %s\n", + ldb_msg_find_attr_as_string(conn_msg, "enabledConnection", "TRUE")); + d_printf("\tServer DNS name : %s\n", dc_dns_name); + d_printf("\tServer DN name : %s\n", + ldb_msg_find_attr_as_string(conn_msg, "fromServer", NULL)); + transport_type = ldb_msg_find_attr_as_string(conn_msg, "transportType", NULL); + d_printf("\t\tTransportType: %s\n", + net_drs_transport_type_str(drs_ctx, transport_type)); + /* TODO: print Connection options in friendly format */ + options = ldb_msg_find_attr_as_uint(conn_msg, "options", 0); + d_printf("\t\toptions: 0x%08X\n", options); + + /* print replicated NCs for this connection */ + msg_elem = ldb_msg_find_element(conn_msg, "mS-DS-ReplicatesNCReason"); + if (!msg_elem) { + d_printf("Warning: No NC replicated for Connection!\n"); + continue; + } + for (k = 0; k < msg_elem->num_values; k++) { + struct dsdb_dn *bin_dn; + + bin_dn = dsdb_dn_parse(mem_ctx, drs_ctx->ldap.ldb, + &msg_elem->values[k], DSDB_SYNTAX_BINARY_DN); + if (!bin_dn) { + d_printf("Unexpected: Failed to parse DN - %s\n", + msg_elem->values[k].data); + } + d_printf("\t\tReplicatesNC: %s\n", ldb_dn_get_linearized(bin_dn->dn)); + /* TODO: print Reason flags in friendly format */ + options = RIVAL(bin_dn->extra_part.data, 0); + d_printf("\t\tReason: 0x%08X\n", options); + d_printf("\t\t\tReplica link has been added.\n"); + } + } + + talloc_free(mem_ctx); + return true; + +failed: + talloc_free(mem_ctx); + return false; +} + +/** + * Prints all DC's connections failure. + * + * NOTE: Still don't know exactly what + * this information means + */ +static bool net_drs_showrepl_print_connect_failures(struct net_drs_context *drs_ctx) +{ + int i; + bool bret; + struct ldb_dn *ntds_dn; + struct drsuapi_DsReplicaKccDsaFailure *failure; + struct drsuapi_DsReplicaKccDsaFailuresCtr *connect_failures; + union drsuapi_DsReplicaInfo replica_info; + TALLOC_CTX *mem_ctx; + + d_printf("\n==== CONNECION FAILURES ====\n"); + + bret = net_drs_exec_DsReplicaGetInfo(drs_ctx, + DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES, + &replica_info); + if (!bret) { + d_printf("DsReplicaGetInfo() failed for DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES.\n"); + return false; + } + connect_failures = replica_info.connectfailures; + + mem_ctx = talloc_new(drs_ctx); + + for (i = 0; i < connect_failures->count; i++) { + failure = &connect_failures->array[i]; + + ntds_dn = ldb_dn_new(mem_ctx, drs_ctx->ldap.ldb, failure->dsa_obj_dn); + d_printf("Source: %s\n", net_drs_dc_canonical_string(ntds_dn, mem_ctx)); + d_printf("******* %d CONSECUTIVE FAILURES since %s\n", + failure->num_failures, + nt_time_string(mem_ctx, failure->first_failure)); + d_printf("Last error: %d (%s):\n", + W_ERROR_V(failure->last_result), + win_errstr(failure->last_result)); + d_printf("\t\t\t%s\n", get_friendly_werror_msg(failure->last_result)); + } + + talloc_free(mem_ctx); + return true; +} + +/** + * Prints all DC's link failures + */ +static bool net_drs_showrepl_print_link_failures(struct net_drs_context *drs_ctx) +{ + int i; + bool bret; + struct ldb_dn *ntds_dn; + struct drsuapi_DsReplicaKccDsaFailure *failure; + struct drsuapi_DsReplicaKccDsaFailuresCtr *link_failures; + union drsuapi_DsReplicaInfo replica_info; + TALLOC_CTX *mem_ctx; + + d_printf("\n==== LINK FAILURES ====\n"); + + bret = net_drs_exec_DsReplicaGetInfo(drs_ctx, + DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES, &replica_info); + if (!bret) { + d_printf("DsReplicaGetInfo() failed for DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES.\n"); + return false; + } + link_failures = replica_info.linkfailures; + + mem_ctx = talloc_new(drs_ctx); + + for (i = 0; i < link_failures->count; i++) { + failure = &link_failures->array[i]; + + ntds_dn = ldb_dn_new(mem_ctx, drs_ctx->ldap.ldb, failure->dsa_obj_dn); + d_printf("Source: %s\n", net_drs_dc_canonical_string(ntds_dn, mem_ctx)); + d_printf("******* %d CONSECUTIVE FAILURES since %s\n", + failure->num_failures, + nt_time_string(mem_ctx, failure->first_failure)); + d_printf("Last error: %d (%s):\n", + W_ERROR_V(failure->last_result), + win_errstr(failure->last_result)); + d_printf("\t\t\t%s\n", get_friendly_werror_msg(failure->last_result)); + } + + talloc_free(mem_ctx); + return true; +} + +/** + * 'samba-tool drs showrepl' command entry point + */ +int net_drs_showrepl_cmd(struct net_context *ctx, int argc, const char **argv) +{ + const char *dc_name; + struct net_drs_context *drs_ctx = NULL; + + /* only one arg expected */ + if (argc != 1) { + return net_drs_showrepl_usage(ctx, argc, argv); + } + + dc_name = argv[0]; + + if (!net_drs_create_context(ctx, dc_name, &drs_ctx)) { + goto failed; + } + + /* Print DC and Site info */ + if (!net_drs_showrepl_print_dc_info(drs_ctx)) { + goto failed; + } + + /* INBOUND Neighbors */ + if (!net_drs_showrepl_print_inbound_neihbors(drs_ctx)) { + goto failed; + } + + /* OUTBOUND Neighbors */ + if (!net_drs_showrepl_print_outbound_neihbors(drs_ctx)) { + goto failed; + } + + /* Connection objects for DC */ + if (!net_drs_showrepl_print_connection_objects(drs_ctx)) { + goto failed; + } + + /* Connection failures */ + if (!net_drs_showrepl_print_connect_failures(drs_ctx)) { + goto failed; + } + + /* Link failures */ + if (!net_drs_showrepl_print_link_failures(drs_ctx)) { + goto failed; + } + + talloc_free(drs_ctx); + return 0; + +failed: + talloc_free(drs_ctx); + return -1; +} + +/** + * 'samba-tool drs showrepl' usage + */ +int net_drs_showrepl_usage(struct net_context *ctx, int argc, const char **argv) +{ + d_printf("samba-tool drs showrepl <DC_NAME>\n"); + return 0; +} |