1 /*- 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by ThinkSec AS and 6 * NAI Labs, the Security Research Division of Network Associates, Inc. 7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 8 * DARPA CHATS research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> 33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> 34 * 35 * Permission to use, copy, modify, and distribute this software for any 36 * purpose with or without fee is hereby granted, provided that the above 37 * copyright notice and this permission notice appear in all copies. 38 * 39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 46 */ 47 48 /* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */ 49 50 #include "includes.h" 51 /* 52 * NetBSD local changes 53 */ 54 __RCSID("$NetBSD: auth-pam.c,v 1.19 2019/04/20 17:16:40 christos Exp $"); 55 #define _LIB_PTHREAD_H 56 #undef USE_POSIX_THREADS /* Not yet */ 57 #define HAVE_SECURITY_PAM_APPL_H 58 #define HAVE_PAM_GETENVLIST 59 #define HAVE_PAM_PUTENV 60 #define sshpam_const const /* LinuxPAM, OpenPAM */ 61 #define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member) 62 #define mysig_t sig_t 63 void sshpam_password_change_required(int); 64 #define SSHD_PAM_SERVICE getprogname() 65 /* end NetBSD local changes */ 66 67 #include <sys/types.h> 68 #include <sys/socket.h> 69 #include <sys/stat.h> 70 #include <sys/wait.h> 71 72 #include <errno.h> 73 #include <signal.h> 74 #include <stdarg.h> 75 #include <string.h> 76 #include <unistd.h> 77 #include <pwd.h> 78 79 #ifdef USE_PAM 80 #if defined(HAVE_SECURITY_PAM_APPL_H) 81 #include <security/pam_appl.h> 82 #elif defined (HAVE_PAM_PAM_APPL_H) 83 #include <pam/pam_appl.h> 84 #endif 85 86 #if !defined(SSHD_PAM_SERVICE) 87 extern char *__progname; 88 # define SSHD_PAM_SERVICE __progname 89 #endif 90 91 #ifndef __NetBSD__ 92 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */ 93 #ifdef PAM_SUN_CODEBASE 94 # define sshpam_const /* Solaris, HP-UX, SunOS */ 95 #else 96 # define sshpam_const const /* LinuxPAM, OpenPAM, AIX */ 97 #endif 98 99 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */ 100 #ifdef PAM_SUN_CODEBASE 101 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member) 102 #else 103 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member) 104 #endif 105 #endif 106 107 #include "xmalloc.h" 108 #include "sshbuf.h" 109 #include "ssherr.h" 110 #include "hostfile.h" 111 #include "auth.h" 112 #include "auth-pam.h" 113 #include "canohost.h" 114 #include "log.h" 115 #include "msg.h" 116 #include "packet.h" 117 #include "misc.h" 118 #include "servconf.h" 119 #include "ssh2.h" 120 #include "auth-options.h" 121 #ifdef GSSAPI 122 #include "ssh-gss.h" 123 #endif 124 #include "monitor_wrap.h" 125 126 extern ServerOptions options; 127 extern struct sshbuf *loginmsg; 128 extern u_int utmp_len; 129 130 /* so we don't silently change behaviour */ 131 #ifdef USE_POSIX_THREADS 132 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK" 133 #endif 134 135 /* 136 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported 137 * and generally a bad idea. Use at own risk and do not expect support if 138 * this breaks. 139 */ 140 #ifdef UNSUPPORTED_POSIX_THREADS_HACK 141 #error "foo" 142 #include <pthread.h> 143 /* 144 * Avoid namespace clash when *not* using pthreads for systems *with* 145 * pthreads, which unconditionally define pthread_t via sys/types.h 146 * (e.g. Linux) 147 */ 148 typedef pthread_t sp_pthread_t; 149 #else 150 typedef pid_t sp_pthread_t; 151 #endif 152 153 struct pam_ctxt { 154 sp_pthread_t pam_thread; 155 int pam_psock; 156 int pam_csock; 157 int pam_done; 158 }; 159 160 static void sshpam_free_ctx(void *); 161 static struct pam_ctxt *cleanup_ctxt; 162 163 #ifndef UNSUPPORTED_POSIX_THREADS_HACK 164 /* 165 * Simulate threads with processes. 166 */ 167 168 static int sshpam_thread_status = -1; 169 static mysig_t sshpam_oldsig; 170 171 static void 172 sshpam_sigchld_handler(int sig) 173 { 174 signal(SIGCHLD, SIG_DFL); 175 if (cleanup_ctxt == NULL) 176 return; /* handler called after PAM cleanup, shouldn't happen */ 177 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) 178 <= 0) { 179 /* PAM thread has not exitted, privsep slave must have */ 180 kill(cleanup_ctxt->pam_thread, SIGTERM); 181 while (waitpid(cleanup_ctxt->pam_thread, 182 &sshpam_thread_status, 0) == -1) { 183 if (errno == EINTR) 184 continue; 185 return; 186 } 187 } 188 if (WIFSIGNALED(sshpam_thread_status) && 189 WTERMSIG(sshpam_thread_status) == SIGTERM) 190 return; /* terminated by pthread_cancel */ 191 if (!WIFEXITED(sshpam_thread_status)) 192 sigdie("PAM: authentication thread exited unexpectedly"); 193 if (WEXITSTATUS(sshpam_thread_status) != 0) 194 sigdie("PAM: authentication thread exited uncleanly"); 195 } 196 197 /* ARGSUSED */ 198 __dead static void 199 pthread_exit(void *value) 200 { 201 _exit(0); 202 } 203 204 /* ARGSUSED */ 205 static int 206 pthread_create(sp_pthread_t *thread, const void *attr, 207 void *(*thread_start)(void *), void *arg) 208 { 209 pid_t pid; 210 struct pam_ctxt *ctx = arg; 211 212 sshpam_thread_status = -1; 213 switch ((pid = fork())) { 214 case -1: 215 error("fork(): %s", strerror(errno)); 216 return (-1); 217 case 0: 218 close(ctx->pam_psock); 219 ctx->pam_psock = -1; 220 thread_start(arg); 221 _exit(1); 222 default: 223 *thread = pid; 224 close(ctx->pam_csock); 225 ctx->pam_csock = -1; 226 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); 227 return (0); 228 } 229 } 230 231 static int 232 pthread_cancel(sp_pthread_t thread) 233 { 234 signal(SIGCHLD, sshpam_oldsig); 235 return (kill(thread, SIGTERM)); 236 } 237 238 /* ARGSUSED */ 239 static int 240 pthread_join(sp_pthread_t thread, void **value) 241 { 242 int status; 243 244 if (sshpam_thread_status != -1) 245 return (sshpam_thread_status); 246 signal(SIGCHLD, sshpam_oldsig); 247 while (waitpid(thread, &status, 0) == -1) { 248 if (errno == EINTR) 249 continue; 250 fatal("%s: waitpid: %s", __func__, strerror(errno)); 251 } 252 return (status); 253 } 254 #endif 255 256 257 static pam_handle_t *sshpam_handle = NULL; 258 static int sshpam_err = 0; 259 static int sshpam_authenticated = 0; 260 static int sshpam_session_open = 0; 261 static int sshpam_cred_established = 0; 262 static int sshpam_account_status = -1; 263 static int sshpam_maxtries_reached = 0; 264 static char **sshpam_env = NULL; 265 static Authctxt *sshpam_authctxt = NULL; 266 static const char *sshpam_password = NULL; 267 static char *sshpam_rhost = NULL; 268 static char *sshpam_laddr = NULL; 269 static char *sshpam_conninfo = NULL; 270 271 /* Some PAM implementations don't implement this */ 272 #ifndef HAVE_PAM_GETENVLIST 273 static char ** 274 pam_getenvlist(pam_handle_t *pamh) 275 { 276 /* 277 * XXX - If necessary, we can still support envrionment passing 278 * for platforms without pam_getenvlist by searching for known 279 * env vars (e.g. KRB5CCNAME) from the PAM environment. 280 */ 281 return NULL; 282 } 283 #endif 284 285 /* 286 * Some platforms, notably Solaris, do not enforce password complexity 287 * rules during pam_chauthtok() if the real uid of the calling process 288 * is 0, on the assumption that it's being called by "passwd" run by root. 289 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do 290 * the right thing. 291 */ 292 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID 293 static int 294 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) 295 { 296 int result; 297 298 if (sshpam_authctxt == NULL) 299 fatal("PAM: sshpam_authctxt not initialized"); 300 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1) 301 fatal("%s: setreuid failed: %s", __func__, strerror(errno)); 302 result = pam_chauthtok(pamh, flags); 303 if (setreuid(0, -1) == -1) 304 fatal("%s: setreuid failed: %s", __func__, strerror(errno)); 305 return result; 306 } 307 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) 308 #endif 309 310 void 311 sshpam_password_change_required(int reqd) 312 { 313 extern struct sshauthopt *auth_opts; 314 static int saved_port, saved_agent, saved_x11; 315 316 debug3("%s %d", __func__, reqd); 317 if (sshpam_authctxt == NULL) 318 fatal("%s: PAM authctxt not initialized", __func__); 319 sshpam_authctxt->force_pwchange = reqd; 320 if (reqd) { 321 saved_port = auth_opts->permit_port_forwarding_flag; 322 saved_agent = auth_opts->permit_agent_forwarding_flag; 323 saved_x11 = auth_opts->permit_x11_forwarding_flag; 324 auth_opts->permit_port_forwarding_flag = 0; 325 auth_opts->permit_agent_forwarding_flag = 0; 326 auth_opts->permit_x11_forwarding_flag = 0; 327 } else { 328 if (saved_port) 329 auth_opts->permit_port_forwarding_flag = saved_port; 330 if (saved_agent) 331 auth_opts->permit_agent_forwarding_flag = saved_agent; 332 if (saved_x11) 333 auth_opts->permit_x11_forwarding_flag = saved_x11; 334 } 335 } 336 337 /* Import regular and PAM environment from subprocess */ 338 static void 339 import_environments(struct sshbuf *b) 340 { 341 char *env; 342 u_int n, i, num_env; 343 int r; 344 345 debug3("PAM: %s entering", __func__); 346 347 #ifndef UNSUPPORTED_POSIX_THREADS_HACK 348 /* Import variables set by do_pam_account */ 349 if ((r = sshbuf_get_u32(b, &n)) != 0) 350 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 351 if (n > INT_MAX) 352 fatal("%s: invalid PAM account status %u", __func__, n); 353 sshpam_account_status = (int)n; 354 if ((r = sshbuf_get_u32(b, &n)) != 0) 355 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 356 sshpam_password_change_required(n != 0); 357 358 /* Import environment from subprocess */ 359 if ((r = sshbuf_get_u32(b, &num_env)) != 0) 360 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 361 if (num_env > 1024) 362 fatal("%s: received %u environment variables, expected <= 1024", 363 __func__, num_env); 364 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env)); 365 debug3("PAM: num env strings %d", num_env); 366 for(i = 0; i < num_env; i++) { 367 if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0) 368 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 369 } 370 sshpam_env[num_env] = NULL; 371 372 /* Import PAM environment from subprocess */ 373 if ((r = sshbuf_get_u32(b, &num_env)) != 0) 374 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 375 debug("PAM: num PAM env strings %d", num_env); 376 for (i = 0; i < num_env; i++) { 377 if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0) 378 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 379 #ifdef HAVE_PAM_PUTENV 380 /* Errors are not fatal here */ 381 if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { 382 error("PAM: pam_putenv: %s", 383 pam_strerror(sshpam_handle, r)); 384 } 385 #endif 386 /* XXX leak env? */ 387 } 388 #endif 389 } 390 391 /* 392 * Conversation function for authentication thread. 393 */ 394 static int 395 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg, 396 struct pam_response **resp, void *data) 397 { 398 struct sshbuf *buffer; 399 struct pam_ctxt *ctxt; 400 struct pam_response *reply; 401 int r, i; 402 u_char status; 403 404 debug3("PAM: %s entering, %d messages", __func__, n); 405 *resp = NULL; 406 407 if (data == NULL) { 408 error("PAM: conversation function passed a null context"); 409 return (PAM_CONV_ERR); 410 } 411 ctxt = data; 412 if (n <= 0 || n > PAM_MAX_NUM_MSG) 413 return (PAM_CONV_ERR); 414 415 if ((reply = calloc(n, sizeof(*reply))) == NULL) 416 return PAM_CONV_ERR; 417 if ((buffer = sshbuf_new()) == NULL) { 418 free(reply); 419 return PAM_CONV_ERR; 420 } 421 422 for (i = 0; i < n; ++i) { 423 switch (PAM_MSG_MEMBER(msg, i, msg_style)) { 424 case PAM_PROMPT_ECHO_OFF: 425 case PAM_PROMPT_ECHO_ON: 426 if ((r = sshbuf_put_cstring(buffer, 427 PAM_MSG_MEMBER(msg, i, msg))) != 0) 428 fatal("%s: buffer error: %s", 429 __func__, ssh_err(r)); 430 if (ssh_msg_send(ctxt->pam_csock, 431 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1) 432 goto fail; 433 434 if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1) 435 goto fail; 436 if ((r = sshbuf_get_u8(buffer, &status)) != 0) 437 fatal("%s: buffer error: %s", 438 __func__, ssh_err(r)); 439 if (status != PAM_AUTHTOK) 440 goto fail; 441 if ((r = sshbuf_get_cstring(buffer, 442 &reply[i].resp, NULL)) != 0) 443 fatal("%s: buffer error: %s", 444 __func__, ssh_err(r)); 445 break; 446 case PAM_ERROR_MSG: 447 case PAM_TEXT_INFO: 448 if ((r = sshbuf_put_cstring(buffer, 449 PAM_MSG_MEMBER(msg, i, msg))) != 0) 450 fatal("%s: buffer error: %s", 451 __func__, ssh_err(r)); 452 if (ssh_msg_send(ctxt->pam_csock, 453 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1) 454 goto fail; 455 break; 456 default: 457 goto fail; 458 } 459 sshbuf_reset(buffer); 460 } 461 sshbuf_free(buffer); 462 *resp = reply; 463 return (PAM_SUCCESS); 464 465 fail: 466 for(i = 0; i < n; i++) { 467 free(reply[i].resp); 468 } 469 free(reply); 470 sshbuf_free(buffer); 471 return (PAM_CONV_ERR); 472 } 473 474 /* 475 * Authentication thread. 476 */ 477 static void * 478 sshpam_thread(void *ctxtp) 479 { 480 struct pam_ctxt *ctxt = ctxtp; 481 struct sshbuf *buffer = NULL; 482 struct pam_conv sshpam_conv; 483 int r, flags = (options.permit_empty_passwd == 0 ? 484 PAM_DISALLOW_NULL_AUTHTOK : 0); 485 #ifndef UNSUPPORTED_POSIX_THREADS_HACK 486 extern char **environ; 487 char **env_from_pam; 488 u_int i; 489 const char *pam_user; 490 const char **ptr_pam_user = &pam_user; 491 char *tz = getenv("TZ"); 492 493 sshpam_err = pam_get_item(sshpam_handle, PAM_USER, 494 (sshpam_const void **)ptr_pam_user); 495 if (sshpam_err != PAM_SUCCESS) 496 goto auth_fail; 497 498 environ[0] = NULL; 499 if (tz != NULL) 500 if (setenv("TZ", tz, 1) == -1) 501 error("PAM: could not set TZ environment: %s", 502 strerror(errno)); 503 504 if (sshpam_authctxt != NULL) { 505 setproctitle("%s [pam]", 506 sshpam_authctxt->valid ? pam_user : "unknown"); 507 } 508 #endif 509 510 sshpam_conv.conv = sshpam_thread_conv; 511 sshpam_conv.appdata_ptr = ctxt; 512 513 if (sshpam_authctxt == NULL) 514 fatal("%s: PAM authctxt not initialized", __func__); 515 516 if ((buffer = sshbuf_new()) == NULL) 517 fatal("%s: sshbuf_new failed", __func__); 518 519 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 520 (const void *)&sshpam_conv); 521 if (sshpam_err != PAM_SUCCESS) 522 goto auth_fail; 523 sshpam_err = pam_authenticate(sshpam_handle, flags); 524 if (sshpam_err == PAM_MAXTRIES) 525 sshpam_set_maxtries_reached(1); 526 if (sshpam_err != PAM_SUCCESS) 527 goto auth_fail; 528 529 if (!do_pam_account()) { 530 sshpam_err = PAM_ACCT_EXPIRED; 531 goto auth_fail; 532 } 533 if (sshpam_authctxt->force_pwchange) { 534 sshpam_err = pam_chauthtok(sshpam_handle, 535 PAM_CHANGE_EXPIRED_AUTHTOK); 536 if (sshpam_err != PAM_SUCCESS) 537 goto auth_fail; 538 sshpam_password_change_required(0); 539 } 540 541 if ((r = sshbuf_put_cstring(buffer, "OK")) != 0) 542 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 543 544 #ifndef UNSUPPORTED_POSIX_THREADS_HACK 545 /* Export variables set by do_pam_account */ 546 if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 || 547 (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0) 548 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 549 550 /* Export any environment strings set in child */ 551 for (i = 0; environ[i] != NULL; i++) { 552 /* Count */ 553 if (i > INT_MAX) 554 fatal("%s: too many enviornment strings", __func__); 555 } 556 if ((r = sshbuf_put_u32(buffer, i)) != 0) 557 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 558 for (i = 0; environ[i] != NULL; i++) { 559 if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0) 560 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 561 } 562 /* Export any environment strings set by PAM in child */ 563 env_from_pam = pam_getenvlist(sshpam_handle); 564 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) { 565 /* Count */ 566 if (i > INT_MAX) 567 fatal("%s: too many PAM enviornment strings", __func__); 568 } 569 if ((r = sshbuf_put_u32(buffer, i)) != 0) 570 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 571 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) { 572 if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0) 573 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 574 } 575 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */ 576 577 /* XXX - can't do much about an error here */ 578 ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer); 579 sshbuf_free(buffer); 580 pthread_exit(NULL); 581 582 auth_fail: 583 if ((r = sshbuf_put_cstring(buffer, 584 pam_strerror(sshpam_handle, sshpam_err))) != 0) 585 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 586 /* XXX - can't do much about an error here */ 587 if (sshpam_err == PAM_ACCT_EXPIRED) 588 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer); 589 else if (sshpam_maxtries_reached) 590 ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer); 591 else 592 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer); 593 sshbuf_free(buffer); 594 pthread_exit(NULL); 595 596 return (NULL); /* Avoid warning for non-pthread case */ 597 } 598 599 void 600 sshpam_thread_cleanup(void) 601 { 602 struct pam_ctxt *ctxt = cleanup_ctxt; 603 604 debug3("PAM: %s entering", __func__); 605 if (ctxt != NULL && ctxt->pam_thread != 0) { 606 pthread_cancel(ctxt->pam_thread); 607 pthread_join(ctxt->pam_thread, NULL); 608 close(ctxt->pam_psock); 609 close(ctxt->pam_csock); 610 memset(ctxt, 0, sizeof(*ctxt)); 611 cleanup_ctxt = NULL; 612 } 613 } 614 615 static int 616 sshpam_null_conv(int n, sshpam_const struct pam_message **msg, 617 struct pam_response **resp, void *data) 618 { 619 debug3("PAM: %s entering, %d messages", __func__, n); 620 return (PAM_CONV_ERR); 621 } 622 623 static struct pam_conv null_conv = { sshpam_null_conv, NULL }; 624 625 static int 626 sshpam_store_conv(int n, sshpam_const struct pam_message **msg, 627 struct pam_response **resp, void *data) 628 { 629 struct pam_response *reply; 630 int r, i; 631 632 debug3("PAM: %s called with %d messages", __func__, n); 633 *resp = NULL; 634 635 if (n <= 0 || n > PAM_MAX_NUM_MSG) 636 return (PAM_CONV_ERR); 637 638 if ((reply = calloc(n, sizeof(*reply))) == NULL) 639 return (PAM_CONV_ERR); 640 641 for (i = 0; i < n; ++i) { 642 switch (PAM_MSG_MEMBER(msg, i, msg_style)) { 643 case PAM_ERROR_MSG: 644 case PAM_TEXT_INFO: 645 if ((r = sshbuf_putf(loginmsg, "%s\n", 646 PAM_MSG_MEMBER(msg, i, msg))) != 0) 647 fatal("%s: buffer error: %s", 648 __func__, ssh_err(r)); 649 reply[i].resp_retcode = PAM_SUCCESS; 650 break; 651 default: 652 goto fail; 653 } 654 } 655 *resp = reply; 656 return (PAM_SUCCESS); 657 658 fail: 659 for(i = 0; i < n; i++) { 660 free(reply[i].resp); 661 } 662 free(reply); 663 return (PAM_CONV_ERR); 664 } 665 666 static struct pam_conv store_conv = { sshpam_store_conv, NULL }; 667 668 void 669 sshpam_cleanup(void) 670 { 671 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor())) 672 return; 673 debug("PAM: cleanup"); 674 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); 675 if (sshpam_session_open) { 676 debug("PAM: closing session"); 677 pam_close_session(sshpam_handle, PAM_SILENT); 678 sshpam_session_open = 0; 679 } 680 if (sshpam_cred_established) { 681 debug("PAM: deleting credentials"); 682 pam_setcred(sshpam_handle, PAM_DELETE_CRED); 683 sshpam_cred_established = 0; 684 } 685 sshpam_authenticated = 0; 686 pam_end(sshpam_handle, sshpam_err); 687 sshpam_handle = NULL; 688 } 689 690 static int 691 sshpam_init(struct ssh *ssh, Authctxt *authctxt) 692 { 693 const char *pam_user, *user = authctxt->user; 694 const char **ptr_pam_user = &pam_user; 695 696 if (sshpam_handle == NULL) { 697 if (ssh == NULL) { 698 fatal("%s: called initially with no " 699 "packet context", __func__); 700 } 701 } if (sshpam_handle != NULL) { 702 /* We already have a PAM context; check if the user matches */ 703 sshpam_err = pam_get_item(sshpam_handle, 704 PAM_USER, (sshpam_const void **)ptr_pam_user); 705 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) 706 return (0); 707 pam_end(sshpam_handle, sshpam_err); 708 sshpam_handle = NULL; 709 } 710 debug("PAM: initializing for \"%s\"", user); 711 sshpam_err = 712 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); 713 sshpam_authctxt = authctxt; 714 715 if (sshpam_err != PAM_SUCCESS) { 716 pam_end(sshpam_handle, sshpam_err); 717 sshpam_handle = NULL; 718 return (-1); 719 } 720 721 if (ssh != NULL && sshpam_rhost == NULL) { 722 /* 723 * We need to cache these as we don't have packet context 724 * during the kbdint flow. 725 */ 726 sshpam_rhost = xstrdup(auth_get_canonical_hostname(ssh, 727 options.use_dns)); 728 sshpam_laddr = get_local_ipaddr( 729 ssh_packet_get_connection_in(ssh)); 730 xasprintf(&sshpam_conninfo, "SSH_CONNECTION=%.50s %d %.50s %d", 731 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 732 sshpam_laddr, ssh_local_port(ssh)); 733 } 734 if (sshpam_rhost != NULL) { 735 debug("PAM: setting PAM_RHOST to \"%s\"", sshpam_rhost); 736 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, 737 sshpam_rhost); 738 if (sshpam_err != PAM_SUCCESS) { 739 pam_end(sshpam_handle, sshpam_err); 740 sshpam_handle = NULL; 741 return (-1); 742 } 743 /* Put SSH_CONNECTION in the PAM environment too */ 744 pam_putenv(sshpam_handle, sshpam_conninfo); 745 } 746 747 #ifdef PAM_TTY_KLUDGE 748 /* 749 * Some silly PAM modules (e.g. pam_time) require a TTY to operate. 750 * sshd doesn't set the tty until too late in the auth process and 751 * may not even set one (for tty-less connections) 752 */ 753 debug("PAM: setting PAM_TTY to \"ssh\""); 754 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); 755 if (sshpam_err != PAM_SUCCESS) { 756 pam_end(sshpam_handle, sshpam_err); 757 sshpam_handle = NULL; 758 return (-1); 759 } 760 #endif 761 return (0); 762 } 763 764 static void 765 expose_authinfo(const char *caller) 766 { 767 char *auth_info; 768 769 /* 770 * Expose authentication information to PAM. 771 * The environment variable is versioned. Please increment the 772 * version suffix if the format of session_info changes. 773 */ 774 if (sshpam_authctxt->session_info == NULL) 775 auth_info = xstrdup(""); 776 else if ((auth_info = sshbuf_dup_string( 777 sshpam_authctxt->session_info)) == NULL) 778 fatal("%s: sshbuf_dup_string failed", __func__); 779 780 debug2("%s: auth information in SSH_AUTH_INFO_0", caller); 781 do_pam_putenv("SSH_AUTH_INFO_0", auth_info); 782 free(auth_info); 783 } 784 785 static void * 786 sshpam_init_ctx(Authctxt *authctxt) 787 { 788 struct pam_ctxt *ctxt; 789 int socks[2]; 790 791 debug3("PAM: %s entering", __func__); 792 /* 793 * Refuse to start if we don't have PAM enabled or do_pam_account 794 * has previously failed. 795 */ 796 if (!options.use_pam || sshpam_account_status == 0) 797 return NULL; 798 799 /* Initialize PAM */ 800 if (sshpam_init(NULL, authctxt) == -1) { 801 error("PAM: initialization failed"); 802 return (NULL); 803 } 804 805 expose_authinfo(__func__); 806 ctxt = xcalloc(1, sizeof *ctxt); 807 808 /* Start the authentication thread */ 809 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { 810 error("PAM: failed create sockets: %s", strerror(errno)); 811 free(ctxt); 812 return (NULL); 813 } 814 ctxt->pam_psock = socks[0]; 815 ctxt->pam_csock = socks[1]; 816 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { 817 error("PAM: failed to start authentication thread: %s", 818 strerror(errno)); 819 close(socks[0]); 820 close(socks[1]); 821 free(ctxt); 822 return (NULL); 823 } 824 cleanup_ctxt = ctxt; 825 return (ctxt); 826 } 827 828 static int 829 sshpam_query(void *ctx, char **name, char **info, 830 u_int *num, char ***prompts, u_int **echo_on) 831 { 832 struct sshbuf *buffer; 833 struct pam_ctxt *ctxt = ctx; 834 size_t plen; 835 u_char type; 836 char *msg; 837 size_t len, mlen; 838 int r; 839 840 debug3("PAM: %s entering", __func__); 841 if ((buffer = sshbuf_new()) == NULL) 842 fatal("%s: sshbuf_new failed", __func__); 843 *name = xstrdup(""); 844 *info = xstrdup(""); 845 *prompts = xmalloc(sizeof(char *)); 846 **prompts = NULL; 847 plen = 0; 848 *echo_on = xmalloc(sizeof(u_int)); 849 while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) { 850 if ((r = sshbuf_get_u8(buffer, &type)) != 0 || 851 (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0) 852 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 853 switch (type) { 854 case PAM_PROMPT_ECHO_ON: 855 case PAM_PROMPT_ECHO_OFF: 856 *num = 1; 857 len = plen + mlen + 1; 858 **prompts = xreallocarray(**prompts, 1, len); 859 strlcpy(**prompts + plen, msg, len - plen); 860 plen += mlen; 861 **echo_on = (type == PAM_PROMPT_ECHO_ON); 862 free(msg); 863 return (0); 864 case PAM_ERROR_MSG: 865 case PAM_TEXT_INFO: 866 /* accumulate messages */ 867 len = plen + mlen + 2; 868 **prompts = xreallocarray(**prompts, 1, len); 869 strlcpy(**prompts + plen, msg, len - plen); 870 plen += mlen; 871 strlcat(**prompts + plen, "\n", len - plen); 872 plen++; 873 free(msg); 874 break; 875 case PAM_ACCT_EXPIRED: 876 case PAM_MAXTRIES: 877 if (type == PAM_ACCT_EXPIRED) 878 sshpam_account_status = 0; 879 if (type == PAM_MAXTRIES) 880 sshpam_set_maxtries_reached(1); 881 /* FALLTHROUGH */ 882 case PAM_AUTH_ERR: 883 debug3("PAM: %s", pam_strerror(sshpam_handle, type)); 884 if (**prompts != NULL && strlen(**prompts) != 0) { 885 *info = **prompts; 886 **prompts = NULL; 887 *num = 0; 888 **echo_on = 0; 889 ctxt->pam_done = -1; 890 free(msg); 891 return 0; 892 } 893 /* FALLTHROUGH */ 894 case PAM_SUCCESS: 895 if (**prompts != NULL) { 896 /* drain any accumulated messages */ 897 debug("PAM: %s", **prompts); 898 if ((r = sshbuf_put(loginmsg, **prompts, 899 strlen(**prompts))) != 0) 900 fatal("%s: buffer error: %s", 901 __func__, ssh_err(r)); 902 free(**prompts); 903 **prompts = NULL; 904 } 905 if (type == PAM_SUCCESS) { 906 if (!sshpam_authctxt->valid || 907 (sshpam_authctxt->pw->pw_uid == 0 && 908 options.permit_root_login != PERMIT_YES)) 909 fatal("Internal error: PAM auth " 910 "succeeded when it should have " 911 "failed"); 912 import_environments(buffer); 913 *num = 0; 914 **echo_on = 0; 915 ctxt->pam_done = 1; 916 free(msg); 917 return (0); 918 } 919 error("PAM: %s for %s%.100s from %.100s", msg, 920 sshpam_authctxt->valid ? "" : "illegal user ", 921 sshpam_authctxt->user, sshpam_rhost); 922 /* FALLTHROUGH */ 923 default: 924 *num = 0; 925 **echo_on = 0; 926 free(msg); 927 ctxt->pam_done = -1; 928 return (-1); 929 } 930 } 931 return (-1); 932 } 933 934 /* 935 * Returns a junk password of identical length to that the user supplied. 936 * Used to mitigate timing attacks against crypt(3)/PAM stacks that 937 * vary processing time in proportion to password length. 938 */ 939 static char * 940 fake_password(const char *wire_password) 941 { 942 const char junk[] = "\b\n\r\177INCORRECT"; 943 char *ret = NULL; 944 size_t i, l = wire_password != NULL ? strlen(wire_password) : 0; 945 946 if (l >= INT_MAX) 947 fatal("%s: password length too long: %zu", __func__, l); 948 949 ret = malloc(l + 1); 950 if (ret == NULL) 951 return NULL; 952 for (i = 0; i < l; i++) 953 ret[i] = junk[i % (sizeof(junk) - 1)]; 954 ret[i] = '\0'; 955 return ret; 956 } 957 958 /* XXX - see also comment in auth-chall.c:verify_response */ 959 static int 960 sshpam_respond(void *ctx, u_int num, char **resp) 961 { 962 struct sshbuf *buffer; 963 struct pam_ctxt *ctxt = ctx; 964 char *fake; 965 int r; 966 967 debug2("PAM: %s entering, %u responses", __func__, num); 968 switch (ctxt->pam_done) { 969 case 1: 970 sshpam_authenticated = 1; 971 return (0); 972 case 0: 973 break; 974 default: 975 return (-1); 976 } 977 if (num != 1) { 978 error("PAM: expected one response, got %u", num); 979 return (-1); 980 } 981 if ((buffer = sshbuf_new()) == NULL) 982 fatal("%s: sshbuf_new failed", __func__); 983 if (sshpam_authctxt->valid && 984 (sshpam_authctxt->pw->pw_uid != 0 || 985 options.permit_root_login == PERMIT_YES)) { 986 if ((r = sshbuf_put_cstring(buffer, *resp)) != 0) 987 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 988 } else { 989 fake = fake_password(*resp); 990 if ((r = sshbuf_put_cstring(buffer, fake)) != 0) 991 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 992 free(fake); 993 } 994 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) { 995 sshbuf_free(buffer); 996 return (-1); 997 } 998 sshbuf_free(buffer); 999 return (1); 1000 } 1001 1002 static void 1003 sshpam_free_ctx(void *ctxtp) 1004 { 1005 struct pam_ctxt *ctxt = ctxtp; 1006 1007 debug3("PAM: %s entering", __func__); 1008 sshpam_thread_cleanup(); 1009 free(ctxt); 1010 /* 1011 * We don't call sshpam_cleanup() here because we may need the PAM 1012 * handle at a later stage, e.g. when setting up a session. It's 1013 * still on the cleanup list, so pam_end() *will* be called before 1014 * the server process terminates. 1015 */ 1016 } 1017 1018 KbdintDevice sshpam_device = { 1019 "pam", 1020 sshpam_init_ctx, 1021 sshpam_query, 1022 sshpam_respond, 1023 sshpam_free_ctx 1024 }; 1025 1026 KbdintDevice mm_sshpam_device = { 1027 "pam", 1028 mm_sshpam_init_ctx, 1029 mm_sshpam_query, 1030 mm_sshpam_respond, 1031 mm_sshpam_free_ctx 1032 }; 1033 1034 /* 1035 * This replaces auth-pam.c 1036 */ 1037 void 1038 start_pam(struct ssh *ssh) 1039 { 1040 Authctxt *authctxt = ssh->authctxt; 1041 1042 if (!options.use_pam) 1043 fatal("PAM: initialisation requested when UsePAM=no"); 1044 1045 if (sshpam_init(ssh, authctxt) == -1) 1046 fatal("PAM: initialisation failed"); 1047 } 1048 1049 void 1050 finish_pam(void) 1051 { 1052 sshpam_cleanup(); 1053 } 1054 1055 1056 u_int 1057 do_pam_account(void) 1058 { 1059 debug("%s: called", __func__); 1060 if (sshpam_account_status != -1) 1061 return (sshpam_account_status); 1062 1063 expose_authinfo(__func__); 1064 1065 sshpam_err = pam_acct_mgmt(sshpam_handle, 0); 1066 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, 1067 pam_strerror(sshpam_handle, sshpam_err)); 1068 1069 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { 1070 sshpam_account_status = 0; 1071 return (sshpam_account_status); 1072 } 1073 1074 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) 1075 sshpam_password_change_required(1); 1076 1077 sshpam_account_status = 1; 1078 return (sshpam_account_status); 1079 } 1080 1081 void 1082 do_pam_setcred(int init) 1083 { 1084 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 1085 (const void *)&store_conv); 1086 if (sshpam_err != PAM_SUCCESS) 1087 fatal("PAM: failed to set PAM_CONV: %s", 1088 pam_strerror(sshpam_handle, sshpam_err)); 1089 if (init) { 1090 debug("PAM: establishing credentials"); 1091 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); 1092 } else { 1093 debug("PAM: reinitializing credentials"); 1094 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); 1095 } 1096 if (sshpam_err == PAM_SUCCESS) { 1097 sshpam_cred_established = 1; 1098 return; 1099 } 1100 if (sshpam_authenticated) 1101 fatal("PAM: pam_setcred(): %s", 1102 pam_strerror(sshpam_handle, sshpam_err)); 1103 else 1104 debug("PAM: pam_setcred(): %s", 1105 pam_strerror(sshpam_handle, sshpam_err)); 1106 } 1107 1108 static int 1109 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg, 1110 struct pam_response **resp, void *data) 1111 { 1112 char input[PAM_MAX_MSG_SIZE]; 1113 struct pam_response *reply; 1114 int i; 1115 1116 debug3("PAM: %s called with %d messages", __func__, n); 1117 1118 *resp = NULL; 1119 1120 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) 1121 return (PAM_CONV_ERR); 1122 1123 if ((reply = calloc(n, sizeof(*reply))) == NULL) 1124 return (PAM_CONV_ERR); 1125 1126 for (i = 0; i < n; ++i) { 1127 switch (PAM_MSG_MEMBER(msg, i, msg_style)) { 1128 case PAM_PROMPT_ECHO_OFF: 1129 reply[i].resp = 1130 read_passphrase(PAM_MSG_MEMBER(msg, i, msg), 1131 RP_ALLOW_STDIN); 1132 reply[i].resp_retcode = PAM_SUCCESS; 1133 break; 1134 case PAM_PROMPT_ECHO_ON: 1135 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); 1136 if (fgets(input, sizeof input, stdin) == NULL) 1137 input[0] = '\0'; 1138 if ((reply[i].resp = strdup(input)) == NULL) 1139 goto fail; 1140 reply[i].resp_retcode = PAM_SUCCESS; 1141 break; 1142 case PAM_ERROR_MSG: 1143 case PAM_TEXT_INFO: 1144 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); 1145 reply[i].resp_retcode = PAM_SUCCESS; 1146 break; 1147 default: 1148 goto fail; 1149 } 1150 } 1151 *resp = reply; 1152 return (PAM_SUCCESS); 1153 1154 fail: 1155 for(i = 0; i < n; i++) { 1156 free(reply[i].resp); 1157 } 1158 free(reply); 1159 return (PAM_CONV_ERR); 1160 } 1161 1162 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; 1163 1164 /* 1165 * XXX this should be done in the authentication phase, but ssh1 doesn't 1166 * support that 1167 */ 1168 void 1169 do_pam_chauthtok(void) 1170 { 1171 if (use_privsep) 1172 fatal("Password expired (unable to change with privsep)"); 1173 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 1174 (const void *)&tty_conv); 1175 if (sshpam_err != PAM_SUCCESS) 1176 fatal("PAM: failed to set PAM_CONV: %s", 1177 pam_strerror(sshpam_handle, sshpam_err)); 1178 debug("PAM: changing password"); 1179 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); 1180 if (sshpam_err != PAM_SUCCESS) 1181 fatal("PAM: pam_chauthtok(): %s", 1182 pam_strerror(sshpam_handle, sshpam_err)); 1183 } 1184 1185 void 1186 do_pam_session(struct ssh *ssh) 1187 { 1188 debug3("PAM: opening session"); 1189 1190 expose_authinfo(__func__); 1191 1192 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 1193 (const void *)&store_conv); 1194 if (sshpam_err != PAM_SUCCESS) 1195 fatal("PAM: failed to set PAM_CONV: %s", 1196 pam_strerror(sshpam_handle, sshpam_err)); 1197 sshpam_err = pam_open_session(sshpam_handle, 0); 1198 if (sshpam_err == PAM_SUCCESS) 1199 sshpam_session_open = 1; 1200 else { 1201 sshpam_session_open = 0; 1202 auth_restrict_session(ssh); 1203 error("PAM: pam_open_session(): %s", 1204 pam_strerror(sshpam_handle, sshpam_err)); 1205 } 1206 1207 } 1208 1209 int 1210 is_pam_session_open(void) 1211 { 1212 return sshpam_session_open; 1213 } 1214 1215 /* 1216 * Set a PAM environment string. We need to do this so that the session 1217 * modules can handle things like Kerberos/GSI credentials that appear 1218 * during the ssh authentication process. 1219 */ 1220 int 1221 do_pam_putenv(const char *name, char *value) 1222 { 1223 int ret = 1; 1224 #ifdef HAVE_PAM_PUTENV 1225 char *compound; 1226 size_t len; 1227 1228 len = strlen(name) + strlen(value) + 2; 1229 compound = xmalloc(len); 1230 1231 snprintf(compound, len, "%s=%s", name, value); 1232 ret = pam_putenv(sshpam_handle, compound); 1233 free(compound); 1234 #endif 1235 1236 return (ret); 1237 } 1238 1239 char ** 1240 fetch_pam_child_environment(void) 1241 { 1242 return sshpam_env; 1243 } 1244 1245 char ** 1246 fetch_pam_environment(void) 1247 { 1248 return (pam_getenvlist(sshpam_handle)); 1249 } 1250 1251 void 1252 free_pam_environment(char **env) 1253 { 1254 char **envp; 1255 1256 if (env == NULL) 1257 return; 1258 1259 for (envp = env; *envp; envp++) 1260 free(*envp); 1261 free(env); 1262 } 1263 1264 /* 1265 * "Blind" conversation function for password authentication. Assumes that 1266 * echo-off prompts are for the password and stores messages for later 1267 * display. 1268 */ 1269 static int 1270 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg, 1271 struct pam_response **resp, void *data) 1272 { 1273 struct pam_response *reply; 1274 int r, i; 1275 size_t len; 1276 1277 debug3("PAM: %s called with %d messages", __func__, n); 1278 1279 *resp = NULL; 1280 1281 if (n <= 0 || n > PAM_MAX_NUM_MSG) 1282 return (PAM_CONV_ERR); 1283 1284 if ((reply = calloc(n, sizeof(*reply))) == NULL) 1285 return (PAM_CONV_ERR); 1286 1287 for (i = 0; i < n; ++i) { 1288 switch (PAM_MSG_MEMBER(msg, i, msg_style)) { 1289 case PAM_PROMPT_ECHO_OFF: 1290 if (sshpam_password == NULL) 1291 goto fail; 1292 if ((reply[i].resp = strdup(sshpam_password)) == NULL) 1293 goto fail; 1294 reply[i].resp_retcode = PAM_SUCCESS; 1295 break; 1296 case PAM_ERROR_MSG: 1297 case PAM_TEXT_INFO: 1298 len = strlen(PAM_MSG_MEMBER(msg, i, msg)); 1299 if (len > 0) { 1300 if ((r = sshbuf_putf(loginmsg, "%s\n", 1301 PAM_MSG_MEMBER(msg, i, msg))) != 0) 1302 fatal("%s: buffer error: %s", 1303 __func__, ssh_err(r)); 1304 } 1305 if ((reply[i].resp = strdup("")) == NULL) 1306 goto fail; 1307 reply[i].resp_retcode = PAM_SUCCESS; 1308 break; 1309 default: 1310 goto fail; 1311 } 1312 } 1313 *resp = reply; 1314 return (PAM_SUCCESS); 1315 1316 fail: 1317 for(i = 0; i < n; i++) { 1318 free(reply[i].resp); 1319 } 1320 free(reply); 1321 return (PAM_CONV_ERR); 1322 } 1323 1324 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; 1325 1326 /* 1327 * Attempt password authentication via PAM 1328 */ 1329 int 1330 sshpam_auth_passwd(Authctxt *authctxt, const char *password) 1331 { 1332 int flags = (options.permit_empty_passwd == 0 ? 1333 PAM_DISALLOW_NULL_AUTHTOK : 0); 1334 char *fake = NULL; 1335 1336 if (!options.use_pam || sshpam_handle == NULL) 1337 fatal("PAM: %s called when PAM disabled or failed to " 1338 "initialise.", __func__); 1339 1340 sshpam_password = password; 1341 sshpam_authctxt = authctxt; 1342 1343 /* 1344 * If the user logging in is invalid, or is root but is not permitted 1345 * by PermitRootLogin, use an invalid password to prevent leaking 1346 * information via timing (eg if the PAM config has a delay on fail). 1347 */ 1348 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && 1349 options.permit_root_login != PERMIT_YES)) 1350 sshpam_password = fake = fake_password(password); 1351 1352 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 1353 (const void *)&passwd_conv); 1354 if (sshpam_err != PAM_SUCCESS) 1355 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, 1356 pam_strerror(sshpam_handle, sshpam_err)); 1357 1358 sshpam_err = pam_authenticate(sshpam_handle, flags); 1359 sshpam_password = NULL; 1360 free(fake); 1361 if (sshpam_err == PAM_MAXTRIES) 1362 sshpam_set_maxtries_reached(1); 1363 if (sshpam_err == PAM_SUCCESS && authctxt->valid) { 1364 debug("PAM: password authentication accepted for %.100s", 1365 authctxt->user); 1366 return 1; 1367 } else { 1368 debug("PAM: password authentication failed for %.100s: %s", 1369 authctxt->valid ? authctxt->user : "an illegal user", 1370 pam_strerror(sshpam_handle, sshpam_err)); 1371 return 0; 1372 } 1373 } 1374 1375 int 1376 sshpam_get_maxtries_reached(void) 1377 { 1378 return sshpam_maxtries_reached; 1379 } 1380 1381 void 1382 sshpam_set_maxtries_reached(int reached) 1383 { 1384 if (reached == 0 || sshpam_maxtries_reached) 1385 return; 1386 sshpam_maxtries_reached = 1; 1387 options.password_authentication = 0; 1388 options.kbd_interactive_authentication = 0; 1389 options.challenge_response_authentication = 0; 1390 } 1391 #endif /* USE_PAM */ 1392