1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 5*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 6*0Sstevel@tonic-gate * are met: 7*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 8*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 9*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 11*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 26*0Sstevel@tonic-gate * Use is subject to license terms. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "includes.h" 30*0Sstevel@tonic-gate RCSID("$OpenBSD: auth.c,v 1.45 2002/09/20 18:41:29 stevesk Exp $"); 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #ifdef HAVE_LOGIN_H 35*0Sstevel@tonic-gate #include <login.h> 36*0Sstevel@tonic-gate #endif 37*0Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 38*0Sstevel@tonic-gate #include <shadow.h> 39*0Sstevel@tonic-gate #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #ifdef HAVE_LIBGEN_H 42*0Sstevel@tonic-gate #include <libgen.h> 43*0Sstevel@tonic-gate #endif 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #include "xmalloc.h" 46*0Sstevel@tonic-gate #include "match.h" 47*0Sstevel@tonic-gate #include "groupaccess.h" 48*0Sstevel@tonic-gate #include "log.h" 49*0Sstevel@tonic-gate #include "servconf.h" 50*0Sstevel@tonic-gate #include "auth.h" 51*0Sstevel@tonic-gate #include "auth-options.h" 52*0Sstevel@tonic-gate #include "canohost.h" 53*0Sstevel@tonic-gate #include "buffer.h" 54*0Sstevel@tonic-gate #include "bufaux.h" 55*0Sstevel@tonic-gate #include "uidswap.h" 56*0Sstevel@tonic-gate #include "tildexpand.h" 57*0Sstevel@tonic-gate #include "misc.h" 58*0Sstevel@tonic-gate #include "bufaux.h" 59*0Sstevel@tonic-gate #include "packet.h" 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #ifdef HAVE_BSM 62*0Sstevel@tonic-gate #include "bsmaudit.h" 63*0Sstevel@tonic-gate #include <bsm/adt.h> 64*0Sstevel@tonic-gate #endif /* HAVE_BSM */ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /* import */ 67*0Sstevel@tonic-gate extern ServerOptions options; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate /* Debugging messages */ 70*0Sstevel@tonic-gate Buffer auth_debug; 71*0Sstevel@tonic-gate int auth_debug_init; 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate /* 74*0Sstevel@tonic-gate * Check if the user is allowed to log in via ssh. If user is listed 75*0Sstevel@tonic-gate * in DenyUsers or one of user's groups is listed in DenyGroups, false 76*0Sstevel@tonic-gate * will be returned. If AllowUsers isn't empty and user isn't listed 77*0Sstevel@tonic-gate * there, or if AllowGroups isn't empty and one of user's groups isn't 78*0Sstevel@tonic-gate * listed there, false will be returned. 79*0Sstevel@tonic-gate * If the user's shell is not executable, false will be returned. 80*0Sstevel@tonic-gate * Otherwise true is returned. 81*0Sstevel@tonic-gate */ 82*0Sstevel@tonic-gate int 83*0Sstevel@tonic-gate allowed_user(struct passwd * pw) 84*0Sstevel@tonic-gate { 85*0Sstevel@tonic-gate struct stat st; 86*0Sstevel@tonic-gate const char *hostname = NULL, *ipaddr = NULL; 87*0Sstevel@tonic-gate char *shell; 88*0Sstevel@tonic-gate int i; 89*0Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 90*0Sstevel@tonic-gate char *loginmsg; 91*0Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 92*0Sstevel@tonic-gate #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ 93*0Sstevel@tonic-gate !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 94*0Sstevel@tonic-gate struct spwd *spw; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 97*0Sstevel@tonic-gate if (!pw || !pw->pw_name) 98*0Sstevel@tonic-gate return 0; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate #define DAY (24L * 60 * 60) /* 1 day in seconds */ 101*0Sstevel@tonic-gate spw = getspnam(pw->pw_name); 102*0Sstevel@tonic-gate if (spw != NULL) { 103*0Sstevel@tonic-gate time_t today = time(NULL) / DAY; 104*0Sstevel@tonic-gate debug3("allowed_user: today %d sp_expire %d sp_lstchg %d" 105*0Sstevel@tonic-gate " sp_max %d", (int)today, (int)spw->sp_expire, 106*0Sstevel@tonic-gate (int)spw->sp_lstchg, (int)spw->sp_max); 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * We assume account and password expiration occurs the 110*0Sstevel@tonic-gate * day after the day specified. 111*0Sstevel@tonic-gate */ 112*0Sstevel@tonic-gate if (spw->sp_expire != -1 && today > spw->sp_expire) { 113*0Sstevel@tonic-gate log("Account %.100s has expired", pw->pw_name); 114*0Sstevel@tonic-gate return 0; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate if (spw->sp_lstchg == 0) { 118*0Sstevel@tonic-gate log("User %.100s password has expired (root forced)", 119*0Sstevel@tonic-gate pw->pw_name); 120*0Sstevel@tonic-gate return 0; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate if (spw->sp_max != -1 && 124*0Sstevel@tonic-gate today > spw->sp_lstchg + spw->sp_max) { 125*0Sstevel@tonic-gate log("User %.100s password has expired (password aged)", 126*0Sstevel@tonic-gate pw->pw_name); 127*0Sstevel@tonic-gate return 0; 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate #else 131*0Sstevel@tonic-gate /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 132*0Sstevel@tonic-gate if (!pw || !pw->pw_name) 133*0Sstevel@tonic-gate return 0; 134*0Sstevel@tonic-gate #endif 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate /* 137*0Sstevel@tonic-gate * Get the shell from the password data. An empty shell field is 138*0Sstevel@tonic-gate * legal, and means /bin/sh. 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate /* deny if shell does not exists or is not executable */ 143*0Sstevel@tonic-gate if (stat(shell, &st) != 0) { 144*0Sstevel@tonic-gate log("User %.100s not allowed because shell %.100s does not exist", 145*0Sstevel@tonic-gate pw->pw_name, shell); 146*0Sstevel@tonic-gate return 0; 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate if (S_ISREG(st.st_mode) == 0 || 149*0Sstevel@tonic-gate (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 150*0Sstevel@tonic-gate log("User %.100s not allowed because shell %.100s is not executable", 151*0Sstevel@tonic-gate pw->pw_name, shell); 152*0Sstevel@tonic-gate return 0; 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if (options.num_deny_users > 0 || options.num_allow_users > 0) { 156*0Sstevel@tonic-gate hostname = get_canonical_hostname(options.verify_reverse_mapping); 157*0Sstevel@tonic-gate ipaddr = get_remote_ipaddr(); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate /* Return false if user is listed in DenyUsers */ 161*0Sstevel@tonic-gate if (options.num_deny_users > 0) { 162*0Sstevel@tonic-gate for (i = 0; i < options.num_deny_users; i++) 163*0Sstevel@tonic-gate if (match_user(pw->pw_name, hostname, ipaddr, 164*0Sstevel@tonic-gate options.deny_users[i])) { 165*0Sstevel@tonic-gate log("User %.100s not allowed because listed in DenyUsers", 166*0Sstevel@tonic-gate pw->pw_name); 167*0Sstevel@tonic-gate return 0; 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate /* Return false if AllowUsers isn't empty and user isn't listed there */ 171*0Sstevel@tonic-gate if (options.num_allow_users > 0) { 172*0Sstevel@tonic-gate for (i = 0; i < options.num_allow_users; i++) 173*0Sstevel@tonic-gate if (match_user(pw->pw_name, hostname, ipaddr, 174*0Sstevel@tonic-gate options.allow_users[i])) 175*0Sstevel@tonic-gate break; 176*0Sstevel@tonic-gate /* i < options.num_allow_users iff we break for loop */ 177*0Sstevel@tonic-gate if (i >= options.num_allow_users) { 178*0Sstevel@tonic-gate log("User %.100s not allowed because not listed in AllowUsers", 179*0Sstevel@tonic-gate pw->pw_name); 180*0Sstevel@tonic-gate return 0; 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 184*0Sstevel@tonic-gate /* Get the user's group access list (primary and supplementary) */ 185*0Sstevel@tonic-gate if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 186*0Sstevel@tonic-gate log("User %.100s not allowed because not in any group", 187*0Sstevel@tonic-gate pw->pw_name); 188*0Sstevel@tonic-gate return 0; 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate /* Return false if one of user's groups is listed in DenyGroups */ 192*0Sstevel@tonic-gate if (options.num_deny_groups > 0) 193*0Sstevel@tonic-gate if (ga_match(options.deny_groups, 194*0Sstevel@tonic-gate options.num_deny_groups)) { 195*0Sstevel@tonic-gate ga_free(); 196*0Sstevel@tonic-gate log("User %.100s not allowed because a group is listed in DenyGroups", 197*0Sstevel@tonic-gate pw->pw_name); 198*0Sstevel@tonic-gate return 0; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * Return false if AllowGroups isn't empty and one of user's groups 202*0Sstevel@tonic-gate * isn't listed there 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate if (options.num_allow_groups > 0) 205*0Sstevel@tonic-gate if (!ga_match(options.allow_groups, 206*0Sstevel@tonic-gate options.num_allow_groups)) { 207*0Sstevel@tonic-gate ga_free(); 208*0Sstevel@tonic-gate log("User %.100s not allowed because none of user's groups are listed in AllowGroups", 209*0Sstevel@tonic-gate pw->pw_name); 210*0Sstevel@tonic-gate return 0; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate ga_free(); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 216*0Sstevel@tonic-gate if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { 217*0Sstevel@tonic-gate if (loginmsg && *loginmsg) { 218*0Sstevel@tonic-gate /* Remove embedded newlines (if any) */ 219*0Sstevel@tonic-gate char *p; 220*0Sstevel@tonic-gate for (p = loginmsg; *p; p++) { 221*0Sstevel@tonic-gate if (*p == '\n') 222*0Sstevel@tonic-gate *p = ' '; 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate /* Remove trailing newline */ 225*0Sstevel@tonic-gate *--p = '\0'; 226*0Sstevel@tonic-gate log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate return 0; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate /* We found no reason not to let this user try to log on... */ 233*0Sstevel@tonic-gate return 1; 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate Authctxt * 237*0Sstevel@tonic-gate authctxt_new(void) 238*0Sstevel@tonic-gate { 239*0Sstevel@tonic-gate Authctxt *authctxt = xmalloc(sizeof(*authctxt)); 240*0Sstevel@tonic-gate memset(authctxt, 0, sizeof(*authctxt)); 241*0Sstevel@tonic-gate return authctxt; 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate void 245*0Sstevel@tonic-gate auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) 246*0Sstevel@tonic-gate { 247*0Sstevel@tonic-gate void (*authlog) (const char *fmt,...) = verbose; 248*0Sstevel@tonic-gate char *authmsg, *user_str; 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if (authctxt == NULL) 251*0Sstevel@tonic-gate fatal("%s: INTERNAL ERROR", __func__); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate /* Raise logging level */ 254*0Sstevel@tonic-gate if (authenticated == 1 || !authctxt->valid) 255*0Sstevel@tonic-gate authlog = log; 256*0Sstevel@tonic-gate else if (authctxt->failures >= AUTH_FAIL_LOG || 257*0Sstevel@tonic-gate authctxt->attempt >= options.max_auth_tries_log || 258*0Sstevel@tonic-gate authctxt->init_attempt >= options.max_init_auth_tries_log) 259*0Sstevel@tonic-gate authlog = notice; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if (authctxt->method) { 262*0Sstevel@tonic-gate authmsg = "Failed"; 263*0Sstevel@tonic-gate if (authctxt->method->postponed) 264*0Sstevel@tonic-gate authmsg = "Postponed"; /* shouldn't happen */ 265*0Sstevel@tonic-gate if (authctxt->method->abandoned) 266*0Sstevel@tonic-gate authmsg = "Abandoned"; 267*0Sstevel@tonic-gate if (authctxt->method->authenticated) { 268*0Sstevel@tonic-gate if (userauth_check_partial_failure(authctxt)) 269*0Sstevel@tonic-gate authmsg = "Partially accepted"; 270*0Sstevel@tonic-gate else 271*0Sstevel@tonic-gate authmsg = "Accepted"; 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate else 274*0Sstevel@tonic-gate authmsg = "Failed"; 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate else { 277*0Sstevel@tonic-gate authmsg = authenticated ? "Accepted" : "Failed"; 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate if (authctxt->user == NULL || *authctxt->user == '\0') 281*0Sstevel@tonic-gate user_str = "<implicit>"; 282*0Sstevel@tonic-gate else if (!authctxt->valid) 283*0Sstevel@tonic-gate user_str = "<invalid username>"; 284*0Sstevel@tonic-gate else 285*0Sstevel@tonic-gate user_str = authctxt->user; 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate authlog("%s %s for %s from %.200s port %d%s", 288*0Sstevel@tonic-gate authmsg, 289*0Sstevel@tonic-gate (method != NULL) ? method : "<unknown authentication method>", 290*0Sstevel@tonic-gate user_str, 291*0Sstevel@tonic-gate get_remote_ipaddr(), 292*0Sstevel@tonic-gate get_remote_port(), 293*0Sstevel@tonic-gate info); 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 296*0Sstevel@tonic-gate if (authenticated == 0 && strcmp(method, "password") == 0) 297*0Sstevel@tonic-gate loginfailed(authctxt->user, 298*0Sstevel@tonic-gate get_canonical_hostname(options.verify_reverse_mapping), 299*0Sstevel@tonic-gate "ssh"); 300*0Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate #ifdef HAVE_BSM 305*0Sstevel@tonic-gate void 306*0Sstevel@tonic-gate audit_failed_login_cleanup(void *ctxt) 307*0Sstevel@tonic-gate { 308*0Sstevel@tonic-gate Authctxt *authctxt = (Authctxt *)ctxt; 309*0Sstevel@tonic-gate adt_session_data_t *ah; 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate if (authctxt == NULL) 312*0Sstevel@tonic-gate /* Internal error */ 313*0Sstevel@tonic-gate audit_sshd_login_failure(&ah, PAM_ABORT); 314*0Sstevel@tonic-gate else if (authctxt->pam == NULL && authctxt->pam_retval != PAM_SUCCESS) 315*0Sstevel@tonic-gate audit_sshd_login_failure(&ah, authctxt->pam_retval); 316*0Sstevel@tonic-gate else if (authctxt->pam == NULL && authctxt->pam_retval == PAM_SUCCESS) 317*0Sstevel@tonic-gate audit_sshd_login_failure(&ah, PAM_PERM_DENIED); 318*0Sstevel@tonic-gate else if (!authctxt->valid) 319*0Sstevel@tonic-gate audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN); 320*0Sstevel@tonic-gate else 321*0Sstevel@tonic-gate audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt, 322*0Sstevel@tonic-gate PAM_PERM_DENIED)); 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate #endif /* HAVE_BSM */ 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate /* 327*0Sstevel@tonic-gate * Check whether root logins are disallowed. 328*0Sstevel@tonic-gate */ 329*0Sstevel@tonic-gate int 330*0Sstevel@tonic-gate auth_root_allowed(char *method) 331*0Sstevel@tonic-gate { 332*0Sstevel@tonic-gate switch (options.permit_root_login) { 333*0Sstevel@tonic-gate case PERMIT_YES: 334*0Sstevel@tonic-gate return 1; 335*0Sstevel@tonic-gate break; 336*0Sstevel@tonic-gate case PERMIT_NO_PASSWD: 337*0Sstevel@tonic-gate if (strcmp(method, "password") != 0 && 338*0Sstevel@tonic-gate strcmp(method, "keyboard-interactive") != 0) 339*0Sstevel@tonic-gate return 1; 340*0Sstevel@tonic-gate break; 341*0Sstevel@tonic-gate case PERMIT_FORCED_ONLY: 342*0Sstevel@tonic-gate if (forced_command) { 343*0Sstevel@tonic-gate log("Root login accepted for forced command."); 344*0Sstevel@tonic-gate return 1; 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate break; 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 349*0Sstevel@tonic-gate return 0; 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* 354*0Sstevel@tonic-gate * Given a template and a passwd structure, build a filename 355*0Sstevel@tonic-gate * by substituting % tokenised options. Currently, %% becomes '%', 356*0Sstevel@tonic-gate * %h becomes the home directory and %u the username. 357*0Sstevel@tonic-gate * 358*0Sstevel@tonic-gate * This returns a buffer allocated by xmalloc. 359*0Sstevel@tonic-gate */ 360*0Sstevel@tonic-gate char * 361*0Sstevel@tonic-gate expand_filename(const char *filename, struct passwd *pw) 362*0Sstevel@tonic-gate { 363*0Sstevel@tonic-gate Buffer buffer; 364*0Sstevel@tonic-gate char *file; 365*0Sstevel@tonic-gate const char *cp; 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate if (pw == 0) 368*0Sstevel@tonic-gate return NULL; /* shouldn't happen */ 369*0Sstevel@tonic-gate /* 370*0Sstevel@tonic-gate * Build the filename string in the buffer by making the appropriate 371*0Sstevel@tonic-gate * substitutions to the given file name. 372*0Sstevel@tonic-gate */ 373*0Sstevel@tonic-gate buffer_init(&buffer); 374*0Sstevel@tonic-gate for (cp = filename; *cp; cp++) { 375*0Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == '%') { 376*0Sstevel@tonic-gate buffer_append(&buffer, "%", 1); 377*0Sstevel@tonic-gate cp++; 378*0Sstevel@tonic-gate continue; 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'h') { 381*0Sstevel@tonic-gate buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir)); 382*0Sstevel@tonic-gate cp++; 383*0Sstevel@tonic-gate continue; 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate if (cp[0] == '%' && cp[1] == 'u') { 386*0Sstevel@tonic-gate buffer_append(&buffer, pw->pw_name, 387*0Sstevel@tonic-gate strlen(pw->pw_name)); 388*0Sstevel@tonic-gate cp++; 389*0Sstevel@tonic-gate continue; 390*0Sstevel@tonic-gate } 391*0Sstevel@tonic-gate buffer_append(&buffer, cp, 1); 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate buffer_append(&buffer, "\0", 1); 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate /* 396*0Sstevel@tonic-gate * Ensure that filename starts anchored. If not, be backward 397*0Sstevel@tonic-gate * compatible and prepend the '%h/' 398*0Sstevel@tonic-gate */ 399*0Sstevel@tonic-gate file = xmalloc(MAXPATHLEN); 400*0Sstevel@tonic-gate cp = buffer_ptr(&buffer); 401*0Sstevel@tonic-gate if (*cp != '/') 402*0Sstevel@tonic-gate snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp); 403*0Sstevel@tonic-gate else 404*0Sstevel@tonic-gate strlcpy(file, cp, MAXPATHLEN); 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate buffer_free(&buffer); 407*0Sstevel@tonic-gate return file; 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate char * 411*0Sstevel@tonic-gate authorized_keys_file(struct passwd *pw) 412*0Sstevel@tonic-gate { 413*0Sstevel@tonic-gate return expand_filename(options.authorized_keys_file, pw); 414*0Sstevel@tonic-gate } 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gate char * 417*0Sstevel@tonic-gate authorized_keys_file2(struct passwd *pw) 418*0Sstevel@tonic-gate { 419*0Sstevel@tonic-gate return expand_filename(options.authorized_keys_file2, pw); 420*0Sstevel@tonic-gate } 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gate /* return ok if key exists in sysfile or userfile */ 423*0Sstevel@tonic-gate HostStatus 424*0Sstevel@tonic-gate check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 425*0Sstevel@tonic-gate const char *sysfile, const char *userfile) 426*0Sstevel@tonic-gate { 427*0Sstevel@tonic-gate Key *found; 428*0Sstevel@tonic-gate char *user_hostfile; 429*0Sstevel@tonic-gate struct stat st; 430*0Sstevel@tonic-gate HostStatus host_status; 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate /* Check if we know the host and its host key. */ 433*0Sstevel@tonic-gate found = key_new(key->type); 434*0Sstevel@tonic-gate host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gate if (host_status != HOST_OK && userfile != NULL) { 437*0Sstevel@tonic-gate user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 438*0Sstevel@tonic-gate if (options.strict_modes && 439*0Sstevel@tonic-gate (stat(user_hostfile, &st) == 0) && 440*0Sstevel@tonic-gate ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 441*0Sstevel@tonic-gate (st.st_mode & 022) != 0)) { 442*0Sstevel@tonic-gate log("Authentication refused for %.100s: " 443*0Sstevel@tonic-gate "bad owner or modes for %.200s", 444*0Sstevel@tonic-gate pw->pw_name, user_hostfile); 445*0Sstevel@tonic-gate } else { 446*0Sstevel@tonic-gate temporarily_use_uid(pw); 447*0Sstevel@tonic-gate host_status = check_host_in_hostfile(user_hostfile, 448*0Sstevel@tonic-gate host, key, found, NULL); 449*0Sstevel@tonic-gate restore_uid(); 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate xfree(user_hostfile); 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate key_free(found); 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 456*0Sstevel@tonic-gate "ok" : "not found", host); 457*0Sstevel@tonic-gate return host_status; 458*0Sstevel@tonic-gate } 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate /* 462*0Sstevel@tonic-gate * Check a given file for security. This is defined as all components 463*0Sstevel@tonic-gate * of the path to the file must be owned by either the owner of 464*0Sstevel@tonic-gate * of the file or root and no directories must be group or world writable. 465*0Sstevel@tonic-gate * 466*0Sstevel@tonic-gate * XXX Should any specific check be done for sym links ? 467*0Sstevel@tonic-gate * 468*0Sstevel@tonic-gate * Takes an open file descriptor, the file name, a uid and and 469*0Sstevel@tonic-gate * error buffer plus max size as arguments. 470*0Sstevel@tonic-gate * 471*0Sstevel@tonic-gate * Returns 0 on success and -1 on failure 472*0Sstevel@tonic-gate */ 473*0Sstevel@tonic-gate int 474*0Sstevel@tonic-gate secure_filename(FILE *f, const char *file, struct passwd *pw, 475*0Sstevel@tonic-gate char *err, size_t errlen) 476*0Sstevel@tonic-gate { 477*0Sstevel@tonic-gate uid_t uid; 478*0Sstevel@tonic-gate char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 479*0Sstevel@tonic-gate char *cp; 480*0Sstevel@tonic-gate struct stat st; 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate if (pw == NULL) 483*0Sstevel@tonic-gate return 0; 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate uid = pw->pw_uid; 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gate if (realpath(file, buf) == NULL) { 488*0Sstevel@tonic-gate snprintf(err, errlen, "realpath %s failed: %s", file, 489*0Sstevel@tonic-gate strerror(errno)); 490*0Sstevel@tonic-gate return -1; 491*0Sstevel@tonic-gate } 492*0Sstevel@tonic-gate if (realpath(pw->pw_dir, homedir) == NULL) { 493*0Sstevel@tonic-gate snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir, 494*0Sstevel@tonic-gate strerror(errno)); 495*0Sstevel@tonic-gate return -1; 496*0Sstevel@tonic-gate } 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate /* check the open file to avoid races */ 499*0Sstevel@tonic-gate if (fstat(fileno(f), &st) < 0 || 500*0Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 501*0Sstevel@tonic-gate (st.st_mode & 022) != 0) { 502*0Sstevel@tonic-gate snprintf(err, errlen, "bad ownership or modes for file %s", 503*0Sstevel@tonic-gate buf); 504*0Sstevel@tonic-gate return -1; 505*0Sstevel@tonic-gate } 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate /* for each component of the canonical path, walking upwards */ 508*0Sstevel@tonic-gate for (;;) { 509*0Sstevel@tonic-gate if ((cp = dirname(buf)) == NULL) { 510*0Sstevel@tonic-gate snprintf(err, errlen, "dirname() failed"); 511*0Sstevel@tonic-gate return -1; 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate strlcpy(buf, cp, sizeof(buf)); 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate debug3("secure_filename: checking '%s'", buf); 516*0Sstevel@tonic-gate if (stat(buf, &st) < 0 || 517*0Sstevel@tonic-gate (st.st_uid != 0 && st.st_uid != uid) || 518*0Sstevel@tonic-gate (st.st_mode & 022) != 0) { 519*0Sstevel@tonic-gate snprintf(err, errlen, 520*0Sstevel@tonic-gate "bad ownership or modes for directory %s", buf); 521*0Sstevel@tonic-gate return -1; 522*0Sstevel@tonic-gate } 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate /* If are passed the homedir then we can stop */ 525*0Sstevel@tonic-gate if (strcmp(homedir, buf) == 0) { 526*0Sstevel@tonic-gate debug3("secure_filename: terminating check at '%s'", 527*0Sstevel@tonic-gate buf); 528*0Sstevel@tonic-gate break; 529*0Sstevel@tonic-gate } 530*0Sstevel@tonic-gate /* 531*0Sstevel@tonic-gate * dirname should always complete with a "/" path, 532*0Sstevel@tonic-gate * but we can be paranoid and check for "." too 533*0Sstevel@tonic-gate */ 534*0Sstevel@tonic-gate if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 535*0Sstevel@tonic-gate break; 536*0Sstevel@tonic-gate } 537*0Sstevel@tonic-gate return 0; 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate struct passwd * 541*0Sstevel@tonic-gate getpwnamallow(const char *user) 542*0Sstevel@tonic-gate { 543*0Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 544*0Sstevel@tonic-gate extern login_cap_t *lc; 545*0Sstevel@tonic-gate #ifdef BSD_AUTH 546*0Sstevel@tonic-gate auth_session_t *as; 547*0Sstevel@tonic-gate #endif 548*0Sstevel@tonic-gate #endif 549*0Sstevel@tonic-gate struct passwd *pw; 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate if (user == NULL || *user == '\0') 552*0Sstevel@tonic-gate return (NULL); /* implicit user, will be set later */ 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate pw = getpwnam(user); 555*0Sstevel@tonic-gate if (pw == NULL) { 556*0Sstevel@tonic-gate log("Illegal user %.100s from %.100s", 557*0Sstevel@tonic-gate user, get_remote_ipaddr()); 558*0Sstevel@tonic-gate return (NULL); 559*0Sstevel@tonic-gate } 560*0Sstevel@tonic-gate if (!allowed_user(pw)) 561*0Sstevel@tonic-gate return (NULL); 562*0Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 563*0Sstevel@tonic-gate if ((lc = login_getclass(pw->pw_class)) == NULL) { 564*0Sstevel@tonic-gate debug("unable to get login class: %s", user); 565*0Sstevel@tonic-gate return (NULL); 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate #ifdef BSD_AUTH 568*0Sstevel@tonic-gate if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 569*0Sstevel@tonic-gate auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 570*0Sstevel@tonic-gate debug("Approval failure for %s", user); 571*0Sstevel@tonic-gate pw = NULL; 572*0Sstevel@tonic-gate } 573*0Sstevel@tonic-gate if (as != NULL) 574*0Sstevel@tonic-gate auth_close(as); 575*0Sstevel@tonic-gate #endif 576*0Sstevel@tonic-gate #endif 577*0Sstevel@tonic-gate if (pw != NULL) 578*0Sstevel@tonic-gate return (pwcopy(pw)); 579*0Sstevel@tonic-gate return (NULL); 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate 582*0Sstevel@tonic-gate void 583*0Sstevel@tonic-gate auth_debug_add(const char *fmt,...) 584*0Sstevel@tonic-gate { 585*0Sstevel@tonic-gate char buf[1024]; 586*0Sstevel@tonic-gate va_list args; 587*0Sstevel@tonic-gate 588*0Sstevel@tonic-gate if (!auth_debug_init) 589*0Sstevel@tonic-gate return; 590*0Sstevel@tonic-gate 591*0Sstevel@tonic-gate va_start(args, fmt); 592*0Sstevel@tonic-gate vsnprintf(buf, sizeof(buf), fmt, args); 593*0Sstevel@tonic-gate va_end(args); 594*0Sstevel@tonic-gate buffer_put_cstring(&auth_debug, buf); 595*0Sstevel@tonic-gate } 596*0Sstevel@tonic-gate 597*0Sstevel@tonic-gate void 598*0Sstevel@tonic-gate auth_debug_send(void) 599*0Sstevel@tonic-gate { 600*0Sstevel@tonic-gate char *msg; 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate if (!auth_debug_init) 603*0Sstevel@tonic-gate return; 604*0Sstevel@tonic-gate while (buffer_len(&auth_debug)) { 605*0Sstevel@tonic-gate msg = buffer_get_string(&auth_debug, NULL); 606*0Sstevel@tonic-gate packet_send_debug("%s", msg); 607*0Sstevel@tonic-gate xfree(msg); 608*0Sstevel@tonic-gate } 609*0Sstevel@tonic-gate } 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate void 612*0Sstevel@tonic-gate auth_debug_reset(void) 613*0Sstevel@tonic-gate { 614*0Sstevel@tonic-gate if (auth_debug_init) 615*0Sstevel@tonic-gate buffer_clear(&auth_debug); 616*0Sstevel@tonic-gate else { 617*0Sstevel@tonic-gate buffer_init(&auth_debug); 618*0Sstevel@tonic-gate auth_debug_init = 1; 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate } 621