s3: Pass smbd_server_connection to srv_free_enc_buffer
[Samba/gebeck_regimport.git] / source3 / smbd / seal.c
blob7ec8c2d75f6989da295bba4c9b297ba8f63ef94a
1 /*
2 Unix SMB/CIFS implementation.
3 SMB Transport encryption (sealing) code - server code.
4 Copyright (C) Jeremy Allison 2007.
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.
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.
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/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/auth/spnego.h"
24 #include "../libcli/auth/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "smb_crypt.h"
27 #include "../lib/util/asn1.h"
28 #include "auth.h"
29 #include "libsmb/libsmb.h"
30 #include "../lib/tsocket/tsocket.h"
32 /******************************************************************************
33 Server side encryption.
34 ******************************************************************************/
36 /******************************************************************************
37 Global server state.
38 ******************************************************************************/
40 struct smb_srv_trans_enc_ctx {
41 struct smb_trans_enc_state *es;
42 struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
45 /******************************************************************************
46 Return global enc context - this must change if we ever do multiple contexts.
47 ******************************************************************************/
49 static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
51 return ec->es->enc_ctx_num;
54 /******************************************************************************
55 Is this an incoming encrypted packet ?
56 ******************************************************************************/
58 bool is_encrypted_packet(struct smbd_server_connection *sconn,
59 const uint8_t *inbuf)
61 NTSTATUS status;
62 uint16_t enc_num;
64 /* Ignore non-session messages or non 0xFF'E' messages. */
65 if(CVAL(inbuf,0)
66 || (smb_len(inbuf) < 8)
67 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
68 return false;
71 status = get_enc_ctx_num(inbuf, &enc_num);
72 if (!NT_STATUS_IS_OK(status)) {
73 return false;
76 /* Encrypted messages are 0xFF'E'<ctx> */
77 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
78 return true;
80 return false;
83 /******************************************************************************
84 Create an auth_ntlmssp_state and ensure pointer copy is correct.
85 ******************************************************************************/
87 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
88 struct smb_srv_trans_enc_ctx *ec)
90 NTSTATUS status = auth_ntlmssp_prepare(remote_address,
91 &ec->auth_ntlmssp_state);
92 if (!NT_STATUS_IS_OK(status)) {
93 return nt_status_squash(status);
96 auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
98 status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
100 if (!NT_STATUS_IS_OK(status)) {
101 return nt_status_squash(status);
105 * We must remember to update the pointer copy for the common
106 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
108 ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
109 return status;
112 /******************************************************************************
113 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
114 ******************************************************************************/
116 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
119 * We must remember to update the pointer copy for the common
120 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
123 if (ec->auth_ntlmssp_state) {
124 TALLOC_FREE(ec->auth_ntlmssp_state);
125 /* The auth_ntlmssp_end killed this already. */
126 ec->es->s.auth_ntlmssp_state = NULL;
130 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
132 /******************************************************************************
133 Import a name.
134 ******************************************************************************/
136 static NTSTATUS get_srv_gss_creds(const char *service,
137 const char *name,
138 gss_cred_usage_t cred_type,
139 gss_cred_id_t *p_srv_cred)
141 OM_uint32 ret;
142 OM_uint32 min;
143 gss_name_t srv_name;
144 gss_buffer_desc input_name;
145 char *host_princ_s = NULL;
146 NTSTATUS status = NT_STATUS_OK;
148 gss_OID_desc nt_hostbased_service =
149 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
151 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
152 return NT_STATUS_NO_MEMORY;
155 input_name.value = host_princ_s;
156 input_name.length = strlen(host_princ_s) + 1;
158 ret = gss_import_name(&min,
159 &input_name,
160 &nt_hostbased_service,
161 &srv_name);
163 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
164 host_princ_s ));
166 if (ret != GSS_S_COMPLETE) {
167 SAFE_FREE(host_princ_s);
168 return map_nt_error_from_gss(ret, min);
172 * We're accessing the krb5.keytab file here.
173 * ensure we have permissions to do so.
175 become_root();
177 ret = gss_acquire_cred(&min,
178 srv_name,
179 GSS_C_INDEFINITE,
180 GSS_C_NULL_OID_SET,
181 cred_type,
182 p_srv_cred,
183 NULL,
184 NULL);
185 unbecome_root();
187 if (ret != GSS_S_COMPLETE) {
188 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
189 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
190 ads_errstr(adss)));
191 status = map_nt_error_from_gss(ret, min);
194 SAFE_FREE(host_princ_s);
195 gss_release_name(&min, &srv_name);
196 return status;
199 /******************************************************************************
200 Create a gss state.
201 Try and get the cifs/server@realm principal first, then fall back to
202 host/server@realm.
203 ******************************************************************************/
205 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
207 NTSTATUS status;
208 gss_cred_id_t srv_cred;
209 fstring fqdn;
211 name_to_fqdn(fqdn, lp_netbios_name());
212 strlower_m(fqdn);
214 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
215 if (!NT_STATUS_IS_OK(status)) {
216 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
217 if (!NT_STATUS_IS_OK(status)) {
218 return nt_status_squash(status);
222 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
223 if (!ec->es->s.gss_state) {
224 OM_uint32 min;
225 gss_release_cred(&min, &srv_cred);
226 return NT_STATUS_NO_MEMORY;
228 ZERO_STRUCTP(ec->es->s.gss_state);
229 ec->es->s.gss_state->creds = srv_cred;
231 /* No context yet. */
232 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
234 return NT_STATUS_OK;
236 #endif
238 /******************************************************************************
239 Shutdown a server encryption context.
240 ******************************************************************************/
242 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
244 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
246 if (!ec) {
247 return;
250 if (ec->es) {
251 switch (ec->es->smb_enc_type) {
252 case SMB_TRANS_ENC_NTLM:
253 destroy_auth_ntlmssp(ec);
254 break;
255 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
256 case SMB_TRANS_ENC_GSS:
257 break;
258 #endif
260 common_free_encryption_state(&ec->es);
263 SAFE_FREE(ec);
264 *pp_ec = NULL;
267 /******************************************************************************
268 Create a server encryption context.
269 ******************************************************************************/
271 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
272 enum smb_trans_enc_type smb_enc_type,
273 struct smb_srv_trans_enc_ctx **pp_ec)
275 struct smb_srv_trans_enc_ctx *ec;
277 *pp_ec = NULL;
279 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
280 if (!ec) {
281 return NT_STATUS_NO_MEMORY;
283 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
284 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
285 if (!ec->es) {
286 SAFE_FREE(ec);
287 return NT_STATUS_NO_MEMORY;
289 ZERO_STRUCTP(ec->es);
290 ec->es->smb_enc_type = smb_enc_type;
291 switch (smb_enc_type) {
292 case SMB_TRANS_ENC_NTLM:
294 NTSTATUS status = make_auth_ntlmssp(remote_address,
295 ec);
296 if (!NT_STATUS_IS_OK(status)) {
297 srv_free_encryption_context(&ec);
298 return status;
301 break;
303 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
304 case SMB_TRANS_ENC_GSS:
305 /* Acquire our credentials by calling gss_acquire_cred here. */
307 NTSTATUS status = make_auth_gss(ec);
308 if (!NT_STATUS_IS_OK(status)) {
309 srv_free_encryption_context(&ec);
310 return status;
313 break;
314 #endif
315 default:
316 srv_free_encryption_context(&ec);
317 return NT_STATUS_INVALID_PARAMETER;
319 *pp_ec = ec;
320 return NT_STATUS_OK;
323 /******************************************************************************
324 Free an encryption-allocated buffer.
325 ******************************************************************************/
327 void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
329 /* We know this is an smb buffer, and we
330 * didn't malloc, only copy, for a keepalive,
331 * so ignore non-session messages. */
333 if(CVAL(buf,0)) {
334 return;
337 if (srv_trans_enc_ctx) {
338 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
342 /******************************************************************************
343 Decrypt an incoming buffer.
344 ******************************************************************************/
346 NTSTATUS srv_decrypt_buffer(char *buf)
348 /* Ignore non-session messages. */
349 if(CVAL(buf,0)) {
350 return NT_STATUS_OK;
353 if (srv_trans_enc_ctx) {
354 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
357 return NT_STATUS_OK;
360 /******************************************************************************
361 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
362 ******************************************************************************/
364 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
366 *buf_out = buf;
368 /* Ignore non-session messages. */
369 if(CVAL(buf,0)) {
370 return NT_STATUS_OK;
373 if (srv_trans_enc_ctx) {
374 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
376 /* Not encrypting. */
377 return NT_STATUS_OK;
380 /******************************************************************************
381 Do the gss encryption negotiation. Parameters are in/out.
382 Until success we do everything on the partial enc ctx.
383 ******************************************************************************/
385 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
386 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
387 unsigned char **ppdata,
388 size_t *p_data_size,
389 DATA_BLOB secblob)
391 OM_uint32 ret;
392 OM_uint32 min;
393 OM_uint32 flags = 0;
394 gss_buffer_desc in_buf, out_buf;
395 struct smb_tran_enc_state_gss *gss_state;
396 DATA_BLOB auth_reply = data_blob_null;
397 DATA_BLOB response = data_blob_null;
398 NTSTATUS status;
400 if (!partial_srv_trans_enc_ctx) {
401 status = make_srv_encryption_context(remote_address,
402 SMB_TRANS_ENC_GSS,
403 &partial_srv_trans_enc_ctx);
404 if (!NT_STATUS_IS_OK(status)) {
405 return status;
409 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
411 in_buf.value = secblob.data;
412 in_buf.length = secblob.length;
414 out_buf.value = NULL;
415 out_buf.length = 0;
417 become_root();
419 ret = gss_accept_sec_context(&min,
420 &gss_state->gss_ctx,
421 gss_state->creds,
422 &in_buf,
423 GSS_C_NO_CHANNEL_BINDINGS,
424 NULL,
425 NULL, /* Ignore oids. */
426 &out_buf, /* To return. */
427 &flags,
428 NULL, /* Ingore time. */
429 NULL); /* Ignore delegated creds. */
430 unbecome_root();
432 status = gss_err_to_ntstatus(ret, min);
433 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
434 return status;
437 /* Ensure we've got sign+seal available. */
438 if (ret == GSS_S_COMPLETE) {
439 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
440 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
441 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
442 "for SMB sealing.\n"));
443 gss_release_buffer(&min, &out_buf);
444 return NT_STATUS_ACCESS_DENIED;
448 auth_reply = data_blob(out_buf.value, out_buf.length);
449 gss_release_buffer(&min, &out_buf);
451 /* Wrap in SPNEGO. */
452 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
453 data_blob_free(&auth_reply);
455 SAFE_FREE(*ppdata);
456 *ppdata = (unsigned char *)memdup(response.data, response.length);
457 if ((*ppdata) == NULL && response.length > 0) {
458 status = NT_STATUS_NO_MEMORY;
460 *p_data_size = response.length;
462 data_blob_free(&response);
464 return status;
466 #endif
468 /******************************************************************************
469 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
470 Until success we do everything on the partial enc ctx.
471 ******************************************************************************/
473 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
474 unsigned char **ppdata,
475 size_t *p_data_size,
476 DATA_BLOB secblob,
477 bool spnego_wrap)
479 NTSTATUS status;
480 DATA_BLOB chal = data_blob_null;
481 DATA_BLOB response = data_blob_null;
483 status = make_srv_encryption_context(remote_address,
484 SMB_TRANS_ENC_NTLM,
485 &partial_srv_trans_enc_ctx);
486 if (!NT_STATUS_IS_OK(status)) {
487 return status;
490 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
491 partial_srv_trans_enc_ctx->auth_ntlmssp_state,
492 secblob, &chal);
494 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
495 * for success ... */
497 if (spnego_wrap) {
498 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
499 data_blob_free(&chal);
500 } else {
501 /* Return the raw blob. */
502 response = chal;
505 SAFE_FREE(*ppdata);
506 *ppdata = (unsigned char *)memdup(response.data, response.length);
507 if ((*ppdata) == NULL && response.length > 0) {
508 status = NT_STATUS_NO_MEMORY;
510 *p_data_size = response.length;
511 data_blob_free(&response);
513 return status;
516 /******************************************************************************
517 Do the SPNEGO encryption negotiation. Parameters are in/out.
518 Based off code in smbd/sesssionsetup.c
519 Until success we do everything on the partial enc ctx.
520 ******************************************************************************/
522 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
523 unsigned char **ppdata,
524 size_t *p_data_size,
525 unsigned char **pparam,
526 size_t *p_param_size)
528 NTSTATUS status;
529 DATA_BLOB blob = data_blob_null;
530 DATA_BLOB secblob = data_blob_null;
531 char *kerb_mech = NULL;
533 blob = data_blob_const(*ppdata, *p_data_size);
535 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
536 if (!NT_STATUS_IS_OK(status)) {
537 return nt_status_squash(status);
540 /* We should have no partial context at this point. */
542 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
544 if (kerb_mech) {
545 TALLOC_FREE(kerb_mech);
547 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
548 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
549 ppdata,
550 p_data_size,
551 secblob);
552 #else
553 /* Currently we don't SPNEGO negotiate
554 * back to NTLMSSP as we do in sessionsetupX. We should... */
555 return NT_STATUS_LOGON_FAILURE;
556 #endif
557 } else {
558 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
559 ppdata,
560 p_data_size,
561 secblob,
562 true);
565 data_blob_free(&secblob);
567 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
568 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
569 return nt_status_squash(status);
572 if (NT_STATUS_IS_OK(status)) {
573 /* Return the context we're using for this encryption state. */
574 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
575 return NT_STATUS_NO_MEMORY;
577 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
578 *p_param_size = 2;
581 return status;
584 /******************************************************************************
585 Complete a SPNEGO encryption negotiation. Parameters are in/out.
586 We only get this for a NTLM auth second stage.
587 ******************************************************************************/
589 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
590 unsigned char **ppdata,
591 size_t *p_data_size,
592 unsigned char **pparam,
593 size_t *p_param_size)
595 NTSTATUS status;
596 DATA_BLOB blob = data_blob_null;
597 DATA_BLOB auth = data_blob_null;
598 DATA_BLOB auth_reply = data_blob_null;
599 DATA_BLOB response = data_blob_null;
600 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
602 /* We must have a partial context here. */
604 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
605 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
606 return NT_STATUS_INVALID_PARAMETER;
609 blob = data_blob_const(*ppdata, *p_data_size);
610 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
611 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
612 return NT_STATUS_INVALID_PARAMETER;
615 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
616 data_blob_free(&auth);
618 /* From RFC4178.
620 * supportedMech
622 * This field SHALL only be present in the first reply from the
623 * target.
624 * So set mechOID to NULL here.
627 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
628 data_blob_free(&auth_reply);
630 if (NT_STATUS_IS_OK(status)) {
631 /* Return the context we're using for this encryption state. */
632 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
633 return NT_STATUS_NO_MEMORY;
635 SSVAL(*pparam,0,ec->es->enc_ctx_num);
636 *p_param_size = 2;
639 SAFE_FREE(*ppdata);
640 *ppdata = (unsigned char *)memdup(response.data, response.length);
641 if ((*ppdata) == NULL && response.length > 0)
642 return NT_STATUS_NO_MEMORY;
643 *p_data_size = response.length;
644 data_blob_free(&response);
645 return status;
648 /******************************************************************************
649 Raw NTLM encryption negotiation. Parameters are in/out.
650 This function does both steps.
651 ******************************************************************************/
653 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
654 unsigned char **ppdata,
655 size_t *p_data_size,
656 unsigned char **pparam,
657 size_t *p_param_size)
659 NTSTATUS status;
660 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
661 DATA_BLOB response = data_blob_null;
662 struct smb_srv_trans_enc_ctx *ec;
664 if (!partial_srv_trans_enc_ctx) {
665 /* This is the initial step. */
666 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
667 ppdata,
668 p_data_size,
669 blob,
670 false);
671 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
672 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
673 return nt_status_squash(status);
675 return status;
678 ec = partial_srv_trans_enc_ctx;
679 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
680 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
681 return NT_STATUS_INVALID_PARAMETER;
684 /* Second step. */
685 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
686 talloc_tos(),
687 blob, &response);
689 if (NT_STATUS_IS_OK(status)) {
690 /* Return the context we're using for this encryption state. */
691 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
692 return NT_STATUS_NO_MEMORY;
694 SSVAL(*pparam,0,ec->es->enc_ctx_num);
695 *p_param_size = 2;
698 /* Return the raw blob. */
699 SAFE_FREE(*ppdata);
700 *ppdata = (unsigned char *)memdup(response.data, response.length);
701 if ((*ppdata) == NULL && response.length > 0)
702 return NT_STATUS_NO_MEMORY;
703 *p_data_size = response.length;
704 data_blob_free(&response);
705 return status;
708 /******************************************************************************
709 Do the SPNEGO encryption negotiation. Parameters are in/out.
710 ******************************************************************************/
712 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
713 unsigned char **ppdata,
714 size_t *p_data_size,
715 unsigned char **pparam,
716 size_t *p_param_size)
718 unsigned char *pdata = *ppdata;
720 SAFE_FREE(*pparam);
721 *p_param_size = 0;
723 if (*p_data_size < 1) {
724 return NT_STATUS_INVALID_PARAMETER;
727 if (pdata[0] == ASN1_APPLICATION(0)) {
728 /* its a negTokenTarg packet */
729 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
732 if (pdata[0] == ASN1_CONTEXT(1)) {
733 /* It's an auth packet */
734 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
737 /* Maybe it's a raw unwrapped auth ? */
738 if (*p_data_size < 7) {
739 return NT_STATUS_INVALID_PARAMETER;
742 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
743 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
746 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
748 return NT_STATUS_LOGON_FAILURE;
751 /******************************************************************************
752 Negotiation was successful - turn on server-side encryption.
753 ******************************************************************************/
755 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
757 if (!ec || !ec->es) {
758 return NT_STATUS_LOGON_FAILURE;
761 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
762 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
763 return NT_STATUS_INVALID_PARAMETER;
766 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
767 return NT_STATUS_INVALID_PARAMETER;
770 /* Todo - check gssapi case. */
772 return NT_STATUS_OK;
775 /******************************************************************************
776 Negotiation was successful - turn on server-side encryption.
777 ******************************************************************************/
779 NTSTATUS srv_encryption_start(connection_struct *conn)
781 NTSTATUS status;
783 /* Check that we are really doing sign+seal. */
784 status = check_enc_good(partial_srv_trans_enc_ctx);
785 if (!NT_STATUS_IS_OK(status)) {
786 return status;
788 /* Throw away the context we're using currently (if any). */
789 srv_free_encryption_context(&srv_trans_enc_ctx);
791 /* Steal the partial pointer. Deliberate shallow copy. */
792 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
793 srv_trans_enc_ctx->es->enc_on = true;
795 partial_srv_trans_enc_ctx = NULL;
797 DEBUG(1,("srv_encryption_start: context negotiated\n"));
798 return NT_STATUS_OK;
801 /******************************************************************************
802 Shutdown all server contexts.
803 ******************************************************************************/
805 void server_encryption_shutdown(void)
807 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
808 srv_free_encryption_context(&srv_trans_enc_ctx);