HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / gssapi / ntlm / accept_sec_context.c
1 /*
2  * Copyright (c) 2006 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "ntlm.h"
35
36 /*
37  *
38  */
39
40 OM_uint32
41 _gss_ntlm_allocate_ctx(OM_uint32 *minor_status, ntlm_ctx *ctx)
42 {
43     OM_uint32 maj_stat;
44     struct ntlm_server_interface *ns_interface = NULL;
45
46 #ifdef DIGEST
47     ns_interface = &ntlmsspi_kdc_digest;
48 #endif
49     if (ns_interface == NULL)
50         return GSS_S_FAILURE;
51
52     *ctx = calloc(1, sizeof(**ctx));
53     if (*ctx == NULL) {
54         *minor_status = ENOMEM;
55         return GSS_S_FAILURE;
56     }
57
58     (*ctx)->server = ns_interface;
59
60     maj_stat = (*(*ctx)->server->nsi_init)(minor_status, &(*ctx)->ictx);
61     if (maj_stat == GSS_S_COMPLETE)
62         return GSS_S_COMPLETE;
63
64     if (*ctx) 
65         free(*ctx);
66     (*ctx) = NULL;
67
68     return maj_stat;
69 }
70
71 /*
72  *
73  */
74
75 OM_uint32 GSSAPI_CALLCONV
76 _gss_ntlm_accept_sec_context
77 (OM_uint32 * minor_status,
78  gss_ctx_id_t * context_handle,
79  gss_const_cred_id_t acceptor_cred_handle,
80  const gss_buffer_t input_token_buffer,
81  const gss_channel_bindings_t input_chan_bindings,
82  gss_name_t * src_name,
83  gss_OID * mech_type,
84  gss_buffer_t output_token,
85  OM_uint32 * ret_flags,
86  OM_uint32 * time_rec,
87  gss_cred_id_t * delegated_cred_handle
88     )
89 {
90     krb5_error_code ret;
91     struct ntlm_buf data;
92     OM_uint32 junk;
93     ntlm_ctx ctx;
94
95     output_token->value = NULL;
96     output_token->length = 0;
97
98     *minor_status = 0;
99
100     if (context_handle == NULL)
101         return GSS_S_FAILURE;
102
103     if (input_token_buffer == GSS_C_NO_BUFFER)
104         return GSS_S_FAILURE;
105
106     if (src_name)
107         *src_name = GSS_C_NO_NAME;
108     if (mech_type)
109         *mech_type = GSS_C_NO_OID;
110     if (ret_flags)
111         *ret_flags = 0;
112     if (time_rec)
113         *time_rec = 0;
114     if (delegated_cred_handle)
115         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
116
117     if (*context_handle == GSS_C_NO_CONTEXT) {
118         struct ntlm_type1 type1;
119         OM_uint32 major_status;
120         OM_uint32 retflags;
121         struct ntlm_buf out;
122
123         major_status = _gss_ntlm_allocate_ctx(minor_status, &ctx);
124         if (major_status)
125             return major_status;
126         *context_handle = (gss_ctx_id_t)ctx;
127
128         /* check if the mechs is allowed by remote service */
129         major_status = (*ctx->server->nsi_probe)(minor_status, ctx->ictx, NULL);
130         if (major_status) {
131             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
132             return major_status;
133         }
134
135         data.data = input_token_buffer->value;
136         data.length = input_token_buffer->length;
137
138         ret = heim_ntlm_decode_type1(&data, &type1);
139         if (ret) {
140             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
141             *minor_status = ret;
142             return GSS_S_DEFECTIVE_TOKEN;
143         }
144
145         if ((type1.flags & NTLM_NEG_UNICODE) == 0) {
146             heim_ntlm_free_type1(&type1);
147             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
148             *minor_status = EINVAL;
149             return GSS_S_FAILURE;
150         }
151
152         if (type1.flags & NTLM_NEG_SIGN)
153             ctx->gssflags |= GSS_C_CONF_FLAG;
154         if (type1.flags & NTLM_NEG_SIGN)
155             ctx->gssflags |= GSS_C_INTEG_FLAG;
156
157         major_status = (*ctx->server->nsi_type2)(minor_status,
158                                                  ctx->ictx,
159                                                  type1.flags,
160                                                  type1.hostname,
161                                                  type1.domain,
162                                                  &retflags,
163                                                  &out);
164         heim_ntlm_free_type1(&type1);
165         if (major_status != GSS_S_COMPLETE) {
166             OM_uint32 gunk;
167             _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
168             return major_status;
169         }
170
171         output_token->value = malloc(out.length);
172         if (output_token->value == NULL && out.length != 0) {
173             OM_uint32 gunk;
174             _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
175             *minor_status = ENOMEM;
176             return GSS_S_FAILURE;
177         }
178         memcpy(output_token->value, out.data, out.length);
179         output_token->length = out.length;
180
181         ctx->flags = retflags;
182
183         return GSS_S_CONTINUE_NEEDED;
184     } else {
185         OM_uint32 maj_stat;
186         struct ntlm_type3 type3;
187         struct ntlm_buf session;
188
189         ctx = (ntlm_ctx)*context_handle;
190
191         data.data = input_token_buffer->value;
192         data.length = input_token_buffer->length;
193
194         ret = heim_ntlm_decode_type3(&data, 1, &type3);
195         if (ret) {
196             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
197             *minor_status = ret;
198             return GSS_S_DEFECTIVE_TOKEN;
199         }
200
201         maj_stat = (*ctx->server->nsi_type3)(minor_status,
202                                              ctx->ictx,
203                                              &type3,
204                                              &session);
205         if (maj_stat) {
206             heim_ntlm_free_type3(&type3);
207             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
208             return maj_stat;
209         }
210
211         if (src_name) {
212             ntlm_name n = calloc(1, sizeof(*n));
213             if (n) {
214                 n->user = strdup(type3.username);
215                 n->domain = strdup(type3.targetname);
216             }
217             if (n == NULL || n->user == NULL || n->domain == NULL) {
218                 gss_name_t tempn =  (gss_name_t)n;
219                 _gss_ntlm_release_name(&junk, &tempn);
220                 heim_ntlm_free_type3(&type3);
221                 _gss_ntlm_delete_sec_context(minor_status,
222                                              context_handle, NULL);
223                 return maj_stat;
224             }
225             *src_name = (gss_name_t)n;
226         }
227
228         heim_ntlm_free_type3(&type3);
229
230         ret = krb5_data_copy(&ctx->sessionkey,
231                              session.data, session.length);
232         if (ret) {
233             if (src_name)
234                 _gss_ntlm_release_name(&junk, src_name);
235             _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
236             *minor_status = ret;
237             return GSS_S_FAILURE;
238         }
239
240         _gss_ntlm_set_keys(ctx);
241
242         if (mech_type)
243             *mech_type = GSS_NTLM_MECHANISM;
244         if (time_rec)
245             *time_rec = GSS_C_INDEFINITE;
246
247         ctx->status |= STATUS_OPEN;
248
249         if (ret_flags)
250             *ret_flags = ctx->gssflags;
251
252         return GSS_S_COMPLETE;
253     }
254 }