xref: /titanic_44/usr/src/cmd/ssh/sshd/auth-passwd.c (revision b9aa66a73c9016cf5c71fe80efe90ce9f2ca5c73)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*7c478bd9Sstevel@tonic-gate  *                    All rights reserved
5*7c478bd9Sstevel@tonic-gate  * Password authentication.  This file contains the functions to check whether
6*7c478bd9Sstevel@tonic-gate  * the password is valid for the user.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
9*7c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
10*7c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
11*7c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
12*7c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Dug Song.  All rights reserved.
15*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
18*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
19*7c478bd9Sstevel@tonic-gate  * are met:
20*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
21*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
22*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
23*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
24*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "includes.h"
39*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth-passwd.c,v 1.27 2002/05/24 16:45:16 stevesk Exp $");
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "packet.h"
42*7c478bd9Sstevel@tonic-gate #include "log.h"
43*7c478bd9Sstevel@tonic-gate #include "servconf.h"
44*7c478bd9Sstevel@tonic-gate #include "auth.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
47*7c478bd9Sstevel@tonic-gate /* Don't need any of these headers for the PAM or SIA cases */
48*7c478bd9Sstevel@tonic-gate # ifdef HAVE_CRYPT_H
49*7c478bd9Sstevel@tonic-gate #  include <crypt.h>
50*7c478bd9Sstevel@tonic-gate # endif
51*7c478bd9Sstevel@tonic-gate # ifdef WITH_AIXAUTHENTICATE
52*7c478bd9Sstevel@tonic-gate #  include <login.h>
53*7c478bd9Sstevel@tonic-gate # endif
54*7c478bd9Sstevel@tonic-gate # ifdef __hpux
55*7c478bd9Sstevel@tonic-gate #  include <hpsecurity.h>
56*7c478bd9Sstevel@tonic-gate #  include <prot.h>
57*7c478bd9Sstevel@tonic-gate # endif
58*7c478bd9Sstevel@tonic-gate # if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
59*7c478bd9Sstevel@tonic-gate #  include <shadow.h>
60*7c478bd9Sstevel@tonic-gate # endif
61*7c478bd9Sstevel@tonic-gate # if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
62*7c478bd9Sstevel@tonic-gate #  include <sys/label.h>
63*7c478bd9Sstevel@tonic-gate #  include <sys/audit.h>
64*7c478bd9Sstevel@tonic-gate #  include <pwdadj.h>
65*7c478bd9Sstevel@tonic-gate # endif
66*7c478bd9Sstevel@tonic-gate # if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
67*7c478bd9Sstevel@tonic-gate #  include "md5crypt.h"
68*7c478bd9Sstevel@tonic-gate # endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate # ifdef HAVE_CYGWIN
71*7c478bd9Sstevel@tonic-gate #  undef ERROR
72*7c478bd9Sstevel@tonic-gate #  include <windows.h>
73*7c478bd9Sstevel@tonic-gate #  include <sys/cygwin.h>
74*7c478bd9Sstevel@tonic-gate #  define is_winnt       (GetVersion() < 0x80000000)
75*7c478bd9Sstevel@tonic-gate # endif
76*7c478bd9Sstevel@tonic-gate #endif /* !USE_PAM && !HAVE_OSF_SIA */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate extern ServerOptions options;
79*7c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
80*7c478bd9Sstevel@tonic-gate extern char *aixloginmsg;
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * Tries to authenticate the user using password.  Returns true if
85*7c478bd9Sstevel@tonic-gate  * authentication succeeds.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate int
auth_password(Authctxt * authctxt,const char * password)88*7c478bd9Sstevel@tonic-gate auth_password(Authctxt *authctxt, const char *password)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate #if defined(USE_PAM)
91*7c478bd9Sstevel@tonic-gate 	if (*password == '\0' && options.permit_empty_passwd == 0)
92*7c478bd9Sstevel@tonic-gate 		return 0;
93*7c478bd9Sstevel@tonic-gate 	return auth_pam_password(authctxt, password);
94*7c478bd9Sstevel@tonic-gate #elif defined(HAVE_OSF_SIA)
95*7c478bd9Sstevel@tonic-gate 	if (*password == '\0' && options.permit_empty_passwd == 0)
96*7c478bd9Sstevel@tonic-gate 		return 0;
97*7c478bd9Sstevel@tonic-gate 	return auth_sia_password(authctxt, password);
98*7c478bd9Sstevel@tonic-gate #else
99*7c478bd9Sstevel@tonic-gate 	struct passwd * pw = authctxt->pw;
100*7c478bd9Sstevel@tonic-gate 	char *encrypted_password;
101*7c478bd9Sstevel@tonic-gate 	char *pw_password;
102*7c478bd9Sstevel@tonic-gate 	char *salt;
103*7c478bd9Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
104*7c478bd9Sstevel@tonic-gate 	struct spwd *spw;
105*7c478bd9Sstevel@tonic-gate #endif
106*7c478bd9Sstevel@tonic-gate #if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
107*7c478bd9Sstevel@tonic-gate 	struct passwd_adjunct *spw;
108*7c478bd9Sstevel@tonic-gate #endif
109*7c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
110*7c478bd9Sstevel@tonic-gate 	char *authmsg;
111*7c478bd9Sstevel@tonic-gate 	int authsuccess;
112*7c478bd9Sstevel@tonic-gate 	int reenter = 1;
113*7c478bd9Sstevel@tonic-gate #endif
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	/* deny if no user. */
116*7c478bd9Sstevel@tonic-gate 	if (pw == NULL)
117*7c478bd9Sstevel@tonic-gate 		return 0;
118*7c478bd9Sstevel@tonic-gate #ifndef HAVE_CYGWIN
119*7c478bd9Sstevel@tonic-gate        if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
120*7c478bd9Sstevel@tonic-gate 		return 0;
121*7c478bd9Sstevel@tonic-gate #endif
122*7c478bd9Sstevel@tonic-gate 	if (*password == '\0' && options.permit_empty_passwd == 0)
123*7c478bd9Sstevel@tonic-gate 		return 0;
124*7c478bd9Sstevel@tonic-gate #ifdef KRB5
125*7c478bd9Sstevel@tonic-gate 	if (options.kerberos_authentication == 1) {
126*7c478bd9Sstevel@tonic-gate 		int ret = auth_krb5_password(authctxt, password);
127*7c478bd9Sstevel@tonic-gate 		if (ret == 1 || ret == 0)
128*7c478bd9Sstevel@tonic-gate 			return ret;
129*7c478bd9Sstevel@tonic-gate 		/* Fall back to ordinary passwd authentication. */
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate #endif
132*7c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
133*7c478bd9Sstevel@tonic-gate 	if (is_winnt) {
134*7c478bd9Sstevel@tonic-gate 		HANDLE hToken = cygwin_logon_user(pw, password);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 		if (hToken == INVALID_HANDLE_VALUE)
137*7c478bd9Sstevel@tonic-gate 			return 0;
138*7c478bd9Sstevel@tonic-gate 		cygwin_set_impersonation_token(hToken);
139*7c478bd9Sstevel@tonic-gate 		return 1;
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate #endif
142*7c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
143*7c478bd9Sstevel@tonic-gate 	authsuccess = (authenticate(pw->pw_name,password,&reenter,&authmsg) == 0);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if (authsuccess)
146*7c478bd9Sstevel@tonic-gate 	        /* We don't have a pty yet, so just label the line as "ssh" */
147*7c478bd9Sstevel@tonic-gate 	        if (loginsuccess(authctxt->user,
148*7c478bd9Sstevel@tonic-gate 			get_canonical_hostname(options.verify_reverse_mapping),
149*7c478bd9Sstevel@tonic-gate 			"ssh", &aixloginmsg) < 0)
150*7c478bd9Sstevel@tonic-gate 				aixloginmsg = NULL;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	return(authsuccess);
153*7c478bd9Sstevel@tonic-gate #endif
154*7c478bd9Sstevel@tonic-gate #ifdef KRB4
155*7c478bd9Sstevel@tonic-gate 	if (options.kerberos_authentication == 1) {
156*7c478bd9Sstevel@tonic-gate 		int ret = auth_krb4_password(authctxt, password);
157*7c478bd9Sstevel@tonic-gate 		if (ret == 1 || ret == 0)
158*7c478bd9Sstevel@tonic-gate 			return ret;
159*7c478bd9Sstevel@tonic-gate 		/* Fall back to ordinary passwd authentication. */
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate #endif
162*7c478bd9Sstevel@tonic-gate #ifdef BSD_AUTH
163*7c478bd9Sstevel@tonic-gate 	if (auth_userokay(pw->pw_name, authctxt->style, "auth-ssh",
164*7c478bd9Sstevel@tonic-gate 	    (char *)password) == 0)
165*7c478bd9Sstevel@tonic-gate 		return 0;
166*7c478bd9Sstevel@tonic-gate 	else
167*7c478bd9Sstevel@tonic-gate 		return 1;
168*7c478bd9Sstevel@tonic-gate #endif
169*7c478bd9Sstevel@tonic-gate 	pw_password = pw->pw_passwd;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	/*
172*7c478bd9Sstevel@tonic-gate 	 * Various interfaces to shadow or protected password data
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
175*7c478bd9Sstevel@tonic-gate 	spw = getspnam(pw->pw_name);
176*7c478bd9Sstevel@tonic-gate 	if (spw != NULL)
177*7c478bd9Sstevel@tonic-gate 		pw_password = spw->sp_pwdp;
178*7c478bd9Sstevel@tonic-gate #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate #if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
181*7c478bd9Sstevel@tonic-gate 	if (issecure() && (spw = getpwanam(pw->pw_name)) != NULL)
182*7c478bd9Sstevel@tonic-gate 		pw_password = spw->pwa_passwd;
183*7c478bd9Sstevel@tonic-gate #endif /* defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW) */
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/* Check for users with no password. */
186*7c478bd9Sstevel@tonic-gate 	if ((password[0] == '\0') && (pw_password[0] == '\0'))
187*7c478bd9Sstevel@tonic-gate 		return 1;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	if (pw_password[0] != '\0')
190*7c478bd9Sstevel@tonic-gate 		salt = pw_password;
191*7c478bd9Sstevel@tonic-gate 	else
192*7c478bd9Sstevel@tonic-gate 		salt = "xx";
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MD5_PASSWORDS
195*7c478bd9Sstevel@tonic-gate 	if (is_md5_salt(salt))
196*7c478bd9Sstevel@tonic-gate 		encrypted_password = md5_crypt(password, salt);
197*7c478bd9Sstevel@tonic-gate 	else
198*7c478bd9Sstevel@tonic-gate 		encrypted_password = crypt(password, salt);
199*7c478bd9Sstevel@tonic-gate #else /* HAVE_MD5_PASSWORDS */
200*7c478bd9Sstevel@tonic-gate 	encrypted_password = crypt(password, salt);
201*7c478bd9Sstevel@tonic-gate #endif /* HAVE_MD5_PASSWORDS */
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/* Authentication is accepted if the encrypted passwords are identical. */
204*7c478bd9Sstevel@tonic-gate 	return (strcmp(encrypted_password, pw_password) == 0);
205*7c478bd9Sstevel@tonic-gate #endif /* !USE_PAM && !HAVE_OSF_SIA */
206*7c478bd9Sstevel@tonic-gate }
207