2 Samba Unix/Linux SMB client library
4 Implements functions offered by repadmin.exe tool under Windows
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "utils/net/net.h"
24 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
25 #include "utils/net/drs/net_drs.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb_wrap.h"
28 #include "system/filesys.h"
32 * 'net drs' supported sub-commands
34 static const struct net_functable net_drs_functable[] = {
35 { "bind", "Display replication features for a domain controller\n", net_drs_bind_cmd, net_drs_bind_usage },
36 { "kcc", "Forces the KCC to recalculate replication topology for a specified domain controller\n",
37 net_drs_kcc_cmd, net_drs_kcc_usage },
42 * 'net drs' entry point
44 int net_drs(struct net_context *ctx, int argc, const char **argv)
46 return net_run_function(ctx, argc, argv, net_drs_functable, net_drs_usage);
50 * 'net drs' usage message
52 int net_drs_usage(struct net_context *ctx, int argc, const char **argv)
54 d_printf("net drs <command> [options]\n");
56 d_printf("Currently implemented commands:\n");
57 d_printf(" bind - Display DC replication features\n");
58 d_printf(" kcc - Forces the KCC to recalculate replication topology for a specified domain controller\n");
63 * Create drsuapi connection to remote DC
64 * and fill-in DC capabilities
66 static bool net_drs_DsBind(struct net_drs_context *drs_ctx, struct net_drs_connection *conn)
69 struct GUID bind_guid;
70 struct drsuapi_DsBind req;
71 struct drsuapi_DsBindInfoCtr in_bind_ctr;
72 union drsuapi_DsBindInfo *bind_info;
74 SMB_ASSERT(conn->binding != NULL);
76 status = dcerpc_pipe_connect_b(conn,
80 drs_ctx->net_ctx->credentials,
81 drs_ctx->net_ctx->event_ctx,
82 drs_ctx->net_ctx->lp_ctx);
83 if (!NT_STATUS_IS_OK(status)) {
84 d_printf("Failed to connect to server: %s\n", nt_errstr(status));
88 ZERO_STRUCT(in_bind_ctr);
89 in_bind_ctr.length = 48;
90 in_bind_ctr.info.info48.pid = (uint32_t)getpid();
91 GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
92 req.in.bind_guid = &bind_guid;
93 req.in.bind_info = &in_bind_ctr;
94 req.out.bind_handle = &conn->bind_handle;
96 status = dcerpc_drsuapi_DsBind(conn->drs_pipe, conn, &req);
97 if (!NT_STATUS_IS_OK(status)) {
98 const char *errstr = nt_errstr(status);
99 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
100 errstr = dcerpc_errstr(conn, conn->drs_pipe->last_fault_code);
102 d_printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
104 } else if (!W_ERROR_IS_OK(req.out.result)) {
105 d_printf("DsBind failed - %s\n", win_errstr(req.out.result));
109 /* fill-in remote DC capabilities */
110 ZERO_STRUCT(conn->info48);
111 bind_info = &req.out.bind_info->info;
112 conn->bind_info_len = req.out.bind_info->length;
113 switch (conn->bind_info_len) {
115 conn->info48.supported_extensions_ext = bind_info->info48.supported_extensions_ext;
116 conn->info48.config_dn_guid = bind_info->info48.config_dn_guid;
118 conn->info48.repl_epoch = bind_info->info28.repl_epoch;
120 conn->info48.supported_extensions = bind_info->info24.supported_extensions;
121 conn->info48.site_guid = bind_info->info24.site_guid;
122 conn->info48.pid = bind_info->info24.pid;
125 d_printf("Error: server returned BindInfo length %d", req.out.bind_info->length);
133 * Close DRSUAPI connection to remote DC
135 static bool net_drs_DsUnbind(struct net_drs_connection *conn)
137 struct drsuapi_DsUnbind r;
138 struct policy_handle bind_handle;
141 r.out.bind_handle = &bind_handle;
143 r.in.bind_handle = &conn->bind_handle;
144 dcerpc_drsuapi_DsUnbind(conn->drs_pipe, conn, &r);
150 * Open secured LDAP connection to remote DC
152 static bool net_drs_ldap_connect(struct net_drs_context *drs_ctx)
157 url = talloc_asprintf(drs_ctx, "ldap://%s/", drs_ctx->dc_name);
159 d_printf(__location__ ": Have no memory");
163 drs_ctx->ldap.ldb = ldb_wrap_connect(drs_ctx,
164 drs_ctx->net_ctx->event_ctx, drs_ctx->net_ctx->lp_ctx,
167 drs_ctx->net_ctx->credentials,
169 if (drs_ctx->ldap.ldb == NULL) {
170 d_printf("Unable to connect to LDAP %s", url);
180 * fetch RootDSE record
182 static bool net_drs_ldap_rootdse(struct net_drs_context *drs_ctx)
185 struct ldb_result *r;
186 struct ldb_dn *basedn;
187 static const char *attrs[] = {
192 SMB_ASSERT(drs_ctx->ldap.ldb != NULL);
194 basedn = ldb_dn_new(drs_ctx, drs_ctx->ldap.ldb, NULL);
196 d_printf(__location__ ": No memory");
200 ret = ldb_search(drs_ctx->ldap.ldb, drs_ctx, &r,
201 basedn, LDB_SCOPE_BASE, attrs,
204 if (ret != LDB_SUCCESS) {
205 d_printf("RootDSE search failed: %s", ldb_errstring(drs_ctx->ldap.ldb));
208 } else if (r->count != 1) {
209 d_printf("RootDSE search returned more than one record!");
214 drs_ctx->ldap.rootdse = r->msgs[0];
220 * parses binding from command line
221 * and gets target DC name
223 static bool net_drs_parse_binding(struct net_drs_context *drs_ctx, const char *dc_binding)
226 struct dcerpc_binding *b;
228 status = dcerpc_parse_binding(drs_ctx->drs_conn, dc_binding, &b);
229 if (!NT_STATUS_IS_OK(status)) {
230 d_printf("Bad binding supplied %s\n", dc_binding);
234 b->transport = NCACN_IP_TCP;
235 b->flags |= DCERPC_SIGN | DCERPC_SEAL;
237 /* cache target DC name */
238 drs_ctx->dc_name = b->target_hostname;
240 drs_ctx->drs_conn->binding = b;
246 * Free DRSUAPI connection upon net_drs_context
249 static int net_drs_context_destructor(struct net_drs_context *drs_ctx)
251 if (drs_ctx->drs_conn && drs_ctx->drs_conn->drs_pipe) {
252 net_drs_DsUnbind(drs_ctx->drs_conn);
258 * Create net_drs_context context to be used
259 * by 'net drs' sub-commands
261 bool net_drs_create_context(struct net_context *net_ctx,
262 const char *dc_binding,
263 struct net_drs_context **_drs_ctx)
265 struct net_drs_context *drs_ctx;
267 drs_ctx = talloc_zero(net_ctx, struct net_drs_context);
269 d_printf(__location__ ": No memory");
273 drs_ctx->drs_conn = talloc_zero(drs_ctx, struct net_drs_connection);
274 if (!drs_ctx->drs_conn) {
275 d_printf(__location__ ": No memory");
279 drs_ctx->net_ctx = net_ctx;
281 if (!net_drs_parse_binding(drs_ctx, dc_binding)) {
286 if (!net_drs_ldap_connect(drs_ctx)) {
290 if (!net_drs_ldap_rootdse(drs_ctx)) {
294 /* DRSUAPI connection */
295 if (!net_drs_DsBind(drs_ctx, drs_ctx->drs_conn)) {
299 /* set destructor to free any open connections */
300 talloc_set_destructor(drs_ctx, net_drs_context_destructor);
306 talloc_free(drs_ctx);