blob: 59c95fe48221e9867b8c6cdfb913ce23be5298c3 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
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 Millerefb4afe2000-04-12 18:45:05 +100012 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
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 Millerb38eff82000-04-01 11:09:21 +100033 */
34
35#include "includes.h"
Kevin Steves7b61cfa2001-01-14 19:11:00 +000036RCSID("$OpenBSD: auth.c,v 1.12 2001/01/13 18:56:48 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
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 Millerb38eff82000-04-01 11:09:21 +100044#include "mpaux.h"
45#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100046#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100047#include "channels.h"
48#include "match.h"
Kevin Steves7b61cfa2001-01-14 19:11:00 +000049#include "groupaccess.h"
Damien Millerd2c208a2000-05-17 22:00:02 +100050#ifdef HAVE_LOGIN_H
51#include <login.h>
52#endif
Damien Miller1f335fb2000-06-26 11:31:33 +100053#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
54#include <shadow.h>
55#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
Damien Millerb38eff82000-04-01 11:09:21 +100056
Damien Millerefb4afe2000-04-12 18:45:05 +100057#include "bufaux.h"
58#include "ssh2.h"
59#include "auth.h"
Damien Millerb38eff82000-04-01 11:09:21 +100060#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100061
Damien Millerb38eff82000-04-01 11:09:21 +100062/* import */
63extern ServerOptions options;
Damien Millerb38eff82000-04-01 11:09:21 +100064
65/*
Kevin Steves7b61cfa2001-01-14 19:11:00 +000066 * Check if the user is allowed to log in via ssh. If user is listed
67 * in DenyUsers or one of user's groups is listed in DenyGroups, false
68 * will be returned. If AllowUsers isn't empty and user isn't listed
69 * there, or if AllowGroups isn't empty and one of user's groups isn't
70 * listed there, false will be returned.
Damien Millerb38eff82000-04-01 11:09:21 +100071 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100072 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100073 */
Damien Millereba71ba2000-04-29 23:57:08 +100074int
Damien Millerb38eff82000-04-01 11:09:21 +100075allowed_user(struct passwd * pw)
76{
77 struct stat st;
Damien Milleref7df542000-05-19 00:03:23 +100078 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100079 int i;
80#ifdef WITH_AIXAUTHENTICATE
81 char *loginmsg;
82#endif /* WITH_AIXAUTHENTICATE */
Kevin Stevesa58e0af2000-10-29 14:38:55 +000083#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
Damien Miller62dd94b2000-09-23 14:26:32 +110084 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Kevin Steves7b61cfa2001-01-14 19:11:00 +000085 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100086
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 Millerb38eff82000-04-01 11:09:21 +100089 return 0;
90
Damien Miller1f335fb2000-06-26 11:31:33 +100091 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100092 if (spw != NULL) {
93 int days = time(NULL) / 86400;
Damien Miller1f335fb2000-06-26 11:31:33 +100094
Damien Millerc7088432000-07-02 18:44:54 +100095 /* Check account expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +110096 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
Damien Millerc7088432000-07-02 18:44:54 +100097 return 0;
98
99 /* Check password expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +1100100 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
Damien Miller606f8802000-09-16 15:39:56 +1100101 (days > (spw->sp_lstchg + spw->sp_max)))
Damien Millerc7088432000-07-02 18:44:54 +1000102 return 0;
103 }
Damien Miller1f335fb2000-06-26 11:31:33 +1000104#else
105 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000106 if (!pw || !pw->pw_name)
Damien Miller1f335fb2000-06-26 11:31:33 +1000107 return 0;
108#endif
109
Damien Milleref7df542000-05-19 00:03:23 +1000110 /*
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 Millerb38eff82000-04-01 11:09:21 +1000118 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) {
Damien Millerb38eff82000-04-01 11:09:21 +1000124 for (i = 0; i < options.num_deny_users; i++)
125 if (match_pattern(pw->pw_name, options.deny_users[i]))
126 return 0;
127 }
128 /* Return false if AllowUsers isn't empty and user isn't listed there */
129 if (options.num_allow_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000130 for (i = 0; i < options.num_allow_users; i++)
131 if (match_pattern(pw->pw_name, options.allow_users[i]))
132 break;
133 /* i < options.num_allow_users iff we break for loop */
134 if (i >= options.num_allow_users)
135 return 0;
136 }
Damien Millerb38eff82000-04-01 11:09:21 +1000137 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000138 /* Get the user's group access list (primary and supplementary) */
139 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000140 return 0;
141
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000142 /* Return false if one of user's groups is listed in DenyGroups */
143 if (options.num_deny_groups > 0)
144 if (ga_match(options.deny_groups,
145 options.num_deny_groups)) {
146 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000147 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000148 }
Damien Millerb38eff82000-04-01 11:09:21 +1000149 /*
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000150 * Return false if AllowGroups isn't empty and one of user's groups
Damien Millerb38eff82000-04-01 11:09:21 +1000151 * isn't listed there
152 */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000153 if (options.num_allow_groups > 0)
154 if (!ga_match(options.allow_groups,
155 options.num_allow_groups)) {
156 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000157 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000158 }
159 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000160 }
161
162#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000163 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000164 if (loginmsg && *loginmsg) {
165 /* Remove embedded newlines (if any) */
166 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000167 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000168 if (*p == '\n')
169 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000170 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000171 /* Remove trailing newline */
172 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000173 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000174 }
Damien Millerb38eff82000-04-01 11:09:21 +1000175 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000176 }
Damien Millerb38eff82000-04-01 11:09:21 +1000177#endif /* WITH_AIXAUTHENTICATE */
178
179 /* We found no reason not to let this user try to log on... */
180 return 1;
181}