f5a091cd784c413bfa1f09885fe0e8145057f6ee
[samba.git] / source / auth / gensec / spnego.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    RFC2478 Compliant SPNEGO implementation
5    
6    Copyright (C) Jim McDonough <jmcd@us.ibm.com>      2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "auth/auth.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_AUTH
30
31 enum spnego_state_position {
32         SPNEGO_SERVER_START,
33         SPNEGO_CLIENT_START,
34         SPNEGO_SERVER_TARG,
35         SPNEGO_CLIENT_TARG,
36         SPNEGO_FALLBACK,
37         SPNEGO_DONE
38 };
39
40 struct spnego_state {
41         uint_t ref_count;
42         enum spnego_message_type expected_packet;
43         enum spnego_state_position state_position;
44         struct gensec_security *sub_sec_security;
45         BOOL no_response_expected;
46 };
47
48
49 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
50 {
51         struct spnego_state *spnego_state;
52
53         spnego_state = talloc(gensec_security, struct spnego_state);            
54         if (!spnego_state) {
55                 return NT_STATUS_NO_MEMORY;
56         }
57
58         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
59         spnego_state->state_position = SPNEGO_CLIENT_START;
60         spnego_state->sub_sec_security = NULL;
61         spnego_state->no_response_expected = False;
62
63         gensec_security->private_data = spnego_state;
64         return NT_STATUS_OK;
65 }
66
67 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
68 {
69         struct spnego_state *spnego_state;
70
71         spnego_state = talloc(gensec_security, struct spnego_state);            
72         if (!spnego_state) {
73                 return NT_STATUS_NO_MEMORY;
74         }
75
76         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
77         spnego_state->state_position = SPNEGO_SERVER_START;
78         spnego_state->sub_sec_security = NULL;
79         spnego_state->no_response_expected = False;
80
81         gensec_security->private_data = spnego_state;
82         return NT_STATUS_OK;
83 }
84
85 /*
86   wrappers for the spnego_*() functions
87 */
88 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security, 
89                                             TALLOC_CTX *mem_ctx, 
90                                             uint8_t *data, size_t length, 
91                                             const uint8_t *whole_pdu, size_t pdu_length, 
92                                             DATA_BLOB *sig)
93 {
94         struct spnego_state *spnego_state = gensec_security->private_data;
95
96         if (spnego_state->state_position != SPNEGO_DONE 
97             && spnego_state->state_position != SPNEGO_FALLBACK) {
98                 return NT_STATUS_INVALID_PARAMETER;
99         }
100         
101         return gensec_unseal_packet(spnego_state->sub_sec_security, 
102                                     mem_ctx, 
103                                     data, length, 
104                                     whole_pdu, pdu_length,
105                                     sig); 
106 }
107
108 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security, 
109                                            TALLOC_CTX *mem_ctx, 
110                                            const uint8_t *data, size_t length, 
111                                            const uint8_t *whole_pdu, size_t pdu_length, 
112                                            const DATA_BLOB *sig)
113 {
114         struct spnego_state *spnego_state = gensec_security->private_data;
115
116         if (spnego_state->state_position != SPNEGO_DONE 
117             && spnego_state->state_position != SPNEGO_FALLBACK) {
118                 return NT_STATUS_INVALID_PARAMETER;
119         }
120         
121         return gensec_check_packet(spnego_state->sub_sec_security, 
122                                    mem_ctx, 
123                                    data, length, 
124                                    whole_pdu, pdu_length,
125                                    sig);
126 }
127
128 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security, 
129                                           TALLOC_CTX *mem_ctx, 
130                                           uint8_t *data, size_t length, 
131                                           const uint8_t *whole_pdu, size_t pdu_length, 
132                                           DATA_BLOB *sig)
133 {
134         struct spnego_state *spnego_state = gensec_security->private_data;
135
136         if (spnego_state->state_position != SPNEGO_DONE 
137             && spnego_state->state_position != SPNEGO_FALLBACK) {
138                 return NT_STATUS_INVALID_PARAMETER;
139         }
140         
141         return gensec_seal_packet(spnego_state->sub_sec_security, 
142                                   mem_ctx, 
143                                   data, length, 
144                                   whole_pdu, pdu_length,
145                                   sig);
146 }
147
148 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security, 
149                                           TALLOC_CTX *mem_ctx, 
150                                           const uint8_t *data, size_t length, 
151                                           const uint8_t *whole_pdu, size_t pdu_length, 
152                                           DATA_BLOB *sig)
153 {
154         struct spnego_state *spnego_state = gensec_security->private_data;
155
156         if (spnego_state->state_position != SPNEGO_DONE 
157             && spnego_state->state_position != SPNEGO_FALLBACK) {
158                 return NT_STATUS_INVALID_PARAMETER;
159         }
160         
161         return gensec_sign_packet(spnego_state->sub_sec_security, 
162                                   mem_ctx, 
163                                   data, length, 
164                                   whole_pdu, pdu_length,
165                                   sig);
166 }
167
168 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security, 
169                                    TALLOC_CTX *mem_ctx, 
170                                    const DATA_BLOB *in, 
171                                    DATA_BLOB *out)
172 {
173         struct spnego_state *spnego_state = gensec_security->private_data;
174
175         if (spnego_state->state_position != SPNEGO_DONE 
176             && spnego_state->state_position != SPNEGO_FALLBACK) {
177                 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
178                 return NT_STATUS_INVALID_PARAMETER;
179         }
180         
181         return gensec_wrap(spnego_state->sub_sec_security, 
182                            mem_ctx, in, out);
183 }
184
185 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security, 
186                                      TALLOC_CTX *mem_ctx, 
187                                      const DATA_BLOB *in, 
188                                      DATA_BLOB *out)
189 {
190         struct spnego_state *spnego_state = gensec_security->private_data;
191
192         if (spnego_state->state_position != SPNEGO_DONE 
193             && spnego_state->state_position != SPNEGO_FALLBACK) {
194                 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
195                 return NT_STATUS_INVALID_PARAMETER;
196         }
197         
198         return gensec_unwrap(spnego_state->sub_sec_security, 
199                              mem_ctx, in, out);
200 }
201
202 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security) 
203 {
204         struct spnego_state *spnego_state = gensec_security->private_data;
205
206         if (spnego_state->state_position != SPNEGO_DONE 
207             && spnego_state->state_position != SPNEGO_FALLBACK) {
208                 return 0;
209         }
210         
211         return gensec_sig_size(spnego_state->sub_sec_security);
212 }
213
214 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security, 
215                                           DATA_BLOB *session_key)
216 {
217         struct spnego_state *spnego_state = gensec_security->private_data;
218         if (!spnego_state->sub_sec_security) {
219                 return NT_STATUS_INVALID_PARAMETER;
220         }
221         
222         return gensec_session_key(spnego_state->sub_sec_security, 
223                                   session_key);
224 }
225
226 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
227                                                                       struct auth_session_info **session_info) 
228 {
229         struct spnego_state *spnego_state = gensec_security->private_data;
230         if (!spnego_state->sub_sec_security) {
231                 return NT_STATUS_INVALID_PARAMETER;
232         }
233         
234         return gensec_session_info(spnego_state->sub_sec_security, 
235                                    session_info);
236 }
237
238 /** Fallback to another GENSEC mechanism, based on magic strings 
239  *
240  * This is the 'fallback' case, where we don't get SPNEGO, and have to
241  * try all the other options (and hope they all have a magic string
242  * they check)
243 */
244
245 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security, 
246                                                   struct spnego_state *spnego_state,
247                                                   TALLOC_CTX *out_mem_ctx, 
248                                                   const DATA_BLOB in, DATA_BLOB *out) 
249 {
250         int i;
251         int num_ops;
252         const struct gensec_security_ops **all_ops = gensec_security_all(&num_ops);
253         for (i=0; i < num_ops; i++) {
254                 NTSTATUS nt_status;
255                 if (!all_ops[i]->oid) {
256                         continue;
257                 }
258                 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid) == 0) {
259                         continue;
260                 }
261
262                 nt_status = gensec_subcontext_start(spnego_state, 
263                                                     gensec_security, 
264                                                     &spnego_state->sub_sec_security);
265                 if (!NT_STATUS_IS_OK(nt_status)) {
266                         return nt_status;
267                 }
268                 /* select the sub context */
269                 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
270                                                      all_ops[i]->oid);
271                 if (!NT_STATUS_IS_OK(nt_status)) {
272                         talloc_free(spnego_state->sub_sec_security);
273                         spnego_state->sub_sec_security = NULL;
274                         continue;
275                 }
276                 nt_status = gensec_update(spnego_state->sub_sec_security,
277                                                           out_mem_ctx, in, out);
278                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
279                         spnego_state->state_position = SPNEGO_FALLBACK;
280                         return nt_status;
281                 }
282                 talloc_free(spnego_state->sub_sec_security);
283                 spnego_state->sub_sec_security = NULL;
284         }
285         DEBUG(1, ("Failed to parse SPNEGO request\n"));
286         return NT_STATUS_INVALID_PARAMETER;
287         
288 }
289
290 /* 
291    Parse the netTokenInit from the client, to the server.  
292
293    
294 */
295
296 static NTSTATUS gensec_spnego_server_parse_negTokenInit(struct gensec_security *gensec_security,
297                                                         struct spnego_state *spnego_state, 
298                                                         TALLOC_CTX *out_mem_ctx, 
299                                                         const char **mechType,
300                                                         const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out) 
301 {
302         NTSTATUS nt_status;
303
304         if (!mechType || !mechType[0]) {
305                 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
306                 return NT_STATUS_INVALID_PARAMETER;
307         }
308
309         nt_status = gensec_subcontext_start(spnego_state,
310                                             gensec_security,
311                                             &spnego_state->sub_sec_security);
312         if (!NT_STATUS_IS_OK(nt_status)) {
313                 return nt_status;
314         }
315         /* select the sub context */
316         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
317                                              mechType[0]);
318         if (!NT_STATUS_IS_OK(nt_status)) {
319                 talloc_free(spnego_state->sub_sec_security);
320                 spnego_state->sub_sec_security = NULL;
321                 return nt_status;
322         }
323
324         if (!unwrapped_in.length) {
325                 return NT_STATUS_INVALID_PARAMETER;
326         }
327                 
328         nt_status = gensec_update(spnego_state->sub_sec_security,
329                                   out_mem_ctx, 
330                                   unwrapped_in,
331                                   unwrapped_out);
332
333         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
334                 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
335                           spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
336                 talloc_free(spnego_state->sub_sec_security);
337                 spnego_state->sub_sec_security = NULL;
338         }
339         return nt_status;
340 }
341
342 static NTSTATUS gensec_spnego_client_parse_negTokenInit(struct gensec_security *gensec_security,
343                                                         struct spnego_state *spnego_state, 
344                                                         TALLOC_CTX *out_mem_ctx, 
345                                                         const char **mechType,
346                                                         const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out) 
347 {
348         int i;
349         NTSTATUS nt_status;
350         DATA_BLOB null_data_blob = data_blob(NULL,0);
351
352         for (i=0; mechType && mechType[i]; i++) {
353                 nt_status = gensec_subcontext_start(spnego_state,
354                                                     gensec_security,
355                                                     &spnego_state->sub_sec_security);
356                 if (!NT_STATUS_IS_OK(nt_status)) {
357                         break;
358                 }
359                 /* select the sub context */
360                 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
361                                                      mechType[i]);
362                 if (!NT_STATUS_IS_OK(nt_status)) {
363                         talloc_free(spnego_state->sub_sec_security);
364                         spnego_state->sub_sec_security = NULL;
365                         continue;
366                 }
367                 
368                 if (i == 0) {
369                         nt_status = gensec_update(spnego_state->sub_sec_security,
370                                                   out_mem_ctx, 
371                                                   unwrapped_in,
372                                                   unwrapped_out);
373                 } else {
374                         /* only get the helping start blob for the first OID */
375                         nt_status = gensec_update(spnego_state->sub_sec_security,
376                                                   out_mem_ctx, 
377                                                   null_data_blob, 
378                                                   unwrapped_out);
379                 }
380                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
381                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
382                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
383                         talloc_free(spnego_state->sub_sec_security);
384                         spnego_state->sub_sec_security = NULL;
385                 }
386                 return nt_status;
387         }
388         if (!mechType || !mechType[i]) {
389                 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
390         }
391         return NT_STATUS_INVALID_PARAMETER;
392 }
393
394 /** create a client negTokenInit 
395  *
396  * This is the case, where the client is the first one who sends data
397 */
398
399 static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec_security, 
400                                                   struct spnego_state *spnego_state,
401                                                   TALLOC_CTX *out_mem_ctx, 
402                                                   const DATA_BLOB in, DATA_BLOB *out) 
403 {
404         DATA_BLOB null_data_blob = data_blob(NULL,0);
405         NTSTATUS nt_status;
406         const char **mechTypes = NULL;
407         DATA_BLOB unwrapped_out = data_blob(NULL,0);
408
409         mechTypes = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
410
411         if (!mechTypes) {
412                 DEBUG(1, ("no GENSEC OID backends available\n"));
413                 return NT_STATUS_INVALID_PARAMETER;
414         }
415
416         nt_status = gensec_subcontext_start(spnego_state, 
417                                             gensec_security, 
418                                             &spnego_state->sub_sec_security);
419         if (!NT_STATUS_IS_OK(nt_status)) {
420                 return nt_status;
421         }
422         /* select our preferred mech */
423         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
424                                              mechTypes[0]);
425         if (!NT_STATUS_IS_OK(nt_status)) {
426                 talloc_free(spnego_state->sub_sec_security);
427                 spnego_state->sub_sec_security = NULL;
428                 return nt_status;
429         }
430         nt_status = gensec_update(spnego_state->sub_sec_security,
431                                   out_mem_ctx, in, &unwrapped_out);
432         if (NT_STATUS_IS_OK(nt_status) || NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
433                 struct spnego_data spnego_out;
434                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
435                 spnego_out.negTokenInit.mechTypes = mechTypes;
436                 spnego_out.negTokenInit.reqFlags = 0;
437                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
438                 spnego_out.negTokenInit.mechToken = unwrapped_out;
439                 
440                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
441                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
442                                 return NT_STATUS_INVALID_PARAMETER;
443                 }
444                 
445                 /* set next state */
446                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
447                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
448                 
449                 if (NT_STATUS_IS_OK(nt_status)) {
450                         spnego_state->no_response_expected = True;
451                 }
452
453                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
454         } 
455         talloc_free(spnego_state->sub_sec_security);
456         spnego_state->sub_sec_security = NULL;
457
458         DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
459         return NT_STATUS_INVALID_PARAMETER;
460 }
461
462
463 /** create a client negTokenTarg 
464  *
465  * This is the case, where the client is the first one who sends data
466 */
467
468 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security, 
469                                                   struct spnego_state *spnego_state,
470                                                   TALLOC_CTX *out_mem_ctx, 
471                                                   NTSTATUS nt_status,
472                                                   const DATA_BLOB unwrapped_out, DATA_BLOB *out) 
473 {
474         struct spnego_data spnego_out;
475         DATA_BLOB null_data_blob = data_blob(NULL, 0);
476
477         /* compose reply */
478         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
479         spnego_out.negTokenTarg.responseToken = unwrapped_out;
480         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
481         spnego_out.negTokenTarg.supportedMech = NULL;
482
483         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
484                 spnego_out.negTokenTarg.supportedMech
485                         = spnego_state->sub_sec_security->ops->oid;
486                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
487                 spnego_state->state_position = SPNEGO_SERVER_TARG;
488         } else if (NT_STATUS_IS_OK(nt_status)) {
489                 if (unwrapped_out.data) {
490                         spnego_out.negTokenTarg.supportedMech
491                                 = spnego_state->sub_sec_security->ops->oid;
492                 }
493                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
494                 spnego_state->state_position = SPNEGO_DONE;
495         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
496                 if (spnego_state->sub_sec_security) {
497                         /* we have a mech, but we just didn't get the input parameter */
498                         spnego_out.negTokenTarg.supportedMech
499                                 = spnego_state->sub_sec_security->ops->oid;
500                 } else {
501                         const char **mechTypes = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
502                         if (!mechTypes) {
503                                 DEBUG(1, ("no GENSEC OID backends available\n"));
504                                 return NT_STATUS_INVALID_PARAMETER;
505                         }
506
507                         nt_status = gensec_subcontext_start(spnego_state, 
508                                                             gensec_security, 
509                                                             &spnego_state->sub_sec_security);
510                         if (!NT_STATUS_IS_OK(nt_status)) {
511                                 return nt_status;
512                         }
513                         /* select our preferred mech */
514                         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
515                                                              mechTypes[0]);
516                         if (!NT_STATUS_IS_OK(nt_status)) {
517                                 talloc_free(spnego_state->sub_sec_security);
518                                 spnego_state->sub_sec_security = NULL;
519                                 return nt_status;
520                         }
521
522                         /* we should be sending the whole list here */
523                         spnego_out.negTokenTarg.supportedMech = mechTypes[0];
524                 }
525                         
526                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
527                 spnego_state->state_position = SPNEGO_SERVER_TARG;
528                 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
529         } else {
530                 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
531                 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
532                 spnego_state->state_position = SPNEGO_DONE;
533         }
534
535         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
536                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
537                 return NT_STATUS_INVALID_PARAMETER;
538         }
539
540         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
541
542         return nt_status;
543 }
544
545
546 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
547                                      const DATA_BLOB in, DATA_BLOB *out) 
548 {
549         struct spnego_state *spnego_state = gensec_security->private_data;
550         DATA_BLOB null_data_blob = data_blob(NULL, 0);
551         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
552         struct spnego_data spnego_out;
553         struct spnego_data spnego;
554
555         ssize_t len;
556
557         *out = data_blob(NULL, 0);
558
559         if (!out_mem_ctx) {
560                 out_mem_ctx = spnego_state;
561         }
562
563         /* and switch into the state machine */
564
565         switch (spnego_state->state_position) {
566         case SPNEGO_FALLBACK:
567                 return gensec_update(spnego_state->sub_sec_security,
568                                      out_mem_ctx, in, out);
569         case SPNEGO_SERVER_START:
570         {
571                 if (in.length) {
572                         NTSTATUS nt_status;
573
574                         len = spnego_read_data(in, &spnego);
575                         if (len == -1) {
576                                 return gensec_spnego_server_try_fallback(gensec_security, spnego_state, out_mem_ctx, in, out);
577                         }
578                         /* client sent NegTargetInit, we send NegTokenTarg */
579
580                         /* OK, so it's real SPNEGO, check the packet's the one we expect */
581                         if (spnego.type != spnego_state->expected_packet) {
582                                 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
583                                           spnego_state->expected_packet));
584                                 dump_data(1, in.data, in.length);
585                                 spnego_free_data(&spnego);
586                                 return NT_STATUS_INVALID_PARAMETER;
587                         }
588                         
589                         nt_status = gensec_spnego_server_parse_negTokenInit(gensec_security,
590                                                                      spnego_state,
591                                                                      out_mem_ctx, 
592                                                                      spnego.negTokenInit.mechTypes,
593                                                                      spnego.negTokenInit.mechToken, 
594                                                                      &unwrapped_out);
595                         
596                         nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
597                                                                       spnego_state,
598                                                                       out_mem_ctx,
599                                                                       nt_status,
600                                                                       unwrapped_out, 
601                                                                       out);
602                         
603                         spnego_free_data(&spnego);
604                         
605                         return nt_status;
606                 } else {
607                         const char **mechlist = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
608
609                         spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
610                         spnego_out.negTokenInit.mechTypes = mechlist;
611                         spnego_out.negTokenInit.reqFlags = 0;
612                         spnego_out.negTokenInit.mechListMIC
613                                 = data_blob_string_const(talloc_asprintf(out_mem_ctx, "%s$@%s", lp_netbios_name(), lp_realm()));
614                         spnego_out.negTokenInit.mechToken = unwrapped_out;
615                         
616                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
617                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
618                                 return NT_STATUS_INVALID_PARAMETER;
619                         }
620                         
621                         /* set next state */
622                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
623                         spnego_state->state_position = SPNEGO_SERVER_TARG;
624                         
625                         return NT_STATUS_MORE_PROCESSING_REQUIRED;
626                 }
627         }
628         
629         case SPNEGO_CLIENT_START:
630         {
631                 /* The server offers a list of mechanisms */
632                 
633                 const char *my_mechs[] = {NULL, NULL};
634                 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
635
636                 if (!in.length) {
637                         /* client to produce negTokenInit */
638                         return gensec_spnego_client_negTokenInit(gensec_security, spnego_state, 
639                                                                  out_mem_ctx, in, out);
640                 }
641                 
642                 len = spnego_read_data(in, &spnego);
643                 
644                 if (len == -1) {
645                         DEBUG(1, ("Invalid SPNEGO request:\n"));
646                         dump_data(1, in.data, in.length);
647                         return NT_STATUS_INVALID_PARAMETER;
648                 }
649                 
650                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
651                 if (spnego.type != spnego_state->expected_packet) {
652                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
653                                   spnego_state->expected_packet));
654                         dump_data(1, in.data, in.length);
655                         spnego_free_data(&spnego);
656                         return NT_STATUS_INVALID_PARAMETER;
657                 }
658
659                 if (spnego.negTokenInit.targetPrincipal) {
660                         DEBUG(5, ("Server claims it's principal name is %s (ignored)\n", spnego.negTokenInit.targetPrincipal));
661                 }
662
663                 nt_status = gensec_spnego_client_parse_negTokenInit(gensec_security,
664                                                              spnego_state,
665                                                              out_mem_ctx, 
666                                                              spnego.negTokenInit.mechTypes,
667                                                              spnego.negTokenInit.mechToken, 
668                                                              &unwrapped_out);
669
670                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
671                         spnego_free_data(&spnego);
672                         return nt_status;
673                 }
674
675                 /* compose reply */
676                 my_mechs[0] = spnego_state->sub_sec_security->ops->oid;
677                 
678                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
679                 spnego_out.negTokenInit.mechTypes = my_mechs;
680                 spnego_out.negTokenInit.reqFlags = 0;
681                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
682                 spnego_out.negTokenInit.mechToken = unwrapped_out;
683                 
684                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
685                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
686                                 return NT_STATUS_INVALID_PARAMETER;
687                 }
688                 
689                 /* set next state */
690                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
691                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
692
693                 if (NT_STATUS_IS_OK(nt_status)) {
694                         spnego_state->no_response_expected = True;
695                 }
696                 
697                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
698         }
699         case SPNEGO_SERVER_TARG:
700         {
701                 NTSTATUS nt_status;
702                 if (!in.length) {
703                         return NT_STATUS_INVALID_PARAMETER;
704                 }
705                 
706                 len = spnego_read_data(in, &spnego);
707                 
708                 if (len == -1) {
709                         DEBUG(1, ("Invalid SPNEGO request:\n"));
710                         dump_data(1, in.data, in.length);
711                         return NT_STATUS_INVALID_PARAMETER;
712                 }
713                 
714                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
715                 if (spnego.type != spnego_state->expected_packet) {
716                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
717                                   spnego_state->expected_packet));
718                         dump_data(1, in.data, in.length);
719                         spnego_free_data(&spnego);
720                         return NT_STATUS_INVALID_PARAMETER;
721                 }
722
723                 if (!spnego_state->sub_sec_security) {
724                         DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
725                         return NT_STATUS_INVALID_PARAMETER;
726                 }
727
728                 nt_status = gensec_update(spnego_state->sub_sec_security,
729                                           out_mem_ctx, 
730                                           spnego.negTokenTarg.responseToken,
731                                           &unwrapped_out);
732
733                 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
734                                                               spnego_state,
735                                                               out_mem_ctx, 
736                                                               nt_status,
737                                                               unwrapped_out, 
738                                                               out);
739
740                 spnego_free_data(&spnego);
741                 
742                 return nt_status;
743         }
744         case SPNEGO_CLIENT_TARG:
745         {
746                 NTSTATUS nt_status;
747                 if (!in.length) {
748                         return NT_STATUS_INVALID_PARAMETER;
749                 }
750                 
751                 len = spnego_read_data(in, &spnego);
752                 
753                 if (len == -1) {
754                         DEBUG(1, ("Invalid SPNEGO request:\n"));
755                         dump_data(1, in.data, in.length);
756                         return NT_STATUS_INVALID_PARAMETER;
757                 }
758                 
759                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
760                 if (spnego.type != spnego_state->expected_packet) {
761                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
762                                   spnego_state->expected_packet));
763                         dump_data(1, in.data, in.length);
764                         spnego_free_data(&spnego);
765                         return NT_STATUS_INVALID_PARAMETER;
766                 }
767         
768                 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
769                         return NT_STATUS_ACCESS_DENIED;
770                 }
771
772                 if (spnego_state->no_response_expected) {
773                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
774                                 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
775                                 nt_status = NT_STATUS_INVALID_PARAMETER;
776                         } else if (spnego.negTokenTarg.responseToken.length) {
777                                 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
778                                 nt_status = NT_STATUS_INVALID_PARAMETER;
779                         } else {
780                                 nt_status = NT_STATUS_OK;
781                         }
782                 } else {
783                         nt_status = gensec_update(spnego_state->sub_sec_security,
784                                                   out_mem_ctx, 
785                                                   spnego.negTokenTarg.responseToken, 
786                                                   &unwrapped_out);
787
788                         if (NT_STATUS_IS_OK(nt_status)) {
789                                 spnego_state->no_response_expected = True;
790                         }
791                 } 
792                 
793                 spnego_free_data(&spnego);
794
795                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
796                         && !NT_STATUS_IS_OK(nt_status)) {
797                         DEBUG(1, ("SPNEGO(%s) login failed: %s\n", 
798                                   spnego_state->sub_sec_security->ops->name, 
799                                   nt_errstr(nt_status)));
800                         return nt_status;
801                 }
802
803                 if (unwrapped_out.length) {
804                         /* compose reply */
805                         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
806                         spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
807                         spnego_out.negTokenTarg.supportedMech = NULL;
808                         spnego_out.negTokenTarg.responseToken = unwrapped_out;
809                         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
810                         
811                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
812                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
813                                 return NT_STATUS_INVALID_PARAMETER;
814                         }
815                 
816                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
817                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
818                 } else {
819
820                         /* all done - server has accepted, and we agree */
821                         *out = null_data_blob;
822
823                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
824                                 /* unless of course it did not accept */
825                                 DEBUG(1,("gensec_update ok but not accepted\n"));
826                                 nt_status = NT_STATUS_INVALID_PARAMETER;
827                         }
828                 }
829                 
830                 spnego_state->state_position = SPNEGO_DONE;
831
832                 return nt_status;
833         }
834         case SPNEGO_DONE:
835                 return NT_STATUS_OK;
836         }
837         return NT_STATUS_INVALID_PARAMETER;
838 }
839
840 static BOOL gensec_spnego_have_feature(struct gensec_security *gensec_security,
841                                        uint32_t feature) 
842 {
843         struct spnego_state *spnego_state = gensec_security->private_data;
844         if (!spnego_state->sub_sec_security) {
845                 return False;
846         }
847         
848         return gensec_have_feature(spnego_state->sub_sec_security, 
849                                    feature);
850 }
851
852 static const struct gensec_security_ops gensec_spnego_security_ops = {
853         .name           = "spnego",
854         .sasl_name      = "GSS-SPNEGO",
855         .auth_type      = DCERPC_AUTH_TYPE_SPNEGO,
856         .oid            = GENSEC_OID_SPNEGO,
857         .client_start   = gensec_spnego_client_start,
858         .server_start   = gensec_spnego_server_start,
859         .update         = gensec_spnego_update,
860         .seal_packet    = gensec_spnego_seal_packet,
861         .sign_packet    = gensec_spnego_sign_packet,
862         .sig_size       = gensec_spnego_sig_size,
863         .check_packet   = gensec_spnego_check_packet,
864         .unseal_packet  = gensec_spnego_unseal_packet,
865         .wrap           = gensec_spnego_wrap,
866         .unwrap         = gensec_spnego_unwrap,
867         .session_key    = gensec_spnego_session_key,
868         .session_info   = gensec_spnego_session_info,
869         .have_feature   = gensec_spnego_have_feature,
870         .enabled        = True
871 };
872
873 NTSTATUS gensec_spnego_init(void)
874 {
875         NTSTATUS ret;
876         ret = gensec_register(&gensec_spnego_security_ops);
877         if (!NT_STATUS_IS_OK(ret)) {
878                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
879                         gensec_spnego_security_ops.name));
880                 return ret;
881         }
882
883         return ret;
884 }