-1

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 ;
    }
oniera
  • 23
  • 1
  • 3
  • Possible duplicate of [How can I get the user id associated with a login on Linux?](https://stackoverflow.com/q/3836365/608639), [Programmatically getting UID and GID from username in Unix?](https://stackoverflow.com/q/1009254/608639), etc. Also see [How to drop root privileges on linux in C?](https://stackoverflow.com/q/25435006/608639) – jww Apr 23 '19 at 02:54

1 Answers1

0

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);
Gabriel Pellegrino
  • 1,042
  • 1
  • 8
  • 17