Home | History | Annotate | Line # | Download | only in dist
auth2-hostbased.c revision 1.1.1.1
      1  1.1  christos /*	$NetBSD: auth2-hostbased.c,v 1.1.1.1 2009/06/07 22:19:02 christos Exp $	*/
      2  1.1  christos /* $OpenBSD: auth2-hostbased.c,v 1.12 2008/07/17 08:51:07 djm Exp $ */
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
      5  1.1  christos  *
      6  1.1  christos  * Redistribution and use in source and binary forms, with or without
      7  1.1  christos  * modification, are permitted provided that the following conditions
      8  1.1  christos  * are met:
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  *
     15  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos 
     28  1.1  christos #include <sys/types.h>
     29  1.1  christos 
     30  1.1  christos #include <pwd.h>
     31  1.1  christos #include <string.h>
     32  1.1  christos #include <stdarg.h>
     33  1.1  christos 
     34  1.1  christos #include "xmalloc.h"
     35  1.1  christos #include "ssh2.h"
     36  1.1  christos #include "packet.h"
     37  1.1  christos #include "buffer.h"
     38  1.1  christos #include "log.h"
     39  1.1  christos #include "servconf.h"
     40  1.1  christos #include "compat.h"
     41  1.1  christos #include "key.h"
     42  1.1  christos #include "hostfile.h"
     43  1.1  christos #include "auth.h"
     44  1.1  christos #include "canohost.h"
     45  1.1  christos #ifdef GSSAPI
     46  1.1  christos #include "ssh-gss.h"
     47  1.1  christos #endif
     48  1.1  christos #include "monitor_wrap.h"
     49  1.1  christos #include "pathnames.h"
     50  1.1  christos 
     51  1.1  christos /* import */
     52  1.1  christos extern ServerOptions options;
     53  1.1  christos extern u_char *session_id2;
     54  1.1  christos extern u_int session_id2_len;
     55  1.1  christos 
     56  1.1  christos static int
     57  1.1  christos userauth_hostbased(Authctxt *authctxt)
     58  1.1  christos {
     59  1.1  christos 	Buffer b;
     60  1.1  christos 	Key *key = NULL;
     61  1.1  christos 	char *pkalg, *cuser, *chost, *service;
     62  1.1  christos 	u_char *pkblob, *sig;
     63  1.1  christos 	u_int alen, blen, slen;
     64  1.1  christos 	int pktype;
     65  1.1  christos 	int authenticated = 0;
     66  1.1  christos 
     67  1.1  christos 	if (!authctxt->valid) {
     68  1.1  christos 		debug2("userauth_hostbased: disabled because of invalid user");
     69  1.1  christos 		return 0;
     70  1.1  christos 	}
     71  1.1  christos 	pkalg = packet_get_string(&alen);
     72  1.1  christos 	pkblob = packet_get_string(&blen);
     73  1.1  christos 	chost = packet_get_string(NULL);
     74  1.1  christos 	cuser = packet_get_string(NULL);
     75  1.1  christos 	sig = packet_get_string(&slen);
     76  1.1  christos 
     77  1.1  christos 	debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
     78  1.1  christos 	    cuser, chost, pkalg, slen);
     79  1.1  christos #ifdef DEBUG_PK
     80  1.1  christos 	debug("signature:");
     81  1.1  christos 	buffer_init(&b);
     82  1.1  christos 	buffer_append(&b, sig, slen);
     83  1.1  christos 	buffer_dump(&b);
     84  1.1  christos 	buffer_free(&b);
     85  1.1  christos #endif
     86  1.1  christos 	pktype = key_type_from_name(pkalg);
     87  1.1  christos 	if (pktype == KEY_UNSPEC) {
     88  1.1  christos 		/* this is perfectly legal */
     89  1.1  christos 		logit("userauth_hostbased: unsupported "
     90  1.1  christos 		    "public key algorithm: %s", pkalg);
     91  1.1  christos 		goto done;
     92  1.1  christos 	}
     93  1.1  christos 	key = key_from_blob(pkblob, blen);
     94  1.1  christos 	if (key == NULL) {
     95  1.1  christos 		error("userauth_hostbased: cannot decode key: %s", pkalg);
     96  1.1  christos 		goto done;
     97  1.1  christos 	}
     98  1.1  christos 	if (key->type != pktype) {
     99  1.1  christos 		error("userauth_hostbased: type mismatch for decoded key "
    100  1.1  christos 		    "(received %d, expected %d)", key->type, pktype);
    101  1.1  christos 		goto done;
    102  1.1  christos 	}
    103  1.1  christos 	service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
    104  1.1  christos 	    authctxt->service;
    105  1.1  christos 	buffer_init(&b);
    106  1.1  christos 	buffer_put_string(&b, session_id2, session_id2_len);
    107  1.1  christos 	/* reconstruct packet */
    108  1.1  christos 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
    109  1.1  christos 	buffer_put_cstring(&b, authctxt->user);
    110  1.1  christos 	buffer_put_cstring(&b, service);
    111  1.1  christos 	buffer_put_cstring(&b, "hostbased");
    112  1.1  christos 	buffer_put_string(&b, pkalg, alen);
    113  1.1  christos 	buffer_put_string(&b, pkblob, blen);
    114  1.1  christos 	buffer_put_cstring(&b, chost);
    115  1.1  christos 	buffer_put_cstring(&b, cuser);
    116  1.1  christos #ifdef DEBUG_PK
    117  1.1  christos 	buffer_dump(&b);
    118  1.1  christos #endif
    119  1.1  christos 	/* test for allowed key and correct signature */
    120  1.1  christos 	authenticated = 0;
    121  1.1  christos 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
    122  1.1  christos 	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
    123  1.1  christos 			buffer_len(&b))) == 1)
    124  1.1  christos 		authenticated = 1;
    125  1.1  christos 
    126  1.1  christos 	buffer_free(&b);
    127  1.1  christos done:
    128  1.1  christos 	debug2("userauth_hostbased: authenticated %d", authenticated);
    129  1.1  christos 	if (key != NULL)
    130  1.1  christos 		key_free(key);
    131  1.1  christos 	xfree(pkalg);
    132  1.1  christos 	xfree(pkblob);
    133  1.1  christos 	xfree(cuser);
    134  1.1  christos 	xfree(chost);
    135  1.1  christos 	xfree(sig);
    136  1.1  christos 	return authenticated;
    137  1.1  christos }
    138  1.1  christos 
    139  1.1  christos /* return 1 if given hostkey is allowed */
    140  1.1  christos int
    141  1.1  christos hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
    142  1.1  christos     Key *key)
    143  1.1  christos {
    144  1.1  christos 	const char *resolvedname, *ipaddr, *lookup;
    145  1.1  christos 	HostStatus host_status;
    146  1.1  christos 	int len;
    147  1.1  christos 
    148  1.1  christos 	resolvedname = get_canonical_hostname(options.use_dns);
    149  1.1  christos 	ipaddr = get_remote_ipaddr();
    150  1.1  christos 
    151  1.1  christos 	debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
    152  1.1  christos 	    chost, resolvedname, ipaddr);
    153  1.1  christos 
    154  1.1  christos 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
    155  1.1  christos 		debug2("stripping trailing dot from chost %s", chost);
    156  1.1  christos 		chost[len - 1] = '\0';
    157  1.1  christos 	}
    158  1.1  christos 
    159  1.1  christos 	if (options.hostbased_uses_name_from_packet_only) {
    160  1.1  christos 		if (auth_rhosts2(pw, cuser, chost, chost) == 0)
    161  1.1  christos 			return 0;
    162  1.1  christos 		lookup = chost;
    163  1.1  christos 	} else {
    164  1.1  christos 		if (strcasecmp(resolvedname, chost) != 0)
    165  1.1  christos 			logit("userauth_hostbased mismatch: "
    166  1.1  christos 			    "client sends %s, but we resolve %s to %s",
    167  1.1  christos 			    chost, ipaddr, resolvedname);
    168  1.1  christos 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
    169  1.1  christos 			return 0;
    170  1.1  christos 		lookup = resolvedname;
    171  1.1  christos 	}
    172  1.1  christos 	debug2("userauth_hostbased: access allowed by auth_rhosts2");
    173  1.1  christos 
    174  1.1  christos 	host_status = check_key_in_hostfiles(pw, key, lookup,
    175  1.1  christos 	    _PATH_SSH_SYSTEM_HOSTFILE,
    176  1.1  christos 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
    177  1.1  christos 
    178  1.1  christos 	/* backward compat if no key has been found. */
    179  1.1  christos 	if (host_status == HOST_NEW)
    180  1.1  christos 		host_status = check_key_in_hostfiles(pw, key, lookup,
    181  1.1  christos 		    _PATH_SSH_SYSTEM_HOSTFILE2,
    182  1.1  christos 		    options.ignore_user_known_hosts ? NULL :
    183  1.1  christos 		    _PATH_SSH_USER_HOSTFILE2);
    184  1.1  christos 
    185  1.1  christos 	return (host_status == HOST_OK);
    186  1.1  christos }
    187  1.1  christos 
    188  1.1  christos Authmethod method_hostbased = {
    189  1.1  christos 	"hostbased",
    190  1.1  christos 	userauth_hostbased,
    191  1.1  christos 	&options.hostbased_authentication
    192  1.1  christos };
    193