2 Unix SMB/CIFS implementation.
3 Use PAM to update user passwords in the local SAM
4 Copyright (C) Steve Langasek 1998-2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* indicate the following groups are defined */
22 #define PAM_SM_PASSWORD
26 /* This is only used in the Sun implementation. FIXME: we really
27 want a define here that distinguishes between the Solaris PAM
28 and others (including FreeBSD). */
31 #if defined(HAVE_SECURITY_PAM_APPL_H)
32 #include <security/pam_appl.h>
33 #elif defined(HAVE_PAM_PAM_APPL_H)
34 #include <pam/pam_appl.h>
38 #if defined(HAVE_SECURITY_PAM_MODULES_H)
39 #include <security/pam_modules.h>
40 #elif defined(HAVE_PAM_PAM_MODULES_H)
41 #include <pam/pam_modules.h>
48 int smb_update_db( pam_handle_t
*pamh
, int ctrl
, const char *user
, const char *pass_new
)
54 retval
= NT_STATUS_IS_OK(local_password_change(user
, LOCAL_SET_PASSWORD
, pass_new
,
60 make_remark(pamh
, ctrl
, PAM_ERROR_MSG
, err_str
);
63 /* FIXME: what value is appropriate here? */
64 retval
= PAM_AUTHTOK_ERR
;
67 make_remark(pamh
, ctrl
, PAM_TEXT_INFO
, msg_str
);
80 #define _SMB_OLD_AUTHTOK "-SMB-OLD-PASS"
81 #define _SMB_NEW_AUTHTOK "-SMB-NEW-PASS"
84 * FUNCTION: pam_sm_chauthtok()
86 * This function is called twice by the PAM library, once with
87 * PAM_PRELIM_CHECK set, and then again with PAM_UPDATE_AUTHTOK set. With
88 * Linux-PAM, these two passes generally involve first checking the old
89 * token and then changing the token. This is what we do here.
91 * Having obtained a new password. The function updates the
92 * SMB_PASSWD_FILE file (normally, $(LIBDIR)/smbpasswd).
95 int pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
96 int argc
, const char **argv
)
101 struct samu
*sampass
= NULL
;
102 void (*oldsig_handler
)(int);
107 /* Samba initialization. */
109 setup_logging( "pam_smbpass", False
);
110 lp_set_in_client(True
);
112 ctrl
= set_ctrl(flags
, argc
, argv
);
115 * First get the name of a user. No need to do anything if we can't
119 retval
= pam_get_user( pamh
, &user
, "Username: " );
120 if (retval
!= PAM_SUCCESS
) {
121 if (on( SMB_DEBUG
, ctrl
)) {
122 _log_err( LOG_DEBUG
, "password: could not identify user" );
126 if (on( SMB_DEBUG
, ctrl
)) {
127 _log_err( LOG_DEBUG
, "username [%s] obtained", user
);
130 if (geteuid() != 0) {
131 _log_err( LOG_DEBUG
, "Cannot access samba password database, not running as root.");
132 return PAM_AUTHINFO_UNAVAIL
;
135 /* Getting into places that might use LDAP -- protect the app
136 from a SIGPIPE it's not expecting */
137 oldsig_handler
= CatchSignal(SIGPIPE
, SIGNAL_CAST SIG_IGN
);
139 if (!initialize_password_db(False
, NULL
)) {
140 _log_err( LOG_ALERT
, "Cannot access samba password database" );
141 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
142 return PAM_AUTHINFO_UNAVAIL
;
145 /* obtain user record */
146 if ( !(sampass
= samu_new( NULL
)) ) {
147 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
148 return nt_status_to_pam(NT_STATUS_NO_MEMORY
);
151 if (!pdb_getsampwnam(sampass
,user
)) {
152 _log_err( LOG_ALERT
, "Failed to find entry for user %s.", user
);
153 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
154 return PAM_USER_UNKNOWN
;
156 if (on( SMB_DEBUG
, ctrl
)) {
157 _log_err( LOG_DEBUG
, "Located account for %s", user
);
160 if (flags
& PAM_PRELIM_CHECK
) {
162 * obtain and verify the current password (OLDAUTHTOK) for
168 if (_smb_blankpasswd( ctrl
, sampass
)) {
170 TALLOC_FREE(sampass
);
171 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
175 /* Password change by root, or for an expired token, doesn't
176 require authentication. Is this a good choice? */
177 if (getuid() != 0 && !(flags
& PAM_CHANGE_EXPIRED_AUTHTOK
)) {
179 /* tell user what is happening */
180 #define greeting "Changing password for "
181 Announce
= SMB_MALLOC_ARRAY(char, sizeof(greeting
)+strlen(user
));
182 if (Announce
== NULL
) {
183 _log_err(LOG_CRIT
, "password: out of memory");
184 TALLOC_FREE(sampass
);
185 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
188 strncpy( Announce
, greeting
, sizeof(greeting
) );
189 strncpy( Announce
+sizeof(greeting
)-1, user
, strlen(user
)+1 );
192 set( SMB__OLD_PASSWD
, ctrl
);
193 retval
= _smb_read_password( pamh
, ctrl
, Announce
, "Current SMB password: ",
194 NULL
, _SMB_OLD_AUTHTOK
, &pass_old
);
195 SAFE_FREE( Announce
);
197 if (retval
!= PAM_SUCCESS
) {
199 , "password - (old) token not obtained" );
200 TALLOC_FREE(sampass
);
201 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
205 /* verify that this is the password for this user */
207 retval
= _smb_verify_password( pamh
, sampass
, pass_old
, ctrl
);
211 retval
= PAM_SUCCESS
; /* root doesn't have to */
215 TALLOC_FREE(sampass
);
216 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
219 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
222 * obtain the proposed password
226 * get the old token back. NULL was ok only if root [at this
227 * point we assume that this has already been enforced on a
228 * previous call to this function].
231 if (off( SMB_NOT_SET_PASS
, ctrl
)) {
232 retval
= pam_get_item( pamh
, PAM_OLDAUTHTOK
,
233 (const void **)&pass_old
);
235 retval
= pam_get_data( pamh
, _SMB_OLD_AUTHTOK
,
236 (const void **)&pass_old
);
237 if (retval
== PAM_NO_MODULE_DATA
) {
239 retval
= PAM_SUCCESS
;
243 if (retval
!= PAM_SUCCESS
) {
244 _log_err( LOG_NOTICE
, "password: user not authenticated" );
245 TALLOC_FREE(sampass
);
246 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
251 * use_authtok is to force the use of a previously entered
252 * password -- needed for pluggable password strength checking
253 * or other module stacking
256 if (on( SMB_USE_AUTHTOK
, ctrl
)) {
257 set( SMB_USE_FIRST_PASS
, ctrl
);
260 retval
= _smb_read_password( pamh
, ctrl
262 , "Enter new SMB password: "
263 , "Retype new SMB password: "
267 if (retval
!= PAM_SUCCESS
) {
268 if (on( SMB_DEBUG
, ctrl
)) {
270 , "password: new password not obtained" );
272 pass_old
= NULL
; /* tidy up */
273 TALLOC_FREE(sampass
);
274 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
279 * At this point we know who the user is and what they
280 * propose as their new password. Verify that the new
281 * password is acceptable.
284 if (pass_new
[0] == '\0') { /* "\0" password = NULL */
288 retval
= _pam_smb_approve_pass(pamh
, ctrl
, pass_old
, pass_new
);
290 if (retval
!= PAM_SUCCESS
) {
291 _log_err(LOG_NOTICE
, "new password not acceptable");
292 pass_new
= pass_old
= NULL
; /* tidy up */
293 TALLOC_FREE(sampass
);
294 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
299 * By reaching here we have approved the passwords and must now
300 * rebuild the smb password file.
303 /* update the password database */
305 retval
= smb_update_db(pamh
, ctrl
, user
, pass_new
);
306 if (retval
== PAM_SUCCESS
) {
309 /* password updated */
310 if (!sid_to_uid(pdb_get_user_sid(sampass
), &uid
)) {
311 _log_err( LOG_NOTICE
, "Unable to get uid for user %s",
312 pdb_get_username(sampass
));
313 _log_err( LOG_NOTICE
, "password for (%s) changed by (%s/%d)",
314 user
, uidtoname(getuid()), getuid());
316 _log_err( LOG_NOTICE
, "password for (%s/%d) changed by (%s/%d)",
317 user
, uid
, uidtoname(getuid()), getuid());
320 _log_err( LOG_ERR
, "password change failed for user %s", user
);
323 pass_old
= pass_new
= NULL
;
325 TALLOC_FREE(sampass
);
329 } else { /* something has broken with the library */
331 _log_err( LOG_ALERT
, "password received unknown request" );
337 TALLOC_FREE(sampass
);
341 TALLOC_FREE(sampass
);
342 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
346 /* static module data */
348 struct pam_module _pam_smbpass_passwd_modstruct
= {