2952ffc976a0f1b4cc6e891d2a1a954fbd651275
[kai/samba.git] / source4 / winbind / wb_cmd_getdcname.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo --getdcname
5
6    Copyright (C) Volker Lendecke 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27
28 #include "librpc/gen_ndr/ndr_netlogon.h"
29
30 struct cmd_getdcname_state {
31         struct composite_context *ctx;
32         const char *domain_name;
33
34         struct netr_GetAnyDCName g;
35 };
36
37 static void getdcname_recv_domain(struct composite_context *ctx);
38 static void getdcname_recv_dcname(struct rpc_request *req);
39
40 struct composite_context *wb_cmd_getdcname_send(TALLOC_CTX *mem_ctx,
41                                                 struct wbsrv_service *service,
42                                                 const char *domain_name)
43 {
44         struct composite_context *result, *ctx;
45         struct cmd_getdcname_state *state;
46
47         result = talloc(mem_ctx, struct composite_context);
48         if (result == NULL) goto failed;
49         result->state = COMPOSITE_STATE_IN_PROGRESS;
50         result->async.fn = NULL;
51         result->event_ctx = service->task->event_ctx;
52
53         state = talloc(result, struct cmd_getdcname_state);
54         if (state == NULL) goto failed;
55         state->ctx = result;
56         result->private_data = state;
57
58         state->domain_name = talloc_strdup(state, domain_name);
59         if (state->domain_name == NULL) goto failed;
60
61         ctx = wb_sid2domain_send(state, service, service->primary_sid);
62         if (ctx == NULL) goto failed;
63
64         ctx->async.fn = getdcname_recv_domain;
65         ctx->async.private_data = state;
66         return result;
67
68  failed:
69         talloc_free(result);
70         return NULL;
71 }
72
73 static void getdcname_recv_domain(struct composite_context *ctx)
74 {
75         struct cmd_getdcname_state *state =
76                 talloc_get_type(ctx->async.private_data,
77                                 struct cmd_getdcname_state);
78         struct wbsrv_domain *domain;
79         struct rpc_request *req;
80
81         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
82         if (!composite_is_ok(state->ctx)) return;
83
84         state->g.in.logon_server = talloc_asprintf(
85                 state, "\\\\%s",
86                 dcerpc_server_name(domain->netlogon_pipe));
87         state->g.in.domainname = state->domain_name;
88
89         req = dcerpc_netr_GetAnyDCName_send(domain->netlogon_pipe, state,
90                                             &state->g);
91         if (composite_nomem(req, state->ctx)) return;
92
93         composite_continue_rpc(state->ctx, req, getdcname_recv_dcname, state);
94 }
95
96 static void getdcname_recv_dcname(struct rpc_request *req)
97 {
98         struct cmd_getdcname_state *state =
99                 talloc_get_type(req->async.private,
100                                 struct cmd_getdcname_state);
101
102         state->ctx->status = dcerpc_ndr_request_recv(req);
103         if (!composite_is_ok(state->ctx)) return;
104         state->ctx->status = werror_to_ntstatus(state->g.out.result);
105         if (!composite_is_ok(state->ctx)) return;
106
107         composite_done(state->ctx);
108 }
109
110 NTSTATUS wb_cmd_getdcname_recv(struct composite_context *c,
111                                TALLOC_CTX *mem_ctx,
112                                const char **dcname)
113 {
114         struct cmd_getdcname_state *state =
115                 talloc_get_type(c->private_data, struct cmd_getdcname_state);
116         NTSTATUS status = composite_wait(c);
117         if (NT_STATUS_IS_OK(status)) {
118                 const char *p = state->g.out.dcname;
119                 if (*p == '\\') p += 1;
120                 if (*p == '\\') p += 1;
121                 *dcname = talloc_strdup(mem_ctx, p);
122                 if (*dcname == NULL) {
123                         status = NT_STATUS_NO_MEMORY;
124                 }
125         }
126         talloc_free(state);
127         return status;
128 }