2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
37 __RCSID("$Heimdal: auth_gssapi.c 13932 2004-06-15 11:24:21Z joda $"
41 #if defined(SASL) && defined(KRB5)
44 extern krb5_context gssapi_krb5_context
;
47 gss_ctx_id_t context_hdl
;
49 gss_name_t client_name
;
54 gss_set_error (struct gss_state
*gs
, int min_stat
)
57 OM_uint32 msg_ctx
= 0;
58 gss_buffer_desc status_string
;
62 ret
= gss_display_status (&new_stat
,
68 pop_auth_set_error(status_string
.value
);
69 gss_release_buffer (&new_stat
, &status_string
);
70 } while (!GSS_ERROR(ret
) && msg_ctx
!= 0);
74 gss_loop(POP
*p
, void *state
,
75 /* const */ void *input
, size_t input_length
,
76 void **output
, size_t *output_length
)
78 struct gss_state
*gs
= state
;
79 gss_buffer_desc real_input_token
, real_output_token
;
80 gss_buffer_t input_token
= &real_input_token
,
81 output_token
= &real_output_token
;
82 OM_uint32 maj_stat
, min_stat
;
83 gss_channel_bindings_t bindings
= GSS_C_NO_CHANNEL_BINDINGS
;
86 /* we require an initial response, so ask for one if not
89 if(input
== NULL
&& input_length
== 0) {
90 /* XXX this could be done better */
91 fputs("+ \r\n", p
->output
);
93 return POP_AUTH_CONTINUE
;
97 input_token
->value
= input
;
98 input_token
->length
= input_length
;
100 gss_accept_sec_context (&min_stat
,
111 if (GSS_ERROR(maj_stat
)) {
112 gss_set_error(gs
, min_stat
);
113 return POP_AUTH_FAILURE
;
115 if (output_token
->length
!= 0) {
116 *output
= output_token
->value
;
117 *output_length
= output_token
->length
;
119 if(maj_stat
== GSS_S_COMPLETE
)
122 return POP_AUTH_CONTINUE
;
126 /* send wanted protection levels */
127 unsigned char x
[4] = { 1, 0, 0, 0 };
129 input_token
->value
= x
;
130 input_token
->length
= 4;
132 maj_stat
= gss_wrap(&min_stat
,
139 if (GSS_ERROR(maj_stat
)) {
140 gss_set_error(gs
, min_stat
);
141 return POP_AUTH_FAILURE
;
143 *output
= output_token
->value
;
144 *output_length
= output_token
->length
;
146 return POP_AUTH_CONTINUE
;
149 /* receive protection levels and username */
151 krb5_principal principal
;
152 gss_buffer_desc export_name
;
156 input_token
->value
= input
;
157 input_token
->length
= input_length
;
159 maj_stat
= gss_unwrap (&min_stat
,
165 if (GSS_ERROR(maj_stat
)) {
166 gss_set_error(gs
, min_stat
);
167 return POP_AUTH_FAILURE
;
169 if(output_token
->length
< 5) {
170 pop_auth_set_error("response too short");
171 return POP_AUTH_FAILURE
;
173 ptr
= output_token
->value
;
175 pop_auth_set_error("must use clear text");
176 return POP_AUTH_FAILURE
;
178 memmove(output_token
->value
, ptr
+ 4, output_token
->length
- 4);
179 ptr
[output_token
->length
- 4] = '\0';
181 maj_stat
= gss_display_name(&min_stat
, gs
->client_name
,
183 if(maj_stat
!= GSS_S_COMPLETE
) {
184 gss_set_error(gs
, min_stat
);
185 return POP_AUTH_FAILURE
;
188 if(oid
!= GSS_KRB5_NT_PRINCIPAL_NAME
) {
189 pop_auth_set_error("unexpected gss name type");
190 gss_release_buffer(&min_stat
, &export_name
);
191 return POP_AUTH_FAILURE
;
193 name
= malloc(export_name
.length
+ 1);
195 pop_auth_set_error("out of memory");
196 gss_release_buffer(&min_stat
, &export_name
);
197 return POP_AUTH_FAILURE
;
199 memcpy(name
, export_name
.value
, export_name
.length
);
200 name
[export_name
.length
] = '\0';
201 gss_release_buffer(&min_stat
, &export_name
);
202 krb5_parse_name(gssapi_krb5_context
, name
, &principal
);
204 if(!krb5_kuserok(gssapi_krb5_context
, principal
, ptr
)) {
205 pop_auth_set_error("Permission denied");
206 return POP_AUTH_FAILURE
;
210 strlcpy(p
->user
, ptr
, sizeof(p
->user
));
211 return POP_AUTH_COMPLETE
;
213 return POP_AUTH_FAILURE
;
218 gss_init(POP
*p
, void **state
)
220 struct gss_state
*gs
= malloc(sizeof(*gs
));
222 pop_auth_set_error("out of memory");
223 return POP_AUTH_FAILURE
;
225 gs
->context_hdl
= GSS_C_NO_CONTEXT
;
228 return POP_AUTH_CONTINUE
;
232 gss_cleanup(POP
*p
, void *state
)
235 struct gss_state
*gs
= state
;
236 if(gs
->context_hdl
!= GSS_C_NO_CONTEXT
)
237 gss_delete_sec_context(&min_stat
, &gs
->context_hdl
, GSS_C_NO_BUFFER
);
239 return POP_AUTH_CONTINUE
;
242 struct auth_mech gssapi_mech
= {
243 "GSSAPI", gss_init
, gss_loop
, gss_cleanup