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