s4:gensec/spnego: only generate the mechListMic when the server expects it
[ira/wip.git] / source4 / auth / gensec / spnego.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    RFC2478 Compliant SPNEGO implementation
5    
6    Copyright (C) Jim McDonough <jmcd@us.ibm.com>      2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8    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 "auth/gensec/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
32 enum spnego_state_position {
33         SPNEGO_SERVER_START,
34         SPNEGO_CLIENT_START,
35         SPNEGO_SERVER_TARG,
36         SPNEGO_CLIENT_TARG,
37         SPNEGO_FALLBACK,
38         SPNEGO_DONE
39 };
40
41 struct spnego_state {
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         const char *neg_oid;
48
49         DATA_BLOB mech_types;
50 };
51
52
53 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
54 {
55         struct spnego_state *spnego_state;
56
57         spnego_state = talloc(gensec_security, struct spnego_state);
58         if (!spnego_state) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61
62         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
63         spnego_state->state_position = SPNEGO_CLIENT_START;
64         spnego_state->sub_sec_security = NULL;
65         spnego_state->no_response_expected = false;
66         spnego_state->mech_types = data_blob(NULL, 0);
67
68         gensec_security->private_data = spnego_state;
69         return NT_STATUS_OK;
70 }
71
72 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
73 {
74         struct spnego_state *spnego_state;
75
76         spnego_state = talloc(gensec_security, struct spnego_state);            
77         if (!spnego_state) {
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
82         spnego_state->state_position = SPNEGO_SERVER_START;
83         spnego_state->sub_sec_security = NULL;
84         spnego_state->no_response_expected = false;
85         spnego_state->mech_types = data_blob(NULL, 0);
86
87         gensec_security->private_data = spnego_state;
88         return NT_STATUS_OK;
89 }
90
91 /*
92   wrappers for the spnego_*() functions
93 */
94 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security, 
95                                             TALLOC_CTX *mem_ctx, 
96                                             uint8_t *data, size_t length, 
97                                             const uint8_t *whole_pdu, size_t pdu_length, 
98                                             const DATA_BLOB *sig)
99 {
100         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
101
102         if (spnego_state->state_position != SPNEGO_DONE 
103             && spnego_state->state_position != SPNEGO_FALLBACK) {
104                 return NT_STATUS_INVALID_PARAMETER;
105         }
106         
107         return gensec_unseal_packet(spnego_state->sub_sec_security, 
108                                     mem_ctx, 
109                                     data, length, 
110                                     whole_pdu, pdu_length,
111                                     sig); 
112 }
113
114 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security, 
115                                            TALLOC_CTX *mem_ctx, 
116                                            const uint8_t *data, size_t length, 
117                                            const uint8_t *whole_pdu, size_t pdu_length, 
118                                            const DATA_BLOB *sig)
119 {
120         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
121
122         if (spnego_state->state_position != SPNEGO_DONE 
123             && spnego_state->state_position != SPNEGO_FALLBACK) {
124                 return NT_STATUS_INVALID_PARAMETER;
125         }
126         
127         return gensec_check_packet(spnego_state->sub_sec_security, 
128                                    mem_ctx, 
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->settings->lp_ctx))
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                 for (i=0; all_sec && all_sec[i].op; i++) {
423                         /* optomisitic token */
424                         if (strcmp(all_sec[i].oid, mechType[0]) == 0) {
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                                 nt_status = gensec_update(spnego_state->sub_sec_security,
441                                                           out_mem_ctx, 
442                                                           unwrapped_in,
443                                                           unwrapped_out);
444                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
445                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
446                                         /* Pretend we never started it (lets the first run find some incompatible demand) */
447                                         
448                                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n", 
449                                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
450                                         talloc_free(spnego_state->sub_sec_security);
451                                         spnego_state->sub_sec_security = NULL;
452                                         break;
453                                 }
454
455                                 spnego_state->neg_oid = all_sec[i].oid;
456                                 break;
457                         }
458                 }
459         }
460         
461         /* Having tried any optomisitc token from the client (if we
462          * were the server), if we didn't get anywhere, walk our list
463          * in our preference order */
464         
465         if (!spnego_state->sub_sec_security) {
466                 for (i=0; all_sec && all_sec[i].op; i++) {
467                         nt_status = gensec_subcontext_start(spnego_state,
468                                                             gensec_security,
469                                                             &spnego_state->sub_sec_security);
470                         if (!NT_STATUS_IS_OK(nt_status)) {
471                                 return nt_status;
472                         }
473                         /* select the sub context */
474                         nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
475                                                              all_sec[i].op);
476                         if (!NT_STATUS_IS_OK(nt_status)) {
477                                 talloc_free(spnego_state->sub_sec_security);
478                                 spnego_state->sub_sec_security = NULL;
479                                 continue;
480                         }
481                         
482                         spnego_state->neg_oid = all_sec[i].oid;
483
484                         /* only get the helping start blob for the first OID */
485                         nt_status = gensec_update(spnego_state->sub_sec_security,
486                                                   out_mem_ctx, 
487                                                   null_data_blob, 
488                                                   unwrapped_out);
489
490                         /* it is likely that a NULL input token will
491                          * not be liked by most server mechs, but if
492                          * we are in the client, we want the first
493                          * update packet to be able to abort the use
494                          * of this mech */
495                         if (spnego_state->state_position != SPNEGO_SERVER_START) {
496                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
497                                     NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
498                                         /* Pretend we never started it (lets the first run find some incompatible demand) */
499                                         
500                                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n", 
501                                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
502                                         talloc_free(spnego_state->sub_sec_security);
503                                         spnego_state->sub_sec_security = NULL;
504                                         continue;
505                                 }
506                         }
507
508                         break;
509                 }
510         }
511
512         if (spnego_state->sub_sec_security) {
513                 /* it is likely that a NULL input token will
514                  * not be liked by most server mechs, but this
515                  * does the right thing in the CIFS client.
516                  * just push us along the merry-go-round
517                  * again, and hope for better luck next
518                  * time */
519                 
520                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
521                         *unwrapped_out = data_blob(NULL, 0);
522                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
523                 }
524                 
525                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) 
526                     && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
527                     && !NT_STATUS_IS_OK(nt_status)) {
528                         DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
529                                   spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
530                         talloc_free(spnego_state->sub_sec_security);
531                         spnego_state->sub_sec_security = NULL;
532                         
533                         /* We started the mech correctly, and the
534                          * input from the other side was valid.
535                          * Return the error (say bad password, invalid
536                          * ticket) */
537                         return nt_status;
538                 }
539         
540                 
541                 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
542         }
543
544         DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
545         /* we could re-negotiate here, but it would only work
546          * if the client or server lied about what it could
547          * support the first time.  Lets keep this code to
548          * reality */
549
550         return nt_status;
551 }
552
553 /** create a negTokenInit 
554  *
555  * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
556 */
557 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security, 
558                                                   struct spnego_state *spnego_state,
559                                                   TALLOC_CTX *out_mem_ctx, 
560                                                   const DATA_BLOB in, DATA_BLOB *out) 
561 {
562         int i;
563         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
564         DATA_BLOB null_data_blob = data_blob(NULL,0);
565         const char **mechTypes = NULL;
566         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
567         const struct gensec_security_ops_wrapper *all_sec;
568         const char *principal = NULL;
569
570         mechTypes = gensec_security_oids(gensec_security, 
571                                          out_mem_ctx, GENSEC_OID_SPNEGO);
572
573         all_sec = gensec_security_by_oid_list(gensec_security, 
574                                               out_mem_ctx, 
575                                               mechTypes,
576                                               GENSEC_OID_SPNEGO);
577         for (i=0; all_sec && all_sec[i].op; i++) {
578                 struct spnego_data spnego_out;
579                 const char **send_mech_types;
580                 bool ok;
581
582                 nt_status = gensec_subcontext_start(spnego_state,
583                                                     gensec_security,
584                                                     &spnego_state->sub_sec_security);
585                 if (!NT_STATUS_IS_OK(nt_status)) {
586                         return nt_status;
587                 }
588                 /* select the sub context */
589                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
590                                                      all_sec[i].op);
591                 if (!NT_STATUS_IS_OK(nt_status)) {
592                         talloc_free(spnego_state->sub_sec_security);
593                         spnego_state->sub_sec_security = NULL;
594                         continue;
595                 }
596
597                 /* In the client, try and produce the first (optimistic) packet */
598                 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
599                         nt_status = gensec_update(spnego_state->sub_sec_security,
600                                                   out_mem_ctx, 
601                                                   null_data_blob,
602                                                   &unwrapped_out);
603                         
604                         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
605                             && !NT_STATUS_IS_OK(nt_status)) {
606                                 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n", 
607                                           spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
608                                 talloc_free(spnego_state->sub_sec_security);
609                                 spnego_state->sub_sec_security = NULL;
610                                 /* Pretend we never started it (lets the first run find some incompatible demand) */
611                                 
612                                 continue;
613                         }
614                 }
615
616                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
617
618                 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
619                                                                         &all_sec[i]);
620
621                 ok = spnego_write_mech_types(spnego_state,
622                                              send_mech_types,
623                                              &spnego_state->mech_types);
624                 if (!ok) {
625                         DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
626                         return NT_STATUS_NO_MEMORY;
627                 }
628
629                 /* List the remaining mechs as options */
630                 spnego_out.negTokenInit.mechTypes = send_mech_types;
631                 spnego_out.negTokenInit.reqFlags = 0;
632                 
633                 if (spnego_state->state_position == SPNEGO_SERVER_START) {
634                         /* server credentials */
635                         struct cli_credentials *creds = gensec_get_credentials(gensec_security);
636                         if (creds) {
637                                 principal = cli_credentials_get_principal(creds, out_mem_ctx);
638                         }
639                 }
640                 if (principal) {
641                         spnego_out.negTokenInit.mechListMIC
642                                 = data_blob_string_const(principal);
643                 } else {
644                         spnego_out.negTokenInit.mechListMIC = null_data_blob;
645                 }
646
647                 spnego_out.negTokenInit.mechToken = unwrapped_out;
648                 
649                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
650                         DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
651                                 return NT_STATUS_INVALID_PARAMETER;
652                 }
653                 
654                 /* set next state */
655                 spnego_state->neg_oid = all_sec[i].oid;
656                 
657                 if (NT_STATUS_IS_OK(nt_status)) {
658                         spnego_state->no_response_expected = true;
659                 }
660
661                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
662         } 
663         talloc_free(spnego_state->sub_sec_security);
664         spnego_state->sub_sec_security = NULL;
665
666         DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
667         return NT_STATUS_INVALID_PARAMETER;
668 }
669
670
671 /** create a server negTokenTarg 
672  *
673  * This is the case, where the client is the first one who sends data
674 */
675
676 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security, 
677                                                   struct spnego_state *spnego_state,
678                                                   TALLOC_CTX *out_mem_ctx, 
679                                                   NTSTATUS nt_status,
680                                                   const DATA_BLOB unwrapped_out,
681                                                   DATA_BLOB mech_list_mic,
682                                                   DATA_BLOB *out)
683 {
684         struct spnego_data spnego_out;
685         DATA_BLOB null_data_blob = data_blob(NULL, 0);
686
687         /* compose reply */
688         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
689         spnego_out.negTokenTarg.responseToken = unwrapped_out;
690         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
691         spnego_out.negTokenTarg.supportedMech = NULL;
692
693         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {   
694                 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
695                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
696                 spnego_state->state_position = SPNEGO_SERVER_TARG;
697         } else if (NT_STATUS_IS_OK(nt_status)) {
698                 if (unwrapped_out.data) {
699                         spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
700                 }
701                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
702                 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
703                 spnego_state->state_position = SPNEGO_DONE;
704         } else {
705                 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
706                 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
707                 spnego_state->state_position = SPNEGO_DONE;
708         }
709
710         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
711                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
712                 return NT_STATUS_INVALID_PARAMETER;
713         }
714
715         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
716
717         return nt_status;
718 }
719
720
721 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
722                                      const DATA_BLOB in, DATA_BLOB *out) 
723 {
724         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
725         DATA_BLOB null_data_blob = data_blob(NULL, 0);
726         DATA_BLOB mech_list_mic = data_blob(NULL, 0);
727         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
728         struct spnego_data spnego_out;
729         struct spnego_data spnego;
730
731         ssize_t len;
732
733         *out = data_blob(NULL, 0);
734
735         if (!out_mem_ctx) {
736                 out_mem_ctx = spnego_state;
737         }
738
739         /* and switch into the state machine */
740
741         switch (spnego_state->state_position) {
742         case SPNEGO_FALLBACK:
743                 return gensec_update(spnego_state->sub_sec_security,
744                                      out_mem_ctx, in, out);
745         case SPNEGO_SERVER_START:
746         {
747                 NTSTATUS nt_status;
748                 if (in.length) {
749
750                         len = spnego_read_data(gensec_security, in, &spnego);
751                         if (len == -1) {
752                                 return gensec_spnego_server_try_fallback(gensec_security, spnego_state, 
753                                                                          out_mem_ctx, in, out);
754                         }
755                         /* client sent NegTargetInit, we send NegTokenTarg */
756
757                         /* OK, so it's real SPNEGO, check the packet's the one we expect */
758                         if (spnego.type != spnego_state->expected_packet) {
759                                 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
760                                           spnego_state->expected_packet));
761                                 dump_data(1, in.data, in.length);
762                                 spnego_free_data(&spnego);
763                                 return NT_STATUS_INVALID_PARAMETER;
764                         }
765                         
766                         nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
767                                                                      spnego_state,
768                                                                      out_mem_ctx, 
769                                                                      spnego.negTokenInit.mechTypes,
770                                                                      spnego.negTokenInit.mechToken, 
771                                                                      &unwrapped_out);
772                         
773                         nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
774                                                                       spnego_state,
775                                                                       out_mem_ctx,
776                                                                       nt_status,
777                                                                       unwrapped_out,
778                                                                       null_data_blob,
779                                                                       out);
780                         
781                         spnego_free_data(&spnego);
782                         
783                         return nt_status;
784                 } else {
785                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
786                                                                       out_mem_ctx, in, out);
787                         spnego_state->state_position = SPNEGO_SERVER_START;
788                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
789                         return nt_status;
790                 }
791         }
792         
793         case SPNEGO_CLIENT_START:
794         {
795                 /* The server offers a list of mechanisms */
796                 
797                 const char *my_mechs[] = {NULL, NULL};
798                 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
799
800                 if (!in.length) {
801                         /* client to produce negTokenInit */
802                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
803                                                                  out_mem_ctx, in, out);
804                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
805                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
806                         return nt_status;
807                 }
808                 
809                 len = spnego_read_data(gensec_security, in, &spnego);
810                 
811                 if (len == -1) {
812                         DEBUG(1, ("Invalid SPNEGO request:\n"));
813                         dump_data(1, in.data, in.length);
814                         return NT_STATUS_INVALID_PARAMETER;
815                 }
816                 
817                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
818                 if (spnego.type != spnego_state->expected_packet) {
819                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
820                                   spnego_state->expected_packet));
821                         dump_data(1, in.data, in.length);
822                         spnego_free_data(&spnego);
823                         return NT_STATUS_INVALID_PARAMETER;
824                 }
825
826                 if (spnego.negTokenInit.targetPrincipal) {
827                         DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
828                         gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
829                 }
830
831                 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
832                                                              spnego_state,
833                                                              out_mem_ctx, 
834                                                              spnego.negTokenInit.mechTypes,
835                                                              spnego.negTokenInit.mechToken, 
836                                                              &unwrapped_out);
837
838                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
839                         spnego_free_data(&spnego);
840                         return nt_status;
841                 }
842
843                 my_mechs[0] = spnego_state->neg_oid;
844                 /* compose reply */
845                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
846                 spnego_out.negTokenInit.mechTypes = my_mechs;
847                 spnego_out.negTokenInit.reqFlags = 0;
848                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
849                 spnego_out.negTokenInit.mechToken = unwrapped_out;
850                 
851                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
852                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
853                                 return NT_STATUS_INVALID_PARAMETER;
854                 }
855                 
856                 /* set next state */
857                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
858                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
859
860                 if (NT_STATUS_IS_OK(nt_status)) {
861                         spnego_state->no_response_expected = true;
862                 }
863                 
864                 spnego_free_data(&spnego);
865                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
866         }
867         case SPNEGO_SERVER_TARG:
868         {
869                 NTSTATUS nt_status;
870                 bool new_spnego = false;
871
872                 if (!in.length) {
873                         return NT_STATUS_INVALID_PARAMETER;
874                 }
875                 
876                 len = spnego_read_data(gensec_security, in, &spnego);
877                 
878                 if (len == -1) {
879                         DEBUG(1, ("Invalid SPNEGO request:\n"));
880                         dump_data(1, in.data, in.length);
881                         return NT_STATUS_INVALID_PARAMETER;
882                 }
883                 
884                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
885                 if (spnego.type != spnego_state->expected_packet) {
886                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
887                                   spnego_state->expected_packet));
888                         dump_data(1, in.data, in.length);
889                         spnego_free_data(&spnego);
890                         return NT_STATUS_INVALID_PARAMETER;
891                 }
892
893                 if (!spnego_state->sub_sec_security) {
894                         DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
895                         spnego_free_data(&spnego);
896                         return NT_STATUS_INVALID_PARAMETER;
897                 }
898
899                 nt_status = gensec_update(spnego_state->sub_sec_security,
900                                           out_mem_ctx, 
901                                           spnego.negTokenTarg.responseToken,
902                                           &unwrapped_out);
903                 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
904                         new_spnego = true;
905                         nt_status = gensec_check_packet(spnego_state->sub_sec_security,
906                                                         out_mem_ctx,
907                                                         spnego_state->mech_types.data,
908                                                         spnego_state->mech_types.length,
909                                                         spnego_state->mech_types.data,
910                                                         spnego_state->mech_types.length,
911                                                         &spnego.negTokenTarg.mechListMIC);
912                         if (!NT_STATUS_IS_OK(nt_status)) {
913                                 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
914                                         nt_errstr(nt_status)));
915                         }
916                 }
917                 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
918                         nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
919                                                        out_mem_ctx,
920                                                        spnego_state->mech_types.data,
921                                                        spnego_state->mech_types.length,
922                                                        spnego_state->mech_types.data,
923                                                        spnego_state->mech_types.length,
924                                                        &mech_list_mic);
925                         if (!NT_STATUS_IS_OK(nt_status)) {
926                                 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
927                                         nt_errstr(nt_status)));
928                         }
929                 }
930
931                 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
932                                                               spnego_state,
933                                                               out_mem_ctx, 
934                                                               nt_status,
935                                                               unwrapped_out,
936                                                               mech_list_mic,
937                                                               out);
938                 
939                 spnego_free_data(&spnego);
940                 
941                 return nt_status;
942         }
943         case SPNEGO_CLIENT_TARG:
944         {
945                 NTSTATUS nt_status;
946                 if (!in.length) {
947                         return NT_STATUS_INVALID_PARAMETER;
948                 }
949                 
950                 len = spnego_read_data(gensec_security, in, &spnego);
951                 
952                 if (len == -1) {
953                         DEBUG(1, ("Invalid SPNEGO request:\n"));
954                         dump_data(1, in.data, in.length);
955                         return NT_STATUS_INVALID_PARAMETER;
956                 }
957                 
958                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
959                 if (spnego.type != spnego_state->expected_packet) {
960                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
961                                   spnego_state->expected_packet));
962                         dump_data(1, in.data, in.length);
963                         spnego_free_data(&spnego);
964                         return NT_STATUS_INVALID_PARAMETER;
965                 }
966         
967                 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
968                         spnego_free_data(&spnego);
969                         return NT_STATUS_ACCESS_DENIED;
970                 }
971
972                 /* Server didn't like our choice of mech, and chose something else */
973                 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
974                     spnego.negTokenTarg.supportedMech &&
975                     strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
976                         DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
977                                  gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech), 
978                                  gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
979                         
980                         talloc_free(spnego_state->sub_sec_security);
981                         nt_status = gensec_subcontext_start(spnego_state,
982                                                             gensec_security,
983                                                             &spnego_state->sub_sec_security);
984                         if (!NT_STATUS_IS_OK(nt_status)) {
985                                 spnego_free_data(&spnego);
986                                 return nt_status;
987                         }
988                         /* select the sub context */
989                         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
990                                                              spnego.negTokenTarg.supportedMech);
991                         if (!NT_STATUS_IS_OK(nt_status)) {
992                                 spnego_free_data(&spnego);
993                                 return nt_status;
994                         }
995
996                         nt_status = gensec_update(spnego_state->sub_sec_security,
997                                                   out_mem_ctx, 
998                                                   spnego.negTokenTarg.responseToken,
999                                                   &unwrapped_out);
1000                         spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1001                 } else if (spnego_state->no_response_expected) {
1002                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1003                                 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1004                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1005                         } else if (spnego.negTokenTarg.responseToken.length) {
1006                                 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1007                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1008                         } else {
1009                                 nt_status = NT_STATUS_OK;
1010                         }
1011                         if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1012                                 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1013                                                                 out_mem_ctx,
1014                                                                 spnego_state->mech_types.data,
1015                                                                 spnego_state->mech_types.length,
1016                                                                 spnego_state->mech_types.data,
1017                                                                 spnego_state->mech_types.length,
1018                                                                 &spnego.negTokenTarg.mechListMIC);
1019                                 if (!NT_STATUS_IS_OK(nt_status)) {
1020                                         DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1021                                                 nt_errstr(nt_status)));
1022                                 }
1023                         }
1024                 } else {
1025                         bool new_spnego = false;
1026
1027                         nt_status = gensec_update(spnego_state->sub_sec_security,
1028                                                   out_mem_ctx, 
1029                                                   spnego.negTokenTarg.responseToken, 
1030                                                   &unwrapped_out);
1031
1032                         if (NT_STATUS_IS_OK(nt_status)
1033                             && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1034                                 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1035                                                                  GENSEC_FEATURE_NEW_SPNEGO);
1036                         }
1037                         if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1038                                 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1039                                                                out_mem_ctx,
1040                                                                spnego_state->mech_types.data,
1041                                                                spnego_state->mech_types.length,
1042                                                                spnego_state->mech_types.data,
1043                                                                spnego_state->mech_types.length,
1044                                                                &mech_list_mic);
1045                                 if (!NT_STATUS_IS_OK(nt_status)) {
1046                                         DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1047                                                 nt_errstr(nt_status)));
1048                                 }
1049                         }
1050                         if (NT_STATUS_IS_OK(nt_status)) {
1051                                 spnego_state->no_response_expected = true;
1052                         }
1053                 } 
1054                 
1055                 spnego_free_data(&spnego);
1056
1057                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1058                         && !NT_STATUS_IS_OK(nt_status)) {
1059                         DEBUG(1, ("SPNEGO(%s) login failed: %s\n", 
1060                                   spnego_state->sub_sec_security->ops->name, 
1061                                   nt_errstr(nt_status)));
1062                         return nt_status;
1063                 }
1064
1065                 if (unwrapped_out.length || mech_list_mic.length) {
1066                         /* compose reply */
1067                         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1068                         spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1069                         spnego_out.negTokenTarg.supportedMech = NULL;
1070                         spnego_out.negTokenTarg.responseToken = unwrapped_out;
1071                         spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1072                         
1073                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1074                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1075                                 return NT_STATUS_INVALID_PARAMETER;
1076                         }
1077                 
1078                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
1079                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1080                 } else {
1081
1082                         /* all done - server has accepted, and we agree */
1083                         *out = null_data_blob;
1084
1085                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1086                                 /* unless of course it did not accept */
1087                                 DEBUG(1,("gensec_update ok but not accepted\n"));
1088                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1089                         }
1090                 
1091                         spnego_state->state_position = SPNEGO_DONE;
1092                 }
1093
1094                 return nt_status;
1095         }
1096         case SPNEGO_DONE:
1097                 /* We should not be called after we are 'done' */
1098                 return NT_STATUS_INVALID_PARAMETER;
1099         }
1100         return NT_STATUS_INVALID_PARAMETER;
1101 }
1102
1103 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1104                                        uint32_t feature)
1105 {
1106         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1107
1108         if (!spnego_state || !spnego_state->sub_sec_security) {
1109                 gensec_security->want_features |= feature;
1110                 return;
1111         }
1112
1113         gensec_want_feature(spnego_state->sub_sec_security,
1114                             feature);
1115 }
1116
1117 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1118                                        uint32_t feature) 
1119 {
1120         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1121         if (!spnego_state->sub_sec_security) {
1122                 return false;
1123         }
1124         
1125         return gensec_have_feature(spnego_state->sub_sec_security, 
1126                                    feature);
1127 }
1128
1129 static const char *gensec_spnego_oids[] = { 
1130         GENSEC_OID_SPNEGO,
1131         NULL 
1132 };
1133
1134 static const struct gensec_security_ops gensec_spnego_security_ops = {
1135         .name             = "spnego",
1136         .sasl_name        = "GSS-SPNEGO",
1137         .auth_type        = DCERPC_AUTH_TYPE_SPNEGO,
1138         .oid              = gensec_spnego_oids,
1139         .client_start     = gensec_spnego_client_start,
1140         .server_start     = gensec_spnego_server_start,
1141         .update           = gensec_spnego_update,
1142         .seal_packet      = gensec_spnego_seal_packet,
1143         .sign_packet      = gensec_spnego_sign_packet,
1144         .sig_size         = gensec_spnego_sig_size,
1145         .max_wrapped_size = gensec_spnego_max_wrapped_size,
1146         .max_input_size   = gensec_spnego_max_input_size,
1147         .check_packet     = gensec_spnego_check_packet,
1148         .unseal_packet    = gensec_spnego_unseal_packet,
1149         .packet_full_request = gensec_spnego_packet_full_request,
1150         .wrap             = gensec_spnego_wrap,
1151         .unwrap           = gensec_spnego_unwrap,
1152         .wrap_packets     = gensec_spnego_wrap_packets,
1153         .unwrap_packets   = gensec_spnego_unwrap_packets,
1154         .session_key      = gensec_spnego_session_key,
1155         .session_info     = gensec_spnego_session_info,
1156         .want_feature     = gensec_spnego_want_feature,
1157         .have_feature     = gensec_spnego_have_feature,
1158         .enabled          = true,
1159         .priority         = GENSEC_SPNEGO
1160 };
1161
1162 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1163 {
1164         NTSTATUS ret;
1165         ret = gensec_register(&gensec_spnego_security_ops);
1166         if (!NT_STATUS_IS_OK(ret)) {
1167                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1168                         gensec_spnego_security_ops.name));
1169                 return ret;
1170         }
1171
1172         return ret;
1173 }