]> granicus.if.org Git - shadow/commitdiff
* src/userdel.c, src/newusers.c, src/chpasswd.c, src/chfn.c,
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Sat, 6 Sep 2008 13:28:02 +0000 (13:28 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Sat, 6 Sep 2008 13:28:02 +0000 (13:28 +0000)
src/groupmems.c, src/usermod.c, src/groupdel.c, src/chgpasswd.c,
src/useradd.c, src/groupmod.c, src/groupadd.c, src/chage.c,
src/chsh.c: Simplify the PAM error handling. Do not keep the pamh
handle, but terminate the PAM transaction as soon as possible if
there are no PAM session opened.

14 files changed:
ChangeLog
src/chage.c
src/chfn.c
src/chgpasswd.c
src/chpasswd.c
src/chsh.c
src/groupadd.c
src/groupdel.c
src/groupmems.c
src/groupmod.c
src/newusers.c
src/useradd.c
src/userdel.c
src/usermod.c

index b1cdb280467f78f05be3a468702e59e19450e13e..1d317ad03a5aa1b13ba65605060529dda9317389 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-09-06  Nicolas François  <nicolas.francois@centraliens.net>
+
+       * src/userdel.c, src/newusers.c, src/chpasswd.c, src/chfn.c,
+       src/groupmems.c, src/usermod.c, src/groupdel.c, src/chgpasswd.c,
+       src/useradd.c, src/groupmod.c, src/groupadd.c, src/chage.c,
+       src/chsh.c: Simplify the PAM error handling. Do not keep the pamh
+       handle, but terminate the PAM transaction as soon as possible if
+       there are no PAM session opened.
+
 2008-09-06  Nicolas François  <nicolas.francois@centraliens.net>
 
        * src/newgrp.c, src/userdel.c, src/grpck.c, src/gpasswd.c,
index 43fa925c8d742e6f0ac3bde93db6d163e1f5e9f8..5766e0ab26427c49c1e2aced077282b14eac68fe 100644 (file)
@@ -83,10 +83,6 @@ static long warndays;
 static long inactdays;
 static long expdays;
 
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
-
 #define        EPOCH           "1969-12-31"
 
 /* local function prototypes */
@@ -132,16 +128,6 @@ static void fail_exit (int code)
        }
 #endif
 
-#ifdef USE_PAM
-       if (NULL != pamh) {
-               /* If there is a PAM error, pam_end will be called by the
-                * caller.
-                * We always end the pam transaction with PAM_SUCCESS here.
-                */
-               (void) pam_end (pamh, PAM_SUCCESS);
-       }
-#endif
-
        exit (code);
 }
 
@@ -499,6 +485,7 @@ static void check_flags (int argc, int opt_index)
 static void check_perms (void)
 {
 #ifdef USE_PAM
+       pam_handle_t *pamh = NULL;
        struct passwd *pampw;
        int retval;
 #endif
@@ -515,34 +502,26 @@ static void check_perms (void)
        }
 
 #ifdef USE_PAM
-       retval = PAM_SUCCESS;
-
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
        if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (PAM_SUCCESS == retval) {
+       } else {
                retval = pam_start ("chage", pampw->pw_name, &conv, &pamh);
        }
 
        if (PAM_SUCCESS == retval) {
                retval = pam_authenticate (pamh, 0);
-               if (PAM_SUCCESS != retval) {
-                       (void) pam_end (pamh, retval);
-               }
        }
 
        if (PAM_SUCCESS == retval) {
                retval = pam_acct_mgmt (pamh, 0);
-               if (PAM_SUCCESS != retval) {
-                       (void) pam_end (pamh, retval);
-               }
        }
 
+       if (NULL != pamh) {
+               (void) pam_end (pamh, retval);
+       }
        if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
-               pamh = NULL;
                fail_exit (E_NOPERM);
        }
 #endif                         /* USE_PAM */
@@ -912,10 +891,6 @@ int main (int argc, char **argv)
 
        SYSLOG ((LOG_INFO, "changed password expiry for %s", user_name));
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        closelog ();
        exit (E_SUCCESS);
 }
