Annotation of src/usr.bin/ssh/auth.c, Revision 1.12
1.1 markus 1: /*
2: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3: * All rights reserved
1.9 deraadt 4: *
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: *
1.2 markus 12: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.9 deraadt 13: *
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.
1.1 markus 33: */
34:
35: #include "includes.h"
1.12 ! markus 36: RCSID("$OpenBSD: auth.c,v 1.11 2000/10/11 20:27:23 markus Exp $");
1.1 markus 37:
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 "mpaux.h"
45: #include "servconf.h"
1.2 markus 46: #include "compat.h"
1.1 markus 47: #include "channels.h"
48: #include "match.h"
1.12 ! markus 49: #include "groupaccess.h"
1.6 markus 50:
1.2 markus 51: #include "bufaux.h"
52: #include "ssh2.h"
53: #include "auth.h"
1.1 markus 54: #include "session.h"
1.2 markus 55:
1.1 markus 56: /* import */
57: extern ServerOptions options;
58:
59: /*
1.12 ! markus 60: * Check if the user is allowed to log in via ssh. If user is listed
! 61: * in DenyUsers or one of user's groups is listed in DenyGroups, false
! 62: * will be returned. If AllowUsers isn't empty and user isn't listed
! 63: * there, or if AllowGroups isn't empty and one of user's groups isn't
! 64: * listed there, false will be returned.
1.1 markus 65: * If the user's shell is not executable, false will be returned.
1.4 markus 66: * Otherwise true is returned.
1.1 markus 67: */
1.5 markus 68: int
1.1 markus 69: allowed_user(struct passwd * pw)
70: {
71: struct stat st;
1.7 deraadt 72: char *shell;
1.1 markus 73: int i;
74:
75: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 ! markus 76: if (!pw || !pw->pw_name)
1.1 markus 77: return 0;
78:
1.7 deraadt 79: /*
80: * Get the shell from the password data. An empty shell field is
81: * legal, and means /bin/sh.
82: */
83: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
84:
1.1 markus 85: /* deny if shell does not exists or is not executable */
1.7 deraadt 86: if (stat(shell, &st) != 0)
1.1 markus 87: return 0;
88: if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
89: return 0;
90:
91: /* Return false if user is listed in DenyUsers */
92: if (options.num_deny_users > 0) {
93: for (i = 0; i < options.num_deny_users; i++)
94: if (match_pattern(pw->pw_name, options.deny_users[i]))
95: return 0;
96: }
97: /* Return false if AllowUsers isn't empty and user isn't listed there */
98: if (options.num_allow_users > 0) {
99: for (i = 0; i < options.num_allow_users; i++)
100: if (match_pattern(pw->pw_name, options.allow_users[i]))
101: break;
102: /* i < options.num_allow_users iff we break for loop */
103: if (i >= options.num_allow_users)
104: return 0;
105: }
106: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 ! markus 107: /* Get the user's group access list (primary and supplementary) */
! 108: if (ga_init(pw->pw_name, pw->pw_gid) == 0)
1.1 markus 109: return 0;
110:
1.12 ! markus 111: /* Return false if one of user's groups is listed in DenyGroups */
! 112: if (options.num_deny_groups > 0)
! 113: if (ga_match(options.deny_groups,
! 114: options.num_deny_groups)) {
! 115: ga_free();
1.1 markus 116: return 0;
1.12 ! markus 117: }
1.1 markus 118: /*
1.12 ! markus 119: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 120: * isn't listed there
121: */
1.12 ! markus 122: if (options.num_allow_groups > 0)
! 123: if (!ga_match(options.allow_groups,
! 124: options.num_allow_groups)) {
! 125: ga_free();
1.1 markus 126: return 0;
1.12 ! markus 127: }
! 128: ga_free();
1.1 markus 129: }
130: /* We found no reason not to let this user try to log on... */
131: return 1;
132: }