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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* indicate the following groups are defined */
23 #define PAM_SM_PASSWORD
27 /* This is only used in the Sun implementation. FIXME: we really
28 want a define here that distinguishes between the Solaris PAM
29 and others (including FreeBSD). */
32 #include <security/pam_appl.h>
35 #include <security/pam_modules.h>
41 int smb_update_db( pam_handle_t
*pamh
, int ctrl
, const char *user
, const char *pass_new
)
50 retval
= NT_STATUS_IS_OK(local_password_change( user
, LOCAL_SET_PASSWORD
, pass_new
,
51 err_str
, sizeof(err_str
),
52 msg_str
, sizeof(msg_str
) ));
56 err_str
[PSTRING_LEN
-1] = '\0';
57 make_remark( pamh
, ctrl
, PAM_ERROR_MSG
, err_str
);
60 /* FIXME: what value is appropriate here? */
61 retval
= PAM_AUTHTOK_ERR
;
64 msg_str
[PSTRING_LEN
-1] = '\0';
65 make_remark( pamh
, ctrl
, PAM_TEXT_INFO
, msg_str
);
76 #define _SMB_OLD_AUTHTOK "-SMB-OLD-PASS"
77 #define _SMB_NEW_AUTHTOK "-SMB-NEW-PASS"
80 * FUNCTION: pam_sm_chauthtok()
82 * This function is called twice by the PAM library, once with
83 * PAM_PRELIM_CHECK set, and then again with PAM_UPDATE_AUTHTOK set. With
84 * Linux-PAM, these two passes generally involve first checking the old
85 * token and then changing the token. This is what we do here.
87 * Having obtained a new password. The function updates the
88 * SMB_PASSWD_FILE file (normally, $(LIBDIR)/smbpasswd).
91 int pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
92 int argc
, const char **argv
)
97 extern BOOL in_client
;
99 struct samu
*sampass
= NULL
;
100 void (*oldsig_handler
)(int);
105 /* Samba initialization. */
107 setup_logging( "pam_smbpass", False
);
110 ctrl
= set_ctrl(flags
, argc
, argv
);
113 * First get the name of a user. No need to do anything if we can't
117 retval
= pam_get_user( pamh
, &user
, "Username: " );
118 if (retval
!= PAM_SUCCESS
) {
119 if (on( SMB_DEBUG
, ctrl
)) {
120 _log_err( LOG_DEBUG
, "password: could not identify user" );
124 if (on( SMB_DEBUG
, ctrl
)) {
125 _log_err( LOG_DEBUG
, "username [%s] obtained", user
);
128 if (geteuid() != 0) {
129 _log_err( LOG_DEBUG
, "Cannot access samba password database, not running as root.");
130 return PAM_AUTHINFO_UNAVAIL
;
133 /* Getting into places that might use LDAP -- protect the app
134 from a SIGPIPE it's not expecting */
135 oldsig_handler
= CatchSignal(SIGPIPE
, SIGNAL_CAST SIG_IGN
);
137 if (!initialize_password_db(False
)) {
138 _log_err( LOG_ALERT
, "Cannot access samba password database" );
139 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
140 return PAM_AUTHINFO_UNAVAIL
;
143 /* obtain user record */
144 if ( !(sampass
= samu_new( NULL
)) ) {
145 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
146 return nt_status_to_pam(NT_STATUS_NO_MEMORY
);
149 if (!pdb_getsampwnam(sampass
,user
)) {
150 _log_err( LOG_ALERT
, "Failed to find entry for user %s.", user
);
151 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
152 return PAM_USER_UNKNOWN
;
154 if (on( SMB_DEBUG
, ctrl
)) {
155 _log_err( LOG_DEBUG
, "Located account for %s", user
);
158 if (flags
& PAM_PRELIM_CHECK
) {
160 * obtain and verify the current password (OLDAUTHTOK) for
166 if (_smb_blankpasswd( ctrl
, sampass
)) {
168 TALLOC_FREE(sampass
);
169 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
173 /* Password change by root, or for an expired token, doesn't
174 require authentication. Is this a good choice? */
175 if (getuid() != 0 && !(flags
& PAM_CHANGE_EXPIRED_AUTHTOK
)) {
177 /* tell user what is happening */
178 #define greeting "Changing password for "
179 Announce
= SMB_MALLOC_ARRAY(char, sizeof(greeting
)+strlen(user
));
180 if (Announce
== NULL
) {
181 _log_err(LOG_CRIT
, "password: out of memory");
182 TALLOC_FREE(sampass
);
183 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
186 strncpy( Announce
, greeting
, sizeof(greeting
) );
187 strncpy( Announce
+sizeof(greeting
)-1, user
, strlen(user
)+1 );
190 set( SMB__OLD_PASSWD
, ctrl
);
191 retval
= _smb_read_password( pamh
, ctrl
, Announce
, "Current SMB password: ",
192 NULL
, _SMB_OLD_AUTHTOK
, &pass_old
);
193 SAFE_FREE( Announce
);
195 if (retval
!= PAM_SUCCESS
) {
197 , "password - (old) token not obtained" );
198 TALLOC_FREE(sampass
);
199 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
203 /* verify that this is the password for this user */
205 retval
= _smb_verify_password( pamh
, sampass
, pass_old
, ctrl
);
209 retval
= PAM_SUCCESS
; /* root doesn't have to */
213 TALLOC_FREE(sampass
);
214 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
217 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
220 * obtain the proposed password
224 * get the old token back. NULL was ok only if root [at this
225 * point we assume that this has already been enforced on a
226 * previous call to this function].
229 if (off( SMB_NOT_SET_PASS
, ctrl
)) {
230 retval
= pam_get_item( pamh
, PAM_OLDAUTHTOK
,
231 (const void **)&pass_old
);
233 retval
= pam_get_data( pamh
, _SMB_OLD_AUTHTOK
,
234 (const void **)&pass_old
);
235 if (retval
== PAM_NO_MODULE_DATA
) {
237 retval
= PAM_SUCCESS
;
241 if (retval
!= PAM_SUCCESS
) {
242 _log_err( LOG_NOTICE
, "password: user not authenticated" );
243 TALLOC_FREE(sampass
);
244 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
249 * use_authtok is to force the use of a previously entered
250 * password -- needed for pluggable password strength checking
251 * or other module stacking
254 if (on( SMB_USE_AUTHTOK
, ctrl
)) {
255 set( SMB_USE_FIRST_PASS
, ctrl
);
258 retval
= _smb_read_password( pamh
, ctrl
260 , "Enter new SMB password: "
261 , "Retype new SMB password: "
265 if (retval
!= PAM_SUCCESS
) {
266 if (on( SMB_DEBUG
, ctrl
)) {
268 , "password: new password not obtained" );
270 pass_old
= NULL
; /* tidy up */
271 TALLOC_FREE(sampass
);
272 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
277 * At this point we know who the user is and what they
278 * propose as their new password. Verify that the new
279 * password is acceptable.
282 if (pass_new
[0] == '\0') { /* "\0" password = NULL */
286 retval
= _pam_smb_approve_pass(pamh
, ctrl
, pass_old
, pass_new
);
288 if (retval
!= PAM_SUCCESS
) {
289 _log_err(LOG_NOTICE
, "new password not acceptable");
290 pass_new
= pass_old
= NULL
; /* tidy up */
291 TALLOC_FREE(sampass
);
292 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
297 * By reaching here we have approved the passwords and must now
298 * rebuild the smb password file.
301 /* update the password database */
303 retval
= smb_update_db(pamh
, ctrl
, user
, pass_new
);
304 if (retval
== PAM_SUCCESS
) {
307 /* password updated */
308 if (!sid_to_uid(pdb_get_user_sid(sampass
), &uid
)) {
309 _log_err( LOG_NOTICE
, "Unable to get uid for user %s",
310 pdb_get_username(sampass
));
311 _log_err( LOG_NOTICE
, "password for (%s) changed by (%s/%d)",
312 user
, uidtoname(getuid()), getuid());
314 _log_err( LOG_NOTICE
, "password for (%s/%d) changed by (%s/%d)",
315 user
, uid
, uidtoname(getuid()), getuid());
318 _log_err( LOG_ERR
, "password change failed for user %s", user
);
321 pass_old
= pass_new
= NULL
;
323 TALLOC_FREE(sampass
);
327 } else { /* something has broken with the library */
329 _log_err( LOG_ALERT
, "password received unknown request" );
335 TALLOC_FREE(sampass
);
339 TALLOC_FREE(sampass
);
340 CatchSignal(SIGPIPE
, SIGNAL_CAST oldsig_handler
);
344 /* static module data */
346 struct pam_module _pam_smbpass_passwd_modstruct
= {