口令文件
- /etc/passwd
- 以冒号分隔的各个字段在<pwd.h>文件中的passwd结构中能找到定义
该结构定义如下
struct passwd {
char *pw_name; /* user name */
char *pw_passwd; /* encrypted password */
int pw_uid; /* user uid */
int pw_gid; /* user gid */
char *pw_comment; /* comment */
char *pw_gecos; /* Honeywell login info */
char *pw_dir; /* home directory */
char *pw_shell; /* default shell */
};
- 以下是口令文件的部分内容
root:x:0:0:root:/root:/bin/bash
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
- root/nobody是用户名, x是加密口令字段,使用了一个占位符,加密口令字存放在另一个文件中,0/65534是user uid或user gid字段,root/nobody是注释字段,/bin/bash或usr/sbin/nologin为可执行程序作为用户的的登陆shell。
- 阻止一个用户登录至系统的方式: 用/dev/null, /bin/false, /bin/true,nologin命令等
- 管理员可用命令vipw编辑口令文件
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);//passwd 文件的(匹配用户名或user uid的)记录项
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwent(void);//口令文件中的下一个记录项
void setpwent(void); //读写地址设置为口令文件的开头
void endpwent(void); //关闭口令文件
阴影口令
- 加密口令是经单向加密算法处理过的用户口令副本,增强了系统的安全性
- 阴影口令文件/etc/shadow,各个字段在文件<shadow.h>的结构struct spwd中能找到定义,如下
The shadow password structure is defined in <shadow.h> as follows:
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 */
};
以下是访问阴影口令文件的一组函数
#include <shadow.h>
struct spwd *getspnam(const char *name);//shadow文件的(匹配用户名)的记录项
struct spwd *getspent(void);//shadow文件中的下一个记录项
void setspent(void);//读写地址设置为shadow文件的开头
void endspent(void);//关闭shadow文件
组文件
- 组文件/etc/group
- 该文件各个字段在文件<grp.h>的结构group中能找到定义,如下
The group structure is defined in <grp.h> as follows:
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* NULL-terminated array of pointers
to names of group members */
};
- 以下是对该文件进行操作的一组函数(返回的都是一个指向静态变量的指针)
#include <sys/types.h>
#include <grp.h>
struct group *getgrent(void);//从文件中读下一个记录
void setgrent(void);//打开组文件
void endgrent(void);//关闭组文件
struct group *getgrnam(const char *name);//和用户名匹配的记录项
struct group *getgrgid(gid_t gid);//和user uid匹配的记录项
附属组ID
- 用户所属组多至16个(除了属于口令文件记录项中组ID所对应的组之外)
- 以下是对附属组ID进行操作的一组函数
- 提供了一个用户同时可以参与多个组的方法
#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);//进程所属各个附属组ID填写到数组list中
#include <grp.h>
int setgroups(size_t size, const gid_t *list);//设置附属组ID表,超级用户可操作
其他数据文件及对其操作的一些例程
- 如记录协议信息的数据文件/etc/protocols
- 如记录网络信息的数据文件/etc/networks
- 如记录各个网络服务器所提供服务的数据文件/etc/services
- 对每个数据文件至少有三个函数,get函数,set函数和end函数
登录账户记录
- /var/run/utmp文件记录当前登录到系统的各个用户
- /var/log/wtmp文件跟踪各个登录和注销事件
系统标识
- 与主机和操作系统有关的信息
#include <sys/utsname.h>
int uname(struct utsname *buf);
The utsname struct is defined in <sys/utsname.h>:
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
#include <unistd.h>
int gethostname(char *name, size_t len);//返回主机名
int sethostname(const char *name, size_t len);
- 名字最大长度支持如下
时间和日期例程
以下是各种时间函数的关系
以下是关于各个函数的介绍
#include <time.h>
time_t time(time_t *tloc);//返回自UTC起经过的秒数
int clock_getres(clockid_t clk_id, struct timespec *res);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);
The res and tp arguments are timespec structures, as specified in <time.h>:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
//linux kernel支持以下clock
CLOCK_REALTIME
CLOCK_REALTIME_COARSE (since Linux 2.6.32; Linux-specific)
CLOCK_MONOTONIC
CLOCK_MONOTONIC_COARSE (since Linux 2.6.32; Linux-specific)
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
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);
truct tm *localtime_r(const time_t *timep, struct tm *result);
The ctime(), gmtime() and localtime() functions all take an argument of data type time_t, which represents calendar time. When interpreted as anabsolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
Broken-down time is stored in the structure tm, which is defined in <time.h> as follows:
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calendar time t into a null-terminated string of the form
"Wed Jun 30 21:49:08 1993\n"
The gmtime() function converts the calendar time timep to broken-down time representation, expressed in Coordinated Universal Time (UTC)
The localtime() function converts the calendar time timep to broken-down time representation, expressed relative to the user's specified time‐zone
The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character arrays of size max.
-format 参数控制时间值的格式,以下是转化说明
下面看一个应用实例
int
main(void)
{
time_t t;
struct tm *tmp;
char buf1[16];
char buf2[64];
time(&t);
tmp = localtime(&t);
if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 16 is too small\n");
else
printf("%s\n", buf1);
if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 64 is too small\n");
else
printf("%s\n", buf2);
exit(0);
}