1149749Sdes/*
2149749Sdes * Copyright (c) 2005 The SCO Group. All rights reserved.
3149749Sdes * Copyright (c) 2005 Tim Rice. All rights reserved.
4149749Sdes *
5149749Sdes * Redistribution and use in source and binary forms, with or without
6149749Sdes * modification, are permitted provided that the following conditions
7149749Sdes * are met:
8149749Sdes * 1. Redistributions of source code must retain the above copyright
9149749Sdes *    notice, this list of conditions and the following disclaimer.
10149749Sdes * 2. Redistributions in binary form must reproduce the above copyright
11149749Sdes *    notice, this list of conditions and the following disclaimer in the
12149749Sdes *    documentation and/or other materials provided with the distribution.
13149749Sdes *
14149749Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15149749Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16149749Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17149749Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18149749Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19149749Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20149749Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21149749Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22149749Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23149749Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24149749Sdes */
25149749Sdes
26149749Sdes#include "includes.h"
27149749Sdes
28192595Sdes#if defined(HAVE_LIBIAF)  &&  !defined(HAVE_SECUREWARE)
29162852Sdes#include <sys/types.h>
30149749Sdes#ifdef HAVE_CRYPT_H
31162852Sdes# include <crypt.h>
32149749Sdes#endif
33162852Sdes#include <pwd.h>
34162852Sdes#include <stdarg.h>
35162852Sdes#include <stdlib.h>
36162852Sdes#include <stdio.h>
37162852Sdes#include <string.h>
38162852Sdes
39162852Sdes#include "xmalloc.h"
40149749Sdes#include "packet.h"
41149749Sdes#include "buffer.h"
42215116Sdes#include "key.h"
43162852Sdes#include "auth-options.h"
44149749Sdes#include "log.h"
45294328Sdes#include "misc.h"	/* servconf.h needs misc.h for struct ForwardOptions */
46149749Sdes#include "servconf.h"
47162852Sdes#include "hostfile.h"
48149749Sdes#include "auth.h"
49162852Sdes#include "ssh.h"
50149749Sdes
51149749Sdesint nischeck(char *);
52149749Sdes
53149749Sdesint
54149749Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
55149749Sdes{
56149749Sdes	struct passwd *pw = authctxt->pw;
57149749Sdes	char *salt;
58149749Sdes	int result;
59149749Sdes
60149749Sdes	/* Just use the supplied fake password if authctxt is invalid */
61149749Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
62149749Sdes
63149749Sdes	/* Check for users with no password. */
64149749Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
65149749Sdes		return (1);
66149749Sdes
67149749Sdes	/* Encrypt the candidate password using the proper salt. */
68149749Sdes	salt = (pw_password[0] && pw_password[1]) ? pw_password : "xx";
69149749Sdes
70149749Sdes	/*
71149749Sdes	 * Authentication is accepted if the encrypted passwords
72149749Sdes	 * are identical.
73149749Sdes	 */
74157016Sdes#ifdef UNIXWARE_LONG_PASSWORDS
75157016Sdes	if (!nischeck(pw->pw_name)) {
76157016Sdes		result = ((strcmp(bigcrypt(password, salt), pw_password) == 0)
77157016Sdes		||  (strcmp(osr5bigcrypt(password, salt), pw_password) == 0));
78157016Sdes	}
79157016Sdes	else
80157016Sdes#endif /* UNIXWARE_LONG_PASSWORDS */
81157016Sdes		result = (strcmp(xcrypt(password, salt), pw_password) == 0);
82149749Sdes
83181111Sdes#ifdef USE_LIBIAF
84149749Sdes	if (authctxt->valid)
85149749Sdes		free(pw_password);
86157016Sdes#endif
87149749Sdes	return(result);
88149749Sdes}
89149749Sdes
90149749Sdes#ifdef UNIXWARE_LONG_PASSWORDS
91149749Sdesint
92149749Sdesnischeck(char *namep)
93149749Sdes{
94149749Sdes	char password_file[] = "/etc/passwd";
95149749Sdes	FILE *fd;
96149749Sdes	struct passwd *ent = NULL;
97149749Sdes
98149749Sdes	if ((fd = fopen (password_file, "r")) == NULL) {
99149749Sdes		/*
100149749Sdes		 * If the passwd file has dissapeared we are in a bad state.
101149749Sdes		 * However, returning 0 will send us back through the
102149749Sdes		 * authentication scheme that has checked the ia database for
103149749Sdes		 * passwords earlier.
104149749Sdes		 */
105149749Sdes		return(0);
106149749Sdes	}
107149749Sdes
108149749Sdes	/*
109149749Sdes	 * fgetpwent() only reads from password file, so we know for certain
110149749Sdes	 * that the user is local.
111149749Sdes	 */
112149749Sdes	while (ent = fgetpwent(fd)) {
113149749Sdes		if (strcmp (ent->pw_name, namep) == 0) {
114149749Sdes			/* Local user */
115149749Sdes			fclose (fd);
116149749Sdes			return(0);
117149749Sdes		}
118149749Sdes	}
119149749Sdes
120149749Sdes	fclose (fd);
121149749Sdes	return (1);
122149749Sdes}
123149749Sdes
124149749Sdes#endif /* UNIXWARE_LONG_PASSWORDS */
125149749Sdes
126149749Sdes/*
127149749Sdes	NOTE: ia_get_logpwd() allocates memory for arg 2
128149749Sdes	functions that call shadow_pw() will need to free
129149749Sdes */
130149749Sdes
131181111Sdes#ifdef USE_LIBIAF
132149749Sdeschar *
133149749Sdesget_iaf_password(struct passwd *pw)
134149749Sdes{
135149749Sdes	char *pw_password = NULL;
136149749Sdes
137149749Sdes	uinfo_t uinfo;
138149749Sdes	if (!ia_openinfo(pw->pw_name,&uinfo)) {
139149749Sdes		ia_get_logpwd(uinfo, &pw_password);
140149749Sdes		if (pw_password == NULL)
141149749Sdes			fatal("ia_get_logpwd: Unable to get the shadow passwd");
142149749Sdes		ia_closeinfo(uinfo);
143149749Sdes	 	return pw_password;
144149749Sdes	}
145149749Sdes	else
146149749Sdes		fatal("ia_openinfo: Unable to open the shadow passwd file");
147149749Sdes}
148181111Sdes#endif /* USE_LIBIAF */
149192595Sdes#endif /* HAVE_LIBIAF and not HAVE_SECUREWARE */
150149749Sdes
151