Linux Terminal password input

Source: Internet
Author: User
Oh, I wrote something to play on my blog, because I searched chinaunix (habitual action) and didn't find it before, so I posted it. don't laugh, correct the error. thank you! : Mrgreen: recently, due to the security needs of a program, you need to enter the user name and password when running the program. Ah, I have never done anything in the program. Oh, I wrote something to play on my blog, because I searched chinaunix (habitual action) and didn't find it before, so I posted it. don't laugh, correct the error. thank you! : Mrgreen:

Recently, due to the security needs of a program, you need to enter the user name and password when running the program. Ah, I have never performed permission authentication during the running of the program. in the past, permission management was achieved through user permission control at the machine system level (this is also the most practical fact, I think, ). OK, it is impossible not to do it. sometimes it is very painful to accept others' ideas. Start with the topic.

Which of the following passwords cannot be displayed? It's not enough to carry some people behind your back, but you have to worry about the technology. you need to know how hard it is to do the technology and how tired it is! Forget it. follow the public's habits. Originally, the getpass () function provided by the system in Linux was too early (I guess this may be the case at the time). it only supports 8 characters, at the same time, there are security risks (overflow !), Obsolate, poor child. Should there be a replacement? For example, a function like getpass_new (), K ~ I have been searching for half a day. I cannot find it! Depressed. No way. find a solution. I checked some information. The most common information is getch () in the ncurses library, but I am Terminal. I cannot use pirated SecureCRT. There are two other approaches, both of which use termios to control the lower layer. One is to use the most powerful function: ioctl (), and use the data struct termio to set TCGETA and TCSETA. I hate ioctl! Originally, this function was quite simple, but I think it was just a lazy person to develop the OS! What do you want to control IO and how to control it? you have to find parameters and data structures on your own. this is depressing. Hey, it's the final turn of the main character. I think this is the right way! By mistake, this is what is hidden in termios. h:

int tcgetattr (int __fd, struct termios *__termios_p);
int tcsetattr (int __fd, int __optional_actions,
              __const struct termios *__termios_p);
 

Ah, after searching for half a day, I finally found her who made me fall in love at first sight ~
In fact, this is also to set the file (a UNIX file), should it also call ioctl? I guess, I didn't see the source code :) but at least I think it is dedicated to this. it depends on how specific people are!

Check it out. Fortunately, the optional actions of tcsetattr has only three options at a glance. of course, Flush action (TCSAFLUSH) is used. Everything is ready! Therefore, _ fd uses 0 (stdin) and stderr for output. action uses TCSAFLUSH. termios_p only needs to set c_lflag, and ~ ECHO and OK. Oh, I finally got it done. Source code:



#include 
 
void UserIf::Echoff(int f)
{
  struct termios sg;        
  tcgetattr(f, &sg);             /* get settings */
  sg.c_lflag &= ~ECHO;     /* turn echo off */
  tcsetattr(f,TCSAFLUSH,&sg); /* set settings */
}
 
void UserIf::Echon(int f)
{
  struct termios sg;
  tcgetattr(f, &sg);    /* get settings */
  sg.c_lflag |= ECHO;  /* turn echo on */
  tcsetattr(f,TCSAFLUSH,&sg); /* set settings */
}
 
string UserIf::getPassword(string prompt, int n)
{
 char c;                   // for read()
 int i;                      // number of  input
 char *w;                // warning on retry
 int f;                      // file descripto
 char *p = new char(n+2);
 string ret;
 f = 0;  // Read from stdin
 // get password
 w = ""; //warning
 do
 {
   fputs(w, stderr);
   fputs(prompt.c_str(), stderr);
   fflush(stderr);
   i = 0;
   Echoff(f);
   do {                    /* read line, keeping n */
     read(f, &c, 1);
     if (i < n)
     p[i++] = c;
   } while (c != );
   Echon(f);
   putc(, stderr);
   fflush(stderr);
   w = "(line too long)";
 } while (p[i-1] != );
 p[i-1] = 0; //!!!notice
 ret = string(p);
 delete p;
 return ret;
} /* end function getPassword */


The writing is a bit messy. I originally wanted to use the stream of std to do it. except for the most basic functions, I still switched to the file operation mode of c.

Ah, it's really hard to do the technology. a small function is a waste of half a day, but it's still a bit of a sense of accomplishment. the process of exploration is also very beautiful: P
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.