blob: 883f08ab03cfd9c007af0cb40d9319b630d0027b [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"
Damien Millere4340be2000-09-16 13:29:08 +110036RCSID("$OpenBSD: auth.c,v 1.10 2000/09/07 21:13:36 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"
44#include "cipher.h"
45#include "mpaux.h"
46#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100047#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100048#include "channels.h"
49#include "match.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/*
66 * Check if the user is allowed to log in via ssh. If user is listed in
67 * DenyUsers or user's primary group is listed in DenyGroups, false will
68 * be returned. If AllowUsers isn't empty and user isn't listed there, or
69 * if AllowGroups isn't empty and user isn't listed there, false will be
Damien Miller4af51302000-04-16 11:18:38 +100070 * 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;
78 struct group *grp;
Damien Milleref7df542000-05-19 00:03:23 +100079 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100080 int i;
81#ifdef WITH_AIXAUTHENTICATE
82 char *loginmsg;
83#endif /* WITH_AIXAUTHENTICATE */
Damien Miller62dd94b2000-09-23 14:26:32 +110084#if !defined(PAM) && defined(HAVE_SHADOW_H) && \
85 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Damien Miller1f335fb2000-06-26 11:31:33 +100086 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100087
88 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
89 if (!pw)
90 return 0;
91
Damien Miller1f335fb2000-06-26 11:31:33 +100092 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100093 if (spw != NULL) {
94 int days = time(NULL) / 86400;
Damien Miller1f335fb2000-06-26 11:31:33 +100095
Damien Millerc7088432000-07-02 18:44:54 +100096 /* Check account expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +110097 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
Damien Millerc7088432000-07-02 18:44:54 +100098 return 0;
99
100 /* Check password expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +1100101 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
Damien Miller606f8802000-09-16 15:39:56 +1100102 (days > (spw->sp_lstchg + spw->sp_max)))
Damien Millerc7088432000-07-02 18:44:54 +1000103 return 0;
104 }
Damien Miller1f335fb2000-06-26 11:31:33 +1000105#else
106 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
107 if (!pw)
108 return 0;
109#endif
110
Damien Milleref7df542000-05-19 00:03:23 +1000111 /*
112 * Get the shell from the password data. An empty shell field is
113 * legal, and means /bin/sh.
114 */
115 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
116
117 /* deny if shell does not exists or is not executable */
118 if (stat(shell, &st) != 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000119 return 0;
120 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
121 return 0;
122
123 /* Return false if user is listed in DenyUsers */
124 if (options.num_deny_users > 0) {
125 if (!pw->pw_name)
126 return 0;
127 for (i = 0; i < options.num_deny_users; i++)
128 if (match_pattern(pw->pw_name, options.deny_users[i]))
129 return 0;
130 }
131 /* Return false if AllowUsers isn't empty and user isn't listed there */
132 if (options.num_allow_users > 0) {
133 if (!pw->pw_name)
134 return 0;
135 for (i = 0; i < options.num_allow_users; i++)
136 if (match_pattern(pw->pw_name, options.allow_users[i]))
137 break;
138 /* i < options.num_allow_users iff we break for loop */
139 if (i >= options.num_allow_users)
140 return 0;
141 }
142 /* Get the primary group name if we need it. Return false if it fails */
143 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
144 grp = getgrgid(pw->pw_gid);
145 if (!grp)
146 return 0;
147
148 /* Return false if user's group is listed in DenyGroups */
149 if (options.num_deny_groups > 0) {
150 if (!grp->gr_name)
151 return 0;
152 for (i = 0; i < options.num_deny_groups; i++)
153 if (match_pattern(grp->gr_name, options.deny_groups[i]))
154 return 0;
155 }
156 /*
157 * Return false if AllowGroups isn't empty and user's group
158 * isn't listed there
159 */
160 if (options.num_allow_groups > 0) {
161 if (!grp->gr_name)
162 return 0;
163 for (i = 0; i < options.num_allow_groups; i++)
164 if (match_pattern(grp->gr_name, options.allow_groups[i]))
165 break;
166 /* i < options.num_allow_groups iff we break for
167 loop */
168 if (i >= options.num_allow_groups)
169 return 0;
170 }
171 }
172
173#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000174 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000175 if (loginmsg && *loginmsg) {
176 /* Remove embedded newlines (if any) */
177 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000178 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000179 if (*p == '\n')
180 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000181 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000182 /* Remove trailing newline */
183 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000184 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000185 }
Damien Millerb38eff82000-04-01 11:09:21 +1000186 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000187 }
Damien Millerb38eff82000-04-01 11:09:21 +1000188#endif /* WITH_AIXAUTHENTICATE */
189
190 /* We found no reason not to let this user try to log on... */
191 return 1;
192}