1*ee116499SAntonio Huete Jimenez /* $OpenBSD: auth.c,v 1.158 2022/06/03 04:47:21 djm Exp $ */ 218de8d7fSPeter Avalos /* 318de8d7fSPeter Avalos * Copyright (c) 2000 Markus Friedl. All rights reserved. 418de8d7fSPeter Avalos * 518de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without 618de8d7fSPeter Avalos * modification, are permitted provided that the following conditions 718de8d7fSPeter Avalos * are met: 818de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright 918de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer. 1018de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright 1118de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the 1218de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution. 1318de8d7fSPeter Avalos * 1418de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1518de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1618de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1718de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1818de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1918de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2018de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2118de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2218de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2318de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2418de8d7fSPeter Avalos */ 2518de8d7fSPeter Avalos 2618de8d7fSPeter Avalos #include "includes.h" 2718de8d7fSPeter Avalos 2818de8d7fSPeter Avalos #include <sys/types.h> 2918de8d7fSPeter Avalos #include <sys/stat.h> 30e9778795SPeter Avalos #include <sys/socket.h> 31664f4763Szrj #include <sys/wait.h> 3218de8d7fSPeter Avalos 3318de8d7fSPeter Avalos #include <netinet/in.h> 3418de8d7fSPeter Avalos 350cbfa66cSDaniel Fojt #include <stdlib.h> 3618de8d7fSPeter Avalos #include <errno.h> 3718de8d7fSPeter Avalos #include <fcntl.h> 3818de8d7fSPeter Avalos #ifdef HAVE_PATHS_H 3918de8d7fSPeter Avalos # include <paths.h> 4018de8d7fSPeter Avalos #endif 4118de8d7fSPeter Avalos #include <pwd.h> 4218de8d7fSPeter Avalos #ifdef HAVE_LOGIN_H 4318de8d7fSPeter Avalos #include <login.h> 4418de8d7fSPeter Avalos #endif 4518de8d7fSPeter Avalos #ifdef USE_SHADOW 4618de8d7fSPeter Avalos #include <shadow.h> 4718de8d7fSPeter Avalos #endif 4818de8d7fSPeter Avalos #include <stdarg.h> 4918de8d7fSPeter Avalos #include <stdio.h> 5018de8d7fSPeter Avalos #include <string.h> 5118de8d7fSPeter Avalos #include <unistd.h> 52e9778795SPeter Avalos #include <limits.h> 53e9778795SPeter Avalos #include <netdb.h> 54664f4763Szrj #include <time.h> 5518de8d7fSPeter Avalos 5618de8d7fSPeter Avalos #include "xmalloc.h" 5718de8d7fSPeter Avalos #include "match.h" 5818de8d7fSPeter Avalos #include "groupaccess.h" 5918de8d7fSPeter Avalos #include "log.h" 60664f4763Szrj #include "sshbuf.h" 6136e94dc5SPeter Avalos #include "misc.h" 6218de8d7fSPeter Avalos #include "servconf.h" 63664f4763Szrj #include "sshkey.h" 6418de8d7fSPeter Avalos #include "hostfile.h" 6518de8d7fSPeter Avalos #include "auth.h" 6618de8d7fSPeter Avalos #include "auth-options.h" 6718de8d7fSPeter Avalos #include "canohost.h" 6818de8d7fSPeter Avalos #include "uidswap.h" 6918de8d7fSPeter Avalos #include "packet.h" 7018de8d7fSPeter Avalos #include "loginrec.h" 7118de8d7fSPeter Avalos #ifdef GSSAPI 7218de8d7fSPeter Avalos #include "ssh-gss.h" 7318de8d7fSPeter Avalos #endif 74856ea928SPeter Avalos #include "authfile.h" 7518de8d7fSPeter Avalos #include "monitor_wrap.h" 76e9778795SPeter Avalos #include "ssherr.h" 7736e94dc5SPeter Avalos #include "compat.h" 78664f4763Szrj #include "channels.h" 7918de8d7fSPeter Avalos 8018de8d7fSPeter Avalos /* import */ 8118de8d7fSPeter Avalos extern ServerOptions options; 820cbfa66cSDaniel Fojt extern struct include_list includes; 8318de8d7fSPeter Avalos extern int use_privsep; 84664f4763Szrj extern struct sshbuf *loginmsg; 8518de8d7fSPeter Avalos extern struct passwd *privsep_pw; 86664f4763Szrj extern struct sshauthopt *auth_opts; 8718de8d7fSPeter Avalos 8818de8d7fSPeter Avalos /* Debugging messages */ 89664f4763Szrj static struct sshbuf *auth_debug; 9018de8d7fSPeter Avalos 9118de8d7fSPeter Avalos /* 9218de8d7fSPeter Avalos * Check if the user is allowed to log in via ssh. If user is listed 9318de8d7fSPeter Avalos * in DenyUsers or one of user's groups is listed in DenyGroups, false 9418de8d7fSPeter Avalos * will be returned. If AllowUsers isn't empty and user isn't listed 9518de8d7fSPeter Avalos * there, or if AllowGroups isn't empty and one of user's groups isn't 9618de8d7fSPeter Avalos * listed there, false will be returned. 9718de8d7fSPeter Avalos * If the user's shell is not executable, false will be returned. 9818de8d7fSPeter Avalos * Otherwise true is returned. 9918de8d7fSPeter Avalos */ 10018de8d7fSPeter Avalos int 101664f4763Szrj allowed_user(struct ssh *ssh, struct passwd * pw) 10218de8d7fSPeter Avalos { 10318de8d7fSPeter Avalos struct stat st; 104*ee116499SAntonio Huete Jimenez const char *hostname = NULL, *ipaddr = NULL; 10518de8d7fSPeter Avalos u_int i; 106ce74bacaSMatthew Dillon int r; 10718de8d7fSPeter Avalos 10818de8d7fSPeter Avalos /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 10918de8d7fSPeter Avalos if (!pw || !pw->pw_name) 11018de8d7fSPeter Avalos return 0; 11118de8d7fSPeter Avalos 112*ee116499SAntonio Huete Jimenez if (!options.use_pam && platform_locked_account(pw)) { 11318de8d7fSPeter Avalos logit("User %.100s not allowed because account is locked", 11418de8d7fSPeter Avalos pw->pw_name); 11518de8d7fSPeter Avalos return 0; 11618de8d7fSPeter Avalos } 11718de8d7fSPeter Avalos 11818de8d7fSPeter Avalos /* 119856ea928SPeter Avalos * Deny if shell does not exist or is not executable unless we 120856ea928SPeter Avalos * are chrooting. 12118de8d7fSPeter Avalos */ 122856ea928SPeter Avalos if (options.chroot_directory == NULL || 123856ea928SPeter Avalos strcasecmp(options.chroot_directory, "none") == 0) { 124856ea928SPeter Avalos char *shell = xstrdup((pw->pw_shell[0] == '\0') ? 125856ea928SPeter Avalos _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */ 12618de8d7fSPeter Avalos 1270cbfa66cSDaniel Fojt if (stat(shell, &st) == -1) { 128856ea928SPeter Avalos logit("User %.100s not allowed because shell %.100s " 129856ea928SPeter Avalos "does not exist", pw->pw_name, shell); 13036e94dc5SPeter Avalos free(shell); 13118de8d7fSPeter Avalos return 0; 13218de8d7fSPeter Avalos } 13318de8d7fSPeter Avalos if (S_ISREG(st.st_mode) == 0 || 13418de8d7fSPeter Avalos (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 135856ea928SPeter Avalos logit("User %.100s not allowed because shell %.100s " 136856ea928SPeter Avalos "is not executable", pw->pw_name, shell); 13736e94dc5SPeter Avalos free(shell); 13818de8d7fSPeter Avalos return 0; 13918de8d7fSPeter Avalos } 14036e94dc5SPeter Avalos free(shell); 141856ea928SPeter Avalos } 14218de8d7fSPeter Avalos 14318de8d7fSPeter Avalos if (options.num_deny_users > 0 || options.num_allow_users > 0 || 14418de8d7fSPeter Avalos options.num_deny_groups > 0 || options.num_allow_groups > 0) { 145e9778795SPeter Avalos hostname = auth_get_canonical_hostname(ssh, options.use_dns); 146e9778795SPeter Avalos ipaddr = ssh_remote_ipaddr(ssh); 14718de8d7fSPeter Avalos } 14818de8d7fSPeter Avalos 14918de8d7fSPeter Avalos /* Return false if user is listed in DenyUsers */ 15018de8d7fSPeter Avalos if (options.num_deny_users > 0) { 151ce74bacaSMatthew Dillon for (i = 0; i < options.num_deny_users; i++) { 152ce74bacaSMatthew Dillon r = match_user(pw->pw_name, hostname, ipaddr, 153ce74bacaSMatthew Dillon options.deny_users[i]); 154ce74bacaSMatthew Dillon if (r < 0) { 155ce74bacaSMatthew Dillon fatal("Invalid DenyUsers pattern \"%.100s\"", 156ce74bacaSMatthew Dillon options.deny_users[i]); 157ce74bacaSMatthew Dillon } else if (r != 0) { 15818de8d7fSPeter Avalos logit("User %.100s from %.100s not allowed " 15918de8d7fSPeter Avalos "because listed in DenyUsers", 16018de8d7fSPeter Avalos pw->pw_name, hostname); 16118de8d7fSPeter Avalos return 0; 16218de8d7fSPeter Avalos } 16318de8d7fSPeter Avalos } 164ce74bacaSMatthew Dillon } 16518de8d7fSPeter Avalos /* Return false if AllowUsers isn't empty and user isn't listed there */ 16618de8d7fSPeter Avalos if (options.num_allow_users > 0) { 167ce74bacaSMatthew Dillon for (i = 0; i < options.num_allow_users; i++) { 168ce74bacaSMatthew Dillon r = match_user(pw->pw_name, hostname, ipaddr, 169ce74bacaSMatthew Dillon options.allow_users[i]); 170ce74bacaSMatthew Dillon if (r < 0) { 171ce74bacaSMatthew Dillon fatal("Invalid AllowUsers pattern \"%.100s\"", 172ce74bacaSMatthew Dillon options.allow_users[i]); 173ce74bacaSMatthew Dillon } else if (r == 1) 17418de8d7fSPeter Avalos break; 175ce74bacaSMatthew Dillon } 17618de8d7fSPeter Avalos /* i < options.num_allow_users iff we break for loop */ 17718de8d7fSPeter Avalos if (i >= options.num_allow_users) { 17818de8d7fSPeter Avalos logit("User %.100s from %.100s not allowed because " 17918de8d7fSPeter Avalos "not listed in AllowUsers", pw->pw_name, hostname); 18018de8d7fSPeter Avalos return 0; 18118de8d7fSPeter Avalos } 18218de8d7fSPeter Avalos } 18318de8d7fSPeter Avalos if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 18418de8d7fSPeter Avalos /* Get the user's group access list (primary and supplementary) */ 18518de8d7fSPeter Avalos if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 18618de8d7fSPeter Avalos logit("User %.100s from %.100s not allowed because " 18718de8d7fSPeter Avalos "not in any group", pw->pw_name, hostname); 18818de8d7fSPeter Avalos return 0; 18918de8d7fSPeter Avalos } 19018de8d7fSPeter Avalos 19118de8d7fSPeter Avalos /* Return false if one of user's groups is listed in DenyGroups */ 19218de8d7fSPeter Avalos if (options.num_deny_groups > 0) 19318de8d7fSPeter Avalos if (ga_match(options.deny_groups, 19418de8d7fSPeter Avalos options.num_deny_groups)) { 19518de8d7fSPeter Avalos ga_free(); 19618de8d7fSPeter Avalos logit("User %.100s from %.100s not allowed " 19718de8d7fSPeter Avalos "because a group is listed in DenyGroups", 19818de8d7fSPeter Avalos pw->pw_name, hostname); 19918de8d7fSPeter Avalos return 0; 20018de8d7fSPeter Avalos } 20118de8d7fSPeter Avalos /* 20218de8d7fSPeter Avalos * Return false if AllowGroups isn't empty and one of user's groups 20318de8d7fSPeter Avalos * isn't listed there 20418de8d7fSPeter Avalos */ 20518de8d7fSPeter Avalos if (options.num_allow_groups > 0) 20618de8d7fSPeter Avalos if (!ga_match(options.allow_groups, 20718de8d7fSPeter Avalos options.num_allow_groups)) { 20818de8d7fSPeter Avalos ga_free(); 20918de8d7fSPeter Avalos logit("User %.100s from %.100s not allowed " 21018de8d7fSPeter Avalos "because none of user's groups are listed " 21118de8d7fSPeter Avalos "in AllowGroups", pw->pw_name, hostname); 21218de8d7fSPeter Avalos return 0; 21318de8d7fSPeter Avalos } 21418de8d7fSPeter Avalos ga_free(); 21518de8d7fSPeter Avalos } 21618de8d7fSPeter Avalos 21718de8d7fSPeter Avalos #ifdef CUSTOM_SYS_AUTH_ALLOWED_USER 218664f4763Szrj if (!sys_auth_allowed_user(pw, loginmsg)) 21918de8d7fSPeter Avalos return 0; 22018de8d7fSPeter Avalos #endif 22118de8d7fSPeter Avalos 22218de8d7fSPeter Avalos /* We found no reason not to let this user try to log on... */ 22318de8d7fSPeter Avalos return 1; 22418de8d7fSPeter Avalos } 22518de8d7fSPeter Avalos 226ce74bacaSMatthew Dillon /* 227ce74bacaSMatthew Dillon * Formats any key left in authctxt->auth_method_key for inclusion in 228ce74bacaSMatthew Dillon * auth_log()'s message. Also includes authxtct->auth_method_info if present. 229ce74bacaSMatthew Dillon */ 230ce74bacaSMatthew Dillon static char * 231ce74bacaSMatthew Dillon format_method_key(Authctxt *authctxt) 23236e94dc5SPeter Avalos { 233ce74bacaSMatthew Dillon const struct sshkey *key = authctxt->auth_method_key; 234ce74bacaSMatthew Dillon const char *methinfo = authctxt->auth_method_info; 235664f4763Szrj char *fp, *cafp, *ret = NULL; 23636e94dc5SPeter Avalos 237ce74bacaSMatthew Dillon if (key == NULL) 238ce74bacaSMatthew Dillon return NULL; 23936e94dc5SPeter Avalos 240664f4763Szrj if (sshkey_is_cert(key)) { 241664f4763Szrj fp = sshkey_fingerprint(key, 242ce74bacaSMatthew Dillon options.fingerprint_hash, SSH_FP_DEFAULT); 243664f4763Szrj cafp = sshkey_fingerprint(key->cert->signature_key, 244664f4763Szrj options.fingerprint_hash, SSH_FP_DEFAULT); 245664f4763Szrj xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s", 246664f4763Szrj sshkey_type(key), fp == NULL ? "(null)" : fp, 247664f4763Szrj key->cert->key_id, 248ce74bacaSMatthew Dillon (unsigned long long)key->cert->serial, 249ce74bacaSMatthew Dillon sshkey_type(key->cert->signature_key), 250664f4763Szrj cafp == NULL ? "(null)" : cafp, 251ce74bacaSMatthew Dillon methinfo == NULL ? "" : ", ", 252ce74bacaSMatthew Dillon methinfo == NULL ? "" : methinfo); 253ce74bacaSMatthew Dillon free(fp); 254664f4763Szrj free(cafp); 255ce74bacaSMatthew Dillon } else { 256ce74bacaSMatthew Dillon fp = sshkey_fingerprint(key, options.fingerprint_hash, 257ce74bacaSMatthew Dillon SSH_FP_DEFAULT); 258ce74bacaSMatthew Dillon xasprintf(&ret, "%s %s%s%s", sshkey_type(key), 259ce74bacaSMatthew Dillon fp == NULL ? "(null)" : fp, 260ce74bacaSMatthew Dillon methinfo == NULL ? "" : ", ", 261ce74bacaSMatthew Dillon methinfo == NULL ? "" : methinfo); 262ce74bacaSMatthew Dillon free(fp); 263ce74bacaSMatthew Dillon } 264ce74bacaSMatthew Dillon return ret; 26536e94dc5SPeter Avalos } 26636e94dc5SPeter Avalos 26736e94dc5SPeter Avalos void 268664f4763Szrj auth_log(struct ssh *ssh, int authenticated, int partial, 26936e94dc5SPeter Avalos const char *method, const char *submethod) 27018de8d7fSPeter Avalos { 271664f4763Szrj Authctxt *authctxt = (Authctxt *)ssh->authctxt; 272664f4763Szrj int level = SYSLOG_LEVEL_VERBOSE; 273ce74bacaSMatthew Dillon const char *authmsg; 274ce74bacaSMatthew Dillon char *extra = NULL; 27518de8d7fSPeter Avalos 27618de8d7fSPeter Avalos if (use_privsep && !mm_is_monitor() && !authctxt->postponed) 27718de8d7fSPeter Avalos return; 27818de8d7fSPeter Avalos 27918de8d7fSPeter Avalos /* Raise logging level */ 28018de8d7fSPeter Avalos if (authenticated == 1 || 28118de8d7fSPeter Avalos !authctxt->valid || 28218de8d7fSPeter Avalos authctxt->failures >= options.max_authtries / 2 || 28318de8d7fSPeter Avalos strcmp(method, "password") == 0) 284664f4763Szrj level = SYSLOG_LEVEL_INFO; 28518de8d7fSPeter Avalos 28618de8d7fSPeter Avalos if (authctxt->postponed) 28718de8d7fSPeter Avalos authmsg = "Postponed"; 28836e94dc5SPeter Avalos else if (partial) 28936e94dc5SPeter Avalos authmsg = "Partial"; 29018de8d7fSPeter Avalos else 29118de8d7fSPeter Avalos authmsg = authenticated ? "Accepted" : "Failed"; 29218de8d7fSPeter Avalos 293ce74bacaSMatthew Dillon if ((extra = format_method_key(authctxt)) == NULL) { 294ce74bacaSMatthew Dillon if (authctxt->auth_method_info != NULL) 295ce74bacaSMatthew Dillon extra = xstrdup(authctxt->auth_method_info); 296ce74bacaSMatthew Dillon } 297ce74bacaSMatthew Dillon 298664f4763Szrj do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s", 29918de8d7fSPeter Avalos authmsg, 30018de8d7fSPeter Avalos method, 30136e94dc5SPeter Avalos submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod, 30218de8d7fSPeter Avalos authctxt->valid ? "" : "invalid user ", 30318de8d7fSPeter Avalos authctxt->user, 304e9778795SPeter Avalos ssh_remote_ipaddr(ssh), 305e9778795SPeter Avalos ssh_remote_port(ssh), 306ce74bacaSMatthew Dillon extra != NULL ? ": " : "", 307ce74bacaSMatthew Dillon extra != NULL ? extra : ""); 308ce74bacaSMatthew Dillon 309ce74bacaSMatthew Dillon free(extra); 31018de8d7fSPeter Avalos 31150a69bb5SSascha Wildner #if defined(CUSTOM_FAILED_LOGIN) || defined(SSH_AUDIT_EVENTS) 31250a69bb5SSascha Wildner if (authenticated == 0 && !(authctxt->postponed || partial)) { 31350a69bb5SSascha Wildner /* Log failed login attempt */ 31418de8d7fSPeter Avalos # ifdef CUSTOM_FAILED_LOGIN 31550a69bb5SSascha Wildner if (strcmp(method, "password") == 0 || 31618de8d7fSPeter Avalos strncmp(method, "keyboard-interactive", 20) == 0 || 31750a69bb5SSascha Wildner strcmp(method, "challenge-response") == 0) 318664f4763Szrj record_failed_login(ssh, authctxt->user, 319e9778795SPeter Avalos auth_get_canonical_hostname(ssh, options.use_dns), "ssh"); 32050a69bb5SSascha Wildner # endif 32150a69bb5SSascha Wildner # ifdef SSH_AUDIT_EVENTS 32250a69bb5SSascha Wildner audit_event(ssh, audit_classify_auth(method)); 32350a69bb5SSascha Wildner # endif 32450a69bb5SSascha Wildner } 32550a69bb5SSascha Wildner #endif 32650a69bb5SSascha Wildner #if defined(CUSTOM_FAILED_LOGIN) && defined(WITH_AIXAUTHENTICATE) 32718de8d7fSPeter Avalos if (authenticated) 32818de8d7fSPeter Avalos sys_auth_record_login(authctxt->user, 329e9778795SPeter Avalos auth_get_canonical_hostname(ssh, options.use_dns), "ssh", 330664f4763Szrj loginmsg); 33118de8d7fSPeter Avalos #endif 33218de8d7fSPeter Avalos } 33318de8d7fSPeter Avalos 33436e94dc5SPeter Avalos void 335664f4763Szrj auth_maxtries_exceeded(struct ssh *ssh) 33636e94dc5SPeter Avalos { 337664f4763Szrj Authctxt *authctxt = (Authctxt *)ssh->authctxt; 338e9778795SPeter Avalos 339e9778795SPeter Avalos error("maximum authentication attempts exceeded for " 340ce74bacaSMatthew Dillon "%s%.100s from %.200s port %d ssh2", 34136e94dc5SPeter Avalos authctxt->valid ? "" : "invalid user ", 34236e94dc5SPeter Avalos authctxt->user, 343e9778795SPeter Avalos ssh_remote_ipaddr(ssh), 344ce74bacaSMatthew Dillon ssh_remote_port(ssh)); 345664f4763Szrj ssh_packet_disconnect(ssh, "Too many authentication failures"); 34636e94dc5SPeter Avalos /* NOTREACHED */ 34736e94dc5SPeter Avalos } 34836e94dc5SPeter Avalos 34918de8d7fSPeter Avalos /* 35018de8d7fSPeter Avalos * Check whether root logins are disallowed. 35118de8d7fSPeter Avalos */ 35218de8d7fSPeter Avalos int 353664f4763Szrj auth_root_allowed(struct ssh *ssh, const char *method) 35418de8d7fSPeter Avalos { 35518de8d7fSPeter Avalos switch (options.permit_root_login) { 35618de8d7fSPeter Avalos case PERMIT_YES: 35718de8d7fSPeter Avalos return 1; 35818de8d7fSPeter Avalos case PERMIT_NO_PASSWD: 359e9778795SPeter Avalos if (strcmp(method, "publickey") == 0 || 360e9778795SPeter Avalos strcmp(method, "hostbased") == 0 || 361e9778795SPeter Avalos strcmp(method, "gssapi-with-mic") == 0) 36218de8d7fSPeter Avalos return 1; 36318de8d7fSPeter Avalos break; 36418de8d7fSPeter Avalos case PERMIT_FORCED_ONLY: 365664f4763Szrj if (auth_opts->force_command != NULL) { 36618de8d7fSPeter Avalos logit("Root login accepted for forced command."); 36718de8d7fSPeter Avalos return 1; 36818de8d7fSPeter Avalos } 36918de8d7fSPeter Avalos break; 37018de8d7fSPeter Avalos } 371e9778795SPeter Avalos logit("ROOT LOGIN REFUSED FROM %.200s port %d", 372e9778795SPeter Avalos ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 37318de8d7fSPeter Avalos return 0; 37418de8d7fSPeter Avalos } 37518de8d7fSPeter Avalos 37618de8d7fSPeter Avalos 37718de8d7fSPeter Avalos /* 37818de8d7fSPeter Avalos * Given a template and a passwd structure, build a filename 37918de8d7fSPeter Avalos * by substituting % tokenised options. Currently, %% becomes '%', 38018de8d7fSPeter Avalos * %h becomes the home directory and %u the username. 38118de8d7fSPeter Avalos * 38218de8d7fSPeter Avalos * This returns a buffer allocated by xmalloc. 38318de8d7fSPeter Avalos */ 3841c188a7fSPeter Avalos char * 38518de8d7fSPeter Avalos expand_authorized_keys(const char *filename, struct passwd *pw) 38618de8d7fSPeter Avalos { 387664f4763Szrj char *file, uidstr[32], ret[PATH_MAX]; 38818de8d7fSPeter Avalos int i; 38918de8d7fSPeter Avalos 390664f4763Szrj snprintf(uidstr, sizeof(uidstr), "%llu", 391664f4763Szrj (unsigned long long)pw->pw_uid); 39218de8d7fSPeter Avalos file = percent_expand(filename, "h", pw->pw_dir, 393664f4763Szrj "u", pw->pw_name, "U", uidstr, (char *)NULL); 39418de8d7fSPeter Avalos 39518de8d7fSPeter Avalos /* 39618de8d7fSPeter Avalos * Ensure that filename starts anchored. If not, be backward 39718de8d7fSPeter Avalos * compatible and prepend the '%h/' 39818de8d7fSPeter Avalos */ 399664f4763Szrj if (path_absolute(file)) 40018de8d7fSPeter Avalos return (file); 40118de8d7fSPeter Avalos 40218de8d7fSPeter Avalos i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file); 40318de8d7fSPeter Avalos if (i < 0 || (size_t)i >= sizeof(ret)) 40418de8d7fSPeter Avalos fatal("expand_authorized_keys: path too long"); 40536e94dc5SPeter Avalos free(file); 40618de8d7fSPeter Avalos return (xstrdup(ret)); 40718de8d7fSPeter Avalos } 40818de8d7fSPeter Avalos 40918de8d7fSPeter Avalos char * 410856ea928SPeter Avalos authorized_principals_file(struct passwd *pw) 411856ea928SPeter Avalos { 412e9778795SPeter Avalos if (options.authorized_principals_file == NULL) 413856ea928SPeter Avalos return NULL; 414856ea928SPeter Avalos return expand_authorized_keys(options.authorized_principals_file, pw); 415856ea928SPeter Avalos } 416856ea928SPeter Avalos 41718de8d7fSPeter Avalos /* return ok if key exists in sysfile or userfile */ 41818de8d7fSPeter Avalos HostStatus 419ce74bacaSMatthew Dillon check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host, 42018de8d7fSPeter Avalos const char *sysfile, const char *userfile) 42118de8d7fSPeter Avalos { 42218de8d7fSPeter Avalos char *user_hostfile; 42318de8d7fSPeter Avalos struct stat st; 42418de8d7fSPeter Avalos HostStatus host_status; 4259f304aafSPeter Avalos struct hostkeys *hostkeys; 4269f304aafSPeter Avalos const struct hostkey_entry *found; 42718de8d7fSPeter Avalos 4289f304aafSPeter Avalos hostkeys = init_hostkeys(); 42950a69bb5SSascha Wildner load_hostkeys(hostkeys, host, sysfile, 0); 4309f304aafSPeter Avalos if (userfile != NULL) { 43118de8d7fSPeter Avalos user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 43218de8d7fSPeter Avalos if (options.strict_modes && 43318de8d7fSPeter Avalos (stat(user_hostfile, &st) == 0) && 43418de8d7fSPeter Avalos ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 43518de8d7fSPeter Avalos (st.st_mode & 022) != 0)) { 43618de8d7fSPeter Avalos logit("Authentication refused for %.100s: " 43718de8d7fSPeter Avalos "bad owner or modes for %.200s", 43818de8d7fSPeter Avalos pw->pw_name, user_hostfile); 439856ea928SPeter Avalos auth_debug_add("Ignored %.200s: bad ownership or modes", 440856ea928SPeter Avalos user_hostfile); 44118de8d7fSPeter Avalos } else { 44218de8d7fSPeter Avalos temporarily_use_uid(pw); 44350a69bb5SSascha Wildner load_hostkeys(hostkeys, host, user_hostfile, 0); 44418de8d7fSPeter Avalos restore_uid(); 44518de8d7fSPeter Avalos } 44636e94dc5SPeter Avalos free(user_hostfile); 44718de8d7fSPeter Avalos } 4489f304aafSPeter Avalos host_status = check_key_in_hostkeys(hostkeys, key, &found); 4499f304aafSPeter Avalos if (host_status == HOST_REVOKED) 4509f304aafSPeter Avalos error("WARNING: revoked key for %s attempted authentication", 4510cbfa66cSDaniel Fojt host); 4529f304aafSPeter Avalos else if (host_status == HOST_OK) 45350a69bb5SSascha Wildner debug_f("key for %s found at %s:%ld", 4549f304aafSPeter Avalos found->host, found->file, found->line); 4559f304aafSPeter Avalos else 45650a69bb5SSascha Wildner debug_f("key for host %s not found", host); 45718de8d7fSPeter Avalos 4589f304aafSPeter Avalos free_hostkeys(hostkeys); 4599f304aafSPeter Avalos 46018de8d7fSPeter Avalos return host_status; 46118de8d7fSPeter Avalos } 46218de8d7fSPeter Avalos 46318de8d7fSPeter Avalos struct passwd * 464664f4763Szrj getpwnamallow(struct ssh *ssh, const char *user) 46518de8d7fSPeter Avalos { 46618de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP 46718de8d7fSPeter Avalos extern login_cap_t *lc; 46818de8d7fSPeter Avalos #ifdef BSD_AUTH 46918de8d7fSPeter Avalos auth_session_t *as; 47018de8d7fSPeter Avalos #endif 47118de8d7fSPeter Avalos #endif 47218de8d7fSPeter Avalos struct passwd *pw; 473664f4763Szrj struct connection_info *ci; 47450a69bb5SSascha Wildner u_int i; 47518de8d7fSPeter Avalos 476664f4763Szrj ci = get_connection_info(ssh, 1, options.use_dns); 47799e85e0dSPeter Avalos ci->user = user; 4780cbfa66cSDaniel Fojt parse_server_match_config(&options, &includes, ci); 479ce74bacaSMatthew Dillon log_change_level(options.log_level); 48050a69bb5SSascha Wildner log_verbose_reset(); 48150a69bb5SSascha Wildner for (i = 0; i < options.num_log_verbose; i++) 48250a69bb5SSascha Wildner log_verbose_add(options.log_verbose[i]); 483ce74bacaSMatthew Dillon process_permitopen(ssh, &options); 48418de8d7fSPeter Avalos 485856ea928SPeter Avalos #if defined(_AIX) && defined(HAVE_SETAUTHDB) 486856ea928SPeter Avalos aix_setauthdb(user); 487856ea928SPeter Avalos #endif 488856ea928SPeter Avalos 48918de8d7fSPeter Avalos pw = getpwnam(user); 490856ea928SPeter Avalos 491856ea928SPeter Avalos #if defined(_AIX) && defined(HAVE_SETAUTHDB) 492856ea928SPeter Avalos aix_restoreauthdb(); 493856ea928SPeter Avalos #endif 49418de8d7fSPeter Avalos if (pw == NULL) { 495e9778795SPeter Avalos logit("Invalid user %.100s from %.100s port %d", 496e9778795SPeter Avalos user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 49718de8d7fSPeter Avalos #ifdef CUSTOM_FAILED_LOGIN 498664f4763Szrj record_failed_login(ssh, user, 499e9778795SPeter Avalos auth_get_canonical_hostname(ssh, options.use_dns), "ssh"); 50018de8d7fSPeter Avalos #endif 50118de8d7fSPeter Avalos #ifdef SSH_AUDIT_EVENTS 502664f4763Szrj audit_event(ssh, SSH_INVALID_USER); 50318de8d7fSPeter Avalos #endif /* SSH_AUDIT_EVENTS */ 50418de8d7fSPeter Avalos return (NULL); 50518de8d7fSPeter Avalos } 506664f4763Szrj if (!allowed_user(ssh, pw)) 50718de8d7fSPeter Avalos return (NULL); 50818de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP 50950a69bb5SSascha Wildner if ((lc = login_getpwclass(pw)) == NULL) { 51018de8d7fSPeter Avalos debug("unable to get login class: %s", user); 51118de8d7fSPeter Avalos return (NULL); 51218de8d7fSPeter Avalos } 51318de8d7fSPeter Avalos #ifdef BSD_AUTH 51418de8d7fSPeter Avalos if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 51518de8d7fSPeter Avalos auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 51618de8d7fSPeter Avalos debug("Approval failure for %s", user); 51718de8d7fSPeter Avalos pw = NULL; 51818de8d7fSPeter Avalos } 51918de8d7fSPeter Avalos if (as != NULL) 52018de8d7fSPeter Avalos auth_close(as); 52118de8d7fSPeter Avalos #endif 52218de8d7fSPeter Avalos #endif 52318de8d7fSPeter Avalos if (pw != NULL) 52418de8d7fSPeter Avalos return (pwcopy(pw)); 52518de8d7fSPeter Avalos return (NULL); 52618de8d7fSPeter Avalos } 52718de8d7fSPeter Avalos 528856ea928SPeter Avalos /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */ 529856ea928SPeter Avalos int 530ce74bacaSMatthew Dillon auth_key_is_revoked(struct sshkey *key) 531856ea928SPeter Avalos { 532e9778795SPeter Avalos char *fp = NULL; 533e9778795SPeter Avalos int r; 534856ea928SPeter Avalos 535856ea928SPeter Avalos if (options.revoked_keys_file == NULL) 536856ea928SPeter Avalos return 0; 537e9778795SPeter Avalos if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 538e9778795SPeter Avalos SSH_FP_DEFAULT)) == NULL) { 539e9778795SPeter Avalos r = SSH_ERR_ALLOC_FAIL; 54050a69bb5SSascha Wildner error_fr(r, "fingerprint key"); 541e9778795SPeter Avalos goto out; 542e9778795SPeter Avalos } 543e9778795SPeter Avalos 544e9778795SPeter Avalos r = sshkey_check_revoked(key, options.revoked_keys_file); 545e9778795SPeter Avalos switch (r) { 54636e94dc5SPeter Avalos case 0: 547e9778795SPeter Avalos break; /* not revoked */ 548e9778795SPeter Avalos case SSH_ERR_KEY_REVOKED: 549e9778795SPeter Avalos error("Authentication key %s %s revoked by file %s", 550e9778795SPeter Avalos sshkey_type(key), fp, options.revoked_keys_file); 551e9778795SPeter Avalos goto out; 55236e94dc5SPeter Avalos default: 55350a69bb5SSascha Wildner error_r(r, "Error checking authentication key %s %s in " 55450a69bb5SSascha Wildner "revoked keys file %s", sshkey_type(key), fp, 55550a69bb5SSascha Wildner options.revoked_keys_file); 556e9778795SPeter Avalos goto out; 55736e94dc5SPeter Avalos } 558e9778795SPeter Avalos 559e9778795SPeter Avalos /* Success */ 560e9778795SPeter Avalos r = 0; 561e9778795SPeter Avalos 562e9778795SPeter Avalos out: 563e9778795SPeter Avalos free(fp); 564e9778795SPeter Avalos return r == 0 ? 0 : 1; 565856ea928SPeter Avalos } 566856ea928SPeter Avalos 56718de8d7fSPeter Avalos void 56818de8d7fSPeter Avalos auth_debug_add(const char *fmt,...) 56918de8d7fSPeter Avalos { 57018de8d7fSPeter Avalos char buf[1024]; 57118de8d7fSPeter Avalos va_list args; 572664f4763Szrj int r; 57318de8d7fSPeter Avalos 574664f4763Szrj if (auth_debug == NULL) 57518de8d7fSPeter Avalos return; 57618de8d7fSPeter Avalos 57718de8d7fSPeter Avalos va_start(args, fmt); 57818de8d7fSPeter Avalos vsnprintf(buf, sizeof(buf), fmt, args); 57918de8d7fSPeter Avalos va_end(args); 580664f4763Szrj if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0) 58150a69bb5SSascha Wildner fatal_fr(r, "sshbuf_put_cstring"); 58218de8d7fSPeter Avalos } 58318de8d7fSPeter Avalos 58418de8d7fSPeter Avalos void 585664f4763Szrj auth_debug_send(struct ssh *ssh) 58618de8d7fSPeter Avalos { 58718de8d7fSPeter Avalos char *msg; 588664f4763Szrj int r; 58918de8d7fSPeter Avalos 590664f4763Szrj if (auth_debug == NULL) 59118de8d7fSPeter Avalos return; 592664f4763Szrj while (sshbuf_len(auth_debug) != 0) { 593664f4763Szrj if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0) 59450a69bb5SSascha Wildner fatal_fr(r, "sshbuf_get_cstring"); 595664f4763Szrj ssh_packet_send_debug(ssh, "%s", msg); 59636e94dc5SPeter Avalos free(msg); 59718de8d7fSPeter Avalos } 59818de8d7fSPeter Avalos } 59918de8d7fSPeter Avalos 60018de8d7fSPeter Avalos void 60118de8d7fSPeter Avalos auth_debug_reset(void) 60218de8d7fSPeter Avalos { 603664f4763Szrj if (auth_debug != NULL) 604664f4763Szrj sshbuf_reset(auth_debug); 605664f4763Szrj else if ((auth_debug = sshbuf_new()) == NULL) 60650a69bb5SSascha Wildner fatal_f("sshbuf_new failed"); 60718de8d7fSPeter Avalos } 60818de8d7fSPeter Avalos 60918de8d7fSPeter Avalos struct passwd * 61018de8d7fSPeter Avalos fakepw(void) 61118de8d7fSPeter Avalos { 612*ee116499SAntonio Huete Jimenez static int done = 0; 61318de8d7fSPeter Avalos static struct passwd fake; 614*ee116499SAntonio Huete Jimenez const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ" 615*ee116499SAntonio Huete Jimenez "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */ 616*ee116499SAntonio Huete Jimenez char *cp; 617*ee116499SAntonio Huete Jimenez 618*ee116499SAntonio Huete Jimenez if (done) 619*ee116499SAntonio Huete Jimenez return (&fake); 62018de8d7fSPeter Avalos 62118de8d7fSPeter Avalos memset(&fake, 0, sizeof(fake)); 62218de8d7fSPeter Avalos fake.pw_name = "NOUSER"; 623*ee116499SAntonio Huete Jimenez fake.pw_passwd = xstrdup("$2a$10$" 624*ee116499SAntonio Huete Jimenez "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 625*ee116499SAntonio Huete Jimenez for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++) 626*ee116499SAntonio Huete Jimenez *cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)]; 62736e94dc5SPeter Avalos #ifdef HAVE_STRUCT_PASSWD_PW_GECOS 62818de8d7fSPeter Avalos fake.pw_gecos = "NOUSER"; 62936e94dc5SPeter Avalos #endif 63018de8d7fSPeter Avalos fake.pw_uid = privsep_pw == NULL ? (uid_t)-1 : privsep_pw->pw_uid; 63118de8d7fSPeter Avalos fake.pw_gid = privsep_pw == NULL ? (gid_t)-1 : privsep_pw->pw_gid; 63236e94dc5SPeter Avalos #ifdef HAVE_STRUCT_PASSWD_PW_CLASS 63318de8d7fSPeter Avalos fake.pw_class = ""; 63418de8d7fSPeter Avalos #endif 63518de8d7fSPeter Avalos fake.pw_dir = "/nonexist"; 63618de8d7fSPeter Avalos fake.pw_shell = "/nonexist"; 637*ee116499SAntonio Huete Jimenez done = 1; 63818de8d7fSPeter Avalos 63918de8d7fSPeter Avalos return (&fake); 64018de8d7fSPeter Avalos } 641e9778795SPeter Avalos 642e9778795SPeter Avalos /* 643e9778795SPeter Avalos * Returns the remote DNS hostname as a string. The returned string must not 644e9778795SPeter Avalos * be freed. NB. this will usually trigger a DNS query the first time it is 645e9778795SPeter Avalos * called. 646e9778795SPeter Avalos * This function does additional checks on the hostname to mitigate some 64750a69bb5SSascha Wildner * attacks on based on conflation of hostnames and IP addresses. 648e9778795SPeter Avalos */ 649e9778795SPeter Avalos 650e9778795SPeter Avalos static char * 651e9778795SPeter Avalos remote_hostname(struct ssh *ssh) 652e9778795SPeter Avalos { 653e9778795SPeter Avalos struct sockaddr_storage from; 654e9778795SPeter Avalos socklen_t fromlen; 655e9778795SPeter Avalos struct addrinfo hints, *ai, *aitop; 656e9778795SPeter Avalos char name[NI_MAXHOST], ntop2[NI_MAXHOST]; 657e9778795SPeter Avalos const char *ntop = ssh_remote_ipaddr(ssh); 658e9778795SPeter Avalos 659e9778795SPeter Avalos /* Get IP address of client. */ 660e9778795SPeter Avalos fromlen = sizeof(from); 661e9778795SPeter Avalos memset(&from, 0, sizeof(from)); 662e9778795SPeter Avalos if (getpeername(ssh_packet_get_connection_in(ssh), 6630cbfa66cSDaniel Fojt (struct sockaddr *)&from, &fromlen) == -1) { 664e9778795SPeter Avalos debug("getpeername failed: %.100s", strerror(errno)); 6650cbfa66cSDaniel Fojt return xstrdup(ntop); 666e9778795SPeter Avalos } 667e9778795SPeter Avalos 668e9778795SPeter Avalos ipv64_normalise_mapped(&from, &fromlen); 669e9778795SPeter Avalos if (from.ss_family == AF_INET6) 670e9778795SPeter Avalos fromlen = sizeof(struct sockaddr_in6); 671e9778795SPeter Avalos 672e9778795SPeter Avalos debug3("Trying to reverse map address %.100s.", ntop); 673e9778795SPeter Avalos /* Map the IP address to a host name. */ 674e9778795SPeter Avalos if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), 675e9778795SPeter Avalos NULL, 0, NI_NAMEREQD) != 0) { 676e9778795SPeter Avalos /* Host name not found. Use ip address. */ 6770cbfa66cSDaniel Fojt return xstrdup(ntop); 678e9778795SPeter Avalos } 679e9778795SPeter Avalos 680e9778795SPeter Avalos /* 681e9778795SPeter Avalos * if reverse lookup result looks like a numeric hostname, 682e9778795SPeter Avalos * someone is trying to trick us by PTR record like following: 683e9778795SPeter Avalos * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5 684e9778795SPeter Avalos */ 685e9778795SPeter Avalos memset(&hints, 0, sizeof(hints)); 686e9778795SPeter Avalos hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 687e9778795SPeter Avalos hints.ai_flags = AI_NUMERICHOST; 688e9778795SPeter Avalos if (getaddrinfo(name, NULL, &hints, &ai) == 0) { 689e9778795SPeter Avalos logit("Nasty PTR record \"%s\" is set up for %s, ignoring", 690e9778795SPeter Avalos name, ntop); 691e9778795SPeter Avalos freeaddrinfo(ai); 6920cbfa66cSDaniel Fojt return xstrdup(ntop); 693e9778795SPeter Avalos } 694e9778795SPeter Avalos 695e9778795SPeter Avalos /* Names are stored in lowercase. */ 696e9778795SPeter Avalos lowercase(name); 697e9778795SPeter Avalos 698e9778795SPeter Avalos /* 699e9778795SPeter Avalos * Map it back to an IP address and check that the given 700e9778795SPeter Avalos * address actually is an address of this host. This is 701e9778795SPeter Avalos * necessary because anyone with access to a name server can 702e9778795SPeter Avalos * define arbitrary names for an IP address. Mapping from 703e9778795SPeter Avalos * name to IP address can be trusted better (but can still be 704e9778795SPeter Avalos * fooled if the intruder has access to the name server of 705e9778795SPeter Avalos * the domain). 706e9778795SPeter Avalos */ 707e9778795SPeter Avalos memset(&hints, 0, sizeof(hints)); 708e9778795SPeter Avalos hints.ai_family = from.ss_family; 709e9778795SPeter Avalos hints.ai_socktype = SOCK_STREAM; 710e9778795SPeter Avalos if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { 711e9778795SPeter Avalos logit("reverse mapping checking getaddrinfo for %.700s " 712e9778795SPeter Avalos "[%s] failed.", name, ntop); 7130cbfa66cSDaniel Fojt return xstrdup(ntop); 714e9778795SPeter Avalos } 715e9778795SPeter Avalos /* Look for the address from the list of addresses. */ 716e9778795SPeter Avalos for (ai = aitop; ai; ai = ai->ai_next) { 717e9778795SPeter Avalos if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2, 718e9778795SPeter Avalos sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 && 719e9778795SPeter Avalos (strcmp(ntop, ntop2) == 0)) 720e9778795SPeter Avalos break; 721e9778795SPeter Avalos } 722e9778795SPeter Avalos freeaddrinfo(aitop); 723e9778795SPeter Avalos /* If we reached the end of the list, the address was not there. */ 724e9778795SPeter Avalos if (ai == NULL) { 725e9778795SPeter Avalos /* Address not found for the host name. */ 726e9778795SPeter Avalos logit("Address %.100s maps to %.600s, but this does not " 727e9778795SPeter Avalos "map back to the address.", ntop, name); 7280cbfa66cSDaniel Fojt return xstrdup(ntop); 729e9778795SPeter Avalos } 7300cbfa66cSDaniel Fojt return xstrdup(name); 731e9778795SPeter Avalos } 732e9778795SPeter Avalos 733e9778795SPeter Avalos /* 734e9778795SPeter Avalos * Return the canonical name of the host in the other side of the current 735e9778795SPeter Avalos * connection. The host name is cached, so it is efficient to call this 736e9778795SPeter Avalos * several times. 737e9778795SPeter Avalos */ 738e9778795SPeter Avalos 739e9778795SPeter Avalos const char * 740e9778795SPeter Avalos auth_get_canonical_hostname(struct ssh *ssh, int use_dns) 741e9778795SPeter Avalos { 742e9778795SPeter Avalos static char *dnsname; 743e9778795SPeter Avalos 744e9778795SPeter Avalos if (!use_dns) 745e9778795SPeter Avalos return ssh_remote_ipaddr(ssh); 746e9778795SPeter Avalos else if (dnsname != NULL) 747e9778795SPeter Avalos return dnsname; 748e9778795SPeter Avalos else { 749e9778795SPeter Avalos dnsname = remote_hostname(ssh); 750e9778795SPeter Avalos return dnsname; 751e9778795SPeter Avalos } 752e9778795SPeter Avalos } 753664f4763Szrj 754664f4763Szrj /* These functions link key/cert options to the auth framework */ 755664f4763Szrj 756664f4763Szrj /* Log sshauthopt options locally and (optionally) for remote transmission */ 757664f4763Szrj void 758664f4763Szrj auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote) 759664f4763Szrj { 760664f4763Szrj int do_env = options.permit_user_env && opts->nenv > 0; 761664f4763Szrj int do_permitopen = opts->npermitopen > 0 && 762664f4763Szrj (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0; 763664f4763Szrj int do_permitlisten = opts->npermitlisten > 0 && 764664f4763Szrj (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0; 765664f4763Szrj size_t i; 766664f4763Szrj char msg[1024], buf[64]; 767664f4763Szrj 768664f4763Szrj snprintf(buf, sizeof(buf), "%d", opts->force_tun_device); 769664f4763Szrj /* Try to keep this alphabetically sorted */ 77050a69bb5SSascha Wildner snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 771664f4763Szrj opts->permit_agent_forwarding_flag ? " agent-forwarding" : "", 772664f4763Szrj opts->force_command == NULL ? "" : " command", 773664f4763Szrj do_env ? " environment" : "", 774664f4763Szrj opts->valid_before == 0 ? "" : "expires", 77550a69bb5SSascha Wildner opts->no_require_user_presence ? " no-touch-required" : "", 776664f4763Szrj do_permitopen ? " permitopen" : "", 777664f4763Szrj do_permitlisten ? " permitlisten" : "", 778664f4763Szrj opts->permit_port_forwarding_flag ? " port-forwarding" : "", 779664f4763Szrj opts->cert_principals == NULL ? "" : " principals", 780664f4763Szrj opts->permit_pty_flag ? " pty" : "", 78150a69bb5SSascha Wildner opts->require_verify ? " uv" : "", 782664f4763Szrj opts->force_tun_device == -1 ? "" : " tun=", 783664f4763Szrj opts->force_tun_device == -1 ? "" : buf, 784664f4763Szrj opts->permit_user_rc ? " user-rc" : "", 78550a69bb5SSascha Wildner opts->permit_x11_forwarding_flag ? " x11-forwarding" : ""); 786664f4763Szrj 787664f4763Szrj debug("%s: %s", loc, msg); 788664f4763Szrj if (do_remote) 789664f4763Szrj auth_debug_add("%s: %s", loc, msg); 790664f4763Szrj 791664f4763Szrj if (options.permit_user_env) { 792664f4763Szrj for (i = 0; i < opts->nenv; i++) { 793664f4763Szrj debug("%s: environment: %s", loc, opts->env[i]); 794664f4763Szrj if (do_remote) { 795664f4763Szrj auth_debug_add("%s: environment: %s", 796664f4763Szrj loc, opts->env[i]); 797664f4763Szrj } 798664f4763Szrj } 799664f4763Szrj } 800664f4763Szrj 801664f4763Szrj /* Go into a little more details for the local logs. */ 802664f4763Szrj if (opts->valid_before != 0) { 803664f4763Szrj format_absolute_time(opts->valid_before, buf, sizeof(buf)); 804664f4763Szrj debug("%s: expires at %s", loc, buf); 805664f4763Szrj } 806664f4763Szrj if (opts->cert_principals != NULL) { 807664f4763Szrj debug("%s: authorized principals: \"%s\"", 808664f4763Szrj loc, opts->cert_principals); 809664f4763Szrj } 810664f4763Szrj if (opts->force_command != NULL) 811664f4763Szrj debug("%s: forced command: \"%s\"", loc, opts->force_command); 812664f4763Szrj if (do_permitopen) { 813664f4763Szrj for (i = 0; i < opts->npermitopen; i++) { 814664f4763Szrj debug("%s: permitted open: %s", 815664f4763Szrj loc, opts->permitopen[i]); 816664f4763Szrj } 817664f4763Szrj } 818664f4763Szrj if (do_permitlisten) { 819664f4763Szrj for (i = 0; i < opts->npermitlisten; i++) { 820664f4763Szrj debug("%s: permitted listen: %s", 821664f4763Szrj loc, opts->permitlisten[i]); 822664f4763Szrj } 823664f4763Szrj } 824664f4763Szrj } 825664f4763Szrj 826664f4763Szrj /* Activate a new set of key/cert options; merging with what is there. */ 827664f4763Szrj int 828664f4763Szrj auth_activate_options(struct ssh *ssh, struct sshauthopt *opts) 829664f4763Szrj { 830664f4763Szrj struct sshauthopt *old = auth_opts; 831664f4763Szrj const char *emsg = NULL; 832664f4763Szrj 83350a69bb5SSascha Wildner debug_f("setting new authentication options"); 834664f4763Szrj if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) { 835664f4763Szrj error("Inconsistent authentication options: %s", emsg); 836664f4763Szrj return -1; 837664f4763Szrj } 838664f4763Szrj return 0; 839664f4763Szrj } 840664f4763Szrj 841664f4763Szrj /* Disable forwarding, etc for the session */ 842664f4763Szrj void 843664f4763Szrj auth_restrict_session(struct ssh *ssh) 844664f4763Szrj { 845664f4763Szrj struct sshauthopt *restricted; 846664f4763Szrj 84750a69bb5SSascha Wildner debug_f("restricting session"); 848664f4763Szrj 849664f4763Szrj /* A blank sshauthopt defaults to permitting nothing */ 850*ee116499SAntonio Huete Jimenez if ((restricted = sshauthopt_new()) == NULL) 851*ee116499SAntonio Huete Jimenez fatal_f("sshauthopt_new failed"); 852664f4763Szrj restricted->permit_pty_flag = 1; 853664f4763Szrj restricted->restricted = 1; 854664f4763Szrj 855664f4763Szrj if (auth_activate_options(ssh, restricted) != 0) 85650a69bb5SSascha Wildner fatal_f("failed to restrict session"); 857664f4763Szrj sshauthopt_free(restricted); 858664f4763Szrj } 859