s3-librpc Remove special case for spnego dcerpc sign/seal
[metze/samba/wip.git] / source3 / librpc / rpc / dcerpc_helpers.c
1 /*
2  *  DCERPC Helper routines
3  *  Günther Deschner <gd@samba.org> 2010.
4  *  Simo Sorce <idra@samba.org> 2010.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "includes.h"
22 #include "librpc/rpc/dcerpc.h"
23 #include "librpc/gen_ndr/ndr_dcerpc.h"
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "../libcli/auth/schannel.h"
26 #include "../libcli/auth/spnego.h"
27 #include "librpc/crypto/gse.h"
28 #include "librpc/crypto/spnego.h"
29 #include "auth/gensec/gensec.h"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_RPC_PARSE
33
34 /**
35 * @brief NDR Encodes a ncacn_packet
36 *
37 * @param mem_ctx        The memory context the blob will be allocated on
38 * @param ptype          The DCERPC packet type
39 * @param pfc_flags      The DCERPC PFC Falgs
40 * @param auth_length    The length of the trailing auth blob
41 * @param call_id        The call ID
42 * @param u              The payload of the packet
43 * @param blob [out]     The encoded blob if successful
44 *
45 * @return an NTSTATUS error code
46 */
47 NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
48                                   enum dcerpc_pkt_type ptype,
49                                   uint8_t pfc_flags,
50                                   uint16_t auth_length,
51                                   uint32_t call_id,
52                                   union dcerpc_payload *u,
53                                   DATA_BLOB *blob)
54 {
55         struct ncacn_packet r;
56         enum ndr_err_code ndr_err;
57
58         r.rpc_vers              = 5;
59         r.rpc_vers_minor        = 0;
60         r.ptype                 = ptype;
61         r.pfc_flags             = pfc_flags;
62         r.drep[0]               = DCERPC_DREP_LE;
63         r.drep[1]               = 0;
64         r.drep[2]               = 0;
65         r.drep[3]               = 0;
66         r.auth_length           = auth_length;
67         r.call_id               = call_id;
68         r.u                     = *u;
69
70         ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
71                 (ndr_push_flags_fn_t)ndr_push_ncacn_packet);
72         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
73                 return ndr_map_error2ntstatus(ndr_err);
74         }
75
76         dcerpc_set_frag_length(blob, blob->length);
77
78
79         if (DEBUGLEVEL >= 10) {
80                 /* set frag len for print function */
81                 r.frag_length = blob->length;
82                 NDR_PRINT_DEBUG(ncacn_packet, &r);
83         }
84
85         return NT_STATUS_OK;
86 }
87
88 /**
89 * @brief Decodes a ncacn_packet
90 *
91 * @param mem_ctx        The memory context on which to allocate the packet
92 *                       elements
93 * @param blob           The blob of data to decode
94 * @param r              An empty ncacn_packet, must not be NULL
95 * @param bigendian      Whether the packet is bignedian encoded
96 *
97 * @return a NTSTATUS error code
98 */
99 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
100                                   const DATA_BLOB *blob,
101                                   struct ncacn_packet *r,
102                                   bool bigendian)
103 {
104         enum ndr_err_code ndr_err;
105         struct ndr_pull *ndr;
106
107         ndr = ndr_pull_init_blob(blob, mem_ctx);
108         if (!ndr) {
109                 return NT_STATUS_NO_MEMORY;
110         }
111         if (bigendian) {
112                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
113         }
114
115         ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, r);
116
117         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
118                 talloc_free(ndr);
119                 return ndr_map_error2ntstatus(ndr_err);
120         }
121         talloc_free(ndr);
122
123         if (DEBUGLEVEL >= 10) {
124                 NDR_PRINT_DEBUG(ncacn_packet, r);
125         }
126
127         return NT_STATUS_OK;
128 }
129
130 /**
131 * @brief NDR Encodes a NL_AUTH_MESSAGE
132 *
133 * @param mem_ctx        The memory context the blob will be allocated on
134 * @param r              The NL_AUTH_MESSAGE to encode
135 * @param blob [out]     The encoded blob if successful
136 *
137 * @return a NTSTATUS error code
138 */
139 NTSTATUS dcerpc_push_schannel_bind(TALLOC_CTX *mem_ctx,
140                                    struct NL_AUTH_MESSAGE *r,
141                                    DATA_BLOB *blob)
142 {
143         enum ndr_err_code ndr_err;
144
145         ndr_err = ndr_push_struct_blob(blob, mem_ctx, r,
146                 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
147         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148                 return ndr_map_error2ntstatus(ndr_err);
149         }
150
151         if (DEBUGLEVEL >= 10) {
152                 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, r);
153         }
154
155         return NT_STATUS_OK;
156 }
157
158 /**
159 * @brief NDR Encodes a dcerpc_auth structure
160 *
161 * @param mem_ctx          The memory context the blob will be allocated on
162 * @param auth_type        The DCERPC Authentication Type
163 * @param auth_level       The DCERPC Authentication Level
164 * @param auth_pad_length  The padding added to the packet this blob will be
165 *                          appended to.
166 * @param auth_context_id  The context id
167 * @param credentials      The authentication credentials blob (signature)
168 * @param blob [out]       The encoded blob if successful
169 *
170 * @return a NTSTATUS error code
171 */
172 NTSTATUS dcerpc_push_dcerpc_auth(TALLOC_CTX *mem_ctx,
173                                  enum dcerpc_AuthType auth_type,
174                                  enum dcerpc_AuthLevel auth_level,
175                                  uint8_t auth_pad_length,
176                                  uint32_t auth_context_id,
177                                  const DATA_BLOB *credentials,
178                                  DATA_BLOB *blob)
179 {
180         struct dcerpc_auth r;
181         enum ndr_err_code ndr_err;
182
183         r.auth_type             = auth_type;
184         r.auth_level            = auth_level;
185         r.auth_pad_length       = auth_pad_length;
186         r.auth_reserved         = 0;
187         r.auth_context_id       = auth_context_id;
188         r.credentials           = *credentials;
189
190         ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
191                 (ndr_push_flags_fn_t)ndr_push_dcerpc_auth);
192         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
193                 return ndr_map_error2ntstatus(ndr_err);
194         }
195
196         if (DEBUGLEVEL >= 10) {
197                 NDR_PRINT_DEBUG(dcerpc_auth, &r);
198         }
199
200         return NT_STATUS_OK;
201 }
202
203 /**
204 * @brief Decodes a dcerpc_auth blob
205 *
206 * @param mem_ctx        The memory context on which to allocate the packet
207 *                       elements
208 * @param blob           The blob of data to decode
209 * @param r              An empty dcerpc_auth structure, must not be NULL
210 *
211 * @return a NTSTATUS error code
212 */
213 NTSTATUS dcerpc_pull_dcerpc_auth(TALLOC_CTX *mem_ctx,
214                                  const DATA_BLOB *blob,
215                                  struct dcerpc_auth *r,
216                                  bool bigendian)
217 {
218         enum ndr_err_code ndr_err;
219         struct ndr_pull *ndr;
220
221         ndr = ndr_pull_init_blob(blob, mem_ctx);
222         if (!ndr) {
223                 return NT_STATUS_NO_MEMORY;
224         }
225         if (bigendian) {
226                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
227         }
228
229         ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, r);
230
231         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
232                 talloc_free(ndr);
233                 return ndr_map_error2ntstatus(ndr_err);
234         }
235         talloc_free(ndr);
236
237         if (DEBUGLEVEL >= 10) {
238                 NDR_PRINT_DEBUG(dcerpc_auth, r);
239         }
240
241         return NT_STATUS_OK;
242 }
243
244 /**
245 * @brief Calculate how much data we can in a packet, including calculating
246 *        auth token and pad lengths.
247 *
248 * @param auth           The pipe_auth_data structure for this pipe.
249 * @param header_len     The length of the packet header
250 * @param data_left      The data left in the send buffer
251 * @param max_xmit_frag  The max fragment size.
252 * @param pad_alignment  The NDR padding size.
253 * @param data_to_send   [out] The max data we will send in the pdu
254 * @param frag_len       [out] The total length of the fragment
255 * @param auth_len       [out] The length of the auth trailer
256 * @param pad_len        [out] The padding to be applied
257 *
258 * @return A NT Error status code.
259 */
260 NTSTATUS dcerpc_guess_sizes(struct pipe_auth_data *auth,
261                             size_t header_len, size_t data_left,
262                             size_t max_xmit_frag, size_t pad_alignment,
263                             size_t *data_to_send, size_t *frag_len,
264                             size_t *auth_len, size_t *pad_len)
265 {
266         size_t max_len;
267         size_t mod_len;
268         struct gensec_security *gensec_security;
269         struct schannel_state *schannel_auth;
270         struct spnego_context *spnego_ctx;
271         NTSTATUS status;
272
273         /* no auth token cases first */
274         switch (auth->auth_level) {
275         case DCERPC_AUTH_LEVEL_NONE:
276         case DCERPC_AUTH_LEVEL_CONNECT:
277         case DCERPC_AUTH_LEVEL_PACKET:
278                 max_len = max_xmit_frag - header_len;
279                 *data_to_send = MIN(max_len, data_left);
280                 *pad_len = 0;
281                 *auth_len = 0;
282                 *frag_len = header_len + *data_to_send;
283                 return NT_STATUS_OK;
284
285         case DCERPC_AUTH_LEVEL_PRIVACY:
286                 break;
287
288         case DCERPC_AUTH_LEVEL_INTEGRITY:
289                 break;
290
291         default:
292                 return NT_STATUS_INVALID_PARAMETER;
293         }
294
295
296         /* Sign/seal case, calculate auth and pad lengths */
297
298         max_len = max_xmit_frag - header_len - DCERPC_AUTH_TRAILER_LENGTH;
299
300         /* Treat the same for all authenticated rpc requests. */
301         switch (auth->auth_type) {
302         case DCERPC_AUTH_TYPE_SPNEGO:
303                 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
304                                                    struct spnego_context);
305                 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
306                 if (!NT_STATUS_IS_OK(status)) {
307                         return status;
308                 }
309                 *auth_len = gensec_sig_size(gensec_security, max_len);
310                 break;
311
312         case DCERPC_AUTH_TYPE_NTLMSSP:
313         case DCERPC_AUTH_TYPE_KRB5:
314                 gensec_security = talloc_get_type_abort(auth->auth_ctx,
315                                                         struct gensec_security);
316                 *auth_len = gensec_sig_size(gensec_security, max_len);
317                 break;
318
319         case DCERPC_AUTH_TYPE_SCHANNEL:
320                 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
321                                                       struct schannel_state);
322                 *auth_len = netsec_outgoing_sig_size(schannel_auth);
323                 break;
324         default:
325                 return NT_STATUS_INVALID_PARAMETER;
326         }
327
328         max_len -= *auth_len;
329
330         *data_to_send = MIN(max_len, data_left);
331
332         mod_len = (header_len + *data_to_send) % pad_alignment;
333         if (mod_len) {
334                 *pad_len = pad_alignment - mod_len;
335         } else {
336                 *pad_len = 0;
337         }
338
339         if (*data_to_send + *pad_len > max_len) {
340                 *data_to_send -= pad_alignment;
341         }
342
343         *frag_len = header_len + *data_to_send + *pad_len
344                         + DCERPC_AUTH_TRAILER_LENGTH + *auth_len;
345
346         return NT_STATUS_OK;
347 }
348
349 /*******************************************************************
350  Create and add the NTLMSSP sign/seal auth data.
351  ********************************************************************/
352
353 static NTSTATUS add_generic_auth_footer(struct gensec_security *gensec_security,
354                                         enum dcerpc_AuthLevel auth_level,
355                                         DATA_BLOB *rpc_out)
356 {
357         uint16_t data_and_pad_len = rpc_out->length
358                                         - DCERPC_RESPONSE_LENGTH
359                                         - DCERPC_AUTH_TRAILER_LENGTH;
360         DATA_BLOB auth_blob;
361         NTSTATUS status;
362
363         if (!gensec_security) {
364                 return NT_STATUS_INVALID_PARAMETER;
365         }
366
367         switch (auth_level) {
368         case DCERPC_AUTH_LEVEL_PRIVACY:
369                 /* Data portion is encrypted. */
370                 status = gensec_seal_packet(gensec_security,
371                                             rpc_out->data,
372                                             rpc_out->data
373                                             + DCERPC_RESPONSE_LENGTH,
374                                             data_and_pad_len,
375                                             rpc_out->data,
376                                             rpc_out->length,
377                                             &auth_blob);
378                 if (!NT_STATUS_IS_OK(status)) {
379                         return status;
380                 }
381                 break;
382
383         case DCERPC_AUTH_LEVEL_INTEGRITY:
384                 /* Data is signed. */
385                 status = gensec_sign_packet(gensec_security,
386                                             rpc_out->data,
387                                             rpc_out->data
388                                             + DCERPC_RESPONSE_LENGTH,
389                                             data_and_pad_len,
390                                             rpc_out->data,
391                                             rpc_out->length,
392                                             &auth_blob);
393                 if (!NT_STATUS_IS_OK(status)) {
394                         return status;
395                 }
396                 break;
397
398         default:
399                 /* Can't happen. */
400                 smb_panic("bad auth level");
401                 /* Notreached. */
402                 return NT_STATUS_INVALID_PARAMETER;
403         }
404
405         /* Finally attach the blob. */
406         if (!data_blob_append(NULL, rpc_out,
407                                 auth_blob.data, auth_blob.length)) {
408                 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
409                           (unsigned int)auth_blob.length));
410                 return NT_STATUS_NO_MEMORY;
411         }
412         data_blob_free(&auth_blob);
413
414         return NT_STATUS_OK;
415 }
416
417 /*******************************************************************
418  Check/unseal the NTLMSSP auth data. (Unseal in place).
419  ********************************************************************/
420
421 static NTSTATUS get_generic_auth_footer(struct gensec_security *gensec_security,
422                                         enum dcerpc_AuthLevel auth_level,
423                                         DATA_BLOB *data, DATA_BLOB *full_pkt,
424                                         DATA_BLOB *auth_token)
425 {
426         switch (auth_level) {
427         case DCERPC_AUTH_LEVEL_PRIVACY:
428                 /* Data portion is encrypted. */
429                 return gensec_unseal_packet(gensec_security,
430                                             data->data,
431                                             data->length,
432                                             full_pkt->data,
433                                             full_pkt->length,
434                                             auth_token);
435
436         case DCERPC_AUTH_LEVEL_INTEGRITY:
437                 /* Data is signed. */
438                 return gensec_check_packet(gensec_security,
439                                            data->data,
440                                            data->length,
441                                            full_pkt->data,
442                                            full_pkt->length,
443                                            auth_token);
444
445         default:
446                 return NT_STATUS_INVALID_PARAMETER;
447         }
448 }
449
450 /*******************************************************************
451  Create and add the schannel sign/seal auth data.
452  ********************************************************************/
453
454 static NTSTATUS add_schannel_auth_footer(struct schannel_state *sas,
455                                         enum dcerpc_AuthLevel auth_level,
456                                         DATA_BLOB *rpc_out)
457 {
458         uint8_t *data_p = rpc_out->data + DCERPC_RESPONSE_LENGTH;
459         size_t data_and_pad_len = rpc_out->length
460                                         - DCERPC_RESPONSE_LENGTH
461                                         - DCERPC_AUTH_TRAILER_LENGTH;
462         DATA_BLOB auth_blob;
463         NTSTATUS status;
464
465         if (!sas) {
466                 return NT_STATUS_INVALID_PARAMETER;
467         }
468
469         DEBUG(10,("add_schannel_auth_footer: SCHANNEL seq_num=%d\n",
470                         sas->seq_num));
471
472         switch (auth_level) {
473         case DCERPC_AUTH_LEVEL_PRIVACY:
474                 status = netsec_outgoing_packet(sas,
475                                                 rpc_out->data,
476                                                 true,
477                                                 data_p,
478                                                 data_and_pad_len,
479                                                 &auth_blob);
480                 break;
481         case DCERPC_AUTH_LEVEL_INTEGRITY:
482                 status = netsec_outgoing_packet(sas,
483                                                 rpc_out->data,
484                                                 false,
485                                                 data_p,
486                                                 data_and_pad_len,
487                                                 &auth_blob);
488                 break;
489         default:
490                 status = NT_STATUS_INTERNAL_ERROR;
491                 break;
492         }
493
494         if (!NT_STATUS_IS_OK(status)) {
495                 DEBUG(1,("add_schannel_auth_footer: failed to process packet: %s\n",
496                         nt_errstr(status)));
497                 return status;
498         }
499
500         if (DEBUGLEVEL >= 10) {
501                 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
502         }
503
504         /* Finally attach the blob. */
505         if (!data_blob_append(NULL, rpc_out,
506                                 auth_blob.data, auth_blob.length)) {
507                 return NT_STATUS_NO_MEMORY;
508         }
509         data_blob_free(&auth_blob);
510
511         return NT_STATUS_OK;
512 }
513
514 /*******************************************************************
515  Check/unseal the Schannel auth data. (Unseal in place).
516  ********************************************************************/
517
518 static NTSTATUS get_schannel_auth_footer(TALLOC_CTX *mem_ctx,
519                                          struct schannel_state *auth_state,
520                                          enum dcerpc_AuthLevel auth_level,
521                                          DATA_BLOB *data, DATA_BLOB *full_pkt,
522                                          DATA_BLOB *auth_token)
523 {
524         switch (auth_level) {
525         case DCERPC_AUTH_LEVEL_PRIVACY:
526                 /* Data portion is encrypted. */
527                 return netsec_incoming_packet(auth_state,
528                                                 true,
529                                                 data->data,
530                                                 data->length,
531                                                 auth_token);
532
533         case DCERPC_AUTH_LEVEL_INTEGRITY:
534                 /* Data is signed. */
535                 return netsec_incoming_packet(auth_state,
536                                                 false,
537                                                 data->data,
538                                                 data->length,
539                                                 auth_token);
540
541         default:
542                 return NT_STATUS_INVALID_PARAMETER;
543         }
544 }
545
546 /**
547 * @brief   Append an auth footer according to what is the current mechanism
548 *
549 * @param auth           The pipe_auth_data associated with the connection
550 * @param pad_len        The padding used in the packet
551 * @param rpc_out        Packet blob up to and including the auth header
552 *
553 * @return A NTSTATUS error code.
554 */
555 NTSTATUS dcerpc_add_auth_footer(struct pipe_auth_data *auth,
556                                 size_t pad_len, DATA_BLOB *rpc_out)
557 {
558         struct schannel_state *schannel_auth;
559         struct gensec_security *gensec_security;
560         struct spnego_context *spnego_ctx;
561         char pad[CLIENT_NDR_PADDING_SIZE] = { 0, };
562         DATA_BLOB auth_info;
563         DATA_BLOB auth_blob;
564         NTSTATUS status;
565
566         if (auth->auth_type == DCERPC_AUTH_TYPE_NONE ||
567             auth->auth_type == DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM) {
568                 return NT_STATUS_OK;
569         }
570
571         if (pad_len) {
572                 /* Copy the sign/seal padding data. */
573                 if (!data_blob_append(NULL, rpc_out, pad, pad_len)) {
574                         return NT_STATUS_NO_MEMORY;
575                 }
576         }
577
578         /* marshall the dcerpc_auth with an actually empty auth_blob.
579          * This is needed because the ntmlssp signature includes the
580          * auth header. We will append the actual blob later. */
581         auth_blob = data_blob_null;
582         status = dcerpc_push_dcerpc_auth(rpc_out->data,
583                                          auth->auth_type,
584                                          auth->auth_level,
585                                          pad_len,
586                                          1 /* context id. */,
587                                          &auth_blob,
588                                          &auth_info);
589         if (!NT_STATUS_IS_OK(status)) {
590                 return status;
591         }
592
593         /* append the header */
594         if (!data_blob_append(NULL, rpc_out,
595                                 auth_info.data, auth_info.length)) {
596                 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
597                           (unsigned int)auth_info.length));
598                 return NT_STATUS_NO_MEMORY;
599         }
600         data_blob_free(&auth_info);
601
602         /* Generate any auth sign/seal and add the auth footer. */
603         switch (auth->auth_type) {
604         case DCERPC_AUTH_TYPE_NONE:
605         case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
606                 status = NT_STATUS_OK;
607                 break;
608         case DCERPC_AUTH_TYPE_SPNEGO:
609                 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
610                                                    struct spnego_context);
611                 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
612                 if (!NT_STATUS_IS_OK(status)) {
613                         return status;
614                 }
615
616                 status = add_generic_auth_footer(gensec_security,
617                                                  auth->auth_level,
618                                                  rpc_out);
619                 break;
620         case DCERPC_AUTH_TYPE_KRB5:
621         case DCERPC_AUTH_TYPE_NTLMSSP:
622                 gensec_security = talloc_get_type_abort(auth->auth_ctx,
623                                                 struct gensec_security);
624                 status = add_generic_auth_footer(gensec_security,
625                                                  auth->auth_level,
626                                                  rpc_out);
627                 break;
628         case DCERPC_AUTH_TYPE_SCHANNEL:
629                 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
630                                                       struct schannel_state);
631                 status = add_schannel_auth_footer(schannel_auth,
632                                                   auth->auth_level,
633                                                   rpc_out);
634                 break;
635         default:
636                 status = NT_STATUS_INVALID_PARAMETER;
637                 break;
638         }
639
640         return status;
641 }
642
643 /**
644 * @brief Check authentication for request/response packets
645 *
646 * @param auth           The auth data for the connection
647 * @param pkt            The actual ncacn_packet
648 * @param pkt_trailer    The stub_and_verifier part of the packet
649 * @param header_size    The header size
650 * @param raw_pkt        The whole raw packet data blob
651 * @param pad_len        [out] The padding length used in the packet
652 *
653 * @return A NTSTATUS error code
654 */
655 NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
656                            struct ncacn_packet *pkt,
657                            DATA_BLOB *pkt_trailer,
658                            size_t header_size,
659                            DATA_BLOB *raw_pkt,
660                            size_t *pad_len)
661 {
662         struct schannel_state *schannel_auth;
663         struct gensec_security *gensec_security;
664         struct spnego_context *spnego_ctx;
665         NTSTATUS status;
666         struct dcerpc_auth auth_info;
667         uint32_t auth_length;
668         DATA_BLOB full_pkt;
669         DATA_BLOB data;
670
671         switch (auth->auth_level) {
672         case DCERPC_AUTH_LEVEL_PRIVACY:
673                 DEBUG(10, ("Requested Privacy.\n"));
674                 break;
675
676         case DCERPC_AUTH_LEVEL_INTEGRITY:
677                 DEBUG(10, ("Requested Integrity.\n"));
678                 break;
679
680         case DCERPC_AUTH_LEVEL_CONNECT:
681                 if (pkt->auth_length != 0) {
682                         break;
683                 }
684                 *pad_len = 0;
685                 return NT_STATUS_OK;
686
687         case DCERPC_AUTH_LEVEL_NONE:
688                 if (pkt->auth_length != 0) {
689                         DEBUG(3, ("Got non-zero auth len on non "
690                                   "authenticated connection!\n"));
691                         return NT_STATUS_INVALID_PARAMETER;
692                 }
693                 *pad_len = 0;
694                 return NT_STATUS_OK;
695
696         default:
697                 DEBUG(3, ("Unimplemented Auth Level %d",
698                           auth->auth_level));
699                 return NT_STATUS_INVALID_PARAMETER;
700         }
701
702         /* Paranioa checks for auth_length. */
703         if (pkt->auth_length > pkt->frag_length) {
704                 return NT_STATUS_INFO_LENGTH_MISMATCH;
705         }
706         if (((unsigned int)pkt->auth_length
707              + DCERPC_AUTH_TRAILER_LENGTH < (unsigned int)pkt->auth_length) ||
708             ((unsigned int)pkt->auth_length
709              + DCERPC_AUTH_TRAILER_LENGTH < DCERPC_AUTH_TRAILER_LENGTH)) {
710                 /* Integer wrap attempt. */
711                 return NT_STATUS_INFO_LENGTH_MISMATCH;
712         }
713
714         status = dcerpc_pull_auth_trailer(pkt, pkt, pkt_trailer,
715                                           &auth_info, &auth_length, false);
716         if (!NT_STATUS_IS_OK(status)) {
717                 return status;
718         }
719
720         data = data_blob_const(raw_pkt->data + header_size,
721                                 pkt_trailer->length - auth_length);
722         full_pkt = data_blob_const(raw_pkt->data,
723                                 raw_pkt->length - auth_info.credentials.length);
724
725         switch (auth->auth_type) {
726         case DCERPC_AUTH_TYPE_NONE:
727         case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
728                 return NT_STATUS_OK;
729
730         case DCERPC_AUTH_TYPE_SPNEGO:
731                 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
732                                                    struct spnego_context);
733                 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
734                 if (!NT_STATUS_IS_OK(status)) {
735                         return status;
736                 }
737
738                 status = get_generic_auth_footer(gensec_security,
739                                                  auth->auth_level,
740                                                  &data, &full_pkt,
741                                                  &auth_info.credentials);
742
743                 if (!NT_STATUS_IS_OK(status)) {
744                         return status;
745                 }
746                 break;
747
748         case DCERPC_AUTH_TYPE_KRB5:
749         case DCERPC_AUTH_TYPE_NTLMSSP:
750
751                 DEBUG(10, ("GENSEC auth\n"));
752
753                 gensec_security = talloc_get_type_abort(auth->auth_ctx,
754                                                 struct gensec_security);
755                 status = get_generic_auth_footer(gensec_security,
756                                                  auth->auth_level,
757                                                  &data, &full_pkt,
758                                                  &auth_info.credentials);
759                 if (!NT_STATUS_IS_OK(status)) {
760                         return status;
761                 }
762                 break;
763
764         case DCERPC_AUTH_TYPE_SCHANNEL:
765
766                 DEBUG(10, ("SCHANNEL auth\n"));
767
768                 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
769                                                       struct schannel_state);
770                 status = get_schannel_auth_footer(pkt, schannel_auth,
771                                                   auth->auth_level,
772                                                   &data, &full_pkt,
773                                                   &auth_info.credentials);
774                 if (!NT_STATUS_IS_OK(status)) {
775                         return status;
776                 }
777                 break;
778
779         default:
780                 DEBUG(0, ("process_request_pdu: "
781                           "unknown auth type %u set.\n",
782                           (unsigned int)auth->auth_type));
783                 return NT_STATUS_INVALID_PARAMETER;
784         }
785
786         /* TODO: remove later
787          * this is still needed because in the server code the
788          * pkt_trailer actually has a copy of the raw data, and they
789          * are still both used in later calls */
790         if (auth->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
791                 memcpy(pkt_trailer->data, data.data, data.length);
792         }
793
794         *pad_len = auth_info.auth_pad_length;
795         data_blob_free(&auth_info.credentials);
796         return NT_STATUS_OK;
797 }
798