r11517: Cleanup time, this looks larger than it is. This mainly gets rid of
[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_stream.h"
27 #include "smbd/service_task.h"
28
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30
31 struct cmd_getdcname_state {
32         struct composite_context *ctx;
33         const char *domain_name;
34
35         struct netr_GetAnyDCName g;
36 };
37
38 static void getdcname_recv_domain(struct composite_context *ctx);
39 static void getdcname_recv_dcname(struct rpc_request *req);
40
41 struct composite_context *wb_cmd_getdcname_send(TALLOC_CTX *mem_ctx,
42                                                 struct wbsrv_service *service,
43                                                 const char *domain_name)
44 {
45         struct composite_context *result, *ctx;
46         struct cmd_getdcname_state *state;
47
48         result = talloc(mem_ctx, struct composite_context);
49         if (result == NULL) goto failed;
50         result->state = COMPOSITE_STATE_IN_PROGRESS;
51         result->async.fn = NULL;
52         result->event_ctx = service->task->event_ctx;
53
54         state = talloc(result, struct cmd_getdcname_state);
55         if (state == NULL) goto failed;
56         state->ctx = result;
57         result->private_data = state;
58
59         state->domain_name = talloc_strdup(state, domain_name);
60         if (state->domain_name == NULL) goto failed;
61
62         ctx = wb_sid2domain_send(state, service, service->primary_sid);
63         if (ctx == NULL) goto failed;
64
65         ctx->async.fn = getdcname_recv_domain;
66         ctx->async.private_data = state;
67         return result;
68
69  failed:
70         talloc_free(result);
71         return NULL;
72 }
73
74 static void getdcname_recv_domain(struct composite_context *ctx)
75 {
76         struct cmd_getdcname_state *state =
77                 talloc_get_type(ctx->async.private_data,
78                                 struct cmd_getdcname_state);
79         struct wbsrv_domain *domain;
80         struct rpc_request *req;
81
82         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
83         if (!composite_is_ok(state->ctx)) return;
84
85         state->g.in.logon_server = talloc_asprintf(
86                 state, "\\\\%s",
87                 dcerpc_server_name(domain->netlogon_pipe));
88         state->g.in.domainname = state->domain_name;
89
90         req = dcerpc_netr_GetAnyDCName_send(domain->netlogon_pipe, state,
91                                             &state->g);
92         if (composite_nomem(req, state->ctx)) return;
93
94         composite_continue_rpc(state->ctx, req, getdcname_recv_dcname, state);
95 }
96
97 static void getdcname_recv_dcname(struct rpc_request *req)
98 {
99         struct cmd_getdcname_state *state =
100                 talloc_get_type(req->async.private,
101                                 struct cmd_getdcname_state);
102
103         state->ctx->status = dcerpc_ndr_request_recv(req);
104         if (!composite_is_ok(state->ctx)) return;
105         state->ctx->status = werror_to_ntstatus(state->g.out.result);
106         if (!composite_is_ok(state->ctx)) return;
107
108         composite_done(state->ctx);
109 }
110
111 NTSTATUS wb_cmd_getdcname_recv(struct composite_context *c,
112                                TALLOC_CTX *mem_ctx,
113                                const char **dcname)
114 {
115         struct cmd_getdcname_state *state =
116                 talloc_get_type(c->private_data, struct cmd_getdcname_state);
117         NTSTATUS status = composite_wait(c);
118         if (NT_STATUS_IS_OK(status)) {
119                 const char *p = state->g.out.dcname;
120                 if (*p == '\\') p += 1;
121                 if (*p == '\\') p += 1;
122                 *dcname = talloc_strdup(mem_ctx, p);
123                 if (*dcname == NULL) {
124                         status = NT_STATUS_NO_MEMORY;
125                 }
126         }
127         talloc_free(state);
128         return status;
129 }