s3:smbd: convert connections.c to use only dbrwap wrapper functions
[Samba/gbeck.git] / source3 / smbd / seal.c
blobc0aaa5ab7d73e96c3bfdc71b1517c653a3cf8f2b
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(struct smbd_server_connection *sconn, 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(struct smbd_server_connection *sconn, char *buf,
365 char **buf_out)
367 *buf_out = buf;
369 /* Ignore non-session messages. */
370 if(CVAL(buf,0)) {
371 return NT_STATUS_OK;
374 if (srv_trans_enc_ctx) {
375 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
377 /* Not encrypting. */
378 return NT_STATUS_OK;
381 /******************************************************************************
382 Do the gss encryption negotiation. Parameters are in/out.
383 Until success we do everything on the partial enc ctx.
384 ******************************************************************************/
386 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
387 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
388 unsigned char **ppdata,
389 size_t *p_data_size,
390 DATA_BLOB secblob)
392 OM_uint32 ret;
393 OM_uint32 min;
394 OM_uint32 flags = 0;
395 gss_buffer_desc in_buf, out_buf;
396 struct smb_tran_enc_state_gss *gss_state;
397 DATA_BLOB auth_reply = data_blob_null;
398 DATA_BLOB response = data_blob_null;
399 NTSTATUS status;
401 if (!partial_srv_trans_enc_ctx) {
402 status = make_srv_encryption_context(remote_address,
403 SMB_TRANS_ENC_GSS,
404 &partial_srv_trans_enc_ctx);
405 if (!NT_STATUS_IS_OK(status)) {
406 return status;
410 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
412 in_buf.value = secblob.data;
413 in_buf.length = secblob.length;
415 out_buf.value = NULL;
416 out_buf.length = 0;
418 become_root();
420 ret = gss_accept_sec_context(&min,
421 &gss_state->gss_ctx,
422 gss_state->creds,
423 &in_buf,
424 GSS_C_NO_CHANNEL_BINDINGS,
425 NULL,
426 NULL, /* Ignore oids. */
427 &out_buf, /* To return. */
428 &flags,
429 NULL, /* Ingore time. */
430 NULL); /* Ignore delegated creds. */
431 unbecome_root();
433 status = gss_err_to_ntstatus(ret, min);
434 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
435 return status;
438 /* Ensure we've got sign+seal available. */
439 if (ret == GSS_S_COMPLETE) {
440 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
441 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
442 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
443 "for SMB sealing.\n"));
444 gss_release_buffer(&min, &out_buf);
445 return NT_STATUS_ACCESS_DENIED;
449 auth_reply = data_blob(out_buf.value, out_buf.length);
450 gss_release_buffer(&min, &out_buf);
452 /* Wrap in SPNEGO. */
453 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
454 data_blob_free(&auth_reply);
456 SAFE_FREE(*ppdata);
457 *ppdata = (unsigned char *)memdup(response.data, response.length);
458 if ((*ppdata) == NULL && response.length > 0) {
459 status = NT_STATUS_NO_MEMORY;
461 *p_data_size = response.length;
463 data_blob_free(&response);
465 return status;
467 #endif
469 /******************************************************************************
470 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
471 Until success we do everything on the partial enc ctx.
472 ******************************************************************************/
474 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
475 unsigned char **ppdata,
476 size_t *p_data_size,
477 DATA_BLOB secblob,
478 bool spnego_wrap)
480 NTSTATUS status;
481 DATA_BLOB chal = data_blob_null;
482 DATA_BLOB response = data_blob_null;
484 status = make_srv_encryption_context(remote_address,
485 SMB_TRANS_ENC_NTLM,
486 &partial_srv_trans_enc_ctx);
487 if (!NT_STATUS_IS_OK(status)) {
488 return status;
491 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
492 partial_srv_trans_enc_ctx->auth_ntlmssp_state,
493 secblob, &chal);
495 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
496 * for success ... */
498 if (spnego_wrap) {
499 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
500 data_blob_free(&chal);
501 } else {
502 /* Return the raw blob. */
503 response = chal;
506 SAFE_FREE(*ppdata);
507 *ppdata = (unsigned char *)memdup(response.data, response.length);
508 if ((*ppdata) == NULL && response.length > 0) {
509 status = NT_STATUS_NO_MEMORY;
511 *p_data_size = response.length;
512 data_blob_free(&response);
514 return status;
517 /******************************************************************************
518 Do the SPNEGO encryption negotiation. Parameters are in/out.
519 Based off code in smbd/sesssionsetup.c
520 Until success we do everything on the partial enc ctx.
521 ******************************************************************************/
523 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
524 unsigned char **ppdata,
525 size_t *p_data_size,
526 unsigned char **pparam,
527 size_t *p_param_size)
529 NTSTATUS status;
530 DATA_BLOB blob = data_blob_null;
531 DATA_BLOB secblob = data_blob_null;
532 char *kerb_mech = NULL;
534 blob = data_blob_const(*ppdata, *p_data_size);
536 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
537 if (!NT_STATUS_IS_OK(status)) {
538 return nt_status_squash(status);
541 /* We should have no partial context at this point. */
543 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
545 if (kerb_mech) {
546 TALLOC_FREE(kerb_mech);
548 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
549 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
550 ppdata,
551 p_data_size,
552 secblob);
553 #else
554 /* Currently we don't SPNEGO negotiate
555 * back to NTLMSSP as we do in sessionsetupX. We should... */
556 return NT_STATUS_LOGON_FAILURE;
557 #endif
558 } else {
559 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
560 ppdata,
561 p_data_size,
562 secblob,
563 true);
566 data_blob_free(&secblob);
568 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
569 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
570 return nt_status_squash(status);
573 if (NT_STATUS_IS_OK(status)) {
574 /* Return the context we're using for this encryption state. */
575 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
576 return NT_STATUS_NO_MEMORY;
578 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
579 *p_param_size = 2;
582 return status;
585 /******************************************************************************
586 Complete a SPNEGO encryption negotiation. Parameters are in/out.
587 We only get this for a NTLM auth second stage.
588 ******************************************************************************/
590 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
591 unsigned char **ppdata,
592 size_t *p_data_size,
593 unsigned char **pparam,
594 size_t *p_param_size)
596 NTSTATUS status;
597 DATA_BLOB blob = data_blob_null;
598 DATA_BLOB auth = data_blob_null;
599 DATA_BLOB auth_reply = data_blob_null;
600 DATA_BLOB response = data_blob_null;
601 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
603 /* We must have a partial context here. */
605 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
606 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
607 return NT_STATUS_INVALID_PARAMETER;
610 blob = data_blob_const(*ppdata, *p_data_size);
611 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
612 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
613 return NT_STATUS_INVALID_PARAMETER;
616 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
617 data_blob_free(&auth);
619 /* From RFC4178.
621 * supportedMech
623 * This field SHALL only be present in the first reply from the
624 * target.
625 * So set mechOID to NULL here.
628 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
629 data_blob_free(&auth_reply);
631 if (NT_STATUS_IS_OK(status)) {
632 /* Return the context we're using for this encryption state. */
633 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
634 return NT_STATUS_NO_MEMORY;
636 SSVAL(*pparam,0,ec->es->enc_ctx_num);
637 *p_param_size = 2;
640 SAFE_FREE(*ppdata);
641 *ppdata = (unsigned char *)memdup(response.data, response.length);
642 if ((*ppdata) == NULL && response.length > 0)
643 return NT_STATUS_NO_MEMORY;
644 *p_data_size = response.length;
645 data_blob_free(&response);
646 return status;
649 /******************************************************************************
650 Raw NTLM encryption negotiation. Parameters are in/out.
651 This function does both steps.
652 ******************************************************************************/
654 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
655 unsigned char **ppdata,
656 size_t *p_data_size,
657 unsigned char **pparam,
658 size_t *p_param_size)
660 NTSTATUS status;
661 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
662 DATA_BLOB response = data_blob_null;
663 struct smb_srv_trans_enc_ctx *ec;
665 if (!partial_srv_trans_enc_ctx) {
666 /* This is the initial step. */
667 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
668 ppdata,
669 p_data_size,
670 blob,
671 false);
672 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
673 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
674 return nt_status_squash(status);
676 return status;
679 ec = partial_srv_trans_enc_ctx;
680 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
681 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
682 return NT_STATUS_INVALID_PARAMETER;
685 /* Second step. */
686 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
687 talloc_tos(),
688 blob, &response);
690 if (NT_STATUS_IS_OK(status)) {
691 /* Return the context we're using for this encryption state. */
692 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
693 return NT_STATUS_NO_MEMORY;
695 SSVAL(*pparam,0,ec->es->enc_ctx_num);
696 *p_param_size = 2;
699 /* Return the raw blob. */
700 SAFE_FREE(*ppdata);
701 *ppdata = (unsigned char *)memdup(response.data, response.length);
702 if ((*ppdata) == NULL && response.length > 0)
703 return NT_STATUS_NO_MEMORY;
704 *p_data_size = response.length;
705 data_blob_free(&response);
706 return status;
709 /******************************************************************************
710 Do the SPNEGO encryption negotiation. Parameters are in/out.
711 ******************************************************************************/
713 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
714 unsigned char **ppdata,
715 size_t *p_data_size,
716 unsigned char **pparam,
717 size_t *p_param_size)
719 unsigned char *pdata = *ppdata;
721 SAFE_FREE(*pparam);
722 *p_param_size = 0;
724 if (*p_data_size < 1) {
725 return NT_STATUS_INVALID_PARAMETER;
728 if (pdata[0] == ASN1_APPLICATION(0)) {
729 /* its a negTokenTarg packet */
730 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
733 if (pdata[0] == ASN1_CONTEXT(1)) {
734 /* It's an auth packet */
735 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
738 /* Maybe it's a raw unwrapped auth ? */
739 if (*p_data_size < 7) {
740 return NT_STATUS_INVALID_PARAMETER;
743 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
744 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
747 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
749 return NT_STATUS_LOGON_FAILURE;
752 /******************************************************************************
753 Negotiation was successful - turn on server-side encryption.
754 ******************************************************************************/
756 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
758 if (!ec || !ec->es) {
759 return NT_STATUS_LOGON_FAILURE;
762 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
763 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
764 return NT_STATUS_INVALID_PARAMETER;
767 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
768 return NT_STATUS_INVALID_PARAMETER;
771 /* Todo - check gssapi case. */
773 return NT_STATUS_OK;
776 /******************************************************************************
777 Negotiation was successful - turn on server-side encryption.
778 ******************************************************************************/
780 NTSTATUS srv_encryption_start(connection_struct *conn)
782 NTSTATUS status;
784 /* Check that we are really doing sign+seal. */
785 status = check_enc_good(partial_srv_trans_enc_ctx);
786 if (!NT_STATUS_IS_OK(status)) {
787 return status;
789 /* Throw away the context we're using currently (if any). */
790 srv_free_encryption_context(&srv_trans_enc_ctx);
792 /* Steal the partial pointer. Deliberate shallow copy. */
793 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
794 srv_trans_enc_ctx->es->enc_on = true;
796 partial_srv_trans_enc_ctx = NULL;
798 DEBUG(1,("srv_encryption_start: context negotiated\n"));
799 return NT_STATUS_OK;
802 /******************************************************************************
803 Shutdown all server contexts.
804 ******************************************************************************/
806 void server_encryption_shutdown(struct smbd_server_connection *sconn)
808 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
809 srv_free_encryption_context(&srv_trans_enc_ctx);