口令文件:/etc/passwd
pwd.h定义的passwd结构:
/* The passwd structure. */
struct passwd
{
char *pw_name; /* Username. */
char *pw_passwd; /* Password. */
__uid_t pw_uid; /* User ID. */
__gid_t pw_gid; /* Group ID. */
char *pw_gecos; /* Real name.这里可以是注释 */
char *pw_dir; /* Home directory. */
char *pw_shell; /* Shell program. */
};
根据username或者user id来获取记录:
#include <sys/type.h>
#include <pwd.h>
struct passwd* getpwuid(uid_t uid);
struct passwd* getpwnam(const char* name);
获取全部口令文件:
#include <sys/type.h>
#include <pwd.h>
struct passwd* getpwent(void);
void setpwent(void);
void endpwent(void);
其中,getpwent依次获取一条记录,知道NULL。setpwent打开归零,endpwent关闭。这个具有通用性,其他文件和这个是类似的。
/etc/shadow
这个文件存放用户名和密码的密文,从而使得passwd文件的密码密文用*代替,防止猜测密码。这样,passwd文件可由各用户读取。
组文件:/etc/group
<grp.h>中定义的group类型:
struct group
{
char *gr_name; /* Group name. */
char *gr_passwd; /* Password. */
__gid_t gr_gid; /* Group ID. */
char **gr_mem; /* Member list. */
};
根据组名或者组id获取group对象:
#include <sys/type.h>
#include <grp.h>
struct group* getgrgid(git_t git);
struct group* getgrnam(const char* name);
这种函数返回的是指针,实际内容存放在静态存储区,要复制的话必须把内容考出来,否则容易冲掉。
获取全部组对象:
#include <sys/type.h>
#include <grp.h>
struct group* getgrent(void);
void setgrent(void);
void endgrent(void);
组的特殊性在于一个user 可以属于不同的 group,称为添加组。存取和设置:
#include <sys/type.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);//把进程所属的user的所有group放在list中,最大个数是size
#include <grp.h>
int setgroups(size_t size, const gid_t* list);//超级用户调用来为进程设置添加组id表。
int initgroups(const char* user, git_t group);//root可以调用,把group加入添加组,同时这个group是/etc/passwd中写的组。
其他数据文件:
对比:
说明 数据文件 头文件 结构 附加的关键字搜索
口令 /etc/passwd <pwd.h> passwd getpwnam,getpwuid
组 /etc/group <grp.h> group getgrnam,getgrgid
主机 /etc/hosts <netdb.h> hostent gethostbyname,gethostbyaddr
网络 /etc/networks <netdb.h> netent getnetbyname,getnetbyaddr
协议 /etc/protocols <netdb.h> protoent getprotobyname,getprotobynumber
服务 /etc/services <netdb.h> servent getservbyname,getservbyport
这些数据文件同样有get*,set*,end*函数,比如主机有:
struct hostent* gethostent(void);
void sethostent(int stayopen);
void endhostent(void);
登录记录文件:
who用的是utmp文件,last用的是wtmp文件。
#include <bits/utmp.h>
/* The structure describing an entry in the user accounting database. */
struct utmp
{
short int ut_type; /* Type of login. */
pid_t ut_pid; /* Process ID of login process. */
char ut_line[UT_LINESIZE]; /* Devicename. */
char ut_id[4]; /* Inittab ID. */
char ut_user[UT_NAMESIZE]; /* Username. */
char ut_host[UT_HOSTSIZE]; /* Hostname for remote login. */
struct exit_status ut_exit; /* Exit status of a process marked
as DEAD_PROCESS. */
/* The ut_session and ut_tv fields must be the same size when compiled
32- and 64-bit. This allows data files and shared memory to be
shared between 32- and 64-bit applications. */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
int32_t ut_session; /* Session ID, used for windowing. */
struct
{
int32_t tv_sec; /* Seconds. */
int32_t tv_usec; /* Microseconds. */
} ut_tv; /* Time entry was made. */
#else
long int ut_session; /* Session ID, used for windowing. */
struct timeval ut_tv; /* Time entry was made. */
#endif
int32_t ut_addr_v6[4]; /* Internet address of remote host. */
char __unused[20]; /* Reserved for future use. */
};
这是ubuntu下的。
系统标识:
这个函数返回与主机和操作系统相关的消息:
#include <sys/utsname.h>
int uname(struct utsname* buf);
/* Structure describing the system and machine. */
struct utsname
{
/* Name of the implementation of the operating system. */
char sysname[_UTSNAME_SYSNAME_LENGTH];
/* Name of this node on the network. */
char nodename[_UTSNAME_NODENAME_LENGTH];
/* Current release level of this implementation. */
char release[_UTSNAME_RELEASE_LENGTH];
/* Current version level of this release. */
char version[_UTSNAME_VERSION_LENGTH];
/* Name of the hardware type the system is running on. */
char machine[_UTSNAME_MACHINE_LENGTH];
#if _UTSNAME_DOMAIN_LENGTH - 0
/* Name of the domain of this node on the network. */
# ifdef __USE_GNU
char domainname[_UTSNAME_DOMAIN_LENGTH];
# else
char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
};
获取主机:
系统时间:
这个是系统时间函数,其中虚线受TZ影响。
#include <time.h>
/* Used by other time functions. */
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
format如下:
%a The abbreviated weekday name according to the current locale.
%A The full weekday name according to the current locale.
%b The abbreviated month name according to the current locale.
%B The full month name according to the current locale.
%c The preferred date and time representation for the current locale.
%C The century number (year/100) as a 2-digit integer. (SU)
%d The day of the month as a decimal number (range 01 to 31).
%D Equivalent to %m/%d/%y. (Yecch — for Americans only. Americans should note that in other countries
%d/%m/%y is rather common. This means that in international context this format is ambiguous and should
not be used.) (SU)
%e Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
%E Modifier: use alternative format, see below. (SU)
%F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%G The ISO 8601 week-based year (see NOTES) with century as a decimal number. The 4-digit year corresponding
to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week
number belongs to the previous or next year, that year is used instead. (TZ)
%g Like %G, but without century, that is, with a 2-digit year (00-99). (TZ)
%h Equivalent to %b. (SU)
%H The hour as a decimal number using a 24-hour clock (range 00 to 23).
%I The hour as a decimal number using a 12-hour clock (range 01 to 12).
%j The day of the year as a decimal number (range 001 to 366).
%k The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See
also %H.) (TZ)
%l The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See
also %I.) (TZ)
%m The month as a decimal number (range 01 to 12).
%M The minute as a decimal number (range 00 to 59).
%n A newline character. (SU)
%O Modifier: use alternative format, see below. (SU)
%p Either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale.
Noon is treated as "PM" and midnight as "AM".
%P Like %p but in lowercase: "am" or "pm" or a corresponding string for the current locale. (GNU)
%r The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to %I:%M:%S %p. (SU)
%R The time in 24-hour notation (%H:%M). (SU) For a version including the seconds, see %T below.
%s The number of seconds since the Epoch, that is, since 1970-01-01 00:00:00 UTC. (TZ)
%S The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap sec‐
onds.)
%t A tab character. (SU)
%T The time in 24-hour notation (%H:%M:%S). (SU)
%u The day of the week as a decimal, range 1 to 7, Monday being 1. See also %w. (SU)
%U The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as
the first day of week 01. See also %V and %W.
%V The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1
is the first week that has at least 4 days in the new year. See also %U and %W. (SU)
%w The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
%W The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as
the first day of week 01.
%x The preferred date representation for the current locale without the time.
%X The preferred time representation for the current locale without the date.
%y The year as a decimal number without a century (range 00 to 99).
%Y The year as a decimal number including the century.
%z The time-zone as hour offset from GMT. Required to emit RFC 822-conformant dates (using
"%a, %d %b %Y %H:%M:%S %z"). (GNU)
%Z The timezone or name or abbreviation.
%+ The date and time in date(1) format. (TZ) (Not supported in glibc2.)
%% A literal '%' character.