
{"id":358,"date":"2009-12-02T17:52:58","date_gmt":"2009-12-02T22:52:58","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=358"},"modified":"2009-12-02T17:52:58","modified_gmt":"2009-12-02T22:52:58","slug":"setting-folder-security-in-msi","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=358","title":{"rendered":"Setting Folder Security in MSI"},"content":{"rendered":"<p>I had a surprisingly difficult time trying to figure out how to set folder permissions in my windows service installer. Here&#8217;s the story: I have a Windows service that writes log information into the Logs directory. If the service is installed under c:\\program files\\company\\product, then it wants to write to c:\\program files\\company\\product\\Logs.<br \/>\n<!--more--><br \/>\nThe service works under &#8220;NETWORK SERVICE&#8221; account, and by default it does not get write access to the Program Files directories. It turned out that I need to create a special installer class to create the Logs folder and change NTFS security on it. Maybe there is a better way to do it, but I am not aware of it. I had to solve the following problems:<\/p>\n<ol>\n<li>creating my installer class<\/li>\n<li>having it called from the main installer<\/li>\n<li>finding installation folder from inside my class<\/li>\n<li>actually setting the security<\/li>\n<\/ol>\n<p>There was surprisingly little accurate information about this on the Internet. Here&#8217;s how I solved those problems:<\/p>\n<h3>Creating Installer Class<\/h3>\n<p>This is easy: just create a class and derive it from Installer (System.Configuration.Install.Installer). Leave it empty and compile the project. I called the class LogInstaller.<\/p>\n<h3>Having your class called from the main installer<\/h3>\n<p>I had my service installer class (created by double-clicking on my Windows Service class, right-clicking and selecting &#8220;Add Installer&#8221;). I have added it as a custom action in my installer project.<\/p>\n<p>Once you have created <b>and compiled<\/b> the LogInstaller class, it appears in the tool box. Just drag it to the design surface of the service installer. This does almost everything, but you will have to manually edit the designer code: locate &#8220;this.Installers.AddRange()&#8221; call in the InitializeComponent() method and add the newly created log installer instance to the list of child installers. NB: this is in the *.designer.cs file for the service installer.<\/p>\n<h3>Find installation folder<\/h3>\n<p>This was the most tricky one, and admittedly I cheated. When your installer class is called, some parameters are passed in this.Context.Paremeters field. This is a string-to-string dictionary. I printed the whole contents of this dictionary, and I found only four parameters: &#8220;action&#8221;, &#8220;installtype&#8221;, &#8220;assemblypath&#8221;, and &#8220;logfile&#8221;, of which only &#8220;assemblypath&#8221; was useful.<\/p>\n<p>Since I knew my assembly is installed in the root directory of the product (c:\\program files\\company\\product), I just used Path.GetDirectoryName(this.Context.Parameters[&#8220;assemblypath&#8221;]) as the application install location. This is not very nice &#8211; what if next time I install the assembly in some subfolder? I will have to modify my log folder installer, and this is a hidden dependency.<\/p>\n<p>The code that handles all that goes into LogDirectoryInstaller.Commit(), by the way.<\/p>\n<h3>Actually setting the security<\/h3>\n<p>Well, there is plenty of information on the Internet for this one. Please find the code of LogDirectoryInstaller below.<\/p>\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt\">\n<div style=\"background-color: #ffffff; overflow: auto; padding: 2px 5px;\"><span style=\"color:#0000ff\">using<\/span> System.Configuration.Install;<br \/>\n<span style=\"color:#0000ff\">using<\/span> System.IO;<br \/>\n<span style=\"color:#0000ff\">using<\/span> System.Security.AccessControl;<\/p>\n<p><span style=\"color:#0000ff\">namespace<\/span> Consulting.Sungard<br \/>\n{<br \/>\n&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">public<\/span> <span style=\"color:#0000ff\">class<\/span> <span style=\"color:#2b91af\">LogDirectoryInstaller<\/span> : <span style=\"color:#2b91af\">Installer<\/span><br \/>\n&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">public<\/span> <span style=\"color:#0000ff\">override<\/span> <span style=\"color:#0000ff\">void<\/span> Install(System.Collections.<span style=\"color:#2b91af\">IDictionary<\/span> stateSaver)<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">base<\/span>.Install(stateSaver);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">string<\/span> logDir = GetLogPath();<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">DirectoryInfo<\/span> dirInfo = <span style=\"color:#2b91af\">Directory<\/span>.CreateDirectory(logDir);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetSecurity(dirInfo);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">private<\/span> <span style=\"color:#0000ff\">void<\/span> SetSecurity(<span style=\"color:#2b91af\">DirectoryInfo<\/span> dirInfo)<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dirInfo.SetAccessControl(GetLogFolderSecurity(dirInfo));<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">private<\/span> <span style=\"color:#2b91af\">DirectorySecurity<\/span> GetLogFolderSecurity(<span style=\"color:#2b91af\">DirectoryInfo<\/span> dirInfo)<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">DirectorySecurity<\/span> security = dirInfo.GetAccessControl();<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">FileSystemAccessRule<\/span> rule = <br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">new<\/span> <span style=\"color:#2b91af\">FileSystemAccessRule<\/span>(<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#a31515\">&quot;NETWORK SERVICE&quot;<\/span>, <br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">FileSystemRights<\/span>.Modify, <br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">InheritanceFlags<\/span>.ContainerInherit | <span style=\"color:#2b91af\">InheritanceFlags<\/span>.ObjectInherit,<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">PropagationFlags<\/span>.None,<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#2b91af\">AccessControlType<\/span>.Allow);<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;security.AddAccessRule(rule);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">return<\/span> security;<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">public<\/span> <span style=\"color:#0000ff\">override<\/span> <span style=\"color:#0000ff\">void<\/span> Commit(System.Collections.<span style=\"color:#2b91af\">IDictionary<\/span> savedState)<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">base<\/span>.Commit(savedState);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">private<\/span> <span style=\"color:#0000ff\">string<\/span> GetLogPath()<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">string<\/span> assemblyPath = Context.Parameters[<span style=\"color:#a31515\">&quot;assemblypath&quot;<\/span>];<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">string<\/span> installDir = <span style=\"color:#2b91af\">Path<\/span>.GetDirectoryName(assemblyPath);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">string<\/span> logDir = <span style=\"color:#2b91af\">Path<\/span>.Combine(installDir, <span style=\"color:#a31515\">&quot;Logs&quot;<\/span>);<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">return<\/span> logDir;<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br \/>\n&#160;&#160;&#160;&#160;}<br \/>\n}<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I had a surprisingly difficult time trying to figure out how to set folder permissions in my windows service installer. Here&#8217;s the story: I have a Windows service that writes <a href=\"https:\/\/ikriv.com\/blog\/?p=358\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","has-more-link","post-358","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=358"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/358\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}