Saturday, August 16, 2014

Functions to get and set shadow and passwd file values

An introduction was given about the shadow and passwd files in linux in my last post. Here are some of the functions with their signatures which are provided to get/set values from/to these files:

struct spwd *getspnam (const char *name);

Following is the structure which gets populated if the user is present:

 struct spwd {
     char *sp_namp;
     char *sp_pwdp;
     long sp_lstchg;
     long sp_min;
     long sp_max;
     long sp_warn;
     long sp_inact;
     long sp_expire;
     unsigned long sp_flag;
};

Following are the functions which may be used to get entries one by one:

void setspent (void);
struct spwd *getspent (void);
void endspent (void);

Following function deals with passwd file entries:

struct passwd *getpwnam (const char *name);

It returns following structure:
 struct passwd {
     char *pw_name;
     char *pw_passwd;
     uid_t pw_uid;
     gid_t pw_gid;
     char *gecos;
     char *pw_dir;
     char *pw_shell;
};

An exhaustive explanation and lists of functions, their return values, header files to include, etc may be found in their manual pages i.e. man 3 getspnam, man 3 getpwnam, etc and the above definitions of structures and functions signatures are taken from these manual pages itself.

No comments:

Post a Comment