tevent: revalidate fde->flags after poll()
[Samba/gebeck_regimport.git] / source4 / libcli / wbclient / wbclient.c
blob4f50c106947b42b785f200b74dbe8e9efbe66161
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client library.
6 Copyright (C) 2008 Kai Blin <kai@samba.org>
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.
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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include <tevent.h>
24 #include "libcli/wbclient/wbclient.h"
26 /**
27 * Initialize the wbclient context, talloc_free() when done.
29 * \param mem_ctx talloc context to allocate memory from
30 * \param msg_ctx message context to use
31 * \param
33 struct wbc_context *wbc_init(TALLOC_CTX *mem_ctx,
34 struct imessaging_context *msg_ctx,
35 struct tevent_context *event_ctx)
37 struct wbc_context *ctx;
39 ctx = talloc(mem_ctx, struct wbc_context);
40 if (ctx == NULL) return NULL;
42 ctx->event_ctx = event_ctx;
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;
52 return ctx;
55 struct wbc_idmap_state {
56 struct composite_context *ctx;
57 struct winbind_get_idmap *req;
58 struct id_map *ids;
61 static void sids_to_xids_recv_ids(struct tevent_req *subreq);
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)
68 struct composite_context *ctx;
69 struct wbc_idmap_state *state;
70 struct tevent_req *subreq;
72 DEBUG(5, ("wbc_sids_to_xids called\n"));
74 ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
75 if (ctx == NULL) return NULL;
77 state = talloc(ctx, struct wbc_idmap_state);
78 if (composite_nomem(state, ctx)) return ctx;
79 ctx->private_data = state;
81 state->req = talloc(state, struct winbind_get_idmap);
82 if (composite_nomem(state->req, ctx)) return ctx;
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;
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;
95 tevent_req_set_callback(subreq, sids_to_xids_recv_ids, state);
97 return ctx;
100 static void sids_to_xids_recv_ids(struct tevent_req *subreq)
102 struct wbc_idmap_state *state =
103 tevent_req_callback_data(subreq,
104 struct wbc_idmap_state);
106 state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
107 TALLOC_FREE(subreq);
108 if (!composite_is_ok(state->ctx)) return;
110 state->ids = state->req->out.ids;
111 composite_done(state->ctx);
114 NTSTATUS wbc_sids_to_xids_recv(struct composite_context *ctx,
115 struct id_map **ids)
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;
126 return status;
129 static void xids_to_sids_recv_ids(struct tevent_req *subreq);
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)
136 struct composite_context *ctx;
137 struct wbc_idmap_state *state;
138 struct tevent_req *subreq;
140 DEBUG(5, ("wbc_xids_to_sids called\n"));
142 ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
143 if (ctx == NULL) return NULL;
145 state = talloc(ctx, struct wbc_idmap_state);
146 if (composite_nomem(state, ctx)) return ctx;
147 ctx->private_data = state;
149 state->req = talloc(state, struct winbind_get_idmap);
150 if (composite_nomem(state->req, ctx)) return ctx;
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;
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;
163 tevent_req_set_callback(subreq, xids_to_sids_recv_ids, state);
165 return ctx;
168 static void xids_to_sids_recv_ids(struct tevent_req *subreq)
170 struct wbc_idmap_state *state =
171 tevent_req_callback_data(subreq,
172 struct wbc_idmap_state);
174 state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
175 TALLOC_FREE(subreq);
176 if (!composite_is_ok(state->ctx)) return;
178 state->ids = state->req->out.ids;
179 composite_done(state->ctx);
182 NTSTATUS wbc_xids_to_sids_recv(struct composite_context *ctx,
183 struct id_map **ids)
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;
194 return status;