gensec: Remove mem_ctx from calls that do not return memory
[nivanova/samba-autobuild/.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    Copyright (C) Stefan Metzmacher <metze@samba.org>  2004-2008
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "../libcli/auth/spnego.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_proto.h"
31 #include "auth/gensec/gensec_toplevel_proto.h"
32 #include "param/param.h"
33
34 _PUBLIC_ NTSTATUS gensec_spnego_init(void);
35
36 enum spnego_state_position {
37         SPNEGO_SERVER_START,
38         SPNEGO_CLIENT_START,
39         SPNEGO_SERVER_TARG,
40         SPNEGO_CLIENT_TARG,
41         SPNEGO_FALLBACK,
42         SPNEGO_DONE
43 };
44
45 struct spnego_state {
46         enum spnego_message_type expected_packet;
47         enum spnego_state_position state_position;
48         struct gensec_security *sub_sec_security;
49         bool no_response_expected;
50
51         const char *neg_oid;
52
53         DATA_BLOB mech_types;
54 };
55
56
57 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
58 {
59         struct spnego_state *spnego_state;
60
61         spnego_state = talloc(gensec_security, struct spnego_state);
62         if (!spnego_state) {
63                 return NT_STATUS_NO_MEMORY;
64         }
65
66         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
67         spnego_state->state_position = SPNEGO_CLIENT_START;
68         spnego_state->sub_sec_security = NULL;
69         spnego_state->no_response_expected = false;
70         spnego_state->mech_types = data_blob(NULL, 0);
71
72         gensec_security->private_data = spnego_state;
73         return NT_STATUS_OK;
74 }
75
76 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
77 {
78         struct spnego_state *spnego_state;
79
80         spnego_state = talloc(gensec_security, struct spnego_state);            
81         if (!spnego_state) {
82                 return NT_STATUS_NO_MEMORY;
83         }
84
85         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
86         spnego_state->state_position = SPNEGO_SERVER_START;
87         spnego_state->sub_sec_security = NULL;
88         spnego_state->no_response_expected = false;
89         spnego_state->mech_types = data_blob(NULL, 0);
90
91         gensec_security->private_data = spnego_state;
92         return NT_STATUS_OK;
93 }
94
95 /*
96   wrappers for the spnego_*() functions
97 */
98 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security, 
99                                             uint8_t *data, size_t length, 
100                                             const uint8_t *whole_pdu, size_t pdu_length, 
101                                             const DATA_BLOB *sig)
102 {
103         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
104
105         if (spnego_state->state_position != SPNEGO_DONE 
106             && spnego_state->state_position != SPNEGO_FALLBACK) {
107                 return NT_STATUS_INVALID_PARAMETER;
108         }
109         
110         return gensec_unseal_packet(spnego_state->sub_sec_security, 
111                                     data, length, 
112                                     whole_pdu, pdu_length,
113                                     sig); 
114 }
115
116 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security, 
117                                            const uint8_t *data, size_t length, 
118                                            const uint8_t *whole_pdu, size_t pdu_length, 
119                                            const DATA_BLOB *sig)
120 {
121         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
122
123         if (spnego_state->state_position != SPNEGO_DONE 
124             && spnego_state->state_position != SPNEGO_FALLBACK) {
125                 return NT_STATUS_INVALID_PARAMETER;
126         }
127         
128         return gensec_check_packet(spnego_state->sub_sec_security, 
129                                    data, length, 
130                                    whole_pdu, pdu_length,
131                                    sig);
132 }
133
134 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security, 
135                                           TALLOC_CTX *mem_ctx, 
136                                           uint8_t *data, size_t length, 
137                                           const uint8_t *whole_pdu, size_t pdu_length, 
138                                           DATA_BLOB *sig)
139 {
140         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
141
142         if (spnego_state->state_position != SPNEGO_DONE 
143             && spnego_state->state_position != SPNEGO_FALLBACK) {
144                 return NT_STATUS_INVALID_PARAMETER;
145         }
146         
147         return gensec_seal_packet(spnego_state->sub_sec_security, 
148                                   mem_ctx, 
149                                   data, length, 
150                                   whole_pdu, pdu_length,
151                                   sig);
152 }
153
154 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security, 
155                                           TALLOC_CTX *mem_ctx, 
156                                           const uint8_t *data, size_t length, 
157                                           const uint8_t *whole_pdu, size_t pdu_length, 
158                                           DATA_BLOB *sig)
159 {
160         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
161
162         if (spnego_state->state_position != SPNEGO_DONE 
163             && spnego_state->state_position != SPNEGO_FALLBACK) {
164                 return NT_STATUS_INVALID_PARAMETER;
165         }
166         
167         return gensec_sign_packet(spnego_state->sub_sec_security, 
168                                   mem_ctx, 
169                                   data, length, 
170                                   whole_pdu, pdu_length,
171                                   sig);
172 }
173
174 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security, 
175                                    TALLOC_CTX *mem_ctx, 
176                                    const DATA_BLOB *in, 
177                                    DATA_BLOB *out)
178 {
179         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
180
181         if (spnego_state->state_position != SPNEGO_DONE 
182             && spnego_state->state_position != SPNEGO_FALLBACK) {
183                 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
184                 return NT_STATUS_INVALID_PARAMETER;
185         }
186         
187         return gensec_wrap(spnego_state->sub_sec_security, 
188                            mem_ctx, in, out);
189 }
190
191 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security, 
192                                      TALLOC_CTX *mem_ctx, 
193                                      const DATA_BLOB *in, 
194                                      DATA_BLOB *out)
195 {
196         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
197
198         if (spnego_state->state_position != SPNEGO_DONE 
199             && spnego_state->state_position != SPNEGO_FALLBACK) {
200                 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
201                 return NT_STATUS_INVALID_PARAMETER;
202         }
203         
204         return gensec_unwrap(spnego_state->sub_sec_security, 
205                              mem_ctx, in, out);
206 }
207
208 static NTSTATUS gensec_spnego_wrap_packets(struct gensec_security *gensec_security, 
209                                            TALLOC_CTX *mem_ctx, 
210                                            const DATA_BLOB *in, 
211                                            DATA_BLOB *out,
212                                            size_t *len_processed) 
213 {
214         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
215
216         if (spnego_state->state_position != SPNEGO_DONE 
217             && spnego_state->state_position != SPNEGO_FALLBACK) {
218                 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
219                 return NT_STATUS_INVALID_PARAMETER;
220         }
221         
222         return gensec_wrap_packets(spnego_state->sub_sec_security, 
223                                    mem_ctx, in, out,
224                                    len_processed);
225 }
226
227 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security, 
228                                                 DATA_BLOB blob, size_t *size)
229 {
230         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
231
232         if (spnego_state->state_position != SPNEGO_DONE 
233             && spnego_state->state_position != SPNEGO_FALLBACK) {
234                 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
235                 return NT_STATUS_INVALID_PARAMETER;
236         }
237         
238         return gensec_packet_full_request(spnego_state->sub_sec_security, 
239                                           blob, size);
240 }
241
242 static NTSTATUS gensec_spnego_unwrap_packets(struct gensec_security *gensec_security, 
243                                              TALLOC_CTX *mem_ctx, 
244                                              const DATA_BLOB *in, 
245                                              DATA_BLOB *out,
246                                              size_t *len_processed) 
247 {
248         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
249
250         if (spnego_state->state_position != SPNEGO_DONE 
251             && spnego_state->state_position != SPNEGO_FALLBACK) {
252                 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
253                 return NT_STATUS_INVALID_PARAMETER;
254         }
255         
256         return gensec_unwrap_packets(spnego_state->sub_sec_security, 
257                                      mem_ctx, in, out,
258                                      len_processed);
259 }
260
261 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size) 
262 {
263         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
264
265         if (spnego_state->state_position != SPNEGO_DONE 
266             && spnego_state->state_position != SPNEGO_FALLBACK) {
267                 return 0;
268         }
269         
270         return gensec_sig_size(spnego_state->sub_sec_security, data_size);
271 }
272
273 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security) 
274 {
275         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
276
277         if (spnego_state->state_position != SPNEGO_DONE 
278             && spnego_state->state_position != SPNEGO_FALLBACK) {
279                 return 0;
280         }
281         
282         return gensec_max_input_size(spnego_state->sub_sec_security);
283 }
284
285 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security) 
286 {
287         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
288
289         if (spnego_state->state_position != SPNEGO_DONE 
290             && spnego_state->state_position != SPNEGO_FALLBACK) {
291                 return 0;
292         }
293         
294         return gensec_max_wrapped_size(spnego_state->sub_sec_security);
295 }
296
297 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security, 
298                                           DATA_BLOB *session_key)
299 {
300         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
301         if (!spnego_state->sub_sec_security) {
302                 return NT_STATUS_INVALID_PARAMETER;
303         }
304         
305         return gensec_session_key(spnego_state->sub_sec_security, 
306                                   session_key);
307 }
308
309 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
310                                                                       struct auth_session_info **session_info) 
311 {
312         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
313         if (!spnego_state->sub_sec_security) {
314                 return NT_STATUS_INVALID_PARAMETER;
315         }
316         
317         return gensec_session_info(spnego_state->sub_sec_security, 
318                                    session_info);
319 }
320
321 /** Fallback to another GENSEC mechanism, based on magic strings 
322  *
323  * This is the 'fallback' case, where we don't get SPNEGO, and have to
324  * try all the other options (and hope they all have a magic string
325  * they check)
326 */
327
328 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security, 
329                                                   struct spnego_state *spnego_state,
330                                                   TALLOC_CTX *out_mem_ctx, 
331                                                   const DATA_BLOB in, DATA_BLOB *out) 
332 {
333         int i,j;
334         struct gensec_security_ops **all_ops
335                 = gensec_security_mechs(gensec_security, out_mem_ctx);
336         for (i=0; all_ops[i]; i++) {
337                 bool is_spnego;
338                 NTSTATUS nt_status;
339
340                 if (gensec_security != NULL && 
341                                 !gensec_security_ops_enabled(all_ops[i], gensec_security))
342                     continue;
343
344                 if (!all_ops[i]->oid) {
345                         continue;
346                 }
347
348                 is_spnego = false;
349                 for (j=0; all_ops[i]->oid[j]; j++) {
350                         if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
351                                 is_spnego = true;
352                         }
353                 }
354                 if (is_spnego) {
355                         continue;
356                 }
357
358                 if (!all_ops[i]->magic) {
359                         continue;
360                 }
361
362                 nt_status = all_ops[i]->magic(gensec_security, &in);
363                 if (!NT_STATUS_IS_OK(nt_status)) {
364                         continue;
365                 }
366
367                 spnego_state->state_position = SPNEGO_FALLBACK;
368
369                 nt_status = gensec_subcontext_start(spnego_state, 
370                                                     gensec_security, 
371                                                     &spnego_state->sub_sec_security);
372
373                 if (!NT_STATUS_IS_OK(nt_status)) {
374                         return nt_status;
375                 }
376                 /* select the sub context */
377                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
378                                                      all_ops[i]);
379                 if (!NT_STATUS_IS_OK(nt_status)) {
380                         return nt_status;
381                 }
382                 nt_status = gensec_update(spnego_state->sub_sec_security,
383                                           out_mem_ctx, in, out);
384                 return nt_status;
385         }
386         DEBUG(1, ("Failed to parse SPNEGO request\n"));
387         return NT_STATUS_INVALID_PARAMETER;
388         
389 }
390
391 /* 
392    Parse the netTokenInit, either from the client, to the server, or
393    from the server to the client.
394 */
395
396 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
397                                                  struct spnego_state *spnego_state, 
398                                                  TALLOC_CTX *out_mem_ctx, 
399                                                  const char **mechType,
400                                                  const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out) 
401 {
402         int i;
403         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
404         DATA_BLOB null_data_blob = data_blob(NULL,0);
405         bool ok;
406
407         const struct gensec_security_ops_wrapper *all_sec
408                 = gensec_security_by_oid_list(gensec_security, 
409                                               out_mem_ctx, 
410                                               mechType,
411                                               GENSEC_OID_SPNEGO);
412
413         ok = spnego_write_mech_types(spnego_state,
414                                      mechType,
415                                      &spnego_state->mech_types);
416         if (!ok) {
417                 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
418                 return NT_STATUS_NO_MEMORY;
419         }
420
421         if (spnego_state->state_position == SPNEGO_SERVER_START) {
422                 uint32_t j;
423                 for (j=0; mechType && mechType[j]; j++) {
424                         for (i=0; all_sec && all_sec[i].op; i++) {
425                                 nt_status = gensec_subcontext_start(spnego_state,
426                                                                     gensec_security,
427                                                                     &spnego_state->sub_sec_security);
428                                 if (!NT_STATUS_IS_OK(nt_status)) {
429                                         return nt_status;
430                                 }
431                                 /* select the sub context */
432                                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
433                                                                      all_sec[i].op);
434                                 if (!NT_STATUS_IS_OK(nt_status)) {
435                                         talloc_free(spnego_state->sub_sec_security);
436                                         spnego_state->sub_sec_security = NULL;
437                                         break;
438                                 }
439
440                                 if (j > 0) {
441                                         /* no optimistic token */
442                                         spnego_state->neg_oid = all_sec[i].oid;
443                                         *unwrapped_out = data_blob_null;
444                                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
445                                         break;
446                                 }
447
448                                 nt_status = gensec_update(spnego_state->sub_sec_security,
449                                                           out_mem_ctx, 
450                                                           unwrapped_in,
451                                                           unwrapped_out);
452                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
453                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
454                                         /* Pretend we never started it (lets the first run find some incompatible demand) */
455                                         
456                                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n", 
457                                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
458                                         talloc_free(spnego_state->sub_sec_security);
459                                         spnego_state->sub_sec_security = NULL;
460                                         break;
461                                 }
462
463                                 spnego_state->neg_oid = all_sec[i].oid;
464                                 break;
465                         }
466                         if (spnego_state->sub_sec_security) {
467                                 break;
468                         }
469                 }
470
471                 if (!spnego_state->sub_sec_security) {
472                         DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
473                         return NT_STATUS_INVALID_PARAMETER;
474                 }
475         }
476         
477         /* Having tried any optimistic token from the client (if we
478          * were the server), if we didn't get anywhere, walk our list
479          * in our preference order */
480         
481         if (!spnego_state->sub_sec_security) {
482                 for (i=0; all_sec && all_sec[i].op; i++) {
483                         nt_status = gensec_subcontext_start(spnego_state,
484                                                             gensec_security,
485                                                             &spnego_state->sub_sec_security);
486                         if (!NT_STATUS_IS_OK(nt_status)) {
487                                 return nt_status;
488                         }
489                         /* select the sub context */
490                         nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
491                                                              all_sec[i].op);
492                         if (!NT_STATUS_IS_OK(nt_status)) {
493                                 talloc_free(spnego_state->sub_sec_security);
494                                 spnego_state->sub_sec_security = NULL;
495                                 continue;
496                         }
497                         
498                         spnego_state->neg_oid = all_sec[i].oid;
499
500                         /* only get the helping start blob for the first OID */
501                         nt_status = gensec_update(spnego_state->sub_sec_security,
502                                                   out_mem_ctx, 
503                                                   null_data_blob, 
504                                                   unwrapped_out);
505
506                         /* it is likely that a NULL input token will
507                          * not be liked by most server mechs, but if
508                          * we are in the client, we want the first
509                          * update packet to be able to abort the use
510                          * of this mech */
511                         if (spnego_state->state_position != SPNEGO_SERVER_START) {
512                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
513                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
514                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
515                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
516                                         /* Pretend we never started it (lets the first run find some incompatible demand) */
517                                         
518                                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n", 
519                                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
520                                         talloc_free(spnego_state->sub_sec_security);
521                                         spnego_state->sub_sec_security = NULL;
522                                         continue;
523                                 }
524                         }
525
526                         break;
527                 }
528         }
529
530         if (spnego_state->sub_sec_security) {
531                 /* it is likely that a NULL input token will
532                  * not be liked by most server mechs, but this
533                  * does the right thing in the CIFS client.
534                  * just push us along the merry-go-round
535                  * again, and hope for better luck next
536                  * time */
537                 
538                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
539                         *unwrapped_out = data_blob(NULL, 0);
540                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
541                 }
542                 
543                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) 
544                     && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
545                     && !NT_STATUS_IS_OK(nt_status)) {
546                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
547                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
548                         talloc_free(spnego_state->sub_sec_security);
549                         spnego_state->sub_sec_security = NULL;
550                         
551                         /* We started the mech correctly, and the
552                          * input from the other side was valid.
553                          * Return the error (say bad password, invalid
554                          * ticket) */
555                         return nt_status;
556                 }
557         
558                 
559                 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
560         }
561
562         DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
563         /* we could re-negotiate here, but it would only work
564          * if the client or server lied about what it could
565          * support the first time.  Lets keep this code to
566          * reality */
567
568         return nt_status;
569 }
570
571 /** create a negTokenInit 
572  *
573  * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
574 */
575 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security, 
576                                                   struct spnego_state *spnego_state,
577                                                   TALLOC_CTX *out_mem_ctx, 
578                                                   const DATA_BLOB in, DATA_BLOB *out) 
579 {
580         int i;
581         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
582         DATA_BLOB null_data_blob = data_blob(NULL,0);
583         const char **mechTypes = NULL;
584         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
585         const struct gensec_security_ops_wrapper *all_sec;
586
587         mechTypes = gensec_security_oids(gensec_security, 
588                                          out_mem_ctx, GENSEC_OID_SPNEGO);
589
590         all_sec = gensec_security_by_oid_list(gensec_security, 
591                                               out_mem_ctx, 
592                                               mechTypes,
593                                               GENSEC_OID_SPNEGO);
594         for (i=0; all_sec && all_sec[i].op; i++) {
595                 struct spnego_data spnego_out;
596                 const char **send_mech_types;
597                 bool ok;
598
599                 nt_status = gensec_subcontext_start(spnego_state,
600                                                     gensec_security,
601                                                     &spnego_state->sub_sec_security);
602                 if (!NT_STATUS_IS_OK(nt_status)) {
603                         return nt_status;
604                 }
605                 /* select the sub context */
606                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
607                                                      all_sec[i].op);
608                 if (!NT_STATUS_IS_OK(nt_status)) {
609                         talloc_free(spnego_state->sub_sec_security);
610                         spnego_state->sub_sec_security = NULL;
611                         continue;
612                 }
613
614                 /* In the client, try and produce the first (optimistic) packet */
615                 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
616                         nt_status = gensec_update(spnego_state->sub_sec_security,
617                                                   out_mem_ctx, 
618                                                   null_data_blob,
619                                                   &unwrapped_out);
620                         
621                         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
622                             && !NT_STATUS_IS_OK(nt_status)) {
623                                 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n", 
624                                           spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
625                                 talloc_free(spnego_state->sub_sec_security);
626                                 spnego_state->sub_sec_security = NULL;
627                                 /* Pretend we never started it (lets the first run find some incompatible demand) */
628                                 
629                                 continue;
630                         }
631                 }
632
633                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
634
635                 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
636                                                                         &all_sec[i]);
637
638                 ok = spnego_write_mech_types(spnego_state,
639                                              send_mech_types,
640                                              &spnego_state->mech_types);
641                 if (!ok) {
642                         DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
643                         return NT_STATUS_NO_MEMORY;
644                 }
645
646                 /* List the remaining mechs as options */
647                 spnego_out.negTokenInit.mechTypes = send_mech_types;
648                 spnego_out.negTokenInit.reqFlags = null_data_blob;
649                 spnego_out.negTokenInit.reqFlagsPadding = 0;
650                 
651                 if (spnego_state->state_position == SPNEGO_SERVER_START) {
652                         spnego_out.negTokenInit.mechListMIC
653                                 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
654                 } else {
655                         spnego_out.negTokenInit.mechListMIC = null_data_blob;
656                 }
657
658                 spnego_out.negTokenInit.mechToken = unwrapped_out;
659                 
660                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
661                         DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
662                                 return NT_STATUS_INVALID_PARAMETER;
663                 }
664                 
665                 /* set next state */
666                 spnego_state->neg_oid = all_sec[i].oid;
667                 
668                 if (NT_STATUS_IS_OK(nt_status)) {
669                         spnego_state->no_response_expected = true;
670                 }
671
672                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
673         } 
674         talloc_free(spnego_state->sub_sec_security);
675         spnego_state->sub_sec_security = NULL;
676
677         DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
678         return NT_STATUS_INVALID_PARAMETER;
679 }
680
681
682 /** create a server negTokenTarg 
683  *
684  * This is the case, where the client is the first one who sends data
685 */
686
687 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security, 
688                                                   struct spnego_state *spnego_state,
689                                                   TALLOC_CTX *out_mem_ctx, 
690                                                   NTSTATUS nt_status,
691                                                   const DATA_BLOB unwrapped_out,
692                                                   DATA_BLOB mech_list_mic,
693                                                   DATA_BLOB *out)
694 {
695         struct spnego_data spnego_out;
696         DATA_BLOB null_data_blob = data_blob(NULL, 0);
697
698         /* compose reply */
699         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
700         spnego_out.negTokenTarg.responseToken = unwrapped_out;
701         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
702         spnego_out.negTokenTarg.supportedMech = NULL;
703
704         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {   
705                 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
706                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
707                 spnego_state->state_position = SPNEGO_SERVER_TARG;
708         } else if (NT_STATUS_IS_OK(nt_status)) {
709                 if (unwrapped_out.data) {
710                         spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
711                 }
712                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
713                 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
714                 spnego_state->state_position = SPNEGO_DONE;
715         } else {
716                 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
717                 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
718                 spnego_state->state_position = SPNEGO_DONE;
719         }
720
721         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
722                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
723                 return NT_STATUS_INVALID_PARAMETER;
724         }
725
726         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
727
728         return nt_status;
729 }
730
731
732 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
733                                      const DATA_BLOB in, DATA_BLOB *out) 
734 {
735         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
736         DATA_BLOB null_data_blob = data_blob(NULL, 0);
737         DATA_BLOB mech_list_mic = data_blob(NULL, 0);
738         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
739         struct spnego_data spnego_out;
740         struct spnego_data spnego;
741
742         ssize_t len;
743
744         *out = data_blob(NULL, 0);
745
746         if (!out_mem_ctx) {
747                 out_mem_ctx = spnego_state;
748         }
749
750         /* and switch into the state machine */
751
752         switch (spnego_state->state_position) {
753         case SPNEGO_FALLBACK:
754                 return gensec_update(spnego_state->sub_sec_security,
755                                      out_mem_ctx, in, out);
756         case SPNEGO_SERVER_START:
757         {
758                 NTSTATUS nt_status;
759                 if (in.length) {
760
761                         len = spnego_read_data(gensec_security, in, &spnego);
762                         if (len == -1) {
763                                 return gensec_spnego_server_try_fallback(gensec_security, spnego_state, 
764                                                                          out_mem_ctx, in, out);
765                         }
766                         /* client sent NegTargetInit, we send NegTokenTarg */
767
768                         /* OK, so it's real SPNEGO, check the packet's the one we expect */
769                         if (spnego.type != spnego_state->expected_packet) {
770                                 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
771                                           spnego_state->expected_packet));
772                                 dump_data(1, in.data, in.length);
773                                 spnego_free_data(&spnego);
774                                 return NT_STATUS_INVALID_PARAMETER;
775                         }
776                         
777                         nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
778                                                                      spnego_state,
779                                                                      out_mem_ctx, 
780                                                                      spnego.negTokenInit.mechTypes,
781                                                                      spnego.negTokenInit.mechToken, 
782                                                                      &unwrapped_out);
783                         
784                         nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
785                                                                       spnego_state,
786                                                                       out_mem_ctx,
787                                                                       nt_status,
788                                                                       unwrapped_out,
789                                                                       null_data_blob,
790                                                                       out);
791                         
792                         spnego_free_data(&spnego);
793                         
794                         return nt_status;
795                 } else {
796                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
797                                                                       out_mem_ctx, in, out);
798                         spnego_state->state_position = SPNEGO_SERVER_START;
799                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
800                         return nt_status;
801                 }
802         }
803         
804         case SPNEGO_CLIENT_START:
805         {
806                 /* The server offers a list of mechanisms */
807                 
808                 const char *my_mechs[] = {NULL, NULL};
809                 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
810
811                 if (!in.length) {
812                         /* client to produce negTokenInit */
813                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
814                                                                  out_mem_ctx, in, out);
815                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
816                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
817                         return nt_status;
818                 }
819                 
820                 len = spnego_read_data(gensec_security, in, &spnego);
821                 
822                 if (len == -1) {
823                         DEBUG(1, ("Invalid SPNEGO request:\n"));
824                         dump_data(1, in.data, in.length);
825                         return NT_STATUS_INVALID_PARAMETER;
826                 }
827                 
828                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
829                 if (spnego.type != spnego_state->expected_packet) {
830                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
831                                   spnego_state->expected_packet));
832                         dump_data(1, in.data, in.length);
833                         spnego_free_data(&spnego);
834                         return NT_STATUS_INVALID_PARAMETER;
835                 }
836
837                 if (spnego.negTokenInit.targetPrincipal
838                     && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
839                         DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
840                         if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
841                                 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
842                         }
843                 }
844
845                 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
846                                                              spnego_state,
847                                                              out_mem_ctx, 
848                                                              spnego.negTokenInit.mechTypes,
849                                                              spnego.negTokenInit.mechToken, 
850                                                              &unwrapped_out);
851
852                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
853                         spnego_free_data(&spnego);
854                         return nt_status;
855                 }
856
857                 my_mechs[0] = spnego_state->neg_oid;
858                 /* compose reply */
859                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
860                 spnego_out.negTokenInit.mechTypes = my_mechs;
861                 spnego_out.negTokenInit.reqFlags = null_data_blob;
862                 spnego_out.negTokenInit.reqFlagsPadding = 0;
863                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
864                 spnego_out.negTokenInit.mechToken = unwrapped_out;
865                 
866                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
867                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
868                                 return NT_STATUS_INVALID_PARAMETER;
869                 }
870                 
871                 /* set next state */
872                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
873                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
874
875                 if (NT_STATUS_IS_OK(nt_status)) {
876                         spnego_state->no_response_expected = true;
877                 }
878                 
879                 spnego_free_data(&spnego);
880                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
881         }
882         case SPNEGO_SERVER_TARG:
883         {
884                 NTSTATUS nt_status;
885                 bool new_spnego = false;
886
887                 if (!in.length) {
888                         return NT_STATUS_INVALID_PARAMETER;
889                 }
890                 
891                 len = spnego_read_data(gensec_security, in, &spnego);
892                 
893                 if (len == -1) {
894                         DEBUG(1, ("Invalid SPNEGO request:\n"));
895                         dump_data(1, in.data, in.length);
896                         return NT_STATUS_INVALID_PARAMETER;
897                 }
898                 
899                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
900                 if (spnego.type != spnego_state->expected_packet) {
901                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
902                                   spnego_state->expected_packet));
903                         dump_data(1, in.data, in.length);
904                         spnego_free_data(&spnego);
905                         return NT_STATUS_INVALID_PARAMETER;
906                 }
907
908                 if (!spnego_state->sub_sec_security) {
909                         DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
910                         spnego_free_data(&spnego);
911                         return NT_STATUS_INVALID_PARAMETER;
912                 }
913
914                 nt_status = gensec_update(spnego_state->sub_sec_security,
915                                           out_mem_ctx, 
916                                           spnego.negTokenTarg.responseToken,
917                                           &unwrapped_out);
918                 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
919                         new_spnego = true;
920                         nt_status = gensec_check_packet(spnego_state->sub_sec_security,
921                                                         spnego_state->mech_types.data,
922                                                         spnego_state->mech_types.length,
923                                                         spnego_state->mech_types.data,
924                                                         spnego_state->mech_types.length,
925                                                         &spnego.negTokenTarg.mechListMIC);
926                         if (!NT_STATUS_IS_OK(nt_status)) {
927                                 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
928                                         nt_errstr(nt_status)));
929                         }
930                 }
931                 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
932                         nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
933                                                        out_mem_ctx,
934                                                        spnego_state->mech_types.data,
935                                                        spnego_state->mech_types.length,
936                                                        spnego_state->mech_types.data,
937                                                        spnego_state->mech_types.length,
938                                                        &mech_list_mic);
939                         if (!NT_STATUS_IS_OK(nt_status)) {
940                                 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
941                                         nt_errstr(nt_status)));
942                         }
943                 }
944
945                 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
946                                                               spnego_state,
947                                                               out_mem_ctx, 
948                                                               nt_status,
949                                                               unwrapped_out,
950                                                               mech_list_mic,
951                                                               out);
952                 
953                 spnego_free_data(&spnego);
954                 
955                 return nt_status;
956         }
957         case SPNEGO_CLIENT_TARG:
958         {
959                 NTSTATUS nt_status;
960                 if (!in.length) {
961                         return NT_STATUS_INVALID_PARAMETER;
962                 }
963                 
964                 len = spnego_read_data(gensec_security, in, &spnego);
965                 
966                 if (len == -1) {
967                         DEBUG(1, ("Invalid SPNEGO request:\n"));
968                         dump_data(1, in.data, in.length);
969                         return NT_STATUS_INVALID_PARAMETER;
970                 }
971                 
972                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
973                 if (spnego.type != spnego_state->expected_packet) {
974                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
975                                   spnego_state->expected_packet));
976                         dump_data(1, in.data, in.length);
977                         spnego_free_data(&spnego);
978                         return NT_STATUS_INVALID_PARAMETER;
979                 }
980         
981                 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
982                         spnego_free_data(&spnego);
983                         return NT_STATUS_ACCESS_DENIED;
984                 }
985
986                 /* Server didn't like our choice of mech, and chose something else */
987                 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
988                     spnego.negTokenTarg.supportedMech &&
989                     strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
990                         DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
991                                  gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech), 
992                                  gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
993                         
994                         talloc_free(spnego_state->sub_sec_security);
995                         nt_status = gensec_subcontext_start(spnego_state,
996                                                             gensec_security,
997                                                             &spnego_state->sub_sec_security);
998                         if (!NT_STATUS_IS_OK(nt_status)) {
999                                 spnego_free_data(&spnego);
1000                                 return nt_status;
1001                         }
1002                         /* select the sub context */
1003                         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
1004                                                              spnego.negTokenTarg.supportedMech);
1005                         if (!NT_STATUS_IS_OK(nt_status)) {
1006                                 spnego_free_data(&spnego);
1007                                 return nt_status;
1008                         }
1009
1010                         nt_status = gensec_update(spnego_state->sub_sec_security,
1011                                                   out_mem_ctx, 
1012                                                   spnego.negTokenTarg.responseToken,
1013                                                   &unwrapped_out);
1014                         spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1015                 } else if (spnego_state->no_response_expected) {
1016                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1017                                 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1018                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1019                         } else if (spnego.negTokenTarg.responseToken.length) {
1020                                 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1021                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1022                         } else {
1023                                 nt_status = NT_STATUS_OK;
1024                         }
1025                         if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1026                                 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1027                                                                 spnego_state->mech_types.data,
1028                                                                 spnego_state->mech_types.length,
1029                                                                 spnego_state->mech_types.data,
1030                                                                 spnego_state->mech_types.length,
1031                                                                 &spnego.negTokenTarg.mechListMIC);
1032                                 if (!NT_STATUS_IS_OK(nt_status)) {
1033                                         DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1034                                                 nt_errstr(nt_status)));
1035                                 }
1036                         }
1037                 } else {
1038                         bool new_spnego = false;
1039
1040                         nt_status = gensec_update(spnego_state->sub_sec_security,
1041                                                   out_mem_ctx, 
1042                                                   spnego.negTokenTarg.responseToken, 
1043                                                   &unwrapped_out);
1044
1045                         if (NT_STATUS_IS_OK(nt_status)
1046                             && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1047                                 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1048                                                                  GENSEC_FEATURE_NEW_SPNEGO);
1049                         }
1050                         if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1051                                 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1052                                                                out_mem_ctx,
1053                                                                spnego_state->mech_types.data,
1054                                                                spnego_state->mech_types.length,
1055                                                                spnego_state->mech_types.data,
1056                                                                spnego_state->mech_types.length,
1057                                                                &mech_list_mic);
1058                                 if (!NT_STATUS_IS_OK(nt_status)) {
1059                                         DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1060                                                 nt_errstr(nt_status)));
1061                                 }
1062                         }
1063                         if (NT_STATUS_IS_OK(nt_status)) {
1064                                 spnego_state->no_response_expected = true;
1065                         }
1066                 } 
1067                 
1068                 spnego_free_data(&spnego);
1069
1070                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1071                         && !NT_STATUS_IS_OK(nt_status)) {
1072                         DEBUG(1, ("SPNEGO(%s) login failed: %s\n", 
1073                                   spnego_state->sub_sec_security->ops->name, 
1074                                   nt_errstr(nt_status)));
1075                         return nt_status;
1076                 }
1077
1078                 if (unwrapped_out.length || mech_list_mic.length) {
1079                         /* compose reply */
1080                         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1081                         spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1082                         spnego_out.negTokenTarg.supportedMech = NULL;
1083                         spnego_out.negTokenTarg.responseToken = unwrapped_out;
1084                         spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1085                         
1086                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1087                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1088                                 return NT_STATUS_INVALID_PARAMETER;
1089                         }
1090                 
1091                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
1092                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1093                 } else {
1094
1095                         /* all done - server has accepted, and we agree */
1096                         *out = null_data_blob;
1097
1098                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1099                                 /* unless of course it did not accept */
1100                                 DEBUG(1,("gensec_update ok but not accepted\n"));
1101                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1102                         }
1103                 
1104                         spnego_state->state_position = SPNEGO_DONE;
1105                 }
1106
1107                 return nt_status;
1108         }
1109         case SPNEGO_DONE:
1110                 /* We should not be called after we are 'done' */
1111                 return NT_STATUS_INVALID_PARAMETER;
1112         }
1113         return NT_STATUS_INVALID_PARAMETER;
1114 }
1115
1116 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1117                                        uint32_t feature)
1118 {
1119         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1120
1121         if (!spnego_state || !spnego_state->sub_sec_security) {
1122                 gensec_security->want_features |= feature;
1123                 return;
1124         }
1125
1126         gensec_want_feature(spnego_state->sub_sec_security,
1127                             feature);
1128 }
1129
1130 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1131                                        uint32_t feature) 
1132 {
1133         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1134         if (!spnego_state->sub_sec_security) {
1135                 return false;
1136         }
1137         
1138         return gensec_have_feature(spnego_state->sub_sec_security, 
1139                                    feature);
1140 }
1141
1142 static const char *gensec_spnego_oids[] = { 
1143         GENSEC_OID_SPNEGO,
1144         NULL 
1145 };
1146
1147 static const struct gensec_security_ops gensec_spnego_security_ops = {
1148         .name             = "spnego",
1149         .sasl_name        = "GSS-SPNEGO",
1150         .auth_type        = DCERPC_AUTH_TYPE_SPNEGO,
1151         .oid              = gensec_spnego_oids,
1152         .client_start     = gensec_spnego_client_start,
1153         .server_start     = gensec_spnego_server_start,
1154         .update           = gensec_spnego_update,
1155         .seal_packet      = gensec_spnego_seal_packet,
1156         .sign_packet      = gensec_spnego_sign_packet,
1157         .sig_size         = gensec_spnego_sig_size,
1158         .max_wrapped_size = gensec_spnego_max_wrapped_size,
1159         .max_input_size   = gensec_spnego_max_input_size,
1160         .check_packet     = gensec_spnego_check_packet,
1161         .unseal_packet    = gensec_spnego_unseal_packet,
1162         .packet_full_request = gensec_spnego_packet_full_request,
1163         .wrap             = gensec_spnego_wrap,
1164         .unwrap           = gensec_spnego_unwrap,
1165         .wrap_packets     = gensec_spnego_wrap_packets,
1166         .unwrap_packets   = gensec_spnego_unwrap_packets,
1167         .session_key      = gensec_spnego_session_key,
1168         .session_info     = gensec_spnego_session_info,
1169         .want_feature     = gensec_spnego_want_feature,
1170         .have_feature     = gensec_spnego_have_feature,
1171         .enabled          = true,
1172         .priority         = GENSEC_SPNEGO
1173 };
1174
1175 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1176 {
1177         NTSTATUS ret;
1178         ret = gensec_register(&gensec_spnego_security_ops);
1179         if (!NT_STATUS_IS_OK(ret)) {
1180                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1181                         gensec_spnego_security_ops.name));
1182                 return ret;
1183         }
1184
1185         return ret;
1186 }