Querying the process credentials

How do you programmatically (in a C program) query the real and effective UIDs /GIDs? Here are the system calls to do so:

#include <unistd.h>
#include <sys/types.h>

uid_t getuid(void);
uid_t geteuid(void);

gid_t getgid(void);
gid_t getegid(void);

This is pretty straightforward:

  • getuid(2) returns the real UID; geteuid(2) returns the effective UID
  • getgid(2) returns the real GID; getegid(2) returns the effective GID
  • uid_t and gid_t are glibc typedefs for an unsigned integer
Here is a neat tip to figure out the typedef for any given data type: you will need to know the header file that contains the definition. Just do this:

$ echo | gcc -E -xc -include 'sys/types.h' - | grep uid_t
typedef unsigned int __uid_t;
typedef __uid_t uid_t;
$

Credit: https://stackoverflow.com/questions/2550774/what-is-size-t-in-c.

A question comes up: the preceding system calls do not take any parameters; they return the real or effective [U|G]IDs, yes, but for which process? The answer, of course, is the calling process, the process that issues the system calls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset