r22350: Add some helpful debug messages.
[ab/samba.git/.git] / source3 / 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         ret = gss_acquire_cred(&min,
131                                 srv_name,
132                                 GSS_C_INDEFINITE,
133                                 GSS_C_NULL_OID_SET,
134                                 cred_type,
135                                 p_srv_cred,
136                                 NULL,
137                                 NULL);
138
139         if (ret != GSS_S_COMPLETE) {
140                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
141                 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
142                         ads_errstr(adss)));
143                 status = map_nt_error_from_gss(ret, min);
144         }
145
146         SAFE_FREE(host_princ_s);
147         gss_release_name(&min, &srv_name);
148         return status;
149 }
150
151 /******************************************************************************
152  Create a gss state.
153  Try and get the cifs/server@realm principal first, then fall back to
154  host/server@realm.
155 ******************************************************************************/
156
157 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
158 {
159         NTSTATUS status;
160         gss_cred_id_t srv_cred;
161         fstring fqdn;
162
163         name_to_fqdn(fqdn, global_myname());
164         strlower_m(fqdn);
165
166         status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
167         if (!NT_STATUS_IS_OK(status)) {
168                 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
169                 if (!NT_STATUS_IS_OK(status)) {
170                         return nt_status_squash(status);
171                 }
172         }
173
174         ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
175         if (!ec->es->s.gss_state) {
176                 OM_uint32 min;
177                 gss_release_cred(&min, &srv_cred);
178                 return NT_STATUS_NO_MEMORY;
179         }
180         ZERO_STRUCTP(ec->es->s.gss_state);
181         ec->es->s.gss_state->creds = srv_cred;
182
183         /* No context yet. */
184         ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
185
186         return NT_STATUS_OK;
187 }
188 #endif
189
190 /******************************************************************************
191  Shutdown a server encryption context.
192 ******************************************************************************/
193
194 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
195 {
196         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
197
198         if (!ec) {
199                 return;
200         }
201
202         if (ec->es) {
203                 switch (ec->es->smb_enc_type) {
204                         case SMB_TRANS_ENC_NTLM:
205                                 destroy_auth_ntlmssp(ec);
206                                 break;
207 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
208                         case SMB_TRANS_ENC_GSS:
209                                 break;
210 #endif
211                 }
212                 common_free_encryption_state(&ec->es);
213         }
214
215         SAFE_FREE(ec);
216         *pp_ec = NULL;
217 }
218
219 /******************************************************************************
220  Create a server encryption context.
221 ******************************************************************************/
222
223 static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
224 {
225         struct smb_srv_trans_enc_ctx *ec;
226
227         *pp_ec = NULL;
228
229         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
230         if (!ec) {
231                 return NT_STATUS_NO_MEMORY;
232         }
233         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
234         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
235         if (!ec->es) {
236                 SAFE_FREE(ec);
237                 return NT_STATUS_NO_MEMORY;
238         }
239         ZERO_STRUCTP(ec->es);
240         ec->es->smb_enc_type = smb_enc_type;
241         switch (smb_enc_type) {
242                 case SMB_TRANS_ENC_NTLM:
243                         {
244                                 NTSTATUS status = make_auth_ntlmssp(ec);
245                                 if (!NT_STATUS_IS_OK(status)) {
246                                         srv_free_encryption_context(&ec);
247                                         return status;
248                                 }
249                         }
250                         break;
251
252 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
253                 case SMB_TRANS_ENC_GSS:
254                         /* Acquire our credentials by calling gss_acquire_cred here. */
255                         {
256                                 NTSTATUS status = make_auth_gss(ec);
257                                 if (!NT_STATUS_IS_OK(status)) {
258                                         srv_free_encryption_context(&ec);
259                                         return status;
260                                 }
261                         }
262                         break;
263 #endif
264                 default:
265                         srv_free_encryption_context(&ec);
266                         return NT_STATUS_INVALID_PARAMETER;
267         }
268         *pp_ec = ec;
269         return NT_STATUS_OK;
270 }
271
272 /******************************************************************************
273  Free an encryption-allocated buffer.
274 ******************************************************************************/
275
276 void srv_free_enc_buffer(char *buf)
277 {
278         /* We know this is an smb buffer, and we
279          * didn't malloc, only copy, for a keepalive,
280          * so ignore session keepalives. */
281
282         if(CVAL(buf,0) == SMBkeepalive) {
283                 return;
284         }
285
286         if (srv_trans_enc_ctx) {
287                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
288         }
289 }
290
291 /******************************************************************************
292  Decrypt an incoming buffer.
293 ******************************************************************************/
294
295 NTSTATUS srv_decrypt_buffer(char *buf)
296 {
297         /* Ignore session keepalives. */
298         if(CVAL(buf,0) == SMBkeepalive) {
299                 return NT_STATUS_OK;
300         }
301
302         if (srv_trans_enc_ctx) {
303                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
304         }
305
306         return NT_STATUS_OK;
307 }
308
309 /******************************************************************************
310  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
311 ******************************************************************************/
312
313 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
314 {
315         *buf_out = buf;
316
317         /* Ignore session keepalives. */
318         if(CVAL(buf,0) == SMBkeepalive) {
319                 return NT_STATUS_OK;
320         }
321
322         if (srv_trans_enc_ctx) {
323                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
324         }
325         /* Not encrypting. */
326         return NT_STATUS_OK;
327 }
328
329 /******************************************************************************
330  Do the gss encryption negotiation. Parameters are in/out.
331  Until success we do everything on the partial enc ctx.
332 ******************************************************************************/
333
334 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
335 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
336 {
337         OM_uint32 ret;
338         OM_uint32 min;
339         OM_uint32 flags = 0;
340         gss_buffer_desc in_buf, out_buf;
341         struct smb_tran_enc_state_gss *gss_state;
342         DATA_BLOB auth_reply = data_blob(NULL,0);
343         DATA_BLOB response = data_blob(NULL,0);
344         NTSTATUS status;
345
346         if (!partial_srv_trans_enc_ctx) {
347                 status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
348                 if (!NT_STATUS_IS_OK(status)) {
349                         return status;
350                 }
351         }
352
353         gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
354
355         in_buf.value = secblob.data;
356         in_buf.length = secblob.length;
357
358         out_buf.value = NULL;
359         out_buf.length = 0;
360
361         ret = gss_accept_sec_context(&min,
362                                 &gss_state->gss_ctx,
363                                 gss_state->creds,
364                                 &in_buf,
365                                 GSS_C_NO_CHANNEL_BINDINGS,
366                                 NULL,
367                                 NULL,           /* Ignore oids. */
368                                 &out_buf,       /* To return. */
369                                 &flags,
370                                 NULL,           /* Ingore time. */
371                                 NULL);          /* Ignore delegated creds. */
372
373         status = gss_err_to_ntstatus(ret, min);
374         if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
375                 return status;
376         }
377
378         /* Ensure we've got sign+seal available. */
379         if (ret == GSS_S_COMPLETE) {
380                 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
381                                 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
382                         DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
383                                 "for SMB sealing.\n"));
384                         gss_release_buffer(&min, &out_buf);
385                         return NT_STATUS_ACCESS_DENIED;
386                 }
387         }
388
389         auth_reply = data_blob(out_buf.value, out_buf.length);
390         gss_release_buffer(&min, &out_buf);
391
392         /* Wrap in SPNEGO. */
393         response = spnego_gen_auth_response(&auth_reply, status, OID_KERBEROS5);
394         data_blob_free(&auth_reply);
395
396         SAFE_FREE(*ppdata);
397         *ppdata = response.data;
398         *p_data_size = response.length;
399
400         return status;
401 }
402 #endif
403
404 /******************************************************************************
405  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
406  Until success we do everything on the partial enc ctx.
407 ******************************************************************************/
408
409 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
410 {
411         NTSTATUS status;
412         DATA_BLOB chal = data_blob(NULL, 0);
413         DATA_BLOB response = data_blob(NULL, 0);
414
415         status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
416         if (!NT_STATUS_IS_OK(status)) {
417                 return status;
418         }
419
420         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
421
422         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
423          * for success ... */
424
425         if (spnego_wrap) {
426                 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
427                 data_blob_free(&chal);
428         } else {
429                 /* Return the raw blob. */
430                 response = chal;
431         }
432
433         SAFE_FREE(*ppdata);
434         *ppdata = response.data;
435         *p_data_size = response.length;
436         return status;
437 }
438
439 /******************************************************************************
440  Do the SPNEGO encryption negotiation. Parameters are in/out.
441  Based off code in smbd/sesssionsetup.c
442  Until success we do everything on the partial enc ctx.
443 ******************************************************************************/
444
445 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
446                                         unsigned char **ppdata,
447                                         size_t *p_data_size,
448                                         unsigned char **pparam,
449                                         size_t *p_param_size)
450 {
451         NTSTATUS status;
452         DATA_BLOB blob = data_blob(NULL,0);
453         DATA_BLOB secblob = data_blob(NULL, 0);
454         BOOL got_kerberos_mechanism = False;
455
456         blob = data_blob_const(*ppdata, *p_data_size);
457
458         status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
459         if (!NT_STATUS_IS_OK(status)) {
460                 return nt_status_squash(status);
461         }
462
463         /* We should have no partial context at this point. */
464
465         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
466
467 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
468         if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
469                 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
470         } else 
471 #endif
472         {
473                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
474         }
475
476         data_blob_free(&secblob);
477
478         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
479                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
480                 return nt_status_squash(status);
481         }
482
483         if (NT_STATUS_IS_OK(status)) {
484                 /* Return the context we're using for this encryption state. */
485                 *pparam = SMB_MALLOC(2);
486                 if (!*pparam) {
487                         return NT_STATUS_NO_MEMORY;
488                 }
489                 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
490                 *p_param_size = 2;
491         }
492
493         return status;
494 }
495
496 /******************************************************************************
497  Complete a SPNEGO encryption negotiation. Parameters are in/out.
498  We only get this for a NTLM auth second stage.
499 ******************************************************************************/
500
501 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
502                                         unsigned char **ppdata,
503                                         size_t *p_data_size,
504                                         unsigned char **pparam,
505                                         size_t *p_param_size)
506 {
507         NTSTATUS status;
508         DATA_BLOB blob = data_blob(NULL,0);
509         DATA_BLOB auth = data_blob(NULL,0);
510         DATA_BLOB auth_reply = data_blob(NULL,0);
511         DATA_BLOB response = data_blob(NULL,0);
512         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
513
514         /* We must have a partial context here. */
515
516         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
517                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
518                 return NT_STATUS_INVALID_PARAMETER;
519         }
520
521         blob = data_blob_const(*ppdata, *p_data_size);
522         if (!spnego_parse_auth(blob, &auth)) {
523                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
524                 return NT_STATUS_INVALID_PARAMETER;
525         }
526
527         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
528         data_blob_free(&auth);
529
530         response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
531         data_blob_free(&auth_reply);
532
533         if (NT_STATUS_IS_OK(status)) {
534                 /* Return the context we're using for this encryption state. */
535                 *pparam = SMB_MALLOC(2);
536                 if (!*pparam) {
537                         return NT_STATUS_NO_MEMORY;
538                 }
539                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
540                 *p_param_size = 2;
541         }
542
543         SAFE_FREE(*ppdata);
544         *ppdata = response.data;
545         *p_data_size = response.length;
546         return status;
547 }
548
549 /******************************************************************************
550  Raw NTLM encryption negotiation. Parameters are in/out.
551  This function does both steps.
552 ******************************************************************************/
553
554 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
555                                         unsigned char **ppdata,
556                                         size_t *p_data_size,
557                                         unsigned char **pparam,
558                                         size_t *p_param_size)
559 {
560         NTSTATUS status;
561         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
562         DATA_BLOB response = data_blob(NULL,0);
563         struct smb_srv_trans_enc_ctx *ec;
564
565         if (!partial_srv_trans_enc_ctx) {
566                 /* This is the initial step. */
567                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
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);
571                 }
572                 return status;
573         }
574
575         ec = partial_srv_trans_enc_ctx;
576         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
577                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
578                 return NT_STATUS_INVALID_PARAMETER;
579         }
580
581         /* Second step. */
582         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
583
584         if (NT_STATUS_IS_OK(status)) {
585                 /* Return the context we're using for this encryption state. */
586                 *pparam = SMB_MALLOC(2);
587                 if (!*pparam) {
588                         return NT_STATUS_NO_MEMORY;
589                 }
590                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
591                 *p_param_size = 2;
592         }
593
594         /* Return the raw blob. */
595         SAFE_FREE(*ppdata);
596         *ppdata = response.data;
597         *p_data_size = response.length;
598         return status;
599 }
600
601 /******************************************************************************
602  Do the SPNEGO encryption negotiation. Parameters are in/out.
603 ******************************************************************************/
604
605 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
606                                         unsigned char **ppdata,
607                                         size_t *p_data_size,
608                                         unsigned char **pparam,
609                                         size_t *p_param_size)
610 {
611         unsigned char *pdata = *ppdata;
612
613         SAFE_FREE(*pparam);
614         *p_param_size = 0;
615
616         if (*p_data_size < 1) {
617                 return NT_STATUS_INVALID_PARAMETER;
618         }
619
620         if (pdata[0] == ASN1_APPLICATION(0)) {
621                 /* its a negTokenTarg packet */
622                 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
623         }
624
625         if (pdata[0] == ASN1_CONTEXT(1)) {
626                 /* It's an auth packet */
627                 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
628         }
629
630         /* Maybe it's a raw unwrapped auth ? */
631         if (*p_data_size < 7) {
632                 return NT_STATUS_INVALID_PARAMETER;
633         }
634
635         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
636                 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
637         }
638
639         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
640
641         return NT_STATUS_LOGON_FAILURE;
642 }
643
644 /******************************************************************************
645  Negotiation was successful - turn on server-side encryption.
646 ******************************************************************************/
647
648 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
649 {
650         if (!ec || !ec->es) {
651                 return NT_STATUS_LOGON_FAILURE;
652         }
653
654         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
655                 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
656                                 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
657                         return NT_STATUS_INVALID_PARAMETER;
658                 }
659         }
660         /* Todo - check gssapi case. */
661
662         return NT_STATUS_OK;
663 }
664
665 /******************************************************************************
666  Negotiation was successful - turn on server-side encryption.
667 ******************************************************************************/
668
669 NTSTATUS srv_encryption_start(connection_struct *conn)
670 {
671         NTSTATUS status;
672
673         /* Check that we are really doing sign+seal. */
674         status = check_enc_good(partial_srv_trans_enc_ctx);
675         if (!NT_STATUS_IS_OK(status)) {
676                 return status;
677         }
678         /* Throw away the context we're using currently (if any). */
679         srv_free_encryption_context(&srv_trans_enc_ctx);
680
681         /* Steal the partial pointer. Deliberate shallow copy. */
682         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
683         srv_trans_enc_ctx->es->enc_on = True;
684
685         partial_srv_trans_enc_ctx = NULL;
686         return NT_STATUS_OK;
687 }
688
689 /******************************************************************************
690  Shutdown all server contexts.
691 ******************************************************************************/
692
693 void server_encryption_shutdown(void)
694 {
695         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
696         srv_free_encryption_context(&srv_trans_enc_ctx);
697 }