r21925: Start to code up the gss acquire creds calls.
[tprouty/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->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->ntlmssp_state = NULL;
85         }
86 }
87
88 /******************************************************************************
89  Shutdown a server encryption context.
90 ******************************************************************************/
91
92 static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
93 {
94         struct smb_srv_trans_enc_ctx *ec = *pp_ec;
95
96         if (!ec) {
97                 return;
98         }
99
100         if (ec->es) {
101                 switch (ec->es->smb_enc_type) {
102                         case SMB_TRANS_ENC_NTLM:
103                                 destroy_auth_ntlmssp(ec);
104                                 break;
105 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
106                         case SMB_TRANS_ENC_GSS:
107                                 break;
108 #endif
109                 }
110                 common_free_encryption_state(&ec->es);
111         }
112
113         SAFE_FREE(ec);
114         *pp_ec = NULL;
115 }
116
117 /******************************************************************************
118  Create a server encryption context.
119 ******************************************************************************/
120
121 static struct smb_srv_trans_enc_ctx *make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type)
122 {
123         struct smb_srv_trans_enc_ctx *ec;
124
125         ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
126         if (!ec) {
127                 return NULL;
128         }
129         ZERO_STRUCTP(partial_srv_trans_enc_ctx);
130         ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
131         if (!ec->es) {
132                 SAFE_FREE(ec);
133                 return NULL;
134         }
135         ZERO_STRUCTP(ec->es);
136         ec->es->smb_enc_type = smb_enc_type;
137         switch (smb_enc_type) {
138                 case SMB_TRANS_ENC_NTLM:
139                         {
140                                 NTSTATUS status = make_auth_ntlmssp(ec);
141                                 if (!NT_STATUS_IS_OK(status)) {
142                                         srv_free_encryption_context(&ec);
143                                         return NULL;
144                                 }
145                         }
146                         break;
147
148 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
149                 case SMB_TRANS_ENC_GSS:
150                         /* Acquire our credentials by calling gss_acquire_cred here. */
151                         break;
152 #endif
153                 default:
154                         srv_free_encryption_context(&ec);
155                         return NULL;
156         }
157         return ec;
158 }
159
160 /******************************************************************************
161  Free an encryption-allocated buffer.
162 ******************************************************************************/
163
164 void srv_free_enc_buffer(char *buf)
165 {
166         if (srv_trans_enc_ctx) {
167                 common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
168         }
169 }
170
171 /******************************************************************************
172  Decrypt an incoming buffer.
173 ******************************************************************************/
174
175 NTSTATUS srv_decrypt_buffer(char *buf)
176 {
177         if (srv_trans_enc_ctx) {
178                 return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
179         }
180         return NT_STATUS_OK;
181 }
182
183 /******************************************************************************
184  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
185 ******************************************************************************/
186
187 NTSTATUS srv_encrypt_buffer(char *buffer, char **buf_out)
188 {
189         if (srv_trans_enc_ctx) {
190                 return common_encrypt_buffer(srv_trans_enc_ctx->es, buffer, buf_out);
191         }
192         /* Not encrypting. */
193         *buf_out = buffer;
194         return NT_STATUS_OK;
195 }
196
197 /******************************************************************************
198  Do the gss encryption negotiation. Parameters are in/out.
199  Until success we do everything on the partial enc ctx.
200 ******************************************************************************/
201
202 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
203 static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
204 {
205         if (!partial_srv_trans_enc_ctx) {
206                 partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_GSS);
207                 if (!partial_srv_trans_enc_ctx) {
208                         return NT_STATUS_NO_MEMORY;
209                 }
210         }
211
212         return NT_STATUS_NOT_SUPPORTED;
213 }
214 #endif
215
216 /******************************************************************************
217  Do the NTLM SPNEGO (or raw) encryption negotiation. Parameters are in/out.
218  Until success we do everything on the partial enc ctx.
219 ******************************************************************************/
220
221 static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, BOOL spnego_wrap)
222 {
223         NTSTATUS status;
224         DATA_BLOB chal = data_blob(NULL, 0);
225         DATA_BLOB response = data_blob(NULL, 0);
226
227         partial_srv_trans_enc_ctx = make_srv_encryption_context(SMB_TRANS_ENC_NTLM);
228         if (!partial_srv_trans_enc_ctx) {
229                 return NT_STATUS_NO_MEMORY;
230         }
231
232         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
233
234         /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
235          * for success ... */
236
237         if (spnego_wrap) {
238                 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
239                 data_blob_free(&chal);
240         } else {
241                 /* Return the raw blob. */
242                 response = chal;
243         }
244
245         SAFE_FREE(*ppdata);
246         *ppdata = response.data;
247         *p_data_size = response.length;
248         return status;
249 }
250
251 /******************************************************************************
252  Do the SPNEGO encryption negotiation. Parameters are in/out.
253  Based off code in smbd/sesssionsetup.c
254  Until success we do everything on the partial enc ctx.
255 ******************************************************************************/
256
257 static NTSTATUS srv_enc_spnego_negotiate(unsigned char **ppdata, size_t *p_data_size)
258 {
259         NTSTATUS status;
260         DATA_BLOB blob = data_blob(NULL,0);
261         DATA_BLOB secblob = data_blob(NULL, 0);
262         BOOL got_kerberos_mechanism = False;
263
264         blob = data_blob_const(*ppdata, *p_data_size);
265
266         status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
267         if (!NT_STATUS_IS_OK(status)) {
268                 return nt_status_squash(status);
269         }
270
271         /* We should have no partial context at this point. */
272
273         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
274
275 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
276         if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
277                 status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
278         } else 
279 #endif
280         {
281                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, True);
282         }
283
284         data_blob_free(&secblob);
285
286         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
287                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
288         }
289
290         return status;
291 }
292
293 /******************************************************************************
294  Complete a SPNEGO encryption negotiation. Parameters are in/out.
295  We only get this for a NTLM auth second stage.
296 ******************************************************************************/
297
298 static NTSTATUS srv_enc_spnego_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
299 {
300         NTSTATUS status;
301         DATA_BLOB blob = data_blob(NULL,0);
302         DATA_BLOB auth = data_blob(NULL,0);
303         DATA_BLOB auth_reply = data_blob(NULL,0);
304         DATA_BLOB response = data_blob(NULL,0);
305         struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
306
307         /* We must have a partial context here. */
308
309         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
310                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
311                 return NT_STATUS_INVALID_PARAMETER;
312         }
313
314         blob = data_blob_const(*ppdata, *p_data_size);
315         if (!spnego_parse_auth(blob, &auth)) {
316                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
317                 return NT_STATUS_INVALID_PARAMETER;
318         }
319
320         status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
321         data_blob_free(&auth);
322
323         response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
324         data_blob_free(&auth_reply);
325
326         SAFE_FREE(*ppdata);
327         *ppdata = response.data;
328         *p_data_size = response.length;
329         return status;
330 }
331
332 /******************************************************************************
333  Raw NTLM encryption negotiation. Parameters are in/out.
334  This function does both steps.
335 ******************************************************************************/
336
337 static NTSTATUS srv_enc_raw_ntlm_auth(unsigned char **ppdata, size_t *p_data_size)
338 {
339         NTSTATUS status;
340         DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
341         DATA_BLOB response = data_blob(NULL,0);
342         struct smb_srv_trans_enc_ctx *ec;
343
344         if (!partial_srv_trans_enc_ctx) {
345                 /* This is the initial step. */
346                 status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, False);
347                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
348                         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
349                         return nt_status_squash(status);
350                 }
351                 return status;
352         }
353
354         ec = partial_srv_trans_enc_ctx;
355         if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
356                 srv_free_encryption_context(&partial_srv_trans_enc_ctx);
357                 return NT_STATUS_INVALID_PARAMETER;
358         }
359
360         /* Second step. */
361         status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
362
363         /* Return the raw blob. */
364         SAFE_FREE(*ppdata);
365         *ppdata = response.data;
366         *p_data_size = response.length;
367         return status;
368 }
369
370 /******************************************************************************
371  Do the SPNEGO encryption negotiation. Parameters are in/out.
372 ******************************************************************************/
373
374 NTSTATUS srv_request_encryption_setup(unsigned char **ppdata, size_t *p_data_size)
375 {
376         unsigned char *pdata = *ppdata;
377
378         if (*p_data_size < 1) {
379                 return NT_STATUS_INVALID_PARAMETER;
380         }
381
382         if (pdata[0] == ASN1_APPLICATION(0)) {
383                 /* 
384                  * Until success we do everything on the partial
385                  * enc state.
386                  */
387                 /* its a negTokenTarg packet */
388                 return srv_enc_spnego_negotiate(ppdata, p_data_size);
389         }
390
391         if (pdata[0] == ASN1_CONTEXT(1)) {
392                 /* It's an auth packet */
393                 return srv_enc_spnego_ntlm_auth(ppdata, p_data_size);
394         }
395
396         /* Maybe it's a raw unwrapped auth ? */
397         if (*p_data_size < 7) {
398                 return NT_STATUS_INVALID_PARAMETER;
399         }
400
401         if (strncmp((char *)pdata, "NTLMSSP", 7) == 0) {
402                 return srv_enc_raw_ntlm_auth(ppdata, p_data_size);
403         }
404
405         DEBUG(1,("srv_request_encryption_setup: Unknown packet\n"));
406
407         return NT_STATUS_LOGON_FAILURE;
408 }
409
410 /******************************************************************************
411  Negotiation was successful - turn on server-side encryption.
412 ******************************************************************************/
413
414 static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
415 {
416         if (!ec || !ec->es) {
417                 return NT_STATUS_LOGON_FAILURE;
418         }
419
420         if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
421                 if ((ec->es->ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
422                                 (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
423                         return NT_STATUS_INVALID_PARAMETER;
424                 }
425         }
426         /* Todo - check gssapi case. */
427
428         return NT_STATUS_OK;
429 }
430
431 /******************************************************************************
432  Negotiation was successful - turn on server-side encryption.
433 ******************************************************************************/
434
435 NTSTATUS srv_encryption_start(void)
436 {
437         NTSTATUS status;
438
439         /* Check that we are really doing sign+seal. */
440         status = check_enc_good(partial_srv_trans_enc_ctx);
441         if (!NT_STATUS_IS_OK(status)) {
442                 return status;
443         }
444         /* Throw away the context we're using currently (if any). */
445         srv_free_encryption_context(&srv_trans_enc_ctx);
446
447         /* Steal the partial pointer. Deliberate shallow copy. */
448         srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
449         srv_trans_enc_ctx->es->enc_on = True;
450
451         partial_srv_trans_enc_ctx = NULL;
452         return NT_STATUS_OK;
453 }
454
455 /******************************************************************************
456  Shutdown all server contexts.
457 ******************************************************************************/
458
459 void server_encryption_shutdown(void)
460 {
461         srv_free_encryption_context(&partial_srv_trans_enc_ctx);
462         srv_free_encryption_context(&srv_trans_enc_ctx);
463 }