/etc
底下/etc/passwd
/etc/group
id
command will return user identitypasswd file
Description | struct password member |
---|---|
User name | char *pw_name |
Encrypted password | char *pw_passwd |
Numerical user id | uid_t pw_uid |
Nunerical group id | gid_t pw_gid |
Comment field | char *pw_gecos |
Initial working dir | char *pw_dir |
Initial shell | char *pw_shell |
User access class | char *pw_class |
Next time to change password | time_t pw_change |
Account expiration time | time_t pw_expire |
:
隔開每個欄位/dev/null
、/bin/false
、/sbin/nologin
等方式拒絕 user 登入系統/etc/shells
中finger
command is a user information lookup programFunction to Retrieve Password Information
include <pwd.h> struct passwd *getpwuid(uid_t uid); struct passwd *getpwnam(const char *name); struct passwd { char *pw_name; /* username */ char *pw_passwd; /* user password */ uid_t pw_uid; /* user ID */ gid_t pw_gid; /* group ID */ char *pw_gecos; /* user information */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ };
#include <sys/types.h> #include <pwd.h> struct passwd *getpwent(void); void setpwent(void); void endpwent(void);
User and Shadow Passwords
/etc/passwd
/etc/master.passwd
/etc/shadow
Linux's Shadow Structure
#include <shadow.h> struct spwd *getspnam(const char *name); struct spwd *getspent(void); void setspent(void); void endspent(void); struct spwd { char *sp_namp; /* Login name */ char *sp_pwdp; /* Encrypted password */ long sp_lstchg; /* Date of last change (measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */ long sp_min; /* Min # of days between changes */ long sp_max; /* Max # of days between changes */ long sp_warn; /* # of days before password expires to warn user to change it */ long sp_inact; /* # of days after password expires until account is disabled */ long sp_expire; /* Date when account expires (measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */ unsigned long sp_flag; /* Reserved */ };
More on User Passwords
$
連接#include <unistd.h> char *crypt(const char *key, const char *salt);
group file
Description | struct group member |
---|---|
Group name | char *gr_name |
Encrypted password | char *gr_passwd |
Nunerical group id | gid_t gr_gid v |
Array of pointers to individual user names |
};
</code>
#include <sys/types.h> #include <grp.h> struct group *getpwent(void); void setgrent(void); void endgrent(void);
Supplement Group IDs
/etc/passwd
中記錄著每個 user 的 default gidGroup setup function
#include <unistd.h> #include <grp.h> int getgroups(int gidsetsize, gid_t grouplist[]); int initgroups(const char *user, gid_t group); int setgroups(size_t size, const gid_t *list);
/etc/group
Other Data Files
Description | Data file | Header | Structure | Lookup function |
---|---|---|---|---|
password | /etc/passwd | <pwd.h> | passwd | getpwnam, getpwuid |
groups | /etc/groups | <grp.h> | group | getgrnam, getgrgid |
shadow | /etc/shadow | <shadow.h> | shwd | getspnam |
hosts | /etc/hosts | <netdb.h> | hostent | gethostbyname, gethostbyaddr |
networks | /etc/networks | <netdb.h> | netent | getnetbyname, getnetbyaddr |
protocols | /etc/protocols | <netdb.h> | protornt | getprotobynabe, getprotobynumber |
services | /etc/services | <netdb.h> | servent | getservbyname, getservbyport |
struct utmp { char ut_line[8]; /* tty line: "ttyh0", "ttyd0", "ttyp0", ... */ char ut_user[8]; /* login name */ long ut_time; /* seconds since Epoch */ };
The uname function
#include <sys/utsname.h> int uname(struct utsname *name); struct utsname { char sysname[]; /* name of the operating system */ char nodename[]; /* name of this node */ char release[]; /* current release of operating system */ char version[]; /* current version of this release */ char machine[]; /* name of hardware type */ };
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/utsname.h> int main(void) { struct utsname buffer; errno = 0; if (uname(&buffer) != 0) { perror("uname"); exit(EXIT_FAILURE); } printf("system name = %s\n", buffer.sysname); printf("node name = %s\n", buffer.nodename); printf("release = %s\n", buffer.release); printf("version = %s\n", buffer.version); printf("machine = %s\n", buffer.machine); #ifdef _GNU_SOURCE printf("domain name = %s\n", buffer.domainname); #endif return EXIT_SUCCESS; }
The uname command
$ uname -a Linux ubuntu-vm 3.16.0-62-generic #83~14.04.1-Ubuntu SMP Fri Feb 26 22:52:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
tm structure
#include <time.h> #include <sys/time.h> time_t time(time_t *t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; struct timezone { int tz_minuteswest; /* minutes west of Greenwich */ int tz_dsttime; /* type of DST correction */ };
#include <time.h> char *asctime(const struct tm *tm); char *asctime_r(const struct tm *tm, char *buf); char *ctime(const time_t *timep); char *ctime_r(const time_t *timep, char *buf); struct tm *gmtime(const time_t *timep); struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime(const time_t *timep); struct tm *localtime_r(const time_t *timep, struct tm *result); time_t mktime(struct tm *tm); struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ };
#include <time.h> size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
Time Zone