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 "librpc/rpc/dcerpc_proto.h"
29 #include "param/param.h"
32 return the rpc syntax and transfer syntax given the pipe uuid and version
34 static NTSTATUS dcerpc_init_syntaxes(const struct ndr_interface_table *table,
35 struct ndr_syntax_id *syntax,
36 struct ndr_syntax_id *transfer_syntax)
38 syntax->uuid = table->syntax_id.uuid;
39 syntax->if_version = table->syntax_id.if_version;
41 *transfer_syntax = ndr_transfer_syntax;
48 Send request to do a non-authenticated dcerpc bind
50 struct composite_context *dcerpc_bind_auth_none_send(TALLOC_CTX *mem_ctx,
51 struct dcerpc_pipe *p,
52 const struct ndr_interface_table *table)
54 struct ndr_syntax_id syntax;
55 struct ndr_syntax_id transfer_syntax;
57 struct composite_context *c;
59 c = composite_create(mem_ctx, p->conn->event_ctx);
60 if (c == NULL) return NULL;
62 c->status = dcerpc_init_syntaxes(table,
63 &syntax, &transfer_syntax);
64 if (!NT_STATUS_IS_OK(c->status)) {
65 DEBUG(2,("Invalid uuid string in "
66 "dcerpc_bind_auth_none_send\n"));
67 composite_error(c, c->status);
71 /* c was only allocated as a container for a possible error */
74 return dcerpc_bind_send(p, mem_ctx, &syntax, &transfer_syntax);
79 Receive result of a non-authenticated dcerpc bind
81 NTSTATUS dcerpc_bind_auth_none_recv(struct composite_context *ctx)
83 return dcerpc_bind_recv(ctx);
88 Perform sync non-authenticated dcerpc bind
90 _PUBLIC_ NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
91 const struct ndr_interface_table *table)
93 struct composite_context *ctx;
95 ctx = dcerpc_bind_auth_none_send(p, p, table);
96 return dcerpc_bind_auth_none_recv(ctx);
100 struct bind_auth_state {
101 struct dcerpc_pipe *pipe;
102 DATA_BLOB credentials;
103 bool more_processing; /* Is there anything more to do after the
104 * first bind itself received? */
107 static void bind_auth_recv_alter(struct composite_context *creq);
109 static void bind_auth_next_step(struct composite_context *c)
111 struct bind_auth_state *state;
112 struct dcerpc_security *sec;
113 struct composite_context *creq;
114 bool more_processing = false;
116 state = talloc_get_type(c->private_data, struct bind_auth_state);
117 sec = &state->pipe->conn->security_state;
119 /* The status value here, from GENSEC is vital to the security
120 * of the system. Even if the other end accepts, if GENSEC
121 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
122 * feeding it blobs, or else the remote host/attacker might
123 * avoid mutal authentication requirements.
125 * Likewise, you must not feed GENSEC too much (after the OK),
126 * it doesn't like that either
129 c->status = gensec_update(sec->generic_state, state,
130 sec->auth_info->credentials,
131 &state->credentials);
133 if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
134 more_processing = true;
135 c->status = NT_STATUS_OK;
138 if (!composite_is_ok(c)) return;
140 if (state->credentials.length == 0) {
145 sec->auth_info->credentials = state->credentials;
147 if (!more_processing) {
148 /* NO reply expected, so just send it */
149 c->status = dcerpc_auth3(state->pipe, state);
150 if (!composite_is_ok(c)) return;
156 /* We are demanding a reply, so use a request that will get us one */
158 creq = dcerpc_alter_context_send(state->pipe, state,
159 &state->pipe->syntax,
160 &state->pipe->transfer_syntax);
161 if (composite_nomem(creq, c)) return;
163 composite_continue(c, creq, bind_auth_recv_alter, c);
167 static void bind_auth_recv_alter(struct composite_context *creq)
169 struct composite_context *c = talloc_get_type(creq->async.private_data,
170 struct composite_context);
172 c->status = dcerpc_alter_context_recv(creq);
173 if (!composite_is_ok(c)) return;
175 bind_auth_next_step(c);
179 static void bind_auth_recv_bindreply(struct composite_context *creq)
181 struct composite_context *c = talloc_get_type(creq->async.private_data,
182 struct composite_context);
183 struct bind_auth_state *state = talloc_get_type(c->private_data,
184 struct bind_auth_state);
186 c->status = dcerpc_bind_recv(creq);
187 if (!composite_is_ok(c)) return;
189 if (!state->more_processing) {
190 /* The first gensec_update has not requested a second run, so
191 * we're done here. */
196 bind_auth_next_step(c);
201 Bind to a DCE/RPC pipe, send async request
202 @param mem_ctx TALLOC_CTX for the allocation of the composite_context
203 @param p The dcerpc_pipe to bind (must already be connected)
204 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
205 @param credentials The credentials of the account to connect with
206 @param auth_type Select the authentication scheme to use
207 @param auth_level Chooses between unprotected (connect), signed or sealed
208 @param service The service (used by Kerberos to select the service principal to contact)
209 @retval A composite context describing the partial state of the bind
212 struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
213 struct dcerpc_pipe *p,
214 const struct ndr_interface_table *table,
215 struct cli_credentials *credentials,
216 struct loadparm_context *lp_ctx,
217 uint8_t auth_type, uint8_t auth_level,
220 struct composite_context *c, *creq;
221 struct bind_auth_state *state;
222 struct dcerpc_security *sec;
224 struct ndr_syntax_id syntax, transfer_syntax;
226 /* composite context allocation and setup */
227 c = composite_create(mem_ctx, p->conn->event_ctx);
228 if (c == NULL) return NULL;
230 state = talloc(c, struct bind_auth_state);
231 if (composite_nomem(state, c)) return c;
232 c->private_data = state;
236 c->status = dcerpc_init_syntaxes(table,
239 if (!composite_is_ok(c)) return c;
241 sec = &p->conn->security_state;
243 c->status = gensec_client_start(p, &sec->generic_state,
246 if (!NT_STATUS_IS_OK(c->status)) {
247 DEBUG(1, ("Failed to start GENSEC client mode: %s\n",
248 nt_errstr(c->status)));
249 composite_error(c, c->status);
253 c->status = gensec_set_credentials(sec->generic_state, credentials);
254 if (!NT_STATUS_IS_OK(c->status)) {
255 DEBUG(1, ("Failed to set GENSEC client credentials: %s\n",
256 nt_errstr(c->status)));
257 composite_error(c, c->status);
261 c->status = gensec_set_target_hostname(sec->generic_state,
262 p->conn->transport.target_hostname(p->conn));
263 if (!NT_STATUS_IS_OK(c->status)) {
264 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
265 nt_errstr(c->status)));
266 composite_error(c, c->status);
270 if (service != NULL) {
271 c->status = gensec_set_target_service(sec->generic_state,
273 if (!NT_STATUS_IS_OK(c->status)) {
274 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
275 nt_errstr(c->status)));
276 composite_error(c, c->status);
281 c->status = gensec_start_mech_by_authtype(sec->generic_state,
282 auth_type, auth_level);
283 if (!NT_STATUS_IS_OK(c->status)) {
284 DEBUG(1, ("Failed to start GENSEC client mechanism %s: %s\n",
285 gensec_get_name_by_authtype(auth_type),
286 nt_errstr(c->status)));
287 composite_error(c, c->status);
291 sec->auth_info = talloc(p, struct dcerpc_auth);
292 if (composite_nomem(sec->auth_info, c)) return c;
294 sec->auth_info->auth_type = auth_type;
295 sec->auth_info->auth_level = auth_level,
296 sec->auth_info->auth_pad_length = 0;
297 sec->auth_info->auth_reserved = 0;
298 sec->auth_info->auth_context_id = random();
299 sec->auth_info->credentials = data_blob(NULL, 0);
301 /* The status value here, from GENSEC is vital to the security
302 * of the system. Even if the other end accepts, if GENSEC
303 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
304 * feeding it blobs, or else the remote host/attacker might
305 * avoid mutal authentication requirements.
307 * Likewise, you must not feed GENSEC too much (after the OK),
308 * it doesn't like that either
311 c->status = gensec_update(sec->generic_state, state,
312 sec->auth_info->credentials,
313 &state->credentials);
314 if (!NT_STATUS_IS_OK(c->status) &&
315 !NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
316 composite_error(c, c->status);
320 state->more_processing = NT_STATUS_EQUAL(c->status,
321 NT_STATUS_MORE_PROCESSING_REQUIRED);
323 if (state->credentials.length == 0) {
328 sec->auth_info->credentials = state->credentials;
330 /* The first request always is a dcerpc_bind. The subsequent ones
331 * depend on gensec results */
332 creq = dcerpc_bind_send(p, state, &syntax, &transfer_syntax);
333 if (composite_nomem(creq, c)) return c;
335 composite_continue(c, creq, bind_auth_recv_bindreply, c);
341 Bind to a DCE/RPC pipe, receive result
342 @param creq A composite context describing state of async call
343 @retval NTSTATUS code
346 NTSTATUS dcerpc_bind_auth_recv(struct composite_context *creq)
348 NTSTATUS result = composite_wait(creq);
349 struct bind_auth_state *state = talloc_get_type(creq->private_data,
350 struct bind_auth_state);
352 if (NT_STATUS_IS_OK(result)) {
354 after a successful authenticated bind the session
355 key reverts to the generic session key
357 state->pipe->conn->security_state.session_key = dcerpc_generic_session_key;
366 Perform a GENSEC authenticated bind to a DCE/RPC pipe, sync
367 @param p The dcerpc_pipe to bind (must already be connected)
368 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
369 @param credentials The credentials of the account to connect with
370 @param auth_type Select the authentication scheme to use
371 @param auth_level Chooses between unprotected (connect), signed or sealed
372 @param service The service (used by Kerberos to select the service principal to contact)
373 @retval NTSTATUS status code
376 _PUBLIC_ NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p,
377 const struct ndr_interface_table *table,
378 struct cli_credentials *credentials,
379 struct loadparm_context *lp_ctx,
380 uint8_t auth_type, uint8_t auth_level,
383 struct composite_context *creq;
384 creq = dcerpc_bind_auth_send(p, p, table, credentials, lp_ctx,
385 auth_type, auth_level, service);
386 return dcerpc_bind_auth_recv(creq);