2013-04-22 91 views
7

我正在爲Linux系統開發一個庫(CLI程序集)。我想爲庫的用戶提供一種方法來切換當前有效的用戶和組。主要原因是提供訪問控制(某些用戶只允許某些操作),其次是啓用某個用戶修改文件系統。在Mono中運行的C#應用​​程序中更改當前的Linux用戶?

我已經確定了兩個可能的方法:

1.啓動單爲根,P /調用libc的例程像個seteuid等

已經通過設置在/ usr/bin中的S位來實現這/單,然後從我的圖書館設置回有效用戶(即單聲道運行時開始後)會導致單崩潰時終止:

ERROR:handles.c:1940:_wapi_handle_update_refs: assertion failed: (thr_ret == 0) 

Native stacktrace: 

mono2 [0x8bb6c] 
    /lib/libc.so.6(__default_rt_sa_restorer_v2+0) [0x4020a5a0] 
    /lib/libc.so.6(gsignal+0x40) [0x4020920c] 

按道理我的理解可能有問題的Wi改變Mono的有效用戶,因爲它管理着許多資源,這可能會導致問題的發生。

2.在本地後臺程序處理身份驗證,並不會改變單聲道的有效用戶

我不知道是否有這方面的任何關閉的,現成的解決方案,但在概念上我想有一個以根用戶身份運行的守護程序,庫將與之通信(例如,通過POSIX消息隊列)以執行身份驗證。守護進程以root用戶身份運行,以便能夠讀取/ etc/shadow。 Mono的有效用戶不會改變,但我的圖書館會跟蹤該進程運行的是哪個「等效用戶」。不幸的是,這種方法不允許庫作爲不同的用戶訪問文件系統。

問題

上午我堅持了第二個選項,或者是有一些方法來改變一個單進程的有效用戶?

謝謝!

回答

5

在我的箱子:)

編輯#1以下工作:這需要root權限執行。 (摘自http://msdn.microsoft.com/en-us/library/w070t6ka.aspx

using System; 
using System.Security.Permissions; 
using System.Security.Principal; 

public class ImpersonationDemo 
{ 
// Test harness. 
// If you incorporate this code into a DLL, be sure to demand FullTrust. 
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] 
public static void Main (string[] args) 
{ 
    try { 
     // Check the identity. 
     Console.WriteLine ("Before impersonation: " + WindowsIdentity.GetCurrent().Name); 

     // Impersonate a user 
     using (WindowsIdentity newId = new WindowsIdentity("Your user name")) 
     using (WindowsImpersonationContext impersonatedUser = newId.Impersonate()) 
     { 
      // Check the identity. 
      Console.WriteLine ("After impersonation: " + WindowsIdentity.GetCurrent().Name); 
     } 

     // Releasing the context object stops the impersonation 
     // Check the identity. 
     Console.WriteLine ("After closing the context: " + WindowsIdentity.GetCurrent().Name); 
    } catch (Exception ex) { 
     Console.WriteLine ("Exception occurred. " + ex.Message); 
    } 
} 
} 
+0

我不知道單聲道實現WindowsIdentity作爲可用的東西 - 感謝指出!有了示例代碼,我仍然遇到了使用seteuid()時出現的分段錯誤。無論如何接受這個答案。 – d99kris 2013-06-08 06:28:16

+0

您使用的是哪種版本的單聲道?我在x64系統上運行單聲道3.0.7-1並且以框架4.0爲目標。 – dna 2013-06-08 12:37:07

相關問題