2 * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "monitor_wrap.h"
41 extern ServerOptions options;
42 extern unsigned char ssh1_key_digest[16];
45 userauth_external(Authctxt *authctxt)
49 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
52 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
55 /* We only support those mechanisms that we know about (ie ones that we know
56 * how to check local user kuserok and the like
59 userauth_gssapi(Authctxt *authctxt)
61 gss_OID_desc oid= {0,NULL};
64 gss_OID_set supported;
69 if (!authctxt->valid || authctxt->user == NULL)
72 if (datafellows & SSH_OLD_GSSAPI) {
73 debug("Early drafts of GSSAPI userauth not supported");
77 mechs=packet_get_int();
79 debug("Mechanism negotiation is not supported");
83 ssh_gssapi_supported_oids(&supported);
87 oid.elements = packet_get_string(&len);
89 gss_test_oid_set_member(&ms, &oid, supported, &present);
91 } while (mechs>0 && !present);
98 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,&oid))))
101 authctxt->methoddata=(void *)ctxt;
103 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
106 packet_start(SSH_SMSG_AUTH_GSSAPI_RESPONSE);
108 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
109 packet_put_string(oid.elements,oid.length);
115 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN,
116 &input_gssapi_token);
118 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
119 &input_gssapi_token);
120 authctxt->postponed = 1;
126 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
128 Authctxt *authctxt = ctxt;
130 gss_buffer_desc send_tok,recv_tok;
131 OM_uint32 maj_status, min_status;
133 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
134 fatal("No authentication or GSSAPI context");
136 gssctxt=authctxt->methoddata;
137 recv_tok.value=packet_get_string(&recv_tok.length);
139 maj_status=PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
143 if (GSS_ERROR(maj_status)) {
144 /* Failure <sniff> */
145 ssh_gssapi_send_error(maj_status,min_status);
146 authctxt->postponed = 0;
147 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
148 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
149 userauth_finish(authctxt, 0, "gssapi");
152 if (send_tok.length != 0) {
153 /* Send a packet back to the client */
155 packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
157 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
158 packet_put_string(send_tok.value,send_tok.length);
161 gss_release_buffer(&min_status, &send_tok);
164 if (maj_status == GSS_S_COMPLETE) {
165 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
166 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
167 /* ssh1 does not have an extra message here */
169 input_gssapi_exchange_complete(0, 0, ctxt);
171 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
172 &input_gssapi_exchange_complete);
176 /* This is called when the client thinks we've completed authentication.
177 * It should only be enabled in the dispatch handler by the function above,
178 * which only enables it once the GSSAPI exchange is complete.
182 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
184 Authctxt *authctxt = ctxt;
188 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
189 fatal("No authentication or GSSAPI context");
191 if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
193 PRIVSEP(ssh_gssapi_localname(&lname));
194 if (lname && lname[0] != '\0') {
195 xfree(authctxt->user);
196 authctxt->user = lname;
197 debug("set username to %s from gssapi context", lname);
198 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
200 debug("failed to set username from gssapi context");
205 PRIVSEP(start_pam(authctxt->pw->pw_name));
213 gssctxt=authctxt->methoddata;
215 /* ssh1 needs to exchange the hash of the keys */
218 OM_uint32 min_status;
219 gss_buffer_desc dummy, msg_tok;
221 /* ssh1 wraps the keys, in the monitor */
223 dummy.value=malloc(sizeof(ssh1_key_digest));
224 memcpy(dummy.value,ssh1_key_digest,sizeof(ssh1_key_digest));
225 dummy.length=sizeof(ssh1_key_digest);
226 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(gssctxt,&dummy,&msg_tok))))
227 fatal("Couldn't wrap keys");
229 packet_start(SSH_SMSG_AUTH_GSSAPI_HASH);
230 packet_put_string((char *)msg_tok.value,msg_tok.length);
233 gss_release_buffer(&min_status,&msg_tok);
237 /* We don't need to check the status, because the stored credentials
238 * which userok uses are only populated once the context init step
239 * has returned complete.
242 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
245 authctxt->postponed = 0;
246 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
247 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
248 userauth_finish(authctxt, authenticated, "gssapi");
251 Authmethod method_external = {
254 &options.gss_authentication
257 Authmethod method_gssapi = {
260 &options.gss_authentication