52d5631cfd1572937eb448a3b3f5b823e5e66518
[kai/samba.git] / source4 / rpc_server / dcesrv_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc authentication code
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/dcerpc_server_proto.h"
26 #include "librpc/rpc/dcerpc_proto.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "param/param.h"
31
32 /*
33   parse any auth information from a dcerpc bind request
34   return false if we can't handle the auth request for some 
35   reason (in which case we send a bind_nak)
36 */
37 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
38 {
39         struct cli_credentials *server_credentials;
40         struct ncacn_packet *pkt = &call->pkt;
41         struct dcesrv_connection *dce_conn = call->conn;
42         struct dcesrv_auth *auth = &dce_conn->auth_state;
43         NTSTATUS status;
44         enum ndr_err_code ndr_err;
45
46         if (pkt->u.bind.auth_info.length == 0) {
47                 dce_conn->auth_state.auth_info = NULL;
48                 return true;
49         }
50
51         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
52         if (!dce_conn->auth_state.auth_info) {
53                 return false;
54         }
55
56         ndr_err = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
57                                        call, NULL,
58                                        dce_conn->auth_state.auth_info,
59                                        (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
60         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
61                 return false;
62         }
63
64         status = gensec_server_start(dce_conn, call->event_ctx, call->conn->dce_ctx->lp_ctx, call->msg_ctx, &auth->gensec_security);
65         if (!NT_STATUS_IS_OK(status)) {
66                 DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
67                 return false;
68         }
69
70         server_credentials 
71                 = cli_credentials_init(call);
72         if (!server_credentials) {
73                 DEBUG(1, ("Failed to init server credentials\n"));
74                 return false;
75         }
76         
77         cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
78         status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
79         if (!NT_STATUS_IS_OK(status)) {
80                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
81                 talloc_free(server_credentials);
82                 server_credentials = NULL;
83         }
84
85         gensec_set_credentials(auth->gensec_security, server_credentials);
86
87         status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type, 
88                                                auth->auth_info->auth_level);
89
90         if (!NT_STATUS_IS_OK(status)) {
91                 DEBUG(1, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n", 
92                           (int)auth->auth_info->auth_type,
93                           (int)auth->auth_info->auth_level,
94                           nt_errstr(status)));
95                 return false;
96         }
97
98         return true;
99 }
100
101 /*
102   add any auth information needed in a bind ack, and process the authentication
103   information found in the bind.
104 */
105 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
106 {
107         struct dcesrv_connection *dce_conn = call->conn;
108         NTSTATUS status;
109
110         if (!call->conn->auth_state.gensec_security) {
111                 return NT_STATUS_OK;
112         }
113
114         status = gensec_update(dce_conn->auth_state.gensec_security,
115                                call,
116                                dce_conn->auth_state.auth_info->credentials, 
117                                &dce_conn->auth_state.auth_info->credentials);
118         
119         if (NT_STATUS_IS_OK(status)) {
120                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
121                                              &dce_conn->auth_state.session_info);
122                 if (!NT_STATUS_IS_OK(status)) {
123                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
124                         return status;
125                 }
126
127                 if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
128                         gensec_want_feature(dce_conn->auth_state.gensec_security,
129                                             GENSEC_FEATURE_SIGN_PKT_HEADER);
130                 }
131
132                 /* Now that we are authenticated, go back to the generic session key... */
133                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
134                 return NT_STATUS_OK;
135         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
136                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
137                 dce_conn->auth_state.auth_info->auth_reserved = 0;
138                 return NT_STATUS_OK;
139         } else {
140                 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
141                 return status;
142         }
143 }
144
145
146 /*
147   process the final stage of a auth request
148 */
149 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
150 {
151         struct ncacn_packet *pkt = &call->pkt;
152         struct dcesrv_connection *dce_conn = call->conn;
153         NTSTATUS status;
154         enum ndr_err_code ndr_err;
155
156         /* We can't work without an existing gensec state, and an new blob to feed it */
157         if (!dce_conn->auth_state.auth_info ||
158             !dce_conn->auth_state.gensec_security ||
159             pkt->u.auth3.auth_info.length == 0) {
160                 return false;
161         }
162
163         ndr_err = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
164                                        call, NULL,
165                                        dce_conn->auth_state.auth_info,
166                                        (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
167         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
168                 return false;
169         }
170
171         /* Pass the extra data we got from the client down to gensec for processing */
172         status = gensec_update(dce_conn->auth_state.gensec_security,
173                                call,
174                                dce_conn->auth_state.auth_info->credentials, 
175                                &dce_conn->auth_state.auth_info->credentials);
176         if (NT_STATUS_IS_OK(status)) {
177                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
178                                              &dce_conn->auth_state.session_info);
179                 if (!NT_STATUS_IS_OK(status)) {
180                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
181                         return false;
182                 }
183                 /* Now that we are authenticated, go back to the generic session key... */
184                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
185                 return true;
186         } else {
187                 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
188                           nt_errstr(status)));
189                 return false;
190         }
191
192         return true;
193 }
194
195 /*
196   parse any auth information from a dcerpc alter request
197   return false if we can't handle the auth request for some 
198   reason (in which case we send a bind_nak (is this true for here?))
199 */
200 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
201 {
202         struct ncacn_packet *pkt = &call->pkt;
203         struct dcesrv_connection *dce_conn = call->conn;
204         enum ndr_err_code ndr_err;
205
206         /* on a pure interface change there is no auth blob */
207         if (pkt->u.alter.auth_info.length == 0) {
208                 return true;
209         }
210
211         /* We can't work without an existing gensec state */
212         if (!dce_conn->auth_state.gensec_security) {
213                 return false;
214         }
215
216         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
217         if (!dce_conn->auth_state.auth_info) {
218                 return false;
219         }
220
221         ndr_err = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
222                                        call, NULL,
223                                        dce_conn->auth_state.auth_info,
224                                        (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
225         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
226                 return false;
227         }
228
229         return true;
230 }
231
232 /*
233   add any auth information needed in a alter ack, and process the authentication
234   information found in the alter.
235 */
236 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
237 {
238         struct dcesrv_connection *dce_conn = call->conn;
239         NTSTATUS status;
240
241         /* on a pure interface change there is no auth_info structure
242            setup */
243         if (!call->conn->auth_state.auth_info ||
244             dce_conn->auth_state.auth_info->credentials.length == 0) {
245                 return NT_STATUS_OK;
246         }
247
248         if (!call->conn->auth_state.gensec_security) {
249                 return NT_STATUS_INVALID_PARAMETER;
250         }
251
252         status = gensec_update(dce_conn->auth_state.gensec_security,
253                                call,
254                                dce_conn->auth_state.auth_info->credentials, 
255                                &dce_conn->auth_state.auth_info->credentials);
256
257         if (NT_STATUS_IS_OK(status)) {
258                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
259                                              &dce_conn->auth_state.session_info);
260                 if (!NT_STATUS_IS_OK(status)) {
261                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
262                         return status;
263                 }
264
265                 /* Now that we are authenticated, got back to the generic session key... */
266                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
267                 return NT_STATUS_OK;
268         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
269                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
270                 dce_conn->auth_state.auth_info->auth_reserved = 0;
271                 return NT_STATUS_OK;
272         }
273
274         DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
275         return status;
276 }
277
278 /*
279   check credentials on a request
280 */
281 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
282 {
283         struct ncacn_packet *pkt = &call->pkt;
284         struct dcesrv_connection *dce_conn = call->conn;
285         DATA_BLOB auth_blob;
286         struct dcerpc_auth auth;
287         struct ndr_pull *ndr;
288         NTSTATUS status;
289         enum ndr_err_code ndr_err;
290         size_t hdr_size = DCERPC_REQUEST_LENGTH;
291
292         if (!dce_conn->auth_state.auth_info ||
293             !dce_conn->auth_state.gensec_security) {
294                 return true;
295         }
296
297         switch (dce_conn->auth_state.auth_info->auth_level) {
298         case DCERPC_AUTH_LEVEL_PRIVACY:
299         case DCERPC_AUTH_LEVEL_INTEGRITY:
300                 break;
301
302         case DCERPC_AUTH_LEVEL_CONNECT:
303                 if (pkt->auth_length != 0) {
304                         break;
305                 }
306                 return true;
307         case DCERPC_AUTH_LEVEL_NONE:
308                 if (pkt->auth_length != 0) {
309                         return false;
310                 }
311                 return true;
312
313         default:
314                 return false;
315         }
316
317         auth_blob.length = 8 + pkt->auth_length;
318
319         /* check for a valid length */
320         if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
321                 return false;
322         }
323
324         auth_blob.data = 
325                 pkt->u.request.stub_and_verifier.data + 
326                 pkt->u.request.stub_and_verifier.length - auth_blob.length;
327         pkt->u.request.stub_and_verifier.length -= auth_blob.length;
328
329         /* pull the auth structure */
330         ndr = ndr_pull_init_blob(&auth_blob, call, lp_iconv_convenience(call->conn->dce_ctx->lp_ctx));
331         if (!ndr) {
332                 return false;
333         }
334
335         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
336                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
337         }
338
339         if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
340                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
341                 hdr_size += 16;
342         }
343
344         ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
345         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
346                 talloc_free(ndr);
347                 return false;
348         }
349
350         /* check signature or unseal the packet */
351         switch (dce_conn->auth_state.auth_info->auth_level) {
352         case DCERPC_AUTH_LEVEL_PRIVACY:
353                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
354                                               call,
355                                               full_packet->data + hdr_size,
356                                               pkt->u.request.stub_and_verifier.length, 
357                                               full_packet->data,
358                                               full_packet->length-auth.credentials.length,
359                                               &auth.credentials);
360                 memcpy(pkt->u.request.stub_and_verifier.data, 
361                        full_packet->data + hdr_size,
362                        pkt->u.request.stub_and_verifier.length);
363                 break;
364
365         case DCERPC_AUTH_LEVEL_INTEGRITY:
366                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
367                                              call,
368                                              pkt->u.request.stub_and_verifier.data, 
369                                              pkt->u.request.stub_and_verifier.length,
370                                              full_packet->data,
371                                              full_packet->length-auth.credentials.length,
372                                              &auth.credentials);
373                 break;
374
375         case DCERPC_AUTH_LEVEL_CONNECT:
376                 /* for now we ignore possible signatures here */
377                 status = NT_STATUS_OK;
378                 break;
379
380         default:
381                 status = NT_STATUS_INVALID_LEVEL;
382                 break;
383         }
384
385         /* remove the indicated amount of padding */
386         if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
387                 talloc_free(ndr);
388                 return false;
389         }
390         pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
391         talloc_free(ndr);
392
393         return NT_STATUS_IS_OK(status);
394 }
395
396
397 /* 
398    push a signed or sealed dcerpc request packet into a blob
399 */
400 bool dcesrv_auth_response(struct dcesrv_call_state *call,
401                           DATA_BLOB *blob, size_t sig_size,
402                           struct ncacn_packet *pkt)
403 {
404         struct dcesrv_connection *dce_conn = call->conn;
405         NTSTATUS status;
406         enum ndr_err_code ndr_err;
407         struct ndr_push *ndr;
408         uint32_t payload_length;
409         DATA_BLOB creds2;
410
411         /* non-signed packets are simple */
412         if (sig_size == 0) {
413                 status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
414                 return NT_STATUS_IS_OK(status);
415         }
416
417         switch (dce_conn->auth_state.auth_info->auth_level) {
418         case DCERPC_AUTH_LEVEL_PRIVACY:
419         case DCERPC_AUTH_LEVEL_INTEGRITY:
420                 break;
421
422         case DCERPC_AUTH_LEVEL_CONNECT:
423                 /*
424                  * TODO: let the gensec mech decide if it wants to generate a signature
425                  *       that might be needed for schannel...
426                  */
427                 status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
428                 return NT_STATUS_IS_OK(status);
429
430         case DCERPC_AUTH_LEVEL_NONE:
431                 status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
432                 return NT_STATUS_IS_OK(status);
433
434         default:
435                 return false;
436         }
437
438         ndr = ndr_push_init_ctx(call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx));
439         if (!ndr) {
440                 return false;
441         }
442
443         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
444                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
445         }
446
447         ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
448         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
449                 return false;
450         }
451
452         /* pad to 16 byte multiple, match win2k3 */
453         dce_conn->auth_state.auth_info->auth_pad_length =
454                 (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
455         ndr_err = ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
456         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
457                 return false;
458         }
459
460         payload_length = pkt->u.response.stub_and_verifier.length +
461                 dce_conn->auth_state.auth_info->auth_pad_length;
462
463         /* we start without signature, it will appended later */
464         dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
465
466         /* add the auth verifier */
467         ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
468                                       dce_conn->auth_state.auth_info);
469         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
470                 return false;
471         }
472
473         /* extract the whole packet as a blob */
474         *blob = ndr_push_blob(ndr);
475
476         /*
477          * Setup the frag and auth length in the packet buffer.
478          * This is needed if the GENSEC mech does AEAD signing
479          * of the packet headers. The signature itself will be
480          * appended later.
481          */
482         dcerpc_set_frag_length(blob, blob->length + sig_size);
483         dcerpc_set_auth_length(blob, sig_size);
484
485         /* sign or seal the packet */
486         switch (dce_conn->auth_state.auth_info->auth_level) {
487         case DCERPC_AUTH_LEVEL_PRIVACY:
488                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
489                                             call,
490                                             ndr->data + DCERPC_REQUEST_LENGTH, 
491                                             payload_length,
492                                             blob->data,
493                                             blob->length,
494                                             &creds2);
495                 break;
496
497         case DCERPC_AUTH_LEVEL_INTEGRITY:
498                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
499                                             call,
500                                             ndr->data + DCERPC_REQUEST_LENGTH, 
501                                             payload_length,
502                                             blob->data,
503                                             blob->length,
504                                             &creds2);
505                 break;
506
507         default:
508                 status = NT_STATUS_INVALID_LEVEL;
509                 break;
510         }
511
512         if (NT_STATUS_IS_OK(status)) {
513                 if (creds2.length != sig_size) {
514                         DEBUG(0,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
515                                 creds2.length, (uint32_t)sig_size,
516                                 dce_conn->auth_state.auth_info->auth_pad_length,
517                                 pkt->u.response.stub_and_verifier.length));
518                         data_blob_free(&creds2);
519                         status = NT_STATUS_INTERNAL_ERROR;
520                 }
521         }
522
523         if (NT_STATUS_IS_OK(status)) {
524                 if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
525                         status = NT_STATUS_NO_MEMORY;
526                 }
527                 data_blob_free(&creds2);
528         }
529
530         if (!NT_STATUS_IS_OK(status)) {
531                 return false;
532         }       
533
534         return true;
535 }