kernel: Remove last use cases of "%b" format.
[dragonfly.git] / crypto / openssh / auth2-gss.c
blob589283b720415332ee04bb44fe42b811f8e28b19
1 /* $OpenBSD: auth2-gss.c,v 1.26 2017/06/24 06:34:38 djm Exp $ */
3 /*
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
8 * are met:
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.
27 #include "includes.h"
29 #ifdef GSSAPI
31 #include <sys/types.h>
33 #include <stdarg.h>
35 #include "xmalloc.h"
36 #include "key.h"
37 #include "hostfile.h"
38 #include "auth.h"
39 #include "ssh2.h"
40 #include "log.h"
41 #include "dispatch.h"
42 #include "buffer.h"
43 #include "misc.h"
44 #include "servconf.h"
45 #include "packet.h"
46 #include "ssh-gss.h"
47 #include "monitor_wrap.h"
49 extern ServerOptions options;
51 static int input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh);
52 static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
53 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
54 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
57 * We only support those mechanisms that we know about (ie ones that we know
58 * how to check local user kuserok and the like)
60 static int
61 userauth_gssapi(struct ssh *ssh)
63 Authctxt *authctxt = ssh->authctxt;
64 gss_OID_desc goid = {0, NULL};
65 Gssctxt *ctxt = NULL;
66 int mechs;
67 int present;
68 OM_uint32 ms;
69 u_int len;
70 u_char *doid = NULL;
72 if (!authctxt->valid || authctxt->user == NULL)
73 return (0);
75 mechs = packet_get_int();
76 if (mechs == 0) {
77 debug("Mechanism negotiation is not supported");
78 return (0);
81 do {
82 mechs--;
84 free(doid);
86 present = 0;
87 doid = packet_get_string(&len);
89 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
90 doid[1] == len - 2) {
91 goid.elements = doid + 2;
92 goid.length = len - 2;
93 ssh_gssapi_test_oid_supported(&ms, &goid, &present);
94 } else {
95 logit("Badly formed OID received");
97 } while (mechs > 0 && !present);
99 if (!present) {
100 free(doid);
101 authctxt->server_caused_failure = 1;
102 return (0);
105 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
106 if (ctxt != NULL)
107 ssh_gssapi_delete_ctx(&ctxt);
108 free(doid);
109 authctxt->server_caused_failure = 1;
110 return (0);
113 authctxt->methoddata = (void *)ctxt;
115 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
117 /* Return the OID that we received */
118 packet_put_string(doid, len);
120 packet_send();
121 free(doid);
123 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
124 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
125 authctxt->postponed = 1;
127 return (0);
130 static int
131 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
133 Authctxt *authctxt = ssh->authctxt;
134 Gssctxt *gssctxt;
135 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
136 gss_buffer_desc recv_tok;
137 OM_uint32 maj_status, min_status, flags;
138 u_int len;
140 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
141 fatal("No authentication or GSSAPI context");
143 gssctxt = authctxt->methoddata;
144 recv_tok.value = packet_get_string(&len);
145 recv_tok.length = len; /* u_int vs. size_t */
147 packet_check_eom();
149 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
150 &send_tok, &flags));
152 free(recv_tok.value);
154 if (GSS_ERROR(maj_status)) {
155 if (send_tok.length != 0) {
156 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
157 packet_put_string(send_tok.value, send_tok.length);
158 packet_send();
160 authctxt->postponed = 0;
161 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
162 userauth_finish(ssh, 0, "gssapi-with-mic", NULL);
163 } else {
164 if (send_tok.length != 0) {
165 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
166 packet_put_string(send_tok.value, send_tok.length);
167 packet_send();
169 if (maj_status == GSS_S_COMPLETE) {
170 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
171 if (flags & GSS_C_INTEG_FLAG)
172 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC,
173 &input_gssapi_mic);
174 else
175 ssh_dispatch_set(ssh,
176 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
177 &input_gssapi_exchange_complete);
181 gss_release_buffer(&min_status, &send_tok);
182 return 0;
185 static int
186 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
188 Authctxt *authctxt = ssh->authctxt;
189 Gssctxt *gssctxt;
190 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
191 gss_buffer_desc recv_tok;
192 OM_uint32 maj_status;
193 u_int len;
195 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
196 fatal("No authentication or GSSAPI context");
198 gssctxt = authctxt->methoddata;
199 recv_tok.value = packet_get_string(&len);
200 recv_tok.length = len;
202 packet_check_eom();
204 /* Push the error token into GSSAPI to see what it says */
205 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
206 &send_tok, NULL));
208 free(recv_tok.value);
210 /* We can't return anything to the client, even if we wanted to */
211 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
212 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
214 /* The client will have already moved on to the next auth */
216 gss_release_buffer(&maj_status, &send_tok);
217 return 0;
221 * This is called when the client thinks we've completed authentication.
222 * It should only be enabled in the dispatch handler by the function above,
223 * which only enables it once the GSSAPI exchange is complete.
226 static int
227 input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
229 Authctxt *authctxt = ssh->authctxt;
230 int authenticated;
231 const char *displayname;
233 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
234 fatal("No authentication or GSSAPI context");
237 * We don't need to check the status, because we're only enabled in
238 * the dispatcher once the exchange is complete
241 packet_check_eom();
243 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
245 if ((!use_privsep || mm_is_monitor()) &&
246 (displayname = ssh_gssapi_displayname()) != NULL)
247 auth2_record_info(authctxt, "%s", displayname);
249 authctxt->postponed = 0;
250 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
251 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
252 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
253 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
254 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
255 return 0;
258 static int
259 input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
261 Authctxt *authctxt = ssh->authctxt;
262 Gssctxt *gssctxt;
263 int authenticated = 0;
264 Buffer b;
265 gss_buffer_desc mic, gssbuf;
266 u_int len;
267 const char *displayname;
269 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
270 fatal("No authentication or GSSAPI context");
272 gssctxt = authctxt->methoddata;
274 mic.value = packet_get_string(&len);
275 mic.length = len;
277 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
278 "gssapi-with-mic");
280 gssbuf.value = buffer_ptr(&b);
281 gssbuf.length = buffer_len(&b);
283 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
284 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
285 else
286 logit("GSSAPI MIC check failed");
288 buffer_free(&b);
289 free(mic.value);
291 if ((!use_privsep || mm_is_monitor()) &&
292 (displayname = ssh_gssapi_displayname()) != NULL)
293 auth2_record_info(authctxt, "%s", displayname);
295 authctxt->postponed = 0;
296 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
297 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
298 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
299 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
300 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
301 return 0;
304 Authmethod method_gssapi = {
305 "gssapi-with-mic",
306 userauth_gssapi,
307 &options.gss_authentication
310 #endif /* GSSAPI */