clinuxpasswdgetpwnam

How to return "You have been logged in!" knowing a password but not UserID?


I have a basic Linux authentication program compiled, but I do not understand what it requires to return "You have been logged in!". I know a password entry, but not the associate UID.

    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <pwd.h>

    #define TRUE 1
    #define FALSE 0
    #define LENGTH 8

    int main(int argc, char *argv[])
    {
      char user[LENGTH];
      char prompt[] = "password: ";
      struct passwd *passwddata;
      char *user_pass;

      while(TRUE)
       {
         printf("login: ");
         fflush(NULL);
         if (gets(user) == NULL)
           exit(0);

         user_pass = getpass(prompt);
         passwddata = getpwnam(user);

         if (passwddata != NULL)
           {
             if (!strcmp(user_pass, "login"))
              {
                printf("You have been logged in! \n");
                break;
              }
            }
            printf ("You are not %s, don't try to fool the system.\n", user);
        }
      return 0 ;
    }

Solution

  • You need to have an user named "login".

    Type "login", when reading "login"

    if (gets(user) == NULL)
    

    And also type "login" when prompted for password.

    It's a little confuse, because you're trying to retrive this "login" user password, but you aren't using this information.

    passwddata = getpwnam(user);