Merge pull request #1669 from andschwa/safe-hostname

Safe hostname
This commit is contained in:
Dongbo Wang 2016-08-08 11:10:53 -07:00 committed by GitHub
commit ba2cd1d2b7
5 changed files with 208 additions and 252 deletions

View file

@ -1026,11 +1026,20 @@ namespace System.Management.Automation
{
return Platform.NonWindowsGetDomainName();
}
}
}
internal static string WinGetUserName()
/// <summary>
/// UserName
/// </summary>
public static string UserName
{
get
{
#if UNIX
return Platform.Unix.UserName;
#else
StringBuilder domainName = new StringBuilder(1024);
uint domainNameLen = (uint)domainName.Capacity;
@ -1045,23 +1054,7 @@ namespace System.Management.Automation
}
}
return string.Empty;
}
/// <summary>
/// UserName
/// </summary>
public static string UserName
{
get
{
if (Platform.IsWindows)
{
return WinGetUserName();
}
else
{
return Platform.NonWindowsGetUserName();
}
#endif
}
}
@ -1146,13 +1139,36 @@ namespace System.Management.Automation
/// </returns>
private static string InternalGetFolderPath(SpecialFolder folder)
{
if (!Platform.IsWindows)
{
return Platform.NonWindowsGetFolderPath(folder);
}
// The API 'SHGetFolderPath' is not available on OneCore, so we have to rely on environment variables
string folderPath = null;
#if UNIX
switch (folder)
{
case SpecialFolder.ProgramFiles:
folderPath = "/bin";
if (!System.IO.Directory.Exists(folderPath)) { folderPath = null; }
break;
case SpecialFolder.ProgramFilesX86:
folderPath = "/usr/bin";
if (!System.IO.Directory.Exists(folderPath)) { folderPath = null; }
break;
case SpecialFolder.System:
case SpecialFolder.SystemX86:
folderPath = "/sbin";
if (!System.IO.Directory.Exists(folderPath)) { folderPath = null; }
break;
case SpecialFolder.Personal:
folderPath = System.Environment.GetEnvironmentVariable("HOME");
break;
case SpecialFolder.LocalApplicationData:
folderPath = System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), ".config");
if (!System.IO.Directory.Exists(folderPath)) { System.IO.Directory.CreateDirectory(folderPath); }
break;
default:
throw new NotSupportedException();
}
#else
string systemRoot = null;
string userProfile = null;
@ -1219,6 +1235,7 @@ namespace System.Management.Automation
default:
throw new NotSupportedException();
}
#endif
return folderPath ?? string.Empty;
}

View file

