Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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".

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    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.

  3. #3
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    NH, USA
    man crypt
    in order to use crypt function call, you need to include unistd.h

  4. $spacer_open
  5. #4
    Linux Guru Rubberman's Avatar
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts