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