| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "mod_sftp.h" |
| 26 |
#include "ssh2.h" |
| 27 |
#include "packet.h" |
| 28 |
#include "msg.h" |
| 29 |
#include "auth.h" |
| 30 |
#include "session.h" |
| 31 |
#include "keys.h" |
| 32 |
#include "keystore.h" |
| 33 |
#include "interop.h" |
| 34 |
#include "blacklist.h" |
| 35 |
#include "utf8.h" |
| 36 |
|
| 37 |
static const char *trace_channel = "ssh2"; |
| 38 |
|
| 39 |
int sftp_auth_hostbased(struct ssh2_packet *pkt, cmd_rec *pass_cmd, |
| 40 |
const char *orig_user, const char *user, const char *service, |
| 41 |
unsigned char **buf, uint32_t *buflen, int *send_userauth_fail) { |
| 42 |
struct passwd *pw; |
| 43 |
char *hostkey_algo, *host_fqdn, *host_user, *host_user_utf8; |
| 44 |
const char *fp = NULL, *fp_algo = NULL; |
| 45 |
unsigned char *hostkey_data, *signature_data; |
| 46 |
unsigned char *buf2, *ptr2; |
| 47 |
const unsigned char *id; |
| 48 |
uint32_t buflen2, bufsz2, hostkey_datalen, id_len, signature_len; |
| 49 |
enum sftp_key_type_e pubkey_type; |
| 50 |
struct sftp_verify_details *details = NULL; |
| 51 |
pr_table_t *verify_notes = NULL; |
| 52 |
int fp_algo_id; |
| 53 |
|
| 54 |
if (pr_cmd_dispatch_phase(pass_cmd, PRE_CMD, 0) < 0) { |
| 55 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 56 |
"authentication request for user '%s' blocked by '%s' handler", |
| 57 |
orig_user, (char *) pass_cmd->argv[0]); |
| 58 |
|
| 59 |
pr_log_auth(PR_LOG_NOTICE, |
| 60 |
"USER %s (Login failed): blocked by '%s' handler", orig_user, |
| 61 |
(char *) pass_cmd->argv[0]); |
| 62 |
|
| 63 |
pr_cmd_dispatch_phase(pass_cmd, POST_CMD_ERR, 0); |
| 64 |
pr_cmd_dispatch_phase(pass_cmd, LOG_CMD_ERR, 0); |
| 65 |
|
| 66 |
*send_userauth_fail = TRUE; |
| 67 |
errno = EPERM; |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
hostkey_algo = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 72 |
if (hostkey_algo == NULL) { |
| 73 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 74 |
"missing required host key algorithm, rejecting request"); |
| 75 |
|
| 76 |
*send_userauth_fail = TRUE; |
| 77 |
errno = EINVAL; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
hostkey_datalen = sftp_msg_read_int(pkt->pool, buf, buflen); |
| 82 |
hostkey_data = sftp_msg_read_data(pkt->pool, buf, buflen, hostkey_datalen); |
| 83 |
|
| 84 |
host_fqdn = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 85 |
|
| 86 |
host_user_utf8 = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 87 |
host_user = sftp_utf8_decode_str(pkt->pool, host_user_utf8); |
| 88 |
|
| 89 |
signature_len = sftp_msg_read_int(pkt->pool, buf, buflen); |
| 90 |
signature_data = sftp_msg_read_data(pkt->pool, buf, buflen, signature_len); |
| 91 |
|
| 92 |
pr_trace_msg(trace_channel, 9, |
| 93 |
"client sent '%s' host key, FQDN %s, and remote user '%s'", |
| 94 |
hostkey_algo, host_fqdn, host_user); |
| 95 |
|
| 96 |
if (strcmp(hostkey_algo, "ssh-rsa") == 0) { |
| 97 |
pubkey_type = SFTP_KEY_RSA; |
| 98 |
|
| 99 |
#if defined(HAVE_SHA256_OPENSSL) |
| 100 |
} else if (strcmp(hostkey_algo, "rsa-sha2-256") == 0) { |
| 101 |
pubkey_type = SFTP_KEY_RSA_SHA256; |
| 102 |
#endif |
| 103 |
|
| 104 |
#if defined(HAVE_SHA512_OPENSSL) |
| 105 |
} else if (strcmp(hostkey_algo, "rsa-sha2-512") == 0) { |
| 106 |
pubkey_type = SFTP_KEY_RSA_SHA512; |
| 107 |
#endif |
| 108 |
|
| 109 |
} else if (strcmp(hostkey_algo, "ssh-dss") == 0) { |
| 110 |
pubkey_type = SFTP_KEY_DSA; |
| 111 |
|
| 112 |
#if defined(PR_USE_OPENSSL_ECC) |
| 113 |
} else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp256") == 0) { |
| 114 |
pubkey_type = SFTP_KEY_ECDSA_256; |
| 115 |
|
| 116 |
} else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp384") == 0) { |
| 117 |
pubkey_type = SFTP_KEY_ECDSA_384; |
| 118 |
|
| 119 |
} else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp521") == 0) { |
| 120 |
pubkey_type = SFTP_KEY_ECDSA_521; |
| 121 |
|
| 122 |
} else if (strcmp(hostkey_algo, "[email protected]") == 0) { |
| 123 |
pubkey_type = SFTP_KEY_ECDSA_256_SK; |
| 124 |
#endif |
| 125 |
|
| 126 |
#if defined(PR_USE_SODIUM) |
| 127 |
} else if (strcmp(hostkey_algo, "ssh-ed25519") == 0) { |
| 128 |
pubkey_type = SFTP_KEY_ED25519; |
| 129 |
|
| 130 |
} else if (strcmp(hostkey_algo, "[email protected]") == 0) { |
| 131 |
pubkey_type = SFTP_KEY_ED25519_SK; |
| 132 |
#endif |
| 133 |
|
| 134 |
#if defined(HAVE_X448_OPENSSL) |
| 135 |
} else if (strcmp(hostkey_algo, "ssh-ed448") == 0) { |
| 136 |
pubkey_type = SFTP_KEY_ED448; |
| 137 |
#endif |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
} else { |
| 142 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 143 |
"unsupported host key algorithm '%s' requested, rejecting request", |
| 144 |
hostkey_algo); |
| 145 |
|
| 146 |
pr_log_auth(PR_LOG_NOTICE, |
| 147 |
"USER %s (Login failed): unsupported host key algorithm '%s' requested", |
| 148 |
user, hostkey_algo); |
| 149 |
|
| 150 |
*send_userauth_fail = TRUE; |
| 151 |
errno = EINVAL; |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
if (sftp_keys_verify_pubkey_type(pkt->pool, hostkey_data, hostkey_datalen, |
| 156 |
pubkey_type) != TRUE) { |
| 157 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 158 |
"unable to verify that given host key matches given '%s' algorithm", |
| 159 |
hostkey_algo); |
| 160 |
|
| 161 |
*send_userauth_fail = TRUE; |
| 162 |
errno = EINVAL; |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
#ifdef OPENSSL_FIPS |
| 167 |
if (FIPS_mode()) { |
| 168 |
# if defined(HAVE_SHA256_OPENSSL) |
| 169 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256; |
| 170 |
fp_algo = "SHA256"; |
| 171 |
# else |
| 172 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA1; |
| 173 |
fp_algo = "SHA1"; |
| 174 |
# endif |
| 175 |
|
| 176 |
fp = sftp_keys_get_fingerprint(pkt->pool, hostkey_data, hostkey_datalen, |
| 177 |
fp_algo_id); |
| 178 |
if (fp != NULL) { |
| 179 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 180 |
"public key %s fingerprint: %s", fp_algo, fp); |
| 181 |
|
| 182 |
} else { |
| 183 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 184 |
"error obtaining public key %s fingerprint: %s", fp_algo, |
| 185 |
strerror(errno)); |
| 186 |
} |
| 187 |
|
| 188 |
} else { |
| 189 |
#endif |
| 190 |
#if defined(HAVE_SHA256_OPENSSL) |
| 191 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256; |
| 192 |
fp_algo = "SHA256"; |
| 193 |
#else |
| 194 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_MD5; |
| 195 |
fp_algo = "MD5"; |
| 196 |
#endif |
| 197 |
|
| 198 |
fp = sftp_keys_get_fingerprint(pkt->pool, hostkey_data, hostkey_datalen, |
| 199 |
fp_algo_id); |
| 200 |
if (fp != NULL) { |
| 201 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 202 |
"public key %s fingerprint: %s", fp_algo, fp); |
| 203 |
|
| 204 |
} else { |
| 205 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 206 |
"error obtaining public key %s fingerprint: %s", fp_algo, |
| 207 |
strerror(errno)); |
| 208 |
} |
| 209 |
#ifdef OPENSSL_FIPS |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
pw = pr_auth_getpwnam(pkt->pool, user); |
| 214 |
if (pw == NULL) { |
| 215 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 216 |
"no account for user '%s' found", user); |
| 217 |
|
| 218 |
pr_log_auth(PR_LOG_NOTICE, |
| 219 |
"USER %s: no such user found from %s [%s] to %s:%d", user, |
| 220 |
session.c->remote_name, pr_netaddr_get_ipstr(session.c->remote_addr), |
| 221 |
pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port); |
| 222 |
|
| 223 |
*send_userauth_fail = TRUE; |
| 224 |
errno = ENOENT; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if (sftp_blacklist_reject_key(pkt->pool, hostkey_data, hostkey_datalen)) { |
| 234 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): requested host " |
| 235 |
"key is blacklisted", user); |
| 236 |
|
| 237 |
*send_userauth_fail = TRUE; |
| 238 |
errno = EACCES; |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
verify_notes = pr_table_nalloc(pkt->pool, 0, 1); |
| 248 |
if (sftp_keystore_verify_host_key(pkt->pool, user, host_fqdn, host_user, |
| 249 |
hostkey_data, hostkey_datalen, verify_notes) < 0) { |
| 250 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): authentication " |
| 251 |
"via '%s' host key failed", user, hostkey_algo); |
| 252 |
|
| 253 |
*send_userauth_fail = TRUE; |
| 254 |
errno = EACCES; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
id_len = sftp_session_get_id(&id); |
| 261 |
|
| 262 |
|
| 263 |
bufsz2 = buflen2 = 2048; |
| 264 |
ptr2 = buf2 = sftp_msg_getbuf(pkt->pool, bufsz2); |
| 265 |
|
| 266 |
sftp_msg_write_data(&buf2, &buflen2, id, id_len, TRUE); |
| 267 |
sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_USER_AUTH_REQUEST); |
| 268 |
sftp_msg_write_string(&buf2, &buflen2, orig_user); |
| 269 |
|
| 270 |
if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_SERVICE_IN_HOST_SIG)) { |
| 271 |
sftp_msg_write_string(&buf2, &buflen2, service); |
| 272 |
|
| 273 |
} else { |
| 274 |
sftp_msg_write_string(&buf2, &buflen2, "ssh-userauth"); |
| 275 |
} |
| 276 |
|
| 277 |
sftp_msg_write_string(&buf2, &buflen2, "hostbased"); |
| 278 |
sftp_msg_write_string(&buf2, &buflen2, hostkey_algo); |
| 279 |
sftp_msg_write_data(&buf2, &buflen2, hostkey_data, hostkey_datalen, TRUE); |
| 280 |
sftp_msg_write_string(&buf2, &buflen2, host_fqdn); |
| 281 |
sftp_msg_write_string(&buf2, &buflen2, host_user_utf8); |
| 282 |
|
| 283 |
details = pcalloc(pkt->pool, sizeof(struct sftp_verify_details)); |
| 284 |
if (sftp_keys_verify_signed_data(pkt->pool, hostkey_algo, hostkey_data, |
| 285 |
hostkey_datalen, signature_data, signature_len, (unsigned char *) ptr2, |
| 286 |
(bufsz2 - buflen2), details) < 0) { |
| 287 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 288 |
"failed to verify '%s' signature on hostbased auth request for " |
| 289 |
"user '%s', host %s", hostkey_algo, orig_user, host_fqdn); |
| 290 |
|
| 291 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature " |
| 292 |
"verification of '%s' host key failed", user, hostkey_algo); |
| 293 |
|
| 294 |
*send_userauth_fail = TRUE; |
| 295 |
errno = EACCES; |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
if (sftp_keys_permit_key(pkt->pool, hostkey_algo, orig_user, details, |
| 300 |
verify_notes) < 0) { |
| 301 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 302 |
"unable to permit '%s' signature on hostbased auth request for " |
| 303 |
"user '%s', host %s due to security key policy", hostkey_algo, orig_user, |
| 304 |
host_fqdn); |
| 305 |
|
| 306 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature " |
| 307 |
"validation of '%s' host key failed", user, hostkey_algo); |
| 308 |
|
| 309 |
*send_userauth_fail = TRUE; |
| 310 |
errno = EACCES; |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (pr_auth_authorize(pkt->pool, user) != PR_AUTH_OK) { |
| 320 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 321 |
"authentication for user '%s' failed: User not authorized", user); |
| 322 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User not authorized " |
| 323 |
"for login", user); |
| 324 |
*send_userauth_fail = TRUE; |
| 325 |
errno = EACCES; |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
return 1; |
| 330 |
} |
| 331 |
|
| 332 |
int sftp_auth_hostbased_init(pool *p) { |
| 333 |
return 0; |
| 334 |
} |