6.系統數據文件和信息

1、引言

歷史原因數據文件通常是ASCII文本文件,,順序掃描很花時間,我們需要非ASCII文本格式存放這些文件,但仍向使用其他文件格式的應用程序提供接口


2、口令文件

  • unix系統口令文件字段包含在pwd.h中的passwd結構中,歷史原因,這是一個ASCII文件(/etc/passwd)
    • 通常包含root項,用戶ID是0
    • 加密口令字段包含一個佔位符
    • 某些字段可能爲空
    • shell字段包含一個可執行程序名(登錄shell),squid的是/dev/null(這阻止了任何人以squid登錄)
    • 可以用/bin/false或/bin/true作爲登錄shell組織用戶登錄,有些系統提供nologin命令(可打印定製的出錯信息)
    • 提供finger命令的某些系統支持註釋字段中的附加信息(姓名、電話號碼等,finger -p root)
    • 某些系統提供vipw命令,允許管理員編輯口令文件
    • posix.1中定義了兩個獲取口令文件項的函數
      struct passwd *getpwuid(uid_t uid);
      int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
              size_t bufsize, struct passwd **result);
      struct passwd *getpwuid(uid_t uid);
      int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
              size_t bufsize, struct passwd **result);
      
      • ls程序用到了getpwuid,將i節點中的數字用戶ID映射爲用戶登錄名
      • 在鍵入登錄名時,login程序調用getpwuid函數
    • 查看整個口令文件可以用下面的函數
      struct passwd *getpwent(void);
      void setpwent(void);
      void endpwent(void);
      
      • getpwent()返回口令文件的下一個記錄項
      • setpwent()反繞所使用的文件,定位到文件開始
      • endpwent()關閉口令文件

3、陰影文件

  • 通過阻止獲取原始加密口令而防止試探方法猜測口令,現在,某些系統將加密口令放在另一個文件中–陰影口令
  • 僅login和passwd等少數程序可以訪問
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);
struct spwd *fgetspent(FILE *stream);
struct spwd *sgetspent(const char *s);
int putspent(const struct spwd *p, FILE *stream);
int lckpwdf(void);
int ulckpwdf(void);

4、組文件

  • UNIX組文件,POSIX稱其爲組數據庫
    #include <grp.h>
    struct group *getgrnam(const char *name);
    struct group *getgrgid(gid_t gid);
    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 */
    };
    
    • 函數也返回一個靜態變量的指針
  • 搜索整個組文件可以下面的接口
    struct group *getgrent(void);
    void setgrent(void);
    void endgrent(void);
    
    • setgrent()打開組文件並反繞,getgrent()讀取下一個,endgrent()關閉組文件

5、附屬組ID

  • 我們不僅可以屬於口令文件中記錄項中組ID對應的組,也可以屬於至多16個另外的組
  • 文件訪問權限被修改爲:不僅比較進程的有效組ID和文件組ID,也比較附屬組ID與文件的組ID
    #include <grp.h>
    int getgroups(int size, gid_t list[]);
    int setgroups(size_t size, const gid_t *list);
    int initgroups(const char *user, gid_t group);
    
    • getgroups()將進程所屬用戶的附屬組ID填到數組grouplist中,附屬組ID數最多爲gidsetsize個,函數返回實際填寫的個數
    • gidsetsize爲0時只返回附屬組ID數,不修改list數組
    • setgroups()設置附屬組ID表
    • initgroups()讀整個組文件,然後對user確定組的成員關係,然後調用setgroups(),以便爲用戶初始化附屬組ID表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章