1 /* Unix NT password database implementation, version 0.7.5.
3 * This program is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 3 of the License, or (at your option)
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 675
15 * Mass Ave, Cambridge, MA 02139, USA.
18 /* indicate the following groups are defined */
26 /* This is only used in the Sun implementation. */
27 #if defined(HAVE_SECURITY_PAM_APPL_H)
28 #include <security/pam_appl.h>
29 #elif defined(HAVE_PAM_PAM_APPL_H)
30 #include <pam/pam_appl.h>
35 #if defined(HAVE_SECURITY_PAM_MODULES_H)
36 #include <security/pam_modules.h>
37 #elif defined(HAVE_PAM_PAM_MODULES_H)
38 #include <pam/pam_modules.h>
47 /* Restore application signal handler */ \
48 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler); \
51 pam_set_data( pamh, "smb_setcred_return" \
52 , (void *) ret_data, NULL ); \
57 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
58 const char *name, struct samu *sampass, BOOL exist);
62 * pam_sm_authenticate() authenticates users against the samba password file.
64 * First, obtain the password from the user. Then use a
65 * routine in 'support.c' to authenticate the user.
68 #define _SMB_AUTHTOK "-SMB-PASS"
70 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
71 int argc, const char **argv)
74 int retval, *ret_data = NULL;
75 struct samu *sampass = NULL;
76 extern BOOL in_client;
78 void (*oldsig_handler)(int) = NULL;
81 /* Points to memory managed by the PAM library. Do not free. */
84 /* Samba initialization. */
86 setup_logging("pam_smbpass",False);
89 ctrl = set_ctrl(flags, argc, argv);
91 /* Get a few bytes so we can pass our return value to
93 ret_data = SMB_MALLOC_P(int);
95 /* we need to do this before we call AUTH_RETURN */
96 /* Getting into places that might use LDAP -- protect the app
97 from a SIGPIPE it's not expecting */
98 oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
100 /* get the username */
101 retval = pam_get_user( pamh, &name, "Username: " );
102 if ( retval != PAM_SUCCESS ) {
103 if (on( SMB_DEBUG, ctrl )) {
104 _log_err(LOG_DEBUG, "auth: could not identify user");
108 if (on( SMB_DEBUG, ctrl )) {
109 _log_err( LOG_DEBUG, "username [%s] obtained", name );
112 if (!initialize_password_db(True, NULL)) {
113 _log_err( LOG_ALERT, "Cannot access samba password database" );
114 retval = PAM_AUTHINFO_UNAVAIL;
118 sampass = samu_new( NULL );
120 _log_err( LOG_ALERT, "Cannot talloc a samu struct" );
121 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
125 found = pdb_getsampwnam( sampass, name );
127 if (on( SMB_MIGRATE, ctrl )) {
128 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
129 TALLOC_FREE(sampass);
134 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
135 retval = PAM_USER_UNKNOWN;
136 TALLOC_FREE(sampass);
141 /* if this user does not have a password... */
143 if (_smb_blankpasswd( ctrl, sampass )) {
144 TALLOC_FREE(sampass);
145 retval = PAM_SUCCESS;
149 /* get this user's authentication token */
151 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
152 if (retval != PAM_SUCCESS ) {
153 _log_err(LOG_CRIT, "auth: no password provided for [%s]", name);
154 TALLOC_FREE(sampass);
158 /* verify the password of this user */
160 retval = _smb_verify_password( pamh, sampass, p, ctrl );
161 TALLOC_FREE(sampass);
167 * This function is for setting samba credentials. If anyone comes up
168 * with any credentials they think should be set, let me know.
171 int pam_sm_setcred(pam_handle_t *pamh, int flags,
172 int argc, const char **argv)
174 int retval, *pretval = NULL;
176 retval = PAM_SUCCESS;
178 pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
183 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
189 /* Helper function for adding a user to the db. */
190 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
191 const char *name, struct samu *sampass, BOOL exist)
195 const char *pass = NULL;
201 /* Get the authtok; if we don't have one, silently fail. */
202 retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
204 if (retval != PAM_SUCCESS) {
206 , "pam_get_item returned error to pam_sm_authenticate" );
207 return PAM_AUTHTOK_RECOVER_ERR;
208 } else if (pass == NULL) {
209 return PAM_AUTHTOK_RECOVER_ERR;
212 /* Add the user to the db if they aren't already there. */
214 retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
217 msg_str, sizeof(msg_str) ));
218 if (!retval && *err_str)
220 err_str[PSTRING_LEN-1] = '\0';
221 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
225 msg_str[PSTRING_LEN-1] = '\0';
226 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
233 /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
234 if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ )
236 retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_SET_PASSWORD, pass, err_str, sizeof(err_str),
237 msg_str, sizeof(msg_str) ));
238 if (!retval && *err_str)
240 err_str[PSTRING_LEN-1] = '\0';
241 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
245 msg_str[PSTRING_LEN-1] = '\0';
246 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
257 /* static module data */
259 struct pam_module _pam_smbpass_auth_modstruct = {