Write a program in C to mimic the “adduser” command on Linux. This command will add either an ordinary user or a system user. It has to handle 2 files “passwd” and “shadow”. Both these files will be in some folder specified by an environment variable PFILE. The program has to take all arguments as command line arguments (Refer man pages for the command line arguments).
When we run „adduser„ command in Linux terminal, it performs following major things:
1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created
User account.
2. Creates and populate a home directory for the new user.
3. Sets permissions and ownerships to home directory.
The options which should be implemented are:
adduser –u UID –g GID –c “Custom comments” name
UID – User ID (An integer value)
GID – Group ID (An integer value)
Custom comments – A string describing the user
name – Name of the user being added
This command modifies the files: passwd, shadow, group and gshadow. You need to have
these files in a directory defined through an environment variable PFILE.
The /etc/passwd contains one entry per line for each user (user account) of the system. All
fields are separated by a colon (:) symbol. Total of seven fields as follows. Generally,
/etc/passwd file entry looks as follows:
/etc/passwd Format
From the above image:
1. Username: It is used when user logs in. It should be between 1 and 32 characters in
length.
2. Password: An x character indicates that encrypted password is stored in /etc/shadow file.
3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for
root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are
reserved by system for administrative and system accounts/groups.
4. Group ID (GID): The primary group ID (stored in /etc/group file)
5. User ID Info: The comment field. It allows you to add extra information about the users
such as user‟s full name, phone number etc.
6. Home directory: The absolute path to the directory the user will be in when they log in. If
this directory does not exist, then users directory becomes /
7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a
shell. Please note that it does not have to be a shell.
The students can hardcode the last two fields as follows:
Home directory - /user/home
Command Shell - /bin/bash
The password field should not be filled with anything.
They have to make sure that the name and the UID are unique for all users.
Example:
Suppose we execute the program as:
adduser –u 100 –g 200 –c “M S Anand” anandms
A line like this will get added (appended) to the passwd file:
What I have tried:
I have done the basic research about the command line arguments