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->pipe->conn->flags & DCERPC_HEADER_SIGNING) {
141 gensec_want_feature(sec->generic_state, GENSEC_FEATURE_SIGN_PKT_HEADER);
144 if (state->credentials.length == 0) {
149 sec->auth_info->credentials = state->credentials;
151 if (!more_processing) {
152 /* NO reply expected, so just send it */
153 c->status = dcerpc_auth3(state->pipe, state);
154 if (!composite_is_ok(c)) return;
160 /* We are demanding a reply, so use a request that will get us one */
162 creq = dcerpc_alter_context_send(state->pipe, state,
163 &state->pipe->syntax,
164 &state->pipe->transfer_syntax);
165 if (composite_nomem(creq, c)) return;
167 composite_continue(c, creq, bind_auth_recv_alter, c);
171 static void bind_auth_recv_alter(struct composite_context *creq)
173 struct composite_context *c = talloc_get_type(creq->async.private_data,
174 struct composite_context);
176 c->status = dcerpc_alter_context_recv(creq);
177 if (!composite_is_ok(c)) return;
179 bind_auth_next_step(c);
183 static void bind_auth_recv_bindreply(struct composite_context *creq)
185 struct composite_context *c = talloc_get_type(creq->async.private_data,
186 struct composite_context);
187 struct bind_auth_state *state = talloc_get_type(c->private_data,
188 struct bind_auth_state);
190 c->status = dcerpc_bind_recv(creq);
191 if (!composite_is_ok(c)) return;
193 if (!state->more_processing) {
194 /* The first gensec_update has not requested a second run, so
195 * we're done here. */
200 bind_auth_next_step(c);
205 Bind to a DCE/RPC pipe, send async request
206 @param mem_ctx TALLOC_CTX for the allocation of the composite_context
207 @param p The dcerpc_pipe to bind (must already be connected)
208 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
209 @param credentials The credentials of the account to connect with
210 @param auth_type Select the authentication scheme to use
211 @param auth_level Chooses between unprotected (connect), signed or sealed
212 @param service The service (used by Kerberos to select the service principal to contact)
213 @retval A composite context describing the partial state of the bind
216 struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
217 struct dcerpc_pipe *p,
218 const struct ndr_interface_table *table,
219 struct cli_credentials *credentials,
220 struct loadparm_context *lp_ctx,
221 uint8_t auth_type, uint8_t auth_level,
224 struct composite_context *c, *creq;
225 struct bind_auth_state *state;
226 struct dcerpc_security *sec;
228 struct ndr_syntax_id syntax, transfer_syntax;
230 /* composite context allocation and setup */
231 c = composite_create(mem_ctx, p->conn->event_ctx);
232 if (c == NULL) return NULL;
234 state = talloc(c, struct bind_auth_state);
235 if (composite_nomem(state, c)) return c;
236 c->private_data = state;
240 c->status = dcerpc_init_syntaxes(table,
243 if (!composite_is_ok(c)) return c;
245 sec = &p->conn->security_state;
247 c->status = gensec_client_start(p, &sec->generic_state,
250 if (!NT_STATUS_IS_OK(c->status)) {
251 DEBUG(1, ("Failed to start GENSEC client mode: %s\n",
252 nt_errstr(c->status)));
253 composite_error(c, c->status);
257 c->status = gensec_set_credentials(sec->generic_state, credentials);
258 if (!NT_STATUS_IS_OK(c->status)) {
259 DEBUG(1, ("Failed to set GENSEC client credentials: %s\n",
260 nt_errstr(c->status)));
261 composite_error(c, c->status);
265 c->status = gensec_set_target_hostname(sec->generic_state,
266 p->conn->transport.target_hostname(p->conn));
267 if (!NT_STATUS_IS_OK(c->status)) {
268 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
269 nt_errstr(c->status)));
270 composite_error(c, c->status);
274 if (service != NULL) {
275 c->status = gensec_set_target_service(sec->generic_state,
277 if (!NT_STATUS_IS_OK(c->status)) {
278 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
279 nt_errstr(c->status)));
280 composite_error(c, c->status);
285 c->status = gensec_start_mech_by_authtype(sec->generic_state,
286 auth_type, auth_level);
287 if (!NT_STATUS_IS_OK(c->status)) {
288 DEBUG(1, ("Failed to start GENSEC client mechanism %s: %s\n",
289 gensec_get_name_by_authtype(auth_type),
290 nt_errstr(c->status)));
291 composite_error(c, c->status);
295 sec->auth_info = talloc(p, struct dcerpc_auth);
296 if (composite_nomem(sec->auth_info, c)) return c;
298 sec->auth_info->auth_type = auth_type;
299 sec->auth_info->auth_level = auth_level,
300 sec->auth_info->auth_pad_length = 0;
301 sec->auth_info->auth_reserved = 0;
302 sec->auth_info->auth_context_id = random();
303 sec->auth_info->credentials = data_blob(NULL, 0);
305 /* The status value here, from GENSEC is vital to the security
306 * of the system. Even if the other end accepts, if GENSEC
307 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
308 * feeding it blobs, or else the remote host/attacker might
309 * avoid mutal authentication requirements.
311 * Likewise, you must not feed GENSEC too much (after the OK),
312 * it doesn't like that either
315 c->status = gensec_update(sec->generic_state, state,
316 sec->auth_info->credentials,
317 &state->credentials);
318 if (!NT_STATUS_IS_OK(c->status) &&
319 !NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
320 composite_error(c, c->status);
324 state->more_processing = NT_STATUS_EQUAL(c->status,
325 NT_STATUS_MORE_PROCESSING_REQUIRED);
327 if (state->credentials.length == 0) {
332 sec->auth_info->credentials = state->credentials;
334 /* The first request always is a dcerpc_bind. The subsequent ones
335 * depend on gensec results */
336 creq = dcerpc_bind_send(p, state, &syntax, &transfer_syntax);
337 if (composite_nomem(creq, c)) return c;
339 composite_continue(c, creq, bind_auth_recv_bindreply, c);
345 Bind to a DCE/RPC pipe, receive result
346 @param creq A composite context describing state of async call
347 @retval NTSTATUS code
350 NTSTATUS dcerpc_bind_auth_recv(struct composite_context *creq)
352 NTSTATUS result = composite_wait(creq);
353 struct bind_auth_state *state = talloc_get_type(creq->private_data,
354 struct bind_auth_state);
356 if (NT_STATUS_IS_OK(result)) {
358 after a successful authenticated bind the session
359 key reverts to the generic session key
361 state->pipe->conn->security_state.session_key = dcerpc_generic_session_key;
370 Perform a GENSEC authenticated bind to a DCE/RPC pipe, sync
371 @param p The dcerpc_pipe to bind (must already be connected)
372 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
373 @param credentials The credentials of the account to connect with
374 @param auth_type Select the authentication scheme to use
375 @param auth_level Chooses between unprotected (connect), signed or sealed
376 @param service The service (used by Kerberos to select the service principal to contact)
377 @retval NTSTATUS status code
380 _PUBLIC_ NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p,
381 const struct ndr_interface_table *table,
382 struct cli_credentials *credentials,
383 struct loadparm_context *lp_ctx,
384 uint8_t auth_type, uint8_t auth_level,
387 struct composite_context *creq;
388 creq = dcerpc_bind_auth_send(p, p, table, credentials, lp_ctx,
389 auth_type, auth_level, service);
390 return dcerpc_bind_auth_recv(creq);