2012-04-20 31 views
1

我以前問來做一些事,像這樣防止用戶在他們的家目錄中刪除某些關鍵的文件/文件夾的問題:是否有可能/建議讓用戶沒有自己的主文件夾?

/home 
└── [-rw-rw-r-- daedalus ] daedalus 
    ├── [-rw-rw-r-- root ] do_not_delete_file.cpp 
    ├── [-rw-rw-r-- root ] do_not_delete_file.html 
    ├── [-rw-rw-r-- root ] do_not_delete_file.php 
    ├── [drwxrwxr-x root ] do_not_delete_folder 
    │   ├── [-rw-rw-r-- root ] do_not_delete_file.cpp 
    │   ├── [-rw-rw-r-- root ] do_not_delete_file.html 
    │   └── [-rw-rw-r-- root ] do_not_delete_file.php 
    └── [-rw-rw-r-- daedalus ] index.html 

但由於用戶daedalus已經寫了,因此刪除,權限上失敗了自己的/home/daedalus文件夾。因此,雖然他們無法修改,例如do_not_delete_file.php,但他們可以隨時刪除它,並在以後進行替換。

但如果相反,我有這樣的事情

/home 
└── [-rw-rw-r-- root ] daedalus 
    ├── [-rw-rw-r-- root ] do_not_delete_file.cpp 
    ├── [-rw-rw-r-- root ] do_not_delete_file.html 
    ├── [-rw-rw-r-- root ] do_not_delete_file.php 
    ├── [drwxrwxr-x root ] do_not_delete_folder 
    │   ├── [-rw-rw-r-- root ] do_not_delete_file.cpp 
    │   ├── [-rw-rw-r-- root ] do_not_delete_file.html 
    │   └── [-rw-rw-r-- root ] do_not_delete_file.php 
    ├── [drwxrwxr-x daedalus ] Documents 
    └── [-rw-rw-r-- root ] index.html 

我假設這是可能的一個小chmod動作,但是它會導致很多很多問題的方案時,自然認爲他們擁有寫主目錄的權限?如果這是一個壞主意,我能否就如何解決這個問題得到一些其他建議。

編輯:我問這個問題的原因與LAMP編程有關。我創建了一系列的虛擬主機,即:

  • www.user1.example.com
  • www.user2.example.com
  • ...
  • www.userN.example.com

而且我要的文檔根在設置:

  • /home/user1
  • /home/user2
  • ...
  • /home/userN

不過,我有給予太多/太少自由的個人用戶掙扎。在最簡單的情況下,我可能需要一個do_not_delete_folder用戶可以訪問但不能修改/刪除。

+0

如果您在這裏沒有得到答案,你可以在serverfault.com上試試這個問題。 – octern 2012-04-20 00:51:28

+0

@KenWhite我在這裏問這個問題的原因是,這與LAMP編程有關,我只是沒有深入細節。我將編輯問題 – puk 2012-04-20 01:09:29

+0

@Ken:我認爲這是話題;通過類比,考慮一下Qmail郵件程序,它需要兩個組和七個用戶ID來安裝。瞭解Unix自由訪問控制的更精細的部分是使它成爲當今安全系統的一部分。 Puk正在構建類似的東西,用於虛擬主機。瞭解他可用的合理選項是構建質量體系的一部分。 – sarnold 2012-04-20 01:40:04

回答

3

這有點棘手,但您可以使用Sticky Bit來做到這一點。

http://en.wikipedia.org/wiki/Sticky_bit

Linux內核忽略粘滯位除目錄外。對於目錄linux只允許超級用戶和所有者刪除,重命名或取消鏈接文件。它不允許使用組權限訪問文件的用戶。

例如,我的用戶名爲葡萄是葡萄組的成員

添加粘滯位可使用chmod命令:

chmod +t [file] 

在這裏,你可以創建一個用戶有一個目錄權限使用羣體,但由超級用戶擁有

drwxrwxr-T 2根葡萄4096 2012-04-19 20:49 ST

在這種情況下,用戶仍然可以創建/聖刪除自己的文件,但不能刪除根目錄中的文件,即使用戶有寫的權限:

-rwxrwxrwx 1 root root 0 2012-04-19 20:45 test 
-rw-rw-r-- 1 grape grape 0 2012-04-19 20:56 test2 

