blob: 19782577bcb0cd144639dfcd11d83a71e740a700 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * auth-rh-rsa.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Sun May 7 03:08:06 1995 ylo
11 *
12 * Rhosts or /etc/hosts.equiv authentication combined with RSA host
13 * authentication.
14 *
15 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller450a7a12000-03-26 13:04:51 +100018RCSID("$Id: auth-rh-rsa.c,v 1.8 2000/03/26 03:04:52 damien Exp $");
19
20#ifdef HAVE_OPENSSL
21#include <openssl/bn.h>
22#include <openssl/rsa.h>
23#include <openssl/dsa.h>
24#endif
25#ifdef HAVE_SSL
26#include <ssl/bn.h>
27#include <ssl/rsa.h>
28#include <ssl/dsa.h>
29#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030
31#include "packet.h"
32#include "ssh.h"
33#include "xmalloc.h"
34#include "uidswap.h"
Damien Miller32265091999-11-12 11:33:04 +110035#include "servconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
Damien Miller450a7a12000-03-26 13:04:51 +100037#include "key.h"
38#include "hostfile.h"
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Tries to authenticate the user using the .rhosts file and the host using
42 * its host key. Returns true if authentication succeeds.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller95def091999-11-25 00:26:21 +110045int
Damien Miller450a7a12000-03-26 13:04:51 +100046auth_rhosts_rsa(struct passwd *pw, const char *client_user, RSA *client_host_key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047{
Damien Miller95def091999-11-25 00:26:21 +110048 extern ServerOptions options;
49 const char *canonical_hostname;
50 HostStatus host_status;
Damien Miller450a7a12000-03-26 13:04:51 +100051 Key *client_key, *found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller95def091999-11-25 00:26:21 +110053 debug("Trying rhosts with RSA host authentication for %.100s", client_user);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller450a7a12000-03-26 13:04:51 +100055 if (client_host_key == NULL)
56 return 0;
57
Damien Miller95def091999-11-25 00:26:21 +110058 /* Check if we would accept it using rhosts authentication. */
59 if (!auth_rhosts(pw, client_user))
60 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller95def091999-11-25 00:26:21 +110062 canonical_hostname = get_canonical_hostname();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller450a7a12000-03-26 13:04:51 +100064 debug("Rhosts RSA authentication: canonical host %.900s", canonical_hostname);
65
66 /* wrap the RSA key into a 'generic' key */
67 client_key = key_new(KEY_RSA);
68 BN_copy(client_key->rsa->e, client_host_key->e);
69 BN_copy(client_key->rsa->n, client_host_key->n);
70 found = key_new(KEY_RSA);
Damien Miller32265091999-11-12 11:33:04 +110071
Damien Miller95def091999-11-25 00:26:21 +110072 /* Check if we know the host and its host key. */
Damien Miller95def091999-11-25 00:26:21 +110073 host_status = check_host_in_hostfile(SSH_SYSTEM_HOSTFILE, canonical_hostname,
Damien Miller450a7a12000-03-26 13:04:51 +100074 client_key, found);
Damien Miller33e511e1999-11-11 11:43:13 +110075
Damien Miller95def091999-11-25 00:26:21 +110076 /* Check user host file unless ignored. */
77 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
78 struct stat st;
79 char *user_hostfile = tilde_expand_filename(SSH_USER_HOSTFILE, pw->pw_uid);
Damien Miller5428f641999-11-25 11:54:57 +110080 /*
81 * Check file permissions of SSH_USER_HOSTFILE, auth_rsa()
82 * did already check pw->pw_dir, but there is a race XXX
83 */
Damien Miller95def091999-11-25 00:26:21 +110084 if (options.strict_modes &&
85 (stat(user_hostfile, &st) == 0) &&
86 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
87 (st.st_mode & 022) != 0)) {
88 log("Rhosts RSA authentication refused for %.100s: bad owner or modes for %.200s",
89 pw->pw_name, user_hostfile);
90 } else {
91 /* XXX race between stat and the following open() */
92 temporarily_use_uid(pw->pw_uid);
93 host_status = check_host_in_hostfile(user_hostfile, canonical_hostname,
Damien Miller450a7a12000-03-26 13:04:51 +100094 client_key, found);
Damien Miller95def091999-11-25 00:26:21 +110095 restore_uid();
96 }
97 xfree(user_hostfile);
98 }
Damien Miller450a7a12000-03-26 13:04:51 +100099 key_free(client_key);
100 key_free(found);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 if (host_status != HOST_OK) {
103 debug("Rhosts with RSA host authentication denied: unknown or invalid host key");
104 packet_send_debug("Your host key cannot be verified: unknown or invalid host key.");
105 return 0;
106 }
107 /* A matching host key was found and is known. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller95def091999-11-25 00:26:21 +1100109 /* Perform the challenge-response dialog with the client for the host key. */
Damien Miller450a7a12000-03-26 13:04:51 +1000110 if (!auth_rsa_challenge_dialog(client_host_key)) {
Damien Miller95def091999-11-25 00:26:21 +1100111 log("Client on %.800s failed to respond correctly to host authentication.",
112 canonical_hostname);
113 return 0;
114 }
Damien Miller5428f641999-11-25 11:54:57 +1100115 /*
116 * We have authenticated the user using .rhosts or /etc/hosts.equiv,
117 * and the host using RSA. We accept the authentication.
118 */
Damien Miller95def091999-11-25 00:26:21 +1100119
120 verbose("Rhosts with RSA host authentication accepted for %.100s, %.100s on %.700s.",
Damien Miller450a7a12000-03-26 13:04:51 +1000121 pw->pw_name, client_user, canonical_hostname);
Damien Miller95def091999-11-25 00:26:21 +1100122 packet_send_debug("Rhosts with RSA host authentication accepted.");
123 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124}