blob: 058dde82be0ab467fe6baadcdf506ae47503abb1 [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 Miller2e1b0821999-12-25 10:11:29 +110012#ifndef HAVE_LIBPAM
Damien Millerdd1c7ba1999-11-19 15:53:20 +110013
Damien Miller2e1b0821999-12-25 10:11:29 +110014RCSID("$Id: auth-passwd.c,v 1.11 1999/12/24 23:11:29 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 Miller2e1b0821999-12-25 10:11:29 +110038 char *pw_password;
39 char *salt;
Damien Miller2cb210f1999-11-13 15:40:10 +110040#ifdef HAVE_SHADOW_H
Damien Miller95def091999-11-25 00:26:21 +110041 struct spwd *spw;
Damien Miller2cb210f1999-11-13 15:40:10 +110042#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller5428f641999-11-25 11:54:57 +110044 if (pw->pw_uid == 0 && options.permit_root_login == 2)
Damien Miller95def091999-11-25 00:26:21 +110045 return 0;
Damien Miller5428f641999-11-25 11:54:57 +110046 if (*password == '\0' && options.permit_empty_passwd == 0)
Damien Miller95def091999-11-25 00:26:21 +110047 return 0;
Damien Miller95def091999-11-25 00:26:21 +110048 /* deny if no user. */
49 if (pw == NULL)
50 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#ifdef SKEY
Damien Miller95def091999-11-25 00:26:21 +110053 if (options.skey_authentication == 1) {
Damien Milleraae6c611999-12-06 11:47:28 +110054 int ret = auth_skey_password(pw, password);
55 if (ret == 1 || ret == 0)
56 return ret;
Damien Miller95def091999-11-25 00:26:21 +110057 /* Fall back to ordinary passwd authentication. */
58 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#endif
Damien Milleraae6c611999-12-06 11:47:28 +110060#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 Miller95def091999-11-25 00:26:21 +110065 /* Fall back to ordinary passwd authentication. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066 }
Damien Milleraae6c611999-12-06 11:47:28 +110067#endif
Damien Miller95def091999-11-25 00:26:21 +110068
69 /* Check for users with no password. */
Damien Miller5428f641999-11-25 11:54:57 +110070 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
Damien Miller95def091999-11-25 00:26:21 +110071 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller2e1b0821999-12-25 10:11:29 +110073 pw_password = pw->pw_passwd;
74
Damien Millercb7e5f91999-12-21 21:03:09 +110075#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
Damien Miller95def091999-11-25 00:26:21 +110076 spw = getspnam(pw->pw_name);
Damien Miller2e1b0821999-12-25 10:11:29 +110077 if (spw == NULL)
Damien Miller95def091999-11-25 00:26:21 +110078 return(0);
Damien Miller2cb210f1999-11-13 15:40:10 +110079
Damien Miller8f9d5071999-12-16 15:10:45 +110080 /* Check for users with no password. */
81 if (strcmp(password, "") == 0 && strcmp(spw->sp_pwdp, "") == 0)
82 return 1;
83
Damien Miller2e1b0821999-12-25 10:11:29 +110084 pw_password = spw->sp_pwdp;
Damien Millercb7e5f91999-12-21 21:03:09 +110085#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
Damien Miller2e1b0821999-12-25 10:11:29 +110086
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 Millerd4a8b7e1999-10-27 13:42:43 +1000103}
Damien Miller2e1b0821999-12-25 10:11:29 +1100104#endif /* !HAVE_LIBPAM */