Search code examples
c++passwordsmode

How to have multiple attempt to enter password, with terminal in RAW mode?


I am creating a login menu in C++

i wish to give the user 3 attempts at entering the password before the program terminates.

My code runs fine if the user gets the password right the first time. It would then go into the home menu ect. However, if the user gets the password wrong. The terminal would run up to:

Login: Fred
Password: ***
Wrong password
Please re-enter password: 

After which point nothing would show up regardless of what the user types. Not even ctrl-C can exit the program. I was wondering if anyone know what's going on and can point me in the right direction.

Here's part of the codes for the method "login" in a class called "HomePage":

            cout<<"Password: ";

            while (loginAttempt < 3){                                       //The user gets to attempt to type
                                                                            //the password 3 times
                    password = receivePassword();                           //Receives password from user


                    if (flatMemberList[match].getPassword()==password){     //Check if the password is correct
                        cout<<endl<<"Welcome back "<<loginName<< endl;      //If correct, display welcome message
                        return;
                    }

                    else{
                       loginAttempt++;                                      //Record down one failed attempt
                       cout<<endl<<"Wrong password"<<endl;                  //If incorrect, display error
                       cout<<"Please re-enter password: ";
                    }
            }
            cout<<"you have exceeded the legal login attempts"<<endl;
            exit(1);

Where receivePassword() is a custom method as follows:

//This method is called when the user types in a password
//The terminal's setting is first changed to 'raw' configuration
//The password are taken in one letter at a time
//It outputs to terminal "*" instead of echoing the input
string HomePage::receivePassword(){

        termios oldt, newt;                                     //The structs for manipulation
        char password[PaswordLength];                           //Password held here
        int j = 0;                                              //Password index

        tcgetattr(STDIN_FILENO, &oldt);                         //Get configuration details
        newt = oldt;
        cfmakeraw(&newt);                                       //Set up new 'raw' configuration structure

    //Set up new terminal configuration

        tcsetattr(STDIN_FILENO, TCSANOW, &newt);

        cin.ignore(1000, '\n');                                 //flush all the buffers

        while(true){
            password[j] = cin.get();            
            if( password[j] == '\r' ) {                         //check if 'enter' key is entered
                password[j] = '\0';                             //replace cr with null to make C string
                break;
            }
            cout.put('*'); //echo the asterisk

            j++;
        } ;

    //Reset terminal to old configuration

        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

        return password;
    }

Thanks in advance

If you think the problem might be elsewhere, let me know, and i'll post the codes.


Solution

  • I don't know what your specific problem is. However, you can apply standard debugging techniques to find out which part is causing the problem.

    First, you're doing weird things with terminals (cfmakeraw, tcsetattr, etc). It sounds like that might be related to the problem. So, remove the code that hides the user input and make sure your program works when the password is echoed to the screen normally. You should be able to do that easily.

    After doing this, you can then decide whether your problem is related to:

    • the terminal attribute code that you removed, or
    • the password loop that remains.

    This is often called the "divide and conquer" debugging technique. If you remove the code that you think is the problem, then whether the problem remains or not can help you decide whether it's related to the code you removed.