2 Unix SMB/CIFS implementation.
4 Generic Authentication Interface
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 Copyright (C) Stefan Metzmacher 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/composite/composite.h"
26 #include "auth/gensec/gensec.h"
27 #include "librpc/rpc/dcerpc.h"
28 #include "param/param.h"
31 return the rpc syntax and transfer syntax given the pipe uuid and version
33 static NTSTATUS dcerpc_init_syntaxes(const struct ndr_interface_table *table,
34 struct ndr_syntax_id *syntax,
35 struct ndr_syntax_id *transfer_syntax)
37 syntax->uuid = table->syntax_id.uuid;
38 syntax->if_version = table->syntax_id.if_version;
40 *transfer_syntax = ndr_transfer_syntax;
47 Send request to do a non-authenticated dcerpc bind
49 struct composite_context *dcerpc_bind_auth_none_send(TALLOC_CTX *mem_ctx,
50 struct dcerpc_pipe *p,
51 const struct ndr_interface_table *table)
53 struct ndr_syntax_id syntax;
54 struct ndr_syntax_id transfer_syntax;
56 struct composite_context *c;
58 c = composite_create(mem_ctx, p->conn->event_ctx);
59 if (c == NULL) return NULL;
61 c->status = dcerpc_init_syntaxes(table,
62 &syntax, &transfer_syntax);
63 if (!NT_STATUS_IS_OK(c->status)) {
64 DEBUG(2,("Invalid uuid string in "
65 "dcerpc_bind_auth_none_send\n"));
66 composite_error(c, c->status);
70 /* c was only allocated as a container for a possible error */
73 return dcerpc_bind_send(p, mem_ctx, &syntax, &transfer_syntax);
78 Receive result of a non-authenticated dcerpc bind
80 NTSTATUS dcerpc_bind_auth_none_recv(struct composite_context *ctx)
82 return dcerpc_bind_recv(ctx);
87 Perform sync non-authenticated dcerpc bind
89 NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
90 const struct ndr_interface_table *table)
92 struct composite_context *ctx;
94 ctx = dcerpc_bind_auth_none_send(p, p, table);
95 return dcerpc_bind_auth_none_recv(ctx);
99 struct bind_auth_state {
100 struct dcerpc_pipe *pipe;
101 DATA_BLOB credentials;
102 bool more_processing; /* Is there anything more to do after the
103 * first bind itself received? */
106 static void bind_auth_recv_alter(struct composite_context *creq);
108 static void bind_auth_next_step(struct composite_context *c)
110 struct bind_auth_state *state;
111 struct dcerpc_security *sec;
112 struct composite_context *creq;
113 bool more_processing = false;
115 state = talloc_get_type(c->private_data, struct bind_auth_state);
116 sec = &state->pipe->conn->security_state;
118 /* The status value here, from GENSEC is vital to the security
119 * of the system. Even if the other end accepts, if GENSEC
120 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
121 * feeding it blobs, or else the remote host/attacker might
122 * avoid mutal authentication requirements.
124 * Likewise, you must not feed GENSEC too much (after the OK),
125 * it doesn't like that either
128 c->status = gensec_update(sec->generic_state, state,
129 sec->auth_info->credentials,
130 &state->credentials);
132 if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
133 more_processing = true;
134 c->status = NT_STATUS_OK;
137 if (!composite_is_ok(c)) return;
139 if (state->credentials.length == 0) {
144 sec->auth_info->credentials = state->credentials;
146 if (!more_processing) {
147 /* NO reply expected, so just send it */
148 c->status = dcerpc_auth3(state->pipe->conn, state);
149 if (!composite_is_ok(c)) return;
155 /* We are demanding a reply, so use a request that will get us one */
157 creq = dcerpc_alter_context_send(state->pipe, state,
158 &state->pipe->syntax,
159 &state->pipe->transfer_syntax);
160 if (composite_nomem(creq, c)) return;
162 composite_continue(c, creq, bind_auth_recv_alter, c);
166 static void bind_auth_recv_alter(struct composite_context *creq)
168 struct composite_context *c = talloc_get_type(creq->async.private_data,
169 struct composite_context);
171 c->status = dcerpc_alter_context_recv(creq);
172 if (!composite_is_ok(c)) return;
174 bind_auth_next_step(c);
178 static void bind_auth_recv_bindreply(struct composite_context *creq)
180 struct composite_context *c = talloc_get_type(creq->async.private_data,
181 struct composite_context);
182 struct bind_auth_state *state = talloc_get_type(c->private_data,
183 struct bind_auth_state);
185 c->status = dcerpc_bind_recv(creq);
186 if (!composite_is_ok(c)) return;
188 if (!state->more_processing) {
189 /* The first gensec_update has not requested a second run, so
190 * we're done here. */
195 bind_auth_next_step(c);
200 Bind to a DCE/RPC pipe, send async request
201 @param mem_ctx TALLOC_CTX for the allocation of the composite_context
202 @param p The dcerpc_pipe to bind (must already be connected)
203 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
204 @param credentials The credentials of the account to connect with
205 @param auth_type Select the authentication scheme to use
206 @param auth_level Chooses between unprotected (connect), signed or sealed
207 @param service The service (used by Kerberos to select the service principal to contact)
208 @retval A composite context describing the partial state of the bind
211 struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
212 struct dcerpc_pipe *p,
213 const struct ndr_interface_table *table,
214 struct cli_credentials *credentials,
215 struct loadparm_context *lp_ctx,
216 uint8_t auth_type, uint8_t auth_level,
219 struct composite_context *c, *creq;
220 struct bind_auth_state *state;
221 struct dcerpc_security *sec;
223 struct ndr_syntax_id syntax, transfer_syntax;
225 /* composite context allocation and setup */
226 c = composite_create(mem_ctx, p->conn->event_ctx);
227 if (c == NULL) return NULL;
229 state = talloc(c, struct bind_auth_state);
230 if (composite_nomem(state, c)) return c;
231 c->private_data = state;
235 c->status = dcerpc_init_syntaxes(table,
238 if (!composite_is_ok(c)) return c;
240 sec = &p->conn->security_state;
242 c->status = gensec_client_start(p, &sec->generic_state,
245 if (!NT_STATUS_IS_OK(c->status)) {
246 DEBUG(1, ("Failed to start GENSEC client mode: %s\n",
247 nt_errstr(c->status)));
248 composite_error(c, c->status);
252 c->status = gensec_set_credentials(sec->generic_state, credentials);
253 if (!NT_STATUS_IS_OK(c->status)) {
254 DEBUG(1, ("Failed to set GENSEC client credentials: %s\n",
255 nt_errstr(c->status)));
256 composite_error(c, c->status);
260 c->status = gensec_set_target_hostname(sec->generic_state,
261 p->conn->transport.target_hostname(p->conn));
262 if (!NT_STATUS_IS_OK(c->status)) {
263 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
264 nt_errstr(c->status)));
265 composite_error(c, c->status);
269 if (service != NULL) {
270 c->status = gensec_set_target_service(sec->generic_state,
272 if (!NT_STATUS_IS_OK(c->status)) {
273 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
274 nt_errstr(c->status)));
275 composite_error(c, c->status);
280 c->status = gensec_start_mech_by_authtype(sec->generic_state,
281 auth_type, auth_level);
282 if (!NT_STATUS_IS_OK(c->status)) {
283 DEBUG(1, ("Failed to start GENSEC client mechanism %s: %s\n",
284 gensec_get_name_by_authtype(auth_type),
285 nt_errstr(c->status)));
286 composite_error(c, c->status);
290 sec->auth_info = talloc(p, struct dcerpc_auth);
291 if (composite_nomem(sec->auth_info, c)) return c;
293 sec->auth_info->auth_type = auth_type;
294 sec->auth_info->auth_level = auth_level,
295 sec->auth_info->auth_pad_length = 0;
296 sec->auth_info->auth_reserved = 0;
297 sec->auth_info->auth_context_id = random();
298 sec->auth_info->credentials = data_blob(NULL, 0);
300 /* The status value here, from GENSEC is vital to the security
301 * of the system. Even if the other end accepts, if GENSEC
302 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
303 * feeding it blobs, or else the remote host/attacker might
304 * avoid mutal authentication requirements.
306 * Likewise, you must not feed GENSEC too much (after the OK),
307 * it doesn't like that either
310 c->status = gensec_update(sec->generic_state, state,
311 sec->auth_info->credentials,
312 &state->credentials);
313 if (!NT_STATUS_IS_OK(c->status) &&
314 !NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
315 composite_error(c, c->status);
319 state->more_processing = NT_STATUS_EQUAL(c->status,
320 NT_STATUS_MORE_PROCESSING_REQUIRED);
322 if (state->credentials.length == 0) {
327 sec->auth_info->credentials = state->credentials;
329 /* The first request always is a dcerpc_bind. The subsequent ones
330 * depend on gensec results */
331 creq = dcerpc_bind_send(p, state, &syntax, &transfer_syntax);
332 if (composite_nomem(creq, c)) return c;
334 composite_continue(c, creq, bind_auth_recv_bindreply, c);
340 Bind to a DCE/RPC pipe, receive result
341 @param creq A composite context describing state of async call
342 @retval NTSTATUS code
345 NTSTATUS dcerpc_bind_auth_recv(struct composite_context *creq)
347 NTSTATUS result = composite_wait(creq);
348 struct bind_auth_state *state = talloc_get_type(creq->private_data,
349 struct bind_auth_state);
351 if (NT_STATUS_IS_OK(result)) {
353 after a successful authenticated bind the session
354 key reverts to the generic session key
356 state->pipe->conn->security_state.session_key = dcerpc_generic_session_key;
365 Perform a GENSEC authenticated bind to a DCE/RPC pipe, sync
366 @param p The dcerpc_pipe to bind (must already be connected)
367 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
368 @param credentials The credentials of the account to connect with
369 @param auth_type Select the authentication scheme to use
370 @param auth_level Chooses between unprotected (connect), signed or sealed
371 @param service The service (used by Kerberos to select the service principal to contact)
372 @retval NTSTATUS status code
375 NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p,
376 const struct ndr_interface_table *table,
377 struct cli_credentials *credentials,
378 struct loadparm_context *lp_ctx,
379 uint8_t auth_type, uint8_t auth_level,
382 struct composite_context *creq;
383 creq = dcerpc_bind_auth_send(p, p, table, credentials, lp_ctx,
384 auth_type, auth_level, service);
385 return dcerpc_bind_auth_recv(creq);