changing passwd from C

changing passwd from C

Post by Marc Unang » Wed, 13 May 1992 07:33:39




>What about doing a popen() to "passwd >/dev/null 2>&1", and write the necessary
>passwords directly - wouldn't that do the job ??

No.  /bin/passwd reads the password directly from /dev/tty in most
versions of Unix, to prevent it being spoofed by another program.
Some versions of /bin/passwd will read from stdin only if they are run
as root; others don't care who you are, and always read from /dev/tty.
This is why you need to use ptys.

Changing the user's password is not at all difficult to do directly
from a C program; it's just a bit system-specific, since you need to
know things like whether /etc/shadow is being used and so forth.  I
would suggest that this discussion does not really belong in
comp.unix.wizards, and therefore have redirected followups to
comp.unix.programmer.  Please continue the discussion there.

--
Marc Unangst                | You know that funny-looking bump on your

<backbone>!sharkey!mudos!mju|

 
 
 

changing passwd from C

Post by John J. Rushfo » Thu, 14 May 1992 08:11:57




>>What about doing a popen() to "passwd >/dev/null 2>&1", and write the necessary
>>passwords directly - wouldn't that do the job ??

I'd use getpwent(3C) to read the password file and get the old password and
the other fields necessary to re-write the entry to the password file.  Use
crypt(3C) to encrypt the new password before writing it to the password file.
Of course your program would have to be owned by root with setuid.

John

 
 
 

changing passwd from C

Post by Domenico De Vit » Thu, 14 May 1992 19:42:57


How about using TIOCSTI (in termio.h) to insert the chars on the terminal
stream for input ?

This works (I think) but how portable is this mechenism ?

Dom

 
 
 

changing passwd from C

Post by Jonathan I. Kame » Fri, 15 May 1992 14:02:25



   How about using TIOCSTI (in termio.h) to insert the chars on the terminal
   stream for input ?

   This works (I think) but how portable is this mechenism ?

1) No, it doesn't work, because the getpass(3) routine, and hence the
"passwd" program, flushes all pending input right before reading the
password.  Any characters stuffed into the buffer are flushed once
"passwd" starts.

*Maybe* you could fork and exec "passwd" in the child and do TIOCSTI
in the parent after waiting enough time for "passwd" to start up, but
that's hideous.  Using pty's or modifying the passwd file directly
would be much more reasonable.

As someone has already pointed out once, it's usually a good idea to
TRY a suggestion before posting it to the entire world.

2) It's "mechanism".

--

MIT Information Systems/Athena              Moderator, news.answers
    (Send correspondence related to the news.answers newsgroup
        {and ONLY correspondence related to the newsgroup}

 
 
 

changing passwd from C

Post by Randy Crawfo » Sun, 17 May 1992 02:43:15





> >>What about doing a popen() to "passwd >/dev/null 2>&1", and write the necessary
> >>passwords directly - wouldn't that do the job ??

> I'd use getpwent(3C) to read the password file and get the old password and
> the other fields necessary to re-write the entry to the password file.  Use
> crypt(3C) to encrypt the new password before writing it to the password file.
> Of course your program would have to be owned by root with setuid.

> John

This discussion of password encryption leads me to ask a related (and possibly
redundant) question.  When using crypt to generate an encrypted password to be
compared with the entry in passwd, what is the 2 char string which is needed to
salt the key?

All I need to do at the moment is verify a password string inside an X application.
I'm guessing the 2 arguments to crypt() are the unencrypted password followed
by _what_?

Or is there a simpler (documented) way?
--


|
| N=1 -> P=NP           703 883-7940

 
 
 

changing passwd from C

Post by Barry Margol » Sun, 17 May 1992 05:38:22



>This discussion of password encryption leads me to ask a related (and possibly
>redundant) question.  When using crypt to generate an encrypted password to be
>compared with the entry in passwd, what is the 2 char string which is needed to
>salt the key?

If you're generating a new password, the salt is any two characters chosen
from the set [a-zA-z0-9./].

If you're validating a password, the salt should be the first two characters
from the encrypted password you want to compare against.  You then compare
the returned value against the entire already-encrypted password.

Actually, I think for maximum portability[*] you should pass the entire
encrypted password as the salt when validating.  On Sun systems using
password.adjunct (Sun's shadow password mechanism), the password field of a
passwd entry is "##username".  When crypt(3) sees that the first two
characters are "##" it performs the validation for you (by calling
pwdauth(3), which uses the salt as a key in the passwd.adjunct file) and
returns the salt argument itself if the comparison succeeded (a similar
thing is done with passwords of the form "#$groupname" and the
group.adjunct file).  If the salt string doesn't begin with "#" then
everything after the first two characters will be ignored.  In general, the
following should always work to validate a password;

    int passwd_ok = 0; char *entered_passwd, *encrypted_passwd;
    entered_passwd = getpass("Password:");
    encrypted_passwd = crypt(entered_passwd, passwd.pw_passwd);
    if (encrypted_passwd != NULL)
        passwd_ok = (strcmp(passwd.pw_passwd, encrypted_passwd) == 0);

[*] I supposed it's possible that this technique isn't really upward
compatible on all systems, so you might need to use conditional compilation
for *really* maximum portability.
--
Barry Margolin
System Manager, Thinking Machines Corp.


 
 
 

changing passwd from C

Post by John J. Rushfo » Mon, 18 May 1992 05:53:31






>> >>What about doing a popen() to "passwd >/dev/null 2>&1", and write the necessary
>> >>passwords directly - wouldn't that do the job ??

>> I'd use getpwent(3C) to read the password file and get the old password and
>> the other fields necessary to re-write the entry to the password file.  Use
>> crypt(3C) to encrypt the new password before writing it to the password file.
>> Of course your program would have to be owned by root with setuid.

>> John

>This discussion of password encryption leads me to ask a related (and possibly
>redundant) question.  When using crypt to generate an encrypted password to be
>compared with the entry in passwd, what is the 2 char string which is needed to
>salt the key?

>All I need to do at the moment is verify a password string inside an X application.
>I'm guessing the 2 arguments to crypt() are the unencrypted password followed
>by _what_?

>Or is there a simpler (documented) way?
>--


>|
>| N=1 -> P=NP           703 883-7940

The first 2 characters in the password field of /etc/passwd are the salt
that was used by crypt() to encrypt the original password.  So to encrypt
a user supplied password for comparison to the password in /etc/passwd,
use the 2 salt characters obtained from the password field.  If the user
supplied password is correct, crypt() will yield a string identical to
that in the password field.

John

 
 
 

1. passwd (NIS): Couldn't change passwd/attributes for joeblogg

yppasswd joeblogg
New password:
Re-enter new password:
passwd (NIS): Couldn't change passwd/attributes for joeblogg
Permission denied

now this machine is solaris 8 and has been set-up as a nis server
unfortunately my memory isn't serving me well and I can't think
what can be causing this.

machine is looking at "files nis" for the passwd in /etc/nsswitch.conf and
/etc/nsswitch.nis

any clues?

2. OT: Passing Unix Variables to a PL/SQL Stored Procedure

3. How passwd changes passwd

4. rlogin fails : can't resolve symbol '__xstat'

5. Can't change passwords with passwd

6. VANCOUVER LINUX USER'S GROUP

7. Changing passwd via WWW-interface

8. Kernel does not compile with RH 5.1

9. changing passwd default lenght 6 and one number

10. remote passwd change

11. NIS+ yp compatibility passwd change

12. 1 line passwd change? anyone?

13. passwd change error