IRPC: Add include guards for the header.
[jelmer/samba4-debian.git] / source / 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                 /* Now that we are authenticated, go back to the generic session key... */
128                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
129                 return NT_STATUS_OK;
130         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
131                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
132                 dce_conn->auth_state.auth_info->auth_reserved = 0;
133                 return NT_STATUS_OK;
134         } else {
135                 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
136                 return status;
137         }
138 }
139
140
141 /*
142   process the final stage of a auth request
143 */
144 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
145 {
146         struct ncacn_packet *pkt = &call->pkt;
147         struct dcesrv_connection *dce_conn = call->conn;
148         NTSTATUS status;
149         enum ndr_err_code ndr_err;
150
151         /* We can't work without an existing gensec state, and an new blob to feed it */
152         if (!dce_conn->auth_state.auth_info ||
153             !dce_conn->auth_state.gensec_security ||
154             pkt->u.auth3.auth_info.length == 0) {
155                 return false;
156         }
157
158         ndr_err = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
159                                        call, NULL,
160                                        dce_conn->auth_state.auth_info,
161                                        (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
162         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
163                 return false;
164         }
165
166         /* Pass the extra data we got from the client down to gensec for processing */
167         status = gensec_update(dce_conn->auth_state.gensec_security,
168                                call,
169                                dce_conn->auth_state.auth_info->credentials, 
170                                &dce_conn->auth_state.auth_info->credentials);
171         if (NT_STATUS_IS_OK(status)) {
172                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
173                                              &dce_conn->auth_state.session_info);
174                 if (!NT_STATUS_IS_OK(status)) {
175                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
176                         return false;
177                 }
178                 /* Now that we are authenticated, go back to the generic session key... */
179                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
180                 return true;
181         } else {
182                 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
183                           nt_errstr(status)));
184                 return false;
185         }
186
187         return true;
188 }
189
190 /*
191   parse any auth information from a dcerpc alter request
192   return false if we can't handle the auth request for some 
193   reason (in which case we send a bind_nak (is this true for here?))
194 */
195 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
196 {
197         struct ncacn_packet *pkt = &call->pkt;
198         struct dcesrv_connection *dce_conn = call->conn;
199         enum ndr_err_code ndr_err;
200
201         /* on a pure interface change there is no auth blob */
202         if (pkt->u.alter.auth_info.length == 0) {
203                 return true;
204         }
205
206         /* We can't work without an existing gensec state */
207         if (!dce_conn->auth_state.gensec_security) {
208                 return false;
209         }
210
211         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
212         if (!dce_conn->auth_state.auth_info) {
213                 return false;
214         }
215
216         ndr_err = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
217                                        call, NULL,
218                                        dce_conn->auth_state.auth_info,
219                                        (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
220         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
221                 return false;
222         }
223
224         return true;
225 }
226
227 /*
228   add any auth information needed in a alter ack, and process the authentication
229   information found in the alter.
230 */
231 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
232 {
233         struct dcesrv_connection *dce_conn = call->conn;
234         NTSTATUS status;
235
236         /* on a pure interface change there is no auth_info structure
237            setup */
238         if (!call->conn->auth_state.auth_info ||
239             dce_conn->auth_state.auth_info->credentials.length == 0) {
240                 return NT_STATUS_OK;
241         }
242
243         if (!call->conn->auth_state.gensec_security) {
244                 return NT_STATUS_INVALID_PARAMETER;
245         }
246
247         status = gensec_update(dce_conn->auth_state.gensec_security,
248                                call,
249                                dce_conn->auth_state.auth_info->credentials, 
250                                &dce_conn->auth_state.auth_info->credentials);
251
252         if (NT_STATUS_IS_OK(status)) {
253                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
254                                              &dce_conn->auth_state.session_info);
255                 if (!NT_STATUS_IS_OK(status)) {
256                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
257                         return status;
258                 }
259
260                 /* Now that we are authenticated, got back to the generic session key... */
261                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
262                 return NT_STATUS_OK;
263         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
264                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
265                 dce_conn->auth_state.auth_info->auth_reserved = 0;
266                 return NT_STATUS_OK;
267         }
268
269         DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
270         return status;
271 }
272
273 /*
274   generate a CONNECT level verifier
275 */
276 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
277 {
278         *blob = data_blob_talloc(mem_ctx, NULL, 16);
279         if (blob->data == NULL) {
280                 return NT_STATUS_NO_MEMORY;
281         }
282         SIVAL(blob->data, 0, 1);
283         memset(blob->data+4, 0, 12);
284         return NT_STATUS_OK;
285 }
286
287 /*
288   generate a CONNECT level verifier
289 */
290 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
291 {
292         if (blob->length != 16 ||
293             IVAL(blob->data, 0) != 1) {
294                 return NT_STATUS_ACCESS_DENIED;
295         }
296         return NT_STATUS_OK;
297 }
298
299
300 /*
301   check credentials on a request
302 */
303 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
304 {
305         struct ncacn_packet *pkt = &call->pkt;
306         struct dcesrv_connection *dce_conn = call->conn;
307         DATA_BLOB auth_blob;
308         struct dcerpc_auth auth;
309         struct ndr_pull *ndr;
310         NTSTATUS status;
311         enum ndr_err_code ndr_err;
312
313         if (!dce_conn->auth_state.auth_info ||
314             !dce_conn->auth_state.gensec_security) {
315                 return true;
316         }
317
318         auth_blob.length = 8 + pkt->auth_length;
319
320         /* check for a valid length */
321         if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
322                 return false;
323         }
324
325         auth_blob.data = 
326                 pkt->u.request.stub_and_verifier.data + 
327                 pkt->u.request.stub_and_verifier.length - auth_blob.length;
328         pkt->u.request.stub_and_verifier.length -= auth_blob.length;
329
330         /* pull the auth structure */
331         ndr = ndr_pull_init_blob(&auth_blob, call, lp_iconv_convenience(call->conn->dce_ctx->lp_ctx));
332         if (!ndr) {
333                 return false;
334         }
335
336         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
337                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
338         }
339
340         ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
341         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
342                 talloc_free(ndr);
343                 return false;
344         }
345
346         /* check signature or unseal the packet */
347         switch (dce_conn->auth_state.auth_info->auth_level) {
348         case DCERPC_AUTH_LEVEL_PRIVACY:
349                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
350                                               call,
351                                               full_packet->data + DCERPC_REQUEST_LENGTH,
352                                               pkt->u.request.stub_and_verifier.length, 
353                                               full_packet->data,
354                                               full_packet->length-auth.credentials.length,
355                                               &auth.credentials);
356                 memcpy(pkt->u.request.stub_and_verifier.data, 
357                        full_packet->data + DCERPC_REQUEST_LENGTH,
358                        pkt->u.request.stub_and_verifier.length);
359                 break;
360
361         case DCERPC_AUTH_LEVEL_INTEGRITY:
362                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
363                                              call,
364                                              pkt->u.request.stub_and_verifier.data, 
365                                              pkt->u.request.stub_and_verifier.length,
366                                              full_packet->data,
367                                              full_packet->length-auth.credentials.length,
368                                              &auth.credentials);
369                 break;
370
371         case DCERPC_AUTH_LEVEL_CONNECT:
372                 status = dcesrv_check_connect_verifier(&auth.credentials);
373                 break;
374
375         default:
376                 status = NT_STATUS_INVALID_LEVEL;
377                 break;
378         }
379
380         /* remove the indicated amount of padding */
381         if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
382                 talloc_free(ndr);
383                 return false;
384         }
385         pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
386         talloc_free(ndr);
387
388         return NT_STATUS_IS_OK(status);
389 }
390
391
392 /* 
393    push a signed or sealed dcerpc request packet into a blob
394 */
395 bool dcesrv_auth_response(struct dcesrv_call_state *call,
396                           DATA_BLOB *blob, struct ncacn_packet *pkt)
397 {
398         struct dcesrv_connection *dce_conn = call->conn;
399         NTSTATUS status;
400         enum ndr_err_code ndr_err;
401         struct ndr_push *ndr;
402         uint32_t payload_length;
403         DATA_BLOB creds2;
404
405         /* non-signed packets are simple */
406         if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
407                 status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
408                 return NT_STATUS_IS_OK(status);
409         }
410
411         ndr = ndr_push_init_ctx(call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx));
412         if (!ndr) {
413                 return false;
414         }
415
416         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
417                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
418         }
419
420         ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
421         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
422                 return false;
423         }
424
425         /* pad to 16 byte multiple, match win2k3 */
426         dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 16);
427         ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
428
429         payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
430
431         if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
432                 status = dcesrv_connect_verifier(call,
433                                                  &dce_conn->auth_state.auth_info->credentials);
434                 if (!NT_STATUS_IS_OK(status)) {
435                         return false;
436                 }
437         } else {
438
439                 /* We hope this length is accruate.  If must be if the
440                  * GENSEC mech does AEAD signing of the packet
441                  * headers */
442                 dce_conn->auth_state.auth_info->credentials
443                         = data_blob_talloc(call, NULL, 
444                                            gensec_sig_size(dce_conn->auth_state.gensec_security, 
445                                                            payload_length));
446                 data_blob_clear(&dce_conn->auth_state.auth_info->credentials);
447         }
448
449         /* add the auth verifier */
450         ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
451                                       dce_conn->auth_state.auth_info);
452         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
453                 return false;
454         }
455
456         /* extract the whole packet as a blob */
457         *blob = ndr_push_blob(ndr);
458
459         /* fill in the fragment length and auth_length, we can't fill
460            in these earlier as we don't know the signature length (it
461            could be variable length) */
462         dcerpc_set_frag_length(blob, blob->length);
463
464         /* We hope this value is accruate.  If must be if the GENSEC
465          * mech does AEAD signing of the packet headers */
466         dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
467
468         /* sign or seal the packet */
469         switch (dce_conn->auth_state.auth_info->auth_level) {
470         case DCERPC_AUTH_LEVEL_PRIVACY:
471                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
472                                             call,
473                                             ndr->data + DCERPC_REQUEST_LENGTH, 
474                                             payload_length,
475                                             blob->data,
476                                             blob->length - dce_conn->auth_state.auth_info->credentials.length,
477                                             &creds2);
478
479                 if (NT_STATUS_IS_OK(status)) {
480                         blob->length -= dce_conn->auth_state.auth_info->credentials.length;
481                         if (!data_blob_append(call, blob, creds2.data, creds2.length))
482                                 status = NT_STATUS_NO_MEMORY;
483                         else
484                                 status = NT_STATUS_OK;
485                 }
486
487                 /* If we did AEAD signing of the packet headers, then we hope
488                  * this value didn't change... */
489                 dcerpc_set_auth_length(blob, creds2.length);
490                 dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
491                 data_blob_free(&creds2);
492                 break;
493
494         case DCERPC_AUTH_LEVEL_INTEGRITY:
495                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
496                                             call,
497                                             ndr->data + DCERPC_REQUEST_LENGTH, 
498                                             payload_length,
499                                             blob->data,
500                                             blob->length - dce_conn->auth_state.auth_info->credentials.length,
501                                             &creds2);
502                 if (NT_STATUS_IS_OK(status)) {
503                         blob->length -= dce_conn->auth_state.auth_info->credentials.length;
504                         if (!data_blob_append(call, blob, creds2.data, creds2.length))
505                                 status = NT_STATUS_NO_MEMORY;
506                         else
507                                 status = NT_STATUS_OK;
508                 }
509
510                 /* If we did AEAD signing of the packet headers, then we hope
511                  * this value didn't change... */
512                 dcerpc_set_auth_length(blob, creds2.length);
513                 dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
514                 data_blob_free(&creds2);
515                 break;
516
517         case DCERPC_AUTH_LEVEL_CONNECT:
518                 break;
519
520         default:
521                 status = NT_STATUS_INVALID_LEVEL;
522                 break;
523         }
524
525         data_blob_free(&dce_conn->auth_state.auth_info->credentials);
526
527         if (!NT_STATUS_IS_OK(status)) {
528                 return false;
529         }       
530
531         return true;
532 }