Operating System - HP-UX
1751765 Members
5026 Online
108781 Solutions
New Discussion юеВ

Changing user's password programmatically

 
SOLVED
Go to solution
Ahmed Atef
Occasional Advisor

Changing user's password programmatically

Hi
I'm trying to write small C code to change the user's password automatically using getpwent(3C), crypt(3C) and then putpwent(3C) ... actually "Clay" has guided me to use these function calls sometime back..

now i have a question, The "salt" value in crypt function "second argument", on which base i choose this value?

Thanks
Ahmed
11 REPLIES 11
Herve BRANGIER
Respected Contributor
Solution

Re: Changing user's password programmatically

Hi

The salt is a two caracters word which is a
crypt argument. You can use random to
calcultate each letter.

This salt is use to be a key for crypt function
After crypt a password you can see that salt
is on the two first signs of the passwd 13
caracters.

HTH

Herv?

Ahmed Atef
Occasional Advisor

Re: Changing user's password programmatically

Hi Herv?

So it is up to me to choose the "salt" value, great...
i have tried this and i got the 13 characters output, which starts with "My_2_Salt_Characters".. then i replaced the password entry for one account with the new password... when i tried to login i had login incorrect.
Is there anything missing of the procedure i described: -
1- getpwnam
2- crypt
3- putpwent

thanks,
Ahmed
Herve BRANGIER
Respected Contributor

Re: Changing user's password programmatically

Ahmed,

I try that :

1) getpwnam
2) crypt new passwd with a salt
3) putpwent
4) try to login : works well !!!

Look at this small piece of code :

---- CUT HERE -----

#include
#include

main ()
{
struct passwd * my_pwstr;
char * new_pwd;
/* You need to open you passwd file for update */
FILE *pwfile = fopen ("/etc/passwd","r+");

my_pwstr = getpwnam ("cg");

printf ("\nEncrypted passwd : %s\n",my_pwstr ->pw_passwd);

new_pwd = crypt ("azerty","q2"); /* new pwd is "azerty" and salt is "q2" */

printf ("New encrypted passwd : %s\n",new_pwd);

strcpy (my_pwstr->pw_passwd, new_pwd);

putpwent (my_pwstr,pwfile);

fclose (pwfile);
}

----- CUT HERE ------


I made no control on file opening and other error
but it works well. You just need to add a
function to calculate your salt.

Good luck

Herv?

Herve BRANGIER
Respected Contributor

Re: Changing user's password programmatically


Ahmed

I just see something : Clay and, now, I help
you... don't forget to assign points to our
reply if we help you.

An information : I don't know where you get new
password. Don't forget that you can't take
passwd field gave by getpwnam : it is encrypted
You need to use a new one not encrypted.

Herv?

Ahmed Atef
Occasional Advisor

Re: Changing user's password programmatically

Definitly i will assign points once i solve this problem, thanks for the reminder :-)

my code is almost typical of what you did the difference is that i'm getting the username and the new password from the command line.

it is not only the problem that i can't login but the user home directory got corrupted after i apply the crypt call this in case the old password is NULL "means that no characters between the two':' in /etc/passwd!!!

i'm attaching my code as well...
Ahmed Atef
Occasional Advisor

Re: Changing user's password programmatically

Sorry ... here is the attachmenet

Thanks, again
Ahmed
Ahmed Atef
Occasional Advisor

Re: Changing user's password programmatically

Ok Ok .. sorry for this silly mistake .. argv[3] should be argv[2] :-) ... i'm sorry again for this overlooked mistake .. but still it doesn't work if i have previously null password ....
Ahmed Atef
Occasional Advisor

Re: Changing user's password programmatically

Okay i solved it ...
When the password has less than 13 characters value the amount of memory allocated will be less than 13 characters so unpredicted results will occure,
To solve this we need to malloc or realloc 13 characters of memory and return the pointer to pw_passwd..

It works fine now...
Thanks and i will assign the points right now..

Ahmed
Herve BRANGIER
Respected Contributor

Re: Changing user's password programmatically

Ahmed,

It seems to work well now but have a look to
your passwd file : when you use putpwent you
add a new line on passwd file.
The problem is that if you have two lines the
first is read and not the others and it seems
that the first is the old one.
I made modification in your program (using grep
in the system call) to delete all "user" lines
when copying /etc/passwd to /tmp/passwd.

I join the file...

Bye

Herv?