C language Getpw () function: Gets the password file data of the specified user
header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
int GETPW (uid_t uid, char *buf);
Function Description: GETPW () will find the user account data specified in the parameter UID from/ETC/PASSWD, and return 1 if the relevant data is not found.
The returned BUF string is formatted as follows:
Account: Password: User ID (UID): Group identification Code (GID): Full name: root: Shell
Return value: A return of 0 indicates success and returns-1 if an error occurs.
Additional Instructions
1. GETPW () may have potential security problems, please try to replace it with a different function.
2. The system using shadow has extracted the user's password/etc/passwd, so the password obtained using GETPW () will be "X".
Example
#include <pwd.h>
#include <sys/types.h>
main ()
{
char buffer[80];
GETPW (0, buffer);
printf ("%s\n", buffer);
}
Perform:
Root:x:0:0:root:/root:/bin/bash
C language Getpwnam () function: Obtain the specified account data from the password file
header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
struct passwd * Getpwnam (const char * name);
Function Description: Getpwnam () is used to search the account name specified by the parameter name, and the user's data is returned in passwd structure when found. PASSWD structure please refer to getpwent ().
Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.
Example
/* Get the ID and root directory of root account */
#include <pwd.h>
#include <sys/types.h>
main ()
{
struct passwd *user;
user = Getpwnam ("root");
printf ("name:%s\n", user->pw_name);
printf ("uid:%d\n", user->pw_uid);
printf ("home:%s\n", User->pw_dir);
}
Perform:
Name:root
uid:0
home:/root
C language Getpwuid () function: Get the specified UID data from the password file
header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
struct passwd * GETPWUID (uid_t uid);
Function Description: Getpwuid () is used to search the parameter uid to specify the user identification code, when found, the user's data to structure return structure please refer to the user's data passwd structure return. PASSWD structure please refer to getpwent ().
Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.
Example
#include <pwd.h>
#include <sys/types.h>
main ()
{
struct passwd *user;
User= Getpwuid (6);
printf ("name:%s\n", user->pw_name);
printf ("uid:%d\n", user->pw_uid);
printf ("home:%s\n", User->pw_dir);
}
Perform:
Name:shutdown
uid:6
home:/sbin