dcesrv_core: better fault codes dcesrv_auth_prepare_auth3()
[Samba.git] / third_party / heimdal / lib / gssapi / ntlm / accept_sec_context.c
blob6a3e8899ee76e5e8ec1ae6550fb92d329d5295f0
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "ntlm.h"
40 OM_uint32
41 _gss_ntlm_allocate_ctx(OM_uint32 *minor_status, ntlm_ctx *ctx)
43 OM_uint32 maj_stat;
44 struct ntlm_server_interface *ns_interface = NULL;
46 #ifdef DIGEST
47 ns_interface = &ntlmsspi_kdc_digest;
48 #endif
49 if (ns_interface == NULL)
50 return GSS_S_FAILURE;
52 *ctx = calloc(1, sizeof(**ctx));
53 if (*ctx == NULL) {
54 *minor_status = ENOMEM;
55 return GSS_S_FAILURE;
58 (*ctx)->server = ns_interface;
60 maj_stat = (*(*ctx)->server->nsi_init)(minor_status, &(*ctx)->ictx);
61 if (maj_stat == GSS_S_COMPLETE)
62 return GSS_S_COMPLETE;
64 if (*ctx)
65 free(*ctx);
66 (*ctx) = NULL;
68 return maj_stat;
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
90 krb5_error_code ret;
91 struct ntlm_buf data;
92 OM_uint32 junk;
93 ntlm_ctx ctx;
95 output_token->value = NULL;
96 output_token->length = 0;
98 *minor_status = 0;
100 if (context_handle == NULL)
101 return GSS_S_FAILURE;
103 if (input_token_buffer == GSS_C_NO_BUFFER)
104 return GSS_S_FAILURE;
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;
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;
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;
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;
135 data.data = input_token_buffer->value;
136 data.length = input_token_buffer->length;
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;
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;
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;
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;
171 output_token->value = malloc(out.length);
172 if (output_token->value == NULL && out.length != 0) {
173 OM_uint32 gunk;
174 heim_ntlm_free_buf(&out);
175 _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
176 *minor_status = ENOMEM;
177 return GSS_S_FAILURE;
179 memcpy(output_token->value, out.data, out.length);
180 output_token->length = out.length;
181 heim_ntlm_free_buf(&out);
183 ctx->flags = retflags;
185 return GSS_S_CONTINUE_NEEDED;
186 } else {
187 OM_uint32 maj_stat;
188 struct ntlm_type3 type3;
189 struct ntlm_buf session;
191 ctx = (ntlm_ctx)*context_handle;
193 data.data = input_token_buffer->value;
194 data.length = input_token_buffer->length;
196 ret = heim_ntlm_decode_type3(&data, 1, &type3);
197 if (ret) {
198 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
199 *minor_status = ret;
200 return GSS_S_DEFECTIVE_TOKEN;
203 maj_stat = (*ctx->server->nsi_type3)(minor_status,
204 ctx->ictx,
205 &type3,
206 &session);
207 if (maj_stat) {
208 heim_ntlm_free_type3(&type3);
209 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
210 return maj_stat;
213 if (src_name) {
214 ntlm_name n = calloc(1, sizeof(*n));
215 if (n) {
216 n->user = strdup(type3.username);
217 n->domain = strdup(type3.targetname);
219 if (n == NULL || n->user == NULL || n->domain == NULL) {
220 gss_name_t tempn = (gss_name_t)n;
221 _gss_ntlm_release_name(&junk, &tempn);
222 heim_ntlm_free_type3(&type3);
223 _gss_ntlm_delete_sec_context(minor_status,
224 context_handle, NULL);
225 return maj_stat;
227 *src_name = (gss_name_t)n;
230 heim_ntlm_free_type3(&type3);
232 ret = krb5_data_copy(&ctx->sessionkey,
233 session.data, session.length);
234 if (ret) {
235 if (src_name)
236 _gss_ntlm_release_name(&junk, src_name);
237 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
238 *minor_status = ret;
239 return GSS_S_FAILURE;
242 _gss_ntlm_set_keys(ctx);
244 if (mech_type)
245 *mech_type = GSS_NTLM_MECHANISM;
246 if (time_rec)
247 *time_rec = GSS_C_INDEFINITE;
249 ctx->status |= STATUS_OPEN;
251 if (ret_flags)
252 *ret_flags = ctx->gssflags;
254 return GSS_S_COMPLETE;