-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlAclConfigItem.cs
194 lines (152 loc) · 6.25 KB
/
UrlAclConfigItem.cs
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
using System;
using System.Collections;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace HttpConfig
{
public class UrlAclConfigItem : ConfigItem
{
private string _url;
private Acl _acl;
public UrlAclConfigItem() { }
public string Url
{
get { return _url; }
set
{
_url = value;
SetItems();
}
}
public Acl Dacl
{
get { return _acl; }
set
{
_acl = value;
SetItems();
}
}
public override string Key
{
get { return _url; }
}
public override string ToString()
{
return _url;
}
public override void ApplyConfig()
{
IntPtr pStruct = IntPtr.Zero;
HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET setStruct = new HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET();
setStruct.KeyDesc.pUrlPrefix = _url;
setStruct.ParamDesc.pStringSecurityDescriptor = _acl.ToSddl();
try
{
pStruct = Marshal.AllocHGlobal(Marshal.SizeOf(setStruct));
Marshal.StructureToPtr(setStruct, pStruct, false);
if((Status == ModifiedStatus.Modified) || (Status == ModifiedStatus.Removed))
{
HttpApi.Error error = HttpApi.HttpDeleteServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
pStruct,
Marshal.SizeOf(setStruct),
IntPtr.Zero);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpDeleteServiceConfiguration (URLACL) failed. Error = " + error);
}
if((Status == ModifiedStatus.Modified) || (Status == ModifiedStatus.Added))
{
HttpApi.Error error = HttpApi.HttpSetServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
pStruct,
Marshal.SizeOf(setStruct),
IntPtr.Zero);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpSetServiceConfiguration (URLACL) failed. Error = " + error);
}
}
finally
{
if(pStruct != IntPtr.Zero)
{
Marshal.DestroyStructure(pStruct, typeof(HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET));
Marshal.FreeHGlobal(pStruct);
}
}
}
public static Hashtable QueryConfig()
{
Hashtable items = new Hashtable();
HttpApi.HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HttpApi.HTTP_SERVICE_CONFIG_URLACL_QUERY();
query.QueryDesc = HttpApi.HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext;
HttpApi.Error error = HttpApi.Error.NO_ERROR;
IntPtr pInput = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HttpApi.HTTP_SERVICE_CONFIG_URLACL_QUERY)));
try
{
for(query.dwToken = 0; ; query.dwToken++)
{
Marshal.StructureToPtr(query, pInput, false);
int requiredBufferLength = 0;
error = QueryServiceConfig(pInput, IntPtr.Zero, 0, out requiredBufferLength);
if(error == HttpApi.Error.ERROR_NO_MORE_ITEMS)
break;
else if(error != HttpApi.Error.ERROR_INSUFFICIENT_BUFFER)
throw new HttpApiException(error, "HttpQueryServiceConfiguration (URLACL) failed. Error = " + error);
IntPtr pOutput = Marshal.AllocHGlobal(requiredBufferLength);
try
{
HttpApi.ZeroMemory(pOutput, requiredBufferLength);
error = QueryServiceConfig(pInput, pOutput, requiredBufferLength, out requiredBufferLength);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpQueryServiceConfiguration (URLACL) failed. Error = " + error);
UrlAclConfigItem item = Deserialize(pOutput);
items.Add(item.Key, item);
}
finally
{
Marshal.FreeHGlobal(pOutput);
}
}
}
finally
{
Marshal.FreeHGlobal(pInput);
}
return items;
}
private static UrlAclConfigItem Deserialize(IntPtr pUrlAclConfigSetStruct)
{
UrlAclConfigItem item = new UrlAclConfigItem();
HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET aclStruct =
(HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pUrlAclConfigSetStruct, typeof(HttpApi.HTTP_SERVICE_CONFIG_URLACL_SET));
item.Url = aclStruct.KeyDesc.pUrlPrefix;
item.Dacl = Acl.FromSddl(aclStruct.ParamDesc.pStringSecurityDescriptor);
item.Status = ModifiedStatus.Unmodified;
return item;
}
private static HttpApi.Error QueryServiceConfig(IntPtr pInput, IntPtr pOutput, int outputLength, out int requiredBufferLength)
{
return HttpApi.HttpQueryServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
pInput,
Marshal.SizeOf(typeof(HttpApi.HTTP_SERVICE_CONFIG_URLACL_QUERY)),
pOutput,
outputLength,
out requiredBufferLength,
IntPtr.Zero);
}
private void SetItems()
{
SubItems.Clear();
if(_url != null)
Text = _url;
if(_acl != null)
SubItems.Add(_acl.FriendlyString);
}
}
}