Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103389 > unrolled thread

Masking the password entered by the user

Started byAlla _ <modelling.data@gmail.com>
First post2017-02-07 09:19 -0800
Last post2017-02-07 12:21 -0800
Articles 8 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Masking the password entered by the user Alla _ <modelling.data@gmail.com> - 2017-02-07 09:19 -0800
    Re: Masking the password entered by the user Keith Thompson <kst-u@mib.org> - 2017-02-07 09:34 -0800
    Re: Masking the password entered by the user "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-07 12:54 -0500
      Re: Masking the password entered by the user Alla _ <modelling.data@gmail.com> - 2017-02-07 10:21 -0800
        Re: Masking the password entered by the user Keith Thompson <kst-u@mib.org> - 2017-02-07 12:16 -0800
    Re: Masking the password entered by the user luser droog <luser.droog@gmail.com> - 2017-02-07 11:10 -0800
    Re: Masking the password entered by the user Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 19:39 +0000
      Re: Masking the password entered by the user Alla _ <modelling.data@gmail.com> - 2017-02-07 12:21 -0800

#103389 — Masking the password entered by the user

FromAlla _ <modelling.data@gmail.com>
Date2017-02-07 09:19 -0800
SubjectMasking the password entered by the user
Message-ID<6fb9a4db-5cba-479c-8ee6-82b7146a2e38@googlegroups.com>
Hello!

One of the functions I have gets a password from the 
user. I would like to hide characters as they are entered
by the user. I have found some pieces of code on some
sites, but I can't say I trust those options, let alone I don't 
understand them )
Is there a good, not too cryptic, way to do that?
Here is my function that asks for a password (pretty
basic one):

char *user_pass()
{
    char *password = NULL;
    if(!(password = buff_alloc()))
        return NULL;
    int c;
    int i = 0;
    
    while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
        password[i++] = c;
    if (c == '\n')
        password[i] = '\0';
    
    return password;
}

Thank you!

[toc] | [next] | [standalone]


#103394

FromKeith Thompson <kst-u@mib.org>
Date2017-02-07 09:34 -0800
Message-ID<lnk29129cv.fsf@kst-u.example.com>
In reply to#103389
Alla _ <modelling.data@gmail.com> writes:
> One of the functions I have gets a password from the 
> user. I would like to hide characters as they are entered
> by the user.
[snip]

There is no portable way to do this.

See question 19.1 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

The discussion there is a bit out of date.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103396

From"James R. Kuyper" <jameskuyper@verizon.net>
Date2017-02-07 12:54 -0500
Message-ID<a19ca74e-265a-b58f-44aa-46c313f3571c@verizon.net>
In reply to#103389
On 02/07/2017 12:19 PM, Alla _ wrote:
> Hello!
>
> One of the functions I have gets a password from the
> user. I would like to hide characters as they are entered
> by the user. I have found some pieces of code on some
> sites, but I can't say I trust those options, let alone I don't
> understand them )
> Is there a good, not too cryptic, way to do that?
> Here is my function that asks for a password (pretty
> basic one):
>
> char *user_pass()
> {
>     char *password = NULL;
>     if(!(password = buff_alloc()))
>         return NULL;
>     int c;
>     int i = 0;
>
>     while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
>         password[i++] = c;
>     if (c == '\n')
>         password[i] = '\0';
>
>     return password;
> }
>
> Thank you!

Hiding characters as they are entered is not a capability that is 
provided in any portable fashion by C's I/O routines. On some systems 
where stdin and stdout are both unbuffered, and refer to the same 
device, collecting a character at a time using getchar(), and calling 
fputs("\b*", stdout) immediately afterward might cause that character to 
be replaced by an asterisk - if the "\b*" gets printed before the user 
types his next letter. Keeping those things synced that way is something 
that can be arranged with certainty on some systems. However, the code 
you need to write to achieve this result on such systems is not 
guaranteed to have the same effect on any other system.

