r11181: Implement wbinfo -s and wbinfo --user-sids. The patch is so large because
[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 static void composite_netr_GetAnyDCName_recv_rpc(struct rpc_request *req);
32
33 static struct composite_context *composite_netr_GetAnyDCName_send(struct dcerpc_pipe *p,
34                                                                   TALLOC_CTX *mem_ctx,
35                                                                   struct netr_GetAnyDCName *r)
36 {
37         struct composite_context *result;
38         struct rpc_request *req;
39
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;
45
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;
50         return result;
51
52  failed:
53         talloc_free(result);
54         return NULL;
55 }
56
57 static void composite_netr_GetAnyDCName_recv_rpc(struct rpc_request *req)
58 {
59         struct composite_context *ctx =
60                 talloc_get_type(req->async.private, struct composite_context);
61
62         ctx->status = dcerpc_ndr_request_recv(req);
63         if (!composite_is_ok(ctx)) return;
64         composite_done(ctx);
65 }
66
67 static NTSTATUS composite_netr_GetAnyDCName_recv(struct composite_context *ctx)
68 {
69         NTSTATUS status = composite_wait(ctx);
70         talloc_free(ctx);
71         return status;
72 }
73
74 struct cmd_getdcname_state {
75         struct composite_context *ctx;
76         const char *domain_name;
77
78         struct netr_GetAnyDCName g;
79 };
80
81 static struct composite_context *getdcname_send_req(struct wbsrv_domain *domain,
82                                                     void *p);
83 static NTSTATUS getdcname_recv_req(struct composite_context *ctx, void *p);
84
85 struct composite_context *wb_cmd_getdcname_send(struct wbsrv_service *service,
86                                                 struct wbsrv_domain *domain,
87                                                 const char *domain_name)
88 {
89         struct cmd_getdcname_state *state;
90
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,
94                                             service->primary_sid,
95                                             getdcname_send_req,
96                                             getdcname_recv_req,
97                                             state);
98         if (state->ctx == NULL) {
99                 talloc_free(state);
100                 return NULL;
101         }
102         state->ctx->private_data = state;
103         return state->ctx;
104 }
105
106 static struct composite_context *getdcname_send_req(struct wbsrv_domain *domain, void *p)
107 {
108         struct cmd_getdcname_state *state =
109                 talloc_get_type(p, struct cmd_getdcname_state);
110
111         state->g.in.logon_server = talloc_asprintf(
112                 state, "\\\\%s",
113                 dcerpc_server_name(domain->netlogon_pipe));
114         state->g.in.domainname = state->domain_name;
115
116         return composite_netr_GetAnyDCName_send(domain->netlogon_pipe,
117                                                 state, &state->g);
118 }
119
120 static NTSTATUS getdcname_recv_req(struct composite_context *ctx, void *p)
121 {
122         struct cmd_getdcname_state *state =
123                 talloc_get_type(p, struct cmd_getdcname_state);
124         NTSTATUS status;
125
126         status = composite_netr_GetAnyDCName_recv(ctx);
127         NT_STATUS_NOT_OK_RETURN(status);
128
129         if (!W_ERROR_IS_OK(state->g.out.result)) {
130                 return werror_to_ntstatus(state->g.out.result);
131         }
132
133         return NT_STATUS_OK;
134 }
135
136 NTSTATUS wb_cmd_getdcname_recv(struct composite_context *c,
137                                TALLOC_CTX *mem_ctx,
138                                const char **dcname)
139 {
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;
150                 }
151         }
152         talloc_free(state);
153         return status;
154 }