Merge <domain> and <user> to support User Principal Name

pull/557/head
NextTurn 2018-11-27 00:00:00 +08:00 committed by Next Turn
parent eac8b8ca45
commit 8beb21568e
6 changed files with 15 additions and 23 deletions

View File

@ -211,7 +211,7 @@ namespace winsw
{ {
if (descriptor.HasServiceAccount()) if (descriptor.HasServiceAccount())
{ {
username = descriptor.ServiceAccountUser; username = descriptor.ServiceAccountUserName;
password = descriptor.ServiceAccountPassword; password = descriptor.ServiceAccountPassword;
allowServiceLogonRight = descriptor.AllowServiceAcountLogonRight; allowServiceLogonRight = descriptor.AllowServiceAcountLogonRight;
} }
@ -219,7 +219,7 @@ namespace winsw
if (allowServiceLogonRight) if (allowServiceLogonRight)
{ {
Security.AddServiceLogonRight(descriptor.ServiceAccountDomain!, descriptor.ServiceAccountName!); Security.AddServiceLogonRight(descriptor.ServiceAccountUserName!);
} }
using Service sc = scm.CreateService( using Service sc = scm.CreateService(

View File

@ -23,7 +23,7 @@ namespace winsw.Configuration
// Installation // Installation
public bool AllowServiceAcountLogonRight => false; public bool AllowServiceAcountLogonRight => false;
public string? ServiceAccountPassword => null; public string? ServiceAccountPassword => null;
public string? ServiceAccountUser => null; public string? ServiceAccountUserName => null;
public Native.SC_ACTION[] FailureActions => new Native.SC_ACTION[0]; public Native.SC_ACTION[] FailureActions => new Native.SC_ACTION[0];
public TimeSpan ResetFailureAfter => TimeSpan.FromDays(1); public TimeSpan ResetFailureAfter => TimeSpan.FromDays(1);

View File

@ -20,7 +20,7 @@ namespace winsw.Configuration
// Installation // Installation
bool AllowServiceAcountLogonRight { get; } bool AllowServiceAcountLogonRight { get; }
string? ServiceAccountPassword { get; } string? ServiceAccountPassword { get; }
string? ServiceAccountUser { get; } string? ServiceAccountUserName { get; }
Native.SC_ACTION[] FailureActions { get; } Native.SC_ACTION[] FailureActions { get; }
TimeSpan ResetFailureAfter { get; } TimeSpan ResetFailureAfter { get; }

View File

@ -8,9 +8,9 @@ namespace winsw.Native
internal static class Security internal static class Security
{ {
/// <exception cref="Win32Exception" /> /// <exception cref="Win32Exception" />
internal static void AddServiceLogonRight(string domain, string user) internal static void AddServiceLogonRight(string userName)
{ {
IntPtr sid = GetAccountSid(domain, user); IntPtr sid = GetAccountSid(userName);
try try
{ {
@ -24,17 +24,16 @@ namespace winsw.Native
} }
/// <exception cref="Win32Exception" /> /// <exception cref="Win32Exception" />
private static IntPtr GetAccountSid(string domain, string user) private static IntPtr GetAccountSid(string accountName)
{ {
int sidSize = 0; int sidSize = 0;
int domainNameLength = 0; int domainNameLength = 0;
if (domain == ".") if (accountName.StartsWith(".\\"))
{ {
domain = Environment.MachineName; accountName = Environment.MachineName + accountName.Substring(1);
} }
string accountName = domain + "\\" + user;
_ = LookupAccountName(null, accountName, IntPtr.Zero, ref sidSize, IntPtr.Zero, ref domainNameLength, out _); _ = LookupAccountName(null, accountName, IntPtr.Zero, ref sidSize, IntPtr.Zero, ref domainNameLength, out _);
IntPtr sid = Marshal.AllocHGlobal(sidSize); IntPtr sid = Marshal.AllocHGlobal(sidSize);

View File

@ -645,17 +645,13 @@ namespace winsw
protected string? AllowServiceLogon => GetServiceAccountPart("allowservicelogon"); protected string? AllowServiceLogon => GetServiceAccountPart("allowservicelogon");
protected internal string? ServiceAccountDomain => GetServiceAccountPart("domain");
protected internal string? ServiceAccountName => GetServiceAccountPart("user");
public string? ServiceAccountPassword => GetServiceAccountPart("password"); public string? ServiceAccountPassword => GetServiceAccountPart("password");
public string? ServiceAccountUser => ServiceAccountName is null ? null : (ServiceAccountDomain ?? ".") + "\\" + ServiceAccountName; public string? ServiceAccountUserName => GetServiceAccountPart("username");
public bool HasServiceAccount() public bool HasServiceAccount()
{ {
return !string.IsNullOrEmpty(ServiceAccountName); return !string.IsNullOrEmpty(ServiceAccountUserName);
} }
public bool AllowServiceAcountLogonRight public bool AllowServiceAcountLogonRight

View File

@ -30,8 +30,7 @@ $@"<service>
<arguments>My Arguments</arguments> <arguments>My Arguments</arguments>
<log mode=""roll""></log> <log mode=""roll""></log>
<serviceaccount> <serviceaccount>
<domain>{Domain}</domain> <username>{Domain}\{Username}</username>
<user>{Username}</user>
<password>{Password}</password> <password>{Password}</password>
<allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon> <allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon>
</serviceaccount> </serviceaccount>
@ -60,8 +59,7 @@ $@"<service>
<startmode>roll</startmode> <startmode>roll</startmode>
<log mode=""roll""></log> <log mode=""roll""></log>
<serviceaccount> <serviceaccount>
<domain>{Domain}</domain> <username>{Domain}\{Username}</username>
<user>{Username}</user>
<password>{Password}</password> <password>{Password}</password>
<allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon> <allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon>
</serviceaccount> </serviceaccount>
@ -86,8 +84,7 @@ $@"<service>
<startmode>manual</startmode> <startmode>manual</startmode>
<log mode=""roll""></log> <log mode=""roll""></log>
<serviceaccount> <serviceaccount>
<domain>{Domain}</domain> <username>{Domain}\{Username}</username>
<user>{Username}</user>
<password>{Password}</password> <password>{Password}</password>
<allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon> <allowservicelogon>{AllowServiceAccountLogonRight}</allowservicelogon>
</serviceaccount> </serviceaccount>
@ -116,7 +113,7 @@ $@"<service>
public void VerifyUsername() public void VerifyUsername()
{ {
Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + _extendedServiceDescriptor.WorkingDirectory); Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + _extendedServiceDescriptor.WorkingDirectory);
Assert.That(_extendedServiceDescriptor.ServiceAccountUser, Is.EqualTo(Domain + "\\" + Username)); Assert.That(_extendedServiceDescriptor.ServiceAccountUserName, Is.EqualTo(Domain + "\\" + Username));
} }
[Test] [Test]