There are ways of doing this using C code to call various routines, but 
which routines you need to call, and the arguments you need to pass 
them, varies from one system to another. If you want to find out how, 
you'll need to identify which kind of system you're using. And someone 
else will need to answer you, because I don't have a lot of recent 
experience with that kind of thing. Since 1996, most of the code I've 
written has been for a batch processing environment. Before then, I did 
write code which did such things on web pages and in a GUI running on a 
VMS machine; neither of which seems likely to be relevant to your 
application (even if I could remember the details of how I did it).

[toc] | [prev] | [next] | [standalone]


#103403

FromAlla _ <modelling.data@gmail.com>
Date2017-02-07 10:21 -0800
Message-ID<f32d1254-7dcc-4044-93b4-64e77a06a048@googlegroups.com>
In reply to#103396
On Tuesday, February 7, 2017 at 8:54:53 PM UTC+3, James R. Kuyper wrote:
> On 02/07/2017 12:19 PM, Alla _ wrote:
> > Hello!
> >
> > One of the functions I have gets a password from the
> > user. I would like to hide characters as they are entered
> > by the user. I have found some pieces of code on some
> > sites, but I can't say I trust those options, let alone I don't
> > understand them )
> > Is there a good, not too cryptic, way to do that?
> > Here is my function that asks for a password (pretty
> > basic one):
> >
> > char *user_pass()
> > {
> >     char *password = NULL;
> >     if(!(password = buff_alloc()))
> >         return NULL;
> >     int c;
> >     int i = 0;
> >
> >     while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
> >         password[i++] = c;
> >     if (c == '\n')
> >         password[i] = '\0';
> >
> >     return password;
> > }
> >
> > Thank you!
> 
> Hiding characters as they are entered is not a capability that is 
> provided in any portable fashion by C's I/O routines. On some systems 
> where stdin and stdout are both unbuffered, and refer to the same 
> device, collecting a character at a time using getchar(), and calling 
> fputs("\b*", stdout) immediately afterward might cause that character to 
> be replaced by an asterisk - if the "\b*" gets printed before the user 
> types his next letter. Keeping those things synced that way is something 
> that can be arranged with certainty on some systems. However, the code 
> you need to write to achieve this result on such systems is not 
> guaranteed to have the same effect on any other system.
> 
> There are ways of doing this using C code to call various routines, but 
> which routines you need to call, and the arguments you need to pass 
> them, varies from one system to another. If you want to find out how, 
> you'll need to identify which kind of system you're using. And someone 
> else will need to answer you, because I don't have a lot of recent 
> experience with that kind of thing. Since 1996, most of the code I've 
> written has been for a batch processing environment. Before then, I did 
> write code which did such things on web pages and in a GUI running on a 
> VMS machine; neither of which seems likely to be relevant to your 
> application (even if I could remember the details of how I did it).

I see. Then I just leave it as it is, keeping in mind that I have to constantly
change my root password, as the one I have now I have used numerous
times and it's there on the Terminal. 
Completely offtopic: I have read that it is not possible to delete Terminal
history; I wonder if closing the window using Cmd W removes the history - 
at least after this action I can't scroll upwards to see it.  

[toc] | [prev] | [next] | [standalone]


#103415

FromKeith Thompson <kst-u@mib.org>
Date2017-02-07 12:16 -0800
Message-ID<lnfujp21u9.fsf@kst-u.example.com>
In reply to#103403
Alla _ <modelling.data@gmail.com> writes:
[...]
> I see. Then I just leave it as it is, keeping in mind that I have to constantly
> change my root password, as the one I have now I have used numerous
> times and it's there on the Terminal. 

Not really a C issue, but I personally would avoid anything that leaves
a password, particularly a root password, on the screen in plain text.

If your application needs to ask for a password, I suggest finding a way
to do it using POSIX features.  Some of the methods suggested in FAQ
19.1 are probably POSIX-compliant.  The most straightforward approach
is probably something like:

    fputs("Enter password: ", stdout);
    fflush(stdout);
    system("stty -echo");
    fgets(password, sizeof password, stdin);
    system("stty echo");
    /* deal with '\n' */

