2 * Copyright (c) 2001-2003 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.
36 #include "monitor_wrap.h"
40 extern ServerOptions options;
43 userauth_external(Authctxt *authctxt)
47 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
50 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
51 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_errtok(int, u_int32_t, void *);
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;
70 if (!authctxt->valid || authctxt->user == NULL)
73 if (datafellows & SSH_OLD_GSSAPI) {
74 debug("Early drafts of GSSAPI userauth not supported");
78 mechs=packet_get_int();
80 debug("Mechanism negotiation is not supported");
84 ssh_gssapi_supported_oids(&supported);
91 debug("Trying to get OID string");
92 doid = packet_get_string(&len);
95 if (doid[0]!=0x06 || doid[1]!=len-2) {
96 log("Mechanism OID received using the old encoding form");
100 oid.elements = doid + 2;
101 oid.length = len - 2;
103 gss_test_oid_set_member(&ms, &oid, supported, &present);
104 } while (mechs>0 && !present);
111 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,&oid)))) {
112 ssh_gssapi_userauth_error(ctxt);
116 authctxt->methoddata=(void *)ctxt;
118 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
120 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
122 /* Just return whatever they sent */
123 packet_put_string(doid,len);
129 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
130 &input_gssapi_token);
131 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,
132 &input_gssapi_errtok);
133 authctxt->postponed = 1;
139 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
141 Authctxt *authctxt = ctxt;
143 gss_buffer_desc send_tok,recv_tok;
144 OM_uint32 maj_status, min_status;
147 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
148 fatal("No authentication or GSSAPI context");
150 gssctxt=authctxt->methoddata;
151 recv_tok.value=packet_get_string(&len);
152 recv_tok.length=len; /* u_int vs. size_t */
154 maj_status=PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
158 if (GSS_ERROR(maj_status)) {
159 ssh_gssapi_userauth_error(gssctxt);
160 if (send_tok.length != 0) {
161 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
162 packet_put_string(send_tok.value,send_tok.length);
166 authctxt->postponed = 0;
167 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
168 userauth_finish(authctxt, 0, "gssapi");
170 if (send_tok.length != 0) {
171 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
172 packet_put_string(send_tok.value,send_tok.length);
176 if (maj_status == GSS_S_COMPLETE) {
177 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
178 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
179 &input_gssapi_exchange_complete);
183 gss_release_buffer(&min_status, &send_tok);
187 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
189 Authctxt *authctxt = ctxt;
191 gss_buffer_desc send_tok,recv_tok;
192 OM_uint32 maj_status;
194 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
195 fatal("No authentication or GSSAPI context");
197 gssctxt=authctxt->methoddata;
198 recv_tok.value=packet_get_string(&recv_tok.length);
200 /* Push the error token into GSSAPI to see what it says */
201 maj_status=PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
205 /* We can't return anything to the client, even if we wanted to */
206 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
207 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,NULL);
209 /* The client will have already moved on to the next auth */
213 /* This is called when the client thinks we've completed authentication.
214 * It should only be enabled in the dispatch handler by the function above,
215 * which only enables it once the GSSAPI exchange is complete.
219 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
221 Authctxt *authctxt = ctxt;
225 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
226 fatal("No authentication or GSSAPI context");
228 gssctxt=authctxt->methoddata;
230 /* We don't need to check the status, because the stored credentials
231 * which userok uses are only populated once the context init step
232 * has returned complete.
235 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
237 authctxt->postponed = 0;
238 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
239 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
240 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
241 userauth_finish(authctxt, authenticated, "gssapi");
244 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
248 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
250 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
253 packet_put_cstring(errstr);
254 packet_put_cstring("");
261 Authmethod method_external = {
264 &options.gss_authentication
267 Authmethod method_gssapi = {
270 &options.gss_authentication