@ -14,18 +14,10 @@ using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System.IO;
#if CORECLR
// SMA.Environment is only available on CoreCLR
using SpecialFolder = System.Management.Automation.Environment.SpecialFolder;
#endif
namespace System.Management.Automation
{
/// <summary>
/// These are platform abstractions and platform specific implementations
///
/// All these properties are calling into platform specific static classes, to make
/// sure the platform implementations are switched at runtime (including pinvokes).
/// </summary>
public static class Platform
{
@ -36,11 +28,11 @@ namespace System.Management.Automation
{
get
{
#if CORECLR
#if CORECLR
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
#else
#else
return false;
#endif
#endif
}
}
@ -51,11 +43,11 @@ namespace System.Management.Automation
{
get
{
#if CORECLR
#if CORECLR
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#else
#else
return false;
#endif
#endif
}
}
@ -66,11 +58,11 @@ namespace System.Management.Automation
{
get
{
#if CORECLR
#if CORECLR
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#else
#else
return true;
#endif
#endif
}
}
@ -81,11 +73,11 @@ namespace System.Management.Automation
{
get
{
#if CORECLR
#if CORECLR
return true;
#else
#else
return false;
#endif
#endif
}
}
@ -271,13 +263,6 @@ namespace System.Management.Automation
return Unix.NativeMethods.GetUserFromPid(path);
}
#if CORECLR
internal static string NonWindowsGetFolderPath(SpecialFolder folder)
{
return Unix.GetFolderPath(folder);
}
#endif
internal static string NonWindowsInternalGetLinkType(FileSystemInfo fileInfo)
{
if (NonWindowsIsSymLink(fileInfo))
@ -312,37 +297,24 @@ namespace System.Management.Automation
internal static string NonWindowsGetDomainName()
{
string fullyQualifiedName = Unix.NativeMethods.GetFullyQualifiedName();
if (string.IsNullOrEmpty(fullyQualifiedName))
string name = Unix.NativeMethods.GetFullyQualifiedName();
if (!string.IsNullOrEmpty(name))
{
int lastError = Marshal.GetLastWin32Error();
throw new InvalidOperationException("Unix.NonWindowsGetDomainName error: " + lastError);
}
int index = fullyQualifiedName.IndexOf('.');
// name is hostname.domainname, so extract domainname
int index = name.IndexOf('.');
if (index >= 0)
{
return fullyQualifiedName.Substring(index + 1);
return name.Substring(index + 1);
}
return "";
}
internal static string NonWindowsGetUserName()
{
return Unix.UserName;
// if the domain name could not be found, do not throw, just return empty
return string.Empty;
}
// Hostname in this context seems to be the FQDN
internal static string NonWindowsGetHostName()
{
string hostName = Unix.NativeMethods.GetFullyQualifiedName();
if (string.IsNullOrEmpty(hostName))
{
int lastError = Marshal.GetLastWin32Error();
throw new InvalidOperationException("Unix.NonWindowsHostName error: " + lastError);
}
return hostName;
return Unix.NativeMethods.GetFullyQualifiedName() ?? string.Empty;
}
internal static bool NonWindowsIsFile(string path)
@ -365,7 +337,6 @@ namespace System.Management.Automation
// TODO:PSL clean this up
return 0;
}
}
internal static class Unix
{
@ -377,13 +348,8 @@ namespace System.Management.Automation
if (string.IsNullOrEmpty(s_userName))
{
s_userName = NativeMethods.GetUserName();
if (string.IsNullOrEmpty(s_userName))
{
int lastError = Marshal.GetLastWin32Error();
throw new InvalidOperationException("Unix.UserName error: " + lastError);
}
}
return s_userName;
return s_userName ?? string.Empty;
}
}
@ -406,41 +372,6 @@ namespace System.Management.Automation
}
}
#if CORECLR
public static string GetFolderPath(SpecialFolder folder)
{
string s = null;
switch (folder)
{
case SpecialFolder.ProgramFiles:
s = "/bin";
break;
case SpecialFolder.ProgramFilesX86:
s = "/usr/bin";
break;
case SpecialFolder.System:
s = "/sbin";
break;
case SpecialFolder.SystemX86:
s = "/sbin";
break;
case SpecialFolder.Personal:
s = System.Environment.GetEnvironmentVariable("HOME");
break;
case SpecialFolder.LocalApplicationData:
s = System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), ".config");
if (!System.IO.Directory.Exists(s))
{
System.IO.Directory.CreateDirectory(s);
}
break;
default:
throw new NotSupportedException();
}
return s;
}
#endif
public static bool IsHardLink(ref IntPtr handle)
{
// TODO:PSL implement using fstat to query inode refcount to see if it is a hard link
@ -572,4 +503,5 @@ namespace System.Management.Automation
internal static extern bool IsDirectory([MarshalAs(UnmanagedType.LPStr)]string filePath);
}
}
}
} // namespace System.Management.Automation

View file

@ -6,6 +6,10 @@ using Dbg = System.Management.Automation.Diagnostics;
using System.Runtime.Serialization;
using System.Collections.Generic;
#if CORECLR
using Environment = System.Management.Automation.Environment;
#endif
namespace System.Management.Automation
{
/// <summary>
@ -38,15 +42,12 @@ namespace System.Management.Automation
this.TimeGenerated = DateTime.Now;
this.Tags = new List<string>();
if (Platform.IsWindows)
{
// domain\user on Windows, just user on Unix
#if UNIX
this.User = Platform.Unix.UserName;
#else
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
else
{
this.User = Platform.NonWindowsGetUserName();
}
// Porting note: PsUtils.GetHostName() already handles platform specifics
#endif
this.Computer = PsUtils.GetHostName();
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
this.NativeThreadId = PsUtils.GetNativeThreadId();

View file

@ -3148,7 +3148,13 @@ namespace System.Management.Automation.Runspaces
// otherwise use the current user name.
var userName = (!string.IsNullOrEmpty(this.UserDriveUserName)) ?
this.UserDriveUserName :
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// domain\user on Windows, just user on Unix
#if UNIX
Platform.Unix.UserName
#else
System.Security.Principal.WindowsIdentity.GetCurrent().Name
#endif
;
// Ensure that user name contains no invalid path characters.
// MSDN indicates that logon names cannot contain any of these invalid characters,

View file

@ -34,7 +34,7 @@ namespace PSTests
// The process should return an exit code of 0 on success
Assert.Equal(0, process.ExitCode);
// It should be the same as what our platform code returns
Assert.Equal(username, Platform.NonWindowsGetUserName());
Assert.Equal(username, Platform.Unix.UserName());
}
}