s3: Fix some nonempty blank lines
[abartlet/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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
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"
31
32 /******************************************************************************
33  Server side encryption.
34 ******************************************************************************/
35
36 /******************************************************************************
37  Global server state.
38 ******************************************************************************/
39
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. */
43 };
44
45 /******************************************************************************
46  Return global enc context - this must change if we ever do multiple contexts.
47 ******************************************************************************/
48
49 uint16_t srv_enc_ctx(void)
50 {
51         return srv_trans_enc_ctx->es->enc_ctx_num;
52 }
53
54 /******************************************************************************
55  Is this an incoming encrypted packet ?
56 ******************************************************************************/
57
58 bool is_encrypted_packet(const uint8_t *inbuf)
59 {
60         NTSTATUS status;
61         uint16_t enc_num;
62
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;
68         }
69
70         status = get_enc_ctx_num(inbuf, &enc_num);
71         if (!NT_STATUS_IS_OK(status)) {
72                 return false;
73         }
74
75         /* Encrypted messages are 0xFF'E'<ctx> */
76         if (srv_trans_enc_ctx && enc_num == srv_enc_ctx()) {
77                 return true;
78         }
79         return false;
80 }
81
82 /******************************************************************************
83  Create an auth_ntlmssp_state and ensure pointer copy is correct.
84 ******************************************************************************/
85
86 static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
87                                   struct smb_srv_trans_enc_ctx *ec)
88 {
89         NTSTATUS status = auth_ntlmssp_prepare(remote_address,
90                                              &ec->auth_ntlmssp_state);
91         if (!NT_STATUS_IS_OK(status)) {
92                 return nt_status_squash(status);
93         }
94
95         auth_ntlmssp_want_feature(ec->auth_ntlmssp_state, NTLMSSP_FEATURE_SEAL);
96
97         status = auth_ntlmssp_start(ec->auth_ntlmssp_state);
98
99         if (!NT_STATUS_IS_OK(status)) {
100                 return nt_status_squash(status);
101         }
102
103         /*
104          * We must remember to update the pointer copy for the common
105          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
106          */
107         ec->es->s.auth_ntlmssp_state = ec->auth_ntlmssp_state;
108         return status;
109 }
110
111 /******************************************************************************
112  Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
113 ******************************************************************************/
114
115 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
116 {
117         /*
118          * We must remember to update the pointer copy for the common
119          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
120          */
121
122         if (ec->auth_ntlmssp_state) {
123                 TALLOC_FREE(ec->auth_ntlmssp_state);
124                 /* The auth_ntlmssp_end killed this already. */
125                 ec->es->s.auth_ntlmssp_state = NULL;
126         }
127 }
128
129 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
130
131 /******************************************************************************
132  Import a name.
133 ******************************************************************************/
134
135 static NTSTATUS get_srv_gss_creds(const char *service,
136                                 const char *name,
137                                 gss_cred_usage_t cred_type,
138                                 gss_cred_id_t *p_srv_cred)
139 {
140         OM_uint32 ret;
141         OM_uint32 min;
142         gss_name_t srv_name;
143         gss_buffer_desc input_name;
144         char *host_princ_s = NULL;
145         NTSTATUS status = NT_STATUS_OK;
146
147         gss_OID_desc nt_hostbased_service =
148         {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
149
150         if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         input_name.value = host_princ_s;
155         input_name.length = strlen(host_princ_s) + 1;
156
157         ret = gss_import_name(&min,
158                                 &input_name,
159                                 &nt_hostbased_service,
160                                 &srv_name);
161
162         DEBUG(10,("get_srv_gss_creds: imported name %s\n",
163                 host_princ_s ));
164
165         if (ret != GSS_S_COMPLETE) {
166                 SAFE_FREE(host_princ_s);
167                 return map_nt_error_from_gss(ret, min);
168         }
169
170         /*
171          * We're accessing the krb5.keytab file here.
172          * ensure we have permissions to do so.
173          */
174         become_root();
175
176         ret = gss_acquire_cred(&min,
177                                 srv_name,
178                                 GSS_C_INDEFINITE,
179                                 GSS_C_NULL_OID_SET,
180                                 cred_type,
181                                 p_srv_cred,
182                                 NULL,
183                                 NULL);
184         unbecome_root();
185
186         if (ret != GSS_S_COMPLETE) {
187                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
188                 DEBUG(10,("get_srv_gss_creds: gss_acquire_cred failed with %s\n",
189                         ads_errstr(adss)));
190                 status = map_nt_error_from_gss(ret, min);
191         }
192
193         SAFE_FREE(host_princ_s);
194         gss_release_name(&min, &srv_name);
195         return status;
196 }
197
198 /******************************************************************************
199  Create a gss state.
200  Try and get the cifs/server@realm principal first, then fall back to
201  host/server@realm.
202 ******************************************************************************/
203
204 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
205 {
206         NTSTATUS status;
207         gss_cred_id_t srv_cred;
208         fstring fqdn;
209
210         name_to_fqdn(fqdn, lp_netbios_name());
211         strlower_m(fqdn);
212
213         status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
214         if (!NT_STATUS_IS_OK(status)) {
215                 status = get_srv_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
216                 if (!NT_STATUS_IS_OK(status)) {
217                         return nt_status_squash(status);
218                 }
219         }
220
221         ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
222         if (!ec->es->s.gss_state) {
223                 OM_uint32 min;
224                 gss_release_cred(&min, &srv_cred);
225                 return NT_STATUS_NO_MEMORY;
226         }
227         ZERO_STRUCTP(ec->es->s.gss_state);
228         ec->es->s.gss_state->creds = srv_cred;
229
230         /* No context yet. */
231         ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
232
233         return NT_STATUS_OK;
234 }
235 #endif
236
237 /******************************************************************************
238  Shutdown a server encryption context.
239 ******************************************************************************/
240
241 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
242 {
243         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
244
245         if (!ec) {
246                 return;
247         }
248
249         if (ec->es) {
250                 switch (ec->es->smb_enc_type) {
251                         case SMB_TRANS_ENC_NTLM:
252                                 destroy_auth_ntlmssp(ec);
253                                 break;
254 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
255                         case SMB_TRANS_ENC_GSS:
256                                 break;
257 #endif
258                 }
259                 common_free_encryption_state(&ec->es);
260         }
261
262         SAFE_FREE(ec);
263         *pp_ec = NULL;
264 }
265
266 /******************************************************************************
267  Create a server encryption context.
268 ******************************************************************************/
269
270 static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
271                                             enum smb_trans_enc_type smb_enc_type,
272                                             struct smb_srv_trans_enc_ctx **pp_ec)
273 {
274         struct smb_srv_trans_enc_ctx *ec;
275
276         *pp_ec = NULL;
277
278         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
279         if (!ec) {
280                 return NT_STATUS_NO_MEMORY;
281         }
282         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
283         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
284         if (!ec->es) {
285                 SAFE_FREE(ec);
286                 return NT_STATUS_NO_MEMORY;
287         }
288         ZERO_STRUCTP(ec->es);
289         ec->es->smb_enc_type = smb_enc_type;
290         switch (smb_enc_type) {
291                 case SMB_TRANS_ENC_NTLM:
292                         {
293                                 NTSTATUS status = make_auth_ntlmssp(remote_address,
294                                                                     ec);
295                                 if (!NT_STATUS_IS_OK(status)) {
296                                         srv_free_encryption_context(&ec);
297                                         return status;
298                                 }
299                         }
300                         break;
301
302 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
303                 case SMB_TRANS_ENC_GSS:
304                         /* Acquire our credentials by calling gss_acquire_cred here. */
305                         {
306                                 NTSTATUS status = make_auth_gss(ec);
307                                 if (!NT_STATUS_IS_OK(status)) {
308                                         srv_free_encryption_context(&ec);
309                                         return status;
310                                 }
311                         }
312                         break;
313 #endif
314                 default:
315                         srv_free_encryption_context(&ec);
316                         return NT_STATUS_INVALID_PARAMETER;
317         }
318         *pp_ec = ec;
319         return NT_STATUS_OK;
320 }
321
322 /******************************************************************************
323  Free an encryption-allocated buffer.
324 ******************************************************************************/
325
326 void srv_free_enc_buffer(char *buf)
327 {
328         /* We know this is an smb buffer, and we
329          * didn't malloc, only copy, for a keepalive,
330          * so ignore non-session messages. */
331
332         if(CVAL(buf,0)) {
333                 return;
334         }
335
336         if (srv_trans_enc_ctx) {
337                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
338         }
339 }
340
341 /******************************************************************************
342  Decrypt an incoming buffer.
343 ******************************************************************************/
344
345 NTSTATUS srv_decrypt_buffer(char *buf)
346 {
347         /* Ignore non-session messages. */
348         if(CVAL(buf,0)) {
349                 return NT_STATUS_OK;
350         }
351
352         if (srv_trans_enc_ctx) {
353                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
354         }
355
356         return NT_STATUS_OK;
357 }
358
359 /******************************************************************************
360  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
361 ******************************************************************************/
362
363 NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
364 {
365         *buf_out = buf;
366
367         /* Ignore non-session messages. */
368         if(CVAL(buf,0)) {
369                 return NT_STATUS_OK;
370         }
371
372         if (srv_trans_enc_ctx) {
373                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
374         }
375         /* Not encrypting. */
376         return NT_STATUS_OK;
377 }
378
379 /******************************************************************************
380  Do the gss encryption negotiation. Parameters are in/out.
381  Until success we do everything on the partial enc ctx.
382 ******************************************************************************/
383
384 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
385 static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
386                                              unsigned char **ppdata,
387                                              size_t *p_data_size,
388                                              DATA_BLOB secblob)
389 {
390         OM_uint32 ret;
391         OM_uint32 min;
392         OM_uint32 flags = 0;
393         gss_buffer_desc in_buf, out_buf;
394         struct smb_tran_enc_state_gss *gss_state;
395         DATA_BLOB auth_reply = data_blob_null;
396         DATA_BLOB response = data_blob_null;
397         NTSTATUS status;
398
399         if (!partial_srv_trans_enc_ctx) {
400                 status = make_srv_encryption_context(remote_address,
401                                                      SMB_TRANS_ENC_GSS,
402                                                      &partial_srv_trans_enc_ctx);
403                 if (!NT_STATUS_IS_OK(status)) {
404                         return status;
405                 }
406         }
407
408         gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
409
410         in_buf.value = secblob.data;
411         in_buf.length = secblob.length;
412
413         out_buf.value = NULL;
414         out_buf.length = 0;
415
416         become_root();
417
418         ret = gss_accept_sec_context(&min,
419                                 &gss_state->gss_ctx,
420                                 gss_state->creds,
421                                 &in_buf,
422                                 GSS_C_NO_CHANNEL_BINDINGS,
423                                 NULL,
424                                 NULL,           /* Ignore oids. */
425                                 &out_buf,       /* To return. */
426                                 &flags,
427                                 NULL,           /* Ingore time. */
428                                 NULL);          /* Ignore delegated creds. */
429         unbecome_root();
430
431         status = gss_err_to_ntstatus(ret, min);
432         if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
433                 return status;
434         }
435
436         /* Ensure we've got sign+seal available. */
437         if (ret == GSS_S_COMPLETE) {
438                 if ((flags & (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) !=
439                                 (GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)) {
440                         DEBUG(0,("srv_enc_spnego_gss_negotiate: quality of service not good enough "
441                                 "for SMB sealing.\n"));
442                         gss_release_buffer(&min, &out_buf);
443                         return NT_STATUS_ACCESS_DENIED;
444                 }
445         }
446
447         auth_reply = data_blob(out_buf.value, out_buf.length);
448         gss_release_buffer(&min, &out_buf);
449
450         /* Wrap in SPNEGO. */
451         response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
452         data_blob_free(&auth_reply);
453
454         SAFE_FREE(*ppdata);
455         *ppdata = (unsigned char *)memdup(response.data, response.length);
456         if ((*ppdata) == NULL && response.length > 0) {
457                 status = NT_STATUS_NO_MEMORY;
458         }
459         *p_data_size = response.length;
460
461         data_blob_free(&response);
462
463         return status;
464 }
465 #endif
466
467 /******************************************************************************
468  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
469  Until success we do everything on the partial enc ctx.
470 ******************************************************************************/
471
472 static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
473                                        unsigned char **ppdata,
474                                        size_t *p_data_size,
475                                        DATA_BLOB secblob,
476                                        bool spnego_wrap)
477 {
478         NTSTATUS status;
479         DATA_BLOB chal = data_blob_null;
480         DATA_BLOB response = data_blob_null;
481
482         status = make_srv_encryption_context(remote_address,
483                                              SMB_TRANS_ENC_NTLM,
484                                              &partial_srv_trans_enc_ctx);
485         if (!NT_STATUS_IS_OK(status)) {
486                 return status;
487         }
488
489         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
490                                      partial_srv_trans_enc_ctx->auth_ntlmssp_state,
491                                      secblob, &chal);
492
493         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
494          * for success ... */
495
496         if (spnego_wrap) {
497                 response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
498                 data_blob_free(&chal);
499         } else {
500                 /* Return the raw blob. */
501                 response = chal;
502         }
503
504         SAFE_FREE(*ppdata);
505         *ppdata = (unsigned char *)memdup(response.data, response.length);
506         if ((*ppdata) == NULL && response.length > 0) {
507                 status = NT_STATUS_NO_MEMORY;
508         }
509         *p_data_size = response.length;
510         data_blob_free(&response);
511
512         return status;
513 }
514
515 /******************************************************************************
516  Do the SPNEGO encryption negotiation. Parameters are in/out.
517  Based off code in smbd/sesssionsetup.c
518  Until success we do everything on the partial enc ctx.
519 ******************************************************************************/
520
521 static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
522                                         unsigned char **ppdata,
523                                         size_t *p_data_size,
524                                         unsigned char **pparam,
525                                         size_t *p_param_size)
526 {
527         NTSTATUS status;
528         DATA_BLOB blob = data_blob_null;
529         DATA_BLOB secblob = data_blob_null;
530         char *kerb_mech = NULL;
531
532         blob = data_blob_const(*ppdata, *p_data_size);
533
534         status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
535         if (!NT_STATUS_IS_OK(status)) {
536                 return nt_status_squash(status);
537         }
538
539         /* We should have no partial context at this point. */
540
541         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
542
543         if (kerb_mech) {
544                 TALLOC_FREE(kerb_mech);
545
546 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
547                 status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
548                                                       ppdata,
549                                                       p_data_size,
550                                                       secblob);
551 #else
552                 /* Currently we don't SPNEGO negotiate
553                  * back to NTLMSSP as we do in sessionsetupX. We should... */
554                 return NT_STATUS_LOGON_FAILURE;
555 #endif
556         } else {
557                 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
558                                                 ppdata,
559                                                 p_data_size,
560                                                 secblob,
561                                                 true);
562         }
563
564         data_blob_free(&secblob);
565
566         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
567                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
568                 return nt_status_squash(status);
569         }
570
571         if (NT_STATUS_IS_OK(status)) {
572                 /* Return the context we're using for this encryption state. */
573                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
574                         return NT_STATUS_NO_MEMORY;
575                 }
576                 SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
577                 *p_param_size = 2;
578         }
579
580         return status;
581 }
582
583 /******************************************************************************
584  Complete a SPNEGO encryption negotiation. Parameters are in/out.
585  We only get this for a NTLM auth second stage.
586 ******************************************************************************/
587
588 static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
589                                         unsigned char **ppdata,
590                                         size_t *p_data_size,
591                                         unsigned char **pparam,
592                                         size_t *p_param_size)
593 {
594         NTSTATUS status;
595         DATA_BLOB blob = data_blob_null;
596         DATA_BLOB auth = data_blob_null;
597         DATA_BLOB auth_reply = data_blob_null;
598         DATA_BLOB response = data_blob_null;
599         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
600
601         /* We must have a partial context here. */
602
603         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
604                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
605                 return NT_STATUS_INVALID_PARAMETER;
606         }
607
608         blob = data_blob_const(*ppdata, *p_data_size);
609         if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
610                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
611                 return NT_STATUS_INVALID_PARAMETER;
612         }
613
614         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, talloc_tos(), auth, &auth_reply);
615         data_blob_free(&auth);
616
617         /* From RFC4178.
618          *
619          *    supportedMech
620          *
621          *          This field SHALL only be present in the first reply from the
622          *                target.
623          * So set mechOID to NULL here.
624          */
625
626         response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
627         data_blob_free(&auth_reply);
628
629         if (NT_STATUS_IS_OK(status)) {
630                 /* Return the context we're using for this encryption state. */
631                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
632                         return NT_STATUS_NO_MEMORY;
633                 }
634                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
635                 *p_param_size = 2;
636         }
637
638         SAFE_FREE(*ppdata);
639         *ppdata = (unsigned char *)memdup(response.data, response.length);
640         if ((*ppdata) == NULL && response.length > 0)
641                 return NT_STATUS_NO_MEMORY;
642         *p_data_size = response.length;
643         data_blob_free(&response);
644         return status;
645 }
646
647 /******************************************************************************
648  Raw NTLM encryption negotiation. Parameters are in/out.
649  This function does both steps.
650 ******************************************************************************/
651
652 static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
653                                         unsigned char **ppdata,
654                                         size_t *p_data_size,
655                                         unsigned char **pparam,
656                                         size_t *p_param_size)
657 {
658         NTSTATUS status;
659         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
660         DATA_BLOB response = data_blob_null;
661         struct smb_srv_trans_enc_ctx *ec;
662
663         if (!partial_srv_trans_enc_ctx) {
664                 /* This is the initial step. */
665                 status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
666                                                 ppdata,
667                                                 p_data_size,
668                                                 blob,
669                                                 false);
670                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
671                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
672                         return nt_status_squash(status);
673                 }
674                 return status;
675         }
676
677         ec = partial_srv_trans_enc_ctx;
678         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
679                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
680                 return NT_STATUS_INVALID_PARAMETER;
681         }
682
683         /* Second step. */
684         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state,
685                                      talloc_tos(),
686                                      blob, &response);
687
688         if (NT_STATUS_IS_OK(status)) {
689                 /* Return the context we're using for this encryption state. */
690                 if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
691                         return NT_STATUS_NO_MEMORY;
692                 }
693                 SSVAL(*pparam,0,ec->es->enc_ctx_num);
694                 *p_param_size = 2;
695         }
696
697         /* Return the raw blob. */
698         SAFE_FREE(*ppdata);
699         *ppdata = (unsigned char *)memdup(response.data, response.length);
700         if ((*ppdata) == NULL && response.length > 0)
701                 return NT_STATUS_NO_MEMORY;
702         *p_data_size = response.length;
703         data_blob_free(&response);
704         return status;
705 }
706
707 /******************************************************************************
708  Do the SPNEGO encryption negotiation. Parameters are in/out.
709 ******************************************************************************/
710
711 NTSTATUS srv_request_encryption_setup(connection_struct *conn,
712                                         unsigned char **ppdata,
713                                         size_t *p_data_size,
714                                         unsigned char **pparam,
715                                         size_t *p_param_size)
716 {
717         unsigned char *pdata = *ppdata;
718
719         SAFE_FREE(*pparam);
720         *p_param_size = 0;
721
722         if (*p_data_size < 1) {
723                 return NT_STATUS_INVALID_PARAMETER;
724         }
725
726         if (pdata[0] == ASN1_APPLICATION(0)) {
727                 /* its a negTokenTarg packet */
728                 return srv_enc_spnego_negotiate(conn, ppdata, p_data_size, pparam, p_param_size);
729         }
730
731         if (pdata[0] == ASN1_CONTEXT(1)) {
732                 /* It's an auth packet */
733                 return srv_enc_spnego_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
734         }
735
736         /* Maybe it's a raw unwrapped auth ? */
737         if (*p_data_size < 7) {
738                 return NT_STATUS_INVALID_PARAMETER;
739         }
740
741         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
742                 return srv_enc_raw_ntlm_auth(conn, ppdata, p_data_size, pparam, p_param_size);
743         }
744
745         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
746
747         return NT_STATUS_LOGON_FAILURE;
748 }
749
750 /******************************************************************************
751  Negotiation was successful - turn on server-side encryption.
752 ******************************************************************************/
753
754 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
755 {
756         if (!ec || !ec->es) {
757                 return NT_STATUS_LOGON_FAILURE;
758         }
759
760         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
761                 if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
762                         return NT_STATUS_INVALID_PARAMETER;
763                 }
764
765                 if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
766                         return NT_STATUS_INVALID_PARAMETER;
767                 }
768         }
769         /* Todo - check gssapi case. */
770
771         return NT_STATUS_OK;
772 }
773
774 /******************************************************************************
775  Negotiation was successful - turn on server-side encryption.
776 ******************************************************************************/
777
778 NTSTATUS srv_encryption_start(connection_struct *conn)
779 {
780         NTSTATUS status;
781
782         /* Check that we are really doing sign+seal. */
783         status = check_enc_good(partial_srv_trans_enc_ctx);
784         if (!NT_STATUS_IS_OK(status)) {
785                 return status;
786         }
787         /* Throw away the context we're using currently (if any). */
788         srv_free_encryption_context(&srv_trans_enc_ctx);
789
790         /* Steal the partial pointer. Deliberate shallow copy. */
791         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
792         srv_trans_enc_ctx->es->enc_on = true;
793
794         partial_srv_trans_enc_ctx = NULL;
795
796         DEBUG(1,("srv_encryption_start: context negotiated\n"));
797         return NT_STATUS_OK;
798 }
799
800 /******************************************************************************
801  Shutdown all server contexts.
802 ******************************************************************************/
803
804 void server_encryption_shutdown(void)
805 {
806         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
807         srv_free_encryption_context(&srv_trans_enc_ctx);
808 }