It might be better to do something a bit lower level.
comp.unix.programmer would be a good place to ask (check for a FAQ
list first, this kind of thing would likely be covered).

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103411

Fromluser droog <luser.droog@gmail.com>
Date2017-02-07 11:10 -0800
Message-ID<d6654417-a1e9-4d40-b994-210ad04296f3@googlegroups.com>
In reply to#103389
On Tuesday, February 7, 2017 at 11:19:51 AM UTC-6, Alla _ wrote:
> Hello!
> 
> One of the functions I have gets a password from the 
> user. I would like to hide characters as they are entered
> by the user. I have found some pieces of code on some
> sites, but I can't say I trust those options, let alone I don't 
> understand them )
> Is there a good, not too cryptic, way to do that?
> Here is my function that asks for a password (pretty
> basic one):
> 
> char *user_pass()
> {
>     char *password = NULL;
>     if(!(password = buff_alloc()))
>         return NULL;
>     int c;
>     int i = 0;
>     
>     while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
>         password[i++] = c;
>     if (c == '\n')
>         password[i] = '\0';
>     
>     return password;
> }
> 
> Thank you!

It really depends on the type of terminal and system.
In a unix-y system there's usually a `clear` command
that will erase whatever's on the first screenfull at
the bottom of the scrollback buffer. On windows cmd
windows, it's called `cls`.

If you're on a unix terminal, you can also turn
echoing on and off with

  system("stty -echo");
  //... getchar(); ...
  system("stty echo");

[toc] | [prev] | [next] | [standalone]


#103413

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-02-07 19:39 +0000
Message-ID<87zihxaize.fsf@bsb.me.uk>
In reply to#103389
Alla _ <modelling.data@gmail.com> writes:

> char *user_pass()
> {
>     char *password = NULL;
>     if(!(password = buff_alloc()))

I'd initialise with the correct value.  Don't make setting things to
zero or NULL a knee-jerk reaction!

And I prefer to be positive:

  char *password = buff_alloc();
  if (password) {
     ... read in the characters ...
  }
  return password;

but there are two sides to this.

>         return NULL;
>     int c;
>     int i = 0;
>     
>     while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
>         password[i++] = c;

Buffer overflow.

>     if (c == '\n')
>         password[i] = '\0';

You always want to return a string so there should be nothing special
about \n.

>     return password;
> }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#103417

FromAlla _ <modelling.data@gmail.com>
Date2017-02-07 12:21 -0800
Message-ID<1120e192-7895-4024-8cf5-f0bd05004511@googlegroups.com>
In reply to#103413
On Tuesday, February 7, 2017 at 10:39:23 PM UTC+3, Ben Bacarisse wrote:
> Alla _ <modelling.data@gmail.com> writes:
> 
> > char *user_pass()
> > {
> >     char *password = NULL;
> >     if(!(password = buff_alloc()))
> 
> I'd initialise with the correct value.  Don't make setting things to
> zero or NULL a knee-jerk reaction!
> 
> And I prefer to be positive:
> 
>   char *password = buff_alloc();
>   if (password) {
>      ... read in the characters ...
>   }
>   return password;
> 
> but there are two sides to this.
> 
> >         return NULL;
> >     int c;
> >     int i = 0;
> >     
> >     while( (c = getchar()) != EOF && (c != '\n' && c != '\r'))
> >         password[i++] = c;
> 
> Buffer overflow.
Yes, indeed, I have missed the check for i, although there is 
a caveat to the user in the calling function about the limited 
amount of characters for password - of course, this cannot 
be used as any guarantee and the check should be anyway. 

I remember about the positive attitude ) It's the one I like 
to follow too. Sometimes I omit it if I see that the test for
a negative outcome makes the code more succinct.
> 
> >     if (c == '\n')
> >         password[i] = '\0';
> 
> You always want to return a string so there should be nothing special
> about \n.
> 
> >     return password;
> > }
> 
> -- 
> Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web