我仍然可以讀/寫這兩個文件,但只有我自己的文件可以刪除/移動

+0

是的,但然後不會每個人都能夠寫入您的粘性主文件夾? – puk 2012-04-20 01:07:00

+0

你是什麼意思?只有該組的成員可以寫入該文件夾,但在這個例子中,我給了我粘貼文件夾0666的權限,在現實世界中664或660更好。 – Eric 2012-04-20 01:09:39

+0

@puk:它需要設置組所有者,以便只有一個用戶可以使用它,或者使用'setfacl(1)'來設置訪問控制列表。 – sarnold 2012-04-20 01:09:53

2

許多程序和用戶希望能夠直接在其主目錄中創建任何需要的文件。這可能是~/.xsession-errors,~/.viminfo,~/.lesshst,~/.bash_history,或其他我可能錯過了 - 或者我沒有運行,但您的用戶確實運行。

所以,這不會是無痛的,但這裏的一些想法:

  • 可以通過讓別人擁有的主目錄來接近你想要什麼,並設置該目錄的粘性位 - 這將要求用戶擁有一個文件或目錄,然後才能刪除;從unlink(2)

    EPERM or EACCES 
         The directory containing pathname has the sticky bit 
         (S_ISVTX) set and the process's effective UID is 
         neither the UID of the file to be deleted nor that of 
         the directory containing it, and the process is not 
         privileged (Linux: does not have the CAP_FOWNER 
         capability). 
    

    你會要麼去過長的長度與任何setfacl(1)設置訪問控制列表或者給每一個用戶自己的組。如果沒有這兩個步驟之一,您可能會無意中授予用戶修改彼此數據的權限。

  • 可以使用chattr(1)屬性i使文件不變 - 它不能被修改,鏈接,或未鏈接,除由特權進程。從chattr(1)

    A file with the `i' attribute cannot be modified: it cannot 
    be deleted or renamed, no link can be created to this file 
    and no data can be written to the file. Only the superuser 
    or a process possessing the CAP_LINUX_IMMUTABLE capability 
    can set or clear this attribute. 
    
  • 您可以使用綁定安裝安裝的文件或目錄到用戶的主目錄。這可以在用戶仍然擁有他們的主目錄時完成;只需運行mount -obind /path/to/source /home/daedalus/do_not_delete。如果目錄或文件不是由用戶所有,則它們不能修改該文件,並且它們不能修改安裝點本身。 (他們可以修改更高級別的目錄,使路徑意義 - 這樣做直接進入主目錄,但不要試圖做成子目錄。)

    # mount -obind /etc /tmp/sarnold/mount_point/ 
    # mount -obind /etc/passwd /tmp/sarnold/passwd 
    $ rm mount_point/ 
    rm: cannot remove `mount_point/': Is a directory 
    $ rmdir mount_point/ 
    rmdir: failed to remove `mount_point/': Device or resource busy 
    $ mv mount_point/ blob 
    mv: cannot move `mount_point/' to `blob': Device or resource busy 
    $ rm /tmp/sarnold/passwd 
    rm: remove write-protected regular file `/tmp/sarnold/passwd'? y 
    rm: cannot remove `/tmp/sarnold/passwd': Device or resource busy 
    $ mv /tmp/sarnold/passwd /tmp/sarnold/old_passwd 
    mv: cannot move `/tmp/sarnold/passwd' to `/tmp/sarnold/old_passwd': Device or resource busy 
    

    (我離開了創建掛載點;這只是一個簡單的touchmkdir。)

    當然,每次重新啓動時綁定掛載都會消失。您可能需要使用pam_exec(8)或正確配置fstab(5)以在每次登錄或引導時重新創建綁定掛載。

  • 可以配置一個mandatory access control工具,如AppArmorSELinuxTOMOYO,或SMACK限制提供給用戶進程的權限。 (我已經成爲AppArmor團隊成員十二年了;根據您的服務器的使用情況,它可能會也可能不是這種用途的理想選擇,我相信大多數這些系統可以配置爲按照您的需要進行配置,但由於AppArmor配置和TOMOYO是基於名稱,我相信他們會更容易控制訪問的文件和目錄比的SELinux或SMACK,這是基於標籤系統。)

+0

非常詳盡的迴應。謝謝。 – puk 2012-04-20 01:19:44

相關問題