/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Security;
using System.Security.Principal;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using DWORD = System.UInt32;
using BOOL = System.UInt32;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
namespace Microsoft.PowerShell
{
internal static class SecurityUtils
{
///
/// gets the size of a file
///
///
/// path to file
///
/// file size
///
///
///
internal static long GetFileSize(string filePath)
{
long size = 0;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
size = fs.Length;
}
return size;
}
#if false
///
/// throw if file is smaller than 4 bytes in length
///
///
/// path to file
///
/// Does not return a value
///
///
///
internal static void CheckIfFileSmallerThan4Bytes(string filePath)
{
if (GetFileSize(filePath) < 4)
{
string message =
StringUtil.Format(
UtilsStrings.FileSmallerThan4Bytes,
new object[] { filePath }
);
throw PSTraceSource.NewArgumentException(message, "path");
/*
// 2004/10/22-JonN The above form of the constructor
// no longer exists. This should probably be as below,
// however I have not tested this. This method is not
// used so I have removed it.
throw PSTraceSource.NewArgumentException(
"path",
"Utils",
"FileSmallerThan4Bytes",
filePath
);
*/
}
}
#endif
///
/// present a prompt for a SecureString data
///
///
/// ref to host ui interface
///
/// prompt text
///
/// user input as secure string
///
///
///
internal static SecureString PromptForSecureString(PSHostUserInterface hostUI,
string prompt)
{
SecureString ss = null;
hostUI.Write(prompt);
ss = hostUI.ReadLineAsSecureString();
hostUI.WriteLine("");
return ss;
}
#if !CORECLR
///
/// get plain text string from a SecureString
///
/// This function will not be required once all of the methods
/// that we call accept a SecureString. The list below has
/// classes/methods that will be changed to accept a SecureString
/// after Whidbey beta1
///
/// -- X509Certificate2.Import (String, String, X509KeyStorageFlags)
/// (DCR #33007 in the DevDiv Schedule db)
///
/// -- NetworkCredential(string, string);
///
///
///
/// input data
///
/// a string representing clear-text equivalent of ss
///
///
///
[ArchitectureSensitive]
internal static string GetStringFromSecureString(SecureString ss)
{
IntPtr p = Marshal.SecureStringToGlobalAllocUnicode(ss);
string s = Marshal.PtrToStringUni(p);
Marshal.ZeroFreeGlobalAllocUnicode(p);
return s;
}
#endif
/*
///
/// display sec-desc of a file
///
///
/// file security descriptor
///
/// Does not return a value
///
///
///
internal static void ShowFileSd(FileSecurity sd)
{
string userName = null;
FileSystemRights rights = 0;
AccessControlType aceType = 0;
rules = sd.GetAccessRules(true, false, typeof(NTAccount));
foreach (FileSystemAccessRule r in rules)
{
userName = r.IdentityReference.ToString();
aceType = r.AccessControlType;
rights = r.FileSystemRights;
Console.WriteLine("{0} : {1} : {2}",
userName,
aceType.ToString(),
rights.ToString());
}
}
}
*/
///
///
///
///
/// resource string
///
/// error identifier
///
/// replacement params for resource string formatting
///
///
///
///
///
internal static
ErrorRecord CreateFileNotFoundErrorRecord(string resourceStr,
string errorId,
params object[] args)
{
string message =
StringUtil.Format(
resourceStr,
args
);
FileNotFoundException e =
new FileNotFoundException(message);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.ObjectNotFound,
null);
return er;
}
///
///
///
///
/// path that was not found
///
/// error identifier
///
/// ErrorRecord instance
///
///
///
internal static
ErrorRecord CreatePathNotFoundErrorRecord(string path,
string errorId)
{
ItemNotFoundException e =
new ItemNotFoundException(path, "PathNotFound", SessionStateStrings.PathNotFound);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.ObjectNotFound,
null);
return er;
}
///
/// Create an error record for 'operation not supported' condition
///
///
/// resource string
///
/// error identifier
///
/// replacement params for resource string formatting
///
///
///
///
///
internal static
ErrorRecord CreateNotSupportedErrorRecord(string resourceStr,
string errorId,
params object[] args)
{
string message = StringUtil.Format(resourceStr, args);
NotSupportedException e =
new NotSupportedException(message);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.NotImplemented,
null);
return er;
}
///
/// Create an error record for 'operation not supported' condition
///
///
/// exception to include in ErrorRecord
///
/// error identifier
///
///
///
///
///
internal static
ErrorRecord CreateInvalidArgumentErrorRecord(Exception e,
string errorId)
{
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.InvalidArgument,
null);
return er;
}
///
/// convert the specifed provider path to a provider path
/// and make sure that all of the following is true:
/// -- it represents a FileSystem path
/// -- it points to a file
/// -- the file exists
///
///
/// cmdlet instance
///
/// provider path
///
///
/// filesystem path if all conditions are true,
/// null otherwise
///
///
///
///
internal static string GetFilePathOfExistingFile(PSCmdlet cmdlet,
string path)
{
string resolvedProviderPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);
if (Utils.NativeFileExists(resolvedProviderPath))
{
return resolvedProviderPath;
}
else
{
return null;
}
}
}
}