Results 1 to 4 of 4
Thread: Adding a user in linux using C
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Aug 2009
Adding a user in linux using C
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.
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.
PS - This application will be running as "root".
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.
- Join Date
- Apr 2009
- I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
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!