日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

UNIX如何設(shè)置用戶ID位?

在Linux系統(tǒng)中每一個(gè)進(jìn)程都有好幾個(gè)用戶ID位 , 這些用戶ID位怎么設(shè)置關(guān)系到文件訪問的權(quán)限 。本文就來以UNIX為例 , 簡單介紹一下UNIX如何設(shè)置用戶ID位 。

UNIX如何設(shè)置用戶ID位?


用stat函數(shù)可以獲取一個(gè)文件的狀態(tài)信息 , 原型是這樣的:
int stat(const char *path ,  struct stat *buf);
其中結(jié)構(gòu)體stat的結(jié)構(gòu):
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size ,  in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
從傳出的參數(shù)buf中可以拿到用st_uid , st_gid 表示的文件所有者ID , 和文件所有者所在的組ID 。
在UNIX進(jìn)程中也有幾組ID的概念 。分別是實(shí)際用戶ID , 實(shí)際用戶組ID , 有效用戶ID和有效用戶組ID等等 。當(dāng)我們開始一個(gè)進(jìn)程是 , 通常這個(gè)進(jìn)程的有效用戶ID就是這個(gè)進(jìn)程的實(shí)際ID(比如我用eric用戶登錄 , 這個(gè)有效用戶就我eric對應(yīng)的ID) 。然而當(dāng)“設(shè)置用戶ID位”打開以后 , 有效ID就是進(jìn)程的程序文件對應(yīng)的所有者的ID 。
$ls -l 1.txt
-rw------- 1 root root 16 4月 29 14:31 1.txt
當(dāng)前目錄下面有一個(gè)文件“1.txt”是所有者root , 并且只有root具有讀和寫權(quán)限 。
1 int main()
2 {
3 int fd;
【UNIX如何設(shè)置用戶ID位?】 4 if((fd=open(“1.txt” , O_RDONLY)) == -1)
5 {
6 printf(“Open failed.\n”);
7 exit(-1);
8 }
9 char buf[1024]={0};
10 read(fd , buf , 1024);
11 printf(buf);
12 printf(“\n”);
13 }
首先我在終端里使用su命令使用root用戶 。gcc read.c -omain 。得到main程序 。
# gcc read.c -omain
# exit
exit
$ main
Open failed.
顯然main的所有者也是root , 但是main程序依舊不可以打開“1.txt” , 這是因?yàn)閙ain啟動(dòng)后這個(gè)進(jìn)程的有效ID是進(jìn)程的實(shí)際用戶ID(也就是eric賬戶的ID) , 而“1.txt”只對root用戶具有讀寫權(quán)限 , 所以open失敗 。
把main的設(shè)置用戶ID位打開可以用shell指令: chmod u+s main
我用的是c程序 , 主要代碼如下:
1 struct stat buf = {0};
2 stat(“main” , &buf);
3 buf.st_mode |= S_ISUID;
4 chmod(“main” , buf.st_mode);
執(zhí)行后 , main的“設(shè)置用戶ID位”就打開了 。再在非root終端下 執(zhí)行main程序 就可以成功的讀出 1.txt的內(nèi)容
$ main
linuxidc.com
linux權(quán)限設(shè)計(jì)還是比較合理的 , 雖然這里main程序可以運(yùn)行時(shí)是已所有者root的權(quán)限 , 但是這需要root用戶的授權(quán):打開這個(gè)程序文件的“set uid bit”(設(shè)置用戶ID位) 。只要在打開這個(gè)set uid bit 時(shí)充分考慮到這個(gè)程序存在的風(fēng)險(xiǎn) 。當(dāng)然授權(quán)需謹(jǐn)慎 。
以上就是UNIX如何設(shè)置用戶ID位的全部內(nèi)容了 , 本文介紹了設(shè)置用戶ID , 設(shè)置用戶ID也是文件權(quán)限設(shè)置的一個(gè)例子 。

推薦閱讀