public NTShare CreateShare(string name, string description, WinAPI.NETAPI32.SHARE_TYPE type, string path) { var newShare = new WinAPI.NETAPI32.SHARE_INFO_502 { shi502_netname = name, shi502_remark = description, shi502_type = (uint)type, shi502_path = path }; uint paramErr; var result = WinAPI.NETAPI32.NetShareAdd(this.Host, 502, ref newShare, out paramErr); if (result != 0) { throw new NetApiException( result, "Unable to create local share '{0}' on host '{1}'", name, Host ); } var share = new NTShare { Host = Host, Name = name }; share.Refresh(); return(share); }
public NTShare[] GetShares(out Exception[] errors) { errors = new Exception[0]; var resumeHandle = 0; var bufPtr = IntPtr.Zero; var localShares = new List <NTShare>(); var errorList = new List <Exception>(); try { int entriesRead; int totalEntries; var result = WinAPI.NETAPI32.NetShareEnum( this.NTCompatibleHostName, 0, ref bufPtr, WinAPI.NETAPI32.MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, ref resumeHandle ); if (result != 0) { throw new NetApiException( result, "Failed to enumerate shares on host '{0}'", Host ); } var iter = bufPtr; var structSize = Marshal.SizeOf(typeof(WinAPI.NETAPI32.SHARE_INFO_0)); var startAddr = bufPtr.ToInt64(); var endAddr = startAddr + entriesRead * structSize; for (var offset = startAddr; offset < endAddr; offset += structSize) { try { var shareInfo = (WinAPI.NETAPI32.SHARE_INFO_0)Marshal.PtrToStructure( new IntPtr(offset), typeof(WinAPI.NETAPI32.SHARE_INFO_0) ); var share = new NTShare { Host = Host, Name = shareInfo.shi0_netname }; share.Refresh(); localShares.Add(share); } catch (Exception error) { errorList.Add(error); } } } finally { if (bufPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(bufPtr); } } errors = errorList.ToArray(); return(localShares.ToArray()); }
public bool TryGetShare(string name, out NTShare share) { share = new NTShare(); if (NTShare.TryLoadLocalShare(NTCompatibleHostName, name, share) == 0) { return(true); } else { share = null; return(false); } }
public void CopyShareSecurity(NTShare sourceShare, NTShare destShare) { ActionObserver.NotifyAction("Copying", "Share Security", sourceShare.FullName, destShare.FullName); try { ShareSecurity sourceSecurity = sourceShare.GetAccessControl(); NTShare.SetAccessControl( destShare.FullName, TranslateShareACL( sourceShare.GetAccessControl() ) ); } catch (Exception error) { ActionObserver.NotifyActionFailed("Copying", "Share Security", sourceShare.FullName, destShare.FullName, error.Message); } }
public void CopyShare(CopyableShare copyableShare) { #region Validation if (!Directory.Exists(copyableShare.DestPath)) { ActionObserver.NotifyError("Cannot copy share '{0}' to '{1}' as the destination path does not exist", copyableShare.SourceShare.FullName, copyableShare.DestPath); return; } #endregion NTShare sourceShare = copyableShare.SourceShare; string destPath = copyableShare.DestPath; ActionObserver.NotifyAction("Copying", "Share", sourceShare.FullName, destPath); try { NTShare destShare; // try to load share at destination server (assumed to be localhost), by name if (!NTHost.CurrentMachine.TryGetShare(sourceShare.Name, out destShare)) { // one doesn't exist so create one destShare = NTHost.CurrentMachine.CreateShare( sourceShare.Name, sourceShare.Description, sourceShare.Type, destPath ); } else { // make sure we loaded one pointing to same directory as we expect if (!Path.Equals(destShare.ServerPath, copyableShare.DestPath)) { // in this branch, a different share of the same name already exists if (AutoResolveShareNameConflicts) { string newName = ResolveShareNameConflict(NTHost.CurrentMachine, sourceShare.Name); ActionObserver.NotifyWarning( "Copying share '{0}\\{1}' as '{2}\\{3}' to avoid name conflict", copyableShare.SourceShare.Host, copyableShare.SourceShare.Name, NTHost.CurrentMachine.Host, newName ); // we want to auto-create a share name destShare = NTHost.CurrentMachine.CreateShare( newName, sourceShare.Description, sourceShare.Type, destPath ); } else { // in this branch, bail out of share copying as share name already taken ActionObserver.NotifyError( "Cannot copy the share '{0}\\{1}' to folder '{2}' as it will conflict with '{3}\\{4}' defined for folder '{5}'", copyableShare.SourceShare.Host, copyableShare.SourceShare.Name, copyableShare.DestPath, destShare.Host, destShare.Name, destShare.ServerPath ); } return; } } // copy details destShare.Name = sourceShare.Name; destShare.Description = sourceShare.Description; destShare.MaxUses = sourceShare.MaxUses; destShare.Password = sourceShare.Password; destShare.Permissions = sourceShare.Permissions; destShare.Reserved = sourceShare.Reserved; destShare.ServerPath = destPath; destShare.Update(); CopyShareSecurity(sourceShare, destShare); // create new share } catch (Exception error) { ActionObserver.NotifyActionFailed("Copying", "Share", copyableShare.SourceShare.FullName, copyableShare.DestPath, error.Message); } }
public void CopyShare(NTShare sourceShare, string destPath) { CopyShare(new CopyableShare(sourceShare, destPath)); }
public CopyableShare(NTShare source, string destPath) { SourceShare = source; DestPath = destPath; }
public CopyableShare(NTShare source) : this(source, source.ServerPath) { }
internal static WinAPI.NETAPI32.NET_API_STATUS TryLoadLocalShare(string host, string name, NTShare share) { WinAPI.NETAPI32.NET_API_STATUS result; var bufPtr = IntPtr.Zero; var securityDescriptorPtr = IntPtr.Zero; try { result = WinAPI.NETAPI32.NetShareGetInfo(host, name, 502, out bufPtr); if (result != WinAPI.NETAPI32.NET_API_STATUS.NERR_Success) { return(result); } var shareInfo = (WinAPI.NETAPI32.SHARE_INFO_502)Marshal.PtrToStructure( bufPtr, typeof(WinAPI.NETAPI32.SHARE_INFO_502) ); share.CurrentUses = (int)shareInfo.shi502_current_uses; share.Description = shareInfo.shi502_remark; share.Host = host; share.MaxUses = (int)shareInfo.shi502_max_uses; share.Name = shareInfo.shi502_netname; share.Password = shareInfo.shi502_passwd; share.ServerPath = shareInfo.shi502_path; share.Permissions = (WinAPI.NETAPI32.GlobalSharePermission)shareInfo.shi502_permissions; share.Reserved = (int)shareInfo.shi502_reserved; share.SecurityDescriptor = shareInfo.shi502_security_descriptor; share.Description = shareInfo.shi502_remark; } finally { if (bufPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(bufPtr); } if (securityDescriptorPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(securityDescriptorPtr); } } return(result); }
public bool ContainsShare(string name) { NTShare share = null; return(TryGetShare(name, out share)); }