style fixes. No code changes
[oweals/busybox.git] / libbb / correct_password.c
index 39625361407302b4a46390a43cd47fc4282bbf2a..c515b26afb581402f6e974861a7820997895d12d 100644 (file)
  * SUCH DAMAGE.
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <syslog.h>
-#include <ctype.h>
-#include <crypt.h>
-
 #include "libbb.h"
 
-
-
 /* Ask the user for a password.
    Return 1 if the user gives the correct password for entry PW,
    0 if not.  Return 1 without asking for a password if run by UID 0
    or if PW has an empty password.  */
 
-int correct_password ( const struct passwd *pw )
+int correct_password(const struct passwd *pw)
 {
-       char *unencrypted, *encrypted, *correct;
-       
-#ifdef CONFIG_FEATURE_SHADOWPASSWDS
-       if (( strcmp ( pw-> pw_passwd, "x" ) == 0 ) || ( strcmp ( pw-> pw_passwd, "*" ) == 0 )) {
-               struct spwd *sp = getspnam ( pw-> pw_name );
-               
-               if ( !sp )
-                       bb_error_msg_and_die ( "no valid shadow password" );
-               
-               correct = sp-> sp_pwdp;
+       char *unencrypted, *encrypted;
+       const char *correct;
+#if ENABLE_FEATURE_SHADOWPASSWDS
+       /* Using _r function to avoid pulling in static buffers */
+       struct spwd spw;
+       struct spwd *result;
+       char buffer[256];
+#endif
+
+       correct = pw->pw_passwd;
+#if ENABLE_FEATURE_SHADOWPASSWDS
+       if (LONE_CHAR(pw->pw_passwd, 'x') || LONE_CHAR(pw->pw_passwd, '*')) {
+               if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result))
+                       bb_error_msg("no valid shadow password, checking ordinary one");
+               else
+                       correct = spw.sp_pwdp;
        }
-       else
 #endif
-       correct = pw-> pw_passwd;
 
-       if ( correct == 0 || correct[0] == '\0' )
+       if (!correct || correct[0] == '\0')
                return 1;
 
-       unencrypted = getpass ( "Password: " );
-       if ( !unencrypted )
-       {
-               fputs ( "getpass: cannot open /dev/tty\n", stderr );
+       unencrypted = bb_askpass(0, "Password: ");
+       if (!unencrypted) {
                return 0;
        }
-       encrypted = crypt ( unencrypted, correct );
-       memset ( unencrypted, 0, bb_strlen ( unencrypted ));
-       return ( strcmp ( encrypted, correct ) == 0 ) ? 1 : 0;
+       encrypted = crypt(unencrypted, correct);
+       memset(unencrypted, 0, strlen(unencrypted));
+       return strcmp(encrypted, correct) == 0;
 }