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