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