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