s4-messaging Rename messaging -> imessaging
[kai/samba.git] / source4 / libcli / wbclient / wbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind client library.
5
6    Copyright (C) 2008 Kai Blin  <kai@samba.org>
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include <tevent.h>
24 #include "libcli/wbclient/wbclient.h"
25
26 /**
27  * Initialize the wbclient context, talloc_free() when done.
28  *
29  * \param mem_ctx talloc context to allocate memory from
30  * \param msg_ctx message context to use
31  * \param
32  */
33 struct wbc_context *wbc_init(TALLOC_CTX *mem_ctx,
34                              struct imessaging_context *msg_ctx,
35                              struct tevent_context *event_ctx)
36 {
37         struct wbc_context *ctx;
38
39         ctx = talloc(mem_ctx, struct wbc_context);
40         if (ctx == NULL) return NULL;
41
42         ctx->event_ctx = event_ctx;
43
44         ctx->irpc_handle = irpc_binding_handle_by_name(ctx, msg_ctx,
45                                                        "winbind_server",
46                                                        &ndr_table_winbind);
47         if (ctx->irpc_handle == NULL) {
48                 talloc_free(ctx);
49                 return NULL;
50         }
51
52         return ctx;
53 }
54
55 struct wbc_idmap_state {
56         struct composite_context *ctx;
57         struct winbind_get_idmap *req;
58         struct id_map *ids;
59 };
60
61 static void sids_to_xids_recv_ids(struct tevent_req *subreq);
62
63 struct composite_context *wbc_sids_to_xids_send(struct wbc_context *wbc_ctx,
64                                                 TALLOC_CTX *mem_ctx,
65                                                 uint32_t count,
66                                                 struct id_map *ids)
67 {
68         struct composite_context *ctx;
69         struct wbc_idmap_state *state;
70         struct tevent_req *subreq;
71
72         DEBUG(5, ("wbc_sids_to_xids called\n"));
73
74         ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
75         if (ctx == NULL) return NULL;
76
77         state = talloc(ctx, struct wbc_idmap_state);
78         if (composite_nomem(state, ctx)) return ctx;
79         ctx->private_data = state;
80
81         state->req = talloc(state, struct winbind_get_idmap);
82         if (composite_nomem(state->req, ctx)) return ctx;
83
84         state->req->in.count = count;
85         state->req->in.level = WINBIND_IDMAP_LEVEL_SIDS_TO_XIDS;
86         state->req->in.ids = ids;
87         state->ctx = ctx;
88
89         subreq = dcerpc_winbind_get_idmap_r_send(state,
90                                                  wbc_ctx->event_ctx,
91                                                  wbc_ctx->irpc_handle,
92                                                  state->req);
93         if (composite_nomem(subreq, ctx)) return ctx;
94
95         tevent_req_set_callback(subreq, sids_to_xids_recv_ids, state);
96
97         return ctx;
98 }
99
100 static void sids_to_xids_recv_ids(struct tevent_req *subreq)
101 {
102         struct wbc_idmap_state *state =
103                 tevent_req_callback_data(subreq,
104                 struct wbc_idmap_state);
105
106         state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
107         TALLOC_FREE(subreq);
108         if (!composite_is_ok(state->ctx)) return;
109
110         state->ids = state->req->out.ids;
111         composite_done(state->ctx);
112 }
113
114 NTSTATUS wbc_sids_to_xids_recv(struct composite_context *ctx,
115                                struct id_map **ids)
116 {
117         NTSTATUS status = composite_wait(ctx);
118                 DEBUG(5, ("wbc_sids_to_xids_recv called\n"));
119         if (NT_STATUS_IS_OK(status)) {
120                 struct wbc_idmap_state *state = talloc_get_type_abort(
121                                                         ctx->private_data,
122                                                         struct wbc_idmap_state);
123                 *ids = state->ids;
124         }
125
126         return status;
127 }
128
129 static void xids_to_sids_recv_ids(struct tevent_req *subreq);
130
131 struct composite_context *wbc_xids_to_sids_send(struct wbc_context *wbc_ctx,
132                                                 TALLOC_CTX *mem_ctx,
133                                                 uint32_t count,
134                                                 struct id_map *ids)
135 {
136         struct composite_context *ctx;
137         struct wbc_idmap_state *state;
138         struct tevent_req *subreq;
139
140         DEBUG(5, ("wbc_xids_to_sids called\n"));
141
142         ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
143         if (ctx == NULL) return NULL;
144
145         state = talloc(ctx, struct wbc_idmap_state);
146         if (composite_nomem(state, ctx)) return ctx;
147         ctx->private_data = state;
148
149         state->req = talloc(state, struct winbind_get_idmap);
150         if (composite_nomem(state->req, ctx)) return ctx;
151
152         state->req->in.count = count;
153         state->req->in.level = WINBIND_IDMAP_LEVEL_XIDS_TO_SIDS;
154         state->req->in.ids = ids;
155         state->ctx = ctx;
156
157         subreq = dcerpc_winbind_get_idmap_r_send(state,
158                                                  wbc_ctx->event_ctx,
159                                                  wbc_ctx->irpc_handle,
160                                                  state->req);
161         if (composite_nomem(subreq, ctx)) return ctx;
162
163         tevent_req_set_callback(subreq, xids_to_sids_recv_ids, state);
164
165         return ctx;
166 }
167
168 static void xids_to_sids_recv_ids(struct tevent_req *subreq)
169 {
170         struct wbc_idmap_state *state =
171                 tevent_req_callback_data(subreq,
172                 struct wbc_idmap_state);
173
174         state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
175         TALLOC_FREE(subreq);
176         if (!composite_is_ok(state->ctx)) return;
177
178         state->ids = state->req->out.ids;
179         composite_done(state->ctx);
180 }
181
182 NTSTATUS wbc_xids_to_sids_recv(struct composite_context *ctx,
183                                struct id_map **ids)
184 {
185         NTSTATUS status = composite_wait(ctx);
186                 DEBUG(5, ("wbc_xids_to_sids_recv called\n"));
187         if (NT_STATUS_IS_OK(status)) {
188                 struct wbc_idmap_state *state = talloc_get_type_abort(
189                                                         ctx->private_data,
190                                                         struct wbc_idmap_state);
191                 *ids = state->ids;
192         }
193
194         return status;
195 }
196