Accesing Basic User and Group Information in Linux Using C

There are a set of environment variables consisting of name-value pairs for each program. These variables are often used to control program behavior. We can access the current environment variables using the getenv() function.

In our first program we will use the getenv() function to find out who the current user is.

#include <stdio.h>
#include <stdlib.h>

int main(void){

char *username = getenv(“USER”);

if(username==NULL){
printf(“Could not access USER environment variable.”);
exit(EXIT_FAILURE);
} else {
printf(“User: %s\n”, username);
}

return 0;
}

We can list all users by accessing the /etc/passwd file. The /etc/passwd file is an ASCII file consisting of seven fields separated by semicolons, with one line per user.

The <pwd.h> standard header file defines the functions that read the user database. The setpwnt() and endpwent() functions open and close the password database. Neither of these two functions return a value or accept an argument.

#include <stdio.h>
#include <pwd.h>

int main(void){

//open the password database
setpwent();

//close the password database
endpwent();

return 0;

}

Remember, access to the user database is through the routines declared in pwd.h.

The getpwnam() function retrieves a record from the password file. The function returns a pointer to a passwd structure that is filled in by the function.

#include <stdio.h>
#include <pwd.h>

int main(void){

struct passwd *pwd;
char *name = “aljensen”;

pwd = getpwnam(name);

if(pwd==NULL){
printf(“Could not find user %s.\n”, name);
} else {
printf(“Found user %s.\n”, name);
}

return 0;

}

The passwd structure contains seven fields. This structure is statically allocated and overwritten on each call to the function. The pw_name field contains the username, the pw_uid contains the user ID, and the pw_gid field contains the group ID.

#include <stdio.h>
#include <pwd.h>

int main(void){

struct passwd *pw;

if((pw = getpwnam(“root”))==NULL){
printf(“Problem accessing user information for root.\n”);
}

printf(“User Name = %s\n”, pw->pw_name);
printf(“User ID = %u\n”, pw->pw_uid);
printf(“Group ID = %u\n”, pw->pw_gid);
printf(“Shell = %s\n”, pw->pw_shell);

return 0;

}

The getpwent() function can be used to read through the entire password database, one user at a time. Like the getpwnam() function,it returns a pointer to a passwd structure.

#include <stdio.h>
#include <pwd.h>

int main(void){

struct passwd *ptr;

//rewinds the file
setpwent();

while(1){
//returns NULL when it reaches the end
ptr = getpwent();

if(ptr==NULL){
break;
}

//print out user ID and username
//-5 included to left align 5 column field
printf(“%-5u %s\n”, ptr->pw_uid, ptr->pw_name);

}

//closes the file
endpwent();

return 0;

}

The format of the group database is similar to that of the individual user database, but with four fields. The last field, gr_mem, contains a list of the group’s members. We can iterate through this list using pointer arithmetic.

The group database is typically located at /etc/group. The group database APIs are declared in <grp.h>.

The getgrent() function can be used to list all of the groups in our group database.

#include <stdio.h>
#include <grp.h>

int main(void){

struct group *grp;

int i = 0;

setgrent();

while((grp=getgrent())!=NULL){
printf(“%-5u %s\t”, grp->gr_gid, grp->gr_name);
i=0;
//gr_mem is basically an array of strings
while(*(grp->gr_mem+i)){
printf(“%s “, *(grp->gr_mem+i));
i++;
}
putchar(‘\n’);
}

endgrent();

return 0;

}

Well, that’s enough for today, I think. Please take a look at my Amazon author page http://www.amazon.com/Al-Jensen/e/B008MN382O/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s