2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo --getdcname
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_stream.h"
27 #include "smbd/service_task.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
31 static void composite_netr_GetAnyDCName_recv_rpc(struct rpc_request *req);
33 static struct composite_context *composite_netr_GetAnyDCName_send(struct dcerpc_pipe *p,
35 struct netr_GetAnyDCName *r)
37 struct composite_context *result;
38 struct rpc_request *req;
40 result = talloc(mem_ctx, struct composite_context);
41 if (result == NULL) goto failed;
42 result->state = COMPOSITE_STATE_IN_PROGRESS;
43 result->async.fn = NULL;
44 result->event_ctx = p->conn->event_ctx;
46 req = dcerpc_netr_GetAnyDCName_send(p, mem_ctx, r);
47 if (req == NULL) goto failed;
48 req->async.callback = composite_netr_GetAnyDCName_recv_rpc;
49 req->async.private = result;
57 static void composite_netr_GetAnyDCName_recv_rpc(struct rpc_request *req)
59 struct composite_context *ctx =
60 talloc_get_type(req->async.private, struct composite_context);
62 ctx->status = dcerpc_ndr_request_recv(req);
63 if (!composite_is_ok(ctx)) return;
67 static NTSTATUS composite_netr_GetAnyDCName_recv(struct composite_context *ctx)
69 NTSTATUS status = composite_wait(ctx);
74 struct cmd_getdcname_state {
75 struct composite_context *ctx;
76 const char *domain_name;
78 struct netr_GetAnyDCName g;
81 static struct composite_context *getdcname_send_req(struct wbsrv_domain *domain,
83 static NTSTATUS getdcname_recv_req(struct composite_context *ctx, void *p);
85 struct composite_context *wb_cmd_getdcname_send(struct wbsrv_service *service,
86 struct wbsrv_domain *domain,
87 const char *domain_name)
89 struct cmd_getdcname_state *state;
91 state = talloc(NULL, struct cmd_getdcname_state);
92 state->domain_name = talloc_strdup(state, domain_name);
93 state->ctx = wb_domain_request_send(state, service,
98 if (state->ctx == NULL) {
102 state->ctx->private_data = state;
106 static struct composite_context *getdcname_send_req(struct wbsrv_domain *domain, void *p)
108 struct cmd_getdcname_state *state =
109 talloc_get_type(p, struct cmd_getdcname_state);
111 state->g.in.logon_server = talloc_asprintf(
113 dcerpc_server_name(domain->netlogon_pipe));
114 state->g.in.domainname = state->domain_name;
116 return composite_netr_GetAnyDCName_send(domain->netlogon_pipe,
120 static NTSTATUS getdcname_recv_req(struct composite_context *ctx, void *p)
122 struct cmd_getdcname_state *state =
123 talloc_get_type(p, struct cmd_getdcname_state);
126 status = composite_netr_GetAnyDCName_recv(ctx);
127 NT_STATUS_NOT_OK_RETURN(status);
129 if (!W_ERROR_IS_OK(state->g.out.result)) {
130 return werror_to_ntstatus(state->g.out.result);
136 NTSTATUS wb_cmd_getdcname_recv(struct composite_context *c,
140 struct cmd_getdcname_state *state =
141 talloc_get_type(c->private_data, struct cmd_getdcname_state);
142 NTSTATUS status = composite_wait(c);
143 if (NT_STATUS_IS_OK(status)) {
144 const char *p = state->g.out.dcname;
145 if (*p == '\\') p += 1;
146 if (*p == '\\') p += 1;
147 *dcname = talloc_strdup(mem_ctx, p);
148 if (*dcname == NULL) {
149 status = NT_STATUS_NO_MEMORY;