]> andersk Git - openssh.git/blob - auth.c
- markus@cvs.openbsd.org 2001/05/20 17:20:36
[openssh.git] / auth.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
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.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: auth.c,v 1.22 2001/05/20 17:20:35 markus Exp $");
27
28 #ifdef HAVE_LOGIN_H
29 #include <login.h>
30 #endif
31 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
32 #include <shadow.h>
33 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
34
35 #include <libgen.h>
36
37 #include "xmalloc.h"
38 #include "match.h"
39 #include "groupaccess.h"
40 #include "log.h"
41 #include "servconf.h"
42 #include "auth.h"
43 #include "auth-options.h"
44 #include "canohost.h"
45 #include "buffer.h"
46 #include "bufaux.h"
47
48 /* import */
49 extern ServerOptions options;
50
51 /*
52  * Check if the user is allowed to log in via ssh. If user is listed
53  * in DenyUsers or one of user's groups is listed in DenyGroups, false
54  * will be returned. If AllowUsers isn't empty and user isn't listed
55  * there, or if AllowGroups isn't empty and one of user's groups isn't
56  * listed there, false will be returned.
57  * If the user's shell is not executable, false will be returned.
58  * Otherwise true is returned.
59  */
60 int
61 allowed_user(struct passwd * pw)
62 {
63         struct stat st;
64         char *shell;
65         int i;
66 #ifdef WITH_AIXAUTHENTICATE
67         char *loginmsg;
68 #endif /* WITH_AIXAUTHENTICATE */
69 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
70         !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
71         struct spwd *spw;
72
73         /* Shouldn't be called if pw is NULL, but better safe than sorry... */
74         if (!pw || !pw->pw_name)
75                 return 0;
76
77         spw = getspnam(pw->pw_name);
78         if (spw != NULL) {
79                 int days = time(NULL) / 86400;
80
81                 /* Check account expiry */
82                 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
83                         return 0;
84
85                 /* Check password expiry */
86                 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
87                     (days > (spw->sp_lstchg + spw->sp_max)))
88                         return 0;
89         }
90 #else
91         /* Shouldn't be called if pw is NULL, but better safe than sorry... */
92         if (!pw || !pw->pw_name)
93                 return 0;
94 #endif
95
96         /*
97          * Get the shell from the password data.  An empty shell field is
98          * legal, and means /bin/sh.
99          */
100         shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
101
102         /* deny if shell does not exists or is not executable */
103         if (stat(shell, &st) != 0)
104                 return 0;
105         if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
106                 return 0;
107
108         /* Return false if user is listed in DenyUsers */
109         if (options.num_deny_users > 0) {
110                 for (i = 0; i < options.num_deny_users; i++)
111                         if (match_pattern(pw->pw_name, options.deny_users[i]))
112                                 return 0;
113         }
114         /* Return false if AllowUsers isn't empty and user isn't listed there */
115         if (options.num_allow_users > 0) {
116                 for (i = 0; i < options.num_allow_users; i++)
117                         if (match_pattern(pw->pw_name, options.allow_users[i]))
118                                 break;
119                 /* i < options.num_allow_users iff we break for loop */
120                 if (i >= options.num_allow_users)
121                         return 0;
122         }
123         if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
124                 /* Get the user's group access list (primary and supplementary) */
125                 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
126                         return 0;
127
128                 /* Return false if one of user's groups is listed in DenyGroups */
129                 if (options.num_deny_groups > 0)
130                         if (ga_match(options.deny_groups,
131                             options.num_deny_groups)) {
132                                 ga_free();
133                                 return 0;
134                         }
135                 /*
136                  * Return false if AllowGroups isn't empty and one of user's groups
137                  * isn't listed there
138                  */
139                 if (options.num_allow_groups > 0)
140                         if (!ga_match(options.allow_groups,
141                             options.num_allow_groups)) {
142                                 ga_free();
143                                 return 0;
144                         }
145                 ga_free();
146         }
147
148 #ifdef WITH_AIXAUTHENTICATE
149         if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
150                 if (loginmsg && *loginmsg) {
151                         /* Remove embedded newlines (if any) */
152                         char *p;
153                         for (p = loginmsg; *p; p++) {
154                                 if (*p == '\n')
155                                         *p = ' ';
156                         }
157                         /* Remove trailing newline */
158                         *--p = '\0';
159                         log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
160                 }
161                 return 0;
162         }
163 #endif /* WITH_AIXAUTHENTICATE */
164
165         /* We found no reason not to let this user try to log on... */
166         return 1;
167 }
168
169 Authctxt *
170 authctxt_new(void)
171 {
172         Authctxt *authctxt = xmalloc(sizeof(*authctxt));
173         memset(authctxt, 0, sizeof(*authctxt));
174         return authctxt;
175 }
176
177 void
178 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
179 {
180         void (*authlog) (const char *fmt,...) = verbose;
181         char *authmsg;
182
183         /* Raise logging level */
184         if (authenticated == 1 ||
185             !authctxt->valid ||
186             authctxt->failures >= AUTH_FAIL_LOG ||
187             strcmp(method, "password") == 0)
188                 authlog = log;
189
190         if (authctxt->postponed)
191                 authmsg = "Postponed";
192         else
193                 authmsg = authenticated ? "Accepted" : "Failed";
194
195         authlog("%s %s for %s%.100s from %.200s port %d%s",
196             authmsg,
197             method,
198             authctxt->valid ? "" : "illegal user ",
199             authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
200             get_remote_ipaddr(),
201             get_remote_port(),
202             info);
203 }
204
205 /*
206  * Check whether root logins are disallowed.
207  */
208 int
209 auth_root_allowed(char *method)
210 {
211         switch (options.permit_root_login) {
212         case PERMIT_YES:
213                 return 1;
214                 break;
215         case PERMIT_NO_PASSWD:
216                 if (strcmp(method, "password") != 0)
217                         return 1;
218                 break;
219         case PERMIT_FORCED_ONLY:
220                 if (forced_command) {
221                         log("Root login accepted for forced command.");
222                         return 1;
223                 }
224                 break;
225         }
226         log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
227         return 0;
228 }
229
230
231 /*
232  * Given a template and a passwd structure, build a filename
233  * by substituting % tokenised options. Currently, %% becomes '%',
234  * %h becomes the home directory and %u the username.
235  *
236  * This returns a buffer allocated by xmalloc.
237  */
238 char *
239 expand_filename(const char *filename, struct passwd *pw)
240 {
241         Buffer buffer;
242         char *file;
243         const char *cp;
244
245         /*
246          * Build the filename string in the buffer by making the appropriate
247          * substitutions to the given file name.
248          */
249         buffer_init(&buffer);
250         for (cp = filename; *cp; cp++) {
251                 if (cp[0] == '%' && cp[1] == '%') {
252                         buffer_append(&buffer, "%", 1);
253                         cp++;
254                         continue;
255                 }
256                 if (cp[0] == '%' && cp[1] == 'h') {
257                         buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
258                         cp++;
259                         continue;
260                 }
261                 if (cp[0] == '%' && cp[1] == 'u') {
262                         buffer_append(&buffer, pw->pw_name,
263                              strlen(pw->pw_name));
264                         cp++;
265                         continue;
266                 }
267                 buffer_append(&buffer, cp, 1);
268         }
269         buffer_append(&buffer, "\0", 1);
270
271         /*
272          * Ensure that filename starts anchored. If not, be backward
273          * compatible and prepend the '%h/'
274          */
275         file = xmalloc(MAXPATHLEN);
276         cp = buffer_ptr(&buffer);
277         if (*cp != '/')
278                 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
279         else
280                 strlcpy(file, cp, MAXPATHLEN);
281
282         buffer_free(&buffer);
283         return file;
284 }
285
286 char *
287 authorized_keys_file(struct passwd *pw)
288 {
289         return expand_filename(options.authorized_keys_file, pw);
290 }
291
292 char *
293 authorized_keys_file2(struct passwd *pw)
294 {
295         return expand_filename(options.authorized_keys_file2, pw);
296 }
297
298 /*
299  * Check a given file for security. This is defined as all components
300  * of the path to the file must either be owned by either the owner of
301  * of the file or root and no directories must be world writable.
302  *
303  * XXX Should any specific check be done for sym links ?
304  *
305  * Takes an open file descriptor, the file name, a uid and and
306  * error buffer plus max size as arguments.
307  *
308  * Returns 0 on success and -1 on failure
309  */
310 int
311 secure_filename(FILE *f, const char *file, uid_t uid, char *err, size_t errlen)
312 {
313         char buf[MAXPATHLEN];
314         char *cp;
315         struct stat st;
316
317         if (realpath(file, buf) == NULL) {
318                 snprintf(err, errlen, "realpath %s failed: %s", file,
319                     strerror(errno));
320                 return -1;
321         }
322
323         /* check the open file to avoid races */
324         if (fstat(fileno(f), &st) < 0 ||
325             (st.st_uid != 0 && st.st_uid != uid) ||
326             (st.st_mode & 022) != 0) {
327                 snprintf(err, errlen, "bad ownership or modes for file %s",
328                     buf);
329                 return -1;
330         }
331
332         /* for each component of the canonical path, walking upwards */
333         for (;;) {
334                 if ((cp = dirname(buf)) == NULL) {
335                         snprintf(err, errlen, "dirname() failed");
336                         return -1;
337                 }
338                 strlcpy(buf, cp, sizeof(buf));
339
340                 debug3("secure_filename: checking '%s'", buf);
341                 if (stat(buf, &st) < 0 ||
342                     (st.st_uid != 0 && st.st_uid != uid) ||
343                     (st.st_mode & 022) != 0) {
344                         snprintf(err, errlen, 
345                             "bad ownership or modes for directory %s", buf);
346                         return -1;
347                 }
348
349                 /*
350                  * dirname should always complete with a "/" path,
351                  * but we can be paranoid and check for "." too
352                  */
353                 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
354                         break;
355         }
356         return 0;
357 }
This page took 0.070845 seconds and 5 git commands to generate.