s4:rpc_server: set the full DCERPC_BIND_NAK_REASON_* in dcesrv_bind()
[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 "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 = NULL;
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
48         if (pkt->auth_length == 0) {
49                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
50                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
51                 auth->auth_context_id = 0;
52                 return true;
53         }
54
55         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
56                                           &call->in_auth_info,
57                                           NULL, true);
58         if (!NT_STATUS_IS_OK(status)) {
59                 /*
60                  * Setting DCERPC_AUTH_LEVEL_NONE,
61                  * gives the caller the reject_reason
62                  * as auth_context_id.
63                  *
64                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
65                  */
66                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
67                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
68                 auth->auth_context_id =
69                         DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
70                 return false;
71         }
72
73         switch (call->in_auth_info.auth_level) {
74         case DCERPC_AUTH_LEVEL_CONNECT:
75         case DCERPC_AUTH_LEVEL_CALL:
76         case DCERPC_AUTH_LEVEL_PACKET:
77         case DCERPC_AUTH_LEVEL_INTEGRITY:
78         case DCERPC_AUTH_LEVEL_PRIVACY:
79                 /*
80                  * We evaluate auth_type only if auth_level was valid
81                  */
82                 break;
83         default:
84                 /*
85                  * Setting DCERPC_AUTH_LEVEL_NONE,
86                  * gives the caller the reject_reason
87                  * as auth_context_id.
88                  *
89                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
90                  */
91                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
92                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
93                 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
94                 return false;
95         }
96
97         auth->auth_type = call->in_auth_info.auth_type;
98         auth->auth_level = call->in_auth_info.auth_level;
99         auth->auth_context_id = call->in_auth_info.auth_context_id;
100
101         server_credentials 
102                 = cli_credentials_init(call);
103         if (!server_credentials) {
104                 DEBUG(1, ("Failed to init server credentials\n"));
105                 return false;
106         }
107         
108         cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
109         status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
110         if (!NT_STATUS_IS_OK(status)) {
111                 DEBUG(1, ("Failed to obtain server credentials: %s\n",
112                           nt_errstr(status)));
113                 return false;
114         }
115
116         status = samba_server_gensec_start(dce_conn, call->event_ctx, 
117                                            call->msg_ctx,
118                                            call->conn->dce_ctx->lp_ctx,
119                                            server_credentials,
120                                            NULL,
121                                            &auth->gensec_security);
122         if (!NT_STATUS_IS_OK(status)) {
123                 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
124                           nt_errstr(status)));
125                 return false;
126         }
127
128         if (call->conn->remote_address != NULL) {
129                 status = gensec_set_remote_address(auth->gensec_security,
130                                                 call->conn->remote_address);
131                 if (!NT_STATUS_IS_OK(status)) {
132                         DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
133                                   nt_errstr(status)));
134                         return false;
135                 }
136         }
137
138         status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
139                                                auth->auth_level);
140         if (!NT_STATUS_IS_OK(status)) {
141                 const char *backend_name =
142                         gensec_get_name_by_authtype(auth->gensec_security,
143                                                     auth->auth_type);
144
145                 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
146                           "auth_type=%d (%s), auth_level=%d: %s\n",
147                           (int)auth->auth_type, backend_name,
148                           (int)auth->auth_level,
149                           nt_errstr(status)));
150
151                 /*
152                  * Setting DCERPC_AUTH_LEVEL_NONE,
153                  * gives the caller the reject_reason
154                  * as auth_context_id.
155                  *
156                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
157                  */
158                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
159                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
160                 if (backend_name != NULL) {
161                         auth->auth_context_id =
162                                 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
163                 } else {
164                         auth->auth_context_id =
165                                 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
166                 }
167                 return false;
168         }
169
170         return true;
171 }
172
173 /*
174   add any auth information needed in a bind ack, and process the authentication
175   information found in the bind.
176 */
177 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
178 {
179         struct dcesrv_connection *dce_conn = call->conn;
180         NTSTATUS status;
181         bool want_header_signing = false;
182
183         dce_conn->allow_alter = true;
184         dce_conn->allow_auth3 = true;
185
186         if (call->pkt.auth_length == 0) {
187                 dce_conn->auth_state.auth_finished = true;
188                 dce_conn->allow_request = true;
189                 return NT_STATUS_OK;
190         }
191
192         /* We can't work without an existing gensec state */
193         if (!call->conn->auth_state.gensec_security) {
194                 return NT_STATUS_INTERNAL_ERROR;
195         }
196
197         if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
198                 dce_conn->auth_state.client_hdr_signing = true;
199                 want_header_signing = true;
200         }
201
202         if (!lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","header signing", true)) {
203                 want_header_signing = false;
204         }
205
206         call->_out_auth_info = (struct dcerpc_auth) {
207                 .auth_type = dce_conn->auth_state.auth_type,
208                 .auth_level = dce_conn->auth_state.auth_level,
209                 .auth_context_id = dce_conn->auth_state.auth_context_id,
210         };
211         call->out_auth_info = &call->_out_auth_info;
212
213         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
214                                call, call->event_ctx,
215                                call->in_auth_info.credentials,
216                                &call->out_auth_info->credentials);
217         
218         if (NT_STATUS_IS_OK(status)) {
219                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
220                                              dce_conn,
221                                              &dce_conn->auth_state.session_info);
222                 if (!NT_STATUS_IS_OK(status)) {
223                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
224                         return status;
225                 }
226                 dce_conn->auth_state.auth_finished = true;
227                 dce_conn->allow_request = true;
228
229                 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
230                                          GENSEC_FEATURE_SIGN_PKT_HEADER))
231                 {
232                         want_header_signing = false;
233                 }
234
235                 if (want_header_signing) {
236                         gensec_want_feature(dce_conn->auth_state.gensec_security,
237                                             GENSEC_FEATURE_SIGN_PKT_HEADER);
238                         dce_conn->auth_state.hdr_signing = true;
239                         pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
240                 }
241
242                 /* Now that we are authenticated, go back to the generic session key... */
243                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
244                 return NT_STATUS_OK;
245         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
246                 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
247                                          GENSEC_FEATURE_SIGN_PKT_HEADER))
248                 {
249                         want_header_signing = false;
250                 }
251
252                 if (want_header_signing) {
253                         gensec_want_feature(dce_conn->auth_state.gensec_security,
254                                             GENSEC_FEATURE_SIGN_PKT_HEADER);
255                         dce_conn->auth_state.hdr_signing = true;
256                         pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
257                 }
258
259                 return NT_STATUS_OK;
260         } else {
261                 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
262                           nt_errstr(status)));
263                 return status;
264         }
265 }
266
267
268 /*
269   process the final stage of a auth request
270 */
271 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
272 {
273         struct ncacn_packet *pkt = &call->pkt;
274         struct dcesrv_connection *dce_conn = call->conn;
275         NTSTATUS status;
276
277         if (pkt->auth_length == 0) {
278                 return false;
279         }
280
281         if (dce_conn->auth_state.auth_finished) {
282                 return false;
283         }
284
285         /* We can't work without an existing gensec state */
286         if (!dce_conn->auth_state.gensec_security) {
287                 return false;
288         }
289
290         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
291                                           &call->in_auth_info, NULL, true);
292         if (!NT_STATUS_IS_OK(status)) {
293                 /*
294                  * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
295                  * instead of DCERPC_NCA_S_PROTO_ERROR.
296                  */
297                 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
298                 return false;
299         }
300
301         if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
302                 return false;
303         }
304
305         if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
306                 return false;
307         }
308
309         if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
310                 return false;
311         }
312
313         call->_out_auth_info = (struct dcerpc_auth) {
314                 .auth_type = dce_conn->auth_state.auth_type,
315                 .auth_level = dce_conn->auth_state.auth_level,
316                 .auth_context_id = dce_conn->auth_state.auth_context_id,
317         };
318         call->out_auth_info = &call->_out_auth_info;
319
320         /* Pass the extra data we got from the client down to gensec for processing */
321         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
322                                call, call->event_ctx,
323                                call->in_auth_info.credentials,
324                                &call->out_auth_info->credentials);
325         if (NT_STATUS_IS_OK(status)) {
326                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
327                                              dce_conn,
328                                              &dce_conn->auth_state.session_info);
329                 if (!NT_STATUS_IS_OK(status)) {
330                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
331                         return false;
332                 }
333                 dce_conn->auth_state.auth_finished = true;
334                 dce_conn->allow_request = true;
335
336                 /* Now that we are authenticated, go back to the generic session key... */
337                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
338
339                 if (call->out_auth_info->credentials.length != 0) {
340
341                         DEBUG(4, ("GENSEC produced output token (len=%u) at bind_auth3\n",
342                                   (unsigned)call->out_auth_info->credentials.length));
343                         return false;
344                 }
345                 return true;
346         } else {
347                 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
348                           nt_errstr(status)));
349                 return false;
350         }
351 }
352
353 /*
354   parse any auth information from a dcerpc alter request
355   return false if we can't handle the auth request for some 
356   reason (in which case we send a bind_nak (is this true for here?))
357 */
358 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
359 {
360         struct ncacn_packet *pkt = &call->pkt;
361         struct dcesrv_connection *dce_conn = call->conn;
362         NTSTATUS status;
363
364         /* on a pure interface change there is no auth blob */
365         if (pkt->auth_length == 0) {
366                 if (!dce_conn->auth_state.auth_finished) {
367                         return false;
368                 }
369                 return true;
370         }
371
372         if (dce_conn->auth_state.auth_finished) {
373                 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
374                 return false;
375         }
376
377         /* We can't work without an existing gensec state */
378         if (!dce_conn->auth_state.gensec_security) {
379                 return false;
380         }
381
382         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
383                                           &call->in_auth_info, NULL, true);
384         if (!NT_STATUS_IS_OK(status)) {
385                 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
386                 return false;
387         }
388
389         if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
390                 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
391                 return false;
392         }
393
394         if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
395                 return false;
396         }
397
398         if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
399                 return false;
400         }
401
402         if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
403                 return false;
404         }
405
406         return true;
407 }
408
409 /*
410   add any auth information needed in a alter ack, and process the authentication
411   information found in the alter.
412 */
413 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
414 {
415         struct dcesrv_connection *dce_conn = call->conn;
416         NTSTATUS status;
417
418         /* on a pure interface change there is no auth_info structure
419            setup */
420         if (call->pkt.auth_length == 0) {
421                 return NT_STATUS_OK;
422         }
423
424         if (!call->conn->auth_state.gensec_security) {
425                 return NT_STATUS_INTERNAL_ERROR;
426         }
427
428         call->_out_auth_info = (struct dcerpc_auth) {
429                 .auth_type = dce_conn->auth_state.auth_type,
430                 .auth_level = dce_conn->auth_state.auth_level,
431                 .auth_context_id = dce_conn->auth_state.auth_context_id,
432         };
433         call->out_auth_info = &call->_out_auth_info;
434
435         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
436                                call, call->event_ctx,
437                                call->in_auth_info.credentials,
438                                &call->out_auth_info->credentials);
439
440         if (NT_STATUS_IS_OK(status)) {
441                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
442                                              dce_conn,
443                                              &dce_conn->auth_state.session_info);
444                 if (!NT_STATUS_IS_OK(status)) {
445                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
446                         return status;
447                 }
448                 dce_conn->auth_state.auth_finished = true;
449                 dce_conn->allow_request = true;
450
451                 /* Now that we are authenticated, got back to the generic session key... */
452                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
453                 return NT_STATUS_OK;
454         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
455                 return NT_STATUS_OK;
456         }
457
458         DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
459                   nt_errstr(status)));
460         return status;
461 }
462
463 /*
464   check credentials on a request
465 */
466 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
467 {
468         struct ncacn_packet *pkt = &call->pkt;
469         struct dcesrv_connection *dce_conn = call->conn;
470         NTSTATUS status;
471         uint32_t auth_length;
472         size_t hdr_size = DCERPC_REQUEST_LENGTH;
473
474         if (!dce_conn->allow_request) {
475                 return false;
476         }
477
478         if (dce_conn->auth_state.auth_invalid) {
479                 return false;
480         }
481
482         if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
483                 hdr_size += 16;
484         }
485
486         switch (dce_conn->auth_state.auth_level) {
487         case DCERPC_AUTH_LEVEL_PRIVACY:
488         case DCERPC_AUTH_LEVEL_INTEGRITY:
489                 break;
490
491         case DCERPC_AUTH_LEVEL_CONNECT:
492                 if (pkt->auth_length != 0) {
493                         break;
494                 }
495                 return true;
496         case DCERPC_AUTH_LEVEL_NONE:
497                 if (pkt->auth_length != 0) {
498                         return false;
499                 }
500                 return true;
501
502         default:
503                 return false;
504         }
505
506         if (!dce_conn->auth_state.gensec_security) {
507                 return false;
508         }
509
510         status = dcerpc_pull_auth_trailer(pkt, call,
511                                           &pkt->u.request.stub_and_verifier,
512                                           &call->in_auth_info, &auth_length, false);
513         if (!NT_STATUS_IS_OK(status)) {
514                 return false;
515         }
516
517         if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
518                 return false;
519         }
520
521         if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
522                 return false;
523         }
524
525         if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
526                 return false;
527         }
528
529         pkt->u.request.stub_and_verifier.length -= auth_length;
530
531         /* check signature or unseal the packet */
532         switch (dce_conn->auth_state.auth_level) {
533         case DCERPC_AUTH_LEVEL_PRIVACY:
534                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
535                                               full_packet->data + hdr_size,
536                                               pkt->u.request.stub_and_verifier.length, 
537                                               full_packet->data,
538                                               full_packet->length-
539                                               call->in_auth_info.credentials.length,
540                                               &call->in_auth_info.credentials);
541                 memcpy(pkt->u.request.stub_and_verifier.data, 
542                        full_packet->data + hdr_size,
543                        pkt->u.request.stub_and_verifier.length);
544                 break;
545
546         case DCERPC_AUTH_LEVEL_INTEGRITY:
547                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
548                                              pkt->u.request.stub_and_verifier.data, 
549                                              pkt->u.request.stub_and_verifier.length,
550                                              full_packet->data,
551                                              full_packet->length-
552                                              call->in_auth_info.credentials.length,
553                                              &call->in_auth_info.credentials);
554                 break;
555
556         case DCERPC_AUTH_LEVEL_CONNECT:
557                 /* for now we ignore possible signatures here */
558                 status = NT_STATUS_OK;
559                 break;
560
561         default:
562                 status = NT_STATUS_INVALID_LEVEL;
563                 break;
564         }
565
566         /* remove the indicated amount of padding */
567         if (pkt->u.request.stub_and_verifier.length < call->in_auth_info.auth_pad_length) {
568                 return false;
569         }
570         pkt->u.request.stub_and_verifier.length -= call->in_auth_info.auth_pad_length;
571
572         return NT_STATUS_IS_OK(status);
573 }
574
575
576 /* 
577    push a signed or sealed dcerpc request packet into a blob
578 */
579 bool dcesrv_auth_response(struct dcesrv_call_state *call,
580                           DATA_BLOB *blob, size_t sig_size,
581                           struct ncacn_packet *pkt)
582 {
583         struct dcesrv_connection *dce_conn = call->conn;
584         NTSTATUS status;
585         enum ndr_err_code ndr_err;
586         struct ndr_push *ndr;
587         uint32_t payload_length;
588         DATA_BLOB creds2;
589
590         switch (dce_conn->auth_state.auth_level) {
591         case DCERPC_AUTH_LEVEL_PRIVACY:
592         case DCERPC_AUTH_LEVEL_INTEGRITY:
593                 if (sig_size == 0) {
594                         return false;
595                 }
596
597                 break;
598
599         case DCERPC_AUTH_LEVEL_CONNECT:
600                 /*
601                  * TODO: let the gensec mech decide if it wants to generate a
602                  *       signature that might be needed for schannel...
603                  */
604                 status = ncacn_push_auth(blob, call, pkt, NULL);
605                 return NT_STATUS_IS_OK(status);
606
607         case DCERPC_AUTH_LEVEL_NONE:
608                 status = ncacn_push_auth(blob, call, pkt, NULL);
609                 return NT_STATUS_IS_OK(status);
610
611         default:
612                 return false;
613         }
614
615         if (!dce_conn->auth_state.gensec_security) {
616                 return false;
617         }
618
619         ndr = ndr_push_init_ctx(call);
620         if (!ndr) {
621                 return false;
622         }
623
624         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
625                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
626         }
627
628         ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
629         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
630                 return false;
631         }
632
633         call->_out_auth_info = (struct dcerpc_auth) {
634                 .auth_type = dce_conn->auth_state.auth_type,
635                 .auth_level = dce_conn->auth_state.auth_level,
636                 .auth_context_id = dce_conn->auth_state.auth_context_id,
637         };
638         call->out_auth_info = &call->_out_auth_info;
639
640         /* pad to 16 byte multiple in the payload portion of the
641            packet. This matches what w2k3 does. Note that we can't use
642            ndr_push_align() as that is relative to the start of the
643            whole packet, whereas w2k8 wants it relative to the start
644            of the stub */
645         call->out_auth_info->auth_pad_length =
646                 DCERPC_AUTH_PAD_LENGTH(pkt->u.response.stub_and_verifier.length);
647         ndr_err = ndr_push_zero(ndr, call->out_auth_info->auth_pad_length);
648         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
649                 return false;
650         }
651
652         payload_length = pkt->u.response.stub_and_verifier.length +
653                 call->out_auth_info->auth_pad_length;
654
655         /* add the auth verifier */
656         ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
657                                        call->out_auth_info);
658         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
659                 return false;
660         }
661
662         /* extract the whole packet as a blob */
663         *blob = ndr_push_blob(ndr);
664
665         /*
666          * Setup the frag and auth length in the packet buffer.
667          * This is needed if the GENSEC mech does AEAD signing
668          * of the packet headers. The signature itself will be
669          * appended later.
670          */
671         dcerpc_set_frag_length(blob, blob->length + sig_size);
672         dcerpc_set_auth_length(blob, sig_size);
673
674         /* sign or seal the packet */
675         switch (dce_conn->auth_state.auth_level) {
676         case DCERPC_AUTH_LEVEL_PRIVACY:
677                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
678                                             call,
679                                             ndr->data + DCERPC_REQUEST_LENGTH, 
680                                             payload_length,
681                                             blob->data,
682                                             blob->length,
683                                             &creds2);
684                 break;
685
686         case DCERPC_AUTH_LEVEL_INTEGRITY:
687                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
688                                             call,
689                                             ndr->data + DCERPC_REQUEST_LENGTH, 
690                                             payload_length,
691                                             blob->data,
692                                             blob->length,
693                                             &creds2);
694                 break;
695
696         default:
697                 status = NT_STATUS_INVALID_LEVEL;
698                 break;
699         }
700
701         if (!NT_STATUS_IS_OK(status)) {
702                 return false;
703         }       
704
705         if (creds2.length != sig_size) {
706                 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
707                          (unsigned)creds2.length, (uint32_t)sig_size,
708                          (unsigned)call->out_auth_info->auth_pad_length,
709                          (unsigned)pkt->u.response.stub_and_verifier.length));
710                 dcerpc_set_frag_length(blob, blob->length + creds2.length);
711                 dcerpc_set_auth_length(blob, creds2.length);
712         }
713
714         if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
715                 status = NT_STATUS_NO_MEMORY;
716                 return false;
717         }
718         data_blob_free(&creds2);
719
720         return true;
721 }