Fix build for pam_smbpass
[Samba.git] / source3 / pam_smbpass / pam_smb_passwd.c
blob326a0b59e7c1c87648a7d1aff252ec996b72e8a8
1 /*
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
24 #include "includes.h"
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). */
30 #ifndef LINUX
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>
35 #endif
36 #endif
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>
42 #endif
44 #include "general.h"
46 #include "support.h"
48 int smb_update_db( pam_handle_t *pamh, int ctrl, const char *user, const char *pass_new )
50 int retval;
51 char *err_str = NULL;
52 char *msg_str = NULL;
54 retval = NT_STATUS_IS_OK(local_password_change(user, LOCAL_SET_PASSWORD, pass_new,
55 &err_str,
56 &msg_str));
58 if (!retval) {
59 if (err_str) {
60 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
63 /* FIXME: what value is appropriate here? */
64 retval = PAM_AUTHTOK_ERR;
65 } else {
66 if (msg_str) {
67 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
69 retval = PAM_SUCCESS;
72 SAFE_FREE(err_str);
73 SAFE_FREE(msg_str);
74 return retval;
78 /* data tokens */
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)
98 unsigned int ctrl;
99 int retval;
101 extern bool in_client;
103 struct samu *sampass = NULL;
104 void (*oldsig_handler)(int);
105 const char *user;
106 char *pass_old;
107 char *pass_new;
109 /* Samba initialization. */
110 load_case_tables();
111 setup_logging( "pam_smbpass", False );
112 in_client = True;
114 ctrl = set_ctrl(flags, argc, argv);
117 * First get the name of a user. No need to do anything if we can't
118 * determine this.
121 retval = pam_get_user( pamh, &user, "Username: " );
122 if (retval != PAM_SUCCESS) {
123 if (on( SMB_DEBUG, ctrl )) {
124 _log_err( LOG_DEBUG, "password: could not identify user" );
126 return retval;
128 if (on( SMB_DEBUG, ctrl )) {
129 _log_err( LOG_DEBUG, "username [%s] obtained", user );
132 if (geteuid() != 0) {
133 _log_err( LOG_DEBUG, "Cannot access samba password database, not running as root.");
134 return PAM_AUTHINFO_UNAVAIL;
137 /* Getting into places that might use LDAP -- protect the app
138 from a SIGPIPE it's not expecting */
139 oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
141 if (!initialize_password_db(False, NULL)) {
142 _log_err( LOG_ALERT, "Cannot access samba password database" );
143 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
144 return PAM_AUTHINFO_UNAVAIL;
147 /* obtain user record */
148 if ( !(sampass = samu_new( NULL )) ) {
149 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
150 return nt_status_to_pam(NT_STATUS_NO_MEMORY);
153 if (!pdb_getsampwnam(sampass,user)) {
154 _log_err( LOG_ALERT, "Failed to find entry for user %s.", user );
155 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
156 return PAM_USER_UNKNOWN;
158 if (on( SMB_DEBUG, ctrl )) {
159 _log_err( LOG_DEBUG, "Located account for %s", user );
162 if (flags & PAM_PRELIM_CHECK) {
164 * obtain and verify the current password (OLDAUTHTOK) for
165 * the user.
168 char *Announce;
170 if (_smb_blankpasswd( ctrl, sampass )) {
172 TALLOC_FREE(sampass);
173 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
174 return PAM_SUCCESS;
177 /* Password change by root, or for an expired token, doesn't
178 require authentication. Is this a good choice? */
179 if (getuid() != 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
181 /* tell user what is happening */
182 #define greeting "Changing password for "
183 Announce = SMB_MALLOC_ARRAY(char, sizeof(greeting)+strlen(user));
184 if (Announce == NULL) {
185 _log_err(LOG_CRIT, "password: out of memory");
186 TALLOC_FREE(sampass);
187 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
188 return PAM_BUF_ERR;
190 strncpy( Announce, greeting, sizeof(greeting) );
191 strncpy( Announce+sizeof(greeting)-1, user, strlen(user)+1 );
192 #undef greeting
194 set( SMB__OLD_PASSWD, ctrl );
195 retval = _smb_read_password( pamh, ctrl, Announce, "Current SMB password: ",
196 NULL, _SMB_OLD_AUTHTOK, &pass_old );
197 SAFE_FREE( Announce );
199 if (retval != PAM_SUCCESS) {
200 _log_err( LOG_NOTICE
201 , "password - (old) token not obtained" );
202 TALLOC_FREE(sampass);
203 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
204 return retval;
207 /* verify that this is the password for this user */
209 retval = _smb_verify_password( pamh, sampass, pass_old, ctrl );
211 } else {
212 pass_old = NULL;
213 retval = PAM_SUCCESS; /* root doesn't have to */
216 pass_old = NULL;
217 TALLOC_FREE(sampass);
218 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
219 return retval;
221 } else if (flags & PAM_UPDATE_AUTHTOK) {
224 * obtain the proposed password
228 * get the old token back. NULL was ok only if root [at this
229 * point we assume that this has already been enforced on a
230 * previous call to this function].
233 if (off( SMB_NOT_SET_PASS, ctrl )) {
234 retval = pam_get_item( pamh, PAM_OLDAUTHTOK,
235 (const void **)&pass_old );
236 } else {
237 retval = pam_get_data( pamh, _SMB_OLD_AUTHTOK,
238 (const void **)&pass_old );
239 if (retval == PAM_NO_MODULE_DATA) {
240 pass_old = NULL;
241 retval = PAM_SUCCESS;
245 if (retval != PAM_SUCCESS) {
246 _log_err( LOG_NOTICE, "password: user not authenticated" );
247 TALLOC_FREE(sampass);
248 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
249 return retval;
253 * use_authtok is to force the use of a previously entered
254 * password -- needed for pluggable password strength checking
255 * or other module stacking
258 if (on( SMB_USE_AUTHTOK, ctrl )) {
259 set( SMB_USE_FIRST_PASS, ctrl );
262 retval = _smb_read_password( pamh, ctrl
263 , NULL
264 , "Enter new SMB password: "
265 , "Retype new SMB password: "
266 , _SMB_NEW_AUTHTOK
267 , &pass_new );
269 if (retval != PAM_SUCCESS) {
270 if (on( SMB_DEBUG, ctrl )) {
271 _log_err( LOG_ALERT
272 , "password: new password not obtained" );
274 pass_old = NULL; /* tidy up */
275 TALLOC_FREE(sampass);
276 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
277 return retval;
281 * At this point we know who the user is and what they
282 * propose as their new password. Verify that the new
283 * password is acceptable.
286 if (pass_new[0] == '\0') { /* "\0" password = NULL */
287 pass_new = NULL;
290 retval = _pam_smb_approve_pass(pamh, ctrl, pass_old, pass_new);
292 if (retval != PAM_SUCCESS) {
293 _log_err(LOG_NOTICE, "new password not acceptable");
294 pass_new = pass_old = NULL; /* tidy up */
295 TALLOC_FREE(sampass);
296 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
297 return retval;
301 * By reaching here we have approved the passwords and must now
302 * rebuild the smb password file.
305 /* update the password database */
307 retval = smb_update_db(pamh, ctrl, user, pass_new);
308 if (retval == PAM_SUCCESS) {
309 uid_t uid;
311 /* password updated */
312 if (!sid_to_uid(pdb_get_user_sid(sampass), &uid)) {
313 _log_err( LOG_NOTICE, "Unable to get uid for user %s",
314 pdb_get_username(sampass));
315 _log_err( LOG_NOTICE, "password for (%s) changed by (%s/%d)",
316 user, uidtoname(getuid()), getuid());
317 } else {
318 _log_err( LOG_NOTICE, "password for (%s/%d) changed by (%s/%d)",
319 user, uid, uidtoname(getuid()), getuid());
321 } else {
322 _log_err( LOG_ERR, "password change failed for user %s", user);
325 pass_old = pass_new = NULL;
326 if (sampass) {
327 TALLOC_FREE(sampass);
328 sampass = NULL;
331 } else { /* something has broken with the library */
333 _log_err( LOG_ALERT, "password received unknown request" );
334 retval = PAM_ABORT;
338 if (sampass) {
339 TALLOC_FREE(sampass);
340 sampass = NULL;
343 TALLOC_FREE(sampass);
344 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
345 return retval;
348 /* static module data */
349 #ifdef PAM_STATIC
350 struct pam_module _pam_smbpass_passwd_modstruct = {
351 "pam_smbpass",
352 NULL,
353 NULL,
354 NULL,
355 NULL,
356 NULL,
357 pam_sm_chauthtok
359 #endif