8b4be6eb751f17d38ee349a10ecbbc364ee33ba4
[samba.git] / source / libcli / auth / 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_p(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_p(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 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
291                                                  struct spnego_state *spnego_state, 
292                                                  TALLOC_CTX *out_mem_ctx, 
293                                                  const char **mechType,
294                                                  const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out) 
295 {
296         int i;
297         NTSTATUS nt_status;
298         DATA_BLOB null_data_blob = data_blob(NULL,0);
299
300         for (i=0; mechType && mechType[i]; i++) {
301                 nt_status = gensec_subcontext_start(spnego_state,
302                                                     gensec_security,
303                                                     &spnego_state->sub_sec_security);
304                 if (!NT_STATUS_IS_OK(nt_status)) {
305                         break;
306                 }
307                 /* select the sub context */
308                 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
309                                                      mechType[i]);
310                 if (!NT_STATUS_IS_OK(nt_status)) {
311                         talloc_free(spnego_state->sub_sec_security);
312                         spnego_state->sub_sec_security = NULL;
313                         continue;
314                 }
315                 
316                 if (i == 0) {
317                         nt_status = gensec_update(spnego_state->sub_sec_security,
318                                                   out_mem_ctx, 
319                                                   unwrapped_in,
320                                                   unwrapped_out);
321                 } else {
322                         /* only get the helping start blob for the first OID */
323                         nt_status = gensec_update(spnego_state->sub_sec_security,
324                                                   out_mem_ctx, 
325                                                   null_data_blob, 
326                                                   unwrapped_out);
327                 }
328                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
329                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
330                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
331                         talloc_free(spnego_state->sub_sec_security);
332                         spnego_state->sub_sec_security = NULL;
333                 }
334                 return nt_status;
335         }
336         if (!mechType || !mechType[i]) {
337                 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
338         }
339         return NT_STATUS_INVALID_PARAMETER;
340 }
341
342 /** create a client negTokenInit 
343  *
344  * This is the case, where the client is the first one who sends data
345 */
346
347 static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec_security, 
348                                                   struct spnego_state *spnego_state,
349                                                   TALLOC_CTX *out_mem_ctx, 
350                                                   const DATA_BLOB in, DATA_BLOB *out) 
351 {
352         DATA_BLOB null_data_blob = data_blob(NULL,0);
353         NTSTATUS nt_status;
354         const char **mechTypes = NULL;
355         DATA_BLOB unwrapped_out = data_blob(NULL,0);
356
357         mechTypes = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
358
359         if (!mechTypes) {
360                 DEBUG(1, ("no GENSEC OID backends available\n"));
361                 return NT_STATUS_INVALID_PARAMETER;
362         }
363
364         nt_status = gensec_subcontext_start(spnego_state, 
365                                             gensec_security, 
366                                             &spnego_state->sub_sec_security);
367         if (!NT_STATUS_IS_OK(nt_status)) {
368                 return nt_status;
369         }
370         /* select our preferred mech */
371         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
372                                              mechTypes[0]);
373         if (!NT_STATUS_IS_OK(nt_status)) {
374                 talloc_free(spnego_state->sub_sec_security);
375                 spnego_state->sub_sec_security = NULL;
376                 return nt_status;
377         }
378         nt_status = gensec_update(spnego_state->sub_sec_security,
379                                   out_mem_ctx, in, &unwrapped_out);
380         if (NT_STATUS_IS_OK(nt_status) || NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
381                 struct spnego_data spnego_out;
382                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
383                 spnego_out.negTokenInit.mechTypes = mechTypes;
384                 spnego_out.negTokenInit.reqFlags = 0;
385                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
386                 spnego_out.negTokenInit.mechToken = unwrapped_out;
387                 
388                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
389                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
390                                 return NT_STATUS_INVALID_PARAMETER;
391                 }
392                 
393                 /* set next state */
394                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
395                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
396                 
397                 if (NT_STATUS_IS_OK(nt_status)) {
398                         spnego_state->no_response_expected = True;
399                 }
400
401                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
402         } 
403         talloc_free(spnego_state->sub_sec_security);
404         spnego_state->sub_sec_security = NULL;
405
406         DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
407         return NT_STATUS_INVALID_PARAMETER;
408 }
409
410
411 /** create a client negTokenTarg 
412  *
413  * This is the case, where the client is the first one who sends data
414 */
415
416 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security, 
417                                                   struct spnego_state *spnego_state,
418                                                   TALLOC_CTX *out_mem_ctx, 
419                                                   NTSTATUS nt_status,
420                                                   const DATA_BLOB unwrapped_out, DATA_BLOB *out) 
421 {
422         struct spnego_data spnego_out;
423         DATA_BLOB null_data_blob = data_blob(NULL, 0);
424
425         /* compose reply */
426         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
427         spnego_out.negTokenTarg.responseToken = unwrapped_out;
428         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
429         spnego_out.negTokenTarg.supportedMech = NULL;
430
431         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
432                 spnego_out.negTokenTarg.supportedMech
433                         = spnego_state->sub_sec_security->ops->oid;
434                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
435                 spnego_state->state_position = SPNEGO_SERVER_TARG;
436         } else if (NT_STATUS_IS_OK(nt_status)) {
437                 if (unwrapped_out.data) {
438                         spnego_out.negTokenTarg.supportedMech
439                                 = spnego_state->sub_sec_security->ops->oid;
440                 }
441                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
442                 spnego_state->state_position = SPNEGO_DONE;
443         } else {
444                 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
445                 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
446                 spnego_state->state_position = SPNEGO_DONE;
447         }
448
449         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
450                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
451                 return NT_STATUS_INVALID_PARAMETER;
452         }
453
454         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
455
456         return nt_status;
457 }
458
459
460 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
461                                      const DATA_BLOB in, DATA_BLOB *out) 
462 {
463         struct spnego_state *spnego_state = gensec_security->private_data;
464         DATA_BLOB null_data_blob = data_blob(NULL, 0);
465         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
466         struct spnego_data spnego_out;
467         struct spnego_data spnego;
468
469         ssize_t len;
470
471         *out = data_blob(NULL, 0);
472
473         if (!out_mem_ctx) {
474                 out_mem_ctx = spnego_state;
475         }
476
477         /* and switch into the state machine */
478
479         switch (spnego_state->state_position) {
480         case SPNEGO_FALLBACK:
481                 return gensec_update(spnego_state->sub_sec_security,
482                                      out_mem_ctx, in, out);
483         case SPNEGO_SERVER_START:
484         {
485                 if (in.length) {
486                         NTSTATUS nt_status;
487
488                         len = spnego_read_data(in, &spnego);
489                         if (len == -1) {
490                                 return gensec_spnego_server_try_fallback(gensec_security, spnego_state, out_mem_ctx, in, out);
491                         }
492                         /* client sent NegTargetInit, we send NegTokenTarg */
493
494                         /* OK, so it's real SPNEGO, check the packet's the one we expect */
495                         if (spnego.type != spnego_state->expected_packet) {
496                                 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
497                                           spnego_state->expected_packet));
498                                 dump_data(1, in.data, in.length);
499                                 spnego_free_data(&spnego);
500                                 return NT_STATUS_INVALID_PARAMETER;
501                         }
502                         
503                         nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
504                                                                      spnego_state,
505                                                                      out_mem_ctx, 
506                                                                      spnego.negTokenInit.mechTypes,
507                                                                      spnego.negTokenInit.mechToken, 
508                                                                      &unwrapped_out);
509                         
510                         nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
511                                                                       spnego_state,
512                                                                       out_mem_ctx,
513                                                                       nt_status,
514                                                                       unwrapped_out, 
515                                                                       out);
516                         
517                         spnego_free_data(&spnego);
518                         
519                         return nt_status;
520                 } else {
521                         const char **mechlist = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
522
523                         spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
524                         spnego_out.negTokenInit.mechTypes = mechlist;
525                         spnego_out.negTokenInit.reqFlags = 0;
526                         spnego_out.negTokenInit.mechListMIC
527                                 = data_blob_string_const(gensec_get_target_principal(gensec_security));
528                         spnego_out.negTokenInit.mechToken = unwrapped_out;
529                         
530                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
531                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
532                                 return NT_STATUS_INVALID_PARAMETER;
533                         }
534                         
535                         /* set next state */
536                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
537                         spnego_state->state_position = SPNEGO_SERVER_TARG;
538                         
539                         return NT_STATUS_MORE_PROCESSING_REQUIRED;
540                 }
541         }
542         
543         case SPNEGO_CLIENT_START:
544         {
545                 /* The server offers a list of mechanisms */
546                 
547                 const char *my_mechs[] = {NULL, NULL};
548                 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
549
550                 if (!in.length) {
551                         /* client to produce negTokenInit */
552                         return gensec_spnego_client_negTokenInit(gensec_security, spnego_state, 
553                                                                  out_mem_ctx, in, out);
554                 }
555                 
556                 len = spnego_read_data(in, &spnego);
557                 
558                 if (len == -1) {
559                         DEBUG(1, ("Invalid SPNEGO request:\n"));
560                         dump_data(1, in.data, in.length);
561                         return NT_STATUS_INVALID_PARAMETER;
562                 }
563                 
564                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
565                 if (spnego.type != spnego_state->expected_packet) {
566                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
567                                   spnego_state->expected_packet));
568                         dump_data(1, in.data, in.length);
569                         spnego_free_data(&spnego);
570                         return NT_STATUS_INVALID_PARAMETER;
571                 }
572
573                 if (spnego.negTokenInit.targetPrincipal) {
574                         DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
575                         nt_status = gensec_set_target_principal(gensec_security, 
576                                                                 spnego.negTokenInit.targetPrincipal);
577                         if (!NT_STATUS_IS_OK(nt_status)) {
578                                 spnego_free_data(&spnego);
579                                 return nt_status;
580                         }
581                 }
582
583                 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
584                                                              spnego_state,
585                                                              out_mem_ctx, 
586                                                              spnego.negTokenInit.mechTypes,
587                                                              spnego.negTokenInit.mechToken, 
588                                                              &unwrapped_out);
589
590                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
591                         spnego_free_data(&spnego);
592                         return nt_status;
593                 }
594
595                 /* compose reply */
596                 my_mechs[0] = spnego_state->sub_sec_security->ops->oid;
597                 
598                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
599                 spnego_out.negTokenInit.mechTypes = my_mechs;
600                 spnego_out.negTokenInit.reqFlags = 0;
601                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
602                 spnego_out.negTokenInit.mechToken = unwrapped_out;
603                 
604                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
605                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
606                                 return NT_STATUS_INVALID_PARAMETER;
607                 }
608                 
609                 /* set next state */
610                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
611                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
612
613                 if (NT_STATUS_IS_OK(nt_status)) {
614                         spnego_state->no_response_expected = True;
615                 }
616                 
617                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
618         }
619         case SPNEGO_SERVER_TARG:
620         {
621                 NTSTATUS nt_status;
622                 if (!in.length) {
623                         return NT_STATUS_INVALID_PARAMETER;
624                 }
625                 
626                 len = spnego_read_data(in, &spnego);
627                 
628                 if (len == -1) {
629                         DEBUG(1, ("Invalid SPNEGO request:\n"));
630                         dump_data(1, in.data, in.length);
631                         return NT_STATUS_INVALID_PARAMETER;
632                 }
633                 
634                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
635                 if (spnego.type != spnego_state->expected_packet) {
636                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
637                                   spnego_state->expected_packet));
638                         dump_data(1, in.data, in.length);
639                         spnego_free_data(&spnego);
640                         return NT_STATUS_INVALID_PARAMETER;
641                 }
642
643                 nt_status = gensec_update(spnego_state->sub_sec_security,
644                                           out_mem_ctx, 
645                                           spnego.negTokenTarg.responseToken,
646                                           &unwrapped_out);
647
648                 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
649                                                               spnego_state,
650                                                               out_mem_ctx, 
651                                                               nt_status,
652                                                               unwrapped_out, 
653                                                               out);
654
655                 spnego_free_data(&spnego);
656                 
657                 return nt_status;
658         }
659         case SPNEGO_CLIENT_TARG:
660         {
661                 NTSTATUS nt_status;
662                 if (!in.length) {
663                         return NT_STATUS_INVALID_PARAMETER;
664                 }
665                 
666                 len = spnego_read_data(in, &spnego);
667                 
668                 if (len == -1) {
669                         DEBUG(1, ("Invalid SPNEGO request:\n"));
670                         dump_data(1, in.data, in.length);
671                         return NT_STATUS_INVALID_PARAMETER;
672                 }
673                 
674                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
675                 if (spnego.type != spnego_state->expected_packet) {
676                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
677                                   spnego_state->expected_packet));
678                         dump_data(1, in.data, in.length);
679                         spnego_free_data(&spnego);
680                         return NT_STATUS_INVALID_PARAMETER;
681                 }
682         
683                 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
684                         return NT_STATUS_ACCESS_DENIED;
685                 }
686
687                 if (spnego_state->no_response_expected) {
688                         nt_status = NT_STATUS_OK;
689                 } else {
690                         nt_status = gensec_update(spnego_state->sub_sec_security,
691                                                   out_mem_ctx, 
692                                                   spnego.negTokenTarg.responseToken, 
693                                                   &unwrapped_out);
694                 } 
695                 
696                 
697                 if (NT_STATUS_IS_OK(nt_status) 
698                     && (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED)) {
699                         DEBUG(1,("gensec_update ok but not accepted\n"));
700                         nt_status = NT_STATUS_INVALID_PARAMETER;
701                 } 
702                 
703                 spnego_free_data(&spnego);
704
705                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
706                         /* compose reply */
707                         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
708                         spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
709                         spnego_out.negTokenTarg.supportedMech = NULL;
710                         spnego_out.negTokenTarg.responseToken = unwrapped_out;
711                         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
712                         
713                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
714                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
715                                 return NT_STATUS_INVALID_PARAMETER;
716                         }
717                 
718                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
719                 } else if (NT_STATUS_IS_OK(nt_status)) {
720                         /* all done - server has accepted, and we agree */
721                         
722                         if (unwrapped_out.length) {
723                                 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
724                                 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
725                                 spnego_out.negTokenTarg.supportedMech = NULL;
726                                 spnego_out.negTokenTarg.responseToken = unwrapped_out;
727                                 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
728                                 
729                                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
730                                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
731                                         return NT_STATUS_INVALID_PARAMETER;
732                                 }
733                         } else {
734                                 *out = null_data_blob;
735                         }
736
737                         spnego_state->state_position = SPNEGO_DONE;
738                 } else {
739                         DEBUG(1, ("SPNEGO(%s) login failed: %s\n", 
740                                   spnego_state->sub_sec_security->ops->name, 
741                                   nt_errstr(nt_status)));
742                 }
743                 return nt_status;
744         }
745         case SPNEGO_DONE:
746                 return NT_STATUS_OK;
747         }
748         return NT_STATUS_INVALID_PARAMETER;
749 }
750
751 static BOOL gensec_spnego_have_feature(struct gensec_security *gensec_security,
752                                        uint32 feature) 
753 {
754         struct spnego_state *spnego_state = gensec_security->private_data;
755         if (!spnego_state->sub_sec_security) {
756                 return False;
757         }
758         
759         return gensec_have_feature(spnego_state->sub_sec_security, 
760                                    feature);
761 }
762
763 static const struct gensec_security_ops gensec_spnego_security_ops = {
764         .name           = "spnego",
765         .sasl_name      = "GSS-SPNEGO",
766         .auth_type      = DCERPC_AUTH_TYPE_SPNEGO,
767         .oid            = GENSEC_OID_SPNEGO,
768         .client_start   = gensec_spnego_client_start,
769         .server_start   = gensec_spnego_server_start,
770         .update         = gensec_spnego_update,
771         .seal_packet    = gensec_spnego_seal_packet,
772         .sign_packet    = gensec_spnego_sign_packet,
773         .sig_size       = gensec_spnego_sig_size,
774         .check_packet   = gensec_spnego_check_packet,
775         .unseal_packet  = gensec_spnego_unseal_packet,
776         .wrap           = gensec_spnego_wrap,
777         .unwrap         = gensec_spnego_unwrap,
778         .session_key    = gensec_spnego_session_key,
779         .session_info   = gensec_spnego_session_info,
780         .have_feature   = gensec_spnego_have_feature
781 };
782
783 NTSTATUS gensec_spnego_init(void)
784 {
785         NTSTATUS ret;
786         ret = gensec_register(&gensec_spnego_security_ops);
787         if (!NT_STATUS_IS_OK(ret)) {
788                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
789                         gensec_spnego_security_ops.name));
790                 return ret;
791         }
792
793         return ret;
794 }