Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [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" |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: auth.c,v 1.17 2001/02/12 16:16:23 markus 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 | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 35 | #include "xmalloc.h" |
| 36 | #include "match.h" |
| 37 | #include "groupaccess.h" |
| 38 | #include "log.h" |
| 39 | #include "servconf.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 40 | #include "auth.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 41 | #include "auth-options.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "canohost.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 43 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 44 | /* import */ |
| 45 | extern ServerOptions options; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 46 | |
| 47 | /* |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 48 | * Check if the user is allowed to log in via ssh. If user is listed |
| 49 | * in DenyUsers or one of user's groups is listed in DenyGroups, false |
| 50 | * will be returned. If AllowUsers isn't empty and user isn't listed |
| 51 | * there, or if AllowGroups isn't empty and one of user's groups isn't |
| 52 | * listed there, false will be returned. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 53 | * If the user's shell is not executable, false will be returned. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 54 | * Otherwise true is returned. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 55 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 56 | int |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 57 | allowed_user(struct passwd * pw) |
| 58 | { |
| 59 | struct stat st; |
Damien Miller | ef7df54 | 2000-05-19 00:03:23 +1000 | [diff] [blame] | 60 | char *shell; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 61 | int i; |
| 62 | #ifdef WITH_AIXAUTHENTICATE |
| 63 | char *loginmsg; |
| 64 | #endif /* WITH_AIXAUTHENTICATE */ |
Kevin Steves | a58e0af | 2000-10-29 14:38:55 +0000 | [diff] [blame] | 65 | #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ |
Damien Miller | 62dd94b | 2000-09-23 14:26:32 +1100 | [diff] [blame] | 66 | !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 67 | struct spwd *spw; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 68 | |
| 69 | /* 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] | 70 | if (!pw || !pw->pw_name) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 71 | return 0; |
| 72 | |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 73 | spw = getspnam(pw->pw_name); |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 74 | if (spw != NULL) { |
| 75 | int days = time(NULL) / 86400; |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 76 | |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 77 | /* Check account expiry */ |
Damien Miller | 62dd94b | 2000-09-23 14:26:32 +1100 | [diff] [blame] | 78 | if ((spw->sp_expire >= 0) && (days > spw->sp_expire)) |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 79 | return 0; |
| 80 | |
| 81 | /* Check password expiry */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 82 | if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) && |
Damien Miller | 606f880 | 2000-09-16 15:39:56 +1100 | [diff] [blame] | 83 | (days > (spw->sp_lstchg + spw->sp_max))) |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 86 | #else |
| 87 | /* 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] | 88 | if (!pw || !pw->pw_name) |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 89 | return 0; |
| 90 | #endif |
| 91 | |
Damien Miller | ef7df54 | 2000-05-19 00:03:23 +1000 | [diff] [blame] | 92 | /* |
| 93 | * Get the shell from the password data. An empty shell field is |
| 94 | * legal, and means /bin/sh. |
| 95 | */ |
| 96 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
| 97 | |
| 98 | /* deny if shell does not exists or is not executable */ |
| 99 | if (stat(shell, &st) != 0) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 100 | return 0; |
| 101 | if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)))) |
| 102 | return 0; |
| 103 | |
| 104 | /* Return false if user is listed in DenyUsers */ |
| 105 | if (options.num_deny_users > 0) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 106 | for (i = 0; i < options.num_deny_users; i++) |
| 107 | if (match_pattern(pw->pw_name, options.deny_users[i])) |
| 108 | return 0; |
| 109 | } |
| 110 | /* Return false if AllowUsers isn't empty and user isn't listed there */ |
| 111 | if (options.num_allow_users > 0) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 112 | for (i = 0; i < options.num_allow_users; i++) |
| 113 | if (match_pattern(pw->pw_name, options.allow_users[i])) |
| 114 | break; |
| 115 | /* i < options.num_allow_users iff we break for loop */ |
| 116 | if (i >= options.num_allow_users) |
| 117 | return 0; |
| 118 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 119 | if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 120 | /* Get the user's group access list (primary and supplementary) */ |
| 121 | if (ga_init(pw->pw_name, pw->pw_gid) == 0) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 122 | return 0; |
| 123 | |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 124 | /* Return false if one of user's groups is listed in DenyGroups */ |
| 125 | if (options.num_deny_groups > 0) |
| 126 | if (ga_match(options.deny_groups, |
| 127 | options.num_deny_groups)) { |
| 128 | ga_free(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 129 | return 0; |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 130 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 131 | /* |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 132 | * 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] | 133 | * isn't listed there |
| 134 | */ |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 135 | if (options.num_allow_groups > 0) |
| 136 | if (!ga_match(options.allow_groups, |
| 137 | options.num_allow_groups)) { |
| 138 | ga_free(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 139 | return 0; |
Kevin Steves | 7b61cfa | 2001-01-14 19:11:00 +0000 | [diff] [blame] | 140 | } |
| 141 | ga_free(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 145 | if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 146 | if (loginmsg && *loginmsg) { |
| 147 | /* Remove embedded newlines (if any) */ |
| 148 | char *p; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 149 | for (p = loginmsg; *p; p++) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 150 | if (*p == '\n') |
| 151 | *p = ' '; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 152 | } |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 153 | /* Remove trailing newline */ |
| 154 | *--p = '\0'; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 155 | log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 156 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 157 | return 0; |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 158 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 159 | #endif /* WITH_AIXAUTHENTICATE */ |
| 160 | |
| 161 | /* We found no reason not to let this user try to log on... */ |
| 162 | return 1; |
| 163 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 164 | |
| 165 | Authctxt * |
| 166 | authctxt_new(void) |
| 167 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 168 | Authctxt *authctxt = xmalloc(sizeof(*authctxt)); |
| 169 | memset(authctxt, 0, sizeof(*authctxt)); |
| 170 | return authctxt; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | struct passwd * |
| 174 | pwcopy(struct passwd *pw) |
| 175 | { |
| 176 | struct passwd *copy = xmalloc(sizeof(*copy)); |
| 177 | memset(copy, 0, sizeof(*copy)); |
| 178 | copy->pw_name = xstrdup(pw->pw_name); |
| 179 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
| 180 | copy->pw_uid = pw->pw_uid; |
| 181 | copy->pw_gid = pw->pw_gid; |
| 182 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
| 183 | copy->pw_class = xstrdup(pw->pw_class); |
| 184 | #endif |
| 185 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 186 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 187 | return copy; |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) |
| 192 | { |
| 193 | void (*authlog) (const char *fmt,...) = verbose; |
| 194 | char *authmsg; |
| 195 | |
| 196 | /* Raise logging level */ |
| 197 | if (authenticated == 1 || |
| 198 | !authctxt->valid || |
| 199 | authctxt->failures >= AUTH_FAIL_LOG || |
| 200 | strcmp(method, "password") == 0) |
| 201 | authlog = log; |
| 202 | |
| 203 | if (authctxt->postponed) |
| 204 | authmsg = "Postponed"; |
| 205 | else |
| 206 | authmsg = authenticated ? "Accepted" : "Failed"; |
| 207 | |
| 208 | authlog("%s %s for %s%.100s from %.200s port %d%s", |
| 209 | authmsg, |
| 210 | method, |
| 211 | authctxt->valid ? "" : "illegal user ", |
| 212 | authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user, |
| 213 | get_remote_ipaddr(), |
| 214 | get_remote_port(), |
| 215 | info); |
| 216 | } |
| 217 | |
| 218 | /* |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 219 | * Check whether root logins are disallowed. |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 220 | */ |
| 221 | int |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 222 | auth_root_allowed(char *method) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 223 | { |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 224 | switch (options.permit_root_login) { |
| 225 | case PERMIT_YES: |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 226 | return 1; |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 227 | break; |
| 228 | case PERMIT_NO_PASSWD: |
| 229 | if (strcmp(method, "password") != 0) |
| 230 | return 1; |
| 231 | break; |
| 232 | case PERMIT_FORCED_ONLY: |
| 233 | if (forced_command) { |
| 234 | log("Root login accepted for forced command."); |
| 235 | return 1; |
| 236 | } |
| 237 | break; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 238 | } |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 239 | log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); |
| 240 | return 0; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 241 | } |