LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2003, 03:59 PM   #1
qcoder
Member
 
Registered: Oct 2003
Location: United States
Distribution: Debian
Posts: 65

Rep: Reputation: 15
Question Security Program C++


I would like to know how I can write a programm that just sits there and waits for the user to input a password. I want to make it so that the user can not exit the program by Ctrl-C. I also would like to have the password show up as "*" when the user types the password.
 
Old 11-28-2003, 04:22 PM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
This little program ignores the signals SIGINT (CTRL-C) and SIGTSTP (CTRL-Z) by adding a signal handler that does nothing (the predefined handler SIGIGN)

Code:
 #include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 
 int main(void) {

     struct sigaction sa;
     struct sigaction oldint;
     struct sigaction oldtstp;
 
     /* Ignore signal completely */
     sa.sa_handler = SIG_IGN;
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = 0;
 
     /* Set up the signal handler for SIGINT (CTRL-C),
	remember old value, so we can restore it later */
     sigaction(SIGINT, &sa, &oldint);

     /* Same for SIGTSTP (CTRL-Z)*/
     sigaction(SIGTSTP, &sa, &oldtstp);
 
     /* Sleep for five seconds, to terminate the program */
     sleep(5);
 
     /* Restore the behavior of SIGINT and SIGTSTP, not really
	neccessary here. Just to show that it's possible*/
     sigaction(SIGINT, &oldint, NULL);
     sigaction(SIGTSTP, &oldtstp, NULL);
 
     return 0;
  };
Martin
 
Old 11-28-2003, 05:21 PM   #3
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
A small search came up with this code sample for character input from terminal:
http://www.pwilson.net/kbhit.html

It's generally not trivial to read character by character from a terminal since there is a buffer that gets flushed when the user presses return. But if you borrow some code from that page, you get a working program. Like this:
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

#ifndef STDIN_FILENO
  #define STDIN_FILENO 0
#endif

static struct termios termattr, save_termattr;
static int ttysavefd = -1;
static enum 
{ 
  RESET, RAW, CBREAK 
} ttystate = RESET;



int
set_tty_raw(void) 
{
  int i;

  i = tcgetattr (STDIN_FILENO, &termattr);
  if (i < 0) 
  {
    printf("tcgetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
    perror ("");
    return -1;
  }
  save_termattr = termattr;

  termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  termattr.c_cflag &= ~(CSIZE | PARENB);
  termattr.c_cflag |= CS8;
  termattr.c_oflag &= ~(OPOST);
   
  termattr.c_cc[VMIN] = 1;
  termattr.c_cc[VTIME] = 0;

  i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
  if (i < 0) 
  {
    printf("tcsetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
    perror("");
    return -1;
  }
   
  ttystate = RAW;
  ttysavefd = STDIN_FILENO;

  return 0;
}

int
set_tty_cooked() 
{
  int i;
  if (ttystate != CBREAK && ttystate != RAW) 
  {
    return 0;
  }
  i = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr);
  if (i < 0) 
  {
    return -1;
  }
  ttystate = RESET;
  return 0;
}

unsigned char
kb_getc_w(void) 
{
  unsigned char ch;
  size_t size;

  while (1)
  {

    usleep(20000);        /* 1/50th second: thanks, Floyd! */

    size = read (STDIN_FILENO, &ch, 1);
    if (size > 0)
    {
      break;
    }
  }
  return ch;
}


int main(void)
{

  unsigned char a;

  set_tty_raw();

  a = kb_getc_w();

  do
    {
      printf("*");
      fflush(stdout); 
      a = kb_getc_w();
    }
  while(a != '\r');

  set_tty_cooked();
  
  printf("\n");

  return 0;

}
Now, combine 1 and 2 to get a working program.

Good luck with your project!
Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed in security ,vonarabilities ,loopholes in linux security haseebnazar Linux - Security 2 11-23-2005 07:16 PM
[Security Questions] Last Login, how good is this feature for security breach info? t3gah Linux - Security 2 06-14-2005 01:02 AM
todays requirements regarding security (not limited to linux security) markus1982 Linux - Security 8 04-25-2004 10:58 PM
Linux security Vs Windows security keene General 50 11-01-2003 11:22 PM
security opinion-program permissions Robert0380 Linux - Security 1 06-30-2003 06:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration