1 /* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
4 * Copyright (c) 2001-2009 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.
31 #include <sys/types.h>
32 #include <sys/param.h>
38 #include "openbsd-compat/sys-queue.h"
52 #include "monitor_wrap.h"
54 extern ServerOptions options;
56 static ssh_gssapi_client gssapi_client =
57 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
58 GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, {NULL, NULL, NULL}};
61 * Acquire credentials for a server running on the current host.
62 * Requires that the context structure contains a valid OID
65 /* Returns a GSSAPI error code */
66 /* Privileged (called from ssh_gssapi_server_ctx) */
68 ssh_gssapi_acquire_cred(Gssctxt *ctx)
71 char lname[MAXHOSTNAMELEN];
74 if (options.gss_strict_acceptor) {
75 gss_create_empty_oid_set(&status, &oidset);
76 gss_add_oid_set_member(&status, ctx->oid, &oidset);
78 if (gethostname(lname, MAXHOSTNAMELEN)) {
79 gss_release_oid_set(&status, &oidset);
83 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
84 gss_release_oid_set(&status, &oidset);
88 if ((ctx->major = gss_acquire_cred(&ctx->minor,
89 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
91 ssh_gssapi_error(ctx);
93 gss_release_oid_set(&status, &oidset);
96 ctx->name = GSS_C_NO_NAME;
97 ctx->creds = GSS_C_NO_CREDENTIAL;
99 return GSS_S_COMPLETE;
104 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
107 ssh_gssapi_delete_ctx(ctx);
108 ssh_gssapi_build_ctx(ctx);
109 ssh_gssapi_set_oid(*ctx, oid);
110 return (ssh_gssapi_acquire_cred(*ctx));
115 ssh_gssapi_server_mechanisms() {
116 gss_OID_set supported;
118 ssh_gssapi_supported_oids(&supported);
119 return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
125 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
130 res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
131 ssh_gssapi_delete_ctx(&ctx);
138 ssh_gssapi_supported_oids(gss_OID_set *oidset)
140 OM_uint32 min_status;
142 gss_indicate_mechs(&min_status, oidset);
146 /* Wrapper around accept_sec_context
147 * Requires that the context contains:
149 * credentials (from ssh_gssapi_acquire_cred)
153 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
154 gss_buffer_desc *send_tok, OM_uint32 *flags)
159 ctx->major = gss_accept_sec_context(&ctx->minor,
160 &ctx->context, ctx->creds, recv_tok,
161 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
162 send_tok, flags, NULL, &ctx->client_creds);
164 if (GSS_ERROR(ctx->major))
165 ssh_gssapi_error(ctx);
167 if (ctx->client_creds)
168 debug("Received some client credentials");
169 else if (ctx->major == GSS_S_COMPLETE)
170 debug("Got no client credentials");
174 /* Now, if we're complete and we have the right flags, then
175 * we flag the user as also having been authenticated
178 if (ctx->major == GSS_S_COMPLETE) {
179 if (options.gss_require_mic &&
180 ((flags == NULL) || !(*flags & GSS_C_INTEG_FLAG))) {
181 debug("GSSAPIRequireMIC true and integrity protection not supported so gssapi-with-mic fails.");
182 } else if (ssh_gssapi_getclient(ctx, &gssapi_client)) {
183 fatal("Couldn't convert client name");
191 * This parses an exported name, extracting the mechanism specific portion
192 * to use for ACL checking. It verifies that the name belongs the mechanism
193 * originally selected.
196 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
205 * Check that ename is long enough for all of the fixed length
206 * header, and that the initial ID bytes are correct
209 if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
210 return GSS_S_FAILURE;
213 * Extract the OID, and check it. Here GSSAPI breaks with tradition
214 * and does use the OID type and length bytes. To confuse things
215 * there are two lengths - the first including these, and the
219 oidl = get_u16(tok+2); /* length including next two bytes */
220 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
223 * Check the BER encoding for correct type and length, that the
224 * string is long enough and that the OID matches that in our context
226 if (tok[4] != 0x06 || tok[5] != oidl ||
227 ename->length < oidl+6 ||
228 !ssh_gssapi_check_oid(ctx, tok+6, oidl))
229 return GSS_S_FAILURE;
233 if (ename->length < offset+4)
234 return GSS_S_FAILURE;
236 name->length = get_u32(tok+offset);
239 if (ename->length < offset+name->length)
240 return GSS_S_FAILURE;
242 name->value = xmalloc(name->length+1);
243 memcpy(name->value, tok+offset, name->length);
244 ((char *)name->value)[name->length] = 0;
246 return GSS_S_COMPLETE;
249 /* Extract the client details from a given context. This can only reliably
250 * be called once for a context */
252 /* Privileged (called from accept_secure_ctx) */
254 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
256 gss_buffer_desc ename;
258 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
259 &client->displayname, NULL))) {
260 ssh_gssapi_error(ctx);
264 if ((ctx->major = gss_duplicate_name(&ctx->minor, ctx->client,
266 ssh_gssapi_error(ctx);
270 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
272 ssh_gssapi_error(ctx);
276 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
277 &client->exportedname))) {
281 gss_release_buffer(&ctx->minor, &ename);
283 /* We can't copy this structure, so we just move the pointer to it */
284 client->creds = ctx->client_creds;
285 ctx->client_creds = GSS_C_NO_CREDENTIAL;
289 /* As user - called on fatal/exit */
291 ssh_gssapi_cleanup_creds(void)
293 if (gssapi_client.store.filename != NULL) {
294 /* Unlink probably isn't sufficient */
295 debug("removing gssapi cred file\"%s\"",
296 gssapi_client.store.filename);
297 unlink(gssapi_client.store.filename);
303 ssh_gssapi_storecreds(void)
307 gss_store_cred(&lmin, gssapi_client.creds,
308 GSS_C_INITIATE, GSS_C_NO_OID,
312 /* This allows GSSAPI methods to do things to the childs environment based
313 * on the passed authentication process and credentials.
317 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
320 if (gssapi_client.store.envvar != NULL &&
321 gssapi_client.store.envval != NULL) {
322 debug("Setting %s to %s", gssapi_client.store.envvar,
323 gssapi_client.store.envval);
324 child_set_env(envp, envsizep, gssapi_client.store.envvar,
325 gssapi_client.store.envval);
331 ssh_gssapi_userok(char *user, struct passwd *pw)
336 if (gssapi_client.exportedname.length == 0 ||
337 gssapi_client.exportedname.value == NULL) {
338 debug("No suitable client data");
342 userok = gss_userok(gssapi_client.name, user);
344 gssapi_client.used = 1;
345 gssapi_client.store.owner = pw;
347 /* Destroy delegated credentials if userok fails */
348 gss_release_buffer(&lmin, &gssapi_client.displayname);
349 gss_release_buffer(&lmin, &gssapi_client.exportedname);
350 gss_release_name(&lmin, &gssapi_client.name);
351 gss_release_cred(&lmin, &gssapi_client.creds);
352 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
360 ssh_gssapi_localname(char **user)
362 OM_uint32 major_status, lmin;
366 major_status = gss_pname_to_uid(&lmin, gssapi_client.name,
368 if (GSS_ERROR(major_status))
369 return (major_status);
373 return (GSS_S_BAD_NAME);
375 *user = xstrdup(pw->pw_name);
377 return (GSS_S_COMPLETE);