public void SetAclDirectory(string path, FileSystemRights rights) { if (path is null) { throw new ArgumentNullException(nameof(path)); } try { Console.WriteLine((InstallMode == InstallMode.Install ? "# set acl on directory: " : "# remove acl on directory: ") + path); if (InstallMode == InstallMode.Install) { CreatePath(path); DirectorySecurity acl = Directory.GetAccessControl(path); acl.SetAccessRule(new FileSystemAccessRule(AccountIdentity, rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); Directory.SetAccessControl(path, acl); } else { if (Directory.Exists(path)) { DirectorySecurity acl = Directory.GetAccessControl(path); acl.PurgeAccessRules(AccountIdentity); Directory.SetAccessControl(path, acl); } } } catch (Exception err) { if (err is DirectoryNotFoundException) { err = new DirectoryNotFoundException("Directory not found."); } err = new InstallerException((InstallMode == InstallMode.Install ? "SetAclDirectory('" + path + "') failed." : "RemoveAclDirectory('" + path + "') failed."), err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }
public void SetAclFile(string path, FileSystemRights rights) { if (path is null) { throw new ArgumentNullException(nameof(path)); } try { Console.WriteLine((InstallMode == InstallMode.Install ? "# set acl on file: " : "# remove acl on file: ") + path); if (InstallMode == InstallMode.Install || File.Exists(path)) { FileSecurity acl = File.GetAccessControl(path); if (InstallMode == InstallMode.Install) { acl.SetAccessRule(new FileSystemAccessRule(AccountIdentity, rights, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow)); } else { acl.PurgeAccessRules(AccountIdentity); } File.SetAccessControl(path, acl); } } catch (Exception err) { if (err is FileNotFoundException) { err = new FileNotFoundException("File not found."); } err = new InstallerException((InstallMode == InstallMode.Install ? "SetAclFile('" + path + "') failed." : "RemoveAclFile('" + path + "') failed."), err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }
public void AccountLSASetServiceLogonRigh() { try { Console.WriteLine("# Set lsa policies SeServiceLogonRight on user: "******"SeServiceLogonRight"); } catch (Exception err) { err = new InstallerException("AccountLSASetServiceLogonRigh('" + AccountName + "') failed.", err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }
public void CreateEventSource() { try { Console.WriteLine("# Create event source: " + ServiceName); if (!System.Diagnostics.EventLog.SourceExists(ServiceName)) { System.Diagnostics.EventLog.CreateEventSource(ServiceName, ServiceBase.EventLogName); } } catch (Exception err) { err = new InstallerException("CreateEventSource('" + ServiceName + "') failed.", err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }
public void FirewallRemoveRules() { try { PowerShell("$program = \"" + ProgramExe + "\"\r\n" + "foreach ($rule in @(Get-NetFirewallRule -All)) {\r\n" + " if (@(Get-NetFirewallApplicationFilter -AssociatedNetFirewallRule $rule).Where({($_.Program -eq $program)})) {\r\n" + " Write-Output (\"# drop firewall rule: \" + $rule.DisplayName)\r\n" + " Remove-NetFirewallRule -Name $rule.Name\r\n" + " }\r\n" + "}"); } catch (Exception err) { err = new InstallerException("FirewallRemoveRules failed.", err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }
public void DatabaseLoginUser(string server, string database, string role) { if (server is null) { throw new ArgumentNullException(nameof(server)); } if (database is null) { throw new ArgumentNullException(nameof(database)); } if (role is null) { throw new ArgumentNullException(nameof(role)); } try { if (AccountName.ToUpperInvariant() != "NT SERVICE") { return; } string accountName = "NT SERVICE\\" + ServiceName; using (SqlConnection sqlConnection = new SqlConnection("Server=" + server + ";Database=" + database + ";Current Language=us_english;Connection Reset=false;Connect Timeout=15;Pooling=No;Trusted_Connection=true")) { sqlConnection.Open(); using (var sqlCmd = new SqlCommand() { Connection = sqlConnection, CommandType = System.Data.CommandType.Text }) { Console.WriteLine("# database drop user: "******"IF EXISTS (SELECT * FROM sys.sysusers WHERE [name] = '" + accountName.Replace("'", "''") + "') DROP USER [" + accountName.Replace("]", "[]") + "]"; sqlCmd.ExecuteNonQuery(); } using (var sqlCmd = new SqlCommand() { Connection = sqlConnection, CommandType = System.Data.CommandType.Text }) { Console.WriteLine("# database drop login: "******"IF EXISTS (SELECT * FROM master.sys.server_principals WHERE [name] = '" + accountName.Replace("'", "''") + "') DROP LOGIN [" + accountName.Replace("]", "[]") + "]"; sqlCmd.ExecuteNonQuery(); } if (InstallMode == InstallMode.Install) { using (var sqlCmd = new SqlCommand() { Connection = sqlConnection, CommandType = System.Data.CommandType.Text }) { Console.WriteLine("# database create login: "******"CREATE LOGIN [" + accountName.Replace("]", "[]") + "] FROM WINDOWS"; sqlCmd.ExecuteNonQuery(); } using (var sqlCmd = new SqlCommand() { Connection = sqlConnection, CommandType = System.Data.CommandType.Text }) { Console.WriteLine("# database create user: "******"CREATE USER [" + accountName.Replace("]", "[]") + "]"; sqlCmd.ExecuteNonQuery(); } using (var sqlCmd = new SqlCommand() { Connection = sqlConnection, CommandType = System.Data.CommandType.Text }) { Console.WriteLine("# database add user to role: " + role); sqlCmd.CommandText = "ALTER ROLE [" + role.Replace("]", "[]") + "] ADD MEMBER [" + accountName.Replace("]", "[]") + "]"; sqlCmd.ExecuteNonQuery(); } } } } catch (Exception err) { err = new InstallerException("DatabaseLoginUser('" + role + "') failed.", err); if (InstallMode == InstallMode.Install) { throw err; } else { DisplayError(err); } } }