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