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