[GNU Manual] [POSIX requirement] [Linux man] [FreeBSD man]
Summary
id - print user identity
Lines of code: 441
Principal syscall: None
Support syscalls: None
Options: 17 (8 short, 9 long)
Descended from id in System V (1985)
Added to Shellutils in November 1992 [First version]
Number of revisions: 157 [Code Evolution]
gidtostr_ptr()
- Converts agid_t
to a stringprint_full_info()
- Prints user, group, effective ids, and contextprint_user()
- Prints a user name based on auid_t
match in passwduidtostr_ptr()
- Converts auid_t
to a string
die()
- Exit with mandatory non-zero error and message to stderrerror()
- Outputs error message to standard error with possible process terminationprint_group()
- Prints a group id (from group_list.c
Setup
Some flags and variables for id are declared as globals, including:
egid
- The effective group ideuid
- The effective user idok
- The execution status of the utilityrgid
- The real group idruid
- The real user id
The majority of variables are locals in main():
just_group
- Flag to print only EGIDjust_group_list
- Flag to print all EGIDsjust_user
- Flag to print only EUIDopt_zero
- Flag to use the NUL delimiter rather than whitespaceoptc
- The first character of the next option to processpw_name
- The name returned from the passwd fileselinux_enabled
- Flag if SELinux is enabledsmack_enabled
- Flag is Smack is enableduse_real
- Flag if real IDs should be displayed rahter than effective IDs
Parsing
Parsing user input for id tells us how the user wants information displayed:
- Should we display IDs or names? Real or effective IDs? All, some, or one?
- Should we delimit with NUL or whitespace?
- Should we display the security context?
- Should we look up the current user or a specified user?
That final question drives where we find the information and which syscalls to use during execution.
Parsing has many sanity checks due to the number of ways users can display data.
Parsing failures
Thess failure cases are explicitly checked:
- Using context options on a system without security features enabled
- More than one argument provided
- Trying to display a user and a context at the same time
- Try to print many 'single only' types at once
- Trying to print
- Using a zero delimiter in default format
- Using an unknown option
This failure result in a short error message followed by the usage instructions.
Execution
The most important factor in the id execution path is if a user lookup is specified. The general idea is:
- Get security context, if requested
- If a user is specified, verify from passwd with
getpwuid()
- If a no user is specified, verify the current user information
- Work through each display flag and print data requesting for user
Failure cases:
- User doesn't exist
- Cannot pull any requested data (EUID, RUID, EGID, RGID, etc)
- Cannot find user groups
- Cannot access passwd
- Cannot find process context