| 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 "disconnect.h" |
| 30 |
#include "interop.h" |
| 31 |
#include "auth.h" |
| 32 |
#include "crypto.h" |
| 33 |
#include "cipher.h" |
| 34 |
#include "mac.h" |
| 35 |
#include "compress.h" |
| 36 |
#include "session.h" |
| 37 |
#include "keys.h" |
| 38 |
#include "keystore.h" |
| 39 |
#include "kbdint.h" |
| 40 |
#include "utf8.h" |
| 41 |
#include "display.h" |
| 42 |
|
| 43 |
|
| 44 |
extern pr_response_t *resp_list, *resp_err_list; |
| 45 |
|
| 46 |
|
| 47 |
static unsigned int auth_attempts_max = 6; |
| 48 |
static unsigned int auth_attempts = 0; |
| 49 |
|
| 50 |
static pool *auth_pool = NULL; |
| 51 |
static char *auth_default_dir = NULL; |
| 52 |
|
| 53 |
static const char *auth_avail_meths = NULL; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static unsigned int auth_meths_enabled_flags = 0; |
| 59 |
|
| 60 |
static array_header *auth_chains = NULL; |
| 61 |
|
| 62 |
static int auth_sent_userauth_banner_file = FALSE; |
| 63 |
static int auth_sent_userauth_success = FALSE; |
| 64 |
|
| 65 |
static const char *auth_user = NULL; |
| 66 |
static const char *auth_service = NULL; |
| 67 |
|
| 68 |
struct sftp_auth_publickey { |
| 69 |
const char *algo; |
| 70 |
enum sftp_key_type_e key_type; |
| 71 |
}; |
| 72 |
|
| 73 |
static struct sftp_auth_publickey publickeys[] = { |
| 74 |
{ "ssh-dss", SFTP_KEY_DSA }, |
| 75 |
{ "ssh-rsa", SFTP_KEY_RSA }, |
| 76 |
#if defined(HAVE_SHA256_OPENSSL) |
| 77 |
{ "rsa-sha2-256", SFTP_KEY_RSA_SHA256 }, |
| 78 |
#endif |
| 79 |
#if defined(HAVE_SHA512_OPENSSL) |
| 80 |
{ "rsa-sha2-512", SFTP_KEY_RSA_SHA512 }, |
| 81 |
#endif |
| 82 |
#if defined(PR_USE_OPENSSL_ECC) |
| 83 |
{ "ecdsa-sha2-nistp256", SFTP_KEY_ECDSA_256 }, |
| 84 |
{ "ecdsa-sha2-nistp384", SFTP_KEY_ECDSA_384 }, |
| 85 |
{ "ecdsa-sha2-nistp521", SFTP_KEY_ECDSA_521 }, |
| 86 |
{ "[email protected]", SFTP_KEY_ECDSA_256_SK }, |
| 87 |
#endif |
| 88 |
#if defined(PR_USE_SODIUM) |
| 89 |
{ "ssh-ed25519", SFTP_KEY_ED25519 }, |
| 90 |
{ "[email protected]", SFTP_KEY_ED25519_SK }, |
| 91 |
#endif |
| 92 |
#if defined(HAVE_X448_OPENSSL) |
| 93 |
{ "ssh-ed448", SFTP_KEY_ED448 }, |
| 94 |
#endif |
| 95 |
{ NULL, 0 } |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
static int (*success_handler)(pool *, const char *) = NULL; |
| 100 |
|
| 101 |
static const char *trace_channel = "ssh2"; |
| 102 |
|
| 103 |
static struct passwd *dup_passwd(pool *p, struct passwd *pw) { |
| 104 |
struct passwd *res = NULL; |
| 105 |
|
| 106 |
res = pcalloc(p, sizeof(struct passwd)); |
| 107 |
res->pw_name = pstrdup(p, pw->pw_name); |
| 108 |
res->pw_uid = pw->pw_uid; |
| 109 |
res->pw_gid = pw->pw_gid; |
| 110 |
res->pw_gecos = pstrdup(p, pw->pw_gecos); |
| 111 |
res->pw_dir = pstrdup(p, pw->pw_dir); |
| 112 |
res->pw_shell = pstrdup(p, pw->pw_shell); |
| 113 |
|
| 114 |
return res; |
| 115 |
} |
| 116 |
|
| 117 |
static void ensure_open_passwd(pool *p) { |
| 118 |
pr_auth_setpwent(p); |
| 119 |
pr_auth_setgrent(p); |
| 120 |
|
| 121 |
pr_auth_getpwent(p); |
| 122 |
pr_auth_getgrent(p); |
| 123 |
} |
| 124 |
|
| 125 |
static const char *get_default_chdir(pool *p) { |
| 126 |
config_rec *c; |
| 127 |
const char *path = NULL; |
| 128 |
|
| 129 |
c = find_config(main_server->conf, CONF_PARAM, "DefaultChdir", FALSE); |
| 130 |
while (c != NULL) { |
| 131 |
int res; |
| 132 |
|
| 133 |
pr_signals_handle(); |
| 134 |
|
| 135 |
if (c->argc < 2) { |
| 136 |
path = c->argv[0]; |
| 137 |
break; |
| 138 |
} |
| 139 |
|
| 140 |
res = pr_expr_eval_group_and(((char **) c->argv) + 1); |
| 141 |
if (res) { |
| 142 |
path = c->argv[0]; |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
c = find_config_next(c, c->next, CONF_PARAM, "DefaultChdir", FALSE); |
| 147 |
} |
| 148 |
|
| 149 |
if (path && |
| 150 |
*path != '/' && |
| 151 |
*path != '~') { |
| 152 |
path = pdircat(p, session.cwd, path, NULL); |
| 153 |
} |
| 154 |
|
| 155 |
if (path != NULL) { |
| 156 |
path = path_subst_uservar(p, &path); |
| 157 |
} |
| 158 |
|
| 159 |
return path; |
| 160 |
} |
| 161 |
|
| 162 |
static const char *get_default_root(pool *p) { |
| 163 |
config_rec *c; |
| 164 |
const char *path = NULL; |
| 165 |
|
| 166 |
c = find_config(main_server->conf, CONF_PARAM, "DefaultRoot", FALSE); |
| 167 |
while (c) { |
| 168 |
int res; |
| 169 |
|
| 170 |
pr_signals_handle(); |
| 171 |
|
| 172 |
if (c->argc < 2) { |
| 173 |
path = c->argv[0]; |
| 174 |
break; |
| 175 |
} |
| 176 |
|
| 177 |
res = pr_expr_eval_group_and(((char **) c->argv) + 1); |
| 178 |
if (res) { |
| 179 |
path = c->argv[0]; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
c = find_config_next(c, c->next, CONF_PARAM, "DefaultRoot", FALSE); |
| 184 |
} |
| 185 |
|
| 186 |
if (path) { |
| 187 |
path = path_subst_uservar(p, &path); |
| 188 |
|
| 189 |
if (strncmp(path, "/", 2) == 0) { |
| 190 |
path = NULL; |
| 191 |
|
| 192 |
} else { |
| 193 |
char *real_path; |
| 194 |
int xerrno = 0; |
| 195 |
|
| 196 |
PRIVS_USER |
| 197 |
real_path = dir_realpath(p, path); |
| 198 |
if (real_path == NULL) { |
| 199 |
xerrno = errno; |
| 200 |
} |
| 201 |
PRIVS_RELINQUISH |
| 202 |
|
| 203 |
if (real_path) { |
| 204 |
path = real_path; |
| 205 |
|
| 206 |
} else { |
| 207 |
char interp_path[PR_TUNABLE_PATH_MAX + 1]; |
| 208 |
|
| 209 |
memset(interp_path, '\0', sizeof(interp_path)); |
| 210 |
(void) pr_fs_interpolate(path, interp_path, sizeof(interp_path) - 1); |
| 211 |
|
| 212 |
pr_log_pri(PR_LOG_NOTICE, |
| 213 |
"notice: unable to use DefaultRoot %s (resolved to '%s'): %s", |
| 214 |
path, interp_path, |
| 215 |
strerror(xerrno)); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return path; |
| 221 |
} |
| 222 |
|
| 223 |
static void set_userauth_methods(void) { |
| 224 |
config_rec *c; |
| 225 |
register unsigned int i; |
| 226 |
|
| 227 |
if (auth_chains != NULL) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
auth_avail_meths = NULL; |
| 232 |
auth_meths_enabled_flags = 0; |
| 233 |
|
| 234 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthMethods", FALSE); |
| 235 |
if (c != NULL) { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
auth_chains = c->argv[0]; |
| 240 |
|
| 241 |
} else { |
| 242 |
struct sftp_auth_chain *auth_chain; |
| 243 |
|
| 244 |
auth_chains = make_array(auth_pool, 0, sizeof(struct sftp_auth_chain *)); |
| 245 |
|
| 246 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedUserKeys", |
| 247 |
FALSE); |
| 248 |
if (c != NULL) { |
| 249 |
auth_chain = sftp_auth_chain_alloc(auth_pool); |
| 250 |
sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_PUBLICKEY, |
| 251 |
"publickey", NULL); |
| 252 |
*((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain; |
| 253 |
|
| 254 |
} else { |
| 255 |
pr_trace_msg(trace_channel, 9, "no SFTPAuthorizedUserKeys configured, " |
| 256 |
"not offering 'publickey' authentication"); |
| 257 |
} |
| 258 |
|
| 259 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedHostKeys", |
| 260 |
FALSE); |
| 261 |
if (c != NULL) { |
| 262 |
auth_chain = sftp_auth_chain_alloc(auth_pool); |
| 263 |
sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_HOSTBASED, |
| 264 |
"hostbased", NULL); |
| 265 |
*((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain; |
| 266 |
|
| 267 |
} else { |
| 268 |
pr_trace_msg(trace_channel, 9, "no SFTPAuthorizedHostKeys configured, " |
| 269 |
"not offering 'hostbased' authentication"); |
| 270 |
} |
| 271 |
|
| 272 |
if (sftp_kbdint_have_drivers() > 0) { |
| 273 |
auth_chain = sftp_auth_chain_alloc(auth_pool); |
| 274 |
sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_KBDINT, |
| 275 |
"keyboard-interactive", NULL); |
| 276 |
*((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain; |
| 277 |
|
| 278 |
} else { |
| 279 |
pr_trace_msg(trace_channel, 9, "no kbdint drivers present, not " |
| 280 |
"offering 'keyboard-interactive' authentication"); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
auth_chain = sftp_auth_chain_alloc(auth_pool); |
| 285 |
sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_PASSWORD, |
| 286 |
"password", NULL); |
| 287 |
*((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain; |
| 288 |
} |
| 289 |
|
| 290 |
for (i = 0 ; i < auth_chains->nelts; i++) { |
| 291 |
struct sftp_auth_chain *auth_chain; |
| 292 |
struct sftp_auth_method *meth; |
| 293 |
|
| 294 |
auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i]; |
| 295 |
meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[0]; |
| 296 |
|
| 297 |
if (!(auth_meths_enabled_flags & meth->method_id)) { |
| 298 |
auth_meths_enabled_flags |= meth->method_id; |
| 299 |
|
| 300 |
if (auth_avail_meths != NULL) { |
| 301 |
auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",", |
| 302 |
meth->method_name, NULL); |
| 303 |
} else { |
| 304 |
auth_avail_meths = meth->method_name; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
pr_trace_msg(trace_channel, 9, "offering authentication methods: %s", |
| 310 |
auth_avail_meths); |
| 311 |
|
| 312 |
|
| 313 |
if (sftp_auth_hostbased_init(auth_pool) < 0) { |
| 314 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 315 |
"error preparing for 'hostbased' authentication: %s", strerror(errno)); |
| 316 |
} |
| 317 |
|
| 318 |
if (sftp_auth_kbdint_init(auth_pool) < 0) { |
| 319 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 320 |
"error preparing for 'keyboard-interactive' authentication: %s", |
| 321 |
strerror(errno)); |
| 322 |
} |
| 323 |
|
| 324 |
if (sftp_auth_password_init(auth_pool) < 0) { |
| 325 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 326 |
"error preparing for 'password' authentication: %s", strerror(errno)); |
| 327 |
} |
| 328 |
|
| 329 |
if (sftp_auth_publickey_init(auth_pool) < 0) { |
| 330 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 331 |
"error preparing for 'publickey' authentication: %s", strerror(errno)); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static int setup_env(pool *p, const char *user) { |
| 336 |
struct passwd *pw; |
| 337 |
config_rec *c; |
| 338 |
int login_acl, i, res, root_revoke = TRUE, show_symlinks = FALSE, xerrno; |
| 339 |
int auth_code = PR_AUTH_OK; |
| 340 |
struct stat st; |
| 341 |
const char *default_chdir, *default_root, *home_dir; |
| 342 |
const char *sess_ttyname = NULL, *xferlog = NULL; |
| 343 |
cmd_rec *cmd; |
| 344 |
|
| 345 |
session.hide_password = TRUE; |
| 346 |
|
| 347 |
pw = pr_auth_getpwnam(p, user); |
| 348 |
if (pw == NULL) { |
| 349 |
xerrno = errno; |
| 350 |
auth_code = PR_AUTH_NOPWD; |
| 351 |
|
| 352 |
|
| 353 |
pr_log_auth(PR_LOG_NOTICE, |
| 354 |
"USER %s (Login failed): Unable to retrieve user information: %s", user, |
| 355 |
strerror(xerrno)); |
| 356 |
pr_event_generate("mod_auth.authentication-code", &auth_code); |
| 357 |
|
| 358 |
errno = xerrno; |
| 359 |
return -1; |
| 360 |
} |
| 361 |
|
| 362 |
pw = dup_passwd(p, pw); |
| 363 |
|
| 364 |
if (pw->pw_uid == PR_ROOT_UID) { |
| 365 |
pr_event_generate("mod_auth.root-login", NULL); |
| 366 |
|
| 367 |
c = find_config(main_server->conf, CONF_PARAM, "RootLogin", FALSE); |
| 368 |
if (c) { |
| 369 |
if (*((int *) c->argv[0]) == FALSE) { |
| 370 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 371 |
"root login attempted, denied by RootLogin configuration"); |
| 372 |
pr_log_auth(PR_LOG_NOTICE, "SECURITY VIOLATION: Root login attempted"); |
| 373 |
return -1; |
| 374 |
} |
| 375 |
|
| 376 |
} else { |
| 377 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 378 |
"root login attempted, denied by RootLogin configuration"); |
| 379 |
pr_log_auth(PR_LOG_NOTICE, "SECURITY VIOLATION: Root login attempted"); |
| 380 |
return -1; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
res = pr_auth_is_valid_shell(main_server->conf, pw->pw_shell); |
| 385 |
if (res == FALSE) { |
| 386 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 387 |
"authentication for user '%s' failed: Invalid shell", user); |
| 388 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Invalid shell: '%s'", |
| 389 |
user, pw->pw_shell); |
| 390 |
return -1; |
| 391 |
} |
| 392 |
|
| 393 |
res = pr_auth_banned_by_ftpusers(main_server->conf, pw->pw_name); |
| 394 |
if (res == TRUE) { |
| 395 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 396 |
"authentication for user '%s' failed: User in " PR_FTPUSERS_PATH, user); |
| 397 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User in " |
| 398 |
PR_FTPUSERS_PATH, pw->pw_name); |
| 399 |
return -1; |
| 400 |
} |
| 401 |
|
| 402 |
session.user = pstrdup(p, pw->pw_name); |
| 403 |
session.user_homedir = pstrdup(p, pw->pw_dir); |
| 404 |
session.group = pstrdup(p, pr_auth_gid2name(p, pw->pw_gid)); |
| 405 |
|
| 406 |
session.login_uid = pw->pw_uid; |
| 407 |
session.login_gid = pw->pw_gid; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
pw->pw_dir = (char *) path_subst_uservar(p, (const char **) &pw->pw_dir); |
| 417 |
|
| 418 |
if (session.gids == NULL && |
| 419 |
session.groups == NULL) { |
| 420 |
res = pr_auth_getgroups(p, pw->pw_name, &session.gids, &session.groups); |
| 421 |
if (res < 1) { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 427 |
"no supplemental groups found for user '%s', " |
| 428 |
"using primary group %s (GID %lu)", pw->pw_name, session.group, |
| 429 |
(unsigned long) session.login_gid); |
| 430 |
|
| 431 |
session.gids = make_array(p, 2, sizeof(gid_t)); |
| 432 |
session.groups = make_array(p, 2, sizeof(char *)); |
| 433 |
|
| 434 |
*((gid_t *) push_array(session.gids)) = session.login_gid; |
| 435 |
*((char **) push_array(session.groups)) = pstrdup(p, session.group); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
login_acl = login_check_limits(main_server->conf, FALSE, TRUE, &i); |
| 440 |
if (!login_acl) { |
| 441 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Limit configuration " |
| 442 |
"denies login", user); |
| 443 |
return -1; |
| 444 |
} |
| 445 |
|
| 446 |
PRIVS_USER |
| 447 |
home_dir = dir_realpath(p, pw->pw_dir); |
| 448 |
PRIVS_RELINQUISH |
| 449 |
|
| 450 |
if (home_dir != NULL) { |
| 451 |
sstrncpy(session.cwd, home_dir, sizeof(session.cwd)); |
| 452 |
|
| 453 |
} else { |
| 454 |
sstrncpy(session.cwd, pw->pw_dir, sizeof(session.cwd)); |
| 455 |
} |
| 456 |
|
| 457 |
c = find_config(main_server->conf, CONF_PARAM, "CreateHome", FALSE); |
| 458 |
if (c != NULL) { |
| 459 |
if (*((unsigned char *) c->argv[0]) == TRUE) { |
| 460 |
if (create_home(p, session.cwd, user, pw->pw_uid, pw->pw_gid) < 0) { |
| 461 |
return -1; |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
default_chdir = get_default_chdir(p); |
| 467 |
if (default_chdir != NULL) { |
| 468 |
default_chdir = dir_abs_path(p, default_chdir, TRUE); |
| 469 |
sstrncpy(session.cwd, default_chdir, sizeof(session.cwd)); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
login_acl = login_check_limits(main_server->conf, FALSE, TRUE, &i); |
| 474 |
if (!login_acl) { |
| 475 |
pr_log_auth(PR_LOG_NOTICE, "USER %s: Limit configuration denies login", |
| 476 |
user); |
| 477 |
return -1; |
| 478 |
} |
| 479 |
|
| 480 |
resolve_deferred_dirs(main_server); |
| 481 |
fixup_dirs(main_server, CF_DEFER); |
| 482 |
|
| 483 |
session.wtmp_log = TRUE; |
| 484 |
|
| 485 |
c = find_config(main_server->conf, CONF_PARAM, "WtmpLog", FALSE); |
| 486 |
if (c != NULL && |
| 487 |
*((unsigned char *) c->argv[0]) == FALSE) { |
| 488 |
session.wtmp_log = FALSE; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
#if defined(HAVE_UTMPX_H) && \ |
| 506 |
defined(__FreeBSD_version) && __FreeBSD_version >= 900007 |
| 507 |
if (session.wtmp_log == TRUE) { |
| 508 |
session.wtmp_log = FALSE; |
| 509 |
|
| 510 |
pr_log_debug(DEBUG5, |
| 511 |
"WtpmLog automatically disabled; see Bug#3482 for details"); |
| 512 |
} |
| 513 |
#endif |
| 514 |
|
| 515 |
PRIVS_ROOT |
| 516 |
|
| 517 |
if (session.wtmp_log) { |
| 518 |
sess_ttyname = pr_session_get_ttyname(p); |
| 519 |
|
| 520 |
log_wtmp(sess_ttyname, session.user, session.c->remote_name, |
| 521 |
session.c->remote_addr); |
| 522 |
} |
| 523 |
|
| 524 |
#if defined(PR_USE_LASTLOG) |
| 525 |
c = find_config(main_server->conf, CONF_PARAM, "UseLastlog", FALSE); |
| 526 |
if (c != NULL && |
| 527 |
*((unsigned char *) c->argv[0]) == TRUE) { |
| 528 |
if (sess_ttyname == NULL) { |
| 529 |
sess_ttyname = pr_session_get_ttyname(p); |
| 530 |
} |
| 531 |
|
| 532 |
log_lastlog(pw->pw_uid, session.user, sess_ttyname, |
| 533 |
session.c->remote_addr); |
| 534 |
} |
| 535 |
#endif |
| 536 |
|
| 537 |
c = find_config(main_server->conf, CONF_PARAM, "TransferLog", FALSE); |
| 538 |
if (c == NULL) { |
| 539 |
xferlog = PR_XFERLOG_PATH; |
| 540 |
|
| 541 |
} else { |
| 542 |
xferlog = c->argv[0]; |
| 543 |
} |
| 544 |
|
| 545 |
if (strcasecmp(xferlog, "none") == 0) { |
| 546 |
xferlog_open(NULL); |
| 547 |
|
| 548 |
} else { |
| 549 |
xferlog_open(xferlog); |
| 550 |
} |
| 551 |
|
| 552 |
pr_event_generate("mod_auth.authentication-code", &auth_code); |
| 553 |
|
| 554 |
res = set_groups(p, pw->pw_gid, session.gids); |
| 555 |
xerrno = errno; |
| 556 |
PRIVS_RELINQUISH |
| 557 |
|
| 558 |
if (res < 0) { |
| 559 |
if (xerrno != ENOSYS) { |
| 560 |
pr_log_pri(PR_LOG_WARNING, "unable to set process groups: %s", |
| 561 |
strerror(xerrno)); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
default_root = get_default_root(session.pool); |
| 566 |
if (default_root != NULL) { |
| 567 |
ensure_open_passwd(p); |
| 568 |
|
| 569 |
if (pr_auth_chroot(default_root) < 0) { |
| 570 |
pr_log_pri(PR_LOG_WARNING, "unable to set DefaultRoot directory '%s'", |
| 571 |
default_root); |
| 572 |
return -1; |
| 573 |
} |
| 574 |
|
| 575 |
if (strncmp(session.cwd, default_root, strlen(default_root)) == 0) { |
| 576 |
char *new_cwd; |
| 577 |
|
| 578 |
new_cwd = &session.cwd[strlen(default_root)]; |
| 579 |
|
| 580 |
if (*new_cwd == '/') { |
| 581 |
new_cwd++; |
| 582 |
} |
| 583 |
session.cwd[0] = '/'; |
| 584 |
|
| 585 |
sstrncpy(&session.cwd[1], new_cwd, sizeof(session.cwd)); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
pr_signals_block(); |
| 590 |
PRIVS_ROOT |
| 591 |
PRIVS_SETUP(pw->pw_uid, pw->pw_gid) |
| 592 |
pr_signals_unblock(); |
| 593 |
|
| 594 |
|
| 595 |
c = find_config(main_server->conf, CONF_PARAM, "RootRevoke", FALSE); |
| 596 |
if (c != NULL) { |
| 597 |
root_revoke = *((int *) c->argv[0]); |
| 598 |
|
| 599 |
if (root_revoke == FALSE) { |
| 600 |
pr_log_debug(DEBUG8, MOD_SFTP_VERSION |
| 601 |
": retaining root privileges per RootRevoke setting"); |
| 602 |
} |
| 603 |
|
| 604 |
} else { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
c = find_config(main_server->conf, CONF_PARAM, "UserOwner", TRUE); |
| 609 |
if (c != NULL) { |
| 610 |
pr_log_debug(DEBUG9, MOD_SFTP_VERSION |
| 611 |
": retaining root privileges per UserOwner setting"); |
| 612 |
root_revoke = FALSE; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
if (root_revoke) { |
| 617 |
PRIVS_ROOT |
| 618 |
PRIVS_REVOKE |
| 619 |
session.disable_id_switching = TRUE; |
| 620 |
} |
| 621 |
|
| 622 |
#ifdef HAVE_GETEUID |
| 623 |
if (getegid() != pw->pw_gid || |
| 624 |
geteuid() != pw->pw_uid) { |
| 625 |
pr_log_pri(PR_LOG_WARNING, |
| 626 |
"process effective IDs do not match expected IDs"); |
| 627 |
return -1; |
| 628 |
} |
| 629 |
#endif |
| 630 |
|
| 631 |
if (pw->pw_dir == NULL || |
| 632 |
strncmp(pw->pw_dir, "", 1) == 0) { |
| 633 |
pr_log_pri(PR_LOG_WARNING, "Home directory for user '%s' is NULL/empty", |
| 634 |
session.user); |
| 635 |
return -1; |
| 636 |
} |
| 637 |
|
| 638 |
c = find_config(main_server->conf, CONF_PARAM, "ShowSymlinks", FALSE); |
| 639 |
if (c != NULL) { |
| 640 |
if (*((unsigned char *) c->argv[0]) == TRUE) { |
| 641 |
show_symlinks = TRUE; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
if (pr_fsio_chdir_canon(session.cwd, !show_symlinks) < 0) { |
| 646 |
xerrno = errno; |
| 647 |
|
| 648 |
if (session.chroot_path != NULL || |
| 649 |
default_root != NULL) { |
| 650 |
|
| 651 |
pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to chroot " |
| 652 |
"directory %s", session.cwd, strerror(xerrno), |
| 653 |
(session.chroot_path ? session.chroot_path : default_root)); |
| 654 |
|
| 655 |
if (pr_fsio_chdir_canon("/", !show_symlinks) == -1) { |
| 656 |
xerrno = errno; |
| 657 |
|
| 658 |
pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"/\") failed: %s", session.user, |
| 659 |
strerror(xerrno)); |
| 660 |
errno = xerrno; |
| 661 |
return -1; |
| 662 |
} |
| 663 |
|
| 664 |
} else if (default_chdir) { |
| 665 |
pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to home " |
| 666 |
"directory %s", session.cwd, strerror(errno), pw->pw_dir); |
| 667 |
|
| 668 |
if (pr_fsio_chdir_canon(pw->pw_dir, !show_symlinks) == -1) { |
| 669 |
xerrno = errno; |
| 670 |
|
| 671 |
pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user, |
| 672 |
session.cwd, strerror(xerrno)); |
| 673 |
errno = xerrno; |
| 674 |
return -1; |
| 675 |
} |
| 676 |
|
| 677 |
} else { |
| 678 |
pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user, |
| 679 |
session.cwd, strerror(xerrno)); |
| 680 |
errno = xerrno; |
| 681 |
return -1; |
| 682 |
} |
| 683 |
|
| 684 |
} else { |
| 685 |
pr_log_debug(DEBUG10, "changed directory to '%s'", session.cwd); |
| 686 |
} |
| 687 |
|
| 688 |
sstrncpy(session.cwd, pr_fs_getcwd(), sizeof(session.cwd)); |
| 689 |
sstrncpy(session.vwd, pr_fs_getvwd(), sizeof(session.vwd)); |
| 690 |
|
| 691 |
|
| 692 |
cmd = pr_cmd_alloc(p, 1, C_PASS); |
| 693 |
cmd->arg = ""; |
| 694 |
dir_check_full(p, cmd, G_NONE, session.cwd, NULL); |
| 695 |
|
| 696 |
session.proc_prefix = pstrdup(session.pool, session.c->remote_name); |
| 697 |
session.sf_flags = 0; |
| 698 |
|
| 699 |
pr_log_auth(PR_LOG_INFO, "USER %s: Login successful", user); |
| 700 |
|
| 701 |
if (pw->pw_uid == PR_ROOT_UID) { |
| 702 |
pr_log_auth(PR_LOG_WARNING, "ROOT SFTP login successful"); |
| 703 |
} |
| 704 |
|
| 705 |
if (pr_fsio_stat(session.cwd, &st) != -1) { |
| 706 |
build_dyn_config2(p, session.cwd, &st); |
| 707 |
} |
| 708 |
|
| 709 |
pr_scoreboard_entry_update(session.pid, |
| 710 |
PR_SCORE_USER, session.user, |
| 711 |
PR_SCORE_CWD, session.cwd, |
| 712 |
NULL); |
| 713 |
|
| 714 |
pr_timer_remove(PR_TIMER_LOGIN, ANY_MODULE); |
| 715 |
|
| 716 |
auth_default_dir = pstrdup(session.pool, pr_fs_getvwd()); |
| 717 |
|
| 718 |
session.user = pstrdup(session.pool, session.user); |
| 719 |
|
| 720 |
if (session.user_homedir != NULL) { |
| 721 |
session.user_homedir = pstrdup(session.pool, session.user_homedir); |
| 722 |
} |
| 723 |
|
| 724 |
if (session.group != NULL) { |
| 725 |
session.group = pstrdup(session.pool, session.group); |
| 726 |
} |
| 727 |
|
| 728 |
if (session.gids != NULL) { |
| 729 |
session.gids = copy_array(session.pool, session.gids); |
| 730 |
} |
| 731 |
|
| 732 |
session.groups = copy_array_str(session.pool, session.groups); |
| 733 |
|
| 734 |
pr_resolve_fs_map(); |
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
static int send_userauth_banner_file(void) { |
| 739 |
struct ssh2_packet *pkt; |
| 740 |
char *path; |
| 741 |
unsigned char *buf, *ptr; |
| 742 |
const char *msg; |
| 743 |
int res; |
| 744 |
uint32_t buflen, bufsz; |
| 745 |
config_rec *c; |
| 746 |
pr_fh_t *fh; |
| 747 |
pool *sub_pool; |
| 748 |
struct stat st; |
| 749 |
|
| 750 |
if (auth_sent_userauth_banner_file) { |
| 751 |
|
| 752 |
return 0; |
| 753 |
} |
| 754 |
|
| 755 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPDisplayBanner", FALSE); |
| 756 |
if (c == NULL) { |
| 757 |
return 0; |
| 758 |
} |
| 759 |
path = c->argv[0]; |
| 760 |
|
| 761 |
if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_USERAUTH_BANNER)) { |
| 762 |
pr_trace_msg(trace_channel, 3, "unable to send SFTPDisplayBanner '%s': " |
| 763 |
"USERAUTH_BANNER supported by client", path); |
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
fh = pr_fsio_open_canon(path, O_RDONLY); |
| 768 |
if (fh == NULL) { |
| 769 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 770 |
"error opening SFTPDisplayBanner '%s': %s", path, strerror(errno)); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
res = pr_fsio_fstat(fh, &st); |
| 775 |
if (res < 0) { |
| 776 |
int xerrno = errno; |
| 777 |
|
| 778 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 779 |
"unable to stat SFTPDisplayBanner '%s': %s", path, strerror(xerrno)); |
| 780 |
|
| 781 |
pr_fsio_close(fh); |
| 782 |
return 0; |
| 783 |
} |
| 784 |
|
| 785 |
if (S_ISDIR(st.st_mode)) { |
| 786 |
int xerrno = EISDIR; |
| 787 |
|
| 788 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 789 |
"unable to use SFTPDisplayBanner '%s': %s", path, strerror(xerrno)); |
| 790 |
|
| 791 |
pr_fsio_close(fh); |
| 792 |
return 0; |
| 793 |
} |
| 794 |
|
| 795 |
sub_pool = make_sub_pool(auth_pool); |
| 796 |
pr_pool_tag(sub_pool, "SSH2 auth banner pool"); |
| 797 |
|
| 798 |
msg = sftp_display_fh_get_msg(sub_pool, fh); |
| 799 |
pr_fsio_close(fh); |
| 800 |
|
| 801 |
if (msg == NULL) { |
| 802 |
destroy_pool(sub_pool); |
| 803 |
return -1; |
| 804 |
} |
| 805 |
|
| 806 |
pr_trace_msg(trace_channel, 3, |
| 807 |
"sending userauth banner from SFTPDisplayBanner file '%s'", path); |
| 808 |
|
| 809 |
pkt = sftp_ssh2_packet_create(sub_pool); |
| 810 |
|
| 811 |
buflen = bufsz = strlen(msg) + 32; |
| 812 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 813 |
|
| 814 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_BANNER); |
| 815 |
sftp_msg_write_string(&buf, &buflen, msg); |
| 816 |
|
| 817 |
|
| 818 |
sftp_msg_write_string(&buf, &buflen, ""); |
| 819 |
|
| 820 |
pkt->payload = ptr; |
| 821 |
pkt->payload_len = (bufsz - buflen); |
| 822 |
|
| 823 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 824 |
destroy_pool(pkt->pool); |
| 825 |
|
| 826 |
if (res < 0) { |
| 827 |
destroy_pool(sub_pool); |
| 828 |
return -1; |
| 829 |
} |
| 830 |
|
| 831 |
auth_sent_userauth_banner_file = TRUE; |
| 832 |
destroy_pool(sub_pool); |
| 833 |
return 0; |
| 834 |
} |
| 835 |
|
| 836 |
static int send_userauth_failure(char *failed_meth) { |
| 837 |
struct ssh2_packet *pkt; |
| 838 |
unsigned char *buf, *ptr; |
| 839 |
char *meths; |
| 840 |
uint32_t buflen, bufsz = 1024; |
| 841 |
int res; |
| 842 |
|
| 843 |
pkt = sftp_ssh2_packet_create(auth_pool); |
| 844 |
|
| 845 |
if (failed_meth) { |
| 846 |
register unsigned int i; |
| 847 |
|
| 848 |
auth_avail_meths = NULL; |
| 849 |
auth_meths_enabled_flags = 0; |
| 850 |
|
| 851 |
for (i = 0 ; i < auth_chains->nelts; i++) { |
| 852 |
register unsigned int j; |
| 853 |
struct sftp_auth_chain *auth_chain; |
| 854 |
struct sftp_auth_method *meth = NULL; |
| 855 |
|
| 856 |
pr_signals_handle(); |
| 857 |
auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i]; |
| 858 |
|
| 859 |
for (j = 0; j < auth_chain->methods->nelts; j++) { |
| 860 |
struct sftp_auth_method *m; |
| 861 |
|
| 862 |
m = ((struct sftp_auth_method **) auth_chain->methods->elts)[j]; |
| 863 |
if (m->succeeded != TRUE && |
| 864 |
m->failed != TRUE) { |
| 865 |
meth = m; |
| 866 |
break; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
if (meth == NULL) { |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
continue; |
| 875 |
} |
| 876 |
|
| 877 |
if (strcmp(meth->method_name, failed_meth) != 0) { |
| 878 |
if (!(auth_meths_enabled_flags & meth->method_id)) { |
| 879 |
auth_meths_enabled_flags |= meth->method_id; |
| 880 |
|
| 881 |
if (auth_avail_meths != NULL) { |
| 882 |
auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",", |
| 883 |
meth->method_name, NULL); |
| 884 |
} else { |
| 885 |
auth_avail_meths = meth->method_name; |
| 886 |
} |
| 887 |
} |
| 888 |
} else { |
| 889 |
meth->failed = TRUE; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
if (auth_avail_meths == NULL) { |
| 894 |
|
| 895 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 896 |
"no more auth methods available, disconnecting"); |
| 897 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, |
| 898 |
NULL); |
| 899 |
} |
| 900 |
|
| 901 |
} else { |
| 902 |
if (auth_avail_meths == NULL) { |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
auth_avail_meths = ""; |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
meths = pstrdup(pkt->pool, auth_avail_meths); |
| 914 |
|
| 915 |
buflen = bufsz; |
| 916 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 917 |
|
| 918 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_FAILURE); |
| 919 |
sftp_msg_write_string(&buf, &buflen, meths); |
| 920 |
sftp_msg_write_bool(&buf, &buflen, FALSE); |
| 921 |
|
| 922 |
pkt->payload = ptr; |
| 923 |
pkt->payload_len = (bufsz - buflen); |
| 924 |
|
| 925 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 926 |
"sending userauth failure; remaining userauth methods: %s", meths); |
| 927 |
|
| 928 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 929 |
if (res < 0) { |
| 930 |
destroy_pool(pkt->pool); |
| 931 |
return -1; |
| 932 |
} |
| 933 |
|
| 934 |
return 0; |
| 935 |
} |
| 936 |
|
| 937 |
static int send_userauth_success(void) { |
| 938 |
struct ssh2_packet *pkt; |
| 939 |
unsigned char *buf, *ptr; |
| 940 |
uint32_t buflen, bufsz = 1024; |
| 941 |
int res; |
| 942 |
|
| 943 |
if (auth_sent_userauth_success) { |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
pkt = sftp_ssh2_packet_create(auth_pool); |
| 948 |
|
| 949 |
buflen = bufsz; |
| 950 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 951 |
|
| 952 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_SUCCESS); |
| 953 |
|
| 954 |
pkt->payload = ptr; |
| 955 |
pkt->payload_len = (bufsz - buflen); |
| 956 |
|
| 957 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 958 |
"sending userauth success"); |
| 959 |
|
| 960 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 961 |
if (res < 0) { |
| 962 |
destroy_pool(pkt->pool); |
| 963 |
return -1; |
| 964 |
} |
| 965 |
|
| 966 |
auth_sent_userauth_success = TRUE; |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
sftp_compress_init_read(SFTP_COMPRESS_FL_AUTHENTICATED); |
| 972 |
sftp_compress_init_write(SFTP_COMPRESS_FL_AUTHENTICATED); |
| 973 |
|
| 974 |
return 0; |
| 975 |
} |
| 976 |
|
| 977 |
static int set_userauth_success(const char *succeeded_meth) { |
| 978 |
register unsigned int i; |
| 979 |
int completed = FALSE; |
| 980 |
|
| 981 |
auth_avail_meths = NULL; |
| 982 |
auth_meths_enabled_flags = 0; |
| 983 |
|
| 984 |
for (i = 0 ; i < auth_chains->nelts; i++) { |
| 985 |
register unsigned int j; |
| 986 |
struct sftp_auth_chain *auth_chain; |
| 987 |
|
| 988 |
pr_signals_handle(); |
| 989 |
auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i]; |
| 990 |
|
| 991 |
for (j = 0; j < auth_chain->methods->nelts; j++) { |
| 992 |
struct sftp_auth_method *meth = NULL; |
| 993 |
|
| 994 |
meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j]; |
| 995 |
if (meth->succeeded != TRUE && |
| 996 |
meth->failed != TRUE) { |
| 997 |
|
| 998 |
if (strcmp(meth->method_name, succeeded_meth) == 0) { |
| 999 |
|
| 1000 |
meth->succeeded = TRUE; |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
j++; |
| 1005 |
if (j < auth_chain->methods->nelts) { |
| 1006 |
meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j]; |
| 1007 |
|
| 1008 |
if (!(auth_meths_enabled_flags & meth->method_id)) { |
| 1009 |
auth_meths_enabled_flags |= meth->method_id; |
| 1010 |
|
| 1011 |
if (auth_avail_meths != NULL) { |
| 1012 |
auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",", |
| 1013 |
meth->method_name, NULL); |
| 1014 |
} else { |
| 1015 |
auth_avail_meths = meth->method_name; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
break; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
for (i = 0 ; i < auth_chains->nelts; i++) { |
| 1030 |
register unsigned int j; |
| 1031 |
struct sftp_auth_chain *auth_chain; |
| 1032 |
int ok = TRUE; |
| 1033 |
|
| 1034 |
pr_signals_handle(); |
| 1035 |
auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i]; |
| 1036 |
|
| 1037 |
for (j = 0; j < auth_chain->methods->nelts; j++) { |
| 1038 |
struct sftp_auth_method *meth = NULL; |
| 1039 |
|
| 1040 |
meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j]; |
| 1041 |
if (meth->succeeded != TRUE) { |
| 1042 |
ok = FALSE; |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (ok == TRUE) { |
| 1048 |
|
| 1049 |
auth_chain->completed = completed = TRUE; |
| 1050 |
break; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (completed == TRUE) { |
| 1055 |
return 1; |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (auth_avail_meths == NULL) { |
| 1059 |
|
| 1060 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1061 |
"no more auth methods available, disconnecting"); |
| 1062 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, |
| 1063 |
NULL); |
| 1064 |
} |
| 1065 |
|
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static int send_userauth_methods(char partial_success) { |
| 1070 |
struct ssh2_packet *pkt; |
| 1071 |
unsigned char *buf, *ptr; |
| 1072 |
uint32_t buflen, bufsz = 1024; |
| 1073 |
int res; |
| 1074 |
|
| 1075 |
pkt = sftp_ssh2_packet_create(auth_pool); |
| 1076 |
|
| 1077 |
buflen = bufsz; |
| 1078 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 1079 |
|
| 1080 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1081 |
"sending acceptable userauth methods: %s", auth_avail_meths); |
| 1082 |
|
| 1083 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_FAILURE); |
| 1084 |
sftp_msg_write_string(&buf, &buflen, auth_avail_meths); |
| 1085 |
sftp_msg_write_bool(&buf, &buflen, partial_success); |
| 1086 |
|
| 1087 |
pkt->payload = ptr; |
| 1088 |
pkt->payload_len = (bufsz - buflen); |
| 1089 |
|
| 1090 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 1091 |
if (res < 0) { |
| 1092 |
destroy_pool(pkt->pool); |
| 1093 |
return -1; |
| 1094 |
} |
| 1095 |
|
| 1096 |
return 0; |
| 1097 |
} |
| 1098 |
|
| 1099 |
static void add_cmd_note(cmd_rec *cmd, const char *key, const char *val) { |
| 1100 |
int res; |
| 1101 |
|
| 1102 |
res = pr_table_add(cmd->notes, key, val, 0); |
| 1103 |
if (res < 0 && |
| 1104 |
errno != EEXIST) { |
| 1105 |
pr_trace_msg(trace_channel, 19, "error adding '%s' command note: %s", |
| 1106 |
key, strerror(errno)); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
static void remove_cmd_note(cmd_rec *cmd, const char *key) { |
| 1111 |
(void) pr_table_remove(cmd->notes, key, NULL); |
| 1112 |
} |
| 1113 |
|
| 1114 |
static void dispatch_cmd_err(cmd_rec *cmd) { |
| 1115 |
pr_response_add_err(R_530, "Login incorrect."); |
| 1116 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1117 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1118 |
pr_response_clear(&resp_err_list); |
| 1119 |
} |
| 1120 |
|
| 1121 |
static void dispatch_nonfatal_pass(cmd_rec *cmd, const char *method) { |
| 1122 |
const char *note = "mod_sftp.nonfatal-attempt"; |
| 1123 |
|
| 1124 |
add_cmd_note(cmd, note, method); |
| 1125 |
dispatch_cmd_err(cmd); |
| 1126 |
remove_cmd_note(cmd, note); |
| 1127 |
} |
| 1128 |
|
| 1129 |
static void incr_auth_attempts(const char *user, cmd_rec *pass_cmd) { |
| 1130 |
auth_attempts++; |
| 1131 |
|
| 1132 |
if (auth_attempts_max > 0 && |
| 1133 |
auth_attempts >= auth_attempts_max) { |
| 1134 |
pr_log_auth(PR_LOG_NOTICE, |
| 1135 |
"Maximum login attempts (%u) exceeded, connection refused", |
| 1136 |
auth_attempts_max); |
| 1137 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1138 |
"Maximum login attempts (%u) exceeded, refusing connection for user '%s'", |
| 1139 |
auth_attempts_max, user); |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 && |
| 1146 |
errno != EEXIST) { |
| 1147 |
pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in " |
| 1148 |
"session.notes: %s", strerror(errno)); |
| 1149 |
} |
| 1150 |
|
| 1151 |
dispatch_cmd_err(pass_cmd); |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
if (pr_table_get(session.notes, "mod_auth.orig-user", NULL) == NULL) { |
| 1163 |
(void) pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0); |
| 1164 |
} |
| 1165 |
|
| 1166 |
pr_event_generate("mod_auth.max-login-attempts", session.c); |
| 1167 |
pr_table_remove(session.notes, "mod_auth.orig-user", NULL); |
| 1168 |
|
| 1169 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
static int handle_auth_success(pool *p, const char *user) { |
| 1174 |
int res = 0; |
| 1175 |
|
| 1176 |
if (success_handler != NULL) { |
| 1177 |
res = (success_handler)(p, user); |
| 1178 |
} |
| 1179 |
|
| 1180 |
return res; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
static int handle_userauth_req(struct ssh2_packet *pkt, char **service) { |
| 1185 |
unsigned char *buf; |
| 1186 |
const char *orig_user, *user; |
| 1187 |
char *method; |
| 1188 |
uint32_t buflen; |
| 1189 |
cmd_rec *cmd, *user_cmd, *pass_cmd; |
| 1190 |
int res, send_userauth_fail = FALSE; |
| 1191 |
config_rec *c; |
| 1192 |
|
| 1193 |
buf = pkt->payload; |
| 1194 |
buflen = pkt->payload_len; |
| 1195 |
|
| 1196 |
orig_user = sftp_msg_read_string(pkt->pool, &buf, &buflen); |
| 1197 |
|
| 1198 |
user_cmd = pr_cmd_alloc(pkt->pool, 2, pstrdup(pkt->pool, C_USER), orig_user); |
| 1199 |
user_cmd->arg = (char *) orig_user; |
| 1200 |
|
| 1201 |
pass_cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, C_PASS)); |
| 1202 |
pass_cmd->arg = pstrdup(pkt->pool, "(hidden)"); |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
if (pr_cmd_dispatch_phase(user_cmd, PRE_CMD, 0) < 0) { |
| 1208 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1209 |
"authentication request for user '%s' blocked by '%s' handler", |
| 1210 |
orig_user, (char *) user_cmd->argv[0]); |
| 1211 |
|
| 1212 |
dispatch_cmd_err(user_cmd); |
| 1213 |
return -1; |
| 1214 |
} |
| 1215 |
|
| 1216 |
if (strcmp(orig_user, user_cmd->arg) == 0) { |
| 1217 |
user = orig_user; |
| 1218 |
|
| 1219 |
} else { |
| 1220 |
user = user_cmd->arg; |
| 1221 |
} |
| 1222 |
|
| 1223 |
if (auth_user != NULL) { |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
if (strcmp(orig_user, auth_user) != 0) { |
| 1229 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1230 |
"client used different user name '%s' in USERAUTH_REQUEST (was '%s'), " |
| 1231 |
"disconnecting", orig_user, auth_user); |
| 1232 |
|
| 1233 |
dispatch_cmd_err(user_cmd); |
| 1234 |
return -1; |
| 1235 |
} |
| 1236 |
|
| 1237 |
} else { |
| 1238 |
auth_user = pstrdup(auth_pool, orig_user); |
| 1239 |
} |
| 1240 |
|
| 1241 |
*service = sftp_msg_read_string(pkt->pool, &buf, &buflen); |
| 1242 |
if (auth_service != NULL) { |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
if (strcmp(*service, auth_service) != 0) { |
| 1248 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1249 |
"client used different service name '%s' in USERAUTH_REQUEST (was " |
| 1250 |
"'%s'), disconnecting", *service, auth_service); |
| 1251 |
|
| 1252 |
dispatch_cmd_err(user_cmd); |
| 1253 |
return -1; |
| 1254 |
} |
| 1255 |
|
| 1256 |
} else { |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
if (strncmp(*service, "ssh-userauth", 13) == 0 || |
| 1270 |
strncmp(*service, "ssh-connection", 15) == 0) { |
| 1271 |
auth_service = pstrdup(auth_pool, *service); |
| 1272 |
|
| 1273 |
} else { |
| 1274 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1275 |
"client requested unknown/unsupported service name '%s' in " |
| 1276 |
"USERAUTH_REQUEST, disconnecting", *service); |
| 1277 |
|
| 1278 |
dispatch_cmd_err(user_cmd); |
| 1279 |
return -1; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
pr_response_add(R_331, "Password required for %s", user); |
| 1284 |
pr_cmd_dispatch_phase(user_cmd, POST_CMD, 0); |
| 1285 |
pr_cmd_dispatch_phase(user_cmd, LOG_CMD, 0); |
| 1286 |
pr_response_clear(&resp_list); |
| 1287 |
|
| 1288 |
method = sftp_msg_read_string(pkt->pool, &buf, &buflen); |
| 1289 |
|
| 1290 |
pr_trace_msg(trace_channel, 10, "auth requested for user '%s', service '%s', " |
| 1291 |
"using method '%s'", user, *service, method); |
| 1292 |
|
| 1293 |
(void) pr_table_remove(session.notes, "mod_auth.orig-user", NULL); |
| 1294 |
if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 && |
| 1295 |
errno != EEXIST) { |
| 1296 |
pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in " |
| 1297 |
"session.notes: %s", strerror(errno)); |
| 1298 |
} |
| 1299 |
|
| 1300 |
cmd = pr_cmd_alloc(pkt->pool, 3, pstrdup(pkt->pool, "USERAUTH_REQUEST"), |
| 1301 |
pstrdup(pkt->pool, user), pstrdup(pkt->pool, method)); |
| 1302 |
cmd->arg = pstrcat(pkt->pool, user, " ", method, NULL); |
| 1303 |
cmd->cmd_class = CL_AUTH|CL_SSH; |
| 1304 |
cmd->cmd_id = SFTP_CMD_ID; |
| 1305 |
|
| 1306 |
if (auth_attempts_max > 0 && |
| 1307 |
auth_attempts > auth_attempts_max) { |
| 1308 |
pr_log_auth(PR_LOG_NOTICE, |
| 1309 |
"Maximum login attempts (%u) exceeded, connection refused", |
| 1310 |
auth_attempts_max); |
| 1311 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1312 |
"Maximum login attempts (%u) exceeded, refusing connection for user '%s'", |
| 1313 |
auth_attempts_max, user); |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 && |
| 1320 |
errno != EEXIST) { |
| 1321 |
pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in " |
| 1322 |
"session.notes: %s", strerror(errno)); |
| 1323 |
} |
| 1324 |
|
| 1325 |
dispatch_cmd_err(pass_cmd); |
| 1326 |
pr_event_generate("mod_auth.max-login-attempts", session.c); |
| 1327 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 1328 |
} |
| 1329 |
|
| 1330 |
set_userauth_methods(); |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
if (strcmp(method, "none") != 0) { |
| 1338 |
pr_env_unset(cmd->pool, "SFTP_USER_AUTH_METHOD"); |
| 1339 |
pr_env_set(cmd->pool, "SFTP_USER_AUTH_METHOD", method); |
| 1340 |
(void) pr_table_add_dup(session.notes, "SFTP_USER_AUTH_METHOD", method, 0); |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
if (strncmp(method, "none", 5) == 0) { |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
if (send_userauth_methods(FALSE) < 0) { |
| 1359 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1360 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1361 |
return -1; |
| 1362 |
} |
| 1363 |
|
| 1364 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1365 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1366 |
pr_response_clear(&resp_list); |
| 1367 |
|
| 1368 |
return 0; |
| 1369 |
|
| 1370 |
} else if (strncmp(method, "publickey", 10) == 0) { |
| 1371 |
if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_PUBLICKEY) { |
| 1372 |
int xerrno; |
| 1373 |
|
| 1374 |
res = sftp_auth_publickey(pkt, pass_cmd, orig_user, user, *service, |
| 1375 |
&buf, &buflen, &send_userauth_fail); |
| 1376 |
xerrno = errno; |
| 1377 |
|
| 1378 |
if (res < 0) { |
| 1379 |
pr_event_generate("mod_sftp.ssh2.auth-publickey.failed", NULL); |
| 1380 |
|
| 1381 |
} else if (res == 1) { |
| 1382 |
pr_event_generate("mod_sftp.ssh2.auth-publickey", NULL); |
| 1383 |
} |
| 1384 |
|
| 1385 |
errno = xerrno; |
| 1386 |
|
| 1387 |
} else { |
| 1388 |
pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method); |
| 1389 |
|
| 1390 |
if (send_userauth_methods(FALSE) < 0) { |
| 1391 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1392 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1393 |
return -1; |
| 1394 |
} |
| 1395 |
|
| 1396 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1397 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1398 |
|
| 1399 |
incr_auth_attempts(user, pass_cmd); |
| 1400 |
return 0; |
| 1401 |
} |
| 1402 |
|
| 1403 |
} else if (strncmp(method, "keyboard-interactive", 21) == 0) { |
| 1404 |
if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_KBDINT) { |
| 1405 |
int xerrno = errno; |
| 1406 |
|
| 1407 |
res = sftp_auth_kbdint(pkt, pass_cmd, orig_user, user, *service, |
| 1408 |
&buf, &buflen, &send_userauth_fail); |
| 1409 |
xerrno = errno; |
| 1410 |
|
| 1411 |
if (res < 0) { |
| 1412 |
pr_event_generate("mod_sftp.ssh2.auth-kbdint.failed", NULL); |
| 1413 |
|
| 1414 |
} else if (res == 1) { |
| 1415 |
pr_event_generate("mod_sftp.ssh2.auth-kbdint", NULL); |
| 1416 |
} |
| 1417 |
|
| 1418 |
errno = xerrno; |
| 1419 |
|
| 1420 |
} else { |
| 1421 |
pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method); |
| 1422 |
|
| 1423 |
if (send_userauth_methods(FALSE) < 0) { |
| 1424 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1425 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1426 |
return -1; |
| 1427 |
} |
| 1428 |
|
| 1429 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1430 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1431 |
|
| 1432 |
incr_auth_attempts(user, pass_cmd); |
| 1433 |
return 0; |
| 1434 |
} |
| 1435 |
|
| 1436 |
} else if (strncmp(method, "password", 9) == 0) { |
| 1437 |
if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_PASSWORD) { |
| 1438 |
int xerrno; |
| 1439 |
|
| 1440 |
res = sftp_auth_password(pkt, pass_cmd, orig_user, user, *service, |
| 1441 |
&buf, &buflen, &send_userauth_fail); |
| 1442 |
xerrno = errno; |
| 1443 |
|
| 1444 |
if (res < 0) { |
| 1445 |
pr_event_generate("mod_sftp.ssh2.auth-password.failed", NULL); |
| 1446 |
|
| 1447 |
} else if (res == 1) { |
| 1448 |
pr_event_generate("mod_sftp.ssh2.auth-password", NULL); |
| 1449 |
} |
| 1450 |
|
| 1451 |
errno = xerrno; |
| 1452 |
|
| 1453 |
} else { |
| 1454 |
pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method); |
| 1455 |
|
| 1456 |
if (send_userauth_methods(FALSE) < 0) { |
| 1457 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1458 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1459 |
return -1; |
| 1460 |
} |
| 1461 |
|
| 1462 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1463 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1464 |
|
| 1465 |
incr_auth_attempts(user, pass_cmd); |
| 1466 |
return 0; |
| 1467 |
} |
| 1468 |
|
| 1469 |
} else if (strncmp(method, "hostbased", 10) == 0) { |
| 1470 |
if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_HOSTBASED) { |
| 1471 |
int xerrno; |
| 1472 |
|
| 1473 |
res = sftp_auth_hostbased(pkt, pass_cmd, orig_user, user, *service, |
| 1474 |
&buf, &buflen, &send_userauth_fail); |
| 1475 |
xerrno = errno; |
| 1476 |
|
| 1477 |
if (res < 0) { |
| 1478 |
pr_event_generate("mod_sftp.ssh2.auth-hostbased.failed", NULL); |
| 1479 |
|
| 1480 |
} else if (res == 1) { |
| 1481 |
pr_event_generate("mod_sftp.ssh2.auth-hostbased", NULL); |
| 1482 |
} |
| 1483 |
|
| 1484 |
errno = xerrno; |
| 1485 |
|
| 1486 |
} else { |
| 1487 |
pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method); |
| 1488 |
|
| 1489 |
if (send_userauth_methods(FALSE) < 0) { |
| 1490 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1491 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1492 |
return -1; |
| 1493 |
} |
| 1494 |
|
| 1495 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1496 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1497 |
|
| 1498 |
incr_auth_attempts(user, pass_cmd); |
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
} else { |
| 1503 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1504 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1505 |
|
| 1506 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1507 |
"unsupported authentication method '%s' requested", method); |
| 1508 |
|
| 1509 |
incr_auth_attempts(user, pass_cmd); |
| 1510 |
return 0; |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
pass_cmd->arg = pstrdup(pkt->pool, "(hidden)"); |
| 1517 |
|
| 1518 |
if (res <= 0) { |
| 1519 |
int xerrno = errno; |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
if (res < 0) { |
| 1528 |
dispatch_cmd_err(pass_cmd); |
| 1529 |
|
| 1530 |
} else { |
| 1531 |
dispatch_nonfatal_pass(pass_cmd, method); |
| 1532 |
} |
| 1533 |
|
| 1534 |
pr_cmd_dispatch_phase(cmd, res == 0 ? POST_CMD : POST_CMD_ERR, 0); |
| 1535 |
pr_cmd_dispatch_phase(cmd, res == 0 ? LOG_CMD : LOG_CMD_ERR, 0); |
| 1536 |
|
| 1537 |
if (send_userauth_fail) { |
| 1538 |
errno = xerrno; |
| 1539 |
|
| 1540 |
if (send_userauth_failure(errno != EPERM ? NULL : method) < 0) { |
| 1541 |
return -1; |
| 1542 |
} |
| 1543 |
|
| 1544 |
incr_auth_attempts(user, pass_cmd); |
| 1545 |
} |
| 1546 |
|
| 1547 |
return res; |
| 1548 |
} |
| 1549 |
|
| 1550 |
res = set_userauth_success(method); |
| 1551 |
if (res == 0) { |
| 1552 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1553 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1554 |
|
| 1555 |
if (send_userauth_methods(TRUE) < 0) { |
| 1556 |
return -1; |
| 1557 |
} |
| 1558 |
|
| 1559 |
return res; |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
res = handle_auth_success(pkt->pool, user); |
| 1569 |
if (res < 0) { |
| 1570 |
dispatch_cmd_err(pass_cmd); |
| 1571 |
|
| 1572 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1573 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1574 |
|
| 1575 |
if (send_userauth_failure(NULL) < 0) { |
| 1576 |
return -1; |
| 1577 |
} |
| 1578 |
|
| 1579 |
return 0; |
| 1580 |
} |
| 1581 |
|
| 1582 |
if (send_userauth_success() < 0) { |
| 1583 |
dispatch_cmd_err(pass_cmd); |
| 1584 |
|
| 1585 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1586 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1587 |
|
| 1588 |
return -1; |
| 1589 |
} |
| 1590 |
|
| 1591 |
if (session.auth_mech != NULL) { |
| 1592 |
pr_log_debug(DEBUG2, "user '%s' authenticated by %s", user, |
| 1593 |
session.auth_mech); |
| 1594 |
} |
| 1595 |
|
| 1596 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1597 |
"user '%s' authenticated via '%s' method", user, method); |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
pr_response_add(R_230, "User %s logged in", user); |
| 1603 |
pr_cmd_dispatch_phase(pass_cmd, POST_CMD, 0); |
| 1604 |
pr_cmd_dispatch_phase(pass_cmd, LOG_CMD, 0); |
| 1605 |
pr_response_clear(&resp_list); |
| 1606 |
|
| 1607 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1608 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
c = find_config(main_server->conf, CONF_PARAM, "Protocols", FALSE); |
| 1614 |
if (c != NULL) { |
| 1615 |
register unsigned int i; |
| 1616 |
unsigned int services = 0UL; |
| 1617 |
array_header *protocols; |
| 1618 |
char **elts; |
| 1619 |
|
| 1620 |
protocols = c->argv[0]; |
| 1621 |
elts = protocols->elts; |
| 1622 |
|
| 1623 |
for (i = 0; i < protocols->nelts; i++) { |
| 1624 |
char *protocol; |
| 1625 |
|
| 1626 |
protocol = elts[i]; |
| 1627 |
if (protocol != NULL) { |
| 1628 |
if (strncasecmp(protocol, "sftp", 5) == 0) { |
| 1629 |
services |= SFTP_SERVICE_FL_SFTP; |
| 1630 |
|
| 1631 |
} else if (strncasecmp(protocol, "scp", 4) == 0) { |
| 1632 |
services |= SFTP_SERVICE_FL_SCP; |
| 1633 |
|
| 1634 |
} else if (strncasecmp(protocol, "date", 5) == 0) { |
| 1635 |
services |= SFTP_SERVICE_FL_DATE; |
| 1636 |
} |
| 1637 |
} |
| 1638 |
} |
| 1639 |
|
| 1640 |
sftp_services = services; |
| 1641 |
} |
| 1642 |
|
| 1643 |
sftp_keys_send_hostkeys(pkt->pool); |
| 1644 |
return 1; |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
struct sftp_auth_chain *sftp_auth_chain_alloc(pool *p) { |
| 1649 |
pool *sub_pool; |
| 1650 |
struct sftp_auth_chain *auth_chain; |
| 1651 |
|
| 1652 |
if (p == NULL) { |
| 1653 |
errno = EINVAL; |
| 1654 |
return NULL; |
| 1655 |
} |
| 1656 |
|
| 1657 |
sub_pool = pr_pool_create_sz(p, 256); |
| 1658 |
pr_pool_tag(sub_pool, "SSH2 Auth Chain Pool"); |
| 1659 |
|
| 1660 |
auth_chain = pcalloc(sub_pool, sizeof(struct sftp_auth_chain)); |
| 1661 |
auth_chain->pool = sub_pool; |
| 1662 |
auth_chain->methods = make_array(sub_pool, 1, |
| 1663 |
sizeof(struct sftp_auth_method *)); |
| 1664 |
auth_chain->completed = FALSE; |
| 1665 |
|
| 1666 |
return auth_chain; |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
int sftp_auth_chain_isvalid(struct sftp_auth_chain *auth_chain) { |
| 1674 |
register unsigned int i; |
| 1675 |
int has_password = FALSE, has_hostbased = FALSE; |
| 1676 |
|
| 1677 |
for (i = 0; i < auth_chain->methods->nelts; i++) { |
| 1678 |
struct sftp_auth_method *meth; |
| 1679 |
|
| 1680 |
meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[i]; |
| 1681 |
|
| 1682 |
switch (meth->method_id) { |
| 1683 |
case SFTP_AUTH_FL_METH_PASSWORD: |
| 1684 |
if (has_password == TRUE) { |
| 1685 |
errno = EPERM; |
| 1686 |
return -1; |
| 1687 |
} |
| 1688 |
|
| 1689 |
has_password = TRUE; |
| 1690 |
break; |
| 1691 |
|
| 1692 |
case SFTP_AUTH_FL_METH_HOSTBASED: |
| 1693 |
if (has_hostbased == TRUE) { |
| 1694 |
errno = EPERM; |
| 1695 |
return -1; |
| 1696 |
} |
| 1697 |
|
| 1698 |
has_hostbased = TRUE; |
| 1699 |
break; |
| 1700 |
|
| 1701 |
case SFTP_AUTH_FL_METH_PUBLICKEY: |
| 1702 |
case SFTP_AUTH_FL_METH_KBDINT: |
| 1703 |
default: |
| 1704 |
break; |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
return 0; |
| 1709 |
} |
| 1710 |
|
| 1711 |
int sftp_auth_chain_add_method(struct sftp_auth_chain *auth_chain, |
| 1712 |
unsigned int method_id, const char *method_name, |
| 1713 |
const char *submethod_name) { |
| 1714 |
struct sftp_auth_method *meth; |
| 1715 |
|
| 1716 |
if (auth_chain == NULL || |
| 1717 |
method_name == NULL) { |
| 1718 |
errno = EINVAL; |
| 1719 |
return -1; |
| 1720 |
} |
| 1721 |
|
| 1722 |
|
| 1723 |
if (submethod_name != NULL && |
| 1724 |
method_id != SFTP_AUTH_FL_METH_KBDINT) { |
| 1725 |
errno = EPERM; |
| 1726 |
return -1; |
| 1727 |
} |
| 1728 |
|
| 1729 |
meth = pcalloc(auth_chain->pool, sizeof(struct sftp_auth_method)); |
| 1730 |
meth->method_id = method_id; |
| 1731 |
meth->method_name = pstrdup(auth_chain->pool, method_name); |
| 1732 |
if (submethod_name != NULL) { |
| 1733 |
meth->submethod_name = pstrdup(auth_chain->pool, submethod_name); |
| 1734 |
} |
| 1735 |
meth->succeeded = FALSE; |
| 1736 |
meth->failed = FALSE; |
| 1737 |
|
| 1738 |
*((struct sftp_auth_method **) push_array(auth_chain->methods)) = meth; |
| 1739 |
return 0; |
| 1740 |
} |
| 1741 |
|
| 1742 |
int sftp_auth_chain_parse_method(pool *p, const char *name, |
| 1743 |
unsigned int *method_id, const char **method_name, |
| 1744 |
const char **submethod_name) { |
| 1745 |
char *ptr; |
| 1746 |
size_t method_namelen; |
| 1747 |
|
| 1748 |
if (name == NULL || |
| 1749 |
method_id == NULL || |
| 1750 |
method_name == NULL) { |
| 1751 |
errno = EINVAL; |
| 1752 |
return -1; |
| 1753 |
} |
| 1754 |
|
| 1755 |
|
| 1756 |
ptr = strchr(name, ':'); |
| 1757 |
if (ptr == NULL) { |
| 1758 |
method_namelen = strlen(name); |
| 1759 |
} else { |
| 1760 |
method_namelen = ptr - name - 1; |
| 1761 |
} |
| 1762 |
|
| 1763 |
if (strncmp(name, "publickey", method_namelen) == 0) { |
| 1764 |
*method_id = SFTP_AUTH_FL_METH_PUBLICKEY; |
| 1765 |
*method_name = name; |
| 1766 |
|
| 1767 |
} else if (strncmp(name, "hostbased", method_namelen) == 0) { |
| 1768 |
*method_id = SFTP_AUTH_FL_METH_HOSTBASED; |
| 1769 |
*method_name = name; |
| 1770 |
|
| 1771 |
} else if (strncmp(name, "password", method_namelen) == 0) { |
| 1772 |
*method_id = SFTP_AUTH_FL_METH_PASSWORD; |
| 1773 |
*method_name = name; |
| 1774 |
|
| 1775 |
} else if (strncmp(name, "keyboard-interactive", method_namelen) == 0) { |
| 1776 |
*method_id = SFTP_AUTH_FL_METH_KBDINT; |
| 1777 |
|
| 1778 |
if (sftp_kbdint_have_drivers() == 0) { |
| 1779 |
errno = EPERM; |
| 1780 |
return -1; |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
if (ptr != NULL) { |
| 1787 |
if (sftp_kbdint_get_driver(ptr) == NULL) { |
| 1788 |
errno = EPERM; |
| 1789 |
return -1; |
| 1790 |
} |
| 1791 |
|
| 1792 |
*method_name = pstrndup(p, name, method_namelen); |
| 1793 |
if (submethod_name != NULL) { |
| 1794 |
*submethod_name = ptr; |
| 1795 |
} |
| 1796 |
|
| 1797 |
} else { |
| 1798 |
*method_name = name; |
| 1799 |
} |
| 1800 |
|
| 1801 |
} else { |
| 1802 |
|
| 1803 |
errno = EINVAL; |
| 1804 |
return -1; |
| 1805 |
} |
| 1806 |
|
| 1807 |
return 0; |
| 1808 |
} |
| 1809 |
|
| 1810 |
array_header *sftp_auth_chain_parse_method_chain(pool *p, |
| 1811 |
const char *method_list) { |
| 1812 |
char *ptr; |
| 1813 |
size_t method_listlen; |
| 1814 |
array_header *method_names; |
| 1815 |
|
| 1816 |
if (p == NULL || |
| 1817 |
method_list == NULL) { |
| 1818 |
errno = EINVAL; |
| 1819 |
return NULL; |
| 1820 |
} |
| 1821 |
|
| 1822 |
ptr = strchr(method_list, '+'); |
| 1823 |
if (ptr == NULL) { |
| 1824 |
method_names = make_array(p, 0, sizeof(char *)); |
| 1825 |
*((char **) push_array(method_names)) = pstrdup(p, method_list); |
| 1826 |
return method_names; |
| 1827 |
} |
| 1828 |
|
| 1829 |
if (ptr == method_list) { |
| 1830 |
|
| 1831 |
errno = EPERM; |
| 1832 |
return NULL; |
| 1833 |
} |
| 1834 |
|
| 1835 |
method_listlen = strlen(method_list); |
| 1836 |
if (method_list[method_listlen-1] == '+') { |
| 1837 |
|
| 1838 |
errno = EPERM; |
| 1839 |
return NULL; |
| 1840 |
} |
| 1841 |
|
| 1842 |
method_names = make_array(p, 0, sizeof(char *)); |
| 1843 |
|
| 1844 |
while (ptr != NULL) { |
| 1845 |
size_t namelen; |
| 1846 |
|
| 1847 |
pr_signals_handle(); |
| 1848 |
|
| 1849 |
namelen = (ptr - method_list); |
| 1850 |
if (namelen == 0) { |
| 1851 |
|
| 1852 |
errno = EPERM; |
| 1853 |
return NULL; |
| 1854 |
} |
| 1855 |
|
| 1856 |
*((char **) push_array(method_names)) = pstrndup(p, method_list, namelen); |
| 1857 |
|
| 1858 |
method_list = ptr + 1; |
| 1859 |
ptr = strchr(method_list, '+'); |
| 1860 |
|
| 1861 |
|
| 1862 |
if (ptr == NULL) { |
| 1863 |
*((char **) push_array(method_names)) = pstrdup(p, method_list); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
return method_names; |
| 1868 |
} |
| 1869 |
|
| 1870 |
char *sftp_auth_get_default_dir(void) { |
| 1871 |
return auth_default_dir; |
| 1872 |
} |
| 1873 |
|
| 1874 |
int sftp_auth_publickey_isvalid(const char *algo, |
| 1875 |
enum sftp_key_type_e *pubkey_type) { |
| 1876 |
register unsigned int i; |
| 1877 |
|
| 1878 |
if (algo == NULL) { |
| 1879 |
errno = EINVAL; |
| 1880 |
return -1; |
| 1881 |
} |
| 1882 |
|
| 1883 |
for (i = 0; publickeys[i].algo != NULL; i++) { |
| 1884 |
if (strcmp(publickeys[i].algo, algo) == 0) { |
| 1885 |
if (pubkey_type != NULL) { |
| 1886 |
*pubkey_type = publickeys[i].key_type; |
| 1887 |
} |
| 1888 |
|
| 1889 |
return TRUE; |
| 1890 |
} |
| 1891 |
} |
| 1892 |
|
| 1893 |
return FALSE; |
| 1894 |
} |
| 1895 |
|
| 1896 |
int sftp_auth_send_banner(const char *banner) { |
| 1897 |
struct ssh2_packet *pkt; |
| 1898 |
unsigned char *buf, *ptr; |
| 1899 |
uint32_t buflen, bufsz; |
| 1900 |
size_t banner_len; |
| 1901 |
int res; |
| 1902 |
|
| 1903 |
if (banner == NULL) { |
| 1904 |
errno = EINVAL; |
| 1905 |
return -1; |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
if (sftp_sess_state & SFTP_SESS_STATE_HAVE_AUTH) { |
| 1912 |
pr_trace_msg(trace_channel, 1, |
| 1913 |
"unable to send banner: client has authenticated"); |
| 1914 |
return 0; |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
banner_len = strlen(banner); |
| 1921 |
if (banner[banner_len-2] != '\r' || |
| 1922 |
banner[banner_len-1] != '\n') { |
| 1923 |
banner = pstrcat(auth_pool, banner, "\r\n", NULL); |
| 1924 |
banner_len = strlen(banner); |
| 1925 |
} |
| 1926 |
|
| 1927 |
pkt = sftp_ssh2_packet_create(auth_pool); |
| 1928 |
|
| 1929 |
buflen = bufsz = banner_len + 32; |
| 1930 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 1931 |
|
| 1932 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_BANNER); |
| 1933 |
sftp_msg_write_string(&buf, &buflen, banner); |
| 1934 |
|
| 1935 |
|
| 1936 |
sftp_msg_write_string(&buf, &buflen, ""); |
| 1937 |
|
| 1938 |
pkt->payload = ptr; |
| 1939 |
pkt->payload_len = (bufsz - buflen); |
| 1940 |
|
| 1941 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1942 |
"sending userauth banner"); |
| 1943 |
|
| 1944 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 1945 |
if (res < 0) { |
| 1946 |
destroy_pool(pkt->pool); |
| 1947 |
return -1; |
| 1948 |
} |
| 1949 |
|
| 1950 |
destroy_pool(pkt->pool); |
| 1951 |
return 0; |
| 1952 |
} |
| 1953 |
|
| 1954 |
int sftp_auth_handle(struct ssh2_packet *pkt) { |
| 1955 |
char *service = NULL; |
| 1956 |
int res; |
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
if (send_userauth_banner_file() < 0) { |
| 1962 |
return -1; |
| 1963 |
} |
| 1964 |
|
| 1965 |
if (pr_response_get_pool() == NULL) { |
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
pr_response_set_pool(session.pool); |
| 1970 |
} |
| 1971 |
|
| 1972 |
res = handle_userauth_req(pkt, &service); |
| 1973 |
if (res < 0) { |
| 1974 |
destroy_pool(pkt->pool); |
| 1975 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_ERROR, NULL); |
| 1976 |
} |
| 1977 |
|
| 1978 |
destroy_pool(pkt->pool); |
| 1979 |
return res; |
| 1980 |
} |
| 1981 |
|
| 1982 |
int sftp_auth_init(void) { |
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
if (auth_pool == NULL) { |
| 1994 |
unsigned int *max_logins; |
| 1995 |
|
| 1996 |
auth_pool = make_sub_pool(sftp_pool); |
| 1997 |
pr_pool_tag(auth_pool, "SSH2 Auth Pool"); |
| 1998 |
|
| 1999 |
max_logins = get_param_ptr(main_server->conf, "MaxLoginAttempts", FALSE); |
| 2000 |
if (max_logins != NULL) { |
| 2001 |
auth_attempts_max = *max_logins; |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
sftp_auth_set_success_handler(setup_env); |
| 2006 |
return 0; |
| 2007 |
} |
| 2008 |
|
| 2009 |
void sftp_auth_set_success_handler(int (*handler)(pool *, const char *)) { |
| 2010 |
success_handler = handler; |
| 2011 |
} |