歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Unix下文件的訪問權限

Unix下文件的訪問權限

日期:2017/2/28 15:47:14   编辑:Linux教程

access Function:

When we open a file, the kernel performs its access tests based on the effective user and group IDs. There are times when a process wants to test accessibility based on the real user and group IDs. This is useful when a process is running as someone else, using either the set-user-ID or the set-group-ID feature. Even though a process might beset-user-ID to root, it could still want to verify that the read user can access a given file. The access function bases its tests on the real user and group IDs.

  1. #include <fcntl.h>
  2. #include "apue.h"
  3. #include "my_err.h"
  4. int main(int argc, char* argv[])
  5. {
  6. if( argc != 2 )
  7. {
  8. err_quit("usage: a .out <pathname>");
  9. }
  10. if( access( argv[1], R_OK) < 0 )
  11. {
  12. err_ret("access error for %s", argv[1]);
  13. }
  14. else
  15. {
  16. printf("read access ok\n");
  17. }
  18. if( open( argv[1], O_RDONLY) < 0 )
  19. {
  20. err_ret("open error for %s", argv[1]);
  21. }
  22. else
  23. {
  24. printf("open for reading OK\n");
  25. }
  26. exit(0);
  27. }



說明: a.out本來沒有權限訪問/etc/shadow文件,該文件只有root用戶才有權限,但是我們把a.out改成root的文件,再增加S屬性,雖然用的是普通的用戶去執行root用戶的文件,由於a.out 文件有S屬性,所以,它的有效用戶id還是root的id,也只有這樣,a.out 才能訪問/etc/shadow文件。
Copyright © Linux教程網 All Rights Reserved