1 /*
2  * Copyright (c) 2005 The SCO Group. All rights reserved.
3  * Copyright (c) 2005 Tim Rice. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "includes.h"
27 
28 #if defined(HAVE_LIBIAF)  &&  !defined(HAVE_SECUREWARE)
29 #include <sys/types.h>
30 #ifdef HAVE_CRYPT_H
31 # include <crypt.h>
32 #endif
33 #include <pwd.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include "xmalloc.h"
40 #include "packet.h"
41 #include "buffer.h"
42 #include "key.h"
43 #include "auth-options.h"
44 #include "log.h"
45 #include "misc.h"	/* servconf.h needs misc.h for struct ForwardOptions */
46 #include "servconf.h"
47 #include "hostfile.h"
48 #include "auth.h"
49 #include "ssh.h"
50 #include "ssh_api.h"
51 
52 int nischeck(char *);
53 
54 int
55 sys_auth_passwd(struct ssh *ssh, const char *password)
56 {
57 	Authctxt *authctxt = ssh->authctxt;
58 	struct passwd *pw = authctxt->pw;
59 	char *salt;
60 	int result;
61 
62 	/* Just use the supplied fake password if authctxt is invalid */
63 	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
64 
65 	/* Check for users with no password. */
66 	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
67 		return (1);
68 
69 	/* Encrypt the candidate password using the proper salt. */
70 	salt = (pw_password[0] && pw_password[1]) ? pw_password : "xx";
71 
72 	/*
73 	 * Authentication is accepted if the encrypted passwords
74 	 * are identical.
75 	 */
76 #ifdef UNIXWARE_LONG_PASSWORDS
77 	if (!nischeck(pw->pw_name)) {
78 		result = ((strcmp(bigcrypt(password, salt), pw_password) == 0)
79 		||  (strcmp(osr5bigcrypt(password, salt), pw_password) == 0));
80 	}
81 	else
82 #endif /* UNIXWARE_LONG_PASSWORDS */
83 		result = (strcmp(xcrypt(password, salt), pw_password) == 0);
84 
85 #ifdef USE_LIBIAF
86 	if (authctxt->valid)
87 		free(pw_password);
88 #endif
89 	return(result);
90 }
91 
92 #ifdef UNIXWARE_LONG_PASSWORDS
93 int
94 nischeck(char *namep)
95 {
96 	char password_file[] = "/etc/passwd";
97 	FILE *fd;
98 	struct passwd *ent = NULL;
99 
100 	if ((fd = fopen (password_file, "r")) == NULL) {
101 		/*
102 		 * If the passwd file has disappeared we are in a bad state.
103 		 * However, returning 0 will send us back through the
104 		 * authentication scheme that has checked the ia database for
105 		 * passwords earlier.
106 		 */
107 		return(0);
108 	}
109 
110 	/*
111 	 * fgetpwent() only reads from password file, so we know for certain
112 	 * that the user is local.
113 	 */
114 	while (ent = fgetpwent(fd)) {
115 		if (strcmp (ent->pw_name, namep) == 0) {
116 			/* Local user */
117 			fclose (fd);
118 			return(0);
119 		}
120 	}
121 
122 	fclose (fd);
123 	return (1);
124 }
125 
126 #endif /* UNIXWARE_LONG_PASSWORDS */
127 
128 /*
129 	NOTE: ia_get_logpwd() allocates memory for arg 2
130 	functions that call shadow_pw() will need to free
131  */
132 
133 #ifdef USE_LIBIAF
134 char *
135 get_iaf_password(struct passwd *pw)
136 {
137 	char *pw_password = NULL;
138 
139 	uinfo_t uinfo;
140 	if (!ia_openinfo(pw->pw_name,&uinfo)) {
141 		ia_get_logpwd(uinfo, &pw_password);
142 		if (pw_password == NULL)
143 			fatal("ia_get_logpwd: Unable to get the shadow passwd");
144 		ia_closeinfo(uinfo);
145 	 	return pw_password;
146 	}
147 	else
148 		fatal("ia_openinfo: Unable to open the shadow passwd file");
149 }
150 #endif /* USE_LIBIAF */
151 #endif /* HAVE_LIBIAF and not HAVE_SECUREWARE */
152 
153