1 /* $OpenBSD: auth2-gss.c,v 1.3 2003/09/01 20:44:54 markus Exp $ */
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "monitor_wrap.h"
44 extern ServerOptions options;
45 unsigned char ssh1_key_digest[16];
48 userauth_external(Authctxt *authctxt)
52 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
55 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
56 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
57 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
58 static void input_gssapi_errtok(int, u_int32_t, void *);
61 * We only support those mechanisms that we know about (ie ones that we know
62 * how to check local user kuserok and the like
65 userauth_gssapi(Authctxt *authctxt)
67 gss_OID_desc oid = {0, NULL};
70 gss_OID_set supported;
76 if (!authctxt->valid || authctxt->user == NULL)
79 mechs = packet_get_int();
81 debug("Mechanism negotiation is not supported");
85 ssh_gssapi_supported_oids(&supported);
92 doid = packet_get_string(&len);
94 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
95 logit("Mechanism OID received using the old encoding form");
99 oid.elements = doid + 2;
100 oid.length = len - 2;
102 gss_test_oid_set_member(&ms, &oid, supported, &present);
103 } while (mechs > 0 && !present);
105 gss_release_oid_set(&ms, &supported);
112 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
117 authctxt->methoddata=(void *)ctxt;
121 packet_start(SSH_SMSG_AUTH_GSSAPI_RESPONSE);
122 packet_put_string(oid.elements,oid.length);
126 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
128 /* Return OID in same format as we received it*/
129 packet_put_string(doid, len);
137 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, &input_gssapi_token);
139 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
140 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
141 authctxt->postponed = 1;
147 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
149 Authctxt *authctxt = ctxt;
151 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
152 gss_buffer_desc recv_tok;
153 OM_uint32 maj_status, min_status;
156 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
157 fatal("No authentication or GSSAPI context");
159 gssctxt = authctxt->methoddata;
160 recv_tok.value = packet_get_string(&len);
161 recv_tok.length = len; /* u_int vs. size_t */
165 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
168 xfree(recv_tok.value);
170 if (GSS_ERROR(maj_status)) {
171 ssh_gssapi_userauth_error(gssctxt);
172 if (send_tok.length != 0) {
174 packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
176 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
177 packet_put_string(send_tok.value, send_tok.length);
180 authctxt->postponed = 0;
181 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
182 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
183 userauth_finish(authctxt, 0, "gssapi");
185 if (send_tok.length != 0) {
187 packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
189 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
190 packet_put_string(send_tok.value, send_tok.length);
193 if (maj_status == GSS_S_COMPLETE) {
194 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
195 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
197 input_gssapi_exchange_complete(0, 0, ctxt);
199 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
200 &input_gssapi_exchange_complete);
204 gss_release_buffer(&min_status, &send_tok);
208 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
210 Authctxt *authctxt = ctxt;
212 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
213 gss_buffer_desc recv_tok;
214 OM_uint32 maj_status;
217 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
218 fatal("No authentication or GSSAPI context");
220 gssctxt = authctxt->methoddata;
221 recv_tok.value = packet_get_string(&len);
222 recv_tok.length = len;
226 /* Push the error token into GSSAPI to see what it says */
227 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
230 xfree(recv_tok.value);
232 /* We can't return anything to the client, even if we wanted to */
233 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
234 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
235 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
237 /* The client will have already moved on to the next auth */
239 gss_release_buffer(&maj_status, &send_tok);
243 * This is called when the client thinks we've completed authentication.
244 * It should only be enabled in the dispatch handler by the function above,
245 * which only enables it once the GSSAPI exchange is complete.
249 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
251 Authctxt *authctxt = ctxt;
255 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
256 fatal("No authentication or GSSAPI context");
258 if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
260 PRIVSEP(ssh_gssapi_localname(&lname));
261 if (lname && lname[0] != '\0') {
262 xfree(authctxt->user);
263 authctxt->user = lname;
264 debug("set username to %s from gssapi context", lname);
265 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
267 debug("failed to set username from gssapi context");
272 PRIVSEP(start_pam(authctxt->pw->pw_name));
280 gssctxt = authctxt->methoddata;
282 /* ssh1 needs to exchange the hash of the keys */
285 OM_uint32 min_status;
286 gss_buffer_desc dummy, msg_tok;
288 /* ssh1 wraps the keys, in the monitor */
290 dummy.value=malloc(sizeof(ssh1_key_digest));
291 memcpy(dummy.value,ssh1_key_digest,sizeof(ssh1_key_digest));
292 dummy.length=sizeof(ssh1_key_digest);
293 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(gssctxt,&dummy,&msg_tok))))
294 fatal("Couldn't wrap keys");
296 packet_start(SSH_SMSG_AUTH_GSSAPI_HASH);
297 packet_put_string((char *)msg_tok.value,msg_tok.length);
300 gss_release_buffer(&min_status,&msg_tok);
304 * We don't need to check the status, because the stored credentials
305 * which userok uses are only populated once the context init step
306 * has returned complete.
311 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
314 authctxt->postponed = 0;
315 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
316 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
317 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
318 userauth_finish(authctxt, authenticated, "gssapi");
321 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
325 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
328 packet_send_debug(errstr);
330 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
333 packet_put_cstring(errstr);
334 packet_put_cstring("");
342 Authmethod method_external = {
345 &options.gss_authentication
348 Authmethod method_gssapi = {
351 &options.gss_authentication