s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / smbd / seal.c
blobf1c0f9cf3bbb749c183b45b2862a595c7e05ca9e
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 uint16_t srv_enc_ctx(void)
51 return srv_trans_enc_ctx->es->enc_ctx_num;
54 /******************************************************************************
55 Is this an incoming encrypted packet ?
56 ******************************************************************************/
58 bool is_encrypted_packet(const uint8_t *inbuf)
60 NTSTATUS status;
61 uint16_t enc_num;
63 /* Ignore non-session messages or non 0xFF'E' messages. */
64 if(CVAL(inbuf,0)
65 || (smb_len(inbuf) < 8)
66 || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
67 return false;
70 status = get_enc_ctx_num(inbuf, &enc_num);
71 if (!NT_STATUS_IS_OK(status)) {
72 return false;
75 /* Encrypted messages are 0xFF'E'<ctx> */
76 if (srv_trans_enc_ctx && enc_num == srv_enc_ctx()) {
77 return true;
79 return false;
82 /******************************************************************************
83 Create an auth_ntlmssp_state and ensure pointer copy is correct.
84 ******************************************************************************/
86 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
87 struct smb_srv_trans_enc_ctx *ec)
89 NTSTATUS status = auth_ntlmssp_start(remote_address,
90 &ec->auth_ntlmssp_state);
91 if (!NT_STATUS_IS_OK(status)) {
92 return nt_status_squash(status);
96 * We must remember to update the pointer copy for the common
97 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
99 ec->es->s.ntlmssp_state = auth_ntlmssp_get_ntlmssp_state(ec->auth_ntlmssp_state);
100 return status;
103 /******************************************************************************
104 Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
105 ******************************************************************************/
107 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
110 * We must remember to update the pointer copy for the common
111 * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
114 if (ec->auth_ntlmssp_state) {
115 TALLOC_FREE(ec->auth_ntlmssp_state);
116 /* The auth_ntlmssp_end killed this already. */
117 ec->es->s.ntlmssp_state = NULL;
121 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
123 /******************************************************************************
124 Import a name.
125 ******************************************************************************/
127 static NTSTATUS get_srv_gss_creds(const char *service,
128 const char *name,
129 gss_cred_usage_t cred_type,
130 gss_cred_id_t *p_srv_cred)
132 OM_uint32 ret;
133 OM_uint32 min;
134 gss_name_t srv_name;
135 gss_buffer_desc input_name;
136 char *host_princ_s = NULL;
137 NTSTATUS status = NT_STATUS_OK;
139 gss_OID_desc nt_hostbased_service =
140 {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
142 if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
143 return NT_STATUS_NO_MEMORY;
146 input_name.value = host_princ_s;
147 input_name.length = strlen(host_princ_s) + 1;
149 ret = gss_import_name(&min,
150 &input_name,
151 &nt_hostbased_service,
152 &srv_name);
154 DEBUG(10,("get_srv_gss_creds: imported name %s\n",
155 host_princ_s ));
157 if (ret != GSS_S_COMPLETE) {
158 SAFE_FREE(host_princ_s);
159 return map_nt_error_from_gss(ret, min);
163 * We're accessing the krb5.keytab file here.
164 * ensure we have permissions to do so.
166 become_root();
168 ret = gss_acquire_cred(&min,
169 srv_name,
170 GSS_C_INDEFINITE,
171 GSS_C_NULL_OID_SET,
172 cred_type,
173 p_srv_cred,
174 NULL,
175 NULL);
176 unbecome_root();
178 if (ret != GSS_S_COMPLETE) {
179 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
180 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
181 ads_errstr(adss)));
182 status = map_nt_error_from_gss(ret, min);
185 SAFE_FREE(host_princ_s);
186 gss_release_name(&min, &srv_name);
187 return status;
190 /******************************************************************************
191 Create a gss state.
192 Try and get the cifs/server@realm principal first, then fall back to
193 host/server@realm.
194 ******************************************************************************/
196 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
198 NTSTATUS status;
199 gss_cred_id_t srv_cred;
200 fstring fqdn;
202 name_to_fqdn(fqdn, lp_netbios_name());
203 strlower_m(fqdn);
205 status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
206 if (!NT_STATUS_IS_OK(status)) {
207 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
208 if (!NT_STATUS_IS_OK(status)) {
209 return nt_status_squash(status);
213 ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
214 if (!ec->es->s.gss_state) {
215 OM_uint32 min;
216 gss_release_cred(&min, &srv_cred);
217 return NT_STATUS_NO_MEMORY;
219 ZERO_STRUCTP(ec->es->s.gss_state);
220 ec->es->s.gss_state->creds = srv_cred;
222 /* No context yet. */
223 ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
225 return NT_STATUS_OK;
227 #endif
229 /******************************************************************************
230 Shutdown a server encryption context.
231 ******************************************************************************/
233 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
235 struct smb_srv_trans_enc_ctx *ec = *pp_ec;
237 if (!ec) {
238 return;
241 if (ec->es) {
242 switch (ec->es->smb_enc_type) {
243 case SMB_TRANS_ENC_NTLM:
244 destroy_auth_ntlmssp(ec);
245 break;
246 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
247 case SMB_TRANS_ENC_GSS:
248 break;
249 #endif
251 common_free_encryption_state(&ec->es);
254 SAFE_FREE(ec);
255 *pp_ec = NULL;
258 /******************************************************************************
259 Create a server encryption context.
260 ******************************************************************************/
262 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
263 enum smb_trans_enc_type smb_enc_type,
264 struct smb_srv_trans_enc_ctx **pp_ec)
266 struct smb_srv_trans_enc_ctx *ec;
268 *pp_ec = NULL;
270 ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
271 if (!ec) {
272 return NT_STATUS_NO_MEMORY;
274 ZERO_STRUCTP(partial_srv_trans_enc_ctx);
275 ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
276 if (!ec->es) {
277 SAFE_FREE(ec);
278 return NT_STATUS_NO_MEMORY;
280 ZERO_STRUCTP(ec->es);
281 ec->es->smb_enc_type = smb_enc_type;
282 switch (smb_enc_type) {
283 case SMB_TRANS_ENC_NTLM:
285 NTSTATUS status = make_auth_ntlmssp(remote_address,
286 ec);
287 if (!NT_STATUS_IS_OK(status)) {
288 srv_free_encryption_context(&ec);
289 return status;
292 break;
294 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
295 case SMB_TRANS_ENC_GSS:
296 /* Acquire our credentials by calling gss_acquire_cred here. */
298 NTSTATUS status = make_auth_gss(ec);
299 if (!NT_STATUS_IS_OK(status)) {
300 srv_free_encryption_context(&ec);
301 return status;
304 break;
305 #endif
306 default:
307 srv_free_encryption_context(&ec);
308 return NT_STATUS_INVALID_PARAMETER;
310 *pp_ec = ec;
311 return NT_STATUS_OK;
314 /******************************************************************************
315 Free an encryption-allocated buffer.
316 ******************************************************************************/
318 void srv_free_enc_buffer(char *buf)
320 /* We know this is an smb buffer, and we
321 * didn't malloc, only copy, for a keepalive,
322 * so ignore non-session messages. */
324 if(CVAL(buf,0)) {
325 return;
328 if (srv_trans_enc_ctx) {
329 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
333 /******************************************************************************
334 Decrypt an incoming buffer.
335 ******************************************************************************/
337 NTSTATUS srv_decrypt_buffer(char *buf)
339 /* Ignore non-session messages. */
340 if(CVAL(buf,0)) {
341 return NT_STATUS_OK;
344 if (srv_trans_enc_ctx) {
345 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
348 return NT_STATUS_OK;
351 /******************************************************************************
352 Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
353 ******************************************************************************/
355 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
357 *buf_out = buf;
359 /* Ignore non-session messages. */
360 if(CVAL(buf,0)) {
361 return NT_STATUS_OK;
364 if (srv_trans_enc_ctx) {
365 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
367 /* Not encrypting. */
368 return NT_STATUS_OK;
371 /******************************************************************************
372 Do the gss encryption negotiation. Parameters are in/out.
373 Until success we do everything on the partial enc ctx.
374 ******************************************************************************/
376 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
377 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
378 unsigned char **ppdata,
379 size_t *p_data_size,
380 DATA_BLOB secblob)
382 OM_uint32 ret;
383 OM_uint32 min;
384 OM_uint32 flags = 0;
385 gss_buffer_desc in_buf, out_buf;
386 struct smb_tran_enc_state_gss *gss_state;
387 DATA_BLOB auth_reply = data_blob_null;
388 DATA_BLOB response = data_blob_null;
389 NTSTATUS status;
391 if (!partial_srv_trans_enc_ctx) {
392 status = make_srv_encryption_context(remote_address,
393 SMB_TRANS_ENC_GSS,
394 &partial_srv_trans_enc_ctx);
395 if (!NT_STATUS_IS_OK(status)) {
396 return status;
400 gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
402 in_buf.value = secblob.data;
403 in_buf.length = secblob.length;
405 out_buf.value = NULL;
406 out_buf.length = 0;
408 become_root();
410 ret = gss_accept_sec_context(&min,
411 &gss_state->gss_ctx,
412 gss_state->creds,
413 &in_buf,
414 GSS_C_NO_CHANNEL_BINDINGS,
415 NULL,
416 NULL, /* Ignore oids. */
417 &out_buf, /* To return. */
418 &flags,
419 NULL, /* Ingore time. */
420 NULL); /* Ignore delegated creds. */
421 unbecome_root();
423 status = gss_err_to_ntstatus(ret, min);
424 if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
425 return status;
428 /* Ensure we've got sign+seal available. */
429 if (ret == GSS_S_COMPLETE) {
430 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
431 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
432 DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
433 "for SMB sealing.\n"));
434 gss_release_buffer(&min, &out_buf);
435 return NT_STATUS_ACCESS_DENIED;
439 auth_reply = data_blob(out_buf.value, out_buf.length);
440 gss_release_buffer(&min, &out_buf);
442 /* Wrap in SPNEGO. */
443 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
444 data_blob_free(&auth_reply);
446 SAFE_FREE(*ppdata);
447 *ppdata = (unsigned char *)memdup(response.data, response.length);
448 if ((*ppdata) == NULL && response.length > 0) {
449 status = NT_STATUS_NO_MEMORY;
451 *p_data_size = response.length;
453 data_blob_free(&response);
455 return status;
457 #endif
459 /******************************************************************************
460 Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
461 Until success we do everything on the partial enc ctx.
462 ******************************************************************************/
464 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
465 unsigned char **ppdata,
466 size_t *p_data_size,
467 DATA_BLOB secblob,
468 bool spnego_wrap)
470 NTSTATUS status;
471 DATA_BLOB chal = data_blob_null;
472 DATA_BLOB response = data_blob_null;
474 status = make_srv_encryption_context(remote_address,
475 SMB_TRANS_ENC_NTLM,
476 &partial_srv_trans_enc_ctx);
477 if (!NT_STATUS_IS_OK(status)) {
478 return status;
481 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
483 /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
484 * for success ... */
486 if (spnego_wrap) {
487 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
488 data_blob_free(&chal);
489 } else {
490 /* Return the raw blob. */
491 response = chal;
494 SAFE_FREE(*ppdata);
495 *ppdata = (unsigned char *)memdup(response.data, response.length);
496 if ((*ppdata) == NULL && response.length > 0) {
497 status = NT_STATUS_NO_MEMORY;
499 *p_data_size = response.length;
500 data_blob_free(&response);
502 return status;
505 /******************************************************************************
506 Do the SPNEGO encryption negotiation. Parameters are in/out.
507 Based off code in smbd/sesssionsetup.c
508 Until success we do everything on the partial enc ctx.
509 ******************************************************************************/
511 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
512 unsigned char **ppdata,
513 size_t *p_data_size,
514 unsigned char **pparam,
515 size_t *p_param_size)
517 NTSTATUS status;
518 DATA_BLOB blob = data_blob_null;
519 DATA_BLOB secblob = data_blob_null;
520 char *kerb_mech = NULL;
522 blob = data_blob_const(*ppdata, *p_data_size);
524 status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
525 if (!NT_STATUS_IS_OK(status)) {
526 return nt_status_squash(status);
529 /* We should have no partial context at this point. */
531 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
533 if (kerb_mech) {
534 TALLOC_FREE(kerb_mech);
536 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
537 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
538 ppdata,
539 p_data_size,
540 secblob);
541 #else
542 /* Currently we don't SPNEGO negotiate
543 * back to NTLMSSP as we do in sessionsetupX. We should... */
544 return NT_STATUS_LOGON_FAILURE;
545 #endif
546 } else {
547 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
548 ppdata,
549 p_data_size,
550 secblob,
551 true);
554 data_blob_free(&secblob);
556 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
557 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
558 return nt_status_squash(status);
561 if (NT_STATUS_IS_OK(status)) {
562 /* Return the context we're using for this encryption state. */
563 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
564 return NT_STATUS_NO_MEMORY;
566 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
567 *p_param_size = 2;
570 return status;
573 /******************************************************************************
574 Complete a SPNEGO encryption negotiation. Parameters are in/out.
575 We only get this for a NTLM auth second stage.
576 ******************************************************************************/
578 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
579 unsigned char **ppdata,
580 size_t *p_data_size,
581 unsigned char **pparam,
582 size_t *p_param_size)
584 NTSTATUS status;
585 DATA_BLOB blob = data_blob_null;
586 DATA_BLOB auth = data_blob_null;
587 DATA_BLOB auth_reply = data_blob_null;
588 DATA_BLOB response = data_blob_null;
589 struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
591 /* We must have a partial context here. */
593 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
594 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
595 return NT_STATUS_INVALID_PARAMETER;
598 blob = data_blob_const(*ppdata, *p_data_size);
599 if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
600 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
601 return NT_STATUS_INVALID_PARAMETER;
604 status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
605 data_blob_free(&auth);
607 /* From RFC4178.
609 * supportedMech
611 * This field SHALL only be present in the first reply from the
612 * target.
613 * So set mechOID to NULL here.
616 response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
617 data_blob_free(&auth_reply);
619 if (NT_STATUS_IS_OK(status)) {
620 /* Return the context we're using for this encryption state. */
621 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
622 return NT_STATUS_NO_MEMORY;
624 SSVAL(*pparam,0,ec->es->enc_ctx_num);
625 *p_param_size = 2;
628 SAFE_FREE(*ppdata);
629 *ppdata = (unsigned char *)memdup(response.data, response.length);
630 if ((*ppdata) == NULL && response.length > 0)
631 return NT_STATUS_NO_MEMORY;
632 *p_data_size = response.length;
633 data_blob_free(&response);
634 return status;
637 /******************************************************************************
638 Raw NTLM encryption negotiation. Parameters are in/out.
639 This function does both steps.
640 ******************************************************************************/
642 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
643 unsigned char **ppdata,
644 size_t *p_data_size,
645 unsigned char **pparam,
646 size_t *p_param_size)
648 NTSTATUS status;
649 DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
650 DATA_BLOB response = data_blob_null;
651 struct smb_srv_trans_enc_ctx *ec;
653 if (!partial_srv_trans_enc_ctx) {
654 /* This is the initial step. */
655 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
656 ppdata,
657 p_data_size,
658 blob,
659 false);
660 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
661 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
662 return nt_status_squash(status);
664 return status;
667 ec = partial_srv_trans_enc_ctx;
668 if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
669 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
670 return NT_STATUS_INVALID_PARAMETER;
673 /* Second step. */
674 status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
676 if (NT_STATUS_IS_OK(status)) {
677 /* Return the context we're using for this encryption state. */
678 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
679 return NT_STATUS_NO_MEMORY;
681 SSVAL(*pparam,0,ec->es->enc_ctx_num);
682 *p_param_size = 2;
685 /* Return the raw blob. */
686 SAFE_FREE(*ppdata);
687 *ppdata = (unsigned char *)memdup(response.data, response.length);
688 if ((*ppdata) == NULL && response.length > 0)
689 return NT_STATUS_NO_MEMORY;
690 *p_data_size = response.length;
691 data_blob_free(&response);
692 return status;
695 /******************************************************************************
696 Do the SPNEGO encryption negotiation. Parameters are in/out.
697 ******************************************************************************/
699 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
700 unsigned char **ppdata,
701 size_t *p_data_size,
702 unsigned char **pparam,
703 size_t *p_param_size)
705 unsigned char *pdata = *ppdata;
707 SAFE_FREE(*pparam);
708 *p_param_size = 0;
710 if (*p_data_size < 1) {
711 return NT_STATUS_INVALID_PARAMETER;
714 if (pdata[0] == ASN1_APPLICATION(0)) {
715 /* its a negTokenTarg packet */
716 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
719 if (pdata[0] == ASN1_CONTEXT(1)) {
720 /* It's an auth packet */
721 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
724 /* Maybe it's a raw unwrapped auth ? */
725 if (*p_data_size < 7) {
726 return NT_STATUS_INVALID_PARAMETER;
729 if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
730 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
733 DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
735 return NT_STATUS_LOGON_FAILURE;
738 /******************************************************************************
739 Negotiation was successful - turn on server-side encryption.
740 ******************************************************************************/
742 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
744 if (!ec || !ec->es) {
745 return NT_STATUS_LOGON_FAILURE;
748 if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
749 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
750 return NT_STATUS_INVALID_PARAMETER;
753 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
754 return NT_STATUS_INVALID_PARAMETER;
757 /* Todo - check gssapi case. */
759 return NT_STATUS_OK;
762 /******************************************************************************
763 Negotiation was successful - turn on server-side encryption.
764 ******************************************************************************/
766 NTSTATUS srv_encryption_start(connection_struct *conn)
768 NTSTATUS status;
770 /* Check that we are really doing sign+seal. */
771 status = check_enc_good(partial_srv_trans_enc_ctx);
772 if (!NT_STATUS_IS_OK(status)) {
773 return status;
775 /* Throw away the context we're using currently (if any). */
776 srv_free_encryption_context(&srv_trans_enc_ctx);
778 /* Steal the partial pointer. Deliberate shallow copy. */
779 srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
780 srv_trans_enc_ctx->es->enc_on = true;
782 partial_srv_trans_enc_ctx = NULL;
784 DEBUG(1,("srv_encryption_start: context negotiated\n"));
785 return NT_STATUS_OK;
788 /******************************************************************************
789 Shutdown all server contexts.
790 ******************************************************************************/
792 void server_encryption_shutdown(void)
794 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
795 srv_free_encryption_context(&srv_trans_enc_ctx);