Found by Guenther - fix up our fallback paths from krb5 to NTLMSSP when using SMB2.
[Samba/ekacnet.git] / source4 / libcli / wbclient / wbclient.c
blob3c3f166d06b3788dc02796271f07aa24b0547f42
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 "libcli/wbclient/wbclient.h"
25 /**
26 * Get the server_id of the winbind task.
28 * \param[in] msg_ctx message context to use
29 * \param[in] mem_ctx talloc context to use
30 * \param[out] ids array of server_id structs containing the winbind id
31 * \return NT_STATUS_OK on success, NT_STATUS_INTERNAL_ERROR on failure
33 static NTSTATUS get_server_id(struct messaging_context *msg_ctx,
34 TALLOC_CTX *mem_ctx, struct server_id **ids)
36 *ids = irpc_servers_byname(msg_ctx, mem_ctx, "winbind_server");
37 if (*ids == NULL || (*ids)[0].id == 0) {
38 DEBUG(0, ("Getting the winbind server ID failed.\n"));
39 return NT_STATUS_INTERNAL_ERROR;
41 return NT_STATUS_OK;
44 /**
45 * Initialize the wbclient context, talloc_free() when done.
47 * \param mem_ctx talloc context to allocate memory from
48 * \param msg_ctx message context to use
49 * \param
51 struct wbc_context *wbc_init(TALLOC_CTX *mem_ctx,
52 struct messaging_context *msg_ctx,
53 struct tevent_context *event_ctx)
55 struct wbc_context *ctx;
56 NTSTATUS status;
58 ctx = talloc(mem_ctx, struct wbc_context);
59 if (ctx == NULL) return NULL;
61 status = get_server_id(msg_ctx, mem_ctx, &ctx->ids);
62 if (!NT_STATUS_IS_OK(status)) {
63 talloc_free(ctx);
64 return NULL;
67 ctx->msg_ctx = msg_ctx;
68 ctx->event_ctx = event_ctx;
70 return ctx;
73 struct wbc_idmap_state {
74 struct composite_context *ctx;
75 struct winbind_get_idmap *req;
76 struct irpc_request *irpc_req;
77 struct id_map *ids;
80 static void sids_to_xids_recv_ids(struct irpc_request *req);
82 struct composite_context *wbc_sids_to_xids_send(struct wbc_context *wbc_ctx,
83 TALLOC_CTX *mem_ctx,
84 uint32_t count,
85 struct id_map *ids)
87 struct composite_context *ctx;
88 struct wbc_idmap_state *state;
90 DEBUG(5, ("wbc_sids_to_xids called\n"));
92 ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
93 if (ctx == NULL) return NULL;
95 state = talloc(ctx, struct wbc_idmap_state);
96 if (composite_nomem(state, ctx)) return ctx;
97 ctx->private_data = state;
99 state->req = talloc(state, struct winbind_get_idmap);
100 if (composite_nomem(state->req, ctx)) return ctx;
102 state->req->in.count = count;
103 state->req->in.level = WINBIND_IDMAP_LEVEL_SIDS_TO_XIDS;
104 state->req->in.ids = ids;
105 state->ctx = ctx;
107 state->irpc_req = IRPC_CALL_SEND(wbc_ctx->msg_ctx, wbc_ctx->ids[0],
108 winbind, WINBIND_GET_IDMAP, state->req,
109 state);
110 if (composite_nomem(state->irpc_req, ctx)) return ctx;
112 composite_continue_irpc(ctx, state->irpc_req, sids_to_xids_recv_ids,
113 state);
114 return ctx;
117 static void sids_to_xids_recv_ids(struct irpc_request *req)
119 struct wbc_idmap_state *state = talloc_get_type_abort(
120 req->async.private_data,
121 struct wbc_idmap_state);
123 state->ctx->status = irpc_call_recv(state->irpc_req);
124 if (!composite_is_ok(state->ctx)) return;
126 state->ids = state->req->out.ids;
127 composite_done(state->ctx);
130 NTSTATUS wbc_sids_to_xids_recv(struct composite_context *ctx,
131 struct id_map **ids)
133 NTSTATUS status = composite_wait(ctx);
134 DEBUG(5, ("wbc_sids_to_xids_recv called\n"));
135 if (NT_STATUS_IS_OK(status)) {
136 struct wbc_idmap_state *state = talloc_get_type_abort(
137 ctx->private_data,
138 struct wbc_idmap_state);
139 *ids = state->ids;
142 return status;
145 static void xids_to_sids_recv_ids(struct irpc_request *req);
147 struct composite_context *wbc_xids_to_sids_send(struct wbc_context *wbc_ctx,
148 TALLOC_CTX *mem_ctx,
149 uint32_t count,
150 struct id_map *ids)
152 struct composite_context *ctx;
153 struct wbc_idmap_state *state;
155 DEBUG(5, ("wbc_xids_to_sids called\n"));
157 ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
158 if (ctx == NULL) return NULL;
160 state = talloc(ctx, struct wbc_idmap_state);
161 if (composite_nomem(state, ctx)) return ctx;
162 ctx->private_data = state;
164 state->req = talloc(state, struct winbind_get_idmap);
165 if (composite_nomem(state->req, ctx)) return ctx;
167 state->req->in.count = count;
168 state->req->in.level = WINBIND_IDMAP_LEVEL_XIDS_TO_SIDS;
169 state->req->in.ids = ids;
170 state->ctx = ctx;
172 state->irpc_req = IRPC_CALL_SEND(wbc_ctx->msg_ctx, wbc_ctx->ids[0],
173 winbind, WINBIND_GET_IDMAP, state->req,
174 state);
175 if (composite_nomem(state->irpc_req, ctx)) return ctx;
177 composite_continue_irpc(ctx, state->irpc_req, xids_to_sids_recv_ids,
178 state);
180 return ctx;
183 static void xids_to_sids_recv_ids(struct irpc_request *req)
185 struct wbc_idmap_state *state = talloc_get_type_abort(
186 req->async.private_data,
187 struct wbc_idmap_state);
189 state->ctx->status = irpc_call_recv(state->irpc_req);
190 if (!composite_is_ok(state->ctx)) return;
192 state->ids = state->req->out.ids;
193 composite_done(state->ctx);
196 NTSTATUS wbc_xids_to_sids_recv(struct composite_context *ctx,
197 struct id_map **ids)
199 NTSTATUS status = composite_wait(ctx);
200 DEBUG(5, ("wbc_xids_to_sids_recv called\n"));
201 if (NT_STATUS_IS_OK(status)) {
202 struct wbc_idmap_state *state = talloc_get_type_abort(
203 ctx->private_data,
204 struct wbc_idmap_state);
205 *ids = state->ids;
208 return status;