1 /*-
2  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3  * Copyright (c) 2004-2011 Dag-Erling Smørgrav
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by ThinkSec AS and
7  * Network Associates Laboratories, the Security Research Division of
8  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
9  * ("CBOSS"), as part of the DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $Id: pam_unix.c 648 2013-03-05 17:54:27Z des $
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41 
42 #include <sys/param.h>
43 
44 #include <pwd.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #ifdef HAVE_CRYPT_H
51 # include <crypt.h>
52 #endif
53 
54 #include <security/pam_modules.h>
55 #include <security/pam_appl.h>
56 
57 #ifndef OPENPAM
58 static char password_prompt[] = "Password:";
59 #endif
60 
61 #ifndef PAM_EXTERN
62 #define PAM_EXTERN
63 #endif
64 
65 PAM_EXTERN int
66 pam_sm_authenticate(pam_handle_t *pamh, int flags,
67 	int argc, const char *argv[])
68 {
69 #ifndef OPENPAM
70 	struct pam_conv *conv;
71 	struct pam_message msg;
72 	const struct pam_message *msgp;
73 	struct pam_response *resp;
74 #endif
75 	struct passwd *pwd;
76 	const char *user;
77 	const char *crypt_password, *password;
78 	int pam_err, retry;
79 
80 	(void)argc;
81 	(void)argv;
82 
83 	/* identify user */
84 	if ((pam_err = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS)
85 		return (pam_err);
86 	if ((pwd = getpwnam(user)) == NULL)
87 		return (PAM_USER_UNKNOWN);
88 
89 	/* get password */
90 #ifndef OPENPAM
91 	pam_err = pam_get_item(pamh, PAM_CONV, (const void **)&conv);
92 	if (pam_err != PAM_SUCCESS)
93 		return (PAM_SYSTEM_ERR);
94 	msg.msg_style = PAM_PROMPT_ECHO_OFF;
95 	msg.msg = password_prompt;
96 	msgp = &msg;
97 #endif
98 	for (retry = 0; retry < 3; ++retry) {
99 #ifdef OPENPAM
100 		pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
101 		    &password, NULL);
102 #else
103 		resp = NULL;
104 		pam_err = (*conv->conv)(1, &msgp, &resp, conv->appdata_ptr);
105 		if (resp != NULL) {
106 			if (pam_err == PAM_SUCCESS)
107 				password = resp->resp;
108 			else
109 				free(resp->resp);
110 			free(resp);
111 		}
112 #endif
113 		if (pam_err == PAM_SUCCESS)
114 			break;
115 	}
116 	if (pam_err == PAM_CONV_ERR)
117 		return (pam_err);
118 	if (pam_err != PAM_SUCCESS)
119 		return (PAM_AUTH_ERR);
120 
121 	/* compare passwords */
122 	if ((!pwd->pw_passwd[0] && (flags & PAM_DISALLOW_NULL_AUTHTOK)) ||
123 	    (crypt_password = crypt(password, pwd->pw_passwd)) == NULL ||
124 	    strcmp(crypt_password, pwd->pw_passwd) != 0)
125 		pam_err = PAM_AUTH_ERR;
126 	else
127 		pam_err = PAM_SUCCESS;
128 #ifndef OPENPAM
129 	free(password);
130 #endif
131 	return (pam_err);
132 }
133 
134 PAM_EXTERN int
135 pam_sm_setcred(pam_handle_t *pamh, int flags,
136 	int argc, const char *argv[])
137 {
138 
139 	(void)pamh;
140 	(void)flags;
141 	(void)argc;
142 	(void)argv;
143 	return (PAM_SUCCESS);
144 }
145 
146 PAM_EXTERN int
147 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
148 	int argc, const char *argv[])
149 {
150 
151 	(void)pamh;
152 	(void)flags;
153 	(void)argc;
154 	(void)argv;
155 	return (PAM_SUCCESS);
156 }
157 
158 PAM_EXTERN int
159 pam_sm_open_session(pam_handle_t *pamh, int flags,
160 	int argc, const char *argv[])
161 {
162 
163 	(void)pamh;
164 	(void)flags;
165 	(void)argc;
166 	(void)argv;
167 	return (PAM_SUCCESS);
168 }
169 
170 PAM_EXTERN int
171 pam_sm_close_session(pam_handle_t *pamh, int flags,
172 	int argc, const char *argv[])
173 {
174 
175 	(void)pamh;
176 	(void)flags;
177 	(void)argc;
178 	(void)argv;
179 	return (PAM_SUCCESS);
180 }
181 
182 PAM_EXTERN int
183 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
184 	int argc, const char *argv[])
185 {
186 
187 	(void)pamh;
188 	(void)flags;
189 	(void)argc;
190 	(void)argv;
191 	return (PAM_SERVICE_ERR);
192 }
193 
194 #ifdef PAM_MODULE_ENTRY
195 PAM_MODULE_ENTRY("pam_unix");
196 #endif
197