Masking Password with *'s


 
Thread Tools Search this Thread
Top Forums Programming Masking Password with *'s
# 15  
Old 12-17-2010
Quote:
No. By block mysteriously, I mean, block on more than one character even when vmin=1. I wasn't able to get "proper" behavior as you describe it, and don't want to give someone "compliant" yet broken code.
The proper behavior is to wait for one character. I have used this construct (~ICANON|ECHO, VMIN=1, VTIME=0) on tons of systems (Linux, *BSD, OSX, AIX, Tru64 and Digital UNIX, IRIX, HP-UX, UnixWare) as well as Cygwin and it always worked as expected. It is standard, documented and it works.

Did you try the exact code Jim posted with the only change being VMIN=1?

Blocking "mysteriously" in noncanonical mode after tcsetattr() usually happens if you DON'T set VMIN, in which case some systems such as IRIX and (I think) Solaris default to VMIN=4, whereas Linux and BSD default to 1. But then they only block until you entered 4 chars.

I labeled the select() solution a kludge because it seemed an unnecessarily contrived solution if you're setting VMIN to 1, since the read operation will block until 1 char has been read and no timeout was indended.
# 16  
Old 12-19-2010
Okay so now I'm almost thinking that the reason your code didn't work when I tried it was because I think I might have compiled the wrong file. Because as i try to compile it now I'm getting errors and I didn't even change anything. It's giving me this error message:
Code:
test2.c: In function ākbd_rdā:
test2.c:28: error: āfd_setā undeclared (first use in this function)
test2.c:28: error: (Each undeclared identifier is reported only once
test2.c:28: error: for each function it appears in.)
test2.c:28: error: expected ā;ā before āset_readā
test2.c:29: warning: ISO C90 forbids mixed declarations and code
test2.c:32: error: variable ātv_tmoutā has initializer but incomplete type
test2.c:32: warning: excess elements in struct initializer
test2.c:32: warning: (near initialization for ātv_tmoutā)
test2.c:32: warning: excess elements in struct initializer
test2.c:32: warning: (near initialization for ātv_tmoutā)
test2.c:32: error: storage size of ātv_tmoutā isnāt known
test2.c:34: warning: implicit declaration of function āFD_ZEROā
test2.c:34: error: āset_readā undeclared (first use in this function)
test2.c:35: warning: implicit declaration of function āFD_SETā
test2.c:36: warning: implicit declaration of function āselectā
test2.c:37: warning: implicit declaration of function āFD_ISSETā
test2.c:32: warning: unused variable ātv_tmoutā
test2.c: In function āmainā:
test2.c:50: warning: implicit declaration of function āfilenoā

Some of these confuse me because it seems that it is saying that certain things are undeclared right where they are being declared, if that makes sense. Thanks for the help by the way guys, I really appreciate it. It seems like a lot of this is over my head as of right now. I'll need more than one quarter of class to fully understand what's going on.
# 17  
Old 12-19-2010
The code is missing one or more includes.

If you're on a newish system, try adding "#include <sys/select.h>". Otherwise, try "#include <sys/types.h>" and "#include <unistd.h>".

Jim's code looks generally acceptable, except that it does not do any error checking, and I'd rather set c_cc[VMIN] = 1 and not use select().

If Jim's code doesn't work for you, try the following much simplified version. Yes, you heard right, I'd even go as far as using stdio on a noncanonical terminal device. I have no indications that it is prohibited by UNIX to do this, nor have I encountered any real world systems which do not allow it. (What did break stdio for me was nonblocking file descriptors on HP-UX, although I do not remember details)

Code:
#include <termios.h>
#include <stdio.h>

static struct termios termold;

void
echo_off(void) {
        struct termios t;
        (void) tcgetattr(0, &termold);
        t = termold;
        t.c_lflag &= ~(ICANON | ECHO);
        t.c_cc[VMIN] = 1;
        t.c_cc[VTIME] = 0;
        (void) tcsetattr(0, TCSANOW, &t);
}

void
echo_on(void) {
        (void) tcsetattr(0, TCSANOW, &termold);
}

void
read_unbuffered(void) {
        int ch;
        setbuf(stdin, NULL);
        echo_off();
        while ((ch = getchar()) != '\n') {
             putchar('*');
        }
        echo_on();
}

int
main() {
        read_unbuffered();
}

