aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Firewall/wixext/FirewallDecompiler.cs
blob: 69f2c3f4dd5ae0ea2cf7d037e8226b60d8d69ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Firewall
{
    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using WixToolset.Data;
    using WixToolset.Data.WindowsInstaller;
    using WixToolset.Extensibility;

    /// <summary>
    /// The decompiler for the WiX Toolset Firewall Extension.
    /// </summary>
    public sealed class FirewallDecompiler : BaseWindowsInstallerDecompilerExtension
    {
        public override IReadOnlyCollection<TableDefinition> TableDefinitions => FirewallTableDefinitions.All;

        /// <summary>
        /// Called at the beginning of the decompilation of a database.
        /// </summary>
        /// <param name="tables">The collection of all tables.</param>
        public override void PreDecompileTables(TableIndexedCollection tables)
        {
        }

        /// <summary>
        /// Decompiles an extension table.
        /// </summary>
        /// <param name="table">The table to decompile.</param>
        public override bool TryDecompileTable(Table table)
        {
            switch (table.Name)
            {
                case "Wix4FirewallException":
                    this.DecompileWixFirewallExceptionTable(table);
                    break;
                default:
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Finalize decompilation.
        /// </summary>
        /// <param name="tables">The collection of all tables.</param>
        public override void PostDecompileTables(TableIndexedCollection tables)
        {
            this.FinalizeFirewallExceptionTable(tables);
        }

        /// <summary>
        /// Decompile the WixFirewallException table.
        /// </summary>
        /// <param name="table">The table to decompile.</param>
        private void DecompileWixFirewallExceptionTable(Table table)
        {
            foreach (Row row in table.Rows)
            {
                var firewallException = new XElement(FirewallConstants.FirewallExceptionName,
                    new XAttribute("Id", row.FieldAsString(0)),
                    new XAttribute("Name", row.FieldAsString(1))
                );

                if (!row.IsColumnEmpty(2))
                {
                    string[] addresses = ((string)row[2]).Split(',');
                    if (addresses.Length == 1)
                    {
                        // special-case the Scope attribute values
                        if (addresses[0] == "*")
                        {
                            firewallException.Add(new XAttribute("Scope", "any"));
                        }
                        else if (addresses[0] == "LocalSubnet")
                        {
                            firewallException.Add(new XAttribute("Scope", "localSubnet"));
                        }
                        else
                        {
                            FirewallDecompiler.AddRemoteAddress(firewallException, addresses[0]);
                        }
                    }
                    else
                    {
                        foreach (string address in addresses)
                        {
                            FirewallDecompiler.AddRemoteAddress(firewallException, address);
                        }
                    }
                }

                if (!row.IsColumnEmpty(3))
                {
                    firewallException.Add(new XAttribute("Port", row.FieldAsString(3)));
                }

                if (!row.IsColumnEmpty(4))
                {
                    switch (Convert.ToInt32(row[4]))
                    {
                        case FirewallConstants.NET_FW_IP_PROTOCOL_TCP:
                            firewallException.Add(new XAttribute("Protocol", "tcp"));
                            break;
                        case FirewallConstants.NET_FW_IP_PROTOCOL_UDP:
                            firewallException.Add(new XAttribute("Protocol", "udp"));
                            break;
                    }
                }

                if (!row.IsColumnEmpty(5))
                {
                    firewallException.Add(new XAttribute("Program", row.FieldAsString(5)));
                }

                if (!row.IsColumnEmpty(6))
                {
                    var attr = Convert.ToInt32(row[6]);
                    AttributeIfNotNull("IgnoreFailure", (attr & 0x1) == 0x1);
                }

                if (!row.IsColumnEmpty(7))
                {
                    switch (Convert.ToInt32(row[7]))
                    {
                        case FirewallConstants.NET_FW_PROFILE2_DOMAIN:
                            firewallException.Add(new XAttribute("Profile", "domain"));
                            break;
                        case FirewallConstants.NET_FW_PROFILE2_PRIVATE:
                            firewallException.Add(new XAttribute("Profile", "private"));
                            break;
                        case FirewallConstants.NET_FW_PROFILE2_PUBLIC:
                            firewallException.Add(new XAttribute("Profile", "public"));
                            break;
                        case FirewallConstants.NET_FW_PROFILE2_ALL:
                            firewallException.Add(new XAttribute("Profile", "all"));
                            break;
                    }
                }

                if (!row.IsColumnEmpty(9))
                {
                    firewallException.Add(new XAttribute("Description", row.FieldAsString(9)));
                }

                if (!row.IsColumnEmpty(10))
                {
                    switch (Convert.ToInt32(row[10]))
                    {
                        case FirewallConstants.NET_FW_RULE_DIR_IN:

                            firewallException.Add(AttributeIfNotNull("Outbound", false));
                            break;
                        case FirewallConstants.NET_FW_RULE_DIR_OUT:
                            firewallException.Add(AttributeIfNotNull("Outbound", true));
                            break;
                    }
                }

                this.DecompilerHelper.IndexElement(row, firewallException);
            }
        }

        private static void AddRemoteAddress(XElement firewallException, string address)
        {
            var remoteAddress = new XElement(FirewallConstants.RemoteAddressName,
                new XAttribute("Value", address)
            );

            firewallException.AddAfterSelf(remoteAddress);
        }

        private static XAttribute AttributeIfNotNull(string name, bool value)
        {
            return new XAttribute(name, value ? "yes" : "no");
        }

        /// <summary>
        /// Finalize the FirewallException table.
        /// </summary>
        /// <param name="tables">Collection of all tables.</param>
        private void FinalizeFirewallExceptionTable(TableIndexedCollection tables)
        {
            if (tables.TryGetTable("Wix4FirewallException", out var firewallExceptionTable))
            {
                foreach (var row in firewallExceptionTable.Rows)
                {
                    var xmlConfig = this.DecompilerHelper.GetIndexedElement(row);

                    var componentId = row.FieldAsString(8);
                    if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
                    {
                        component.Add(xmlConfig);
                    }
                    else
                    {
                        this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, firewallExceptionTable.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
                    }
                }
            }
        }
    }
}