r22352: Wow - working gss SMB sealing !
[gd/samba/.git] / source / smbd / seal.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB Transport encryption (sealing) code - server code.
4    Copyright (C) Jeremy Allison 2007.
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /******************************************************************************
24  Server side encryption.
25 ******************************************************************************/
26
27 /******************************************************************************
28  Global server state.
29 ******************************************************************************/
30
31 struct smb_srv_trans_enc_ctx {
32         struct smb_trans_enc_state *es;
33         AUTH_NTLMSSP_STATE *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
34 };
35
36 static struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
37 static struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
38
39 /******************************************************************************
40  Is server encryption on ?
41 ******************************************************************************/
42
43 BOOL srv_encryption_on(void)
44 {
45         if (srv_trans_enc_ctx) {
46                 return common_encryption_on(srv_trans_enc_ctx->es);
47         }
48         return False;
49 }
50
51 /******************************************************************************
52  Create an auth_ntlmssp_state and ensure pointer copy is correct.
53 ******************************************************************************/
54
55 static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
56 {
57         NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
58         if (!NT_STATUS_IS_OK(status)) {
59                 return nt_status_squash(status);
60         }
61
62         /*
63          * We must remember to update the pointer copy for the common
64          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
65          */
66         ec->es->s.ntlmssp_state = ec->auth_ntlmssp_state->ntlmssp_state;
67         return status;
68 }
69
70 /******************************************************************************
71  Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
72 ******************************************************************************/
73
74 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
75 {
76         /*
77          * We must remember to update the pointer copy for the common
78          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
79          */
80
81         if (ec->auth_ntlmssp_state) {
82                 auth_ntlmssp_end(&ec->auth_ntlmssp_state);
83                 /* The auth_ntlmssp_end killed this already. */
84                 ec->es->s.ntlmssp_state = NULL;
85         }
86 }
87
88 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
89
90 /******************************************************************************
91  Import a name.
92 ******************************************************************************/
93
94 static NTSTATUS get_srv_gss_creds(const char *service,
95                                 const char *name,
96                                 gss_cred_usage_t cred_type,
97                                 gss_cred_id_t *p_srv_cred)
98 {
99         OM_uint32 ret;
100         OM_uint32 min;
101         gss_name_t srv_name;
102         gss_buffer_desc input_name;
103         char *host_princ_s = NULL;
104         NTSTATUS status = NT_STATUS_OK;
105
106         gss_OID_desc nt_hostbased_service =
107         {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
108
109         asprintf(&host_princ_s, "%s@%s", service, name);
110         if (host_princ_s == NULL) {
111                 return NT_STATUS_NO_MEMORY;
112         }
113
114         input_name.value = host_princ_s;
115         input_name.length = strlen(host_princ_s) + 1;
116
117         ret = gss_import_name(&min,
118                                 &input_name,
119                                 &nt_hostbased_service,
120                                 &srv_name);
121
122         DEBUG(10,("get_srv_gss_creds: imported name %s\n",
123                 host_princ_s ));
124
125         if (ret != GSS_S_COMPLETE) {
126                 SAFE_FREE(host_princ_s);
127                 return map_nt_error_from_gss(ret, min);
128         }
129
130         /*
131          * We're accessing the krb5.keytab file here.
132          * ensure we have permissions to do so.
133          */
134         become_root();
135
136         ret = gss_acquire_cred(&min,
137                                 srv_name,
138                                 GSS_C_INDEFINITE,
139                                 GSS_C_NULL_OID_SET,
140                                 cred_type,
141                                 p_srv_cred,
142                                 NULL,
143                                 NULL);
144         unbecome_root();
145
146         if (ret != GSS_S_COMPLETE) {
147                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
148                 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
149                         ads_errstr(adss)));
150                 status = map_nt_error_from_gss(ret, min);
151         }
152
153         SAFE_FREE(host_princ_s);
154         gss_release_name(&min, &srv_name);
155         return status;
156 }
157
158 /******************************************************************************
159  Create a gss state.
160  Try and get the cifs/server@realm principal first, then fall back to
161  host/server@realm.
162 ******************************************************************************/
163
164 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
165 {
166         NTSTATUS status;
167         gss_cred_id_t srv_cred;
168         fstring fqdn;
169
170         name_to_fqdn(fqdn, global_myname());
171         strlower_m(fqdn);
172
173         status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
174         if (!NT_STATUS_IS_OK(status)) {
175                 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
176                 if (!NT_STATUS_IS_OK(status)) {
177                         return nt_status_squash(status);
178                 }
179         }
180
181         ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
182         if (!ec->es->s.gss_state) {
183                 OM_uint32 min;
184                 gss_release_cred(&min, &srv_cred);
185                 return NT_STATUS_NO_MEMORY;
186         }
187         ZERO_STRUCTP(ec->es->s.gss_state);
188         ec->es->s.gss_state->creds = srv_cred;
189
190         /* No context yet. */
191         ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
192
193         return NT_STATUS_OK;
194 }
195 #endif
196
197 /******************************************************************************
198  Shutdown a server encryption context.
199 ******************************************************************************/
200
201 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
202 {
203         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
204
205         if (!ec) {
206                 return;
207         }
208
209         if (ec->es) {
210                 switch (ec->es->smb_enc_type) {
211                         case SMB_TRANS_ENC_NTLM:
212                                 destroy_auth_ntlmssp(ec);
213                                 break;
214 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
215                         case SMB_TRANS_ENC_GSS:
216                                 break;
217 #endif
218                 }
219                 common_free_encryption_state(&ec->es);
220         }
221
222         SAFE_FREE(ec);
223         *pp_ec = NULL;
224 }
225
226 /******************************************************************************
227  Create a server encryption context.
228 ******************************************************************************/
229
230 static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
231 {
232         struct smb_srv_trans_enc_ctx *ec;
233
234         *pp_ec = NULL;
235
236         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
237         if (!ec) {
238                 return NT_STATUS_NO_MEMORY;
239         }
240         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
241         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
242         if (!ec->es) {
243                 SAFE_FREE(ec);
244                 return NT_STATUS_NO_MEMORY;
245         }
246         ZERO_STRUCTP(ec->es);
247         ec->es->smb_enc_type = smb_enc_type;
248         switch (smb_enc_type) {
249                 case SMB_TRANS_ENC_NTLM:
250                         {
251                                 NTSTATUS status = make_auth_ntlmssp(ec);
252                                 if (!NT_STATUS_IS_OK(status)) {
253                                         srv_free_encryption_context(&ec);
254                                         return status;
255                                 }
256                         }
257                         break;
258
259 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
260                 case SMB_TRANS_ENC_GSS:
261                         /* Acquire our credentials by calling gss_acquire_cred here. */
262                         {
263                                 NTSTATUS status = make_auth_gss(ec);
264                                 if (!NT_STATUS_IS_OK(status)) {
265                                         srv_free_encryption_context(&ec);
266                                         return status;
267                                 }
268                         }
269                         break;
270 #endif
271                 default:
272                         srv_free_encryption_context(&ec);
273                         return NT_STATUS_INVALID_PARAMETER;
274         }
275         *pp_ec = ec;
276         return NT_STATUS_OK;
277 }
278
279 /******************************************************************************
280  Free an encryption-allocated buffer.
281 ******************************************************************************/
282
283 void srv_free_enc_buffer(char *buf)
284 {
285         /* We know this is an smb buffer, and we
286          * didn't malloc, only copy, for a keepalive,
287          * so ignore session keepalives. */
288
289         if(CVAL(buf,0) == SMBkeepalive) {
290                 return;
291         }
292
293         if (srv_trans_enc_ctx) {
294                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
295         }
296 }
297
298 /******************************************************************************
299  Decrypt an incoming buffer.
300 ******************************************************************************/
301
302 NTSTATUS srv_decrypt_buffer(char *buf)
303 {
304         /* Ignore session keepalives. */
305         if(CVAL(buf,0) == SMBkeepalive) {
306                 return NT_STATUS_OK;
307         }
308
309         if (srv_trans_enc_ctx) {
310                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
311         }
312
313         return NT_STATUS_OK;
314 }
315
316 /******************************************************************************
317  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
318 ******************************************************************************/
319
320 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
321 {
322         *buf_out = buf;
323
324         /* Ignore session keepalives. */
325         if(CVAL(buf,0) == SMBkeepalive) {
326                 return NT_STATUS_OK;
327         }
328
329         if (srv_trans_enc_ctx) {
330                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
331         }
332         /* Not encrypting. */
333         return NT_STATUS_OK;
334 }
335
336 /******************************************************************************
337  Do the gss encryption negotiation. Parameters are in/out.
338  Until success we do everything on the partial enc ctx.
339 ******************************************************************************/
340
341 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
342 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
343 {
344         OM_uint32 ret;
345         OM_uint32 min;
346         OM_uint32 flags = 0;
347         gss_buffer_desc in_buf, out_buf;
348         struct smb_tran_enc_state_gss *gss_state;
349         DATA_BLOB auth_reply = data_blob(NULL,0);
350         DATA_BLOB response = data_blob(NULL,0);
351         NTSTATUS status;
352
353         if (!partial_srv_trans_enc_ctx) {
354                 status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
355                 if (!NT_STATUS_IS_OK(status)) {
356                         return status;
357                 }
358         }
359
360         gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
361
362         in_buf.value = secblob.data;
363         in_buf.length = secblob.length;
364
365         out_buf.value = NULL;
366         out_buf.length = 0;
367
368         become_root();
369
370         ret = gss_accept_sec_context(&min,
371                                 &gss_state->gss_ctx,
372                                 gss_state->creds,
373                                 &in_buf,
374                                 GSS_C_NO_CHANNEL_BINDINGS,
375                                 NULL,
376                                 NULL,           /* Ignore oids. */
377                                 &out_buf,       /* To return. */
378                                 &flags,
379                                 NULL,           /* Ingore time. */
380                                 NULL);          /* Ignore delegated creds. */
381         unbecome_root();
382
383         status = gss_err_to_ntstatus(ret, min);
384         if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
385                 return status;
386         }
387
388         /* Ensure we've got sign+seal available. */
389         if (ret == GSS_S_COMPLETE) {
390                 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
391                                 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
392                         DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
393                                 "for SMB sealing.\n"));
394                         gss_release_buffer(&min, &out_buf);
395                         return NT_STATUS_ACCESS_DENIED;
396                 }
397         }
398
399         auth_reply = data_blob(out_buf.value, out_buf.length);
400         gss_release_buffer(&min, &out_buf);
401
402         /* Wrap in SPNEGO. */
403         response = spnego_gen_auth_response(&auth_reply, status, OID_KERBEROS5);
404         data_blob_free(&auth_reply);
405
406         SAFE_FREE(*ppdata);
407         *ppdata = response.data;
408         *p_data_size = response.length;
409
410         return status;
411 }
412 #endif
413
414 /******************************************************************************
415  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
416  Until success we do everything on the partial enc ctx.
417 ******************************************************************************/
418
419 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
420 {
421         NTSTATUS status;
422         DATA_BLOB chal = data_blob(NULL, 0);
423         DATA_BLOB response = data_blob(NULL, 0);
424
425         status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
426         if (!NT_STATUS_IS_OK(status)) {
427                 return status;
428         }
429
430         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
431
432         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
433          * for success ... */
434
435         if (spnego_wrap) {
436                 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
437                 data_blob_free(&chal);
438         } else {
439                 /* Return the raw blob. */
440                 response = chal;
441         }
442
443         SAFE_FREE(*ppdata);
444         *ppdata = response.data;
445         *p_data_size = response.length;
446         return status;
447 }
448
449 /******************************************************************************
450  Do the SPNEGO encryption negotiation. Parameters are in/out.
451  Based off code in smbd/sesssionsetup.c
452  Until success we do everything on the partial enc ctx.
453 ******************************************************************************/
454
455 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
456                                         unsigned char **ppdata,
457                                         size_t *p_data_size,
458                                         unsigned char **pparam,
459                                         size_t *p_param_size)
460 {
461         NTSTATUS status;
462         DATA_BLOB blob = data_blob(NULL,0);
463         DATA_BLOB secblob = data_blob(NULL, 0);
464         BOOL got_kerberos_mechanism = False;
465
466         blob = data_blob_const(*ppdata, *p_data_size);
467
468         status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
469         if (!NT_STATUS_IS_OK(status)) {
470                 return nt_status_squash(status);
471         }
472
473         /* We should have no partial context at this point. */
474
475         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
476
477 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
478         if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
479                 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
480         } else 
481 #endif
482         {
483                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
484         }
485
486         data_blob_free(&secblob);
487
488         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
489                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
490                 return nt_status_squash(status);
491         }
492
493         if (NT_STATUS_IS_OK(status)) {
494                 /* Return the context we're using for this encryption state. */
495                 *pparam = SMB_MALLOC(2);
496                 if (!*pparam) {
497                         return NT_STATUS_NO_MEMORY;
498                 }
499                 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
500                 *p_param_size = 2;
501         }
502
503         return status;
504 }
505
506 /******************************************************************************
507  Complete a SPNEGO encryption negotiation. Parameters are in/out.
508  We only get this for a NTLM auth second stage.
509 ******************************************************************************/
510
511 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
512                                         unsigned char **ppdata,
513                                         size_t *p_data_size,
514                                         unsigned char **pparam,
515                                         size_t *p_param_size)
516 {
517         NTSTATUS status;
518         DATA_BLOB blob = data_blob(NULL,0);
519         DATA_BLOB auth = data_blob(NULL,0);
520         DATA_BLOB auth_reply = data_blob(NULL,0);
521         DATA_BLOB response = data_blob(NULL,0);
522         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
523
524         /* We must have a partial context here. */
525
526         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
527                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
528                 return NT_STATUS_INVALID_PARAMETER;
529         }
530
531         blob = data_blob_const(*ppdata, *p_data_size);
532         if (!spnego_parse_auth(blob, &auth)) {
533                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
534                 return NT_STATUS_INVALID_PARAMETER;
535         }
536
537         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
538         data_blob_free(&auth);
539
540         response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
541         data_blob_free(&auth_reply);
542
543         if (NT_STATUS_IS_OK(status)) {
544                 /* Return the context we're using for this encryption state. */
545                 *pparam = SMB_MALLOC(2);
546                 if (!*pparam) {
547                         return NT_STATUS_NO_MEMORY;
548                 }
549                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
550                 *p_param_size = 2;
551         }
552
553         SAFE_FREE(*ppdata);
554         *ppdata = response.data;
555         *p_data_size = response.length;
556         return status;
557 }
558
559 /******************************************************************************
560  Raw NTLM encryption negotiation. Parameters are in/out.
561  This function does both steps.
562 ******************************************************************************/
563
564 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
565                                         unsigned char **ppdata,
566                                         size_t *p_data_size,
567                                         unsigned char **pparam,
568                                         size_t *p_param_size)
569 {
570         NTSTATUS status;
571         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
572         DATA_BLOB response = data_blob(NULL,0);
573         struct smb_srv_trans_enc_ctx *ec;
574
575         if (!partial_srv_trans_enc_ctx) {
576                 /* This is the initial step. */
577                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
578                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
579                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
580                         return nt_status_squash(status);
581                 }
582                 return status;
583         }
584
585         ec = partial_srv_trans_enc_ctx;
586         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
587                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
588                 return NT_STATUS_INVALID_PARAMETER;
589         }
590
591         /* Second step. */
592         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
593
594         if (NT_STATUS_IS_OK(status)) {
595                 /* Return the context we're using for this encryption state. */
596                 *pparam = SMB_MALLOC(2);
597                 if (!*pparam) {
598                         return NT_STATUS_NO_MEMORY;
599                 }
600                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
601                 *p_param_size = 2;
602         }
603
604         /* Return the raw blob. */
605         SAFE_FREE(*ppdata);
606         *ppdata = response.data;
607         *p_data_size = response.length;
608         return status;
609 }
610
611 /******************************************************************************
612  Do the SPNEGO encryption negotiation. Parameters are in/out.
613 ******************************************************************************/
614
615 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
616                                         unsigned char **ppdata,
617                                         size_t *p_data_size,
618                                         unsigned char **pparam,
619                                         size_t *p_param_size)
620 {
621         unsigned char *pdata = *ppdata;
622
623         SAFE_FREE(*pparam);
624         *p_param_size = 0;
625
626         if (*p_data_size < 1) {
627                 return NT_STATUS_INVALID_PARAMETER;
628         }
629
630         if (pdata[0] == ASN1_APPLICATION(0)) {
631                 /* its a negTokenTarg packet */
632                 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
633         }
634
635         if (pdata[0] == ASN1_CONTEXT(1)) {
636                 /* It's an auth packet */
637                 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
638         }
639
640         /* Maybe it's a raw unwrapped auth ? */
641         if (*p_data_size < 7) {
642                 return NT_STATUS_INVALID_PARAMETER;
643         }
644
645         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
646                 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
647         }
648
649         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
650
651         return NT_STATUS_LOGON_FAILURE;
652 }
653
654 /******************************************************************************
655  Negotiation was successful - turn on server-side encryption.
656 ******************************************************************************/
657
658 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
659 {
660         if (!ec || !ec->es) {
661                 return NT_STATUS_LOGON_FAILURE;
662         }
663
664         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
665                 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
666                                 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
667                         return NT_STATUS_INVALID_PARAMETER;
668                 }
669         }
670         /* Todo - check gssapi case. */
671
672         return NT_STATUS_OK;
673 }
674
675 /******************************************************************************
676  Negotiation was successful - turn on server-side encryption.
677 ******************************************************************************/
678
679 NTSTATUS srv_encryption_start(connection_struct *conn)
680 {
681         NTSTATUS status;
682
683         /* Check that we are really doing sign+seal. */
684         status = check_enc_good(partial_srv_trans_enc_ctx);
685         if (!NT_STATUS_IS_OK(status)) {
686                 return status;
687         }
688         /* Throw away the context we're using currently (if any). */
689         srv_free_encryption_context(&srv_trans_enc_ctx);
690
691         /* Steal the partial pointer. Deliberate shallow copy. */
692         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
693         srv_trans_enc_ctx->es->enc_on = True;
694
695         partial_srv_trans_enc_ctx = NULL;
696         return NT_STATUS_OK;
697 }
698
699 /******************************************************************************
700  Shutdown all server contexts.
701 ******************************************************************************/
702
703 void server_encryption_shutdown(void)
704 {
705         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
706         srv_free_encryption_context(&srv_trans_enc_ctx);
707 }