Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
| 5 | * Created: Sat Mar 18 05:11:38 1995 ylo |
| 6 | * Password authentication. This file contains the functions to check whether |
| 7 | * the password is valid for the user. |
| 8 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 9 | |
| 10 | #include "includes.h" |
Damien Miller | dd1c7ba | 1999-11-19 15:53:20 +1100 | [diff] [blame] | 11 | |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 12 | #ifndef HAVE_LIBPAM |
Damien Miller | dd1c7ba | 1999-11-19 15:53:20 +1100 | [diff] [blame] | 13 | |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 14 | RCSID("$Id: auth-passwd.c,v 1.11 1999/12/24 23:11:29 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "packet.h" |
| 17 | #include "ssh.h" |
| 18 | #include "servconf.h" |
| 19 | #include "xmalloc.h" |
Damien Miller | 2cb210f | 1999-11-13 15:40:10 +1100 | [diff] [blame] | 20 | |
| 21 | #ifdef HAVE_SHADOW_H |
| 22 | #include <shadow.h> |
| 23 | #endif |
| 24 | |
Damien Miller | dd1c7ba | 1999-11-19 15:53:20 +1100 | [diff] [blame] | 25 | #ifdef HAVE_MD5_PASSWORDS |
| 26 | #include "md5crypt.h" |
| 27 | #endif |
| 28 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 29 | /* |
| 30 | * Tries to authenticate the user using password. Returns true if |
| 31 | * authentication succeeds. |
| 32 | */ |
| 33 | int |
| 34 | auth_password(struct passwd * pw, const char *password) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | extern ServerOptions options; |
| 37 | char *encrypted_password; |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 38 | char *pw_password; |
| 39 | char *salt; |
Damien Miller | 2cb210f | 1999-11-13 15:40:10 +1100 | [diff] [blame] | 40 | #ifdef HAVE_SHADOW_H |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | struct spwd *spw; |
Damien Miller | 2cb210f | 1999-11-13 15:40:10 +1100 | [diff] [blame] | 42 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 44 | if (pw->pw_uid == 0 && options.permit_root_login == 2) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | return 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 46 | if (*password == '\0' && options.permit_empty_passwd == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | /* deny if no user. */ |
| 49 | if (pw == NULL) |
| 50 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | #ifdef SKEY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | if (options.skey_authentication == 1) { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 54 | int ret = auth_skey_password(pw, password); |
| 55 | if (ret == 1 || ret == 0) |
| 56 | return ret; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | /* Fall back to ordinary passwd authentication. */ |
| 58 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | #endif |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 60 | #ifdef KRB4 |
| 61 | if (options.kerberos_authentication == 1) { |
| 62 | int ret = auth_krb4_password(pw, password); |
| 63 | if (ret == 1 || ret == 0) |
| 64 | return ret; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 65 | /* Fall back to ordinary passwd authentication. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | } |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 67 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | |
| 69 | /* Check for users with no password. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 70 | if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 73 | pw_password = pw->pw_passwd; |
| 74 | |
Damien Miller | cb7e5f9 | 1999-12-21 21:03:09 +1100 | [diff] [blame] | 75 | #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | spw = getspnam(pw->pw_name); |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 77 | if (spw == NULL) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | return(0); |
Damien Miller | 2cb210f | 1999-11-13 15:40:10 +1100 | [diff] [blame] | 79 | |
Damien Miller | 8f9d507 | 1999-12-16 15:10:45 +1100 | [diff] [blame] | 80 | /* Check for users with no password. */ |
| 81 | if (strcmp(password, "") == 0 && strcmp(spw->sp_pwdp, "") == 0) |
| 82 | return 1; |
| 83 | |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 84 | pw_password = spw->sp_pwdp; |
Damien Miller | cb7e5f9 | 1999-12-21 21:03:09 +1100 | [diff] [blame] | 85 | #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 86 | |
| 87 | if (pw_password[0] != '\0') |
| 88 | salt = pw_password; |
| 89 | else |
| 90 | salt = "xx"; |
| 91 | |
| 92 | #ifdef HAVE_MD5_PASSWORDS |
| 93 | if (is_md5_salt(salt)) |
| 94 | encrypted_password = md5_crypt(password, salt); |
| 95 | else |
| 96 | encrypted_password = crypt(password, salt); |
| 97 | #else /* HAVE_MD5_PASSWORDS */ |
| 98 | encrypted_password = crypt(password, salt); |
| 99 | #endif /* HAVE_MD5_PASSWORDS */ |
| 100 | |
| 101 | /* Authentication is accepted if the encrypted passwords are identical. */ |
| 102 | return (strcmp(encrypted_password, pw_password) == 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | } |
Damien Miller | 2e1b082 | 1999-12-25 10:11:29 +1100 | [diff] [blame^] | 104 | #endif /* !HAVE_LIBPAM */ |