1 /* $OpenBSD: auth-rh-rsa.c,v 1.42 2006/08/03 03:34:41 deraadt Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Rhosts or /etc/hosts.equiv authentication combined with RSA host
7  * authentication.
8  *
9  * As far as I am concerned, the code I have written for this software
10  * can be used freely for any purpose.  Any derived versions of this
11  * software must be clearly marked as such, and if the derived work is
12  * incompatible with the protocol description in the RFC file, it must be
13  * called by a name other than "ssh" or "Secure Shell".
14  */
15 
16 #include <sys/types.h>
17 
18 #include <pwd.h>
19 #include <stdarg.h>
20 
21 #include "packet.h"
22 #include "uidswap.h"
23 #include "log.h"
24 #include "buffer.h"
25 #include "servconf.h"
26 #include "key.h"
27 #include "hostfile.h"
28 #include "pathnames.h"
29 #include "auth.h"
30 #include "canohost.h"
31 #include "monitor_wrap.h"
32 
33 __RCSID("$MirOS: src/usr.bin/ssh/auth-rh-rsa.c,v 1.4 2011/01/15 21:52:39 tg Exp $");
34 
35 /* import */
36 extern ServerOptions options;
37 
38 int
auth_rhosts_rsa_key_allowed(struct passwd * pw,char * cuser,char * chost,Key * client_host_key)39 auth_rhosts_rsa_key_allowed(struct passwd *pw, char *cuser, char *chost,
40     Key *client_host_key)
41 {
42 	HostStatus host_status;
43 
44 	/* Check if we would accept it using rhosts authentication. */
45 	if (!auth_rhosts(pw, cuser))
46 		return 0;
47 
48 	host_status = check_key_in_hostfiles(pw, client_host_key,
49 	    chost, _PATH_SSH_SYSTEM_HOSTFILE,
50 	    options.ignore_user_known_hosts ? NULL :
51 #ifdef _PATH_SSH_ROOT_HOSTFILE
52 	    (!pw->pw_dir || !pw->pw_dir[0] || (pw->pw_dir[0] == '/' &&
53 	    !pw->pw_dir[1])) ? _PATH_SSH_ROOT_HOSTFILE :
54 #endif
55 	    _PATH_SSH_USER_HOSTFILE);
56 
57 	return (host_status == HOST_OK);
58 }
59 
60 /*
61  * Tries to authenticate the user using the .rhosts file and the host using
62  * its host key.  Returns true if authentication succeeds.
63  */
64 int
auth_rhosts_rsa(Authctxt * authctxt,char * cuser,Key * client_host_key)65 auth_rhosts_rsa(Authctxt *authctxt, char *cuser, Key *client_host_key)
66 {
67 	char *chost;
68 	struct passwd *pw = authctxt->pw;
69 
70 	debug("Trying rhosts with RSA host authentication for client user %.100s",
71 	    cuser);
72 
73 	if (!authctxt->valid || client_host_key == NULL ||
74 	    client_host_key->rsa == NULL)
75 		return 0;
76 
77 	chost = (char *)get_canonical_hostname(options.use_dns);
78 	debug("Rhosts RSA authentication: canonical host %.900s", chost);
79 
80 	if (!PRIVSEP(auth_rhosts_rsa_key_allowed(pw, cuser, chost, client_host_key))) {
81 		debug("Rhosts with RSA host authentication denied: unknown or invalid host key");
82 		packet_send_debug("Your host key cannot be verified: unknown or invalid host key.");
83 		return 0;
84 	}
85 	/* A matching host key was found and is known. */
86 
87 	/* Perform the challenge-response dialog with the client for the host key. */
88 	if (!auth_rsa_challenge_dialog(client_host_key)) {
89 		logit("Client on %.800s failed to respond correctly to host authentication.",
90 		    chost);
91 		return 0;
92 	}
93 	/*
94 	 * We have authenticated the user using .rhosts or /etc/hosts.equiv,
95 	 * and the host using RSA. We accept the authentication.
96 	 */
97 
98 	verbose("Rhosts with RSA host authentication accepted for %.100s, %.100s on %.700s.",
99 	    pw->pw_name, cuser, chost);
100 	packet_send_debug("Rhosts with RSA host authentication accepted.");
101 	return 1;
102 }
103