2013-04-22 92 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

相关问题