r21969: Start working on the gss-side of the server negotiation.
[samba.git] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /******************************************************************************
24  Server side encryption.
25 ******************************************************************************/
26
27 /******************************************************************************
28  Global server state.
29 ******************************************************************************/
30
31 struct smb_srv_trans_enc_ctx {
32         struct smb_trans_enc_state *es;
33         AUTH_NTLMSSP_STATE *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
34 };
35
36 static struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
37 static struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
38
39 /******************************************************************************
40  Is server encryption on ?
41 ******************************************************************************/
42
43 BOOL srv_encryption_on(void)
44 {
45         if (srv_trans_enc_ctx) {
46                 return common_encryption_on(srv_trans_enc_ctx->es);
47         }
48         return False;
49 }
50
51 /******************************************************************************
52  Create an auth_ntlmssp_state and ensure pointer copy is correct.
53 ******************************************************************************/
54
55 static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
56 {
57         NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
58         if (!NT_STATUS_IS_OK(status)) {
59                 return nt_status_squash(status);
60         }
61
62         /*
63          * We must remember to update the pointer copy for the common
64          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
65          */
66         ec->es->s.ntlmssp_state = ec->auth_ntlmssp_state->ntlmssp_state;
67         return status;
68 }
69
70 /******************************************************************************
71  Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
72 ******************************************************************************/
73
74 static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
75 {
76         /*
77          * We must remember to update the pointer copy for the common
78          * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
79          */
80
81         if (ec->auth_ntlmssp_state) {
82                 auth_ntlmssp_end(&ec->auth_ntlmssp_state);
83                 /* The auth_ntlmssp_end killed this already. */
84                 ec->es->s.ntlmssp_state = NULL;
85         }
86 }
87
88 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
89
90 /******************************************************************************
91  Import a name.
92 ******************************************************************************/
93
94 static NTSTATUS get_gss_creds(const char *service,
95                                 const char *name,
96                                 gss_cred_usage_t cred_type,
97                                 gss_cred_id_t *p_srv_cred)
98 {
99         OM_uint32 ret;
100         OM_uint32 min;
101         gss_name_t srv_name;
102         gss_buffer_desc input_name;
103         char *host_princ_s = NULL;
104         NTSTATUS status = NT_STATUS_OK;
105
106         asprintf(&host_princ_s, "%s@%s", service, name);
107         if (host_princ_s == NULL) {
108                 return NT_STATUS_NO_MEMORY;
109         }
110
111         input_name.value = host_princ_s;
112         input_name.length = strlen(host_princ_s) + 1;
113
114         ret = gss_import_name(&min,
115                                 &input_name,
116                                 GSS_C_NT_HOSTBASED_SERVICE,
117                                 &srv_name);
118
119         if (ret != GSS_S_COMPLETE) {
120                 SAFE_FREE(host_princ_s);
121                 return map_nt_error_from_gss(ret, min);
122         }
123
124         ret = gss_acquire_cred(&min,
125                                 &srv_name,
126                                 GSS_C_INDEFINITE,
127                                 GSS_C_NULL_OID_SET,
128                                 cred_type,
129                                 p_srv_cred,
130                                 NULL,
131                                 NULL);
132
133         if (ret != GSS_S_COMPLETE) {
134                 status = map_nt_error_from_gss(ret, min);
135         }
136
137         SAFE_FREE(host_princ_s);
138         gss_release_name(&min, &srv_name);
139         return status;
140 }
141
142 /******************************************************************************
143  Create a gss state.
144 ******************************************************************************/
145
146 static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
147 {
148         NTSTATUS status;
149         gss_cred_id_t srv_cred;
150         fstring fqdn;
151
152         name_to_fqdn(fqdn, global_myname());
153         strlower_m(fqdn);
154
155         status = get_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
156         if (!NT_STATUS_IS_OK(status)) {
157                 status = get_gss_creds("host", fqdn, GSS_C_ACCEPT, &srv_cred);
158                 if (!NT_STATUS_IS_OK(status)) {
159                         return nt_status_squash(status);
160                 }
161         }
162
163         return NT_STATUS_OK;
164 }
165 #endif
166
167 /******************************************************************************
168  Shutdown a server encryption context.
169 ******************************************************************************/
170
171 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
172 {
173         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
174
175         if (!ec) {
176                 return;
177         }
178
179         if (ec->es) {
180                 switch (ec->es->smb_enc_type) {
181                         case SMB_TRANS_ENC_NTLM:
182                                 destroy_auth_ntlmssp(ec);
183                                 break;
184 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
185                         case SMB_TRANS_ENC_GSS:
186                                 break;
187 #endif
188                 }
189                 common_free_encryption_state(&ec->es);
190         }
191
192         SAFE_FREE(ec);
193         *pp_ec = NULL;
194 }
195
196 /******************************************************************************
197  Create a server encryption context.
198 ******************************************************************************/
199
200 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
201 {
202         struct smb_srv_trans_enc_ctx *ec;
203
204         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
205         if (!ec) {
206                 return NULL;
207         }
208         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
209         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
210         if (!ec->es) {
211                 SAFE_FREE(ec);
212                 return NULL;
213         }
214         ZERO_STRUCTP(ec->es);
215         ec->es->smb_enc_type = smb_enc_type;
216         switch (smb_enc_type) {
217                 case SMB_TRANS_ENC_NTLM:
218                         {
219                                 NTSTATUS status = make_auth_ntlmssp(ec);
220                                 if (!NT_STATUS_IS_OK(status)) {
221                                         srv_free_encryption_context(&ec);
222                                         return NULL;
223                                 }
224                         }
225                         break;
226
227 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
228                 case SMB_TRANS_ENC_GSS:
229                         /* Acquire our credentials by calling gss_acquire_cred here. */
230                         {
231                                 NTSTATUS status = make_auth_gss(ec);
232                                 if (!NT_STATUS_IS_OK(status)) {
233                                         srv_free_encryption_context(&ec);
234                                         return NULL;
235                                 }
236                         }
237                         break;
238 #endif
239                 default:
240                         srv_free_encryption_context(&ec);
241                         return NULL;
242         }
243         return ec;
244 }
245
246 /******************************************************************************
247  Free an encryption-allocated buffer.
248 ******************************************************************************/
249
250 void srv_free_enc_buffer(char *buf)
251 {
252         if (srv_trans_enc_ctx) {
253                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
254         }
255 }
256
257 /******************************************************************************
258  Decrypt an incoming buffer.
259 ******************************************************************************/
260
261 NTSTATUS srv_decrypt_buffer(char *buf)
262 {
263         if (srv_trans_enc_ctx) {
264                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
265         }
266         return NT_STATUS_OK;
267 }
268
269 /******************************************************************************
270  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
271 ******************************************************************************/
272
273 NTSTATUS srv_encrypt_buffer(char *buffer, char **buf_out)
274 {
275         if (srv_trans_enc_ctx) {
276                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buffer, buf_out);
277         }
278         /* Not encrypting. */
279         *buf_out = buffer;
280         return NT_STATUS_OK;
281 }
282
283 /******************************************************************************
284  Do the gss encryption negotiation. Parameters are in/out.
285  Until success we do everything on the partial enc ctx.
286 ******************************************************************************/
287
288 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
289 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
290 {
291         if (!partial_srv_trans_enc_ctx) {
292                 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_GSS);
293                 if (!partial_srv_trans_enc_ctx) {
294                         return NT_STATUS_NO_MEMORY;
295                 }
296         }
297
298         return NT_STATUS_NOT_SUPPORTED;
299 }
300 #endif
301
302 /******************************************************************************
303  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
304  Until success we do everything on the partial enc ctx.
305 ******************************************************************************/
306
307 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
308 {
309         NTSTATUS status;
310         DATA_BLOB chal = data_blob(NULL, 0);
311         DATA_BLOB response = data_blob(NULL, 0);
312
313         partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
314         if (!partial_srv_trans_enc_ctx) {
315                 return NT_STATUS_NO_MEMORY;
316         }
317
318         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
319
320         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
321          * for success ... */
322
323         if (spnego_wrap) {
324                 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
325                 data_blob_free(&chal);
326         } else {
327                 /* Return the raw blob. */
328                 response = chal;
329         }
330
331         SAFE_FREE(*ppdata);
332         *ppdata = response.data;
333         *p_data_size = response.length;
334         return status;
335 }
336
337 /******************************************************************************
338  Do the SPNEGO encryption negotiation. Parameters are in/out.
339  Based off code in smbd/sesssionsetup.c
340  Until success we do everything on the partial enc ctx.
341 ******************************************************************************/
342
343 static NTSTATUS srv_enc_spnego_negotiate(unsigned char **ppdata, size_t *p_data_size)
344 {
345         NTSTATUS status;
346         DATA_BLOB blob = data_blob(NULL,0);
347         DATA_BLOB secblob = data_blob(NULL, 0);
348         BOOL got_kerberos_mechanism = False;
349
350         blob = data_blob_const(*ppdata, *p_data_size);
351
352         status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
353         if (!NT_STATUS_IS_OK(status)) {
354                 return nt_status_squash(status);
355         }
356
357         /* We should have no partial context at this point. */
358
359         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
360
361 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
362         if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
363                 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
364         } else 
365 #endif
366         {
367                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
368         }
369
370         data_blob_free(&secblob);
371
372         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
373                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
374         }
375
376         return status;
377 }
378
379 /******************************************************************************
380  Complete a SPNEGO encryption negotiation. Parameters are in/out.
381  We only get this for a NTLM auth second stage.
382 ******************************************************************************/
383
384 static NTSTATUS srv_enc_spnego_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
385 {
386         NTSTATUS status;
387         DATA_BLOB blob = data_blob(NULL,0);
388         DATA_BLOB auth = data_blob(NULL,0);
389         DATA_BLOB auth_reply = data_blob(NULL,0);
390         DATA_BLOB response = data_blob(NULL,0);
391         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
392
393         /* We must have a partial context here. */
394
395         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
396                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
397                 return NT_STATUS_INVALID_PARAMETER;
398         }
399
400         blob = data_blob_const(*ppdata, *p_data_size);
401         if (!spnego_parse_auth(blob, &auth)) {
402                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
403                 return NT_STATUS_INVALID_PARAMETER;
404         }
405
406         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
407         data_blob_free(&auth);
408
409         response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
410         data_blob_free(&auth_reply);
411
412         SAFE_FREE(*ppdata);
413         *ppdata = response.data;
414         *p_data_size = response.length;
415         return status;
416 }
417
418 /******************************************************************************
419  Raw NTLM encryption negotiation. Parameters are in/out.
420  This function does both steps.
421 ******************************************************************************/
422
423 static NTSTATUS srv_enc_raw_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
424 {
425         NTSTATUS status;
426         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
427         DATA_BLOB response = data_blob(NULL,0);
428         struct smb_srv_trans_enc_ctx *ec;
429
430         if (!partial_srv_trans_enc_ctx) {
431                 /* This is the initial step. */
432                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
433                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
434                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
435                         return nt_status_squash(status);
436                 }
437                 return status;
438         }
439
440         ec = partial_srv_trans_enc_ctx;
441         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
442                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
443                 return NT_STATUS_INVALID_PARAMETER;
444         }
445
446         /* Second step. */
447         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
448
449         /* Return the raw blob. */
450         SAFE_FREE(*ppdata);
451         *ppdata = response.data;
452         *p_data_size = response.length;
453         return status;
454 }
455
456 /******************************************************************************
457  Do the SPNEGO encryption negotiation. Parameters are in/out.
458 ******************************************************************************/
459
460 NTSTATUS srv_request_encryption_setup(unsigned char **ppdata, size_t *p_data_size)
461 {
462         unsigned char *pdata = *ppdata;
463
464         if (*p_data_size < 1) {
465                 return NT_STATUS_INVALID_PARAMETER;
466         }
467
468         if (pdata[0] == ASN1_APPLICATION(0)) {
469                 /* 
470                  * Until success we do everything on the partial
471                  * enc state.
472                  */
473                 /* its a negTokenTarg packet */
474                 return srv_enc_spnego_negotiate(ppdata, p_data_size);
475         }
476
477         if (pdata[0] == ASN1_CONTEXT(1)) {
478                 /* It's an auth packet */
479                 return srv_enc_spnego_ntlm_auth(ppdata, p_data_size);
480         }
481
482         /* Maybe it's a raw unwrapped auth ? */
483         if (*p_data_size < 7) {
484                 return NT_STATUS_INVALID_PARAMETER;
485         }
486
487         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
488                 return srv_enc_raw_ntlm_auth(ppdata, p_data_size);
489         }
490
491         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
492
493         return NT_STATUS_LOGON_FAILURE;
494 }
495
496 /******************************************************************************
497  Negotiation was successful - turn on server-side encryption.
498 ******************************************************************************/
499
500 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
501 {
502         if (!ec || !ec->es) {
503                 return NT_STATUS_LOGON_FAILURE;
504         }
505
506         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
507                 if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
508                                 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
509                         return NT_STATUS_INVALID_PARAMETER;
510                 }
511         }
512         /* Todo - check gssapi case. */
513
514         return NT_STATUS_OK;
515 }
516
517 /******************************************************************************
518  Negotiation was successful - turn on server-side encryption.
519 ******************************************************************************/
520
521 NTSTATUS srv_encryption_start(void)
522 {
523         NTSTATUS status;
524
525         /* Check that we are really doing sign+seal. */
526         status = check_enc_good(partial_srv_trans_enc_ctx);
527         if (!NT_STATUS_IS_OK(status)) {
528                 return status;
529         }
530         /* Throw away the context we're using currently (if any). */
531         srv_free_encryption_context(&srv_trans_enc_ctx);
532
533         /* Steal the partial pointer. Deliberate shallow copy. */
534         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
535         srv_trans_enc_ctx->es->enc_on = True;
536
537         partial_srv_trans_enc_ctx = NULL;
538         return NT_STATUS_OK;
539 }
540
541 /******************************************************************************
542  Shutdown all server contexts.
543 ******************************************************************************/
544
545 void server_encryption_shutdown(void)
546 {
547         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
548         srv_free_encryption_context(&srv_trans_enc_ctx);
549 }