Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 3 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 4 | * |
| 5 | * As far as I am concerned, the code I have written for this software |
| 6 | * can be used freely for any purpose. Any derived versions of this |
| 7 | * software must be clearly marked as such, and if the derived work is |
| 8 | * incompatible with the protocol description in the RFC file, it must be |
| 9 | * called by a name other than "ssh" or "Secure Shell". |
| 10 | * |
| 11 | * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 12 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include "includes.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 36 | RCSID("$OpenBSD: auth.c,v 1.11 2000/10/11 20:27:23 markus Exp $"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 37 | |
| 38 | #include "xmalloc.h" |
| 39 | #include "rsa.h" |
| 40 | #include "ssh.h" |
| 41 | #include "pty.h" |
| 42 | #include "packet.h" |
| 43 | #include "buffer.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 44 | #include "mpaux.h" |
| 45 | #include "servconf.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 46 | #include "compat.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 47 | #include "channels.h" |
| 48 | #include "match.h" |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 49 | #ifdef HAVE_LOGIN_H |
| 50 | #include <login.h> |
| 51 | #endif |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 52 | #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) |
| 53 | #include <shadow.h> |
| 54 | #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 55 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 56 | #include "bufaux.h" |
| 57 | #include "ssh2.h" |
| 58 | #include "auth.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 59 | #include "session.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 60 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 61 | /* import */ |
| 62 | extern ServerOptions options; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * Check if the user is allowed to log in via ssh. If user is listed in |
| 66 | * DenyUsers or user's primary group is listed in DenyGroups, false will |
| 67 | * be returned. If AllowUsers isn't empty and user isn't listed there, or |
| 68 | * if AllowGroups isn't empty and user isn't listed there, false will be |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 69 | * returned. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 70 | * If the user's shell is not executable, false will be returned. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 71 | * Otherwise true is returned. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 72 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 73 | int |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 74 | allowed_user(struct passwd * pw) |
| 75 | { |
| 76 | struct stat st; |
| 77 | struct group *grp; |
Damien Miller | ef7df54 | 2000-05-19 00:03:23 +1000 | [diff] [blame] | 78 | char *shell; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 79 | int i; |
| 80 | #ifdef WITH_AIXAUTHENTICATE |
| 81 | char *loginmsg; |
| 82 | #endif /* WITH_AIXAUTHENTICATE */ |
Kevin Steves | a58e0af | 2000-10-29 14:38:55 +0000 | [diff] [blame] | 83 | #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ |
Damien Miller | 62dd94b | 2000-09-23 14:26:32 +1100 | [diff] [blame] | 84 | !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 85 | struct spwd *spw; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 86 | |
| 87 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ |
| 88 | if (!pw) |
| 89 | return 0; |
| 90 | |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 91 | spw = getspnam(pw->pw_name); |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 92 | if (spw != NULL) { |
| 93 | int days = time(NULL) / 86400; |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 94 | |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 95 | /* Check account expiry */ |
Damien Miller | 62dd94b | 2000-09-23 14:26:32 +1100 | [diff] [blame] | 96 | if ((spw->sp_expire >= 0) && (days > spw->sp_expire)) |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 97 | return 0; |
| 98 | |
| 99 | /* Check password expiry */ |
Damien Miller | 62dd94b | 2000-09-23 14:26:32 +1100 | [diff] [blame] | 100 | if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) && |
Damien Miller | 606f880 | 2000-09-16 15:39:56 +1100 | [diff] [blame] | 101 | (days > (spw->sp_lstchg + spw->sp_max))) |
Damien Miller | c708843 | 2000-07-02 18:44:54 +1000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
Damien Miller | 1f335fb | 2000-06-26 11:31:33 +1000 | [diff] [blame] | 104 | #else |
| 105 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ |
| 106 | if (!pw) |
| 107 | return 0; |
| 108 | #endif |
| 109 | |
Damien Miller | ef7df54 | 2000-05-19 00:03:23 +1000 | [diff] [blame] | 110 | /* |
| 111 | * Get the shell from the password data. An empty shell field is |
| 112 | * legal, and means /bin/sh. |
| 113 | */ |
| 114 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
| 115 | |
| 116 | /* deny if shell does not exists or is not executable */ |
| 117 | if (stat(shell, &st) != 0) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 118 | return 0; |
| 119 | if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)))) |
| 120 | return 0; |
| 121 | |
| 122 | /* Return false if user is listed in DenyUsers */ |
| 123 | if (options.num_deny_users > 0) { |
| 124 | if (!pw->pw_name) |
| 125 | return 0; |
| 126 | for (i = 0; i < options.num_deny_users; i++) |
| 127 | if (match_pattern(pw->pw_name, options.deny_users[i])) |
| 128 | return 0; |
| 129 | } |
| 130 | /* Return false if AllowUsers isn't empty and user isn't listed there */ |
| 131 | if (options.num_allow_users > 0) { |
| 132 | if (!pw->pw_name) |
| 133 | return 0; |
| 134 | for (i = 0; i < options.num_allow_users; i++) |
| 135 | if (match_pattern(pw->pw_name, options.allow_users[i])) |
| 136 | break; |
| 137 | /* i < options.num_allow_users iff we break for loop */ |
| 138 | if (i >= options.num_allow_users) |
| 139 | return 0; |
| 140 | } |
| 141 | /* Get the primary group name if we need it. Return false if it fails */ |
| 142 | if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { |
| 143 | grp = getgrgid(pw->pw_gid); |
| 144 | if (!grp) |
| 145 | return 0; |
| 146 | |
| 147 | /* Return false if user's group is listed in DenyGroups */ |
| 148 | if (options.num_deny_groups > 0) { |
| 149 | if (!grp->gr_name) |
| 150 | return 0; |
| 151 | for (i = 0; i < options.num_deny_groups; i++) |
| 152 | if (match_pattern(grp->gr_name, options.deny_groups[i])) |
| 153 | return 0; |
| 154 | } |
| 155 | /* |
| 156 | * Return false if AllowGroups isn't empty and user's group |
| 157 | * isn't listed there |
| 158 | */ |
| 159 | if (options.num_allow_groups > 0) { |
| 160 | if (!grp->gr_name) |
| 161 | return 0; |
| 162 | for (i = 0; i < options.num_allow_groups; i++) |
| 163 | if (match_pattern(grp->gr_name, options.allow_groups[i])) |
| 164 | break; |
| 165 | /* i < options.num_allow_groups iff we break for |
| 166 | loop */ |
| 167 | if (i >= options.num_allow_groups) |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 173 | if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 174 | if (loginmsg && *loginmsg) { |
| 175 | /* Remove embedded newlines (if any) */ |
| 176 | char *p; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 177 | for (p = loginmsg; *p; p++) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 178 | if (*p == '\n') |
| 179 | *p = ' '; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 180 | } |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 181 | /* Remove trailing newline */ |
| 182 | *--p = '\0'; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 183 | log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 184 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 185 | return 0; |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 186 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 187 | #endif /* WITH_AIXAUTHENTICATE */ |
| 188 | |
| 189 | /* We found no reason not to let this user try to log on... */ |
| 190 | return 1; |
| 191 | } |