2008-07-04 02:54:25 +00:00
|
|
|
/* $OpenBSD: auth2-pubkey.c,v 1.19 2008/07/03 21:46:58 otto Exp $ */
|
2002-06-06 20:27:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 00:45:54 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2008-06-13 04:51:28 +00:00
|
|
|
#include <fcntl.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <pwd.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <stdarg.h>
|
2008-06-13 23:01:24 +00:00
|
|
|
#include <unistd.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "xmalloc.h"
|
2004-12-06 11:47:41 +00:00
|
|
|
#include "ssh.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "ssh2.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "servconf.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "key.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "hostfile.h"
|
|
|
|
#include "auth.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "uidswap.h"
|
|
|
|
#include "auth-options.h"
|
|
|
|
#include "canohost.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#ifdef GSSAPI
|
|
|
|
#include "ssh-gss.h"
|
|
|
|
#endif
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "monitor_wrap.h"
|
2004-12-11 02:39:50 +00:00
|
|
|
#include "misc.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
/* import */
|
|
|
|
extern ServerOptions options;
|
|
|
|
extern u_char *session_id2;
|
2003-06-28 02:38:01 +00:00
|
|
|
extern u_int session_id2_len;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
userauth_pubkey(Authctxt *authctxt)
|
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
Key *key = NULL;
|
|
|
|
char *pkalg;
|
|
|
|
u_char *pkblob, *sig;
|
|
|
|
u_int alen, blen, slen;
|
|
|
|
int have_sig, pktype;
|
|
|
|
int authenticated = 0;
|
|
|
|
|
|
|
|
if (!authctxt->valid) {
|
|
|
|
debug2("userauth_pubkey: disabled because of invalid user");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
have_sig = packet_get_char();
|
|
|
|
if (datafellows & SSH_BUG_PKAUTH) {
|
|
|
|
debug2("userauth_pubkey: SSH_BUG_PKAUTH");
|
|
|
|
/* no explicit pkalg given */
|
|
|
|
pkblob = packet_get_string(&blen);
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, pkblob, blen);
|
|
|
|
/* so we have to extract the pkalg from the pkblob */
|
|
|
|
pkalg = buffer_get_string(&b, &alen);
|
|
|
|
buffer_free(&b);
|
|
|
|
} else {
|
|
|
|
pkalg = packet_get_string(&alen);
|
|
|
|
pkblob = packet_get_string(&blen);
|
|
|
|
}
|
|
|
|
pktype = key_type_from_name(pkalg);
|
|
|
|
if (pktype == KEY_UNSPEC) {
|
|
|
|
/* this is perfectly legal */
|
2003-04-09 10:59:48 +00:00
|
|
|
logit("userauth_pubkey: unsupported public key algorithm: %s",
|
2002-06-06 20:27:55 +00:00
|
|
|
pkalg);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
key = key_from_blob(pkblob, blen);
|
|
|
|
if (key == NULL) {
|
|
|
|
error("userauth_pubkey: cannot decode key: %s", pkalg);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (key->type != pktype) {
|
|
|
|
error("userauth_pubkey: type mismatch for decoded key "
|
|
|
|
"(received %d, expected %d)", key->type, pktype);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (have_sig) {
|
|
|
|
sig = packet_get_string(&slen);
|
|
|
|
packet_check_eom();
|
|
|
|
buffer_init(&b);
|
|
|
|
if (datafellows & SSH_OLD_SESSIONID) {
|
|
|
|
buffer_append(&b, session_id2, session_id2_len);
|
|
|
|
} else {
|
|
|
|
buffer_put_string(&b, session_id2, session_id2_len);
|
|
|
|
}
|
|
|
|
/* reconstruct packet */
|
|
|
|
buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
|
|
|
|
buffer_put_cstring(&b, authctxt->user);
|
|
|
|
buffer_put_cstring(&b,
|
|
|
|
datafellows & SSH_BUG_PKSERVICE ?
|
|
|
|
"ssh-userauth" :
|
|
|
|
authctxt->service);
|
|
|
|
if (datafellows & SSH_BUG_PKAUTH) {
|
|
|
|
buffer_put_char(&b, have_sig);
|
|
|
|
} else {
|
|
|
|
buffer_put_cstring(&b, "publickey");
|
|
|
|
buffer_put_char(&b, have_sig);
|
|
|
|
buffer_put_cstring(&b, pkalg);
|
|
|
|
}
|
|
|
|
buffer_put_string(&b, pkblob, blen);
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
buffer_dump(&b);
|
|
|
|
#endif
|
|
|
|
/* test for correct signature */
|
|
|
|
authenticated = 0;
|
|
|
|
if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
|
|
|
|
PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
|
2004-01-21 00:02:50 +00:00
|
|
|
buffer_len(&b))) == 1)
|
2002-06-06 20:27:55 +00:00
|
|
|
authenticated = 1;
|
2004-01-21 00:02:50 +00:00
|
|
|
buffer_free(&b);
|
2002-06-06 20:27:55 +00:00
|
|
|
xfree(sig);
|
|
|
|
} else {
|
|
|
|
debug("test whether pkalg/pkblob are acceptable");
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
/* XXX fake reply and always send PK_OK ? */
|
|
|
|
/*
|
|
|
|
* XXX this allows testing whether a user is allowed
|
|
|
|
* to login: if you happen to have a valid pubkey this
|
|
|
|
* message is sent. the message is NEVER sent at all
|
|
|
|
* if a user is not allowed to login. is this an
|
|
|
|
* issue? -markus
|
|
|
|
*/
|
|
|
|
if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
|
|
|
|
packet_start(SSH2_MSG_USERAUTH_PK_OK);
|
|
|
|
packet_put_string(pkalg, alen);
|
|
|
|
packet_put_string(pkblob, blen);
|
|
|
|
packet_send();
|
|
|
|
packet_write_wait();
|
|
|
|
authctxt->postponed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (authenticated != 1)
|
|
|
|
auth_clear_options();
|
|
|
|
done:
|
|
|
|
debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
|
|
|
|
if (key != NULL)
|
|
|
|
key_free(key);
|
|
|
|
xfree(pkalg);
|
|
|
|
xfree(pkblob);
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 1 if user allows given key */
|
|
|
|
static int
|
|
|
|
user_key_allowed2(struct passwd *pw, Key *key, char *file)
|
|
|
|
{
|
2004-12-06 11:47:41 +00:00
|
|
|
char line[SSH_MAX_PUBKEY_BYTES];
|
2008-07-02 12:37:30 +00:00
|
|
|
int found_key = 0;
|
2002-06-06 20:27:55 +00:00
|
|
|
FILE *f;
|
|
|
|
u_long linenum = 0;
|
|
|
|
Key *found;
|
|
|
|
char *fp;
|
|
|
|
|
|
|
|
/* Temporarily use the user's uid. */
|
|
|
|
temporarily_use_uid(pw);
|
|
|
|
|
|
|
|
debug("trying public key file %s", file);
|
2008-07-02 12:37:30 +00:00
|
|
|
f = auth_openkeyfile(file, pw, options.strict_modes);
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2008-07-02 12:37:30 +00:00
|
|
|
if (!f) {
|
2002-06-06 20:27:55 +00:00
|
|
|
restore_uid();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
found_key = 0;
|
|
|
|
found = key_new(key->type);
|
|
|
|
|
2004-12-06 11:47:41 +00:00
|
|
|
while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
|
2004-06-22 02:56:01 +00:00
|
|
|
char *cp, *key_options = NULL;
|
2004-12-06 11:47:41 +00:00
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
/* Skip leading whitespace, empty and comment lines. */
|
|
|
|
for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
|
|
|
|
;
|
|
|
|
if (!*cp || *cp == '\n' || *cp == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (key_read(found, &cp) != 1) {
|
|
|
|
/* no key? check if there are options for this key */
|
|
|
|
int quoted = 0;
|
|
|
|
debug2("user_key_allowed: check options: '%s'", cp);
|
2004-06-22 02:56:01 +00:00
|
|
|
key_options = cp;
|
2002-06-06 20:27:55 +00:00
|
|
|
for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
|
|
|
|
if (*cp == '\\' && cp[1] == '"')
|
|
|
|
cp++; /* Skip both */
|
|
|
|
else if (*cp == '"')
|
|
|
|
quoted = !quoted;
|
|
|
|
}
|
|
|
|
/* Skip remaining whitespace. */
|
|
|
|
for (; *cp == ' ' || *cp == '\t'; cp++)
|
|
|
|
;
|
|
|
|
if (key_read(found, &cp) != 1) {
|
|
|
|
debug2("user_key_allowed: advance: '%s'", cp);
|
|
|
|
/* still no key? advance to next line*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (key_equal(found, key) &&
|
2004-06-22 02:56:01 +00:00
|
|
|
auth_parse_options(pw, key_options, file, linenum) == 1) {
|
2002-06-06 20:27:55 +00:00
|
|
|
found_key = 1;
|
|
|
|
debug("matching key found: file %s, line %lu",
|
|
|
|
file, linenum);
|
|
|
|
fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
|
|
|
|
verbose("Found matching %s key: %s",
|
|
|
|
key_type(found), fp);
|
|
|
|
xfree(fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
restore_uid();
|
|
|
|
fclose(f);
|
|
|
|
key_free(found);
|
|
|
|
if (!found_key)
|
|
|
|
debug2("key not found");
|
|
|
|
return found_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check whether given key is in .ssh/authorized_keys* */
|
|
|
|
int
|
|
|
|
user_key_allowed(struct passwd *pw, Key *key)
|
|
|
|
{
|
|
|
|
int success;
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
file = authorized_keys_file(pw);
|
|
|
|
success = user_key_allowed2(pw, key, file);
|
|
|
|
xfree(file);
|
|
|
|
if (success)
|
|
|
|
return success;
|
|
|
|
|
|
|
|
/* try suffix "2" for backward compat, too */
|
|
|
|
file = authorized_keys_file2(pw);
|
|
|
|
success = user_key_allowed2(pw, key, file);
|
|
|
|
xfree(file);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Authmethod method_pubkey = {
|
|
|
|
"publickey",
|
|
|
|
userauth_pubkey,
|
|
|
|
&options.pubkey_authentication
|
|
|
|
};
|