blob: 0311a493e7a07beaf322cd5c111e199f8162b0aa [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * 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 Millerd4a8b7e1999-10-27 13:42:43 +10009
10#include "includes.h"
Damien Millerdd1c7ba1999-11-19 15:53:20 +110011
Damien Millerbeb4ba51999-12-28 15:09:35 +110012#ifndef USE_PAM
Damien Millerdd1c7ba1999-11-19 15:53:20 +110013
Damien Millerbeb4ba51999-12-28 15:09:35 +110014RCSID("$Id: auth-passwd.c,v 1.12 1999/12/28 04:09:36 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "packet.h"
17#include "ssh.h"
18#include "servconf.h"
19#include "xmalloc.h"
Damien Miller2cb210f1999-11-13 15:40:10 +110020
21#ifdef HAVE_SHADOW_H
Damien Millerbeb4ba51999-12-28 15:09:35 +110022# include <shadow.h>
Damien Miller2cb210f1999-11-13 15:40:10 +110023#endif
Damien Millerbeb4ba51999-12-28 15:09:35 +110024#if defined(HAVE_CRYPT_H) && !defined(CRYPT_H_BREAKS_BUILD)
25# include <crypt.h>
26#endif /* defined(HAVE_CRYPT_H) && !defined(CRYPT_H_BREAKS_BUILD) */
27#if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
28# include "md5crypt.h"
29#endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */
Damien Millerdd1c7ba1999-11-19 15:53:20 +110030
Damien Miller95def091999-11-25 00:26:21 +110031/*
32 * Tries to authenticate the user using password. Returns true if
33 * authentication succeeds.
34 */
35int
36auth_password(struct passwd * pw, const char *password)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037{
Damien Miller95def091999-11-25 00:26:21 +110038 extern ServerOptions options;
39 char *encrypted_password;
Damien Miller2e1b0821999-12-25 10:11:29 +110040 char *pw_password;
41 char *salt;
Damien Miller2cb210f1999-11-13 15:40:10 +110042#ifdef HAVE_SHADOW_H
Damien Miller95def091999-11-25 00:26:21 +110043 struct spwd *spw;
Damien Miller2cb210f1999-11-13 15:40:10 +110044#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller5428f641999-11-25 11:54:57 +110046 if (pw->pw_uid == 0 && options.permit_root_login == 2)
Damien Miller95def091999-11-25 00:26:21 +110047 return 0;
Damien Miller5428f641999-11-25 11:54:57 +110048 if (*password == '\0' && options.permit_empty_passwd == 0)
Damien Miller95def091999-11-25 00:26:21 +110049 return 0;
Damien Miller95def091999-11-25 00:26:21 +110050 /* deny if no user. */
51 if (pw == NULL)
52 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#ifdef SKEY
Damien Miller95def091999-11-25 00:26:21 +110055 if (options.skey_authentication == 1) {
Damien Milleraae6c611999-12-06 11:47:28 +110056 int ret = auth_skey_password(pw, password);
57 if (ret == 1 || ret == 0)
58 return ret;
Damien Miller95def091999-11-25 00:26:21 +110059 /* Fall back to ordinary passwd authentication. */
60 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#endif
Damien Milleraae6c611999-12-06 11:47:28 +110062#ifdef KRB4
63 if (options.kerberos_authentication == 1) {
64 int ret = auth_krb4_password(pw, password);
65 if (ret == 1 || ret == 0)
66 return ret;
Damien Miller95def091999-11-25 00:26:21 +110067 /* Fall back to ordinary passwd authentication. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068 }
Damien Milleraae6c611999-12-06 11:47:28 +110069#endif
Damien Miller95def091999-11-25 00:26:21 +110070
71 /* Check for users with no password. */
Damien Miller5428f641999-11-25 11:54:57 +110072 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
Damien Miller95def091999-11-25 00:26:21 +110073 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller2e1b0821999-12-25 10:11:29 +110075 pw_password = pw->pw_passwd;
76
Damien Millercb7e5f91999-12-21 21:03:09 +110077#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
Damien Miller95def091999-11-25 00:26:21 +110078 spw = getspnam(pw->pw_name);
Damien Miller2e1b0821999-12-25 10:11:29 +110079 if (spw == NULL)
Damien Miller95def091999-11-25 00:26:21 +110080 return(0);
Damien Miller2cb210f1999-11-13 15:40:10 +110081
Damien Miller8f9d5071999-12-16 15:10:45 +110082 /* Check for users with no password. */
83 if (strcmp(password, "") == 0 && strcmp(spw->sp_pwdp, "") == 0)
84 return 1;
85
Damien Miller2e1b0821999-12-25 10:11:29 +110086 pw_password = spw->sp_pwdp;
Damien Millercb7e5f91999-12-21 21:03:09 +110087#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
Damien Miller2e1b0821999-12-25 10:11:29 +110088
89 if (pw_password[0] != '\0')
90 salt = pw_password;
91 else
92 salt = "xx";
93
94#ifdef HAVE_MD5_PASSWORDS
95 if (is_md5_salt(salt))
96 encrypted_password = md5_crypt(password, salt);
97 else
98 encrypted_password = crypt(password, salt);
99#else /* HAVE_MD5_PASSWORDS */
100 encrypted_password = crypt(password, salt);
101#endif /* HAVE_MD5_PASSWORDS */
102
103 /* Authentication is accepted if the encrypted passwords are identical. */
104 return (strcmp(encrypted_password, pw_password) == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105}
Damien Millerbeb4ba51999-12-28 15:09:35 +1100106#endif /* !USE_PAM */