1 /* $OpenBSD: gss-serv.c,v 1.23 2011/08/01 19:18:15 markus 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, NULL}, 0, 0};
60 ssh_gssapi_mech gssapi_null_mech =
61 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
64 extern ssh_gssapi_mech gssapi_kerberos_mech;
67 ssh_gssapi_mech* supported_mechs[]= {
69 &gssapi_kerberos_mech,
76 * Acquire credentials for a server running on the current host.
77 * Requires that the context structure contains a valid OID
80 /* Returns a GSSAPI error code */
81 /* Privileged (called from ssh_gssapi_server_ctx) */
83 ssh_gssapi_acquire_cred(Gssctxt *ctx)
86 char lname[MAXHOSTNAMELEN];
89 if (options.gss_strict_acceptor) {
90 gss_create_empty_oid_set(&status, &oidset);
91 gss_add_oid_set_member(&status, ctx->oid, &oidset);
93 if (gethostname(lname, MAXHOSTNAMELEN)) {
94 gss_release_oid_set(&status, &oidset);
98 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
99 gss_release_oid_set(&status, &oidset);
103 if ((ctx->major = gss_acquire_cred(&ctx->minor,
104 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
106 ssh_gssapi_error(ctx);
108 gss_release_oid_set(&status, &oidset);
111 ctx->name = GSS_C_NO_NAME;
112 ctx->creds = GSS_C_NO_CREDENTIAL;
114 return GSS_S_COMPLETE;
119 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
122 ssh_gssapi_delete_ctx(ctx);
123 ssh_gssapi_build_ctx(ctx);
124 ssh_gssapi_set_oid(*ctx, oid);
125 return (ssh_gssapi_acquire_cred(*ctx));
130 ssh_gssapi_server_mechanisms() {
131 gss_OID_set supported;
133 ssh_gssapi_supported_oids(&supported);
134 return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
140 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
145 res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
146 ssh_gssapi_delete_ctx(&ctx);
153 ssh_gssapi_supported_oids(gss_OID_set *oidset)
156 OM_uint32 min_status;
158 gss_OID_set supported;
160 gss_create_empty_oid_set(&min_status, oidset);
162 if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
165 while (supported_mechs[i]->name != NULL) {
166 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
167 &supported_mechs[i]->oid, supported, &present)))
170 gss_add_oid_set_member(&min_status,
171 &supported_mechs[i]->oid, oidset);
175 gss_release_oid_set(&min_status, &supported);
179 /* Wrapper around accept_sec_context
180 * Requires that the context contains:
182 * credentials (from ssh_gssapi_acquire_cred)
186 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
187 gss_buffer_desc *send_tok, OM_uint32 *flags)
192 ctx->major = gss_accept_sec_context(&ctx->minor,
193 &ctx->context, ctx->creds, recv_tok,
194 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
195 send_tok, flags, NULL, &ctx->client_creds);
197 if (GSS_ERROR(ctx->major))
198 ssh_gssapi_error(ctx);
200 if (ctx->client_creds)
201 debug("Received some client credentials");
203 debug("Got no client credentials");
207 /* Now, if we're complete and we have the right flags, then
208 * we flag the user as also having been authenticated
211 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
212 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
213 if (ssh_gssapi_getclient(ctx, &gssapi_client))
214 fatal("Couldn't convert client name");
221 * This parses an exported name, extracting the mechanism specific portion
222 * to use for ACL checking. It verifies that the name belongs the mechanism
223 * originally selected.
226 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
235 * Check that ename is long enough for all of the fixed length
236 * header, and that the initial ID bytes are correct
239 if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
240 return GSS_S_FAILURE;
243 * Extract the OID, and check it. Here GSSAPI breaks with tradition
244 * and does use the OID type and length bytes. To confuse things
245 * there are two lengths - the first including these, and the
249 oidl = get_u16(tok+2); /* length including next two bytes */
250 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
253 * Check the BER encoding for correct type and length, that the
254 * string is long enough and that the OID matches that in our context
256 if (tok[4] != 0x06 || tok[5] != oidl ||
257 ename->length < oidl+6 ||
258 !ssh_gssapi_check_oid(ctx, tok+6, oidl))
259 return GSS_S_FAILURE;
263 if (ename->length < offset+4)
264 return GSS_S_FAILURE;
266 name->length = get_u32(tok+offset);
269 if (UINT_MAX - offset < name->length)
270 return GSS_S_FAILURE;
271 if (ename->length < offset+name->length)
272 return GSS_S_FAILURE;
274 name->value = xmalloc(name->length+1);
275 memcpy(name->value, tok+offset, name->length);
276 ((char *)name->value)[name->length] = 0;
278 return GSS_S_COMPLETE;
281 /* Extract the client details from a given context. This can only reliably
282 * be called once for a context */
284 /* Privileged (called from accept_secure_ctx) */
286 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
290 gss_name_t new_name = GSS_C_NO_NAME;
291 gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
293 if (options.gss_store_rekey && client->used && ctx->client_creds) {
294 if (client->mech->oid.length != ctx->oid->length ||
295 (memcmp(client->mech->oid.elements,
296 ctx->oid->elements, ctx->oid->length) !=0)) {
297 debug("Rekeyed credentials have different mechanism");
298 return GSS_S_COMPLETE;
301 if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
302 ctx->client_creds, ctx->oid, &new_name,
303 NULL, NULL, NULL))) {
304 ssh_gssapi_error(ctx);
308 ctx->major = gss_compare_name(&ctx->minor, client->name,
311 if (GSS_ERROR(ctx->major)) {
312 ssh_gssapi_error(ctx);
317 debug("Rekeyed credentials have different name");
318 return GSS_S_COMPLETE;
321 debug("Marking rekeyed credentials for export");
323 gss_release_name(&ctx->minor, &client->name);
324 gss_release_cred(&ctx->minor, &client->creds);
325 client->name = new_name;
326 client->creds = ctx->client_creds;
327 ctx->client_creds = GSS_C_NO_CREDENTIAL;
329 return GSS_S_COMPLETE;
334 while (supported_mechs[i]->name != NULL) {
335 if (supported_mechs[i]->oid.length == ctx->oid->length &&
336 (memcmp(supported_mechs[i]->oid.elements,
337 ctx->oid->elements, ctx->oid->length) == 0))
338 client->mech = supported_mechs[i];
342 if (client->mech == NULL)
343 return GSS_S_FAILURE;
345 if (ctx->client_creds &&
346 (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
347 ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
348 ssh_gssapi_error(ctx);
352 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
353 &client->displayname, NULL))) {
354 ssh_gssapi_error(ctx);
358 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
360 ssh_gssapi_error(ctx);
364 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
365 &client->exportedname))) {
369 gss_release_buffer(&ctx->minor, &ename);
371 /* We can't copy this structure, so we just move the pointer to it */
372 client->creds = ctx->client_creds;
373 ctx->client_creds = GSS_C_NO_CREDENTIAL;
377 /* As user - called on fatal/exit */
379 ssh_gssapi_cleanup_creds(void)
381 if (gssapi_client.store.filename != NULL) {
382 /* Unlink probably isn't sufficient */
383 debug("removing gssapi cred file\"%s\"",
384 gssapi_client.store.filename);
385 unlink(gssapi_client.store.filename);
391 ssh_gssapi_storecreds(void)
393 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
394 (*gssapi_client.mech->storecreds)(&gssapi_client);
396 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
399 /* This allows GSSAPI methods to do things to the childs environment based
400 * on the passed authentication process and credentials.
404 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
407 if (gssapi_client.store.envvar != NULL &&
408 gssapi_client.store.envval != NULL) {
409 debug("Setting %s to %s", gssapi_client.store.envvar,
410 gssapi_client.store.envval);
411 child_set_env(envp, envsizep, gssapi_client.store.envvar,
412 gssapi_client.store.envval);
418 ssh_gssapi_userok(char *user, struct passwd *pw)
422 if (gssapi_client.exportedname.length == 0 ||
423 gssapi_client.exportedname.value == NULL) {
424 debug("No suitable client data");
427 if (gssapi_client.mech && gssapi_client.mech->userok)
428 if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
429 gssapi_client.used = 1;
430 gssapi_client.store.owner = pw;
433 /* Destroy delegated credentials if userok fails */
434 gss_release_buffer(&lmin, &gssapi_client.displayname);
435 gss_release_buffer(&lmin, &gssapi_client.exportedname);
436 gss_release_cred(&lmin, &gssapi_client.creds);
437 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
441 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
445 /* These bits are only used for rekeying. The unpriviledged child is running
446 * as the user, the monitor is root.
448 * In the child, we want to :
449 * *) Ask the monitor to store our credentials into the store we specify
450 * *) If it succeeds, maybe do a PAM update
456 static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
457 struct pam_response **resp, void *data)
459 return (PAM_CONV_ERR);
464 ssh_gssapi_rekey_creds() {
468 pam_handle_t *pamh = NULL;
469 struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
473 if (gssapi_client.store.filename == NULL &&
474 gssapi_client.store.envval == NULL &&
475 gssapi_client.store.envvar == NULL)
478 ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
483 debug("Rekeyed credentials stored successfully");
485 /* Actually managing to play with the ssh pam stack from here will
486 * be next to impossible. In any case, we may want different options
487 * for rekeying. So, use our own :)
491 debug("Not even going to try and do PAM with privsep disabled");
495 ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
500 xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
501 gssapi_client.store.envval);
503 ret = pam_putenv(pamh, envstr);
505 pam_setcred(pamh, PAM_REINITIALIZE_CRED);
506 pam_end(pamh, PAM_SUCCESS);
511 ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
514 /* Check we've got credentials to store */
515 if (!gssapi_client.updated)
518 gssapi_client.updated = 0;
520 temporarily_use_uid(gssapi_client.store.owner);
521 if (gssapi_client.mech && gssapi_client.mech->updatecreds)
522 ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
524 debug("No update function for this mechanism");