]> andersk Git - openssh.git/blame - auth.c
NB: big update - may break stuff. Please test!
[openssh.git] / auth.c
CommitLineData
7368a6c8 1/*
e78a59f5 2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
bcbf86ec 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.
7368a6c8 23 */
24
25#include "includes.h"
61e96248 26RCSID("$OpenBSD: auth.c,v 1.15 2001/02/03 10:08:37 markus Exp $");
7368a6c8 27
c1ef8333 28#ifdef HAVE_LOGIN_H
29#include <login.h>
30#endif
4cb5ffa0 31#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
32#include <shadow.h>
33#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
7368a6c8 34
42f11eb2 35#include "xmalloc.h"
36#include "match.h"
37#include "groupaccess.h"
38#include "log.h"
39#include "servconf.h"
e78a59f5 40#include "auth.h"
59c97189 41#include "auth-options.h"
42f11eb2 42#include "canohost.h"
e78a59f5 43
7368a6c8 44/* import */
45extern ServerOptions options;
7368a6c8 46
47/*
c6a69271 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.
7368a6c8 53 * If the user's shell is not executable, false will be returned.
6ae2364d 54 * Otherwise true is returned.
7368a6c8 55 */
a306f2dd 56int
7368a6c8 57allowed_user(struct passwd * pw)
58{
59 struct stat st;
301e9b01 60 char *shell;
7368a6c8 61 int i;
62#ifdef WITH_AIXAUTHENTICATE
63 char *loginmsg;
64#endif /* WITH_AIXAUTHENTICATE */
75b90ced 65#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
37c1c46d 66 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
c6a69271 67 struct spwd *spw;
7368a6c8 68
69 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 70 if (!pw || !pw->pw_name)
7368a6c8 71 return 0;
72
4cb5ffa0 73 spw = getspnam(pw->pw_name);
27494968 74 if (spw != NULL) {
75 int days = time(NULL) / 86400;
4cb5ffa0 76
27494968 77 /* Check account expiry */
37c1c46d 78 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
27494968 79 return 0;
80
81 /* Check password expiry */
37c1c46d 82 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
7f8f5e00 83 (days > (spw->sp_lstchg + spw->sp_max)))
27494968 84 return 0;
85 }
4cb5ffa0 86#else
87 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 88 if (!pw || !pw->pw_name)
4cb5ffa0 89 return 0;
90#endif
91
301e9b01 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)
7368a6c8 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) {
7368a6c8 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) {
7368a6c8 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 }
7368a6c8 119 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
c6a69271 120 /* Get the user's group access list (primary and supplementary) */
121 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
7368a6c8 122 return 0;
123
c6a69271 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();
7368a6c8 129 return 0;
c6a69271 130 }
7368a6c8 131 /*
c6a69271 132 * Return false if AllowGroups isn't empty and one of user's groups
7368a6c8 133 * isn't listed there
134 */
c6a69271 135 if (options.num_allow_groups > 0)
136 if (!ga_match(options.allow_groups,
137 options.num_allow_groups)) {
138 ga_free();
7368a6c8 139 return 0;
c6a69271 140 }
141 ga_free();
7368a6c8 142 }
143
144#ifdef WITH_AIXAUTHENTICATE
5daf7064 145 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
c1ef8333 146 if (loginmsg && *loginmsg) {
147 /* Remove embedded newlines (if any) */
148 char *p;
5daf7064 149 for (p = loginmsg; *p; p++) {
c1ef8333 150 if (*p == '\n')
151 *p = ' ';
5daf7064 152 }
c1ef8333 153 /* Remove trailing newline */
154 *--p = '\0';
5daf7064 155 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
c1ef8333 156 }
7368a6c8 157 return 0;
c1ef8333 158 }
7368a6c8 159#endif /* WITH_AIXAUTHENTICATE */
160
161 /* We found no reason not to let this user try to log on... */
162 return 1;
163}
59c97189 164
165Authctxt *
166authctxt_new(void)
167{
168 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
169 memset(authctxt, 0, sizeof(*authctxt));
170 return authctxt;
171}
172
173struct passwd *
174pwcopy(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
190void
191auth_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/*
219 * Check if the user is logging in as root and root logins are disallowed.
220 * Note that root login is _allways_ allowed for forced commands.
221 */
222int
223auth_root_allowed(void)
224{
225 if (options.permit_root_login)
226 return 1;
227 if (forced_command) {
228 log("Root login accepted for forced command.");
229 return 1;
230 } else {
61e96248 231 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
59c97189 232 return 0;
233 }
234}
This page took 0.101934 seconds and 5 git commands to generate.