using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.management;
using system.text;
using system.runtime.interopservices;
namespace setacl
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.button setacl;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.textbox2 = new system.windows.forms.textbox();
this.setacl = new system.windows.forms.button();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.button1 = new system.windows.forms.button();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point(80, 32);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(152, 21);
this.textbox1.tabindex = 0;
this.textbox1.text = "c:\\test";
//
// textbox2
//
this.textbox2.location = new system.drawing.point(80, 80);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(152, 21);
this.textbox2.tabindex = 1;
this.textbox2.text = "aspnet";
//
// setacl
//
this.setacl.location = new system.drawing.point(152, 136);
this.setacl.name = "setacl";
this.setacl.tabindex = 2;
this.setacl.text = "设置";
this.setacl.click += new system.eventhandler(this.setacl_click);
//
// label1
//
this.label1.location = new system.drawing.point(24, 80);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(48, 23);
this.label1.tabindex = 5;
this.label1.text = "用户:";
//
// label2
//
this.label2.location = new system.drawing.point(24, 32);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(48, 23);
this.label2.tabindex = 6;
this.label2.text = "目录:";
//
// button1
//
this.button1.location = new system.drawing.point(8, 168);
this.button1.name = "button1";
this.button1.tabindex = 7;
this.button1.text = "button1";
this.button1.click += new system.eventhandler(this.button1_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(280, 197);
this.controls.add(this.button1);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.setacl);
this.controls.add(this.textbox2);
this.controls.add(this.textbox1);
this.maximizebox = false;
this.minimizebox = false;
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
[dllimport("advapi32.dll")]
public static extern bool lookupaccountname(string lpsystemname, string lpaccountname,byte[] sid,ref int cbsid, stringbuilder referenceddomainname, ref int cbreferenceddomainname,ref int peuse);
private void setacl_click(object sender, system.eventargs e)
{
string filepath=this.textbox1.text;
string username=this.textbox2.text;
if(getfilesystem(filepath.substring(0,1))!="ntfs")
return;
if(findtrustee(filepath,username))
return;
setdacl(filepath,username);
}
private string getfilesystem(string diskname)
{
string filesystem="";
system.management.managementobjectsearcher diskclass=new managementobjectsearcher("select filesystem from win32_logicaldisk where name="+diskname+":");
managementobjectcollection disks=diskclass.get();
foreach(managementobject disk in disks)
{
propertydatacollection diskproperties=disk.properties;
foreach (propertydata diskproperty in diskproperties)
{
filesystem = diskproperty.value.tostring();
}
}
return filesystem;
}
private bool findtrustee(string filepath,string username)
{
arraylist trusteesname=new arraylist();
managementpath path = new managementpath( );
path.server = ".";
path.namespacepath = @"root\cimv2";
path.relativepath = @"win32_logicalfilesecuritysetting.path="+filepath+""; //定位到文件夹
managementobject dir = new managementobject(path);
managementbaseobject outparams = dir.invokemethod("getsecuritydescriptor", null, null); //获取安全描述符
if (((uint)(outparams.properties["returnvalue"].value)) != 0) //ok
{
throw new exception("获取文件描述符失败");
}
managementbaseobject descriptor = ((managementbaseobject)(outparams.properties["descriptor"].value));
managementbaseobject[] daclobject = ((managementbaseobject[])(descriptor.properties["dacl"].value)); //获取访问控制列表
for(int i=0;i<daclobject.length;i++)
{
trusteesname.add(((managementbaseobject)daclobject[i].properties["trustee"].value).properties["name"].value);
}
return trusteesname.contains(username);
}
private void setdacl(string filepath,string username)
{
//获取帐户信息
int cbsid = 100;
byte[] usersid = new byte[28] ;
stringbuilder domainname=new stringbuilder(255);
int domainnamelength = 255;
int sidtype =255;
bool result = lookupaccountname(null, username,usersid, ref cbsid, domainname,ref domainnamelength,ref sidtype);
if(!result)
return;
//获取文件描述符
managementpath path = new managementpath();
path.server = ".";
path.namespacepath = @"root\cimv2";
path.relativepath = @"win32_logicalfilesecuritysetting.path="+filepath+"";
managementobject dir = new managementobject(path);
managementbaseobject outparams = dir.invokemethod("getsecuritydescriptor", null, null);
if (((uint)(outparams.properties["returnvalue"].value)) != 0)
{
throw new exception("获取文件描述符失败");
}
managementbaseobject descriptor = ((managementbaseobject)(outparams.properties["descriptor"].value));
//获取访问控制列表
managementbaseobject[] daclobject = ((managementbaseobject[])(descriptor.properties["dacl"].value));
//复制一个访问控制项
managementbaseobject ace=(managementbaseobject)daclobject[0].clone();
//设置访问控制项属性
managementbaseobject trustee=(managementbaseobject)ace.properties["trustee"].value;
trustee.properties["domain"].value=domainname.tostring();
trustee.properties["name"].value=username;
trustee.properties["sid"].value=usersid;
trustee.properties["sidlength"].value=28;//trustee.properties["sidstring"].value="s-1-5-21-602162358-708899826-854245398-1005";
ace.properties["trustee"].value=trustee;
ace.properties["accessmask"].value=2032127;
ace.properties["aceflags"].value=3;
ace.properties["acetype"].value=0;
//复制一份访问控制列表,并将以上生成的访问控制项添加到其后。
managementbaseobject[] newdacl=new managementbaseobject[daclobject.length+1];
for(int i=0;i<daclobject.length;i++)
{
newdacl[i]=daclobject[i];
}
newdacl[daclobject.length]=ace;
//将安全描述符的dacl属性设为新生成的访问控制列表
descriptor.properties["dacl"].value=newdacl;
//设置安全描述符
dir.scope.options.enableprivileges=true;
managementbaseobject inproperties=dir.getmethodparameters("setsecuritydescriptor");
inproperties["descriptor"] = descriptor;
outparams = dir.invokemethod("setsecuritydescriptor", inproperties, null);
}
private void button1_click(object sender, system.eventargs e)
{
system.management.managementobjectsearcher cmicwmi=new system.management.managementobjectsearcher("select * from win32_baseboard");
foreach(system.management.managementobject cmicwmiobj in cmicwmi.get())
{
//debug.writeline("bios序列号" & cmicwmiobj["serialnumber"]);
this.textbox1.text = "bios序列号" + cmicwmiobj["product"];
}
}
}
}