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*11044SHuie-Ying.Lee@Sun.COM * Copyright 2009 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" 47*11044SHuie-Ying.Lee@Sun.COM #include "buffer.h" 480Sstevel@tonic-gate #include "servconf.h" 490Sstevel@tonic-gate #include "auth.h" 500Sstevel@tonic-gate #include "auth-options.h" 510Sstevel@tonic-gate #include "canohost.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 3098206SBrent.Paulson@Sun.COM /* 3108206SBrent.Paulson@Sun.COM * This table lists the different variable combinations evaluated and 3118206SBrent.Paulson@Sun.COM * what the resulting PAM return value is. As the table shows 3128206SBrent.Paulson@Sun.COM * authctxt and authctxt->valid need to be checked before either of 3138206SBrent.Paulson@Sun.COM * the authctxt->pam* variables. 3148206SBrent.Paulson@Sun.COM * 3158206SBrent.Paulson@Sun.COM * authctxt-> authctxt-> 3168206SBrent.Paulson@Sun.COM * authctxt valid authctxt->pam pam_retval PAM rval 3178206SBrent.Paulson@Sun.COM * -------- ---------- ------------- ------------ -------- 3188206SBrent.Paulson@Sun.COM * NULL ANY ANY ANY PAM_ABORT 3198206SBrent.Paulson@Sun.COM * OK zero (0) ANY ANY PAM_USER_UNKNOWN 3208206SBrent.Paulson@Sun.COM * OK one (1) NULL PAM_SUCCESS PAM_PERM_DENIED 3218206SBrent.Paulson@Sun.COM * OK one (1) NULL !PAM_SUCCESS authctxt-> 3228206SBrent.Paulson@Sun.COM * pam_retval 3238206SBrent.Paulson@Sun.COM * OK one (1) VALID ANY authctxt-> 3248206SBrent.Paulson@Sun.COM * pam_retval (+) 3258206SBrent.Paulson@Sun.COM * (+) If not set then default to PAM_PERM_DENIED 3268206SBrent.Paulson@Sun.COM */ 3278206SBrent.Paulson@Sun.COM 3288206SBrent.Paulson@Sun.COM if (authctxt == NULL) { 3290Sstevel@tonic-gate /* Internal error */ 3308064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_ABORT, NULL); 3318206SBrent.Paulson@Sun.COM return; 3328206SBrent.Paulson@Sun.COM } 3338206SBrent.Paulson@Sun.COM 3348206SBrent.Paulson@Sun.COM if (authctxt->valid == 0) { 3358064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN, NULL); 3368206SBrent.Paulson@Sun.COM } else if (authctxt->pam == NULL) { 3378206SBrent.Paulson@Sun.COM if (authctxt->pam_retval == PAM_SUCCESS) { 3388206SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_PERM_DENIED, 3398206SBrent.Paulson@Sun.COM authctxt->user); 3408206SBrent.Paulson@Sun.COM } else { 3418206SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, authctxt->pam_retval, 3428206SBrent.Paulson@Sun.COM authctxt->user); 3438206SBrent.Paulson@Sun.COM } 3448206SBrent.Paulson@Sun.COM } else { 3450Sstevel@tonic-gate audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt, 3468064SBrent.Paulson@Sun.COM PAM_PERM_DENIED), authctxt->user); 3478206SBrent.Paulson@Sun.COM } 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate #endif /* HAVE_BSM */ 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate /* 3520Sstevel@tonic-gate * Check whether root logins are disallowed. 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate int 3550Sstevel@tonic-gate auth_root_allowed(char *method) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate switch (options.permit_root_login) { 3580Sstevel@tonic-gate case PERMIT_YES: 3590Sstevel@tonic-gate return 1; 3600Sstevel@tonic-gate break; 3610Sstevel@tonic-gate case PERMIT_NO_PASSWD: 3620Sstevel@tonic-gate if (strcmp(method, "password") != 0 && 3630Sstevel@tonic-gate strcmp(method, "keyboard-interactive") != 0) 3640Sstevel@tonic-gate return 1; 3650Sstevel@tonic-gate break; 3660Sstevel@tonic-gate case PERMIT_FORCED_ONLY: 3670Sstevel@tonic-gate if (forced_command) { 3680Sstevel@tonic-gate log("Root login accepted for forced command."); 3690Sstevel@tonic-gate return 1; 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate break; 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 3740Sstevel@tonic-gate return 0; 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * Given a template and a passwd structure, build a filename 3800Sstevel@tonic-gate * by substituting % tokenised options. Currently, %% becomes '%', 3810Sstevel@tonic-gate * %h becomes the home directory and %u the username. 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * This returns a buffer allocated by xmalloc. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate char * 3860Sstevel@tonic-gate expand_filename(const char *filename, struct passwd *pw) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate Buffer buffer; 3890Sstevel@tonic-gate char *file; 3900Sstevel@tonic-gate const char *cp; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if (pw == 0) 3930Sstevel@tonic-gate return NULL; /* shouldn't happen */ 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * Build the filename string in the buffer by making the appropriate 3960Sstevel@tonic-gate * substitutions to the given file name. 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate buffer_init(&buffer); 3990Sstevel@tonic-gate for (cp = filename; *cp; cp++) { 4000Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == '%') { 4010Sstevel@tonic-gate buffer_append(&buffer, "%", 1); 4020Sstevel@tonic-gate cp++; 4030Sstevel@tonic-gate continue; 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'h') { 4060Sstevel@tonic-gate buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir)); 4070Sstevel@tonic-gate cp++; 4080Sstevel@tonic-gate continue; 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'u') { 4110Sstevel@tonic-gate buffer_append(&buffer, pw->pw_name, 4120Sstevel@tonic-gate strlen(pw->pw_name)); 4130Sstevel@tonic-gate cp++; 4140Sstevel@tonic-gate continue; 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate buffer_append(&buffer, cp, 1); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate buffer_append(&buffer, "\0", 1); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Ensure that filename starts anchored. If not, be backward 4220Sstevel@tonic-gate * compatible and prepend the '%h/' 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate file = xmalloc(MAXPATHLEN); 4250Sstevel@tonic-gate cp = buffer_ptr(&buffer); 4260Sstevel@tonic-gate if (*cp != '/') 4270Sstevel@tonic-gate snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp); 4280Sstevel@tonic-gate else 4290Sstevel@tonic-gate strlcpy(file, cp, MAXPATHLEN); 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate buffer_free(&buffer); 4320Sstevel@tonic-gate return file; 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate char * 4360Sstevel@tonic-gate authorized_keys_file(struct passwd *pw) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate return expand_filename(options.authorized_keys_file, pw); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate char * 4420Sstevel@tonic-gate authorized_keys_file2(struct passwd *pw) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate return expand_filename(options.authorized_keys_file2, pw); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* return ok if key exists in sysfile or userfile */ 4480Sstevel@tonic-gate HostStatus 4490Sstevel@tonic-gate check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 4500Sstevel@tonic-gate const char *sysfile, const char *userfile) 4510Sstevel@tonic-gate { 4520Sstevel@tonic-gate Key *found; 4530Sstevel@tonic-gate char *user_hostfile; 4540Sstevel@tonic-gate struct stat st; 4550Sstevel@tonic-gate HostStatus host_status; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* Check if we know the host and its host key. */ 4580Sstevel@tonic-gate found = key_new(key->type); 4590Sstevel@tonic-gate host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate if (host_status != HOST_OK && userfile != NULL) { 4620Sstevel@tonic-gate user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 4630Sstevel@tonic-gate if (options.strict_modes && 4640Sstevel@tonic-gate (stat(user_hostfile, &st) == 0) && 4650Sstevel@tonic-gate ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 4660Sstevel@tonic-gate (st.st_mode & 022) != 0)) { 4670Sstevel@tonic-gate log("Authentication refused for %.100s: " 4680Sstevel@tonic-gate "bad owner or modes for %.200s", 4690Sstevel@tonic-gate pw->pw_name, user_hostfile); 4700Sstevel@tonic-gate } else { 4710Sstevel@tonic-gate temporarily_use_uid(pw); 4720Sstevel@tonic-gate host_status = check_host_in_hostfile(user_hostfile, 4730Sstevel@tonic-gate host, key, found, NULL); 4740Sstevel@tonic-gate restore_uid(); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate xfree(user_hostfile); 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate key_free(found); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 4810Sstevel@tonic-gate "ok" : "not found", host); 4820Sstevel@tonic-gate return host_status; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * Check a given file for security. This is defined as all components 4880Sstevel@tonic-gate * of the path to the file must be owned by either the owner of 4890Sstevel@tonic-gate * of the file or root and no directories must be group or world writable. 4900Sstevel@tonic-gate * 4910Sstevel@tonic-gate * XXX Should any specific check be done for sym links ? 4920Sstevel@tonic-gate * 4930Sstevel@tonic-gate * Takes an open file descriptor, the file name, a uid and and 4940Sstevel@tonic-gate * error buffer plus max size as arguments. 4950Sstevel@tonic-gate * 4960Sstevel@tonic-gate * Returns 0 on success and -1 on failure 4970Sstevel@tonic-gate */ 4980Sstevel@tonic-gate int 4990Sstevel@tonic-gate secure_filename(FILE *f, const char *file, struct passwd *pw, 5000Sstevel@tonic-gate char *err, size_t errlen) 5010Sstevel@tonic-gate { 5020Sstevel@tonic-gate uid_t uid; 5030Sstevel@tonic-gate char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 5040Sstevel@tonic-gate char *cp; 5053984Sjp161948 int comparehome = 0; 5060Sstevel@tonic-gate struct stat st; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate if (pw == NULL) 5090Sstevel@tonic-gate return 0; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate uid = pw->pw_uid; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate if (realpath(file, buf) == NULL) { 5140Sstevel@tonic-gate snprintf(err, errlen, "realpath %s failed: %s", file, 5150Sstevel@tonic-gate strerror(errno)); 5160Sstevel@tonic-gate return -1; 5170Sstevel@tonic-gate } 5183984Sjp161948 5193984Sjp161948 /* 5203984Sjp161948 * A user is not required to have all the files that are subject to 5213984Sjp161948 * the strict mode checking in his/her home directory. If the 5223984Sjp161948 * directory is not present at the moment, which might be the case if 5233984Sjp161948 * the directory is not mounted until the user is authenticated, do 5243984Sjp161948 * not perform the home directory check below. 5253984Sjp161948 */ 5263984Sjp161948 if (realpath(pw->pw_dir, homedir) != NULL) 5273984Sjp161948 comparehome = 1; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* check the open file to avoid races */ 5300Sstevel@tonic-gate if (fstat(fileno(f), &st) < 0 || 5310Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 5320Sstevel@tonic-gate (st.st_mode & 022) != 0) { 5330Sstevel@tonic-gate snprintf(err, errlen, "bad ownership or modes for file %s", 5340Sstevel@tonic-gate buf); 5350Sstevel@tonic-gate return -1; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* for each component of the canonical path, walking upwards */ 5390Sstevel@tonic-gate for (;;) { 5400Sstevel@tonic-gate if ((cp = dirname(buf)) == NULL) { 5410Sstevel@tonic-gate snprintf(err, errlen, "dirname() failed"); 5420Sstevel@tonic-gate return -1; 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate strlcpy(buf, cp, sizeof(buf)); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate debug3("secure_filename: checking '%s'", buf); 5470Sstevel@tonic-gate if (stat(buf, &st) < 0 || 5480Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 5490Sstevel@tonic-gate (st.st_mode & 022) != 0) { 5500Sstevel@tonic-gate snprintf(err, errlen, 5510Sstevel@tonic-gate "bad ownership or modes for directory %s", buf); 5520Sstevel@tonic-gate return -1; 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5553984Sjp161948 /* If we passed the homedir then we can stop. */ 5563984Sjp161948 if (comparehome && strcmp(homedir, buf) == 0) { 5570Sstevel@tonic-gate debug3("secure_filename: terminating check at '%s'", 5580Sstevel@tonic-gate buf); 5590Sstevel@tonic-gate break; 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate /* 5620Sstevel@tonic-gate * dirname should always complete with a "/" path, 5630Sstevel@tonic-gate * but we can be paranoid and check for "." too 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 5660Sstevel@tonic-gate break; 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate return 0; 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate struct passwd * 5720Sstevel@tonic-gate getpwnamallow(const char *user) 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 5750Sstevel@tonic-gate extern login_cap_t *lc; 5760Sstevel@tonic-gate #ifdef BSD_AUTH 5770Sstevel@tonic-gate auth_session_t *as; 5780Sstevel@tonic-gate #endif 5790Sstevel@tonic-gate #endif 5800Sstevel@tonic-gate struct passwd *pw; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (user == NULL || *user == '\0') 5830Sstevel@tonic-gate return (NULL); /* implicit user, will be set later */ 5840Sstevel@tonic-gate 585*11044SHuie-Ying.Lee@Sun.COM parse_server_match_config(&options, user, 586*11044SHuie-Ying.Lee@Sun.COM get_canonical_hostname(options.verify_reverse_mapping), get_remote_ipaddr()); 587*11044SHuie-Ying.Lee@Sun.COM 5880Sstevel@tonic-gate pw = getpwnam(user); 5890Sstevel@tonic-gate if (pw == NULL) { 5900Sstevel@tonic-gate log("Illegal user %.100s from %.100s", 5910Sstevel@tonic-gate user, get_remote_ipaddr()); 5920Sstevel@tonic-gate return (NULL); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate if (!allowed_user(pw)) 5950Sstevel@tonic-gate return (NULL); 5960Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 5970Sstevel@tonic-gate if ((lc = login_getclass(pw->pw_class)) == NULL) { 5980Sstevel@tonic-gate debug("unable to get login class: %s", user); 5990Sstevel@tonic-gate return (NULL); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate #ifdef BSD_AUTH 6020Sstevel@tonic-gate if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 6030Sstevel@tonic-gate auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 6040Sstevel@tonic-gate debug("Approval failure for %s", user); 6050Sstevel@tonic-gate pw = NULL; 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate if (as != NULL) 6080Sstevel@tonic-gate auth_close(as); 6090Sstevel@tonic-gate #endif 6100Sstevel@tonic-gate #endif 6110Sstevel@tonic-gate if (pw != NULL) 6120Sstevel@tonic-gate return (pwcopy(pw)); 6130Sstevel@tonic-gate return (NULL); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate void 6170Sstevel@tonic-gate auth_debug_add(const char *fmt,...) 6180Sstevel@tonic-gate { 6190Sstevel@tonic-gate char buf[1024]; 6200Sstevel@tonic-gate va_list args; 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (!auth_debug_init) 6230Sstevel@tonic-gate return; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate va_start(args, fmt); 6260Sstevel@tonic-gate vsnprintf(buf, sizeof(buf), fmt, args); 6270Sstevel@tonic-gate va_end(args); 6280Sstevel@tonic-gate buffer_put_cstring(&auth_debug, buf); 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate void 6320Sstevel@tonic-gate auth_debug_send(void) 6330Sstevel@tonic-gate { 6340Sstevel@tonic-gate char *msg; 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (!auth_debug_init) 6370Sstevel@tonic-gate return; 6380Sstevel@tonic-gate while (buffer_len(&auth_debug)) { 6390Sstevel@tonic-gate msg = buffer_get_string(&auth_debug, NULL); 6400Sstevel@tonic-gate packet_send_debug("%s", msg); 6410Sstevel@tonic-gate xfree(msg); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate void 6460Sstevel@tonic-gate auth_debug_reset(void) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate if (auth_debug_init) 6490Sstevel@tonic-gate buffer_clear(&auth_debug); 6500Sstevel@tonic-gate else { 6510Sstevel@tonic-gate buffer_init(&auth_debug); 6520Sstevel@tonic-gate auth_debug_init = 1; 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate } 655