s4-rpc: added NDR64 support
[Samba/aatanasov.git] / source4 / librpc / rpc / dcerpc_auth.c
blobbca7a8d186044ebf4b808902e80b183dd6e3f172
1 /*
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/>.
24 #include "includes.h"
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 uint32_t pipe_flags,
36 struct ndr_syntax_id *syntax,
37 struct ndr_syntax_id *transfer_syntax)
39 syntax->uuid = table->syntax_id.uuid;
40 syntax->if_version = table->syntax_id.if_version;
42 if (pipe_flags & DCERPC_NDR64) {
43 *transfer_syntax = ndr64_transfer_syntax;
44 } else {
45 *transfer_syntax = ndr_transfer_syntax;
48 return NT_STATUS_OK;
53 Send request to do a non-authenticated dcerpc bind
55 struct composite_context *dcerpc_bind_auth_none_send(TALLOC_CTX *mem_ctx,
56 struct dcerpc_pipe *p,
57 const struct ndr_interface_table *table)
59 struct ndr_syntax_id syntax;
60 struct ndr_syntax_id transfer_syntax;
62 struct composite_context *c;
64 c = composite_create(mem_ctx, p->conn->event_ctx);
65 if (c == NULL) return NULL;
67 c->status = dcerpc_init_syntaxes(table, p->conn->flags,
68 &syntax, &transfer_syntax);
69 if (!NT_STATUS_IS_OK(c->status)) {
70 DEBUG(2,("Invalid uuid string in "
71 "dcerpc_bind_auth_none_send\n"));
72 composite_error(c, c->status);
73 return c;
76 /* c was only allocated as a container for a possible error */
77 talloc_free(c);
79 return dcerpc_bind_send(p, mem_ctx, &syntax, &transfer_syntax);
84 Receive result of a non-authenticated dcerpc bind
86 NTSTATUS dcerpc_bind_auth_none_recv(struct composite_context *ctx)
88 return dcerpc_bind_recv(ctx);
93 Perform sync non-authenticated dcerpc bind
95 _PUBLIC_ NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
96 const struct ndr_interface_table *table)
98 struct composite_context *ctx;
100 ctx = dcerpc_bind_auth_none_send(p, p, table);
101 return dcerpc_bind_auth_none_recv(ctx);
105 struct bind_auth_state {
106 struct dcerpc_pipe *pipe;
107 DATA_BLOB credentials;
108 bool more_processing; /* Is there anything more to do after the
109 * first bind itself received? */
112 static void bind_auth_recv_alter(struct composite_context *creq);
114 static void bind_auth_next_step(struct composite_context *c)
116 struct bind_auth_state *state;
117 struct dcerpc_security *sec;
118 struct composite_context *creq;
119 bool more_processing = false;
121 state = talloc_get_type(c->private_data, struct bind_auth_state);
122 sec = &state->pipe->conn->security_state;
124 /* The status value here, from GENSEC is vital to the security
125 * of the system. Even if the other end accepts, if GENSEC
126 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
127 * feeding it blobs, or else the remote host/attacker might
128 * avoid mutal authentication requirements.
130 * Likewise, you must not feed GENSEC too much (after the OK),
131 * it doesn't like that either
134 c->status = gensec_update(sec->generic_state, state,
135 sec->auth_info->credentials,
136 &state->credentials);
137 data_blob_free(&sec->auth_info->credentials);
139 if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
140 more_processing = true;
141 c->status = NT_STATUS_OK;
144 if (!composite_is_ok(c)) return;
146 if (state->pipe->conn->flags & DCERPC_HEADER_SIGNING) {
147 gensec_want_feature(sec->generic_state, GENSEC_FEATURE_SIGN_PKT_HEADER);
150 if (state->credentials.length == 0) {
151 composite_done(c);
152 return;
155 sec->auth_info->credentials = state->credentials;
157 if (!more_processing) {
158 /* NO reply expected, so just send it */
159 c->status = dcerpc_auth3(state->pipe, state);
160 data_blob_free(&state->credentials);
161 sec->auth_info->credentials = data_blob(NULL, 0);
162 if (!composite_is_ok(c)) return;
164 composite_done(c);
165 return;
168 /* We are demanding a reply, so use a request that will get us one */
170 creq = dcerpc_alter_context_send(state->pipe, state,
171 &state->pipe->syntax,
172 &state->pipe->transfer_syntax);
173 data_blob_free(&state->credentials);
174 sec->auth_info->credentials = data_blob(NULL, 0);
175 if (composite_nomem(creq, c)) return;
177 composite_continue(c, creq, bind_auth_recv_alter, c);
181 static void bind_auth_recv_alter(struct composite_context *creq)
183 struct composite_context *c = talloc_get_type(creq->async.private_data,
184 struct composite_context);
186 c->status = dcerpc_alter_context_recv(creq);
187 if (!composite_is_ok(c)) return;
189 bind_auth_next_step(c);
193 static void bind_auth_recv_bindreply(struct composite_context *creq)
195 struct composite_context *c = talloc_get_type(creq->async.private_data,
196 struct composite_context);
197 struct bind_auth_state *state = talloc_get_type(c->private_data,
198 struct bind_auth_state);
200 c->status = dcerpc_bind_recv(creq);
201 if (!composite_is_ok(c)) return;
203 if (!state->more_processing) {
204 /* The first gensec_update has not requested a second run, so
205 * we're done here. */
206 composite_done(c);
207 return;
210 bind_auth_next_step(c);
215 Bind to a DCE/RPC pipe, send async request
216 @param mem_ctx TALLOC_CTX for the allocation of the composite_context
217 @param p The dcerpc_pipe to bind (must already be connected)
218 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
219 @param credentials The credentials of the account to connect with
220 @param auth_type Select the authentication scheme to use
221 @param auth_level Chooses between unprotected (connect), signed or sealed
222 @param service The service (used by Kerberos to select the service principal to contact)
223 @retval A composite context describing the partial state of the bind
226 struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
227 struct dcerpc_pipe *p,
228 const struct ndr_interface_table *table,
229 struct cli_credentials *credentials,
230 struct gensec_settings *gensec_settings,
231 uint8_t auth_type, uint8_t auth_level,
232 const char *service)
234 struct composite_context *c, *creq;
235 struct bind_auth_state *state;
236 struct dcerpc_security *sec;
238 struct ndr_syntax_id syntax, transfer_syntax;
240 /* composite context allocation and setup */
241 c = composite_create(mem_ctx, p->conn->event_ctx);
242 if (c == NULL) return NULL;
244 state = talloc(c, struct bind_auth_state);
245 if (composite_nomem(state, c)) return c;
246 c->private_data = state;
248 state->pipe = p;
250 c->status = dcerpc_init_syntaxes(table, p->conn->flags,
251 &syntax,
252 &transfer_syntax);
253 if (!composite_is_ok(c)) return c;
255 sec = &p->conn->security_state;
257 c->status = gensec_client_start(p, &sec->generic_state,
258 p->conn->event_ctx,
259 gensec_settings);
260 if (!NT_STATUS_IS_OK(c->status)) {
261 DEBUG(1, ("Failed to start GENSEC client mode: %s\n",
262 nt_errstr(c->status)));
263 composite_error(c, c->status);
264 return c;
267 c->status = gensec_set_credentials(sec->generic_state, credentials);
268 if (!NT_STATUS_IS_OK(c->status)) {
269 DEBUG(1, ("Failed to set GENSEC client credentials: %s\n",
270 nt_errstr(c->status)));
271 composite_error(c, c->status);
272 return c;
275 c->status = gensec_set_target_hostname(sec->generic_state,
276 p->conn->transport.target_hostname(p->conn));
277 if (!NT_STATUS_IS_OK(c->status)) {
278 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
279 nt_errstr(c->status)));
280 composite_error(c, c->status);
281 return c;
284 if (service != NULL) {
285 c->status = gensec_set_target_service(sec->generic_state,
286 service);
287 if (!NT_STATUS_IS_OK(c->status)) {
288 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
289 nt_errstr(c->status)));
290 composite_error(c, c->status);
291 return c;
295 c->status = gensec_start_mech_by_authtype(sec->generic_state,
296 auth_type, auth_level);
297 if (!NT_STATUS_IS_OK(c->status)) {
298 DEBUG(1, ("Failed to start GENSEC client mechanism %s: %s\n",
299 gensec_get_name_by_authtype(sec->generic_state, auth_type),
300 nt_errstr(c->status)));
301 composite_error(c, c->status);
302 return c;
305 sec->auth_info = talloc(p, struct dcerpc_auth);
306 if (composite_nomem(sec->auth_info, c)) return c;
308 sec->auth_info->auth_type = auth_type;
309 sec->auth_info->auth_level = auth_level,
310 sec->auth_info->auth_pad_length = 0;
311 sec->auth_info->auth_reserved = 0;
312 sec->auth_info->auth_context_id = random();
313 sec->auth_info->credentials = data_blob(NULL, 0);
315 /* The status value here, from GENSEC is vital to the security
316 * of the system. Even if the other end accepts, if GENSEC
317 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
318 * feeding it blobs, or else the remote host/attacker might
319 * avoid mutal authentication requirements.
321 * Likewise, you must not feed GENSEC too much (after the OK),
322 * it doesn't like that either
325 c->status = gensec_update(sec->generic_state, state,
326 sec->auth_info->credentials,
327 &state->credentials);
328 if (!NT_STATUS_IS_OK(c->status) &&
329 !NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
330 composite_error(c, c->status);
331 return c;
334 state->more_processing = NT_STATUS_EQUAL(c->status,
335 NT_STATUS_MORE_PROCESSING_REQUIRED);
337 if (state->credentials.length == 0) {
338 composite_done(c);
339 return c;
342 sec->auth_info->credentials = state->credentials;
344 /* The first request always is a dcerpc_bind. The subsequent ones
345 * depend on gensec results */
346 creq = dcerpc_bind_send(p, state, &syntax, &transfer_syntax);
347 data_blob_free(&state->credentials);
348 sec->auth_info->credentials = data_blob(NULL, 0);
349 if (composite_nomem(creq, c)) return c;
351 composite_continue(c, creq, bind_auth_recv_bindreply, c);
352 return c;
357 Bind to a DCE/RPC pipe, receive result
358 @param creq A composite context describing state of async call
359 @retval NTSTATUS code
362 NTSTATUS dcerpc_bind_auth_recv(struct composite_context *creq)
364 NTSTATUS result = composite_wait(creq);
365 struct bind_auth_state *state = talloc_get_type(creq->private_data,
366 struct bind_auth_state);
368 if (NT_STATUS_IS_OK(result)) {
370 after a successful authenticated bind the session
371 key reverts to the generic session key
373 state->pipe->conn->security_state.session_key = dcerpc_generic_session_key;
376 talloc_free(creq);
377 return result;
382 Perform a GENSEC authenticated bind to a DCE/RPC pipe, sync
383 @param p The dcerpc_pipe to bind (must already be connected)
384 @param table The interface table to use (the DCE/RPC bind both selects and interface and authenticates)
385 @param credentials The credentials of the account to connect with
386 @param auth_type Select the authentication scheme to use
387 @param auth_level Chooses between unprotected (connect), signed or sealed
388 @param service The service (used by Kerberos to select the service principal to contact)
389 @retval NTSTATUS status code
392 _PUBLIC_ NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p,
393 const struct ndr_interface_table *table,
394 struct cli_credentials *credentials,
395 struct gensec_settings *gensec_settings,
396 uint8_t auth_type, uint8_t auth_level,
397 const char *service)
399 struct composite_context *creq;
400 creq = dcerpc_bind_auth_send(p, p, table, credentials, gensec_settings,
401 auth_type, auth_level, service);
402 return dcerpc_bind_auth_recv(creq);