aboutsummaryrefslogtreecommitdiff
path: root/src/tools/heat/IIsWebSiteHarvester.cs
blob: 6e5e29c758b69cd0017f9c317c23e0f602e29328 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// 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.Harvesters
{
    using System;
    using System.DirectoryServices;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using WixToolset.Data;
    using WixToolset.Harvesters.Data;
    using WixToolset.Harvesters.Extensibility;
    using IIs = WixToolset.Harvesters.Serialize.IIs;
    using Wix = WixToolset.Harvesters.Serialize;

    /// <summary>
    /// The web site harvester for the WiX Toolset Internet Information Services Extension.
    /// </summary>
    public sealed class IIsWebSiteHarvester : BaseHarvesterExtension
    {
        /// <summary>
        /// Harvest a WiX document.
        /// </summary>
        /// <param name="argument">The argument for harvesting.</param>
        /// <returns>The harvested Fragment.</returns>
        public override Wix.Fragment[] Harvest(string argument)
        {
            DirectoryHarvester directoryHarvester = new DirectoryHarvester();
            directoryHarvester.Core = this.Core;
            directoryHarvester.KeepEmptyDirectories = true;

            IIsWebSiteHarvester iisWebSiteHarvester = new IIsWebSiteHarvester();
            iisWebSiteHarvester.Core = this.Core;

            IIs.WebSite webSite = iisWebSiteHarvester.HarvestWebSite(argument);

            Wix.Component component = new Wix.Component();
            component.AddChild(new Wix.CreateFolder());
            component.AddChild(webSite);

            this.Core.RootDirectory = webSite.Directory;
            Wix.Directory directory = directoryHarvester.HarvestDirectory(webSite.Directory, true);
            directory.AddChild(component);

            Wix.Fragment fragment = new Wix.Fragment();
            fragment.AddChild(directory);

            return new Wix.Fragment[] { fragment };
        }

        /// <summary>
        /// Harvest a web site.
        /// </summary>
        /// <param name="name">The name of the web site.</param>
        /// <returns>The harvested web site.</returns>
        public IIs.WebSite HarvestWebSite(string name)
        {
            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry("IIS://localhost/W3SVC");

                foreach (DirectoryEntry childEntry in directoryEntry.Children)
                {
                    if ("IIsWebServer" == childEntry.SchemaClassName)
                    {
                        if (String.Equals((string)childEntry.Properties["ServerComment"].Value, name, StringComparison.OrdinalIgnoreCase))
                        {
                            return this.HarvestWebSite(childEntry);
                        }
                    }
                }
            }
            catch (COMException ce)
            {
                // 0x8007005 - access denied
                // If we don't have permission to harvest a website, it's likely because we're on
                // Vista or higher and aren't an Admin.
                if ((0x80070005 == unchecked((uint)ce.ErrorCode)))
                {
                    throw new WixException(HarvesterErrors.InsufficientPermissionHarvestWebSite());
                }
                // 0x80005000 - unknown error
                else if ((0x80005000 == unchecked((uint)ce.ErrorCode)))
                {
                    throw new WixException(HarvesterErrors.CannotHarvestWebSite());
                }
            }

            throw new WixException(HarvesterErrors.WebSiteNotFound(name));
        }

        /// <summary>
        /// Harvest a web site.
        /// </summary>
        /// <param name="webSiteEntry">The web site directory entry.</param>
        /// <returns>The harvested web site.</returns>
        private IIs.WebSite HarvestWebSite(DirectoryEntry webSiteEntry)
        {
            IIs.WebSite webSite = new IIs.WebSite();

            foreach (string propertyName in webSiteEntry.Properties.PropertyNames)
            {
                PropertyValueCollection property = webSiteEntry.Properties[propertyName];
                PropertyValueCollection parentProperty = webSiteEntry.Parent.Properties[propertyName];

                if (null == parentProperty.Value || parentProperty.Value.ToString() != property.Value.ToString())
                {
                    switch (propertyName)
                    {
                        case "SecureBindings":
                            IIs.WebAddress secureWebAddress = this.HarvestBindings(propertyName, property);
                            if (null != secureWebAddress)
                            {
                                webSite.AddChild(secureWebAddress);
                            }
                            break;
                        case "ServerBindings":
                            IIs.WebAddress webAddress = this.HarvestBindings(propertyName, property);
                            if (null != webAddress)
                            {
                                webSite.AddChild(webAddress);
                            }
                            break;
                        case "ServerComment":
                            webSite.Description = (string)property.Value;
                            break;
                    }
                }
            }

            foreach (DirectoryEntry childEntry in webSiteEntry.Children)
            {
                switch (childEntry.SchemaClassName)
                {
                    case "IIsFilters":
                        string loadOrder = (string)childEntry.Properties["FilterLoadOrder"].Value;
                        if (loadOrder.Length > 0)
                        {
                            string[] filterNames = loadOrder.Split(",".ToCharArray());

                            for (int i = 0; i < filterNames.Length; i++)
                            {
                                using (DirectoryEntry webFilterEntry = new DirectoryEntry(String.Concat(childEntry.Path, '/', filterNames[i])))
                                {
                                    IIs.WebFilter webFilter = this.HarvestWebFilter(webFilterEntry);

                                    webFilter.LoadOrder = (i + 1).ToString(CultureInfo.InvariantCulture);

                                    webSite.AddChild(webFilter);
                                }
                            }
                        }
                        break;
                    case "IIsWebDirectory":
                        this.HarvestWebDirectory(childEntry, webSite);
                        break;
                    case "IIsWebVirtualDir":
                        foreach (string propertyName in childEntry.Properties.PropertyNames)
                        {
                            PropertyValueCollection property = childEntry.Properties[propertyName];

                            switch (propertyName)
                            {
                                case "Path":
                                    webSite.Directory = (string)property.Value;
                                    break;
                            }
                        }

                        IIs.WebDirProperties webDirProps = this.HarvestWebDirProperties(childEntry);
                        if (null != webDirProps)
                        {
                            webSite.AddChild(webDirProps);
                        }

                        foreach (DirectoryEntry child2Entry in childEntry.Children)
                        {
                            switch (child2Entry.SchemaClassName)
                            {
                                case "IIsWebDirectory":
                                    this.HarvestWebDirectory(child2Entry, webSite);
                                    break;
                                case "IIsWebVirtualDir":
                                    this.HarvestWebVirtualDir(child2Entry, webSite);
                                    break;
                            }
                        }
                        break;
                }
            }

            return webSite;
        }

        /// <summary>
        /// Harvest bindings.
        /// </summary>
        /// <param name="propertyName">The property name of the bindings property.</param>
        /// <param name="bindingsProperty">The bindings property.</param>
        /// <returns>The harvested bindings.</returns>
        private IIs.WebAddress HarvestBindings(string propertyName, PropertyValueCollection bindingsProperty)
        {
            if (1 == bindingsProperty.Count)
            {
                IIs.WebAddress webAddress = new IIs.WebAddress();

                string[] bindings = ((string)bindingsProperty[0]).Split(":".ToCharArray());

                if (0 < bindings[0].Length)
                {
                    webAddress.IP = bindings[0];
                }

                if (0 < bindings[1].Length)
                {
                    webAddress.Port = bindings[1];
                }

                if (0 < bindings[2].Length)
                {
                    webAddress.Header = bindings[2];
                }

                if ("SecureBindings" == propertyName)
                {
                    webAddress.Secure = IIs.YesNoType.yes;
                }

                return webAddress;
            }

            return null;
        }

        /// <summary>
        /// Harvest a web directory.
        /// </summary>
        /// <param name="webDirectoryEntry">The web directory directory entry.</param>
        /// <param name="webSite">The parent web site.</param>
        private void HarvestWebDirectory(DirectoryEntry webDirectoryEntry, IIs.WebSite webSite)
        {
            foreach (DirectoryEntry childEntry in webDirectoryEntry.Children)
            {
                switch (childEntry.SchemaClassName)
                {
                    case "IIsWebDirectory":
                        this.HarvestWebDirectory(childEntry, webSite);
                        break;
                    case "IIsWebVirtualDir":
                        this.HarvestWebVirtualDir(childEntry, webSite);
                        break;
                }
            }

            IIs.WebDirProperties webDirProperties = this.HarvestWebDirProperties(webDirectoryEntry);

            if (null != webDirProperties)
            {
                IIs.WebDir webDir = new IIs.WebDir();

                int indexOfRoot = webDirectoryEntry.Path.IndexOf("ROOT/", StringComparison.OrdinalIgnoreCase);
                webDir.Path = webDirectoryEntry.Path.Substring(indexOfRoot + 5);

                webDir.AddChild(webDirProperties);

                webSite.AddChild(webDir);
            }
        }

        /// <summary>
        /// Harvest a web filter.
        /// </summary>
        /// <param name="webFilterEntry">The web filter directory entry.</param>
        /// <returns>The harvested web filter.</returns>
        private IIs.WebFilter HarvestWebFilter(DirectoryEntry webFilterEntry)
        {
            IIs.WebFilter webFilter = new IIs.WebFilter();

            webFilter.Name = webFilterEntry.Name;

            foreach (string propertyName in webFilterEntry.Properties.PropertyNames)
            {
                PropertyValueCollection property = webFilterEntry.Properties[propertyName];

                switch (propertyName)
                {
                    case "FilterDescription":
                        webFilter.Description = (string)property.Value;
                        break;
                    case "FilterFlags":
                        webFilter.Flags = (int)property.Value;
                        break;
                    case "FilterPath":
                        webFilter.Path = (string)property.Value;
                        break;
                }
            }

            return webFilter;
        }

        /// <summary>
        /// Harvest a web directory's properties.
        /// </summary>
        /// <param name="directoryEntry">The web directory directory entry.</param>
        /// <returns>The harvested web directory's properties.</returns>
        private IIs.WebDirProperties HarvestWebDirProperties(DirectoryEntry directoryEntry)
        {
            bool foundProperties = false;
            IIs.WebDirProperties webDirProperties = new IIs.WebDirProperties();

            // Cannot read properties for "iisadmin" site.
            if (String.Equals("iisadmin", directoryEntry.Name, StringComparison.OrdinalIgnoreCase) &&
                String.Equals("ROOT", directoryEntry.Parent.Name, StringComparison.OrdinalIgnoreCase))
            {
                return null;
            }

            foreach (string propertyName in directoryEntry.Properties.PropertyNames)
            {
                PropertyValueCollection property = directoryEntry.Properties[propertyName];
                PropertyValueCollection parentProperty = directoryEntry.Parent.Properties[propertyName];

                if (null == parentProperty.Value || parentProperty.Value.ToString() != property.Value.ToString())
                {
                    switch (propertyName)
                    {
                        case "AccessFlags":
                            int access = (int)property.Value;

                            if (0x1 == (access & 0x1))
                            {
                                webDirProperties.Read = IIs.YesNoType.yes;
                            }

                            if (0x2 == (access & 0x2))
                            {
                                webDirProperties.Write = IIs.YesNoType.yes;
                            }

                            if (0x4 == (access & 0x4))
                            {
                                webDirProperties.Execute = IIs.YesNoType.yes;
                            }

                            if (0x200 == (access & 0x200))
                            {
                                webDirProperties.Script = IIs.YesNoType.yes;
                            }

                            foundProperties = true;
                            break;
                        case "AuthFlags":
                            int authorization = (int)property.Value;

                            if (0x1 == (authorization & 0x1))
                            {
                                webDirProperties.AnonymousAccess = IIs.YesNoType.yes;
                            }

                            if (0x2 == (authorization & 0x2))
                            {
                                webDirProperties.BasicAuthentication = IIs.YesNoType.yes;
                            }

                            if (0x4 == (authorization & 0x4))
                            {
                                webDirProperties.WindowsAuthentication = IIs.YesNoType.yes;
                            }

                            if (0x10 == (authorization & 0x10))
                            {
                                webDirProperties.DigestAuthentication = IIs.YesNoType.yes;
                            }

                            if (0x40 == (authorization & 0x40))
                            {
                                webDirProperties.PassportAuthentication = IIs.YesNoType.yes;
                            }

                            foundProperties = true;
                            break;
                    }
                }
            }

            return foundProperties ? webDirProperties : null;
        }

        /// <summary>
        /// Harvest a web virtual directory.
        /// </summary>
        /// <param name="webVirtualDirEntry">The web virtual directory directory entry.</param>
        /// <param name="webSite">The parent web site.</param>
        private void HarvestWebVirtualDir(DirectoryEntry webVirtualDirEntry, IIs.WebSite webSite)
        {
            IIs.WebVirtualDir webVirtualDir = new IIs.WebVirtualDir();

            foreach (string propertyName in webVirtualDirEntry.Properties.PropertyNames)
            {
                PropertyValueCollection property = webVirtualDirEntry.Properties[propertyName];
                PropertyValueCollection parentProperty = webVirtualDirEntry.Parent.Properties[propertyName];

                if (null == parentProperty.Value || parentProperty.Value.ToString() != property.Value.ToString())
                {
                    switch (propertyName)
                    {
                        case "Path":
                            webVirtualDir.Directory = (string)property.Value;
                            break;
                    }
                }
            }

            int indexOfRoot = webVirtualDirEntry.Path.IndexOf("ROOT/", StringComparison.OrdinalIgnoreCase);
            webVirtualDir.Alias = webVirtualDirEntry.Path.Substring(indexOfRoot + 5);

            IIs.WebDirProperties webDirProps = this.HarvestWebDirProperties(webVirtualDirEntry);
            if (webDirProps != null)
            {
                webVirtualDir.AddChild(webDirProps);
            }

            foreach (DirectoryEntry childEntry in webVirtualDirEntry.Children)
            {
                switch (childEntry.SchemaClassName)
                {
                    case "IIsWebDirectory":
                        this.HarvestWebDirectory(childEntry, webSite);
                        break;
                    case "IIsWebVirtualDir":
                        this.HarvestWebVirtualDir(childEntry, webSite);
                        break;
                }
            }

            webSite.AddChild(webVirtualDir);
        }
    }
}