blob: 685b8bb35216785f931bc1b16e262be7a56e572e [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 Millerefb4afe2000-04-12 18:45:05 +10004 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millerb38eff82000-04-01 11:09:21 +10005 */
6
7#include "includes.h"
Damien Milleref7df542000-05-19 00:03:23 +10008RCSID("$OpenBSD: auth.c,v 1.7 2000/05/17 21:37:24 deraadt Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +10009
10#include "xmalloc.h"
11#include "rsa.h"
12#include "ssh.h"
13#include "pty.h"
14#include "packet.h"
15#include "buffer.h"
16#include "cipher.h"
17#include "mpaux.h"
18#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100019#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100020#include "channels.h"
21#include "match.h"
Damien Millerd2c208a2000-05-17 22:00:02 +100022#ifdef HAVE_LOGIN_H
23#include <login.h>
24#endif
Damien Millerb38eff82000-04-01 11:09:21 +100025
Damien Millerefb4afe2000-04-12 18:45:05 +100026#include "bufaux.h"
27#include "ssh2.h"
28#include "auth.h"
Damien Millerb38eff82000-04-01 11:09:21 +100029#include "session.h"
30#include "dispatch.h"
31
Damien Millerefb4afe2000-04-12 18:45:05 +100032
Damien Millerb38eff82000-04-01 11:09:21 +100033/* import */
34extern ServerOptions options;
35extern char *forced_command;
36
37/*
38 * Check if the user is allowed to log in via ssh. If user is listed in
39 * DenyUsers or user's primary group is listed in DenyGroups, false will
40 * be returned. If AllowUsers isn't empty and user isn't listed there, or
41 * if AllowGroups isn't empty and user isn't listed there, false will be
Damien Miller4af51302000-04-16 11:18:38 +100042 * returned.
Damien Millerb38eff82000-04-01 11:09:21 +100043 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100044 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100045 */
Damien Millereba71ba2000-04-29 23:57:08 +100046int
Damien Millerb38eff82000-04-01 11:09:21 +100047allowed_user(struct passwd * pw)
48{
49 struct stat st;
50 struct group *grp;
Damien Milleref7df542000-05-19 00:03:23 +100051 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100052 int i;
53#ifdef WITH_AIXAUTHENTICATE
54 char *loginmsg;
55#endif /* WITH_AIXAUTHENTICATE */
56
57 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
58 if (!pw)
59 return 0;
60
Damien Milleref7df542000-05-19 00:03:23 +100061 /*
62 * Get the shell from the password data. An empty shell field is
63 * legal, and means /bin/sh.
64 */
65 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
66
67 /* deny if shell does not exists or is not executable */
68 if (stat(shell, &st) != 0)
Damien Millerb38eff82000-04-01 11:09:21 +100069 return 0;
70 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
71 return 0;
72
73 /* Return false if user is listed in DenyUsers */
74 if (options.num_deny_users > 0) {
75 if (!pw->pw_name)
76 return 0;
77 for (i = 0; i < options.num_deny_users; i++)
78 if (match_pattern(pw->pw_name, options.deny_users[i]))
79 return 0;
80 }
81 /* Return false if AllowUsers isn't empty and user isn't listed there */
82 if (options.num_allow_users > 0) {
83 if (!pw->pw_name)
84 return 0;
85 for (i = 0; i < options.num_allow_users; i++)
86 if (match_pattern(pw->pw_name, options.allow_users[i]))
87 break;
88 /* i < options.num_allow_users iff we break for loop */
89 if (i >= options.num_allow_users)
90 return 0;
91 }
92 /* Get the primary group name if we need it. Return false if it fails */
93 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
94 grp = getgrgid(pw->pw_gid);
95 if (!grp)
96 return 0;
97
98 /* Return false if user's group is listed in DenyGroups */
99 if (options.num_deny_groups > 0) {
100 if (!grp->gr_name)
101 return 0;
102 for (i = 0; i < options.num_deny_groups; i++)
103 if (match_pattern(grp->gr_name, options.deny_groups[i]))
104 return 0;
105 }
106 /*
107 * Return false if AllowGroups isn't empty and user's group
108 * isn't listed there
109 */
110 if (options.num_allow_groups > 0) {
111 if (!grp->gr_name)
112 return 0;
113 for (i = 0; i < options.num_allow_groups; i++)
114 if (match_pattern(grp->gr_name, options.allow_groups[i]))
115 break;
116 /* i < options.num_allow_groups iff we break for
117 loop */
118 if (i >= options.num_allow_groups)
119 return 0;
120 }
121 }
122
123#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000124 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000125 if (loginmsg && *loginmsg) {
126 /* Remove embedded newlines (if any) */
127 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000128 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000129 if (*p == '\n')
130 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000131 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000132 /* Remove trailing newline */
133 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000134 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000135 }
Damien Millerb38eff82000-04-01 11:09:21 +1000136 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000137 }
Damien Millerb38eff82000-04-01 11:09:21 +1000138#endif /* WITH_AIXAUTHENTICATE */
139
140 /* We found no reason not to let this user try to log on... */
141 return 1;
142}