Annotation of src/usr.bin/ssh/auth.c, Revision 1.11
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.11 ! markus 36: RCSID("$OpenBSD: auth.c,v 1.10 2000/09/07 21:13:36 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.6 markus 49:
1.2 markus 50: #include "bufaux.h"
51: #include "ssh2.h"
52: #include "auth.h"
1.1 markus 53: #include "session.h"
1.2 markus 54:
1.1 markus 55: /* import */
56: extern ServerOptions options;
57:
58: /*
59: * Check if the user is allowed to log in via ssh. If user is listed in
60: * DenyUsers or user's primary group is listed in DenyGroups, false will
61: * be returned. If AllowUsers isn't empty and user isn't listed there, or
62: * if AllowGroups isn't empty and user isn't listed there, false will be
1.4 markus 63: * returned.
1.1 markus 64: * If the user's shell is not executable, false will be returned.
1.4 markus 65: * Otherwise true is returned.
1.1 markus 66: */
1.5 markus 67: int
1.1 markus 68: allowed_user(struct passwd * pw)
69: {
70: struct stat st;
71: struct group *grp;
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... */
76: if (!pw)
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: if (!pw->pw_name)
94: return 0;
95: for (i = 0; i < options.num_deny_users; i++)
96: if (match_pattern(pw->pw_name, options.deny_users[i]))
97: return 0;
98: }
99: /* Return false if AllowUsers isn't empty and user isn't listed there */
100: if (options.num_allow_users > 0) {
101: if (!pw->pw_name)
102: return 0;
103: for (i = 0; i < options.num_allow_users; i++)
104: if (match_pattern(pw->pw_name, options.allow_users[i]))
105: break;
106: /* i < options.num_allow_users iff we break for loop */
107: if (i >= options.num_allow_users)
108: return 0;
109: }
110: /* Get the primary group name if we need it. Return false if it fails */
111: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
112: grp = getgrgid(pw->pw_gid);
113: if (!grp)
114: return 0;
115:
116: /* Return false if user's group is listed in DenyGroups */
117: if (options.num_deny_groups > 0) {
118: if (!grp->gr_name)
119: return 0;
120: for (i = 0; i < options.num_deny_groups; i++)
121: if (match_pattern(grp->gr_name, options.deny_groups[i]))
122: return 0;
123: }
124: /*
125: * Return false if AllowGroups isn't empty and user's group
126: * isn't listed there
127: */
128: if (options.num_allow_groups > 0) {
129: if (!grp->gr_name)
130: return 0;
131: for (i = 0; i < options.num_allow_groups; i++)
132: if (match_pattern(grp->gr_name, options.allow_groups[i]))
133: break;
134: /* i < options.num_allow_groups iff we break for
135: loop */
136: if (i >= options.num_allow_groups)
137: return 0;
138: }
139: }
140: /* We found no reason not to let this user try to log on... */
141: return 1;
142: }