第六章-系統數據文件和信息

口令文件

  • /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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章