blob: 14e7f7e7125501b4507dd08947d7414688a4ee0f [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +00002 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +11003 *
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 Millerb38eff82000-04-01 11:09:21 +100023 */
24
25#include "includes.h"
Ben Lindstromd69191b2001-03-17 23:13:27 +000026RCSID("$OpenBSD: auth.c,v 1.20 2001/03/17 17:27:59 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100027
Damien Millerd2c208a2000-05-17 22:00:02 +100028#ifdef HAVE_LOGIN_H
29#include <login.h>
30#endif
Damien Miller1f335fb2000-06-26 11:31:33 +100031#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
32#include <shadow.h>
33#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
Damien Millerb38eff82000-04-01 11:09:21 +100034
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "xmalloc.h"
36#include "match.h"
37#include "groupaccess.h"
38#include "log.h"
39#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100040#include "auth.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000041#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "canohost.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100043
Damien Millerb38eff82000-04-01 11:09:21 +100044/* import */
45extern ServerOptions options;
Damien Millerb38eff82000-04-01 11:09:21 +100046
47/*
Kevin Steves7b61cfa2001-01-14 19:11:00 +000048 * 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 Millerb38eff82000-04-01 11:09:21 +100053 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100054 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100055 */
Damien Millereba71ba2000-04-29 23:57:08 +100056int
Damien Millerb38eff82000-04-01 11:09:21 +100057allowed_user(struct passwd * pw)
58{
59 struct stat st;
Ben Lindstromd69191b2001-03-17 23:13:27 +000060 char *shell, *cp;
Damien Millerb38eff82000-04-01 11:09:21 +100061 int i;
62#ifdef WITH_AIXAUTHENTICATE
63 char *loginmsg;
64#endif /* WITH_AIXAUTHENTICATE */
Kevin Stevesa58e0af2000-10-29 14:38:55 +000065#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
Damien Miller62dd94b2000-09-23 14:26:32 +110066 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Kevin Steves7b61cfa2001-01-14 19:11:00 +000067 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100068
69 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000070 if (!pw || !pw->pw_name)
Damien Millerb38eff82000-04-01 11:09:21 +100071 return 0;
72
Damien Miller1f335fb2000-06-26 11:31:33 +100073 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100074 if (spw != NULL) {
75 int days = time(NULL) / 86400;
Damien Miller1f335fb2000-06-26 11:31:33 +100076
Damien Millerc7088432000-07-02 18:44:54 +100077 /* Check account expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +110078 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
Damien Millerc7088432000-07-02 18:44:54 +100079 return 0;
80
81 /* Check password expiry */
Kevin Stevesef4eea92001-02-05 12:42:17 +000082 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
Damien Miller606f8802000-09-16 15:39:56 +110083 (days > (spw->sp_lstchg + spw->sp_max)))
Damien Millerc7088432000-07-02 18:44:54 +100084 return 0;
85 }
Damien Miller1f335fb2000-06-26 11:31:33 +100086#else
87 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000088 if (!pw || !pw->pw_name)
Damien Miller1f335fb2000-06-26 11:31:33 +100089 return 0;
90#endif
91
Damien Milleref7df542000-05-19 00:03:23 +100092 /*
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
Ben Lindstromd69191b2001-03-17 23:13:27 +000098 /* disallow anyone who does not have a standard shell */
99 setusershell();
100 while ((cp = getusershell()) != NULL)
101 if (strcmp(cp, shell) == 0)
102 break;
103 endusershell();
104 if (cp == NULL)
105 return 0;
106
Damien Milleref7df542000-05-19 00:03:23 +1000107 /* deny if shell does not exists or is not executable */
108 if (stat(shell, &st) != 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000109 return 0;
110 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
111 return 0;
112
113 /* Return false if user is listed in DenyUsers */
114 if (options.num_deny_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000115 for (i = 0; i < options.num_deny_users; i++)
116 if (match_pattern(pw->pw_name, options.deny_users[i]))
117 return 0;
118 }
119 /* Return false if AllowUsers isn't empty and user isn't listed there */
120 if (options.num_allow_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000121 for (i = 0; i < options.num_allow_users; i++)
122 if (match_pattern(pw->pw_name, options.allow_users[i]))
123 break;
124 /* i < options.num_allow_users iff we break for loop */
125 if (i >= options.num_allow_users)
126 return 0;
127 }
Damien Millerb38eff82000-04-01 11:09:21 +1000128 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000129 /* Get the user's group access list (primary and supplementary) */
130 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000131 return 0;
132
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000133 /* Return false if one of user's groups is listed in DenyGroups */
134 if (options.num_deny_groups > 0)
135 if (ga_match(options.deny_groups,
136 options.num_deny_groups)) {
137 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000138 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000139 }
Damien Millerb38eff82000-04-01 11:09:21 +1000140 /*
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000141 * Return false if AllowGroups isn't empty and one of user's groups
Damien Millerb38eff82000-04-01 11:09:21 +1000142 * isn't listed there
143 */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000144 if (options.num_allow_groups > 0)
145 if (!ga_match(options.allow_groups,
146 options.num_allow_groups)) {
147 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000148 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000149 }
150 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000151 }
152
153#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000154 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000155 if (loginmsg && *loginmsg) {
156 /* Remove embedded newlines (if any) */
157 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000158 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000159 if (*p == '\n')
160 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000161 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000162 /* Remove trailing newline */
163 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000164 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000165 }
Damien Millerb38eff82000-04-01 11:09:21 +1000166 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000167 }
Damien Millerb38eff82000-04-01 11:09:21 +1000168#endif /* WITH_AIXAUTHENTICATE */
169
170 /* We found no reason not to let this user try to log on... */
171 return 1;
172}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000173
174Authctxt *
175authctxt_new(void)
176{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000177 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
178 memset(authctxt, 0, sizeof(*authctxt));
179 return authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000180}
181
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000182void
183auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
184{
185 void (*authlog) (const char *fmt,...) = verbose;
186 char *authmsg;
187
188 /* Raise logging level */
189 if (authenticated == 1 ||
190 !authctxt->valid ||
191 authctxt->failures >= AUTH_FAIL_LOG ||
192 strcmp(method, "password") == 0)
193 authlog = log;
194
195 if (authctxt->postponed)
196 authmsg = "Postponed";
197 else
198 authmsg = authenticated ? "Accepted" : "Failed";
199
200 authlog("%s %s for %s%.100s from %.200s port %d%s",
201 authmsg,
202 method,
203 authctxt->valid ? "" : "illegal user ",
204 authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
205 get_remote_ipaddr(),
206 get_remote_port(),
207 info);
208}
209
210/*
Ben Lindstromd8a90212001-02-15 03:08:27 +0000211 * Check whether root logins are disallowed.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000212 */
213int
Ben Lindstromd8a90212001-02-15 03:08:27 +0000214auth_root_allowed(char *method)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000215{
Ben Lindstromd8a90212001-02-15 03:08:27 +0000216 switch (options.permit_root_login) {
217 case PERMIT_YES:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000218 return 1;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000219 break;
220 case PERMIT_NO_PASSWD:
221 if (strcmp(method, "password") != 0)
222 return 1;
223 break;
224 case PERMIT_FORCED_ONLY:
225 if (forced_command) {
226 log("Root login accepted for forced command.");
227 return 1;
228 }
229 break;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 }
Ben Lindstromd8a90212001-02-15 03:08:27 +0000231 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
232 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233}