473,756 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Network share permissions with WMI

Hi guys,

So far I've spent about a week hacking away at this code, and I just
can't get it to add an ACE to a the DACL for a network share using WMI.

Just to set the scene, I'm trying to add an ACL from machine A
(workstation; Saturn) and set it to a UNC path
(\\Mercury\Inet pub\Websites\Lo calUser\Test) on machine B (server;
Mercury).

Neither of these machines are on a domain, and the trustee for the new
ACL is a local user on machine B (server; Mercury), lets call him
'test' for now.

I don't want to use xcacls because it's a bit of a hack, and the ADSI
code from Microsoft looks a little offputting as it uses COM
(http://support.microsoft.com/kb/899553/EN-US/).

At this point I'm able to use the following code to apply permissions
to a local resource on machine A (e.g. C:\Test), however when I try it
on a UNC path it throws a ManagementExcep tion with the message "Not
Found", which isn't very useful.

I can only presume it's complaing about the UNC path. I've tried
doubling up the slashes, and just having single slashes (which makes no
difference).

// Works when server name is ".", "SATURN" but not "MERCURY".
ManagementScope scope = new ManagementScope (@"\\" + ServerName +
@"\root\cimv2") ;

// Works when fileName is local directory, but not UNC path.
ManagementPath path = new ManagementPath( );
path.RelativePa th = @"Win32_Logical FileSecuritySet ting.Path="
+ "'" + fileName + "'";

ManagementObjec t fileSecurity = new ManagementObjec t(
scope, path, null);

// When used with UNC path, exception with "Not Found" is thrown.
ManagementBaseO bject outParams =
(ManagementBase Object)fileSecu rity.InvokeMeth od(
"GetSecurityDes criptor", null, null);

// Get security descriptor and DACL for specified file.
ManagementBaseO bject descriptor =
(ManagementBase Object)outParam s.Properties["Descriptor "].Value;
ManagementBaseO bject[] dacl =
(ManagementBase Object[])descriptor.Pro perties["Dacl"].Value;

// Get the user account to be trustee.
ManagementObjec t userAccount = new ManagementClass (scope,
new ManagementPath( "Win32_Trustee" ), null);
userAccount.Pro perties["Name"].Value = account;

// Create a new ACE for the descriptor.
ManagementObjec t newAce = new ManagementClass (scope,
new ManagementPath( "Win32_ACE" ), null);
newAce.Properti es["Trustee"].Value = userAccount;

// Low level ace flags.
int FILE_READ_DATA = 0x0;
int FILE_WRITE_DATA = 0x1;
int FILE_APPEND_DAT A = 0x4;
int DELETE = 0x10000;

// Translate FileSystemRight s to flags.
switch (accessRights)
{
case FileSystemRight s.Read:
newAce.Properti es["AccessMask "].Value = FILE_READ_DATA;
break;

case FileSystemRight s.Modify:
newAce.Properti es["AccessMask "].Value = FILE_READ_DATA
| FILE_WRITE_DATA | FILE_APPEND_DAT A | DELETE;
break;
}

// ACL will be inherited.
newAce.Properti es["AceFlags"].Value = 0x10;

// Allow access to resource.
newAce.Properti es["AceType"].Value = 0;

// Add ACE to DACL and set to descriptor.
ArrayList daclArray = new ArrayList(dacl) ;
daclArray.Add(n ewAce);

descriptor.Prop erties["Dacl"].Value = daclArray.ToArr ay();

// User SetSecurityDesc riptor to apply the descriptor.
ManagementBaseO bject inParams =
fileSecurity.Ge tMethodParamete rs("SetSecurity Descriptor");
inParams["Descriptor "] = descriptor;
fileSecurity.In vokeMethod("Set SecurityDescrip tor", inParams, null);

Jan 10 '06 #1
2 14979
If you connect to MERCURY using ManagementScope , you are effectively
accessing MERCURY's local drives. That means that you should specify the
local path and not a UNC path when executing path.RelativePa th =
@"Win32_Logical FileSecuritySet ting.Path=...

If you need to set the ACL's on the "share" you need to query the share
using it's name and look for it's associated
Win32_LogicalSh areSecuritySett ing. Once you have this one you can set the
security for the share using the same technique as for a local filz objzct.
Willy.

<ma**@rensoft.n et> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
| Hi guys,
|
| So far I've spent about a week hacking away at this code, and I just
| can't get it to add an ACE to a the DACL for a network share using WMI.
|
| Just to set the scene, I'm trying to add an ACL from machine A
| (workstation; Saturn) and set it to a UNC path
| (\\Mercury\Inet pub\Websites\Lo calUser\Test) on machine B (server;
| Mercury).
|
| Neither of these machines are on a domain, and the trustee for the new
| ACL is a local user on machine B (server; Mercury), lets call him
| 'test' for now.
|
| I don't want to use xcacls because it's a bit of a hack, and the ADSI
| code from Microsoft looks a little offputting as it uses COM
| (http://support.microsoft.com/kb/899553/EN-US/).
|
| At this point I'm able to use the following code to apply permissions
| to a local resource on machine A (e.g. C:\Test), however when I try it
| on a UNC path it throws a ManagementExcep tion with the message "Not
| Found", which isn't very useful.
|
| I can only presume it's complaing about the UNC path. I've tried
| doubling up the slashes, and just having single slashes (which makes no
| difference).
|
| // Works when server name is ".", "SATURN" but not "MERCURY".
| ManagementScope scope = new ManagementScope (@"\\" + ServerName +
| @"\root\cimv2") ;
|
| // Works when fileName is local directory, but not UNC path.
| ManagementPath path = new ManagementPath( );
| path.RelativePa th = @"Win32_Logical FileSecuritySet ting.Path="
| + "'" + fileName + "'";
|
| ManagementObjec t fileSecurity = new ManagementObjec t(
| scope, path, null);
|
| // When used with UNC path, exception with "Not Found" is thrown.
| ManagementBaseO bject outParams =
| (ManagementBase Object)fileSecu rity.InvokeMeth od(
| "GetSecurityDes criptor", null, null);
|
| // Get security descriptor and DACL for specified file.
| ManagementBaseO bject descriptor =
| (ManagementBase Object)outParam s.Properties["Descriptor "].Value;
| ManagementBaseO bject[] dacl =
| (ManagementBase Object[])descriptor.Pro perties["Dacl"].Value;
|
| // Get the user account to be trustee.
| ManagementObjec t userAccount = new ManagementClass (scope,
| new ManagementPath( "Win32_Trustee" ), null);
| userAccount.Pro perties["Name"].Value = account;
|
| // Create a new ACE for the descriptor.
| ManagementObjec t newAce = new ManagementClass (scope,
| new ManagementPath( "Win32_ACE" ), null);
| newAce.Properti es["Trustee"].Value = userAccount;
|
| // Low level ace flags.
| int FILE_READ_DATA = 0x0;
| int FILE_WRITE_DATA = 0x1;
| int FILE_APPEND_DAT A = 0x4;
| int DELETE = 0x10000;
|
| // Translate FileSystemRight s to flags.
| switch (accessRights)
| {
| case FileSystemRight s.Read:
| newAce.Properti es["AccessMask "].Value = FILE_READ_DATA;
| break;
|
| case FileSystemRight s.Modify:
| newAce.Properti es["AccessMask "].Value = FILE_READ_DATA
| | FILE_WRITE_DATA | FILE_APPEND_DAT A | DELETE;
| break;
| }
|
| // ACL will be inherited.
| newAce.Properti es["AceFlags"].Value = 0x10;
|
| // Allow access to resource.
| newAce.Properti es["AceType"].Value = 0;
|
| // Add ACE to DACL and set to descriptor.
| ArrayList daclArray = new ArrayList(dacl) ;
| daclArray.Add(n ewAce);
|
| descriptor.Prop erties["Dacl"].Value = daclArray.ToArr ay();
|
| // User SetSecurityDesc riptor to apply the descriptor.
| ManagementBaseO bject inParams =
| fileSecurity.Ge tMethodParamete rs("SetSecurity Descriptor");
| inParams["Descriptor "] = descriptor;
| fileSecurity.In vokeMethod("Set SecurityDescrip tor", inParams, null);
|
Jan 10 '06 #2
Hi Willy,

Sorry, I just realised that share security exists as well as file
security. I actually meant altering a Win32_LogicalFi leSecuritySetti ng
through a UNC path.

I'm trying to produce the software in such a way that it will run
completely over UNC and not local file paths. This is so I can develop
on a workstation using a virtual server for testing. But then, roll the
application out on to a production server. I'd like to avoid using
local file paths to keep everything simple... Is a good idea?

Also, I tried accessing the Win32_LogicalFi leSecuritySetti ng via a UNC
path, without specifying a server and it threw the same "Not Found"
error as before.

Perhaps you could show me a snippet of code to enumerate the DACL via a
UNC path?

Thanks Willy.

Nick

Jan 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1565
by: Rick Csucsai | last post by:
I have an ASP app that calls an object on the web server which goes out to a mapped network drive to retrieve a file from a W2K file server. My current web server is a W2K. The share on the file server as well as the NTFS permissions on the server are wide open (Everyone Full Control) but still require me to run the web server under an account other than the IUSR account for the object to be able to access the file. None the less, it works...
1
6571
by: brian.oneil2 | last post by:
Is there a way to install this onto a network file share and allow a team to access it? I would say share a CD from a networked CD drive, but there are multiple CD's that would have to be inserted. TIA, Brian
1
4450
by: edge | last post by:
hi, here it is my problem. My console app, reads a text file where it grabs username/password. Next, my app creates a .BAT file to trigger the command ftp:\\user:password@ftphomeaddress. Then, I use Process() to start the batch. In my local machine, the app runs just fine. But the users
4
5906
by: Scott Nicholson | last post by:
I've got a site set up that uses a network share as it's home directory. Simple stuff is working fine. When I try to put a database in there, though, I run into problems. I'm using: dim dbconn,sql,dbcomm dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("test.mdb") & ";")
5
10180
by: Josh Rolfe | last post by:
I have a page in classic asp that accces a network drive, The code is as follows: <% dim fso dim objFolder set fso=server.createObject("Scripting.FileSystemObject") set objFolder=fso.GetFolder("f:\") for each objFile in objFolder.files response.write objFile.name & "<br>" next
2
2071
by: Johnny Fugazzi | last post by:
I would like to access a network share from my vb.net application. I do not want to map a drive to the share, however. I would also like to specifiy a user credential to use when connecting to the share, as the user running the application will not have security rights to hit the share themselves. Any pointers on this?
3
5057
by: musosdev | last post by:
Hi guys Okay, I've setup my projects to open and compile fine in VS2005 using FPSE and remote web, but it's *really* slow. So I thought I'd have a go at doing it the normal way, by loading from the network share. It loads in VS2005 fine, and I can edit and save code changes etc, but when I try and Build the solution, I get the following error... An error occured loading a configuration file: Failed to start monitoring
2
2423
by: Michael | last post by:
We have an ASP.NET 2.0 web application running on a Windows 2003 domain controller. Part of that application needs to read and write files from and to a network share ( living on a MAC Xserveraid) on the same network segment. I mapped a drive on the server to that share. When mapping the drive, I could enter access credentials for the shares (an account from the Mac side that does not exist in the windows domain). Using Windows Explorer,...
1
1713
by: =?Utf-8?B?aGVjc2FuMDc=?= | last post by:
Hello I am fairly new to .NET Development. I need to query folders within a network drive and return some metadata related to the directories. For instance, I might have the following path: \\MyServer\Tech I would want to get the size and name of all the folders inside this folder. I tried using a simple DirectoryInfo object, but I am getting AccessDeniedException. I know there has to be a better way of doing this using Impersonation....
1
3756
by: Raymond Du | last post by:
Hi, I try to use ASP.Net 2.0 FileUpload control to upload files. The page is working fine when I upload files and save them into my local computer, but fails when the files are to be saved to a network drive. I believe this is permission issue, the account on my local computer does not have permissions to write to network drive. Can someone tell me how resolve this problem? I already went to IIS and impersonate anonymous access...
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.