10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate /* 25*8064SBrent.Paulson@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 260Sstevel@tonic-gate * Use is subject to license terms. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "includes.h" 300Sstevel@tonic-gate RCSID("$OpenBSD: auth.c,v 1.45 2002/09/20 18:41:29 stevesk Exp $"); 310Sstevel@tonic-gate 320Sstevel@tonic-gate #ifdef HAVE_LOGIN_H 330Sstevel@tonic-gate #include <login.h> 340Sstevel@tonic-gate #endif 350Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 360Sstevel@tonic-gate #include <shadow.h> 370Sstevel@tonic-gate #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef HAVE_LIBGEN_H 400Sstevel@tonic-gate #include <libgen.h> 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include "xmalloc.h" 440Sstevel@tonic-gate #include "match.h" 450Sstevel@tonic-gate #include "groupaccess.h" 460Sstevel@tonic-gate #include "log.h" 470Sstevel@tonic-gate #include "servconf.h" 480Sstevel@tonic-gate #include "auth.h" 490Sstevel@tonic-gate #include "auth-options.h" 500Sstevel@tonic-gate #include "canohost.h" 510Sstevel@tonic-gate #include "buffer.h" 520Sstevel@tonic-gate #include "bufaux.h" 530Sstevel@tonic-gate #include "uidswap.h" 540Sstevel@tonic-gate #include "tildexpand.h" 550Sstevel@tonic-gate #include "misc.h" 560Sstevel@tonic-gate #include "bufaux.h" 570Sstevel@tonic-gate #include "packet.h" 580Sstevel@tonic-gate 590Sstevel@tonic-gate #ifdef HAVE_BSM 600Sstevel@tonic-gate #include "bsmaudit.h" 610Sstevel@tonic-gate #include <bsm/adt.h> 620Sstevel@tonic-gate #endif /* HAVE_BSM */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* import */ 650Sstevel@tonic-gate extern ServerOptions options; 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* Debugging messages */ 680Sstevel@tonic-gate Buffer auth_debug; 690Sstevel@tonic-gate int auth_debug_init; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * Check if the user is allowed to log in via ssh. If user is listed 730Sstevel@tonic-gate * in DenyUsers or one of user's groups is listed in DenyGroups, false 740Sstevel@tonic-gate * will be returned. If AllowUsers isn't empty and user isn't listed 750Sstevel@tonic-gate * there, or if AllowGroups isn't empty and one of user's groups isn't 760Sstevel@tonic-gate * listed there, false will be returned. 770Sstevel@tonic-gate * If the user's shell is not executable, false will be returned. 780Sstevel@tonic-gate * Otherwise true is returned. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate int 810Sstevel@tonic-gate allowed_user(struct passwd * pw) 820Sstevel@tonic-gate { 830Sstevel@tonic-gate struct stat st; 840Sstevel@tonic-gate const char *hostname = NULL, *ipaddr = NULL; 850Sstevel@tonic-gate char *shell; 860Sstevel@tonic-gate int i; 870Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 880Sstevel@tonic-gate char *loginmsg; 890Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 900Sstevel@tonic-gate #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ 910Sstevel@tonic-gate !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 920Sstevel@tonic-gate struct spwd *spw; 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 950Sstevel@tonic-gate if (!pw || !pw->pw_name) 960Sstevel@tonic-gate return 0; 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define DAY (24L * 60 * 60) /* 1 day in seconds */ 990Sstevel@tonic-gate spw = getspnam(pw->pw_name); 1000Sstevel@tonic-gate if (spw != NULL) { 1010Sstevel@tonic-gate time_t today = time(NULL) / DAY; 1020Sstevel@tonic-gate debug3("allowed_user: today %d sp_expire %d sp_lstchg %d" 1030Sstevel@tonic-gate " sp_max %d", (int)today, (int)spw->sp_expire, 1040Sstevel@tonic-gate (int)spw->sp_lstchg, (int)spw->sp_max); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * We assume account and password expiration occurs the 1080Sstevel@tonic-gate * day after the day specified. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate if (spw->sp_expire != -1 && today > spw->sp_expire) { 1110Sstevel@tonic-gate log("Account %.100s has expired", pw->pw_name); 1120Sstevel@tonic-gate return 0; 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate if (spw->sp_lstchg == 0) { 1160Sstevel@tonic-gate log("User %.100s password has expired (root forced)", 1170Sstevel@tonic-gate pw->pw_name); 1180Sstevel@tonic-gate return 0; 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate if (spw->sp_max != -1 && 1220Sstevel@tonic-gate today > spw->sp_lstchg + spw->sp_max) { 1230Sstevel@tonic-gate log("User %.100s password has expired (password aged)", 1240Sstevel@tonic-gate pw->pw_name); 1250Sstevel@tonic-gate return 0; 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate #else 1290Sstevel@tonic-gate /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 1300Sstevel@tonic-gate if (!pw || !pw->pw_name) 1310Sstevel@tonic-gate return 0; 1320Sstevel@tonic-gate #endif 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* 1350Sstevel@tonic-gate * Get the shell from the password data. An empty shell field is 1360Sstevel@tonic-gate * legal, and means /bin/sh. 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* deny if shell does not exists or is not executable */ 1410Sstevel@tonic-gate if (stat(shell, &st) != 0) { 1420Sstevel@tonic-gate log("User %.100s not allowed because shell %.100s does not exist", 1430Sstevel@tonic-gate pw->pw_name, shell); 1440Sstevel@tonic-gate return 0; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate if (S_ISREG(st.st_mode) == 0 || 1470Sstevel@tonic-gate (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 1480Sstevel@tonic-gate log("User %.100s not allowed because shell %.100s is not executable", 1490Sstevel@tonic-gate pw->pw_name, shell); 1500Sstevel@tonic-gate return 0; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate if (options.num_deny_users > 0 || options.num_allow_users > 0) { 1540Sstevel@tonic-gate hostname = get_canonical_hostname(options.verify_reverse_mapping); 1550Sstevel@tonic-gate ipaddr = get_remote_ipaddr(); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* Return false if user is listed in DenyUsers */ 1590Sstevel@tonic-gate if (options.num_deny_users > 0) { 1600Sstevel@tonic-gate for (i = 0; i < options.num_deny_users; i++) 1610Sstevel@tonic-gate if (match_user(pw->pw_name, hostname, ipaddr, 1620Sstevel@tonic-gate options.deny_users[i])) { 1630Sstevel@tonic-gate log("User %.100s not allowed because listed in DenyUsers", 1640Sstevel@tonic-gate pw->pw_name); 1650Sstevel@tonic-gate return 0; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate /* Return false if AllowUsers isn't empty and user isn't listed there */ 1690Sstevel@tonic-gate if (options.num_allow_users > 0) { 1700Sstevel@tonic-gate for (i = 0; i < options.num_allow_users; i++) 1710Sstevel@tonic-gate if (match_user(pw->pw_name, hostname, ipaddr, 1720Sstevel@tonic-gate options.allow_users[i])) 1730Sstevel@tonic-gate break; 1740Sstevel@tonic-gate /* i < options.num_allow_users iff we break for loop */ 1750Sstevel@tonic-gate if (i >= options.num_allow_users) { 1760Sstevel@tonic-gate log("User %.100s not allowed because not listed in AllowUsers", 1770Sstevel@tonic-gate pw->pw_name); 1780Sstevel@tonic-gate return 0; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 1820Sstevel@tonic-gate /* Get the user's group access list (primary and supplementary) */ 1830Sstevel@tonic-gate if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 1840Sstevel@tonic-gate log("User %.100s not allowed because not in any group", 1850Sstevel@tonic-gate pw->pw_name); 1860Sstevel@tonic-gate return 0; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* Return false if one of user's groups is listed in DenyGroups */ 1900Sstevel@tonic-gate if (options.num_deny_groups > 0) 1910Sstevel@tonic-gate if (ga_match(options.deny_groups, 1920Sstevel@tonic-gate options.num_deny_groups)) { 1930Sstevel@tonic-gate ga_free(); 1940Sstevel@tonic-gate log("User %.100s not allowed because a group is listed in DenyGroups", 1950Sstevel@tonic-gate pw->pw_name); 1960Sstevel@tonic-gate return 0; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate /* 1990Sstevel@tonic-gate * Return false if AllowGroups isn't empty and one of user's groups 2000Sstevel@tonic-gate * isn't listed there 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate if (options.num_allow_groups > 0) 2030Sstevel@tonic-gate if (!ga_match(options.allow_groups, 2040Sstevel@tonic-gate options.num_allow_groups)) { 2050Sstevel@tonic-gate ga_free(); 2060Sstevel@tonic-gate log("User %.100s not allowed because none of user's groups are listed in AllowGroups", 2070Sstevel@tonic-gate pw->pw_name); 2080Sstevel@tonic-gate return 0; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate ga_free(); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 2140Sstevel@tonic-gate if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { 2150Sstevel@tonic-gate if (loginmsg && *loginmsg) { 2160Sstevel@tonic-gate /* Remove embedded newlines (if any) */ 2170Sstevel@tonic-gate char *p; 2180Sstevel@tonic-gate for (p = loginmsg; *p; p++) { 2190Sstevel@tonic-gate if (*p == '\n') 2200Sstevel@tonic-gate *p = ' '; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate /* Remove trailing newline */ 2230Sstevel@tonic-gate *--p = '\0'; 2240Sstevel@tonic-gate log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate return 0; 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* We found no reason not to let this user try to log on... */ 2310Sstevel@tonic-gate return 1; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate Authctxt * 2350Sstevel@tonic-gate authctxt_new(void) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate Authctxt *authctxt = xmalloc(sizeof(*authctxt)); 2380Sstevel@tonic-gate memset(authctxt, 0, sizeof(*authctxt)); 2390Sstevel@tonic-gate return authctxt; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate void 2430Sstevel@tonic-gate auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) 2440Sstevel@tonic-gate { 2450Sstevel@tonic-gate void (*authlog) (const char *fmt,...) = verbose; 2460Sstevel@tonic-gate char *authmsg, *user_str; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (authctxt == NULL) 2490Sstevel@tonic-gate fatal("%s: INTERNAL ERROR", __func__); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* Raise logging level */ 2520Sstevel@tonic-gate if (authenticated == 1 || !authctxt->valid) 2530Sstevel@tonic-gate authlog = log; 2540Sstevel@tonic-gate else if (authctxt->failures >= AUTH_FAIL_LOG || 2550Sstevel@tonic-gate authctxt->attempt >= options.max_auth_tries_log || 2560Sstevel@tonic-gate authctxt->init_attempt >= options.max_init_auth_tries_log) 2570Sstevel@tonic-gate authlog = notice; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate if (authctxt->method) { 2600Sstevel@tonic-gate authmsg = "Failed"; 2610Sstevel@tonic-gate if (authctxt->method->postponed) 2620Sstevel@tonic-gate authmsg = "Postponed"; /* shouldn't happen */ 2630Sstevel@tonic-gate if (authctxt->method->abandoned) 2640Sstevel@tonic-gate authmsg = "Abandoned"; 2650Sstevel@tonic-gate if (authctxt->method->authenticated) { 2660Sstevel@tonic-gate if (userauth_check_partial_failure(authctxt)) 2670Sstevel@tonic-gate authmsg = "Partially accepted"; 2680Sstevel@tonic-gate else 2690Sstevel@tonic-gate authmsg = "Accepted"; 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate else 2720Sstevel@tonic-gate authmsg = "Failed"; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate else { 2750Sstevel@tonic-gate authmsg = authenticated ? "Accepted" : "Failed"; 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate if (authctxt->user == NULL || *authctxt->user == '\0') 2790Sstevel@tonic-gate user_str = "<implicit>"; 2800Sstevel@tonic-gate else if (!authctxt->valid) 2810Sstevel@tonic-gate user_str = "<invalid username>"; 2820Sstevel@tonic-gate else 2830Sstevel@tonic-gate user_str = authctxt->user; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate authlog("%s %s for %s from %.200s port %d%s", 2860Sstevel@tonic-gate authmsg, 2870Sstevel@tonic-gate (method != NULL) ? method : "<unknown authentication method>", 2880Sstevel@tonic-gate user_str, 2890Sstevel@tonic-gate get_remote_ipaddr(), 2900Sstevel@tonic-gate get_remote_port(), 2910Sstevel@tonic-gate info); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 2940Sstevel@tonic-gate if (authenticated == 0 && strcmp(method, "password") == 0) 2950Sstevel@tonic-gate loginfailed(authctxt->user, 2960Sstevel@tonic-gate get_canonical_hostname(options.verify_reverse_mapping), 2970Sstevel@tonic-gate "ssh"); 2980Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate #ifdef HAVE_BSM 3030Sstevel@tonic-gate void 3040Sstevel@tonic-gate audit_failed_login_cleanup(void *ctxt) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate Authctxt *authctxt = (Authctxt *)ctxt; 3070Sstevel@tonic-gate adt_session_data_t *ah; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate if (authctxt == NULL) 3100Sstevel@tonic-gate /* Internal error */ 311*8064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_ABORT, NULL); 3120Sstevel@tonic-gate else if (authctxt->pam == NULL && authctxt->pam_retval != PAM_SUCCESS) 313*8064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, authctxt->pam_retval, 314*8064SBrent.Paulson@Sun.COM authctxt->user); 3150Sstevel@tonic-gate else if (authctxt->pam == NULL && authctxt->pam_retval == PAM_SUCCESS) 316*8064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_PERM_DENIED, authctxt->user); 3170Sstevel@tonic-gate else if (!authctxt->valid) 318*8064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN, NULL); 3190Sstevel@tonic-gate else 3200Sstevel@tonic-gate audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt, 321*8064SBrent.Paulson@Sun.COM PAM_PERM_DENIED), authctxt->user); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate #endif /* HAVE_BSM */ 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * Check whether root logins are disallowed. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate int 3290Sstevel@tonic-gate auth_root_allowed(char *method) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate switch (options.permit_root_login) { 3320Sstevel@tonic-gate case PERMIT_YES: 3330Sstevel@tonic-gate return 1; 3340Sstevel@tonic-gate break; 3350Sstevel@tonic-gate case PERMIT_NO_PASSWD: 3360Sstevel@tonic-gate if (strcmp(method, "password") != 0 && 3370Sstevel@tonic-gate strcmp(method, "keyboard-interactive") != 0) 3380Sstevel@tonic-gate return 1; 3390Sstevel@tonic-gate break; 3400Sstevel@tonic-gate case PERMIT_FORCED_ONLY: 3410Sstevel@tonic-gate if (forced_command) { 3420Sstevel@tonic-gate log("Root login accepted for forced command."); 3430Sstevel@tonic-gate return 1; 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 3480Sstevel@tonic-gate return 0; 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * Given a template and a passwd structure, build a filename 3540Sstevel@tonic-gate * by substituting % tokenised options. Currently, %% becomes '%', 3550Sstevel@tonic-gate * %h becomes the home directory and %u the username. 3560Sstevel@tonic-gate * 3570Sstevel@tonic-gate * This returns a buffer allocated by xmalloc. 3580Sstevel@tonic-gate */ 3590Sstevel@tonic-gate char * 3600Sstevel@tonic-gate expand_filename(const char *filename, struct passwd *pw) 3610Sstevel@tonic-gate { 3620Sstevel@tonic-gate Buffer buffer; 3630Sstevel@tonic-gate char *file; 3640Sstevel@tonic-gate const char *cp; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (pw == 0) 3670Sstevel@tonic-gate return NULL; /* shouldn't happen */ 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * Build the filename string in the buffer by making the appropriate 3700Sstevel@tonic-gate * substitutions to the given file name. 3710Sstevel@tonic-gate */ 3720Sstevel@tonic-gate buffer_init(&buffer); 3730Sstevel@tonic-gate for (cp = filename; *cp; cp++) { 3740Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == '%') { 3750Sstevel@tonic-gate buffer_append(&buffer, "%", 1); 3760Sstevel@tonic-gate cp++; 3770Sstevel@tonic-gate continue; 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'h') { 3800Sstevel@tonic-gate buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir)); 3810Sstevel@tonic-gate cp++; 3820Sstevel@tonic-gate continue; 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'u') { 3850Sstevel@tonic-gate buffer_append(&buffer, pw->pw_name, 3860Sstevel@tonic-gate strlen(pw->pw_name)); 3870Sstevel@tonic-gate cp++; 3880Sstevel@tonic-gate continue; 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate buffer_append(&buffer, cp, 1); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate buffer_append(&buffer, "\0", 1); 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * Ensure that filename starts anchored. If not, be backward 3960Sstevel@tonic-gate * compatible and prepend the '%h/' 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate file = xmalloc(MAXPATHLEN); 3990Sstevel@tonic-gate cp = buffer_ptr(&buffer); 4000Sstevel@tonic-gate if (*cp != '/') 4010Sstevel@tonic-gate snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp); 4020Sstevel@tonic-gate else 4030Sstevel@tonic-gate strlcpy(file, cp, MAXPATHLEN); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate buffer_free(&buffer); 4060Sstevel@tonic-gate return file; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate char * 4100Sstevel@tonic-gate authorized_keys_file(struct passwd *pw) 4110Sstevel@tonic-gate { 4120Sstevel@tonic-gate return expand_filename(options.authorized_keys_file, pw); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate char * 4160Sstevel@tonic-gate authorized_keys_file2(struct passwd *pw) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate return expand_filename(options.authorized_keys_file2, pw); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate /* return ok if key exists in sysfile or userfile */ 4220Sstevel@tonic-gate HostStatus 4230Sstevel@tonic-gate check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 4240Sstevel@tonic-gate const char *sysfile, const char *userfile) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate Key *found; 4270Sstevel@tonic-gate char *user_hostfile; 4280Sstevel@tonic-gate struct stat st; 4290Sstevel@tonic-gate HostStatus host_status; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* Check if we know the host and its host key. */ 4320Sstevel@tonic-gate found = key_new(key->type); 4330Sstevel@tonic-gate host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if (host_status != HOST_OK && userfile != NULL) { 4360Sstevel@tonic-gate user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 4370Sstevel@tonic-gate if (options.strict_modes && 4380Sstevel@tonic-gate (stat(user_hostfile, &st) == 0) && 4390Sstevel@tonic-gate ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 4400Sstevel@tonic-gate (st.st_mode & 022) != 0)) { 4410Sstevel@tonic-gate log("Authentication refused for %.100s: " 4420Sstevel@tonic-gate "bad owner or modes for %.200s", 4430Sstevel@tonic-gate pw->pw_name, user_hostfile); 4440Sstevel@tonic-gate } else { 4450Sstevel@tonic-gate temporarily_use_uid(pw); 4460Sstevel@tonic-gate host_status = check_host_in_hostfile(user_hostfile, 4470Sstevel@tonic-gate host, key, found, NULL); 4480Sstevel@tonic-gate restore_uid(); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate xfree(user_hostfile); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate key_free(found); 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 4550Sstevel@tonic-gate "ok" : "not found", host); 4560Sstevel@tonic-gate return host_status; 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * Check a given file for security. This is defined as all components 4620Sstevel@tonic-gate * of the path to the file must be owned by either the owner of 4630Sstevel@tonic-gate * of the file or root and no directories must be group or world writable. 4640Sstevel@tonic-gate * 4650Sstevel@tonic-gate * XXX Should any specific check be done for sym links ? 4660Sstevel@tonic-gate * 4670Sstevel@tonic-gate * Takes an open file descriptor, the file name, a uid and and 4680Sstevel@tonic-gate * error buffer plus max size as arguments. 4690Sstevel@tonic-gate * 4700Sstevel@tonic-gate * Returns 0 on success and -1 on failure 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate int 4730Sstevel@tonic-gate secure_filename(FILE *f, const char *file, struct passwd *pw, 4740Sstevel@tonic-gate char *err, size_t errlen) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate uid_t uid; 4770Sstevel@tonic-gate char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 4780Sstevel@tonic-gate char *cp; 4793984Sjp161948 int comparehome = 0; 4800Sstevel@tonic-gate struct stat st; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate if (pw == NULL) 4830Sstevel@tonic-gate return 0; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate uid = pw->pw_uid; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate if (realpath(file, buf) == NULL) { 4880Sstevel@tonic-gate snprintf(err, errlen, "realpath %s failed: %s", file, 4890Sstevel@tonic-gate strerror(errno)); 4900Sstevel@tonic-gate return -1; 4910Sstevel@tonic-gate } 4923984Sjp161948 4933984Sjp161948 /* 4943984Sjp161948 * A user is not required to have all the files that are subject to 4953984Sjp161948 * the strict mode checking in his/her home directory. If the 4963984Sjp161948 * directory is not present at the moment, which might be the case if 4973984Sjp161948 * the directory is not mounted until the user is authenticated, do 4983984Sjp161948 * not perform the home directory check below. 4993984Sjp161948 */ 5003984Sjp161948 if (realpath(pw->pw_dir, homedir) != NULL) 5013984Sjp161948 comparehome = 1; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* check the open file to avoid races */ 5040Sstevel@tonic-gate if (fstat(fileno(f), &st) < 0 || 5050Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 5060Sstevel@tonic-gate (st.st_mode & 022) != 0) { 5070Sstevel@tonic-gate snprintf(err, errlen, "bad ownership or modes for file %s", 5080Sstevel@tonic-gate buf); 5090Sstevel@tonic-gate return -1; 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* for each component of the canonical path, walking upwards */ 5130Sstevel@tonic-gate for (;;) { 5140Sstevel@tonic-gate if ((cp = dirname(buf)) == NULL) { 5150Sstevel@tonic-gate snprintf(err, errlen, "dirname() failed"); 5160Sstevel@tonic-gate return -1; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate strlcpy(buf, cp, sizeof(buf)); 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate debug3("secure_filename: checking '%s'", buf); 5210Sstevel@tonic-gate if (stat(buf, &st) < 0 || 5220Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 5230Sstevel@tonic-gate (st.st_mode & 022) != 0) { 5240Sstevel@tonic-gate snprintf(err, errlen, 5250Sstevel@tonic-gate "bad ownership or modes for directory %s", buf); 5260Sstevel@tonic-gate return -1; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5293984Sjp161948 /* If we passed the homedir then we can stop. */ 5303984Sjp161948 if (comparehome && strcmp(homedir, buf) == 0) { 5310Sstevel@tonic-gate debug3("secure_filename: terminating check at '%s'", 5320Sstevel@tonic-gate buf); 5330Sstevel@tonic-gate break; 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * dirname should always complete with a "/" path, 5370Sstevel@tonic-gate * but we can be paranoid and check for "." too 5380Sstevel@tonic-gate */ 5390Sstevel@tonic-gate if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 5400Sstevel@tonic-gate break; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate return 0; 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate struct passwd * 5460Sstevel@tonic-gate getpwnamallow(const char *user) 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 5490Sstevel@tonic-gate extern login_cap_t *lc; 5500Sstevel@tonic-gate #ifdef BSD_AUTH 5510Sstevel@tonic-gate auth_session_t *as; 5520Sstevel@tonic-gate #endif 5530Sstevel@tonic-gate #endif 5540Sstevel@tonic-gate struct passwd *pw; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate if (user == NULL || *user == '\0') 5570Sstevel@tonic-gate return (NULL); /* implicit user, will be set later */ 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate pw = getpwnam(user); 5600Sstevel@tonic-gate if (pw == NULL) { 5610Sstevel@tonic-gate log("Illegal user %.100s from %.100s", 5620Sstevel@tonic-gate user, get_remote_ipaddr()); 5630Sstevel@tonic-gate return (NULL); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate if (!allowed_user(pw)) 5660Sstevel@tonic-gate return (NULL); 5670Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 5680Sstevel@tonic-gate if ((lc = login_getclass(pw->pw_class)) == NULL) { 5690Sstevel@tonic-gate debug("unable to get login class: %s", user); 5700Sstevel@tonic-gate return (NULL); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate #ifdef BSD_AUTH 5730Sstevel@tonic-gate if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 5740Sstevel@tonic-gate auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 5750Sstevel@tonic-gate debug("Approval failure for %s", user); 5760Sstevel@tonic-gate pw = NULL; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate if (as != NULL) 5790Sstevel@tonic-gate auth_close(as); 5800Sstevel@tonic-gate #endif 5810Sstevel@tonic-gate #endif 5820Sstevel@tonic-gate if (pw != NULL) 5830Sstevel@tonic-gate return (pwcopy(pw)); 5840Sstevel@tonic-gate return (NULL); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate void 5880Sstevel@tonic-gate auth_debug_add(const char *fmt,...) 5890Sstevel@tonic-gate { 5900Sstevel@tonic-gate char buf[1024]; 5910Sstevel@tonic-gate va_list args; 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate if (!auth_debug_init) 5940Sstevel@tonic-gate return; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate va_start(args, fmt); 5970Sstevel@tonic-gate vsnprintf(buf, sizeof(buf), fmt, args); 5980Sstevel@tonic-gate va_end(args); 5990Sstevel@tonic-gate buffer_put_cstring(&auth_debug, buf); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate void 6030Sstevel@tonic-gate auth_debug_send(void) 6040Sstevel@tonic-gate { 6050Sstevel@tonic-gate char *msg; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate if (!auth_debug_init) 6080Sstevel@tonic-gate return; 6090Sstevel@tonic-gate while (buffer_len(&auth_debug)) { 6100Sstevel@tonic-gate msg = buffer_get_string(&auth_debug, NULL); 6110Sstevel@tonic-gate packet_send_debug("%s", msg); 6120Sstevel@tonic-gate xfree(msg); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate void 6170Sstevel@tonic-gate auth_debug_reset(void) 6180Sstevel@tonic-gate { 6190Sstevel@tonic-gate if (auth_debug_init) 6200Sstevel@tonic-gate buffer_clear(&auth_debug); 6210Sstevel@tonic-gate else { 6220Sstevel@tonic-gate buffer_init(&auth_debug); 6230Sstevel@tonic-gate auth_debug_init = 1; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate } 626