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