librpc/rpc: add support DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN
[metze/samba/wb-ndr.git] / source / librpc / rpc / dcerpc_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Generic Authentication Interface
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8    Copyright (C) Stefan Metzmacher 2004
9    
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.
14    
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.
19    
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/>.
22 */
23
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"
30
31 /*
32   return the rpc syntax and transfer syntax given the pipe uuid and version
33 */
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)
37 {
38         syntax->uuid = table->syntax_id.uuid;
39         syntax->if_version = table->syntax_id.if_version;
40
41         *transfer_syntax = ndr_transfer_syntax;
42
43         return NT_STATUS_OK;
44 }
45
46
47 /*
48   Send request to do a non-authenticated dcerpc bind
49 */
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)
53 {
54         struct ndr_syntax_id syntax;
55         struct ndr_syntax_id transfer_syntax;
56
57         struct composite_context *c;
58
59         c = composite_create(mem_ctx, p->conn->event_ctx);
60         if (c == NULL) return NULL;
61
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);
68                 return c;
69         }
70
71         /* c was only allocated as a container for a possible error */
72         talloc_free(c);
73
74         return dcerpc_bind_send(p, mem_ctx, &syntax, &transfer_syntax);
75 }
76
77
78 /*
79   Receive result of a non-authenticated dcerpc bind
80 */
81 NTSTATUS dcerpc_bind_auth_none_recv(struct composite_context *ctx)
82 {
83         return dcerpc_bind_recv(ctx);
84 }
85
86
87 /*
88   Perform sync non-authenticated dcerpc bind
89 */
90 _PUBLIC_ NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
91                                const struct ndr_interface_table *table)
92 {
93         struct composite_context *ctx;
94
95         ctx = dcerpc_bind_auth_none_send(p, p, table);
96         return dcerpc_bind_auth_none_recv(ctx);
97 }
98
99
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? */
105 };
106
107 static void bind_auth_recv_alter(struct composite_context *creq);
108
109 static void bind_auth_next_step(struct composite_context *c)
110 {
111         struct bind_auth_state *state;
112         struct dcerpc_security *sec;
113         struct composite_context *creq;
114         bool more_processing = false;
115
116         state = talloc_get_type(c->private_data, struct bind_auth_state);
117         sec = &state->pipe->conn->security_state;
118
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.
124          *
125          * Likewise, you must not feed GENSEC too much (after the OK),
126          * it doesn't like that either
127          */
128
129         c->status = gensec_update(sec->generic_state, state,
130                                   sec->auth_info->credentials,
131                                   &state->credentials);
132
133         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
134                 more_processing = true;
135                 c->status = NT_STATUS_OK;
136         }
137
138         if (!composite_is_ok(c)) return;
139
140         if (state->pipe->conn->flags & DCERPC_HEADER_SIGNING) {
141                 gensec_want_feature(sec->generic_state, GENSEC_FEATURE_SIGN_PKT_HEADER);
142         }
143
144         if (state->credentials.length == 0) {
145                 composite_done(c);
146                 return;
147         }
148
149         sec->auth_info->credentials = state->credentials;
150
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;
155
156                 composite_done(c);
157                 return;
158         }
159
160         /* We are demanding a reply, so use a request that will get us one */
161
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;
166
167         composite_continue(c, creq, bind_auth_recv_alter, c);
168 }
169
170
171 static void bind_auth_recv_alter(struct composite_context *creq)
172 {
173         struct composite_context *c = talloc_get_type(creq->async.private_data,
174                                                       struct composite_context);
175
176         c->status = dcerpc_alter_context_recv(creq);
177         if (!composite_is_ok(c)) return;
178
179         bind_auth_next_step(c);
180 }
181
182
183 static void bind_auth_recv_bindreply(struct composite_context *creq)
184 {
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);
189
190         c->status = dcerpc_bind_recv(creq);
191         if (!composite_is_ok(c)) return;
192
193         if (!state->more_processing) {
194                 /* The first gensec_update has not requested a second run, so
195                  * we're done here. */
196                 composite_done(c);
197                 return;
198         }
199
200         bind_auth_next_step(c);
201 }
202
203
204 /**
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
214 */
215
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,
222                                                 const char *service)
223 {
224         struct composite_context *c, *creq;
225         struct bind_auth_state *state;
226         struct dcerpc_security *sec;
227
228         struct ndr_syntax_id syntax, transfer_syntax;
229
230         /* composite context allocation and setup */
231         c = composite_create(mem_ctx, p->conn->event_ctx);
232         if (c == NULL) return NULL;
233
234         state = talloc(c, struct bind_auth_state);
235         if (composite_nomem(state, c)) return c;
236         c->private_data = state;
237
238         state->pipe = p;
239
240         c->status = dcerpc_init_syntaxes(table,
241                                          &syntax,
242                                          &transfer_syntax);
243         if (!composite_is_ok(c)) return c;
244
245         sec = &p->conn->security_state;
246
247         c->status = gensec_client_start(p, &sec->generic_state,
248                                         p->conn->event_ctx,
249                                         lp_ctx);
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);
254                 return c;
255         }
256
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);
262                 return c;
263         }
264
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);
271                 return c;
272         }
273
274         if (service != NULL) {
275                 c->status = gensec_set_target_service(sec->generic_state,
276                                                       service);
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);
281                         return c;
282                 }
283         }
284
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);
292                 return c;
293         }
294
295         sec->auth_info = talloc(p, struct dcerpc_auth);
296         if (composite_nomem(sec->auth_info, c)) return c;
297
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);
304
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.
310          *
311          * Likewise, you must not feed GENSEC too much (after the OK),
312          * it doesn't like that either
313          */
314
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);
321                 return c;
322         }
323
324         state->more_processing = NT_STATUS_EQUAL(c->status,
325                                                  NT_STATUS_MORE_PROCESSING_REQUIRED);
326
327         if (state->credentials.length == 0) {
328                 composite_done(c);
329                 return c;
330         }
331
332         sec->auth_info->credentials = state->credentials;
333
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;
338
339         composite_continue(c, creq, bind_auth_recv_bindreply, c);
340         return c;
341 }
342
343
344 /**
345    Bind to a DCE/RPC pipe, receive result
346    @param creq A composite context describing state of async call
347    @retval NTSTATUS code
348 */
349
350 NTSTATUS dcerpc_bind_auth_recv(struct composite_context *creq)
351 {
352         NTSTATUS result = composite_wait(creq);
353         struct bind_auth_state *state = talloc_get_type(creq->private_data,
354                                                         struct bind_auth_state);
355
356         if (NT_STATUS_IS_OK(result)) {
357                 /*
358                   after a successful authenticated bind the session
359                   key reverts to the generic session key
360                 */
361                 state->pipe->conn->security_state.session_key = dcerpc_generic_session_key;
362         }
363         
364         talloc_free(creq);
365         return result;
366 }
367
368
369 /**
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
378 */
379
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,
385                           const char *service)
386 {
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);
391 }