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