sync'ing up for 3.0alpha20 release
[Samba.git] / source / pam_smbpass / pam_smb_passwd.c
blob91eae3c7a1960712a8810a5e9b5394a6de47af5b
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)
6 * any later version.
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
11 * more details.
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
21 #include "includes.h"
23 #ifndef LINUX
25 /* This is only used in the Sun implementation. */
26 #include <security/pam_appl.h>
28 #endif /* LINUX */
30 #include <security/pam_modules.h>
32 #include "general.h"
34 #include "support.h"
36 int smb_update_db( pam_handle_t *pamh, int ctrl, const char *user, const char *pass_new )
38 int retval;
39 pstring err_str;
40 pstring msg_str;
42 err_str[0] = '\0';
43 msg_str[0] = '\0';
45 retval = local_password_change( user, 0, pass_new, err_str, sizeof(err_str),
46 msg_str, sizeof(msg_str) );
48 if (!retval) {
49 if (*err_str) {
50 err_str[PSTRING_LEN-1] = '\0';
51 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
54 /* FIXME: what value is appropriate here? */
55 retval = PAM_AUTHTOK_ERR;
56 } else {
57 if (*msg_str) {
58 msg_str[PSTRING_LEN-1] = '\0';
59 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
61 retval = PAM_SUCCESS;
64 return retval;
69 /* data tokens */
71 #define _SMB_OLD_AUTHTOK "-SMB-OLD-PASS"
72 #define _SMB_NEW_AUTHTOK "-SMB-NEW-PASS"
75 * FUNCTION: pam_sm_chauthtok()
77 * This function is called twice by the PAM library, once with
78 * PAM_PRELIM_CHECK set, and then again with PAM_UPDATE_AUTHTOK set. With
79 * Linux-PAM, these two passes generally involve first checking the old
80 * token and then changing the token. This is what we do here.
82 * Having obtained a new password. The function updates the
83 * SMB_PASSWD_FILE file (normally, $(LIBDIR)/smbpasswd).
86 int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
87 int argc, const char **argv)
89 unsigned int ctrl;
90 int retval;
92 extern BOOL in_client;
94 SAM_ACCOUNT *sampass = NULL;
95 const char *user;
96 char *pass_old;
97 char *pass_new;
99 NTSTATUS nt_status;
101 /* Samba initialization. */
102 setup_logging( "pam_smbpass", False );
103 in_client = True;
105 ctrl = set_ctrl(flags, argc, argv);
108 * First get the name of a user. No need to do anything if we can't
109 * determine this.
112 retval = pam_get_user( pamh, &user, "Username: " );
113 if (retval != PAM_SUCCESS) {
114 if (on( SMB_DEBUG, ctrl )) {
115 _log_err( LOG_DEBUG, "password: could not identify user" );
117 return retval;
119 if (on( SMB_DEBUG, ctrl )) {
120 _log_err( LOG_DEBUG, "username [%s] obtained", user );
123 if (!initialize_password_db(True)) {
124 _log_err( LOG_ALERT, "Cannot access samba password database" );
125 return PAM_AUTHINFO_UNAVAIL;
128 /* obtain user record */
129 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
130 return nt_status_to_pam(nt_status);
133 if (!pdb_getsampwnam(sampass,user)) {
134 _log_err( LOG_ALERT, "Failed to find entry for user %s.", user );
135 return PAM_USER_UNKNOWN;
138 if (flags & PAM_PRELIM_CHECK) {
140 * obtain and verify the current password (OLDAUTHTOK) for
141 * the user.
144 char *Announce;
146 if (_smb_blankpasswd( ctrl, sampass )) {
148 pdb_free_sam(&sampass);
149 return PAM_SUCCESS;
152 /* Password change by root, or for an expired token, doesn't
153 require authentication. Is this a good choice? */
154 if (getuid() != 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
156 /* tell user what is happening */
157 #define greeting "Changing password for "
158 Announce = (char *) malloc(sizeof(greeting)+strlen(user));
159 if (Announce == NULL) {
160 _log_err(LOG_CRIT, "password: out of memory");
161 pdb_free_sam(&sampass);
162 return PAM_BUF_ERR;
164 strncpy( Announce, greeting, sizeof(greeting) );
165 strncpy( Announce+sizeof(greeting)-1, user, strlen(user)+1 );
166 #undef greeting
168 set( SMB__OLD_PASSWD, ctrl );
169 retval = _smb_read_password( pamh, ctrl, Announce, "Current SMB password: ",
170 NULL, _SMB_OLD_AUTHTOK, &pass_old );
171 SAFE_FREE( Announce );
173 if (retval != PAM_SUCCESS) {
174 _log_err( LOG_NOTICE
175 , "password - (old) token not obtained" );
176 pdb_free_sam(&sampass);
177 return retval;
180 /* verify that this is the password for this user */
182 retval = _smb_verify_password( pamh, sampass, pass_old, ctrl );
184 } else {
185 pass_old = NULL;
186 retval = PAM_SUCCESS; /* root doesn't have to */
189 pass_old = NULL;
190 pdb_free_sam(&sampass);
191 return retval;
193 } else if (flags & PAM_UPDATE_AUTHTOK) {
195 #if 0
196 /* We used to return when this flag was set, but that breaks
197 password synchronization when /other/ tokens are expired. For
198 now, we change the password whenever we're asked. SRL */
199 if (flags & PAM_CHANGE_EXPIRED_AUTHTOK) {
200 pdb_free_sam(&sampass);
201 return PAM_SUCCESS;
203 #endif
205 * obtain the proposed password
209 * get the old token back. NULL was ok only if root [at this
210 * point we assume that this has already been enforced on a
211 * previous call to this function].
214 if (off( SMB_NOT_SET_PASS, ctrl )) {
215 retval = pam_get_item( pamh, PAM_OLDAUTHTOK,
216 (const void **)&pass_old );
217 } else {
218 retval = pam_get_data( pamh, _SMB_OLD_AUTHTOK,
219 (const void **)&pass_old );
220 if (retval == PAM_NO_MODULE_DATA) {
221 pass_old = NULL;
222 retval = PAM_SUCCESS;
226 if (retval != PAM_SUCCESS) {
227 _log_err( LOG_NOTICE, "password: user not authenticated" );
228 pdb_free_sam(&sampass);
229 return retval;
233 * use_authtok is to force the use of a previously entered
234 * password -- needed for pluggable password strength checking
235 * or other module stacking
238 if (on( SMB_USE_AUTHTOK, ctrl )) {
239 set( SMB_USE_FIRST_PASS, ctrl );
242 retval = _smb_read_password( pamh, ctrl
243 , NULL
244 , "Enter new SMB password: "
245 , "Retype new SMB password: "
246 , _SMB_NEW_AUTHTOK
247 , &pass_new );
249 if (retval != PAM_SUCCESS) {
250 if (on( SMB_DEBUG, ctrl )) {
251 _log_err( LOG_ALERT
252 , "password: new password not obtained" );
254 pass_old = NULL; /* tidy up */
255 pdb_free_sam(&sampass);
256 return retval;
260 * At this point we know who the user is and what they
261 * propose as their new password. Verify that the new
262 * password is acceptable.
265 if (pass_new[0] == '\0') { /* "\0" password = NULL */
266 pass_new = NULL;
269 retval = _pam_smb_approve_pass(pamh, ctrl, pass_old, pass_new);
271 if (retval != PAM_SUCCESS) {
272 _log_err(LOG_NOTICE, "new password not acceptable");
273 pass_new = pass_old = NULL; /* tidy up */
274 pdb_free_sam(&sampass);
275 return retval;
279 * By reaching here we have approved the passwords and must now
280 * rebuild the smb password file.
283 /* update the password database */
285 retval = smb_update_db(pamh, ctrl, user, pass_new);
286 if (retval == PAM_SUCCESS) {
287 /* password updated */
288 _log_err( LOG_NOTICE, "password for (%s/%d) changed by (%s/%d)"
289 , user, pdb_get_uid(sampass), uidtoname( getuid() )
290 , getuid() );
291 } else {
292 _log_err( LOG_ERR, "password change failed for user %s"
293 , user );
296 pass_old = pass_new = NULL;
297 if (sampass) {
298 pdb_free_sam(&sampass);
299 sampass = NULL;
302 } else { /* something has broken with the library */
304 _log_err( LOG_ALERT, "password received unknown request" );
305 retval = PAM_ABORT;
309 if (sampass) {
310 pdb_free_sam(&sampass);
311 sampass = NULL;
314 pdb_free_sam(&sampass);
315 return retval;
318 /* static module data */
319 #ifdef PAM_STATIC
320 struct pam_module _pam_smbpass_passwd_modstruct = {
321 "pam_smbpass",
322 NULL,
323 NULL,
324 NULL,
325 NULL,
326 NULL,
327 pam_sm_chauthtok
329 #endif