//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace FixAcl
{
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
///
/// Installer that fixes ACL on Application Data directory so data files can be updated by normal users.
///
[RunInstaller(true)]
public partial class Installer1 : Installer
{
///
/// Initializes a new instance of the class.
///
public Installer1()
{
this.InitializeComponent();
}
///
/// Perform installation and set ACL data.
///
/// stateSaver dictionary data.
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
// This gets the named parameters passed in from your custom action
string folder = Context.Parameters["folder"];
// This gets the "Authenticated Users" group, no matter what it's called
// SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
// Create the rules
FileSystemAccessRule writerule1 = new FileSystemAccessRule(sid, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
FileSystemAccessRule writerule2 = new FileSystemAccessRule(sid, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow);
FileSystemAccessRule writerule3 = new FileSystemAccessRule(sid, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
{
// Get your file's ACL
DirectorySecurity fsecurity = Directory.GetAccessControl(folder);
fsecurity.PurgeAccessRules(sid);
// Add the new rule to the ACL
fsecurity.AddAccessRule(writerule1);
fsecurity.AddAccessRule(writerule2);
fsecurity.AddAccessRule(writerule3);
// Set the ACL back to the file
Directory.SetAccessControl(folder, fsecurity);
}
// Explicitly call the overriden method to properly return control to the installer
base.Install(stateSaver);
}
///
/// Commit state.
///
/// Saved state.
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
// System.Diagnostics.Process.Start("http://www.microsoft.com");
}
///
/// Rollback transaction.
///
/// Saved state.
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
///
/// Uninstall actions.
///
/// Saved state.
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
}
}
}