index 9f0f60c059fbac0e931aa84416f432c285c41a8b..0137a9cf9b9b1d5e1b6f87180c12b9b8dcbcb7f9 100644 (file)
@@ -69,9 +69,6 @@ static bool rflg = false;             /* -r - set room number              */
 static bool wflg = false;              /* -w - set work phone number        */
 static bool hflg = false;              /* -h - set home phone number        */
 static bool oflg = false;              /* -o - set other information        */
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
 static bool pw_locked = false;
 
 /*
@@ -338,6 +335,7 @@ static void process_flags (int argc, char **argv)
 static void check_perms (const struct passwd *pw)
 {
 #ifdef USE_PAM
+       pam_handle_t *pamh = NULL;
        int retval;
        struct passwd *pampw;
 #endif
@@ -377,32 +375,25 @@ static void check_perms (const struct passwd *pw)
        }
 
 #else                          /* !USE_PAM */
-       retval = PAM_SUCCESS;
-
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
-       if (pampw == NULL) {
+       if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (retval == PAM_SUCCESS) {
+       } else {
                retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
        }
 
-       if (retval == PAM_SUCCESS) {
+       if (PAM_SUCCESS == retval) {
                retval = pam_authenticate (pamh, 0);
-               if (retval != PAM_SUCCESS) {
-                       pam_end (pamh, retval);
-               }
        }
 
-       if (retval == PAM_SUCCESS) {
+       if (PAM_SUCCESS == retval) {
                retval = pam_acct_mgmt (pamh, 0);
-               if (retval != PAM_SUCCESS) {
-                       pam_end (pamh, retval);
-               }
        }
 
-       if (retval != PAM_SUCCESS) {
+       if (NULL != pamh) {
+               (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (E_NOPERM);
        }
@@ -725,10 +716,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("passwd");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        closelog ();
        exit (E_SUCCESS);
 }
index 9262d19b01ea49fcc51e9db85715bc3f3baa374b..aaa39e1cb33a92327cda4dab77cb7293b54447b3 100644 (file)
@@ -68,10 +68,6 @@ static bool sgr_locked = false;
 #endif
 static bool gr_locked = false;
 
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
-
 /* local function prototypes */
 static void fail_exit (int code);
 static void usage (void);
@@ -251,15 +247,14 @@ static void check_flags (void)
 static void check_perms (void)
 {
 #ifdef USE_PAM
-       int retval = PAM_SUCCESS;
+       pam_handle_t *pamh = NULL;
+       int retval;
        struct passwd *pampw;
 
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
        if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (PAM_SUCCESS == retval) {
+       } else {
                retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
        }
 
@@ -271,8 +266,10 @@ static void check_perms (void)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (1);
        }
@@ -528,10 +525,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        return (0);
 }
 
index 723bdfcc1e2720e1d566163486d42d8c6c909efe..99093ffa6b8d8f4025674372e25ed47853854a8b 100644 (file)
@@ -64,10 +64,6 @@ static bool is_shadow_pwd;
 static bool pw_locked = false;
 static bool spw_locked = false;
 
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
-
 /* local function prototypes */
 static void fail_exit (int code);
 static void usage (void);
@@ -245,15 +241,14 @@ static void check_flags (void)
 static void check_perms (void)
 {
 #ifdef USE_PAM
-       int retval = PAM_SUCCESS;
+       pam_handle_t *pamh = NULL;
+       int retval;
        struct passwd *pampw;
 
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
        if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (PAM_SUCCESS == retval) {
+       } else {
                retval = pam_start ("chpasswd", pampw->pw_name, &conv, &pamh);
        }
 
@@ -265,8 +260,10 @@ static void check_perms (void)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (1);
        }
@@ -513,10 +510,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("passwd");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        return (0);
 }
 
index 7b64017fd41682a119e4d0c0fbd1e0521b2623bb..269b7a150a3cb714e12d68a1dc0e308751b24da5 100644 (file)
@@ -64,9 +64,6 @@ static bool amroot;           /* Real UID is root */
 static char loginsh[BUFSIZ];   /* Name of new login shell */
 /* command line options */
 static bool sflg = false;      /* -s - set shell from command line  */
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
 static bool pw_locked = false;
 
 /* external identifiers */
@@ -251,6 +248,7 @@ static void process_flags (int argc, char **argv)
 static void check_perms (const struct passwd *pw)
 {
 #ifdef USE_PAM
+       pam_handle_t *pamh = NULL;
        int retval;
        struct passwd *pampw;
 #endif
@@ -306,14 +304,10 @@ static void check_perms (const struct passwd *pw)
         }
 
 #else                          /* !USE_PAM */
-       retval = PAM_SUCCESS;
-
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
        if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (PAM_SUCCESS == retval) {
+       } else {
                retval = pam_start ("chsh", pampw->pw_name, &conv, &pamh);
        }
 
@@ -325,8 +319,10 @@ static void check_perms (const struct passwd *pw)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (E_NOPERM);
        }
@@ -544,10 +540,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("passwd");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        closelog ();
        exit (E_SUCCESS);
 }
