系統資料檔案和資訊之附加組ID

2022-05-04 20:21:21 字數 1474 閱讀 2787

4.2bsd引入了附加組id(supplementary group id)的概念。我們不僅可以屬於口令檔案記錄項中組id所對應的組,也可屬於多達16個另外的組。檔案訪問許可權檢查相應被修改為:不僅將程序的有效組id與檔案的組id相比較,而且也將所有附加組id與檔案的組id進行比較。

常量ngroups_max規定了附加組id的數量,其常用值是16.

使用附加組id的優點是不必再顯式地經常更改組。乙個使用者會參加多個專案,因此也就要同時屬於多個組。

為了獲取和設定附加組id,提供了下列三個函式:

#include int getgroups( int

gidsetsize, gid_t grouplist );

返回值:若成功則返回附加組id數,若出錯則返回-1

#include

/*on linux

*/#include

/*on freebsd, mac os x, and solaris

*/int setgroups( int ngroups, const

gid_t grouplist );

#include

/*on linux and solaris

*/#include

/*on freebsd and mac os x

*/int initgroups( const

char *username, gid_t basegid );

兩個函式返回值:若成功則返回0,若出錯則返回-1

getgroups將各附加組id填寫到陣列grouplist中,該陣列中存放的元素最多為gidsetsize個。實際填寫到陣列中的附加組id數由函式返回。

作為乙個特例,如若gidsetsize為0,則函式只返回附加組id數,而對陣列grouplist則不作修改(這使呼叫者可以確定grouplist陣列的長度,以便進行分配)。

setgroups可由超級使用者呼叫以便為呼叫程序設定附加組id表。grouplist是組id陣列,而ngroups指定了陣列中的元素個數。ngroups的值不能大於ngroups_max。

通常,只有initgroups函式呼叫setgroups,initgroups讀整個組檔案(用前面說明的函式getgrent、setgrent和endgrent),然後對username確定其組的成員關係。然後,它呼叫setgroups,以便為該使用者初始化附加組id表。因為initgroups呼叫setgroups,所以只有超級使用者才能呼叫initgroups。除了在組檔案中找到username是成員的所有組,initgroups也在附加組id表中包括了baseid。baseid是username在口令檔案中的組id。

只有少數幾個程式呼叫initgroups,例如login(1)程式在使用者登入時呼叫該函式。

本篇博文內容摘自《unix環境高階程式設計》(第二版),僅作個人學習記錄所用。關於本書可參考:

系統資料檔案和資訊之組檔案

unix組檔案 posix.1稱其為組資料庫 包含了表6 3中所示的字段。這些字段包含在中所定義的group結構中。表6 3 etc group檔案中的字段 欄位gr mem是乙個指標陣列,其中每個指標各指向乙個屬於該組的使用者名稱。該陣列以空指標結尾。可以用下列兩個由posix.1定義的函式來檢視...

系統資料檔案和資訊

include 分別通過uid和使用者名稱獲取與指定使用者相關的passwd資訊 成功返回指標,出錯返回null struct passwd getpwuid uid t uid struct passwd getpwnam const char name passwd結構可以儲存 etc pass...

APUE筆記 系統資料檔案和資訊

passwd結構體 include 這個結構體的字段也就是 ect passwd中看到的字段 root x 0 0 root root bin bash bin x 1 1 bin bin sbin nologin daemon x 2 2 daemon sbin sbin nologin etc ...