source: trunk/server/source3/pam_smbpass/pam_smb_passwd.c@ 845

Last change on this file since 845 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 10.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Use PAM to update user passwords in the local SAM
4 Copyright (C) Steve Langasek 1998-2003
5
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.
10
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.
15
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/>.
18
19*/
20
21/* indicate the following groups are defined */
22#define PAM_SM_PASSWORD
23
24#include "includes.h"
25
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). */
29
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
37
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
43
44#include "general.h"
45
46#include "support.h"
47
48static int smb_update_db( pam_handle_t *pamh, int ctrl, const char *user, const char *pass_new )
49{
50 int retval;
51 char *err_str = NULL;
52 char *msg_str = NULL;
53
54 retval = NT_STATUS_IS_OK(local_password_change(user, LOCAL_SET_PASSWORD, pass_new,
55 &err_str,
56 &msg_str));
57
58 if (!retval) {
59 if (err_str) {
60 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
61 }
62
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 );
68 }
69 retval = PAM_SUCCESS;
70 }
71
72 SAFE_FREE(err_str);
73 SAFE_FREE(msg_str);
74 return retval;
75}
76
77
78/* data tokens */
79
80#define _SMB_OLD_AUTHTOK "-SMB-OLD-PASS"
81#define _SMB_NEW_AUTHTOK "-SMB-NEW-PASS"
82
83/*
84 * FUNCTION: pam_sm_chauthtok()
85 *
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.
90 *
91 * Having obtained a new password. The function updates the
92 * SMB_PASSWD_FILE file (normally, $(LIBDIR)/smbpasswd).
93 */
94
95int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
96 int argc, const char **argv)
97{
98 unsigned int ctrl;
99 int retval;
100
101 struct samu *sampass = NULL;
102 void (*oldsig_handler)(int);
103 const char *user;
104 char *pass_old;
105 char *pass_new;
106
107 /* Samba initialization. */
108 load_case_tables_library();
109 lp_set_in_client(True);
110
111 ctrl = set_ctrl(pamh, flags, argc, argv);
112
113 /*
114 * First get the name of a user. No need to do anything if we can't
115 * determine this.
116 */
117
118 retval = pam_get_user( pamh, &user, "Username: " );
119 if (retval != PAM_SUCCESS) {
120 if (on( SMB_DEBUG, ctrl )) {
121 _log_err(pamh, LOG_DEBUG, "password: could not identify user");
122 }
123 return retval;
124 }
125 if (on( SMB_DEBUG, ctrl )) {
126 _log_err(pamh, LOG_DEBUG, "username [%s] obtained", user);
127 }
128
129 if (geteuid() != 0) {
130 _log_err(pamh, LOG_DEBUG, "Cannot access samba password database, not running as root.");
131 return PAM_AUTHINFO_UNAVAIL;
132 }
133
134 /* Getting into places that might use LDAP -- protect the app
135 from a SIGPIPE it's not expecting */
136 oldsig_handler = CatchSignal(SIGPIPE, SIG_IGN);
137
138 if (!initialize_password_db(False, NULL)) {
139 _log_err(pamh, LOG_ALERT, "Cannot access samba password database" );
140 CatchSignal(SIGPIPE, oldsig_handler);
141 return PAM_AUTHINFO_UNAVAIL;
142 }
143
144 /* obtain user record */
145 if ( !(sampass = samu_new( NULL )) ) {
146 CatchSignal(SIGPIPE, oldsig_handler);
147 return nt_status_to_pam(NT_STATUS_NO_MEMORY);
148 }
149
150 if (!pdb_getsampwnam(sampass,user)) {
151 _log_err(pamh, LOG_ALERT, "Failed to find entry for user %s.", user);
152 CatchSignal(SIGPIPE, oldsig_handler);
153 return PAM_USER_UNKNOWN;
154 }
155 if (on( SMB_DEBUG, ctrl )) {
156 _log_err(pamh, LOG_DEBUG, "Located account for %s", user);
157 }
158
159 if (flags & PAM_PRELIM_CHECK) {
160 /*
161 * obtain and verify the current password (OLDAUTHTOK) for
162 * the user.
163 */
164
165 char *Announce;
166
167 if (_smb_blankpasswd( ctrl, sampass )) {
168
169 TALLOC_FREE(sampass);
170 CatchSignal(SIGPIPE, oldsig_handler);
171 return PAM_SUCCESS;
172 }
173
174 /* Password change by root, or for an expired token, doesn't
175 require authentication. Is this a good choice? */
176 if (getuid() != 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
177
178 /* tell user what is happening */
179 if (asprintf(&Announce, "Changing password for %s", user) == -1) {
180 _log_err(pamh, LOG_CRIT, "password: out of memory");
181 TALLOC_FREE(sampass);
182 CatchSignal(SIGPIPE, oldsig_handler);
183 return PAM_BUF_ERR;
184 }
185
186 set( SMB__OLD_PASSWD, ctrl );
187 retval = _smb_read_password( pamh, ctrl, Announce, "Current SMB password: ",
188 NULL, _SMB_OLD_AUTHTOK, &pass_old );
189 SAFE_FREE( Announce );
190
191 if (retval != PAM_SUCCESS) {
192 _log_err(pamh, LOG_NOTICE,
193 "password - (old) token not obtained");
194 TALLOC_FREE(sampass);
195 CatchSignal(SIGPIPE, oldsig_handler);
196 return retval;
197 }
198
199 /* verify that this is the password for this user */
200
201 retval = _smb_verify_password( pamh, sampass, pass_old, ctrl );
202
203 } else {
204 pass_old = NULL;
205 retval = PAM_SUCCESS; /* root doesn't have to */
206 }
207
208 pass_old = NULL;
209 TALLOC_FREE(sampass);
210 CatchSignal(SIGPIPE, oldsig_handler);
211 return retval;
212
213 } else if (flags & PAM_UPDATE_AUTHTOK) {
214
215 /*
216 * obtain the proposed password
217 */
218
219 /*
220 * get the old token back. NULL was ok only if root [at this
221 * point we assume that this has already been enforced on a
222 * previous call to this function].
223 */
224
225 if (off( SMB_NOT_SET_PASS, ctrl )) {
226 retval = _pam_get_item( pamh, PAM_OLDAUTHTOK,
227 &pass_old );
228 } else {
229 retval = _pam_get_data( pamh, _SMB_OLD_AUTHTOK,
230 &pass_old );
231 if (retval == PAM_NO_MODULE_DATA) {
232 pass_old = NULL;
233 retval = PAM_SUCCESS;
234 }
235 }
236
237 if (retval != PAM_SUCCESS) {
238 _log_err(pamh, LOG_NOTICE, "password: user not authenticated");
239 TALLOC_FREE(sampass);
240 CatchSignal(SIGPIPE, oldsig_handler);
241 return retval;
242 }
243
244 /*
245 * use_authtok is to force the use of a previously entered
246 * password -- needed for pluggable password strength checking
247 * or other module stacking
248 */
249
250 if (on( SMB_USE_AUTHTOK, ctrl )) {
251 set( SMB_USE_FIRST_PASS, ctrl );
252 }
253
254 retval = _smb_read_password( pamh, ctrl
255 , NULL
256 , "Enter new SMB password: "
257 , "Retype new SMB password: "
258 , _SMB_NEW_AUTHTOK
259 , &pass_new );
260
261 if (retval != PAM_SUCCESS) {
262 if (on( SMB_DEBUG, ctrl )) {
263 _log_err(pamh, LOG_ALERT,
264 "password: new password not obtained");
265 }
266 pass_old = NULL; /* tidy up */
267 TALLOC_FREE(sampass);
268 CatchSignal(SIGPIPE, oldsig_handler);
269 return retval;
270 }
271
272 /*
273 * At this point we know who the user is and what they
274 * propose as their new password. Verify that the new
275 * password is acceptable.
276 */
277
278 if (pass_new[0] == '\0') { /* "\0" password = NULL */
279 pass_new = NULL;
280 }
281
282 retval = _pam_smb_approve_pass(pamh, ctrl, pass_old, pass_new);
283
284 if (retval != PAM_SUCCESS) {
285 _log_err(pamh, LOG_NOTICE, "new password not acceptable");
286 pass_new = pass_old = NULL; /* tidy up */
287 TALLOC_FREE(sampass);
288 CatchSignal(SIGPIPE, oldsig_handler);
289 return retval;
290 }
291
292 /*
293 * By reaching here we have approved the passwords and must now
294 * rebuild the smb password file.
295 */
296
297 /* update the password database */
298
299 retval = smb_update_db(pamh, ctrl, user, pass_new);
300 if (retval == PAM_SUCCESS) {
301 uid_t uid;
302
303 /* password updated */
304 if (!sid_to_uid(pdb_get_user_sid(sampass), &uid)) {
305 _log_err(pamh, LOG_NOTICE,
306 "Unable to get uid for user %s",
307 pdb_get_username(sampass));
308 _log_err(pamh, LOG_NOTICE, "password for (%s) changed by (%s/%d)",
309 user, uidtoname(getuid()), getuid());
310 } else {
311 _log_err(pamh, LOG_NOTICE, "password for (%s/%d) changed by (%s/%d)",
312 user, uid, uidtoname(getuid()), getuid());
313 }
314 } else {
315 _log_err(pamh, LOG_ERR, "password change failed for user %s", user);
316 }
317
318 pass_old = pass_new = NULL;
319 if (sampass) {
320 TALLOC_FREE(sampass);
321 sampass = NULL;
322 }
323
324 } else { /* something has broken with the library */
325
326 _log_err(pamh, LOG_ALERT, "password received unknown request");
327 retval = PAM_ABORT;
328
329 }
330
331 if (sampass) {
332 TALLOC_FREE(sampass);
333 sampass = NULL;
334 }
335
336 TALLOC_FREE(sampass);
337 CatchSignal(SIGPIPE, oldsig_handler);
338 return retval;
339}
340
341/* static module data */
342#ifdef PAM_STATIC
343struct pam_module _pam_smbpass_passwd_modstruct = {
344 "pam_smbpass",
345 NULL,
346 NULL,
347 NULL,
348 NULL,
349 NULL,
350 pam_sm_chauthtok
351};
352#endif
353
Note: See TracBrowser for help on using the repository browser.