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 2 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 */
19 #define PAM_SM_PASSWORD
25 /* This is only used in the Sun implementation. */
26 #include <security/pam_appl.h>
30 #include <security/pam_modules.h>
36 int smb_update_db( pam_handle_t *pamh, int ctrl, const char *user
37 , const char *pass_new )
47 retval = local_password_change( user, LOCAL_SET_PASSWORD, pass_new, err_str, sizeof(err_str),
48 msg_str, sizeof(msg_str) );
52 err_str[PSTRING_LEN-1] = '\0';
53 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
56 /* FIXME: what value is appropriate here? */
57 retval = PAM_AUTHTOK_ERR;
60 msg_str[PSTRING_LEN-1] = '\0';
61 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
73 #define _SMB_OLD_AUTHTOK "-SMB-OLD-PASS"
74 #define _SMB_NEW_AUTHTOK "-SMB-NEW-PASS"
77 * FUNCTION: pam_sm_chauthtok()
79 * This function is called twice by the PAM library, once with
80 * PAM_PRELIM_CHECK set, and then again with PAM_UPDATE_AUTHTOK set. With
81 * Linux-PAM, these two passes generally involve first checking the old
82 * token and then changing the token. This is what we do here.
84 * Having obtained a new password. The function updates the
85 * SMB_PASSWD_FILE file (normally, $(LIBDIR)/smbpasswd).
88 int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
89 int argc, const char **argv)
94 extern BOOL in_client;
96 struct smb_passwd *smb_pwent=NULL;
98 const char *pass_old, *pass_new;
100 /* Samba initialization. */
101 setup_logging( "pam_smbpass", False );
104 ctrl = set_ctrl(flags, argc, argv);
107 * First get the name of a user. No need to do anything if we can't
111 retval = pam_get_user( pamh, &user, "Username: " );
112 if (retval != PAM_SUCCESS) {
113 if (on( SMB_DEBUG, ctrl )) {
114 _log_err( LOG_DEBUG, "password: could not identify user" );
118 if (on( SMB_DEBUG, ctrl )) {
119 _log_err( LOG_DEBUG, "username [%s] obtained", user );
122 if (!initialize_password_db()) {
123 _log_err( LOG_ALERT, "Cannot access samba password database" );
124 return PAM_AUTHINFO_UNAVAIL;
127 /* obtain user record */
128 smb_pwent = getsmbpwnam(user);
130 if (smb_pwent == NULL) {
131 _log_err( LOG_ALERT, "Failed to find entry for user %s.", user );
132 return PAM_USER_UNKNOWN;
135 if (flags & PAM_PRELIM_CHECK) {
137 * obtain and verify the current password (OLDAUTHTOK) for
143 if (_smb_blankpasswd( ctrl, smb_pwent )) {
149 /* Password change by root, or for an expired token, doesn't
150 require authentication. Is this a good choice? */
151 if (getuid() != 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
153 /* tell user what is happening */
154 #define greeting "Changing password for "
155 Announce = (char *) malloc(sizeof(greeting)+strlen(user));
156 if (Announce == NULL) {
157 _log_err(LOG_CRIT, "password: out of memory");
160 strncpy( Announce, greeting, sizeof(greeting) );
161 strncpy( Announce+sizeof(greeting)-1, user, strlen(user)+1 );
164 set( SMB__OLD_PASSWD, ctrl );
165 retval = _smb_read_password( pamh, ctrl
167 , "Current SMB password: "
173 if (retval != PAM_SUCCESS) {
175 , "password - (old) token not obtained" );
179 /* verify that this is the password for this user */
181 retval = _smb_verify_password( pamh, smb_pwent, pass_old, ctrl );
185 retval = PAM_SUCCESS; /* root doesn't have to */
191 } else if (flags & PAM_UPDATE_AUTHTOK) {
193 if (flags & PAM_CHANGE_EXPIRED_AUTHTOK) {
194 /* NOTE: there is currently no support for password expiring
195 under Samba. Support will be added here when it becomes
200 * obtain the proposed password
204 * get the old token back. NULL was ok only if root [at this
205 * point we assume that this has already been enforced on a
206 * previous call to this function].
209 if (off( SMB_NOT_SET_PASS, ctrl )) {
210 retval = pam_get_item( pamh, PAM_OLDAUTHTOK,
211 (const void **)&pass_old );
213 retval = pam_get_data( pamh, _SMB_OLD_AUTHTOK,
214 (const void **)&pass_old );
215 if (retval == PAM_NO_MODULE_DATA) {
217 retval = PAM_SUCCESS;
221 if (retval != PAM_SUCCESS) {
222 _log_err( LOG_NOTICE, "password: user not authenticated" );
227 * use_authtok is to force the use of a previously entered
228 * password -- needed for pluggable password strength checking
229 * or other module stacking
232 if (on( SMB_USE_AUTHTOK, ctrl )) {
233 set( SMB_USE_FIRST_PASS, ctrl );
236 retval = _smb_read_password( pamh, ctrl
238 , "Enter new SMB password: "
239 , "Retype new SMB password: "
243 if (retval != PAM_SUCCESS) {
244 if (on( SMB_DEBUG, ctrl )) {
246 , "password: new password not obtained" );
248 pass_old = NULL; /* tidy up */
253 * At this point we know who the user is and what they
254 * propose as their new password. Verify that the new
255 * password is acceptable.
258 if (pass_new[0] == '\0') { /* "\0" password = NULL */
262 retval = _pam_smb_approve_pass(pamh, ctrl, pass_old, pass_new);
264 if (retval != PAM_SUCCESS) {
265 _log_err(LOG_NOTICE, "new password not acceptable");
266 pass_new = pass_old = NULL; /* tidy up */
271 * By reaching here we have approved the passwords and must now
272 * rebuild the smb password file.
275 /* update the password database */
277 retval = smb_update_db(pamh, ctrl, user, pass_new);
278 if (retval == PAM_SUCCESS) {
279 /* password updated */
280 _log_err( LOG_NOTICE, "password for (%s/%d) changed by (%s/%d)"
281 , user, smb_pwent->smb_userid, uidtoname( getuid() )
284 _log_err( LOG_ERR, "password change failed for user %s"
288 pass_old = pass_new = NULL;
291 } else { /* something has broken with the library */
293 _log_err( LOG_ALERT, "password received unknown request" );
301 /* static module data */
303 struct pam_module _pam_smbpass_passwd_modstruct = {