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"
45149749Sdes#include "servconf.h"
46162852Sdes#include "hostfile.h"
47149749Sdes#include "auth.h"
48162852Sdes#include "ssh.h"
49149749Sdes
50149749Sdesint nischeck(char *);
51149749Sdes
52149749Sdesint
53149749Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
54149749Sdes{
55149749Sdes	struct passwd *pw = authctxt->pw;
56149749Sdes	char *salt;
57149749Sdes	int result;
58149749Sdes
59149749Sdes	/* Just use the supplied fake password if authctxt is invalid */
60149749Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
61149749Sdes
62149749Sdes	/* Check for users with no password. */
63149749Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
64149749Sdes		return (1);
65149749Sdes
66149749Sdes	/* Encrypt the candidate password using the proper salt. */
67149749Sdes	salt = (pw_password[0] && pw_password[1]) ? pw_password : "xx";
68149749Sdes
69149749Sdes	/*
70149749Sdes	 * Authentication is accepted if the encrypted passwords
71149749Sdes	 * are identical.
72149749Sdes	 */
73157016Sdes#ifdef UNIXWARE_LONG_PASSWORDS
74157016Sdes	if (!nischeck(pw->pw_name)) {
75157016Sdes		result = ((strcmp(bigcrypt(password, salt), pw_password) == 0)
76157016Sdes		||  (strcmp(osr5bigcrypt(password, salt), pw_password) == 0));
77157016Sdes	}
78157016Sdes	else
79157016Sdes#endif /* UNIXWARE_LONG_PASSWORDS */
80157016Sdes		result = (strcmp(xcrypt(password, salt), pw_password) == 0);
81149749Sdes
82181111Sdes#ifdef USE_LIBIAF
83149749Sdes	if (authctxt->valid)
84149749Sdes		free(pw_password);
85157016Sdes#endif
86149749Sdes	return(result);
87149749Sdes}
88149749Sdes
89149749Sdes#ifdef UNIXWARE_LONG_PASSWORDS
90149749Sdesint
91149749Sdesnischeck(char *namep)
92149749Sdes{
93149749Sdes	char password_file[] = "/etc/passwd";
94149749Sdes	FILE *fd;
95149749Sdes	struct passwd *ent = NULL;
96149749Sdes
97149749Sdes	if ((fd = fopen (password_file, "r")) == NULL) {
98149749Sdes		/*
99149749Sdes		 * If the passwd file has dissapeared we are in a bad state.
100149749Sdes		 * However, returning 0 will send us back through the
101149749Sdes		 * authentication scheme that has checked the ia database for
102149749Sdes		 * passwords earlier.
103149749Sdes		 */
104149749Sdes		return(0);
105149749Sdes	}
106149749Sdes
107149749Sdes	/*
108149749Sdes	 * fgetpwent() only reads from password file, so we know for certain
109149749Sdes	 * that the user is local.
110149749Sdes	 */
111149749Sdes	while (ent = fgetpwent(fd)) {
112149749Sdes		if (strcmp (ent->pw_name, namep) == 0) {
113149749Sdes			/* Local user */
114149749Sdes			fclose (fd);
115149749Sdes			return(0);
116149749Sdes		}
117149749Sdes	}
118149749Sdes
119149749Sdes	fclose (fd);
120149749Sdes	return (1);
121149749Sdes}
122149749Sdes
123149749Sdes#endif /* UNIXWARE_LONG_PASSWORDS */
124149749Sdes
125149749Sdes/*
126149749Sdes	NOTE: ia_get_logpwd() allocates memory for arg 2
127149749Sdes	functions that call shadow_pw() will need to free
128149749Sdes */
129149749Sdes
130181111Sdes#ifdef USE_LIBIAF
131149749Sdeschar *
132149749Sdesget_iaf_password(struct passwd *pw)
133149749Sdes{
134149749Sdes	char *pw_password = NULL;
135149749Sdes
136149749Sdes	uinfo_t uinfo;
137149749Sdes	if (!ia_openinfo(pw->pw_name,&uinfo)) {
138149749Sdes		ia_get_logpwd(uinfo, &pw_password);
139149749Sdes		if (pw_password == NULL)
140149749Sdes			fatal("ia_get_logpwd: Unable to get the shadow passwd");
141149749Sdes		ia_closeinfo(uinfo);
142149749Sdes	 	return pw_password;
143149749Sdes	}
144149749Sdes	else
145149749Sdes		fatal("ia_openinfo: Unable to open the shadow passwd file");
146149749Sdes}
147181111Sdes#endif /* USE_LIBIAF */
148192595Sdes#endif /* HAVE_LIBIAF and not HAVE_SECUREWARE */
149149749Sdes
150