]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_passwd.c
Relevant BUGIDs:
[linux-pam] / modules / pam_unix / pam_unix_passwd.c
1 /*
2  * Main coding by Elliot Lee <sopwith@redhat.com>, Red Hat Software.
3  * Copyright (C) 1996.
4  * Copyright (c) Jan Rêkorajski, 1999.
5  * Copyright (c) Red Hat, Inc., 2007, 2008.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, and the entire permission notice in its entirety,
12  *    including the disclaimer of warranties.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * ALTERNATIVELY, this product may be distributed under the terms of
21  * the GNU Public License, in which case the provisions of the GPL are
22  * required INSTEAD OF the above restrictions.  (This clause is
23  * necessary due to a potential bad interaction between the GPL and
24  * the restrictions contained in a BSD-style copyright.)
25  *
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36  * OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include "config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include <malloc.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <sys/types.h>
49 #include <pwd.h>
50 #include <syslog.h>
51 #include <shadow.h>
52 #include <time.h>               /* for time() */
53 #include <fcntl.h>
54 #include <ctype.h>
55 #include <sys/time.h>
56 #include <sys/stat.h>
57 #include <rpc/rpc.h>
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
60
61 #include <signal.h>
62 #include <errno.h>
63 #include <sys/wait.h>
64 #ifdef WITH_SELINUX
65 static int selinux_enabled=-1;
66 #include <selinux/selinux.h>
67 #define SELINUX_ENABLED (selinux_enabled!=-1 ? selinux_enabled : (selinux_enabled=is_selinux_enabled()>0))
68 #endif
69
70 #include <security/_pam_macros.h>
71
72 /* indicate the following groups are defined */
73
74 #define PAM_SM_PASSWORD
75
76 #include <security/pam_modules.h>
77 #include <security/pam_ext.h>
78 #include <security/pam_modutil.h>
79
80 #include "yppasswd.h"
81 #include "md5.h"
82 #include "support.h"
83 #include "passverify.h"
84 #include "bigcrypt.h"
85
86 #if !((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
87 extern int getrpcport(const char *host, unsigned long prognum,
88                       unsigned long versnum, unsigned int proto);
89 #endif                          /* GNU libc 2.1 */
90
91 /*
92    How it works:
93    Gets in username (has to be done) from the calling program
94    Does authentication of user (only if we are not running as root)
95    Gets new password/checks for sanity
96    Sets it.
97  */
98
99 /* data tokens */
100
101 #define _UNIX_OLD_AUTHTOK       "-UN*X-OLD-PASS"
102 #define _UNIX_NEW_AUTHTOK       "-UN*X-NEW-PASS"
103
104 #define MAX_PASSWD_TRIES        3
105
106 static char *getNISserver(pam_handle_t *pamh)
107 {
108         char *master;
109         char *domainname;
110         int port, err;
111
112         if ((err = yp_get_default_domain(&domainname)) != 0) {
113                 pam_syslog(pamh, LOG_WARNING, "can't get local yp domain: %s",
114                          yperr_string(err));
115                 return NULL;
116         }
117         if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) {
118                 pam_syslog(pamh, LOG_WARNING, "can't find the master ypserver: %s",
119                          yperr_string(err));
120                 return NULL;
121         }
122         port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
123         if (port == 0) {
124                 pam_syslog(pamh, LOG_WARNING,
125                          "yppasswdd not running on NIS master host");
126                 return NULL;
127         }
128         if (port >= IPPORT_RESERVED) {
129                 pam_syslog(pamh, LOG_WARNING,
130                          "yppasswd daemon running on illegal port");
131                 return NULL;
132         }
133         return master;
134 }
135
136 #ifdef WITH_SELINUX
137
138 static int _unix_run_update_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user,
139     const char *fromwhat, const char *towhat, int remember)
140 {
141     int retval, child, fds[2];
142     struct sigaction newsa, oldsa;
143
144     D(("called."));
145     /* create a pipe for the password */
146     if (pipe(fds) != 0) {
147         D(("could not make pipe"));
148         return PAM_AUTH_ERR;
149     }
150
151     if (off(UNIX_NOREAP, ctrl)) {
152         /*
153          * This code arranges that the demise of the child does not cause
154          * the application to receive a signal it is not expecting - which
155          * may kill the application or worse.
156          *
157          * The "noreap" module argument is provided so that the admin can
158          * override this behavior.
159          */
160         memset(&newsa, '\0', sizeof(newsa));
161         newsa.sa_handler = SIG_DFL;
162         sigaction(SIGCHLD, &newsa, &oldsa);
163     }
164
165     /* fork */
166     child = fork();
167     if (child == 0) {
168         int i=0;
169         struct rlimit rlim;
170         static char *envp[] = { NULL };
171         char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
172         char buffer[16];
173
174         /* XXX - should really tidy up PAM here too */
175
176         /* reopen stdin as pipe */
177         dup2(fds[0], STDIN_FILENO);
178
179         if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
180           if (rlim.rlim_max >= MAX_FD_NO)
181             rlim.rlim_max = MAX_FD_NO;
182           for (i=0; i < (int)rlim.rlim_max; i++) {
183             if (i != STDIN_FILENO)
184                    close(i);
185           }
186         }
187
188         /* exec binary helper */
189         args[0] = x_strdup(UPDATE_HELPER);
190         args[1] = x_strdup(user);
191         args[2] = x_strdup("update");
192         if (on(UNIX_SHADOW, ctrl))
193                 args[3] = x_strdup("1");
194         else
195                 args[3] = x_strdup("0");
196
197         snprintf(buffer, sizeof(buffer), "%d", remember);
198         args[4] = x_strdup(buffer);
199         
200         execve(UPDATE_HELPER, args, envp);
201
202         /* should not get here: exit with error */
203         D(("helper binary is not available"));
204         _exit(PAM_AUTHINFO_UNAVAIL);
205     } else if (child > 0) {
206         /* wait for child */
207         /* if the stored password is NULL */
208         int rc=0;
209         if (fromwhat)
210           pam_modutil_write(fds[1], fromwhat, strlen(fromwhat)+1);
211         else
212           pam_modutil_write(fds[1], "", 1);
213         if (towhat) {
214           pam_modutil_write(fds[1], towhat, strlen(towhat)+1);
215         }
216         else
217           pam_modutil_write(fds[1], "", 1);
218
219         close(fds[0]);       /* close here to avoid possible SIGPIPE above */
220         close(fds[1]);
221         rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
222         if (rc<0) {
223           pam_syslog(pamh, LOG_ERR, "unix_update waitpid failed: %m");
224           retval = PAM_AUTHTOK_ERR;
225         } else if (!WIFEXITED(retval)) {
226           pam_syslog(pamh, LOG_ERR, "unix_update abnormal exit: %d", retval);
227           retval = PAM_AUTHTOK_ERR;
228         } else {
229           retval = WEXITSTATUS(retval);
230         }
231     } else {
232         D(("fork failed"));
233         close(fds[0]);
234         close(fds[1]);
235         retval = PAM_AUTH_ERR;
236     }
237
238     if (off(UNIX_NOREAP, ctrl)) {
239         sigaction(SIGCHLD, &oldsa, NULL);   /* restore old signal handler */
240     }
241
242     return retval;
243 }
244 #endif
245
246 static int check_old_password(const char *forwho, const char *newpass)
247 {
248         static char buf[16384];
249         char *s_luser, *s_uid, *s_npas, *s_pas;
250         int retval = PAM_SUCCESS;
251         FILE *opwfile;
252
253         opwfile = fopen(OLD_PASSWORDS_FILE, "r");
254         if (opwfile == NULL)
255                 return PAM_ABORT;
256
257         while (fgets(buf, 16380, opwfile)) {
258                 if (!strncmp(buf, forwho, strlen(forwho))) {
259                         char *sptr;
260                         buf[strlen(buf) - 1] = '\0';
261                         s_luser = strtok_r(buf, ":,", &sptr);
262                         s_uid = strtok_r(NULL, ":,", &sptr);
263                         s_npas = strtok_r(NULL, ":,", &sptr);
264                         s_pas = strtok_r(NULL, ":,", &sptr);
265                         while (s_pas != NULL) {
266                                 char *md5pass = Goodcrypt_md5(newpass, s_pas);
267                                 if (!strcmp(md5pass, s_pas)) {
268                                         _pam_delete(md5pass);
269                                         retval = PAM_AUTHTOK_ERR;
270                                         break;
271                                 }
272                                 s_pas = strtok_r(NULL, ":,", &sptr);
273                                 _pam_delete(md5pass);
274                         }
275                         break;
276                 }
277         }
278         fclose(opwfile);
279
280         return retval;
281 }
282
283 static int _do_setpass(pam_handle_t* pamh, const char *forwho,
284                        const char *fromwhat,
285                        char *towhat, unsigned int ctrl, int remember)
286 {
287         struct passwd *pwd = NULL;
288         int retval = 0;
289         int unlocked = 0;
290         char *master = NULL;
291
292         D(("called"));
293
294         pwd = getpwnam(forwho);
295
296         if (pwd == NULL) {
297                 retval = PAM_AUTHTOK_ERR;
298                 goto done;
299         }
300
301         if (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, forwho, 0, 1)) {
302             if ((master=getNISserver(pamh)) != NULL) {
303                 struct timeval timeout;
304                 struct yppasswd yppwd;
305                 CLIENT *clnt;
306                 int status;
307                 enum clnt_stat err;
308
309                 /* Unlock passwd file to avoid deadlock */
310                 unlock_pwdf();
311                 unlocked = 1;
312
313                 /* Initialize password information */
314                 yppwd.newpw.pw_passwd = pwd->pw_passwd;
315                 yppwd.newpw.pw_name = pwd->pw_name;
316                 yppwd.newpw.pw_uid = pwd->pw_uid;
317                 yppwd.newpw.pw_gid = pwd->pw_gid;
318                 yppwd.newpw.pw_gecos = pwd->pw_gecos;
319                 yppwd.newpw.pw_dir = pwd->pw_dir;
320                 yppwd.newpw.pw_shell = pwd->pw_shell;
321                 yppwd.oldpass = fromwhat ? strdup (fromwhat) : strdup ("");
322                 yppwd.newpw.pw_passwd = towhat;
323
324                 D(("Set password %s for %s", yppwd.newpw.pw_passwd, forwho));
325
326                 /* The yppasswd.x file said `unix authentication required',
327                  * so I added it. This is the only reason it is in here.
328                  * My yppasswdd doesn't use it, but maybe some others out there
329                  * do.                                        --okir
330                  */
331                 clnt = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
332                 clnt->cl_auth = authunix_create_default();
333                 memset((char *) &status, '\0', sizeof(status));
334                 timeout.tv_sec = 25;
335                 timeout.tv_usec = 0;
336                 err = clnt_call(clnt, YPPASSWDPROC_UPDATE,
337                                 (xdrproc_t) xdr_yppasswd, (char *) &yppwd,
338                                 (xdrproc_t) xdr_int, (char *) &status,
339                                 timeout);
340
341                 free (yppwd.oldpass);
342
343                 if (err) {
344                         _make_remark(pamh, ctrl, PAM_TEXT_INFO,
345                                 clnt_sperrno(err));
346                 } else if (status) {
347                         D(("Error while changing NIS password.\n"));
348                 }
349                 D(("The password has%s been changed on %s.",
350                    (err || status) ? " not" : "", master));
351                 pam_syslog(pamh, LOG_NOTICE, "password%s changed for %s on %s",
352                          (err || status) ? " not" : "", pwd->pw_name, master);
353
354                 auth_destroy(clnt->cl_auth);
355                 clnt_destroy(clnt);
356                 if (err || status) {
357                         _make_remark(pamh, ctrl, PAM_TEXT_INFO,
358                                 _("NIS password could not be changed."));
359                         retval = PAM_TRY_AGAIN;
360                 }
361 #ifdef DEBUG
362                 sleep(5);
363 #endif
364             } else {
365                     retval = PAM_TRY_AGAIN;
366             }
367         }
368
369         if (_unix_comesfromsource(pamh, forwho, 1, 0)) {
370                 if(unlocked) {
371                         if (lock_pwdf() != PAM_SUCCESS) {
372                                 return PAM_AUTHTOK_LOCK_BUSY;
373                         }
374                 }
375 #ifdef WITH_SELINUX
376                 if (unix_selinux_confined())
377                           return _unix_run_update_binary(pamh, ctrl, forwho, fromwhat, towhat, remember);
378 #endif
379                 /* first, save old password */
380                 if (save_old_password(pamh, forwho, fromwhat, remember)) {
381                         retval = PAM_AUTHTOK_ERR;
382                         goto done;
383                 }
384                 if (on(UNIX_SHADOW, ctrl) || is_pwd_shadowed(pwd)) {
385                         retval = unix_update_shadow(pamh, forwho, towhat);
386                         if (retval == PAM_SUCCESS)
387                                 if (!is_pwd_shadowed(pwd))
388                                         retval = unix_update_passwd(pamh, forwho, "x");
389                 } else {
390                         retval = unix_update_passwd(pamh, forwho, towhat);
391                 }
392         }
393
394
395 done:
396         unlock_pwdf();
397
398         return retval;
399 }
400
401 static int _unix_verify_shadow(pam_handle_t *pamh, const char *user, unsigned int ctrl)
402 {
403         struct passwd *pwent = NULL;    /* Password and shadow password */
404         struct spwd *spent = NULL;      /* file entries for the user */
405         int daysleft;
406         int retval;
407
408         retval = get_account_info(pamh, user, &pwent, &spent);
409         if (retval == PAM_USER_UNKNOWN) {
410                 return retval;
411         }
412
413         if (retval == PAM_SUCCESS && spent == NULL)
414                 return PAM_SUCCESS;
415
416         if (retval == PAM_UNIX_RUN_HELPER) {
417                 retval = _unix_run_verify_binary(pamh, ctrl, user, &daysleft);
418                 if (retval == PAM_AUTH_ERR || retval == PAM_USER_UNKNOWN)
419                         return retval;
420         }
421         else if (retval == PAM_SUCCESS)
422                 retval = check_shadow_expiry(pamh, spent, &daysleft);
423
424         if (on(UNIX__IAMROOT, ctrl) || retval == PAM_NEW_AUTHTOK_REQD)
425                 return PAM_SUCCESS;
426
427         return retval;
428 }
429
430 static int _pam_unix_approve_pass(pam_handle_t * pamh
431                                   ,unsigned int ctrl
432                                   ,const char *pass_old
433                                   ,const char *pass_new)
434 {
435         const void *user;
436         const char *remark = NULL;
437         int retval = PAM_SUCCESS;
438
439         D(("&new=%p, &old=%p", pass_old, pass_new));
440         D(("new=[%s]", pass_new));
441         D(("old=[%s]", pass_old));
442
443         if (pass_new == NULL || (pass_old && !strcmp(pass_old, pass_new))) {
444                 if (on(UNIX_DEBUG, ctrl)) {
445                         pam_syslog(pamh, LOG_DEBUG, "bad authentication token");
446                 }
447                 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pass_new == NULL ?
448                         _("No password supplied") : _("Password unchanged"));
449                 return PAM_AUTHTOK_ERR;
450         }
451         /*
452          * if one wanted to hardwire authentication token strength
453          * checking this would be the place - AGM
454          */
455
456         retval = pam_get_item(pamh, PAM_USER, &user);
457         if (retval != PAM_SUCCESS) {
458                 if (on(UNIX_DEBUG, ctrl)) {
459                         pam_syslog(pamh, LOG_ERR, "Can not get username");
460                         return PAM_AUTHTOK_ERR;
461                 }
462         }
463         if (off(UNIX__IAMROOT, ctrl)) {
464                 if (strlen(pass_new) < 6)
465                   remark = _("You must choose a longer password");
466                 D(("length check [%s]", remark));
467                 if (on(UNIX_REMEMBER_PASSWD, ctrl)) {
468                         if ((retval = check_old_password(user, pass_new)) == PAM_AUTHTOK_ERR)
469                           remark = _("Password has been already used. Choose another.");
470                         if (retval == PAM_ABORT) {
471                                 pam_syslog(pamh, LOG_ERR, "can't open %s file to check old passwords",
472                                         OLD_PASSWORDS_FILE);
473                                 return retval;
474                         }
475                 }
476         }
477         if (remark) {
478                 _make_remark(pamh, ctrl, PAM_ERROR_MSG, remark);
479                 retval = PAM_AUTHTOK_ERR;
480         }
481         return retval;
482 }
483
484
485 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
486                                 int argc, const char **argv)
487 {
488         unsigned int ctrl, lctrl;
489         int retval;
490         int remember = -1;
491         int rounds = -1;
492
493         /* <DO NOT free() THESE> */
494         const char *user;
495         const void *pass_old, *pass_new;
496         /* </DO NOT free() THESE> */
497
498         D(("called."));
499
500         ctrl = _set_ctrl(pamh, flags, &remember, &rounds, argc, argv);
501
502         /*
503          * First get the name of a user
504          */
505         retval = pam_get_user(pamh, &user, NULL);
506         if (retval == PAM_SUCCESS) {
507                 /*
508                  * Various libraries at various times have had bugs related to
509                  * '+' or '-' as the first character of a user name. Don't
510                  * allow them.
511                  */
512                 if (user == NULL || user[0] == '-' || user[0] == '+') {
513                         pam_syslog(pamh, LOG_ERR, "bad username [%s]", user);
514                         return PAM_USER_UNKNOWN;
515                 }
516                 if (retval == PAM_SUCCESS && on(UNIX_DEBUG, ctrl))
517                         pam_syslog(pamh, LOG_DEBUG, "username [%s] obtained",
518                                  user);
519         } else {
520                 if (on(UNIX_DEBUG, ctrl))
521                         pam_syslog(pamh, LOG_DEBUG,
522                                  "password - could not identify user");
523                 return retval;
524         }
525
526         D(("Got username of %s", user));
527
528         /*
529          * Before we do anything else, check to make sure that the user's
530          * info is in one of the databases we can modify from this module,
531          * which currently is 'files' and 'nis'.  We have to do this because
532          * getpwnam() doesn't tell you *where* the information it gives you
533          * came from, nor should it.  That's our job.
534          */
535         if (_unix_comesfromsource(pamh, user, 1, on(UNIX_NIS, ctrl)) == 0) {
536                 pam_syslog(pamh, LOG_DEBUG,
537                          "user \"%s\" does not exist in /etc/passwd%s",
538                          user, on(UNIX_NIS, ctrl) ? " or NIS" : "");
539                 return PAM_USER_UNKNOWN;
540         } else {
541                 struct passwd *pwd;
542                 _unix_getpwnam(pamh, user, 1, 1, &pwd);
543                 if (pwd == NULL) {
544                         pam_syslog(pamh, LOG_DEBUG,
545                                 "user \"%s\" has corrupted passwd entry",
546                                 user);
547                         return PAM_USER_UNKNOWN;
548                 }
549         }
550
551         /*
552          * This is not an AUTH module!
553          */
554         if (on(UNIX__NONULL, ctrl))
555                 set(UNIX__NULLOK, ctrl);
556
557         if (on(UNIX__PRELIM, ctrl)) {
558                 /*
559                  * obtain and verify the current password (OLDAUTHTOK) for
560                  * the user.
561                  */
562                 char *Announce;
563
564                 D(("prelim check"));
565
566                 if (_unix_blankpasswd(pamh, ctrl, user)) {
567                         return PAM_SUCCESS;
568                 } else if (off(UNIX__IAMROOT, ctrl)) {
569                         /* instruct user what is happening */
570                         if (asprintf(&Announce, _("Changing password for %s."),
571                                 user) < 0) {
572                                 pam_syslog(pamh, LOG_CRIT,
573                                          "password - out of memory");
574                                 return PAM_BUF_ERR;
575                         }
576
577                         lctrl = ctrl;
578                         set(UNIX__OLD_PASSWD, lctrl);
579                         retval = _unix_read_password(pamh, lctrl
580                                                      ,Announce
581                                              ,_("(current) UNIX password: ")
582                                                      ,NULL
583                                                      ,_UNIX_OLD_AUTHTOK
584                                              ,&pass_old);
585                         free(Announce);
586
587                         if (retval != PAM_SUCCESS) {
588                                 pam_syslog(pamh, LOG_NOTICE,
589                                     "password - (old) token not obtained");
590                                 return retval;
591                         }
592                         /* verify that this is the password for this user */
593
594                         retval = _unix_verify_password(pamh, user, pass_old, ctrl);
595                 } else {
596                         D(("process run by root so do nothing this time around"));
597                         pass_old = NULL;
598                         retval = PAM_SUCCESS;   /* root doesn't have too */
599                 }
600
601                 if (retval != PAM_SUCCESS) {
602                         D(("Authentication failed"));
603                         pass_old = NULL;
604                         return retval;
605                 }
606                 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
607                 pass_old = NULL;
608                 if (retval != PAM_SUCCESS) {
609                         pam_syslog(pamh, LOG_CRIT,
610                                  "failed to set PAM_OLDAUTHTOK");
611                 }
612                 retval = _unix_verify_shadow(pamh,user, ctrl);
613                 if (retval == PAM_AUTHTOK_ERR) {
614                         if (off(UNIX__IAMROOT, ctrl))
615                                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
616                                              _("You must wait longer to change your password"));
617                         else
618                                 retval = PAM_SUCCESS;
619                 }
620         } else if (on(UNIX__UPDATE, ctrl)) {
621                 /*
622                  * tpass is used below to store the _pam_md() return; it
623                  * should be _pam_delete()'d.
624                  */
625
626                 char *tpass = NULL;
627                 int retry = 0;
628
629                 /*
630                  * obtain the proposed password
631                  */
632
633                 D(("do update"));
634
635                 /*
636                  * get the old token back. NULL was ok only if root [at this
637                  * point we assume that this has already been enforced on a
638                  * previous call to this function].
639                  */
640
641                 if (off(UNIX_NOT_SET_PASS, ctrl)) {
642                         retval = pam_get_item(pamh, PAM_OLDAUTHTOK
643                                               ,&pass_old);
644                 } else {
645                         retval = pam_get_data(pamh, _UNIX_OLD_AUTHTOK
646                                               ,&pass_old);
647                         if (retval == PAM_NO_MODULE_DATA) {
648                                 retval = PAM_SUCCESS;
649                                 pass_old = NULL;
650                         }
651                 }
652                 D(("pass_old [%s]", pass_old));
653
654                 if (retval != PAM_SUCCESS) {
655                         pam_syslog(pamh, LOG_NOTICE, "user not authenticated");
656                         return retval;
657                 }
658
659                 D(("get new password now"));
660
661                 lctrl = ctrl;
662
663                 if (on(UNIX_USE_AUTHTOK, lctrl)) {
664                         set(UNIX_USE_FIRST_PASS, lctrl);
665                 }
666                 retry = 0;
667                 retval = PAM_AUTHTOK_ERR;
668                 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
669                         /*
670                          * use_authtok is to force the use of a previously entered
671                          * password -- needed for pluggable password strength checking
672                          */
673
674                         retval = _unix_read_password(pamh, lctrl
675                                                      ,NULL
676                                              ,_("Enter new UNIX password: ")
677                                             ,_("Retype new UNIX password: ")
678                                                      ,_UNIX_NEW_AUTHTOK
679                                              ,&pass_new);
680
681                         if (retval != PAM_SUCCESS) {
682                                 if (on(UNIX_DEBUG, ctrl)) {
683                                         pam_syslog(pamh, LOG_ALERT,
684                                                  "password - new password not obtained");
685                                 }
686                                 pass_old = NULL;        /* tidy up */
687                                 return retval;
688                         }
689                         D(("returned to _unix_chauthtok"));
690
691                         /*
692                          * At this point we know who the user is and what they
693                          * propose as their new password. Verify that the new
694                          * password is acceptable.
695                          */
696
697                         if (*(const char *)pass_new == '\0') {  /* "\0" password = NULL */
698                                 pass_new = NULL;
699                         }
700                         retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new);
701                         
702                         if (retval != PAM_SUCCESS && off(UNIX_NOT_SET_PASS, ctrl)) {
703                                 pam_set_item(pamh, PAM_AUTHTOK, NULL);
704                         }
705                 }
706
707                 if (retval != PAM_SUCCESS) {
708                         pam_syslog(pamh, LOG_NOTICE,
709                                  "new password not acceptable");
710                         pass_new = pass_old = NULL;     /* tidy up */
711                         return retval;
712                 }
713                 if (lock_pwdf() != PAM_SUCCESS) {
714                         return PAM_AUTHTOK_LOCK_BUSY;
715                 }
716
717                 if (pass_old) {
718                         retval = _unix_verify_password(pamh, user, pass_old, ctrl);
719                         if (retval != PAM_SUCCESS) {
720                                 pam_syslog(pamh, LOG_NOTICE, "user password changed by another process");
721                                 unlock_pwdf();
722                                 return retval;
723                         }
724                 }
725
726                 retval = _unix_verify_shadow(pamh, user, ctrl);
727                 if (retval != PAM_SUCCESS) {
728                         pam_syslog(pamh, LOG_NOTICE, "user shadow entry expired");
729                         unlock_pwdf();
730                         return retval;
731                 }
732
733                 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new);
734                 if (retval != PAM_SUCCESS) {
735                         pam_syslog(pamh, LOG_NOTICE,
736                                  "new password not acceptable 2");
737                         pass_new = pass_old = NULL;     /* tidy up */
738                         unlock_pwdf();
739                         return retval;
740                 }
741
742                 /*
743                  * By reaching here we have approved the passwords and must now
744                  * rebuild the password database file.
745                  */
746
747                 /*
748                  * First we encrypt the new password.
749                  */
750
751                 tpass = create_password_hash(pamh, pass_new, ctrl, rounds);
752                 if (tpass == NULL) {
753                         pam_syslog(pamh, LOG_CRIT,
754                                 "out of memory for password");
755                         pass_new = pass_old = NULL;     /* tidy up */
756                         unlock_pwdf();
757                         return PAM_BUF_ERR;
758                 }
759
760                 D(("password processed"));
761
762                 /* update the password database(s) -- race conditions..? */
763
764                 retval = _do_setpass(pamh, user, pass_old, tpass, ctrl,
765                                      remember);
766                 /* _do_setpass has called unlock_pwdf for us */
767
768                 _pam_delete(tpass);
769                 pass_old = pass_new = NULL;
770         } else {                /* something has broken with the module */
771                 pam_syslog(pamh, LOG_ALERT,
772                          "password received unknown request");
773                 retval = PAM_ABORT;
774         }
775
776         D(("retval was %d", retval));
777
778         return retval;
779 }
780
781
782 /* static module data */
783 #ifdef PAM_STATIC
784 struct pam_module _pam_unix_passwd_modstruct = {
785     "pam_unix_passwd",
786     NULL,
787     NULL,
788     NULL,
789     NULL,
790     NULL,
791     pam_sm_chauthtok,
792 };
793 #endif