Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 12-May-20 19:39pm
Comments
Richard MacCutchan 13-May-20 3:41am    
We are happy to help with problems in the code that you write. But it is your assignment so you are expected to do the actual work.

1 solution

For Windows you might use the NetUserAdd function as described here: NetUserAdd function (lmaccess.h) - Win32 apps | Microsoft Docs[^]
 
Share this answer
 
Comments
phil.o 13-May-20 3:43am    
Because it is your assignment, you have to put a minimal work into it.
You should begin by drawing a diagram of required steps to satisfy the requirements. Once done, translate these steps into a C program.
You will only learn how to copy/paste if we do your assignment for you. For now, your question rather looks like a "please do my homework" type of question. On the other hand, if you put some minimal effort into your work, and come up with a concrete question about some code you wrote, we will be glad to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900