blob: efae0fd2b04e444b796e2826bb37c83d80db6f56 [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
12#ifndef HAVE_PAM
13
Damien Milleraae6c611999-12-06 11:47:28 +110014RCSID("$Id: auth-passwd.c,v 1.8 1999/12/06 00:47:28 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
22#include <shadow.h>
23#endif
24
Damien Millerdd1c7ba1999-11-19 15:53:20 +110025#ifdef HAVE_MD5_PASSWORDS
26#include "md5crypt.h"
27#endif
28
Damien Miller95def091999-11-25 00:26:21 +110029/*
30 * Tries to authenticate the user using password. Returns true if
31 * authentication succeeds.
32 */
33int
34auth_password(struct passwd * pw, const char *password)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035{
Damien Miller95def091999-11-25 00:26:21 +110036 extern ServerOptions options;
37 char *encrypted_password;
Damien Miller2cb210f1999-11-13 15:40:10 +110038#ifdef HAVE_SHADOW_H
Damien Miller95def091999-11-25 00:26:21 +110039 struct spwd *spw;
Damien Miller2cb210f1999-11-13 15:40:10 +110040#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Miller5428f641999-11-25 11:54:57 +110042 if (pw->pw_uid == 0 && options.permit_root_login == 2)
Damien Miller95def091999-11-25 00:26:21 +110043 return 0;
Damien Miller5428f641999-11-25 11:54:57 +110044 if (*password == '\0' && options.permit_empty_passwd == 0)
Damien Miller95def091999-11-25 00:26:21 +110045 return 0;
Damien Miller95def091999-11-25 00:26:21 +110046 /* deny if no user. */
47 if (pw == NULL)
48 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#ifdef SKEY
Damien Miller95def091999-11-25 00:26:21 +110051 if (options.skey_authentication == 1) {
Damien Milleraae6c611999-12-06 11:47:28 +110052 int ret = auth_skey_password(pw, password);
53 if (ret == 1 || ret == 0)
54 return ret;
Damien Miller95def091999-11-25 00:26:21 +110055 /* Fall back to ordinary passwd authentication. */
56 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#endif
Damien Milleraae6c611999-12-06 11:47:28 +110058#ifdef KRB4
59 if (options.kerberos_authentication == 1) {
60 int ret = auth_krb4_password(pw, password);
61 if (ret == 1 || ret == 0)
62 return ret;
Damien Miller95def091999-11-25 00:26:21 +110063 /* Fall back to ordinary passwd authentication. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064 }
Damien Milleraae6c611999-12-06 11:47:28 +110065#endif
Damien Miller95def091999-11-25 00:26:21 +110066
67 /* Check for users with no password. */
Damien Miller5428f641999-11-25 11:54:57 +110068 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
Damien Miller95def091999-11-25 00:26:21 +110069 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller2cb210f1999-11-13 15:40:10 +110071#ifdef HAVE_SHADOW_H
Damien Miller95def091999-11-25 00:26:21 +110072 spw = getspnam(pw->pw_name);
73 if (spw == NULL)
74 return(0);
Damien Miller2cb210f1999-11-13 15:40:10 +110075
Damien Miller95def091999-11-25 00:26:21 +110076 if ((spw->sp_namp == NULL) || (strcmp(pw->pw_name, spw->sp_namp) != 0))
77 fatal("Shadow lookup returned garbage.");
Damien Miller2cb210f1999-11-13 15:40:10 +110078
Damien Miller95def091999-11-25 00:26:21 +110079 if (strlen(spw->sp_pwdp) < 3)
80 return(0);
Damien Miller2cb210f1999-11-13 15:40:10 +110081
Damien Miller95def091999-11-25 00:26:21 +110082 /* Encrypt the candidate password using the proper salt. */
Damien Millerdd1c7ba1999-11-19 15:53:20 +110083#ifdef HAVE_MD5_PASSWORDS
Damien Miller95def091999-11-25 00:26:21 +110084 if (is_md5_salt(spw->sp_pwdp))
85 encrypted_password = md5_crypt(password, spw->sp_pwdp);
86 else
87 encrypted_password = crypt(password, spw->sp_pwdp);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110088#else /* HAVE_MD5_PASSWORDS */
Damien Miller95def091999-11-25 00:26:21 +110089 encrypted_password = crypt(password, spw->sp_pwdp);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110090#endif /* HAVE_MD5_PASSWORDS */
Damien Miller95def091999-11-25 00:26:21 +110091 /* Authentication is accepted if the encrypted passwords are identical. */
92 return (strcmp(encrypted_password, spw->sp_pwdp) == 0);
Damien Miller2cb210f1999-11-13 15:40:10 +110093#else /* !HAVE_SHADOW_H */
Damien Miller95def091999-11-25 00:26:21 +110094 encrypted_password = crypt(password,
95 (pw->pw_passwd[0] && pw->pw_passwd[1]) ? pw->pw_passwd : "xx");
Damien Miller2cb210f1999-11-13 15:40:10 +110096
Damien Miller95def091999-11-25 00:26:21 +110097 /* Authentication is accepted if the encrypted passwords are identical. */
98 return (strcmp(encrypted_password, pw->pw_passwd) == 0);
Damien Miller2cb210f1999-11-13 15:40:10 +110099#endif /* !HAVE_SHADOW_H */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100}
Damien Miller2cb210f1999-11-13 15:40:10 +1100101#endif /* !HAVE_PAM */