index d509ee70e9c48ae79d69b5c8d7dd4af675e202fd..f6ff407923ddc2021416dab3c43d6f011ef8ca07 100644 (file)
@@ -87,11 +87,6 @@ static bool sgr_locked = false;
 #endif
 static bool gr_locked = false;
 
-
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
-
 /* local function prototypes */
 static void usage (void);
 static void new_grent (struct group *grent);
@@ -400,14 +395,6 @@ static void fail_exit (int code)
        }
 #endif
 
-#ifdef USE_PAM
-       if (NULL != pamh) {
-               /* If there is a PAM error, fail_exit is not called.
-                * We always end the pam transaction with PAM_SUCCESS here.
-                */
-               (void) pam_end (pamh, PAM_SUCCESS);
-       }
-#endif
        exit (code);
 }
 
@@ -579,34 +566,29 @@ static void check_flags (void)
 static void check_perms (void)
 {
 #ifdef USE_PAM
-       int retval = PAM_SUCCESS;
+       pam_handle_t *pamh = NULL;
+       int retval;
        struct passwd *pampw;
 
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
-       if (pampw == NULL) {
+       if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
+       } else {
+               retval = pam_start ("groupadd", pampw->pw_name, &conv, &pamh);
        }
 
-       if (retval == PAM_SUCCESS) {
-               retval = pam_start ("groupadd", pampw->pw_name,
-                                   &conv, &pamh);
-       }
-
-       if (retval == PAM_SUCCESS) {
+       if (PAM_SUCCESS == retval) {
                retval = pam_authenticate (pamh, 0);
-               if (retval != PAM_SUCCESS) {
-                       (void) pam_end (pamh, retval);
-               }
        }
 
-       if (retval == PAM_SUCCESS) {
+       if (PAM_SUCCESS == retval) {
                retval = pam_acct_mgmt (pamh, 0);
-               if (retval != PAM_SUCCESS) {
-                       (void) pam_end (pamh, retval);
-               }
        }
 
-       if (retval != PAM_SUCCESS) {
+       if (NULL != pamh) {
+               (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (1);
        }
@@ -661,10 +643,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        exit (E_SUCCESS);
        /* NOT REACHED */
 }
index 792450e0b9296b18ecab07c9ffe70773b4e1352f..1981ab1983464148bf17197343e13cf7d516fef8 100644 (file)
@@ -337,16 +337,12 @@ int main (int argc, char **argv)
        OPENLOG ("groupdel");
 
 #ifdef USE_PAM
-       retval = PAM_SUCCESS;
-
        {
                struct passwd *pampw;
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
                if (pampw == NULL) {
                        retval = PAM_USER_UNKNOWN;
-               }
-
-               if (PAM_SUCCESS == retval) {
+               } else {
                        retval = pam_start ("groupdel", pampw->pw_name,
                                            &conv, &pamh);
                }
@@ -360,8 +356,10 @@ int main (int argc, char **argv)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (1);
        }
@@ -435,10 +433,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        return E_SUCCESS;
 }
 
index e5ce73d033c194870b6c7e8d894fcad30d6ab1ae..abfc25bc50fce4881ca85bb32ee69c4628df55dd 100644 (file)
@@ -440,7 +440,7 @@ static void check_perms (void)
        if (!list) {
 #ifdef USE_PAM
                pam_handle_t *pamh = NULL;
-               int retval = PAM_SUCCESS;
+               int retval;
                struct passwd *pampw;
 
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
@@ -459,7 +459,9 @@ static void check_perms (void)
                        retval = pam_acct_mgmt (pamh, 0);
                }
 
-               (void) pam_end (pamh, retval);
+               if (NULL != pamh) {
+                       (void) pam_end (pamh, retval);
+               }
                if (PAM_SUCCESS != retval) {
                        fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                        fail_exit (1);
index 3d536dbfef11878da8fd8378db451ade28be562a..6d09d2b67f87f2c4cab384cb9b7c894a431e95e3 100644 (file)
@@ -702,16 +702,12 @@ int main (int argc, char **argv)
        OPENLOG ("groupmod");
 
 #ifdef USE_PAM
-       retval = PAM_SUCCESS;
-
        {
                struct passwd *pampw;
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
-               if (pampw == NULL) {
+               if (NULL == pamh) {
                        retval = PAM_USER_UNKNOWN;
-               }
-
-               if (PAM_SUCCESS == retval) {
+               } else {
                        retval = pam_start ("groupmod", pampw->pw_name,
                                            &conv, &pamh);
                }
@@ -725,8 +721,10 @@ int main (int argc, char **argv)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                fail_exit (1);
        }
@@ -810,9 +808,6 @@ int main (int argc, char **argv)
 
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
        exit (E_SUCCESS);
        /* NOT REACHED */
 }
index 4509969049422e24235320372186f1ff0f63faae..e30a159bc3f0121a31a9b5e141b18f2e761336ec 100644 (file)
@@ -83,10 +83,6 @@ static bool pw_locked = false;
 static bool gr_locked = false;
 static bool spw_locked = false;
 
-#ifdef USE_PAM
-static pam_handle_t *pamh = NULL;
-#endif
-
 /* local function prototypes */
 static void usage (void);
 static void fail_exit (int);
@@ -566,15 +562,14 @@ static void check_flags (void)
 static void check_perms (void)
 {
 #ifdef USE_PAM
-       int retval = PAM_SUCCESS;
+       pam_handle_t *pamh = NULL;
+       int retval;
        struct passwd *pampw;
 
        pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
        if (NULL == pampw) {
                retval = PAM_USER_UNKNOWN;
-       }
-
-       if (PAM_SUCCESS == retval) {
+       } else {
                retval = pam_start ("newusers", pampw->pw_name, &conv, &pamh);
        }
 
@@ -586,8 +581,10 @@ static void check_perms (void)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                fail_exit (1);
        }
@@ -945,10 +942,6 @@ int main (int argc, char **argv)
        nscd_flush_cache ("passwd");
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        return 0;
 }
 
index e3efcb7e9dc4d37ef956f2b718d2941fdda469cb..40421c393e471ac9722c54ee46084a397be44900 100644 (file)
@@ -1793,9 +1793,7 @@ int main (int argc, char **argv)
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
                if (pampw == NULL) {
                        retval = PAM_USER_UNKNOWN;
-               }
-
-               if (PAM_SUCCESS == retval) {
+               } else {
                        retval = pam_start ("useradd", pampw->pw_name,
                                            &conv, &pamh);
                }
@@ -1809,8 +1807,10 @@ int main (int argc, char **argv)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                fail_exit (1);
        }
@@ -1952,10 +1952,6 @@ int main (int argc, char **argv)
        nscd_flush_cache ("passwd");
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        return E_SUCCESS;
 }
 
index 3a8862db688350bf4aa790e42652a707cefd6488..13dc8cac91bebf42aff5d50cdde6078da9a4a319 100644 (file)
@@ -838,9 +838,7 @@ int main (int argc, char **argv)
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
                if (pampw == NULL) {
                        retval = PAM_USER_UNKNOWN;
-               }
-
-               if (retval == PAM_SUCCESS) {
+               } else {
                        retval = pam_start ("userdel", pampw->pw_name,
                                            &conv, &pamh);
                }
@@ -854,8 +852,10 @@ int main (int argc, char **argv)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (E_PW_UPDATE);
        }
