Remove pstrings from pam_smbpass - make local_password_change
[jra/samba/.git] / source3 / pam_smbpass / pam_smb_passwd.c
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
48 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
95 int 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     extern bool in_client;
102
103     struct samu *sampass = NULL;
104     void (*oldsig_handler)(int);
105     const char *user;
106     char *pass_old;
107     char *pass_new;
108
109     /* Samba initialization. */
110     load_case_tables();
111     setup_logging( "pam_smbpass", False );
112     in_client = True;
113
114     ctrl = set_ctrl(flags, argc, argv);
115
116     /*
117      * First get the name of a user.  No need to do anything if we can't
118      * determine this.
119      */
120
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" );
125         }
126         return retval;
127     }
128     if (on( SMB_DEBUG, ctrl )) {
129         _log_err( LOG_DEBUG, "username [%s] obtained", user );
130     }
131
132     /* Getting into places that might use LDAP -- protect the app
133        from a SIGPIPE it's not expecting */
134     oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
135
136     if (!initialize_password_db(False, NULL)) {
137         _log_err( LOG_ALERT, "Cannot access samba password database" );
138         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
139         return PAM_AUTHINFO_UNAVAIL;
140     }
141
142     /* obtain user record */
143     if ( !(sampass = samu_new( NULL )) ) {
144         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
145         return nt_status_to_pam(NT_STATUS_NO_MEMORY);
146     }
147
148     if (!pdb_getsampwnam(sampass,user)) {
149         _log_err( LOG_ALERT, "Failed to find entry for user %s.", user );
150         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
151         return PAM_USER_UNKNOWN;
152     }
153     if (on( SMB_DEBUG, ctrl )) {
154         _log_err( LOG_DEBUG, "Located account for %s", user );
155     }
156
157     if (flags & PAM_PRELIM_CHECK) {
158         /*
159          * obtain and verify the current password (OLDAUTHTOK) for
160          * the user.
161          */
162
163         char *Announce;
164
165         if (_smb_blankpasswd( ctrl, sampass )) {
166
167             TALLOC_FREE(sampass);
168             CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
169             return PAM_SUCCESS;
170         }
171
172         /* Password change by root, or for an expired token, doesn't
173            require authentication.  Is this a good choice? */
174         if (getuid() != 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
175
176             /* tell user what is happening */
177 #define greeting "Changing password for "
178             Announce = SMB_MALLOC_ARRAY(char, sizeof(greeting)+strlen(user));
179             if (Announce == NULL) {
180                 _log_err(LOG_CRIT, "password: out of memory");
181                 TALLOC_FREE(sampass);
182                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
183                 return PAM_BUF_ERR;
184             }
185             strncpy( Announce, greeting, sizeof(greeting) );
186             strncpy( Announce+sizeof(greeting)-1, user, strlen(user)+1 );
187 #undef greeting
188
189             set( SMB__OLD_PASSWD, ctrl );
190             retval = _smb_read_password( pamh, ctrl, Announce, "Current SMB password: ",
191                                          NULL, _SMB_OLD_AUTHTOK, &pass_old );
192             SAFE_FREE( Announce );
193
194             if (retval != PAM_SUCCESS) {
195                 _log_err( LOG_NOTICE
196                           , "password - (old) token not obtained" );
197                 TALLOC_FREE(sampass);
198                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
199                 return retval;
200             }
201
202             /* verify that this is the password for this user */
203
204             retval = _smb_verify_password( pamh, sampass, pass_old, ctrl );
205
206         } else {
207             pass_old = NULL;
208             retval = PAM_SUCCESS;           /* root doesn't have to */
209         }
210
211         pass_old = NULL;
212         TALLOC_FREE(sampass);
213         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
214         return retval;
215
216     } else if (flags & PAM_UPDATE_AUTHTOK) {
217
218         /*
219          * obtain the proposed password
220          */
221
222         /*
223          * get the old token back. NULL was ok only if root [at this
224          * point we assume that this has already been enforced on a
225          * previous call to this function].
226          */
227
228         if (off( SMB_NOT_SET_PASS, ctrl )) {
229             retval = pam_get_item( pamh, PAM_OLDAUTHTOK,
230                                    (const void **)&pass_old );
231         } else {
232             retval = pam_get_data( pamh, _SMB_OLD_AUTHTOK,
233                                    (const void **)&pass_old );
234             if (retval == PAM_NO_MODULE_DATA) {
235                 pass_old = NULL;
236                 retval = PAM_SUCCESS;
237             }
238         }
239
240         if (retval != PAM_SUCCESS) {
241             _log_err( LOG_NOTICE, "password: user not authenticated" );
242             TALLOC_FREE(sampass);
243             CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
244             return retval;
245         }
246
247         /*
248          * use_authtok is to force the use of a previously entered
249          * password -- needed for pluggable password strength checking
250          * or other module stacking
251          */
252
253         if (on( SMB_USE_AUTHTOK, ctrl )) {
254             set( SMB_USE_FIRST_PASS, ctrl );
255         }
256
257         retval = _smb_read_password( pamh, ctrl
258                                       , NULL
259                                       , "Enter new SMB password: "
260                                       , "Retype new SMB password: "
261                                       , _SMB_NEW_AUTHTOK
262                                       , &pass_new );
263
264         if (retval != PAM_SUCCESS) {
265             if (on( SMB_DEBUG, ctrl )) {
266                 _log_err( LOG_ALERT
267                           , "password: new password not obtained" );
268             }
269             pass_old = NULL;                               /* tidy up */
270             TALLOC_FREE(sampass);
271             CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
272             return retval;
273         }
274
275         /*
276          * At this point we know who the user is and what they
277          * propose as their new password. Verify that the new
278          * password is acceptable.
279          */ 
280
281         if (pass_new[0] == '\0') {     /* "\0" password = NULL */
282             pass_new = NULL;
283         }
284
285         retval = _pam_smb_approve_pass(pamh, ctrl, pass_old, pass_new);
286
287         if (retval != PAM_SUCCESS) {
288             _log_err(LOG_NOTICE, "new password not acceptable");
289             pass_new = pass_old = NULL;               /* tidy up */
290             TALLOC_FREE(sampass);
291             CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
292             return retval;
293         }
294
295         /*
296          * By reaching here we have approved the passwords and must now
297          * rebuild the smb password file.
298          */
299
300         /* update the password database */
301
302         retval = smb_update_db(pamh, ctrl, user, pass_new);
303         if (retval == PAM_SUCCESS) {
304             uid_t uid;
305             
306             /* password updated */
307                 if (!sid_to_uid(pdb_get_user_sid(sampass), &uid)) {
308                         _log_err( LOG_NOTICE, "Unable to get uid for user %s",
309                                 pdb_get_username(sampass));
310                         _log_err( LOG_NOTICE, "password for (%s) changed by (%s/%d)",
311                                 user, uidtoname(getuid()), getuid());
312                 } else {
313                         _log_err( LOG_NOTICE, "password for (%s/%d) changed by (%s/%d)",
314                                 user, uid, uidtoname(getuid()), getuid());
315                 }
316         } else {
317                 _log_err( LOG_ERR, "password change failed for user %s", user);
318         }
319
320         pass_old = pass_new = NULL;
321         if (sampass) {
322                 TALLOC_FREE(sampass);
323                 sampass = NULL;
324         }
325
326     } else {            /* something has broken with the library */
327
328         _log_err( LOG_ALERT, "password received unknown request" );
329         retval = PAM_ABORT;
330
331     }
332     
333     if (sampass) {
334         TALLOC_FREE(sampass);
335         sampass = NULL;
336     }
337
338     TALLOC_FREE(sampass);
339     CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
340     return retval;
341 }
342
343 /* static module data */
344 #ifdef PAM_STATIC
345 struct pam_module _pam_smbpass_passwd_modstruct = {
346      "pam_smbpass",
347      NULL,
348      NULL,
349      NULL,
350      NULL,
351      NULL,
352      pam_sm_chauthtok
353 };
354 #endif
355