# 18  
Old 12-19-2010
Okay so I tried including the headers you said and the first one worked, except it had issues with the reference to "fileno". However, I tried your simplified code and it seems to work... kinda. I could be wrong but is this code reading the password into an array at all? Or is it just covering it up with *'s?
# 19  
Old 12-19-2010
Right, it just shows how to read the data. You'd have to store and evaluate it yourself. You will probably also want to handle backspace (\b) and print "\b \b" to erase that last character from the display.
# 20  
Old 12-19-2010
Oh okay so print("\b \b") will print a backspace? Awesome. Also, what is delete as opposed to backspace? Like backspace is \b. What is delete?

I think that basically is what I needed then. One last thing, so if I wanted to print a colored number then would I just put that where * is in the code? Because I tried that and it said something like it was too big or something. Should I use printf() instead of putchar() in that case? I can look up the code and error message exactly if that doesn't make sense. Sorry, this is off of my iphone right now.
# 21  
Old 12-19-2010
Quote:
Originally Posted by bigdrock44
Oh okay so print("\b \b") will print a backspace? Awesome.
That's two backspaces, with a space between them.
Quote:
Also, what is delete as opposed to backspace? Like backspace is \b. What is delete?
I don't think you can print a delete and get anything sensible.
Quote:
One last thing, so if I wanted to print a colored number then would I just put that where * is in the code? Because I tried that and it said something like it was too big or something.
I can't see your computer from here, please post your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Masking with gsub command

My file "test.dat" data as below Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line. my command awk '{gsub("]","X")}1' test.dat looks not working properly, Appreciate any suggestion... (6 Replies)
Discussion started by: JSKOBS
6 Replies

2. UNIX for Dummies Questions & Answers

Masking data

How Can I mask one particular columns using some unix command? (4 Replies)
Discussion started by: dsa
4 Replies

3. Shell Programming and Scripting

Masking algorithm

I have a requirement of masking few specific fields in the UNIX file. The details are as following- File is fixed length file with each record of 250 charater length. 2 fields needs to be masked – the positions are 21:30 and 110:120 The character by character making needs to be done which... (5 Replies)
Discussion started by: n78298
5 Replies

4. Shell Programming and Scripting

Masking Password from within a Bash Shell Script

Is there a way to mask the password inside of a script to minimize the impact of a comprimised server? So ssh -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.3.1 "mysqldump --opt --all-databases -u root -pPassword| gzip" > $backup_dir/mysqldump.gz a... (2 Replies)
Discussion started by: metallica1973
2 Replies

5. Shell Programming and Scripting

masking issue

Hi I am facing an issue with the below script which has the below line each field being separated with a tab. I need to mask the 8 and 7th field based on following conditions 1. 8th field is 16 in length and is numerics i will mask the middle 6 digits except the first 6 and last 4. input... (2 Replies)
Discussion started by: mad_man12
2 Replies

6. Shell Programming and Scripting

Scripting help/advise on hiding/masking username/password

Hi, I currently have a UNIX script with a function that uses a username and password to connect to the database, retrieve some information and then exit. At the moment, am getting the username and password from a hidden plain text file and permission set to -r--------, i.e. read only to who... (1 Reply)
Discussion started by: newbie_01
1 Replies

7. Emergency UNIX and Linux Support

Masking of number

BAT:0310:2009-08-0:Y4 :H:D:00003721:03103721.IFH:00138770:05767:00000000001279' EXR:CLP:912.570000' STA:A:9071559:2009-08-10::Wer::Mrs' DEF::531.97:531.97:310221661617::+ABC:BAL:1:N::::5:40.00:0.00:2009-08-10:CN:1111111111109962::3:N:missc :N:PH:00010833:... (5 Replies)
Discussion started by: mad_man12
5 Replies

8. Shell Programming and Scripting

Data Masking

I have a pipe delimited file that I need to 'mask' to before loading to keep some data confidential. I need to maintain the first 4 bytes of certain columns and replace the remaining bytes with an 'x'. I would like to maintain spaces but it's not a requirement. Example, need to mask columns 2... (2 Replies)
Discussion started by: 1superdork
2 Replies

9. Shell Programming and Scripting

Masking Content of a String

Hello, I need to know that whether a content of a string can be hidden or masked inside a shell script. My Sample Code is given below <Code> #!/usr/bin/ksh Userid=test DB=temp Passwd=`java Decryption test` # The Above command will get the encryped password for "test" user id and store... (2 Replies)
Discussion started by: maxmave
2 Replies

10. IP Networking

IP Masking

Is it possible for a internal LAN to mask a IP e.g. i have a server ip running the intranet ip being 192.168.0.8 and i want to make that like www.intranet.com is this possible on a internal network ? (1 Reply)
Discussion started by: perleo
1 Replies
Login or Register to Ask a Question