Check "auth event notification" param in log_json
[nivanova/samba-autobuild/.git] / auth / gensec / schannel.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    dcerpc schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 <tevent.h>
25 #include "lib/util/tevent_ntstatus.h"
26 #include "librpc/gen_ndr/ndr_schannel.h"
27 #include "auth/auth.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_internal.h"
31 #include "auth/gensec/gensec_proto.h"
32 #include "../libcli/auth/schannel.h"
33 #include "librpc/gen_ndr/dcerpc.h"
34 #include "param/param.h"
35 #include "auth/gensec/gensec_toplevel_proto.h"
36 #include "lib/crypto/crypto.h"
37 #include "libds/common/roles.h"
38
39 #undef DBGC_CLASS
40 #define DBGC_CLASS DBGC_AUTH
41
42 struct schannel_state {
43         struct gensec_security *gensec;
44         uint64_t seq_num;
45         bool initiator;
46         struct netlogon_creds_CredentialState *creds;
47         struct auth_user_info_dc *user_info_dc;
48 };
49
50 #define SETUP_SEQNUM(state, buf, initiator) do { \
51         uint8_t *_buf = buf; \
52         uint32_t _seq_num_low = (state)->seq_num & UINT32_MAX; \
53         uint32_t _seq_num_high = (state)->seq_num >> 32; \
54         if (initiator) { \
55                 _seq_num_high |= 0x80000000; \
56         } \
57         RSIVAL(_buf, 0, _seq_num_low); \
58         RSIVAL(_buf, 4, _seq_num_high); \
59 } while(0)
60
61 static struct schannel_state *netsec_create_state(
62                                 struct gensec_security *gensec,
63                                 struct netlogon_creds_CredentialState *creds,
64                                 bool initiator)
65 {
66         struct schannel_state *state;
67
68         state = talloc_zero(gensec, struct schannel_state);
69         if (state == NULL) {
70                 return NULL;
71         }
72
73         state->gensec = gensec;
74         state->initiator = initiator;
75         state->creds = netlogon_creds_copy(state, creds);
76         if (state->creds == NULL) {
77                 talloc_free(state);
78                 return NULL;
79         }
80
81         gensec->private_data = state;
82
83         return state;
84 }
85
86 static void netsec_offset_and_sizes(struct schannel_state *state,
87                                     bool do_seal,
88                                     uint32_t *_min_sig_size,
89                                     uint32_t *_used_sig_size,
90                                     uint32_t *_checksum_length,
91                                     uint32_t *_confounder_ofs)
92 {
93         uint32_t min_sig_size;
94         uint32_t used_sig_size;
95         uint32_t checksum_length;
96         uint32_t confounder_ofs;
97
98         if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
99                 min_sig_size = 48;
100                 used_sig_size = 56;
101                 /*
102                  * Note: windows has a bug here and uses the old values...
103                  *
104                  * checksum_length = 32;
105                  * confounder_ofs = 48;
106                  */
107                 checksum_length = 8;
108                 confounder_ofs = 24;
109         } else {
110                 min_sig_size = 24;
111                 used_sig_size = 32;
112                 checksum_length = 8;
113                 confounder_ofs = 24;
114         }
115
116         if (do_seal) {
117                 min_sig_size += 8;
118         }
119
120         if (_min_sig_size) {
121                 *_min_sig_size = min_sig_size;
122         }
123
124         if (_used_sig_size) {
125                 *_used_sig_size = used_sig_size;
126         }
127
128         if (_checksum_length) {
129                 *_checksum_length = checksum_length;
130         }
131
132         if (_confounder_ofs) {
133                 *_confounder_ofs = confounder_ofs;
134         }
135 }
136
137 /*******************************************************************
138  Encode or Decode the sequence number (which is symmetric)
139  ********************************************************************/
140 static void netsec_do_seq_num(struct schannel_state *state,
141                               const uint8_t *checksum,
142                               uint32_t checksum_length,
143                               uint8_t seq_num[8])
144 {
145         if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
146                 AES_KEY key;
147                 uint8_t iv[AES_BLOCK_SIZE];
148
149                 AES_set_encrypt_key(state->creds->session_key, 128, &key);
150                 ZERO_STRUCT(iv);
151                 memcpy(iv+0, checksum, 8);
152                 memcpy(iv+8, checksum, 8);
153
154                 aes_cfb8_encrypt(seq_num, seq_num, 8, &key, iv, AES_ENCRYPT);
155         } else {
156                 static const uint8_t zeros[4];
157                 uint8_t sequence_key[16];
158                 uint8_t digest1[16];
159
160                 hmac_md5(state->creds->session_key, zeros, sizeof(zeros), digest1);
161                 hmac_md5(digest1, checksum, checksum_length, sequence_key);
162                 arcfour_crypt(seq_num, sequence_key, 8);
163         }
164
165         state->seq_num++;
166 }
167
168 static void netsec_do_seal(struct schannel_state *state,
169                            const uint8_t seq_num[8],
170                            uint8_t confounder[8],
171                            uint8_t *data, uint32_t length,
172                            bool forward)
173 {
174         if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
175                 AES_KEY key;
176                 uint8_t iv[AES_BLOCK_SIZE];
177                 uint8_t sess_kf0[16];
178                 int i;
179
180                 for (i = 0; i < 16; i++) {
181                         sess_kf0[i] = state->creds->session_key[i] ^ 0xf0;
182                 }
183
184                 AES_set_encrypt_key(sess_kf0, 128, &key);
185                 ZERO_STRUCT(iv);
186                 memcpy(iv+0, seq_num, 8);
187                 memcpy(iv+8, seq_num, 8);
188
189                 if (forward) {
190                         aes_cfb8_encrypt(confounder, confounder, 8, &key, iv, AES_ENCRYPT);
191                         aes_cfb8_encrypt(data, data, length, &key, iv, AES_ENCRYPT);
192                 } else {
193                         aes_cfb8_encrypt(confounder, confounder, 8, &key, iv, AES_DECRYPT);
194                         aes_cfb8_encrypt(data, data, length, &key, iv, AES_DECRYPT);
195                 }
196         } else {
197                 uint8_t sealing_key[16];
198                 static const uint8_t zeros[4];
199                 uint8_t digest2[16];
200                 uint8_t sess_kf0[16];
201                 int i;
202
203                 for (i = 0; i < 16; i++) {
204                         sess_kf0[i] = state->creds->session_key[i] ^ 0xf0;
205                 }
206
207                 hmac_md5(sess_kf0, zeros, 4, digest2);
208                 hmac_md5(digest2, seq_num, 8, sealing_key);
209
210                 arcfour_crypt(confounder, sealing_key, 8);
211                 arcfour_crypt(data, sealing_key, length);
212         }
213 }
214
215 /*******************************************************************
216  Create a digest over the entire packet (including the data), and
217  MD5 it with the session key.
218  ********************************************************************/
219 static void netsec_do_sign(struct schannel_state *state,
220                            const uint8_t *confounder,
221                            const uint8_t *data, size_t length,
222                            uint8_t header[8],
223                            uint8_t *checksum)
224 {
225         if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
226                 struct HMACSHA256Context ctx;
227
228                 hmac_sha256_init(state->creds->session_key,
229                                  sizeof(state->creds->session_key),
230                                  &ctx);
231
232                 if (confounder) {
233                         SSVAL(header, 0, NL_SIGN_HMAC_SHA256);
234                         SSVAL(header, 2, NL_SEAL_AES128);
235                         SSVAL(header, 4, 0xFFFF);
236                         SSVAL(header, 6, 0x0000);
237
238                         hmac_sha256_update(header, 8, &ctx);
239                         hmac_sha256_update(confounder, 8, &ctx);
240                 } else {
241                         SSVAL(header, 0, NL_SIGN_HMAC_SHA256);
242                         SSVAL(header, 2, NL_SEAL_NONE);
243                         SSVAL(header, 4, 0xFFFF);
244                         SSVAL(header, 6, 0x0000);
245
246                         hmac_sha256_update(header, 8, &ctx);
247                 }
248
249                 hmac_sha256_update(data, length, &ctx);
250
251                 hmac_sha256_final(checksum, &ctx);
252         } else {
253                 uint8_t packet_digest[16];
254                 static const uint8_t zeros[4];
255                 MD5_CTX ctx;
256
257                 MD5Init(&ctx);
258                 MD5Update(&ctx, zeros, 4);
259                 if (confounder) {
260                         SSVAL(header, 0, NL_SIGN_HMAC_MD5);
261                         SSVAL(header, 2, NL_SEAL_RC4);
262                         SSVAL(header, 4, 0xFFFF);
263                         SSVAL(header, 6, 0x0000);
264
265                         MD5Update(&ctx, header, 8);
266                         MD5Update(&ctx, confounder, 8);
267                 } else {
268                         SSVAL(header, 0, NL_SIGN_HMAC_MD5);
269                         SSVAL(header, 2, NL_SEAL_NONE);
270                         SSVAL(header, 4, 0xFFFF);
271                         SSVAL(header, 6, 0x0000);
272
273                         MD5Update(&ctx, header, 8);
274                 }
275                 MD5Update(&ctx, data, length);
276                 MD5Final(packet_digest, &ctx);
277
278                 hmac_md5(state->creds->session_key,
279                          packet_digest, sizeof(packet_digest),
280                          checksum);
281         }
282 }
283
284 static NTSTATUS netsec_incoming_packet(struct schannel_state *state,
285                                 bool do_unseal,
286                                 uint8_t *data, size_t length,
287                                 const uint8_t *whole_pdu, size_t pdu_length,
288                                 const DATA_BLOB *sig)
289 {
290         uint32_t min_sig_size = 0;
291         uint8_t header[8];
292         uint8_t checksum[32];
293         uint32_t checksum_length = sizeof(checksum_length);
294         uint8_t _confounder[8];
295         uint8_t *confounder = NULL;
296         uint32_t confounder_ofs = 0;
297         uint8_t seq_num[8];
298         int ret;
299         const uint8_t *sign_data = NULL;
300         size_t sign_length = 0;
301
302         netsec_offset_and_sizes(state,
303                                 do_unseal,
304                                 &min_sig_size,
305                                 NULL,
306                                 &checksum_length,
307                                 &confounder_ofs);
308
309         if (sig->length < min_sig_size) {
310                 return NT_STATUS_ACCESS_DENIED;
311         }
312
313         if (do_unseal) {
314                 confounder = _confounder;
315                 memcpy(confounder, sig->data+confounder_ofs, 8);
316         } else {
317                 confounder = NULL;
318         }
319
320         SETUP_SEQNUM(state, seq_num, !state->initiator);
321
322         if (do_unseal) {
323                 netsec_do_seal(state, seq_num,
324                                confounder,
325                                data, length,
326                                false);
327         }
328
329         if (state->gensec->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
330                 sign_data = whole_pdu;
331                 sign_length = pdu_length;
332         } else {
333                 sign_data = data;
334                 sign_length = length;
335         }
336
337         netsec_do_sign(state, confounder,
338                        sign_data, sign_length,
339                        header, checksum);
340
341         ret = memcmp(checksum, sig->data+16, checksum_length);
342         if (ret != 0) {
343                 dump_data_pw("calc digest:", checksum, checksum_length);
344                 dump_data_pw("wire digest:", sig->data+16, checksum_length);
345                 return NT_STATUS_ACCESS_DENIED;
346         }
347
348         netsec_do_seq_num(state, checksum, checksum_length, seq_num);
349
350         ret = memcmp(seq_num, sig->data+8, 8);
351         if (ret != 0) {
352                 dump_data_pw("calc seq num:", seq_num, 8);
353                 dump_data_pw("wire seq num:", sig->data+8, 8);
354                 return NT_STATUS_ACCESS_DENIED;
355         }
356
357         return NT_STATUS_OK;
358 }
359
360 static uint32_t netsec_outgoing_sig_size(struct schannel_state *state)
361 {
362         uint32_t sig_size = 0;
363
364         netsec_offset_and_sizes(state,
365                                 true,
366                                 NULL,
367                                 &sig_size,
368                                 NULL,
369                                 NULL);
370
371         return sig_size;
372 }
373
374 static NTSTATUS netsec_outgoing_packet(struct schannel_state *state,
375                                 TALLOC_CTX *mem_ctx,
376                                 bool do_seal,
377                                 uint8_t *data, size_t length,
378                                 const uint8_t *whole_pdu, size_t pdu_length,
379                                 DATA_BLOB *sig)
380 {
381         uint32_t min_sig_size = 0;
382         uint32_t used_sig_size = 0;
383         uint8_t header[8];
384         uint8_t checksum[32];
385         uint32_t checksum_length = sizeof(checksum_length);
386         uint8_t _confounder[8];
387         uint8_t *confounder = NULL;
388         uint32_t confounder_ofs = 0;
389         uint8_t seq_num[8];
390         const uint8_t *sign_data = NULL;
391         size_t sign_length = 0;
392
393         netsec_offset_and_sizes(state,
394                                 do_seal,
395                                 &min_sig_size,
396                                 &used_sig_size,
397                                 &checksum_length,
398                                 &confounder_ofs);
399
400         SETUP_SEQNUM(state, seq_num, state->initiator);
401
402         if (do_seal) {
403                 confounder = _confounder;
404                 generate_random_buffer(confounder, 8);
405         } else {
406                 confounder = NULL;
407         }
408
409         if (state->gensec->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
410                 sign_data = whole_pdu;
411                 sign_length = pdu_length;
412         } else {
413                 sign_data = data;
414                 sign_length = length;
415         }
416
417         netsec_do_sign(state, confounder,
418                        sign_data, sign_length,
419                        header, checksum);
420
421         if (do_seal) {
422                 netsec_do_seal(state, seq_num,
423                                confounder,
424                                data, length,
425                                true);
426         }
427
428         netsec_do_seq_num(state, checksum, checksum_length, seq_num);
429
430         (*sig) = data_blob_talloc_zero(mem_ctx, used_sig_size);
431
432         memcpy(sig->data, header, 8);
433         memcpy(sig->data+8, seq_num, 8);
434         memcpy(sig->data+16, checksum, checksum_length);
435
436         if (confounder) {
437                 memcpy(sig->data+confounder_ofs, confounder, 8);
438         }
439
440         dump_data_pw("signature:", sig->data+ 0, 8);
441         dump_data_pw("seq_num  :", sig->data+ 8, 8);
442         dump_data_pw("digest   :", sig->data+16, checksum_length);
443         dump_data_pw("confound :", sig->data+confounder_ofs, 8);
444
445         return NT_STATUS_OK;
446 }
447
448 _PUBLIC_ NTSTATUS gensec_schannel_init(TALLOC_CTX *ctx);
449
450 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
451 {
452         struct schannel_state *state =
453                 talloc_get_type_abort(gensec_security->private_data,
454                 struct schannel_state);
455
456         return netsec_outgoing_sig_size(state);
457 }
458
459 struct schannel_update_state {
460         NTSTATUS status;
461         DATA_BLOB out;
462 };
463
464 static NTSTATUS schannel_update_internal(struct gensec_security *gensec_security,
465                                          TALLOC_CTX *out_mem_ctx,
466                                          const DATA_BLOB in, DATA_BLOB *out);
467
468 static struct tevent_req *schannel_update_send(TALLOC_CTX *mem_ctx,
469                                                struct tevent_context *ev,
470                                                struct gensec_security *gensec_security,
471                                                const DATA_BLOB in)
472 {
473         struct tevent_req *req;
474         struct schannel_update_state *state = NULL;
475         NTSTATUS status;
476
477         req = tevent_req_create(mem_ctx, &state,
478                                 struct schannel_update_state);
479         if (req == NULL) {
480                 return NULL;
481         }
482
483         status = schannel_update_internal(gensec_security,
484                                           state, in,
485                                           &state->out);
486         state->status = status;
487         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
488                 status = NT_STATUS_OK;
489         }
490         if (tevent_req_nterror(req, status)) {
491                 return tevent_req_post(req, ev);
492         }
493
494         tevent_req_done(req);
495         return tevent_req_post(req, ev);
496 }
497
498 static NTSTATUS schannel_update_internal(struct gensec_security *gensec_security,
499                                          TALLOC_CTX *out_mem_ctx,
500                                          const DATA_BLOB in, DATA_BLOB *out)
501 {
502         struct schannel_state *state =
503                 talloc_get_type(gensec_security->private_data,
504                 struct schannel_state);
505         NTSTATUS status;
506         enum ndr_err_code ndr_err;
507         struct NL_AUTH_MESSAGE bind_schannel = {};
508         struct NL_AUTH_MESSAGE bind_schannel_ack;
509         struct netlogon_creds_CredentialState *creds;
510         const char *workstation;
511         const char *domain;
512
513         *out = data_blob(NULL, 0);
514
515         if (gensec_security->dcerpc_auth_level < DCERPC_AUTH_LEVEL_INTEGRITY) {
516                 switch (gensec_security->gensec_role) {
517                 case GENSEC_CLIENT:
518                         return NT_STATUS_INVALID_PARAMETER_MIX;
519                 case GENSEC_SERVER:
520                         return NT_STATUS_INVALID_PARAMETER;
521                 }
522                 return NT_STATUS_INTERNAL_ERROR;
523         }
524
525         switch (gensec_security->gensec_role) {
526         case GENSEC_CLIENT:
527                 if (state != NULL) {
528                         /* we could parse the bind ack, but we don't know what it is yet */
529                         return NT_STATUS_OK;
530                 }
531
532                 creds = cli_credentials_get_netlogon_creds(gensec_security->credentials);
533                 if (creds == NULL) {
534                         return NT_STATUS_INVALID_PARAMETER_MIX;
535                 }
536
537                 state = netsec_create_state(gensec_security,
538                                             creds, true /* initiator */);
539                 if (state == NULL) {
540                         return NT_STATUS_NO_MEMORY;
541                 }
542
543                 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
544
545                 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
546                                       NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
547                 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
548                 bind_schannel.oem_netbios_computer.a = creds->computer_name;
549
550                 if (creds->secure_channel_type == SEC_CHAN_DNS_DOMAIN) {
551                         bind_schannel.Flags |= NL_FLAG_UTF8_DNS_DOMAIN_NAME;
552                         bind_schannel.utf8_dns_domain.u = cli_credentials_get_realm(gensec_security->credentials);
553
554                         bind_schannel.Flags |= NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
555                         bind_schannel.utf8_netbios_computer.u = creds->computer_name;
556                 }
557
558                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
559                                                (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
560                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
561                         status = ndr_map_error2ntstatus(ndr_err);
562                         DEBUG(3, ("Could not create schannel bind: %s\n",
563                                   nt_errstr(status)));
564                         return status;
565                 }
566
567                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
568         case GENSEC_SERVER:
569
570                 if (state != NULL) {
571                         /* no third leg on this protocol */
572                         return NT_STATUS_INVALID_PARAMETER;
573                 }
574
575                 /* parse the schannel startup blob */
576                 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
577                         (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
578                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
579                         status = ndr_map_error2ntstatus(ndr_err);
580                         DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
581                                   nt_errstr(status)));
582                         return status;
583                 }
584
585                 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
586                         domain = bind_schannel.oem_netbios_domain.a;
587                         if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
588                                 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
589                                           domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
590                                 return NT_STATUS_LOGON_FAILURE;
591                         }
592                 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
593                         domain = bind_schannel.utf8_dns_domain.u;
594                         if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
595                                 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
596                                           domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
597                                 return NT_STATUS_LOGON_FAILURE;
598                         }
599                 } else {
600                         DEBUG(3, ("Request for schannel to without domain\n"));
601                         return NT_STATUS_LOGON_FAILURE;
602                 }
603
604                 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
605                         workstation = bind_schannel.oem_netbios_computer.a;
606                 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
607                         workstation = bind_schannel.utf8_netbios_computer.u;
608                 } else {
609                         DEBUG(3, ("Request for schannel to without netbios workstation\n"));
610                         return NT_STATUS_LOGON_FAILURE;
611                 }
612
613                 status = schannel_get_creds_state(out_mem_ctx,
614                                                   gensec_security->settings->lp_ctx,
615                                                   workstation, &creds);
616                 if (!NT_STATUS_IS_OK(status)) {
617                         DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
618                                   workstation, nt_errstr(status)));
619                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
620                                 return NT_STATUS_LOGON_FAILURE;
621                         }
622                         return status;
623                 }
624
625                 state = netsec_create_state(gensec_security,
626                                             creds, false /* not initiator */);
627                 if (state == NULL) {
628                         return NT_STATUS_NO_MEMORY;
629                 }
630
631                 status = auth_anonymous_user_info_dc(state,
632                                 lpcfg_netbios_name(gensec_security->settings->lp_ctx),
633                                 &state->user_info_dc);
634                 if (!NT_STATUS_IS_OK(status)) {
635                         return status;
636                 }
637
638                 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
639                 bind_schannel_ack.Flags = 0;
640                 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
641                                                             * this does not have
642                                                             * any meaning here
643                                                             * - gd */
644
645                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
646                                                (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
647                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
648                         status = ndr_map_error2ntstatus(ndr_err);
649                         DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
650                                   workstation, nt_errstr(status)));
651                         return status;
652                 }
653
654                 return NT_STATUS_OK;
655         }
656         return NT_STATUS_INVALID_PARAMETER;
657 }
658
659 static NTSTATUS schannel_update_recv(struct tevent_req *req,
660                                      TALLOC_CTX *out_mem_ctx,
661                                      DATA_BLOB *out)
662 {
663         struct schannel_update_state *state =
664                 tevent_req_data(req,
665                 struct schannel_update_state);
666         NTSTATUS status;
667
668         *out = data_blob_null;
669
670         if (tevent_req_is_nterror(req, &status)) {
671                 tevent_req_received(req);
672                 return status;
673         }
674
675         status = state->status;
676         talloc_steal(out_mem_ctx, state->out.data);
677         *out = state->out;
678         tevent_req_received(req);
679         return status;
680 }
681
682 /**
683  * Returns anonymous credentials for schannel, matching Win2k3.
684  *
685  */
686
687 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
688                                       TALLOC_CTX *mem_ctx,
689                                       struct auth_session_info **_session_info)
690 {
691         struct schannel_state *state =
692                 talloc_get_type(gensec_security->private_data,
693                 struct schannel_state);
694         struct auth4_context *auth_ctx = gensec_security->auth_context;
695         struct auth_session_info *session_info = NULL;
696         uint32_t session_info_flags = 0;
697         NTSTATUS status;
698
699         if (auth_ctx == NULL) {
700                 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
701                 return NT_STATUS_INTERNAL_ERROR;
702         }
703
704         if (auth_ctx->generate_session_info == NULL) {
705                 DEBUG(0, ("Cannot generate a session_info without the generate_session_info hook\n"));
706                 return NT_STATUS_INTERNAL_ERROR;
707         }
708
709         if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
710                 session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
711         }
712
713         session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
714
715         status = auth_ctx->generate_session_info(
716                                 auth_ctx,
717                                 mem_ctx,
718                                 state->user_info_dc,
719                                 state->user_info_dc->info->account_name,
720                                 session_info_flags,
721                                 &session_info);
722         if (!NT_STATUS_IS_OK(status)) {
723                 return status;
724         }
725
726         *_session_info = session_info;
727         return NT_STATUS_OK;
728 }
729
730 /*
731  * Reduce the attack surface by ensuring schannel is not availble when
732  * we are not a DC
733  */
734 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
735 {
736         enum server_role server_role
737                 = lpcfg_server_role(gensec_security->settings->lp_ctx);
738
739         switch (server_role) {
740         case ROLE_DOMAIN_BDC:
741         case ROLE_DOMAIN_PDC:
742         case ROLE_ACTIVE_DIRECTORY_DC:
743                 return NT_STATUS_OK;
744         default:
745                 return NT_STATUS_NOT_IMPLEMENTED;
746         }
747 }
748
749 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
750 {
751         return NT_STATUS_OK;
752 }
753
754 static bool schannel_have_feature(struct gensec_security *gensec_security,
755                                          uint32_t feature)
756 {
757         if (gensec_security->dcerpc_auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
758                 if (feature & GENSEC_FEATURE_SIGN) {
759                         return true;
760                 }
761         }
762         if (gensec_security->dcerpc_auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
763                 if (feature & GENSEC_FEATURE_SEAL) {
764                         return true;
765                 }
766         }
767         if (feature & GENSEC_FEATURE_DCE_STYLE) {
768                 return true;
769         }
770         if (feature & GENSEC_FEATURE_SIGN_PKT_HEADER) {
771                 return true;
772         }
773         return false;
774 }
775
776 /*
777   unseal a packet
778 */
779 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
780                                        uint8_t *data, size_t length,
781                                        const uint8_t *whole_pdu, size_t pdu_length,
782                                        const DATA_BLOB *sig)
783 {
784         struct schannel_state *state =
785                 talloc_get_type_abort(gensec_security->private_data,
786                 struct schannel_state);
787
788         return netsec_incoming_packet(state, true,
789                                       discard_const_p(uint8_t, data),
790                                       length,
791                                       whole_pdu, pdu_length,
792                                       sig);
793 }
794
795 /*
796   check the signature on a packet
797 */
798 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
799                                       const uint8_t *data, size_t length,
800                                       const uint8_t *whole_pdu, size_t pdu_length,
801                                       const DATA_BLOB *sig)
802 {
803         struct schannel_state *state =
804                 talloc_get_type_abort(gensec_security->private_data,
805                 struct schannel_state);
806
807         return netsec_incoming_packet(state, false,
808                                       discard_const_p(uint8_t, data),
809                                       length,
810                                       whole_pdu, pdu_length,
811                                       sig);
812 }
813 /*
814   seal a packet
815 */
816 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
817                                      TALLOC_CTX *mem_ctx,
818                                      uint8_t *data, size_t length,
819                                      const uint8_t *whole_pdu, size_t pdu_length,
820                                      DATA_BLOB *sig)
821 {
822         struct schannel_state *state =
823                 talloc_get_type_abort(gensec_security->private_data,
824                 struct schannel_state);
825
826         return netsec_outgoing_packet(state, mem_ctx, true,
827                                       data, length,
828                                       whole_pdu, pdu_length,
829                                       sig);
830 }
831
832 /*
833   sign a packet
834 */
835 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
836                                      TALLOC_CTX *mem_ctx,
837                                      const uint8_t *data, size_t length,
838                                      const uint8_t *whole_pdu, size_t pdu_length,
839                                      DATA_BLOB *sig)
840 {
841         struct schannel_state *state =
842                 talloc_get_type_abort(gensec_security->private_data,
843                 struct schannel_state);
844
845         return netsec_outgoing_packet(state, mem_ctx, false,
846                                       discard_const_p(uint8_t, data),
847                                       length,
848                                       whole_pdu, pdu_length,
849                                       sig);
850 }
851
852 static const struct gensec_security_ops gensec_schannel_security_ops = {
853         .name           = "schannel",
854         .auth_type      = DCERPC_AUTH_TYPE_SCHANNEL,
855         .client_start   = schannel_client_start,
856         .server_start   = schannel_server_start,
857         .update_send    = schannel_update_send,
858         .update_recv    = schannel_update_recv,
859         .seal_packet    = schannel_seal_packet,
860         .sign_packet    = schannel_sign_packet,
861         .check_packet   = schannel_check_packet,
862         .unseal_packet  = schannel_unseal_packet,
863         .session_info   = schannel_session_info,
864         .sig_size       = schannel_sig_size,
865         .have_feature   = schannel_have_feature,
866         .enabled        = true,
867         .priority       = GENSEC_SCHANNEL
868 };
869
870 _PUBLIC_ NTSTATUS gensec_schannel_init(TALLOC_CTX *ctx)
871 {
872         NTSTATUS ret;
873         ret = gensec_register(ctx, &gensec_schannel_security_ops);
874         if (!NT_STATUS_IS_OK(ret)) {
875                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
876                         gensec_schannel_security_ops.name));
877                 return ret;
878         }
879
880         return ret;
881 }