Results 1 to 4 of 4
Hi all,
I am a newbie in area of programming applications for Linux using C. The application I am trying to build need to take following inputs from the user.
...
- 09-08-2010 #1Just Joined!
- Join Date
- Aug 2009
- Posts
- 9
Adding a user in linux using C
Hi all,
I am a newbie in area of programming applications for Linux using C. The application I am trying to build need to take following inputs from the user.
1) username
2) password
This application then need to add the user to linux. I need to do this using C code. I have tried the following thing but it didnt work.
Using system("adduser user1"); and system("passwd user1") . This does works, the user is added to the system. But the problem here is that the program waits for an input from user when passwd is executed. The program I am building should not wait for user input as the user has already entered the username and password before execution of these system calls as these should be added on to a file.
May be this whole approach to the problem is wrong. Using the above method I am able to add a user but could not set the password. Another thing that I found out was the -p switch in adduser command. Which needs a crypted passwd. For some reason I am not able to add crypt() to my code. Other wise the next thing I would have done was
system("adduser user1 -p XXXXXX")
where XXXXXX was a cryped passwd.
Please help any examples or webpages or code is welcome.
Is there another way of doing this. Coz I know making system calls to add users and password does not sounds like good programming.
Ankush Pandit
PS - This application will be running as "root".
- 09-08-2010 #2
Well, the useradd program is to be found on virtually any GNU/Linux system.
Just get the source code and study it.Debian GNU/Linux -- You know you want it.
- 09-08-2010 #3in order to use crypt function call, you need to include unistd.hCode:
man crypt
- 09-08-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You can open a pipe in your program, dup2() that to your stdin file stream, write the password data to the pipe, and then fork/exec to the adduser program and it will read from the pipe to take the appropriate data. Non-intuitive, I know, but it does work. See if you can work it out. If not, I'll provide some more "hints".
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote