UNIX环境高级编程学习笔记(七)系统数据文件和信息

来源:互联网 发布:音箱仿真软件 编辑:程序博客网 时间:2024/04/30 02:23

1.口令文件
UNIX 口令文件包含了用户名、加密口令、数值用户ID和数值组ID等字段,这些字段包含在 pwd.h 中定义的passwd 结构中。POSIX.1 定义了两个存取口令文件中信息的函数。在给出用户登录名或数值用户ID后,这两个函数就能查看相关项。

#include <sys/types.h>#include <pwd.h>struct passwd *getpwuid(uid_t uid) ;struct passwd *getpwnam(const char * name) ;

如果要查看的只是一个登录名或用户ID,那么这两个 POSIX.1 函数能满足要求,但是也有些程序要查看整个口令文件。下列三个函数则可用于此种目的。

#include <sys/types.h>#include <pwd.h>struct passwd *getpwent(void);void setpwent(void);void endpwent(void);

2.阴影口令(shadow password)

  • 加密口令是经单向加密算法处理过的用户口令副本。因为此算法是单向的,所以不能从加密口令猜测到原来的口令。对于加密口令,找不到一种算法可以将其反变为明文口令,但是可以对口令进行猜测,将猜测的口令经单向加密算法变换成加密形式,然后与用户的加密口令进行比较。
  • 为使企图这样做的人难以获得原始资料(加密口令),某些系统将加密口令存放在一个通常称为阴影口令的文件中。该文件至少要包含用户名和加密口令,与该口令相关的其他信息也可存放在该文件中。只有用户登录名和加密口令这两个字段是必须的。
  • 阴影口令文件不应是一般用户可以读取的。仅有少数几个程序需要访问加密口令,如 login(1) 和 passwd(1) ,这些程序常常是设置用户ID为 root 的程序。有阴影口令文件后,普通口令文件 /etc/passwd 可由用户自由读取。

3.组文件
UNIX组文件包含的字段包括:组名、加密口令、数值组ID和指向个用户的指针。这些字段包含在 grp.h 中所定义的 group 结构中。

说明 struct group 成员 组名 char *gr_name 加密口令 char *gr_passwd 数值组ID int gr_gid 指向个用户的指针 char **gr_mem

gr_mem字段是一个指针数组,每个指针指向一个属于该组的用户名。POSIX.1 定义了如下的函数来查看组名或者数值组ID:

#include <grp.h>struct group *getgrgid(gid_t gid);struct group *getgrnam(const char *name);

4.附属组ID
在 UNIX 系统中,对组的使用已经做了些更改。在V7中,每个用户在任何时候都只属于一个组。当用户登录时,系统就按口令文件记录项中的数值组ID,赋给他实际组ID。在任何时候执行 newgrp(1) 以更改组ID。如果 newgrp 命令执行成功,则实际组ID就更改为新的组ID,它将被用于后续的文件访问权限检查,执行不带任何参数的 newgrp 则可返回到原来的组。4.2BSD引入了附属组ID的概念。我们不仅可以属于口令文件记录项中组ID所对应的组,还可以属于多至16个另外的组。文件访问权限检查相应被修改为:不仅将进程的有效组ID与文件的组ID进行比较,而且将所有附属组ID与文件的组ID进行比较。
为了获取和设置附属组ID,提供下列3个函数:

#include <unistd.h>int getgroups(int gidsetsize, gid_t grouplist[]);#include <grp.h> /* on Linux */#include <unistd.h> /* on FreeBSD, Mac OS X, and Solaris */int setgroups(int ngroups, const gid_t grouplist[]);#include <grp.h> /* on Linux, and Solaris*/#include <unistd.h> /* on FreeBSD, and Mac OS X */int initgroups(const char *username, gid_t basegid);

5.其他数据文件
除了口令文件和组文件,UNIX 系统还使用很多其他文件。对于这些数据文件的接口都与上述对口令文件和组文件的相似。一般情况下,每个数据文件至少包括3个函数:

  • get 函数:读一个记录,如果需要,还会打开该文件。
  • set 函数:打开相应数据文件(如果尚未打开),然后反绕该文件。如果希望在文件起始处开始处理,则调用此函数。
  • end 函数:关闭相应数据文件。

以下是访问数据文件的一些例程:

这里写图片描述

6.登录账户记录
大多数UNIX系统都提供下列两个数据文件:utmp文件,它记录当前登录进系统的各个用户; wtmp 文件,它跟踪各个登录和注销事件。在V7中,每次写入这两个文件中的是包含下列结构的一个二进制记录:

struct utmp{    char ut_line[8];    char ut_name[8];    long ut_time;}

大多数UNIX版本仍提供 utmp 和 wtmp 文件。

7.系统标识
POSIX.1定义了uname函数,它返回与主机和操作系统有关的信息:

#include<sys/utsname.h>intuname(struct utsname* name);

通过该函数的参数向其传递一个 utsname 结构的地址,然后该函数填写此结构。POSIX.1 只定义了该结构中最少需提供的字段,而每个数组的长度则由实现确定。

struct utsname {    cahr sysname[];   /* name of 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 */}

gethostname 函数在 POSIX.1 中定义,它指定最大主机名长度是HOST_NAME_MAX。

#include <unistd.h>int gethostname(char *name, int namelen);

8.时间和日期例程

  • time 函数返回当前时间和日期:
#include <time.h>time_t time(time_t *calptr);
  • clock_gettime 函数可用于获取指定时钟的时间。
  • clock_getres 函数把参数tsp指向的timespec结构初始化为与clock_id参数对应的时钟精度。
#include <sys/time.h>int clock_gettime(clockid_t clock_id, struct timespec *tsp);int clock_getres(clockid_t clock_id, struct timespec *tsp);
  • 要对特定的时钟设置时间,可以调用clock_settime函数。
#include <sys/time.h>int clock_settime(clockid_t clock_id, struct timespec *tsp);
  • gettimeofday函数现在已经弃用,然而一些程序仍然在使用这个函数,因为与time函数相比,gettimeofday提供了更高的精度。
#include <sys/time.h>int gettimeofday(struct timeval *restrict tp, struct timeval *restrict tzp);
  • gmtime 和 localtime 函数将指定的Unix 标准时间值转换为格林尼治时间或本地时间并存到 tm 结构中返回其指针:
#include <time.h>struct tm *gmtime(const time_t *calptr);struct tm *localtime(const time_t *calptr);
  • mktime 函数将tm 结构的时间转换为 Unix 标准时间:
#include <time.h>time_t mktime(struct tm *tmptr);
  • asctime 和 ctime 函数将参数指定的时间转换为常见的时间日期格式字符串,受环境变量影响:
#include <time.h>char *asctime(const struct tm *tmptr);char *ctime(const time_t *calptr);
  • 格式化时间字符串
#include <time.h>size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);
0 0
原创粉丝点击