@@ -1000,9 +1000,6 @@ int main (int argc, char **argv)
        nscd_flush_cache ("passwd");
        nscd_flush_cache ("group");
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
        exit ((0 != errors) ? E_HOMEDIR : E_SUCCESS);
        /* NOT REACHED */
 }
index ca1a7688a46eb0aec5ff8d3321f0c6368b532e94..1ca0afd33967c8af1b3238e00b6bcd38e24acf2e 100644 (file)
@@ -1659,9 +1659,7 @@ int main (int argc, char **argv)
                pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
                if (pampw == NULL) {
                        retval = PAM_USER_UNKNOWN;
-               }
-
-               if (PAM_SUCCESS == retval) {
+               } else {
                        retval = pam_start ("usermod", pampw->pw_name,
                                            &conv, &pamh);
                }
@@ -1675,8 +1673,10 @@ int main (int argc, char **argv)
                retval = pam_acct_mgmt (pamh, 0);
        }
 
-       if (PAM_SUCCESS != retval) {
+       if (NULL != pamh) {
                (void) pam_end (pamh, retval);
+       }
+       if (PAM_SUCCESS != retval) {
                fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
                exit (1);
        }
@@ -1722,10 +1722,6 @@ int main (int argc, char **argv)
                            user_gid, gflg ? user_newgid : user_gid);
        }
 
-#ifdef USE_PAM
-       (void) pam_end (pamh, PAM_SUCCESS);
-#endif                         /* USE_PAM */
-
        exit (E_SUCCESS);
        /* NOT REACHED */
 }