winevt_c 0.7.4 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,425 @@
1
+ #include <winevt_c.h>
2
+
3
+ /* clang-format off */
4
+ /*
5
+ * Document-class: Winevt::EventLog::Session
6
+ *
7
+ * Manage Session information for Windows EventLog.
8
+ *
9
+ * @example
10
+ * require 'winevt'
11
+ *
12
+ * @session = Winevt::EventLog::Session.new("127.0.0.1")
13
+ *
14
+ * @session.domain = "<EXAMPLEGROUP>"
15
+ * @session.username = "<username>"
16
+ * @session.password = "<password>"
17
+ * # Then pass @session veriable into Winevt::EventLog::Query or
18
+ * # Winevt::EventLog::Subscribe#subscribe
19
+ * @query = Winevt::EventLog::Query.new(
20
+ * "Application",
21
+ * "*[System[(Level <= 3) and TimeCreated[timediff(@SystemTime) <= 86400000]]]",
22
+ * @session
23
+ * )
24
+ * # some stuff.
25
+ *
26
+ * @subscribe = Winevt::EventLog::Subscribe.new
27
+ * @subscribe.subscribe(
28
+ * "Application",
29
+ * "*[System[(Level <= 4) and TimeCreated[timediff(@SystemTime) <= 86400000]]]",
30
+ * @session
31
+ * )
32
+ * # And some stuff.
33
+ * @since v0.9.0
34
+ */
35
+ /* clang-format on */
36
+
37
+ VALUE rb_cSession;
38
+ VALUE rb_cRpcLoginFlag;
39
+
40
+ static void session_free(void* ptr);
41
+
42
+ static const rb_data_type_t rb_winevt_session_type = { "winevt/session",
43
+ {
44
+ 0,
45
+ session_free,
46
+ 0,
47
+ },
48
+ NULL,
49
+ NULL,
50
+ RUBY_TYPED_FREE_IMMEDIATELY };
51
+
52
+ static void
53
+ session_free(void* ptr)
54
+ {
55
+ struct WinevtSession* winevtSession = (struct WinevtSession*)ptr;
56
+
57
+ if (winevtSession->server)
58
+ free(winevtSession->server);
59
+ if (winevtSession->domain)
60
+ free(winevtSession->domain);
61
+ if (winevtSession->username)
62
+ free(winevtSession->username);
63
+ if (winevtSession->password)
64
+ free(winevtSession->password);
65
+
66
+ xfree(ptr);
67
+ }
68
+
69
+ static VALUE
70
+ rb_winevt_session_alloc(VALUE klass)
71
+ {
72
+ VALUE obj;
73
+ struct WinevtSession* winevtSession;
74
+ obj = TypedData_Make_Struct(
75
+ klass, struct WinevtSession, &rb_winevt_session_type, winevtSession);
76
+ return obj;
77
+ }
78
+
79
+ /*
80
+ * Initalize Session class.
81
+ *
82
+ * @overload initialize(server, domain=nil, username=nil, password=nil, flags=Winevt::EventLog::Session::RpcLoginFlag::AuthDefault)
83
+ * @param server [String] Server ip address or fqdn.
84
+ * @param domain [String] Domain name.
85
+ * @param username [String] username on remote server.
86
+ * @param password [String] Remote server user password.
87
+ * @param flags [Integer] Flags for authentication method choices.
88
+ * @return [Session]
89
+ *
90
+ */
91
+
92
+ static VALUE
93
+ rb_winevt_session_initialize(VALUE self)
94
+ {
95
+ struct WinevtSession* winevtSession;
96
+
97
+ TypedData_Get_Struct(
98
+ self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
99
+
100
+ winevtSession->server = NULL;
101
+ winevtSession->domain = NULL;
102
+ winevtSession->username = NULL;
103
+ winevtSession->password = NULL;
104
+ winevtSession->flags = EvtRpcLoginAuthDefault;
105
+
106
+ return Qnil;
107
+ }
108
+
109
+ /*
110
+ * This method returns server for remoting access.
111
+ *
112
+ * @return [String]
113
+ */
114
+ static VALUE
115
+ rb_winevt_session_get_server(VALUE self)
116
+ {
117
+ struct WinevtSession* winevtSession;
118
+
119
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
120
+
121
+ if (winevtSession->server) {
122
+ return wstr_to_rb_str(CP_UTF8, winevtSession->server, -1);
123
+ } else {
124
+ return rb_str_new2("(NULL)");
125
+ }
126
+ }
127
+
128
+ /*
129
+ * This method specifies server for remoting access.
130
+ *
131
+ * @param rb_server [String] server
132
+ */
133
+ static VALUE
134
+ rb_winevt_session_set_server(VALUE self, VALUE rb_server)
135
+ {
136
+ struct WinevtSession* winevtSession;
137
+ DWORD len;
138
+ VALUE vserverBuf;
139
+ PWSTR wServer;
140
+
141
+ Check_Type(rb_server, T_STRING);
142
+
143
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
144
+
145
+ len =
146
+ MultiByteToWideChar(CP_UTF8, 0,
147
+ RSTRING_PTR(rb_server), RSTRING_LEN(rb_server),
148
+ NULL, 0);
149
+ wServer = ALLOCV_N(WCHAR, vserverBuf, len + 1);
150
+ MultiByteToWideChar(CP_UTF8, 0,
151
+ RSTRING_PTR(rb_server), RSTRING_LEN(rb_server),
152
+ wServer, len);
153
+ winevtSession->server = _wcsdup(wServer);
154
+ wServer[len] = L'\0';
155
+
156
+ ALLOCV_END(vserverBuf);
157
+
158
+ return Qnil;
159
+ }
160
+
161
+ /*
162
+ * This method returns domain for remoting access.
163
+ *
164
+ * @return [String]
165
+ */
166
+ static VALUE
167
+ rb_winevt_session_get_domain(VALUE self)
168
+ {
169
+ struct WinevtSession* winevtSession;
170
+
171
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
172
+
173
+ if (winevtSession->domain) {
174
+ return wstr_to_rb_str(CP_UTF8, winevtSession->domain, -1);
175
+ } else {
176
+ return rb_str_new2("(NULL)");
177
+ }
178
+ }
179
+
180
+ /*
181
+ * This method specifies domain for remoting access.
182
+ *
183
+ * @param rb_domain [String] domain
184
+ */
185
+ static VALUE
186
+ rb_winevt_session_set_domain(VALUE self, VALUE rb_domain)
187
+ {
188
+ struct WinevtSession* winevtSession;
189
+ DWORD len;
190
+ VALUE vdomainBuf;
191
+ PWSTR wDomain;
192
+
193
+ Check_Type(rb_domain, T_STRING);
194
+
195
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
196
+
197
+ len =
198
+ MultiByteToWideChar(CP_UTF8, 0,
199
+ RSTRING_PTR(rb_domain), RSTRING_LEN(rb_domain),
200
+ NULL, 0);
201
+ wDomain = ALLOCV_N(WCHAR, vdomainBuf, len + 1);
202
+ MultiByteToWideChar(CP_UTF8, 0,
203
+ RSTRING_PTR(rb_domain), RSTRING_LEN(rb_domain),
204
+ wDomain, len);
205
+ wDomain[len] = L'\0';
206
+
207
+ winevtSession->domain = _wcsdup(wDomain);
208
+
209
+ ALLOCV_END(vdomainBuf);
210
+
211
+ return Qnil;
212
+ }
213
+
214
+ /*
215
+ * This method returns username for remoting access.
216
+ *
217
+ * @return [String]
218
+ */
219
+ static VALUE
220
+ rb_winevt_session_get_username(VALUE self)
221
+ {
222
+ struct WinevtSession* winevtSession;
223
+
224
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
225
+
226
+ if (winevtSession->username) {
227
+ return wstr_to_rb_str(CP_UTF8, winevtSession->username, -1);
228
+ } else {
229
+ return rb_str_new2("(NULL)");
230
+ }
231
+ }
232
+
233
+ /*
234
+ * This method specifies username for remoting access.
235
+ *
236
+ * @param rb_username [String] username
237
+ */
238
+ static VALUE
239
+ rb_winevt_session_set_username(VALUE self, VALUE rb_username)
240
+ {
241
+ struct WinevtSession* winevtSession;
242
+ DWORD len;
243
+ VALUE vusernameBuf;
244
+ PWSTR wUsername;
245
+
246
+ Check_Type(rb_username, T_STRING);
247
+
248
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
249
+
250
+ len =
251
+ MultiByteToWideChar(CP_UTF8, 0,
252
+ RSTRING_PTR(rb_username), RSTRING_LEN(rb_username),
253
+ NULL, 0);
254
+ wUsername = ALLOCV_N(WCHAR, vusernameBuf, len + 1);
255
+ MultiByteToWideChar(CP_UTF8, 0,
256
+ RSTRING_PTR(rb_username), RSTRING_LEN(rb_username),
257
+ wUsername, len);
258
+ wUsername[len] = L'\0';
259
+
260
+ winevtSession->username = _wcsdup(wUsername);
261
+
262
+ ALLOCV_END(vusernameBuf);
263
+
264
+ return Qnil;
265
+ }
266
+
267
+ /*
268
+ * This method returns password for remoting access.
269
+ *
270
+ * @return [String]
271
+ */
272
+ static VALUE
273
+ rb_winevt_session_get_password(VALUE self)
274
+ {
275
+ struct WinevtSession* winevtSession;
276
+
277
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
278
+
279
+ if (winevtSession->password) {
280
+ return wstr_to_rb_str(CP_UTF8, winevtSession->password, -1);
281
+ } else {
282
+ return rb_str_new2("(NULL)");
283
+ }
284
+ }
285
+
286
+ /*
287
+ * This method specifies password for remoting access.
288
+ *
289
+ * @param rb_password [String] password
290
+ */
291
+ static VALUE
292
+ rb_winevt_session_set_password(VALUE self, VALUE rb_password)
293
+ {
294
+ struct WinevtSession* winevtSession;
295
+ DWORD len;
296
+ VALUE vpasswordBuf;
297
+ PWSTR wPassword;
298
+
299
+ Check_Type(rb_password, T_STRING);
300
+
301
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
302
+
303
+ len =
304
+ MultiByteToWideChar(CP_UTF8, 0,
305
+ RSTRING_PTR(rb_password), RSTRING_LEN(rb_password),
306
+ NULL, 0);
307
+ wPassword = ALLOCV_N(WCHAR, vpasswordBuf, len + 1);
308
+ MultiByteToWideChar(CP_UTF8, 0,
309
+ RSTRING_PTR(rb_password), RSTRING_LEN(rb_password),
310
+ wPassword, len);
311
+ wPassword[len] = L'\0';
312
+
313
+ winevtSession->password = _wcsdup(wPassword);
314
+
315
+ ALLOCV_END(vpasswordBuf);
316
+
317
+ return Qnil;
318
+ }
319
+
320
+ /*
321
+ * This method returns flags for remoting access.
322
+ *
323
+ * @return [Integer]
324
+ */
325
+ static VALUE
326
+ rb_winevt_session_get_flags(VALUE self)
327
+ {
328
+ struct WinevtSession* winevtSession;
329
+
330
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
331
+
332
+ return LONG2NUM(winevtSession->flags);
333
+ }
334
+
335
+ static DWORD
336
+ get_session_rpc_login_flag_from_cstr(char* flag_str)
337
+ {
338
+ if (strcmp(flag_str, "default") == 0)
339
+ return EvtRpcLoginAuthDefault;
340
+ else if (strcmp(flag_str, "negociate") == 0)
341
+ return EvtRpcLoginAuthNegotiate;
342
+ else if (strcmp(flag_str, "kerberos") == 0)
343
+ return EvtRpcLoginAuthKerberos;
344
+ else if (strcmp(flag_str, "ntlm") == 0)
345
+ return EvtRpcLoginAuthNTLM;
346
+ else
347
+ rb_raise(rb_eArgError, "Unknown rpc login flag: %s", flag_str);
348
+
349
+ return 0;
350
+ }
351
+
352
+
353
+ /*
354
+ * This method specifies flags for remoting access.
355
+ *
356
+ * @param rb_flags [Integer] flags
357
+ */
358
+ static VALUE
359
+ rb_winevt_session_set_flags(VALUE self, VALUE rb_flags)
360
+ {
361
+ struct WinevtSession* winevtSession;
362
+ EVT_RPC_LOGIN_FLAGS flags = EvtRpcLoginAuthDefault;
363
+
364
+ TypedData_Get_Struct(self, struct WinevtSession, &rb_winevt_session_type, winevtSession);
365
+
366
+ switch(TYPE(rb_flags)) {
367
+ case T_SYMBOL:
368
+ flags = get_session_rpc_login_flag_from_cstr(RSTRING_PTR(rb_sym2str(rb_flags)));
369
+ break;
370
+ case T_STRING:
371
+ flags = get_session_rpc_login_flag_from_cstr(StringValuePtr(rb_flags));
372
+ break;
373
+ case T_FIXNUM:
374
+ flags = NUM2LONG(rb_flags);
375
+ break;
376
+ default:
377
+ rb_raise(rb_eArgError, "Expected Symbol, String or Fixnum in flags");
378
+ }
379
+ winevtSession->flags = flags;
380
+
381
+ return Qnil;
382
+ }
383
+
384
+ void
385
+ Init_winevt_session(VALUE rb_cEventLog)
386
+ {
387
+ rb_cSession = rb_define_class_under(rb_cEventLog, "Session", rb_cObject);
388
+
389
+ rb_define_alloc_func(rb_cSession, rb_winevt_session_alloc);
390
+
391
+ rb_cRpcLoginFlag = rb_define_module_under(rb_cSession, "RpcLoginFlag");
392
+
393
+ rb_define_method(rb_cSession, "initialize", rb_winevt_session_initialize, 0);
394
+ rb_define_method(rb_cSession, "server", rb_winevt_session_get_server, 0);
395
+ rb_define_method(rb_cSession, "server=", rb_winevt_session_set_server, 1);
396
+ rb_define_method(rb_cSession, "domain", rb_winevt_session_get_domain, 0);
397
+ rb_define_method(rb_cSession, "domain=", rb_winevt_session_set_domain, 1);
398
+ rb_define_method(rb_cSession, "username", rb_winevt_session_get_username, 0);
399
+ rb_define_method(rb_cSession, "username=", rb_winevt_session_set_username, 1);
400
+ rb_define_method(rb_cSession, "password", rb_winevt_session_get_password, 0);
401
+ rb_define_method(rb_cSession, "password=", rb_winevt_session_set_password, 1);
402
+ rb_define_method(rb_cSession, "flags", rb_winevt_session_get_flags, 0);
403
+ rb_define_method(rb_cSession, "flags=", rb_winevt_session_set_flags, 1);
404
+
405
+ /*
406
+ * EVT_RPC_LOGIN_FLAGS enumeration: EvtRpcLoginAuthDefault
407
+ * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_rpc_login_flags
408
+ */
409
+ rb_define_const(rb_cRpcLoginFlag, "AuthDefault", LONG2NUM(EvtRpcLoginAuthDefault));
410
+ /*
411
+ * EVT_RPC_LOGIN_FLAGS enumeration: EvtRpcLoginAuthNegociate
412
+ * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_rpc_login_flags
413
+ */
414
+ rb_define_const(rb_cRpcLoginFlag, "AuthNegociate", LONG2NUM(EvtRpcLoginAuthNegotiate));
415
+ /*
416
+ * EVT_RPC_LOGIN_FLAGS enumeration: EvtRpcLoginAuthKerberos
417
+ * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_rpc_login_flags
418
+ */
419
+ rb_define_const(rb_cRpcLoginFlag, "AuthKerberos", LONG2NUM(EvtRpcLoginAuthKerberos));
420
+ /*
421
+ * EVT_RPC_LOGIN_FLAGS enumeration: EvtRpcLoginAuthNTLM
422
+ * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_rpc_login_flags
423
+ */
424
+ rb_define_const(rb_cRpcLoginFlag, "AuthNTLM", LONG2NUM(EvtRpcLoginAuthNTLM));
425
+ }
@@ -1,599 +1,757 @@
1
- #include <winevt_c.h>
2
-
3
- /* clang-format off */
4
- /*
5
- * Document-class: Winevt::EventLog::Subscribe
6
- *
7
- * Subscribe Windows EventLog channel.
8
- *
9
- * @example
10
- * require 'winevt'
11
- *
12
- * @subscribe = Winevt::EventLog::Subscribe.new
13
- * @subscribe.tail = true
14
- * @subscribe.rate_limit = 80
15
- * @subscribe.subscribe(
16
- * "Application", "*[System[(Level <= 4) and TimeCreated[timediff(@SystemTime) <= 86400000]]]"
17
- * )
18
- * while true do
19
- * @subscribe.each do |eventlog, message, string_inserts|
20
- * puts ({eventlog: eventlog, data: message})
21
- * end
22
- * sleep(0.1)
23
- * end
24
- *
25
- * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtsubscribe
26
- */
27
- /* clang-format on */
28
-
29
- static void subscribe_free(void* ptr);
30
-
31
- static const rb_data_type_t rb_winevt_subscribe_type = { "winevt/subscribe",
32
- {
33
- 0,
34
- subscribe_free,
35
- 0,
36
- },
37
- NULL,
38
- NULL,
39
- RUBY_TYPED_FREE_IMMEDIATELY };
40
-
41
- static void
42
- subscribe_free(void* ptr)
43
- {
44
- struct WinevtSubscribe* winevtSubscribe = (struct WinevtSubscribe*)ptr;
45
- if (winevtSubscribe->signalEvent)
46
- CloseHandle(winevtSubscribe->signalEvent);
47
-
48
- if (winevtSubscribe->subscription)
49
- EvtClose(winevtSubscribe->subscription);
50
-
51
- if (winevtSubscribe->bookmark)
52
- EvtClose(winevtSubscribe->bookmark);
53
-
54
- for (int i = 0; i < winevtSubscribe->count; i++) {
55
- if (winevtSubscribe->hEvents[i]) {
56
- EvtClose(winevtSubscribe->hEvents[i]);
57
- }
58
- }
59
-
60
- xfree(ptr);
61
- }
62
-
63
- static VALUE
64
- rb_winevt_subscribe_alloc(VALUE klass)
65
- {
66
- VALUE obj;
67
- struct WinevtSubscribe* winevtSubscribe;
68
- obj = TypedData_Make_Struct(
69
- klass, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
70
- return obj;
71
- }
72
-
73
- /*
74
- * Initalize Subscribe class.
75
- *
76
- * @return [Subscribe]
77
- *
78
- */
79
- static VALUE
80
- rb_winevt_subscribe_initialize(VALUE self)
81
- {
82
- struct WinevtSubscribe* winevtSubscribe;
83
-
84
- TypedData_Get_Struct(
85
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
86
-
87
- winevtSubscribe->rateLimit = SUBSCRIBE_RATE_INFINITE;
88
- winevtSubscribe->lastTime = 0;
89
- winevtSubscribe->currentRate = 0;
90
- winevtSubscribe->renderAsXML = TRUE;
91
- winevtSubscribe->readExistingEvents = TRUE;
92
- winevtSubscribe->preserveQualifiers = FALSE;
93
-
94
- return Qnil;
95
- }
96
-
97
- /*
98
- * This method specifies whether read existing events or not.
99
- *
100
- * @param rb_read_existing_events_p [Boolean]
101
- */
102
- static VALUE
103
- rb_winevt_subscribe_set_read_existing_events(VALUE self, VALUE rb_read_existing_events_p)
104
- {
105
- struct WinevtSubscribe* winevtSubscribe;
106
-
107
- TypedData_Get_Struct(
108
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
109
-
110
- winevtSubscribe->readExistingEvents = RTEST(rb_read_existing_events_p);
111
-
112
- return Qnil;
113
- }
114
-
115
- /*
116
- * This method returns whether read existing events or not.
117
- *
118
- * @return [Boolean]
119
- */
120
- static VALUE
121
- rb_winevt_subscribe_read_existing_events_p(VALUE self)
122
- {
123
- struct WinevtSubscribe* winevtSubscribe;
124
-
125
- TypedData_Get_Struct(
126
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
127
-
128
- return winevtSubscribe->readExistingEvents ? Qtrue : Qfalse;
129
- }
130
-
131
- /*
132
- * Subscribe into a Windows EventLog channel.
133
- *
134
- * @overload subscribe(path, query, options={})
135
- * @param path [String] Subscribe Channel
136
- * @param query [String] Query string for channel
137
- * @option options [Bookmark] bookmark Bookmark class instance.
138
- * @return [Boolean]
139
- *
140
- */
141
- static VALUE
142
- rb_winevt_subscribe_subscribe(int argc, VALUE* argv, VALUE self)
143
- {
144
- VALUE rb_path, rb_query, rb_bookmark;
145
- EVT_HANDLE hSubscription = NULL, hBookmark = NULL;
146
- HANDLE hSignalEvent;
147
- DWORD len, flags = 0L;
148
- VALUE wpathBuf, wqueryBuf, wBookmarkBuf;
149
- PWSTR path, query, bookmarkXml;
150
- DWORD status = ERROR_SUCCESS;
151
- struct WinevtSubscribe* winevtSubscribe;
152
-
153
- hSignalEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
154
-
155
- TypedData_Get_Struct(
156
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
157
-
158
- rb_scan_args(argc, argv, "21", &rb_path, &rb_query, &rb_bookmark);
159
- Check_Type(rb_path, T_STRING);
160
- Check_Type(rb_query, T_STRING);
161
-
162
- if (rb_obj_is_kind_of(rb_bookmark, rb_cString)) {
163
- // bookmarkXml : To wide char
164
- len = MultiByteToWideChar(
165
- CP_UTF8, 0, RSTRING_PTR(rb_bookmark), RSTRING_LEN(rb_bookmark), NULL, 0);
166
- bookmarkXml = ALLOCV_N(WCHAR, wBookmarkBuf, len + 1);
167
- MultiByteToWideChar(CP_UTF8,
168
- 0,
169
- RSTRING_PTR(rb_bookmark),
170
- RSTRING_LEN(rb_bookmark),
171
- bookmarkXml,
172
- len);
173
- bookmarkXml[len] = L'\0';
174
- hBookmark = EvtCreateBookmark(bookmarkXml);
175
- ALLOCV_END(wBookmarkBuf);
176
- if (hBookmark == NULL) {
177
- status = GetLastError();
178
- raise_system_error(rb_eWinevtQueryError, status);
179
- }
180
- }
181
-
182
- // path : To wide char
183
- len =
184
- MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_path), RSTRING_LEN(rb_path), NULL, 0);
185
- path = ALLOCV_N(WCHAR, wpathBuf, len + 1);
186
- MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_path), RSTRING_LEN(rb_path), path, len);
187
- path[len] = L'\0';
188
-
189
- // query : To wide char
190
- len = MultiByteToWideChar(
191
- CP_UTF8, 0, RSTRING_PTR(rb_query), RSTRING_LEN(rb_query), NULL, 0);
192
- query = ALLOCV_N(WCHAR, wqueryBuf, len + 1);
193
- MultiByteToWideChar(
194
- CP_UTF8, 0, RSTRING_PTR(rb_query), RSTRING_LEN(rb_query), query, len);
195
- query[len] = L'\0';
196
-
197
- if (hBookmark) {
198
- flags |= EvtSubscribeStartAfterBookmark;
199
- } else if (winevtSubscribe->readExistingEvents) {
200
- flags |= EvtSubscribeStartAtOldestRecord;
201
- } else {
202
- flags |= EvtSubscribeToFutureEvents;
203
- }
204
-
205
- hSubscription =
206
- EvtSubscribe(NULL, hSignalEvent, path, query, hBookmark, NULL, NULL, flags);
207
- if (!hSubscription) {
208
- if (hBookmark != NULL) {
209
- EvtClose(hBookmark);
210
- }
211
- if (hSignalEvent != NULL) {
212
- CloseHandle(hSignalEvent);
213
- }
214
- status = GetLastError();
215
- raise_system_error(rb_eWinevtQueryError, status);
216
- }
217
-
218
- if (winevtSubscribe->subscription != NULL) {
219
- // should be disgarded the old event subscription handle.
220
- EvtClose(winevtSubscribe->subscription);
221
- }
222
-
223
- ALLOCV_END(wpathBuf);
224
- ALLOCV_END(wqueryBuf);
225
-
226
- winevtSubscribe->signalEvent = hSignalEvent;
227
- winevtSubscribe->subscription = hSubscription;
228
- if (hBookmark) {
229
- winevtSubscribe->bookmark = hBookmark;
230
- } else {
231
- winevtSubscribe->bookmark = EvtCreateBookmark(NULL);
232
- if (winevtSubscribe->bookmark == NULL) {
233
- if (hSubscription != NULL) {
234
- EvtClose(hSubscription);
235
- }
236
- if (hSignalEvent != NULL) {
237
- CloseHandle(hSignalEvent);
238
- }
239
- status = GetLastError();
240
- raise_system_error(rb_eWinevtQueryError, status);
241
- }
242
- }
243
-
244
- return Qtrue;
245
- }
246
-
247
- BOOL
248
- is_rate_limit_exceeded(struct WinevtSubscribe* winevtSubscribe)
249
- {
250
- time_t now;
251
-
252
- if (winevtSubscribe->rateLimit == SUBSCRIBE_RATE_INFINITE)
253
- return FALSE;
254
-
255
- time(&now);
256
-
257
- if (now <= winevtSubscribe->lastTime) {
258
- if (winevtSubscribe->currentRate >= winevtSubscribe->rateLimit) {
259
- return TRUE;
260
- }
261
- } else {
262
- winevtSubscribe->currentRate = 0;
263
- }
264
-
265
- return FALSE;
266
- }
267
-
268
- void
269
- update_to_reflect_rate_limit_state(struct WinevtSubscribe* winevtSubscribe, ULONG count)
270
- {
271
- time_t lastTime = 0;
272
-
273
- if (winevtSubscribe->rateLimit == SUBSCRIBE_RATE_INFINITE)
274
- return;
275
-
276
- time(&lastTime);
277
- winevtSubscribe->lastTime = lastTime;
278
- winevtSubscribe->currentRate += count;
279
- }
280
-
281
- /*
282
- * Handle the next values. Since v0.6.0, this method is used for
283
- * testing only. Please use #each instead.
284
- *
285
- * @return [Boolean]
286
- *
287
- * @see each
288
- */
289
-
290
- static VALUE
291
- rb_winevt_subscribe_next(VALUE self)
292
- {
293
- EVT_HANDLE hEvents[SUBSCRIBE_ARRAY_SIZE];
294
- ULONG count = 0;
295
- DWORD status = ERROR_SUCCESS;
296
- struct WinevtSubscribe* winevtSubscribe;
297
-
298
- TypedData_Get_Struct(
299
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
300
-
301
- if (is_rate_limit_exceeded(winevtSubscribe)) {
302
- return Qfalse;
303
- }
304
-
305
- if (!EvtNext(winevtSubscribe->subscription,
306
- SUBSCRIBE_ARRAY_SIZE,
307
- hEvents,
308
- INFINITE,
309
- 0,
310
- &count)) {
311
- status = GetLastError();
312
- if (ERROR_NO_MORE_ITEMS != status) {
313
- return Qfalse;
314
- }
315
- }
316
-
317
- if (status == ERROR_SUCCESS) {
318
- winevtSubscribe->count = count;
319
- for (int i = 0; i < count; i++) {
320
- winevtSubscribe->hEvents[i] = hEvents[i];
321
- EvtUpdateBookmark(winevtSubscribe->bookmark, winevtSubscribe->hEvents[i]);
322
- }
323
-
324
- update_to_reflect_rate_limit_state(winevtSubscribe, count);
325
-
326
- return Qtrue;
327
- }
328
-
329
- return Qfalse;
330
- }
331
-
332
- static VALUE
333
- rb_winevt_subscribe_render(VALUE self, EVT_HANDLE event)
334
- {
335
- struct WinevtSubscribe* winevtSubscribe;
336
-
337
- TypedData_Get_Struct(
338
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
339
-
340
- if (winevtSubscribe->renderAsXML) {
341
- return render_to_rb_str(event, EvtRenderEventXml);
342
- } else {
343
- return render_system_event(event, winevtSubscribe->preserveQualifiers);
344
- }
345
- }
346
-
347
- static VALUE
348
- rb_winevt_subscribe_message(EVT_HANDLE event)
349
- {
350
- WCHAR* wResult;
351
- VALUE utf8str;
352
-
353
- wResult = get_description(event);
354
- utf8str = wstr_to_rb_str(CP_UTF8, wResult, -1);
355
- free(wResult);
356
-
357
- return utf8str;
358
- }
359
-
360
- static VALUE
361
- rb_winevt_subscribe_string_inserts(EVT_HANDLE event)
362
- {
363
- return get_values(event);
364
- }
365
-
366
- static VALUE
367
- rb_winevt_subscribe_close_handle(VALUE self)
368
- {
369
- struct WinevtSubscribe* winevtSubscribe;
370
-
371
- TypedData_Get_Struct(
372
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
373
-
374
- for (int i = 0; i < winevtSubscribe->count; i++) {
375
- if (winevtSubscribe->hEvents[i] != NULL) {
376
- EvtClose(winevtSubscribe->hEvents[i]);
377
- winevtSubscribe->hEvents[i] = NULL;
378
- }
379
- }
380
-
381
- return Qnil;
382
- }
383
-
384
- static VALUE
385
- rb_winevt_subscribe_each_yield(VALUE self)
386
- {
387
- RETURN_ENUMERATOR(self, 0, 0);
388
- struct WinevtSubscribe* winevtSubscribe;
389
-
390
- TypedData_Get_Struct(
391
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
392
-
393
- for (int i = 0; i < winevtSubscribe->count; i++) {
394
- rb_yield_values(3,
395
- rb_winevt_subscribe_render(self, winevtSubscribe->hEvents[i]),
396
- rb_winevt_subscribe_message(winevtSubscribe->hEvents[i]),
397
- rb_winevt_subscribe_string_inserts(winevtSubscribe->hEvents[i]));
398
- }
399
-
400
- return Qnil;
401
- }
402
-
403
- /*
404
- * Enumerate to obtain Windows EventLog contents.
405
- *
406
- * This method yields the following:
407
- * (Stringified EventLog, Stringified detail message, Stringified
408
- * insert values)
409
- *
410
- * @yield (String,String,String)
411
- *
412
- */
413
- static VALUE
414
- rb_winevt_subscribe_each(VALUE self)
415
- {
416
- RETURN_ENUMERATOR(self, 0, 0);
417
-
418
- while (rb_winevt_subscribe_next(self)) {
419
- rb_ensure(
420
- rb_winevt_subscribe_each_yield, self, rb_winevt_subscribe_close_handle, self);
421
- }
422
-
423
- return Qnil;
424
- }
425
-
426
- /*
427
- * This method renders bookmark content which is related to Subscribe class instance.
428
- *
429
- * @return [String]
430
- */
431
- static VALUE
432
- rb_winevt_subscribe_get_bookmark(VALUE self)
433
- {
434
- struct WinevtSubscribe* winevtSubscribe;
435
-
436
- TypedData_Get_Struct(
437
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
438
-
439
- return render_to_rb_str(winevtSubscribe->bookmark, EvtRenderBookmark);
440
- }
441
-
442
- /*
443
- * This method returns rate limit value.
444
- *
445
- * @since 0.6.0
446
- * @return [Integer]
447
- */
448
- static VALUE
449
- rb_winevt_subscribe_get_rate_limit(VALUE self)
450
- {
451
- struct WinevtSubscribe* winevtSubscribe;
452
-
453
- TypedData_Get_Struct(
454
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
455
-
456
- return INT2NUM(winevtSubscribe->rateLimit);
457
- }
458
-
459
- /*
460
- * This method specifies rate limit value.
461
- *
462
- * @since 0.6.0
463
- * @param rb_rate_limit [Integer] rate_limit value
464
- */
465
- static VALUE
466
- rb_winevt_subscribe_set_rate_limit(VALUE self, VALUE rb_rate_limit)
467
- {
468
- struct WinevtSubscribe* winevtSubscribe;
469
- DWORD rateLimit;
470
-
471
- TypedData_Get_Struct(
472
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
473
-
474
- rateLimit = NUM2LONG(rb_rate_limit);
475
-
476
- if ((rateLimit != SUBSCRIBE_RATE_INFINITE) && (rateLimit < 10 || rateLimit % 10)) {
477
- rb_raise(rb_eArgError, "Specify a multiples of 10 or RATE_INFINITE constant");
478
- } else {
479
- winevtSubscribe->rateLimit = rateLimit;
480
- }
481
-
482
- return Qnil;
483
- }
484
-
485
- /*
486
- * This method returns whether render as xml or not.
487
- *
488
- * @since 0.6.0
489
- * @return [Boolean]
490
- */
491
- static VALUE
492
- rb_winevt_subscribe_render_as_xml_p(VALUE self)
493
- {
494
- struct WinevtSubscribe* winevtSubscribe;
495
-
496
- TypedData_Get_Struct(
497
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
498
-
499
- return winevtSubscribe->renderAsXML ? Qtrue : Qfalse;
500
- }
501
-
502
- /*
503
- * This method specifies whether render as xml or not.
504
- *
505
- * @since 0.6.0
506
- * @param rb_render_as_xml [Boolean]
507
- */
508
- static VALUE
509
- rb_winevt_subscribe_set_render_as_xml(VALUE self, VALUE rb_render_as_xml)
510
- {
511
- struct WinevtSubscribe* winevtSubscribe;
512
-
513
- TypedData_Get_Struct(
514
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
515
-
516
- winevtSubscribe->renderAsXML = RTEST(rb_render_as_xml);
517
-
518
- return Qnil;
519
- }
520
-
521
- /*
522
- * This method specifies whether preserving qualifiers key or not.
523
- *
524
- * @since 0.7.3
525
- * @param rb_render_as_xml [Boolean]
526
- */
527
- static VALUE
528
- rb_winevt_subscribe_set_preserve_qualifiers(VALUE self, VALUE rb_preserve_qualifiers)
529
- {
530
- struct WinevtSubscribe* winevtSubscribe;
531
-
532
- TypedData_Get_Struct(
533
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
534
-
535
- winevtSubscribe->preserveQualifiers = RTEST(rb_preserve_qualifiers);
536
-
537
- return Qnil;
538
- }
539
-
540
- /*
541
- * This method returns whether preserving qualifiers or not.
542
- *
543
- * @since 0.7.3
544
- * @return [Integer]
545
- */
546
- static VALUE
547
- rb_winevt_subscribe_get_preserve_qualifiers_p(VALUE self)
548
- {
549
- struct WinevtSubscribe* winevtSubscribe;
550
-
551
- TypedData_Get_Struct(
552
- self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
553
-
554
- return winevtSubscribe->preserveQualifiers ? Qtrue : Qfalse;
555
- }
556
-
557
- void
558
- Init_winevt_subscribe(VALUE rb_cEventLog)
559
- {
560
- rb_cSubscribe = rb_define_class_under(rb_cEventLog, "Subscribe", rb_cObject);
561
-
562
- rb_define_alloc_func(rb_cSubscribe, rb_winevt_subscribe_alloc);
563
-
564
- /*
565
- * For Subscribe#rate_limit=. It represents unspecified rate limit.
566
- * @since 0.6.0
567
- */
568
- rb_define_const(rb_cSubscribe, "RATE_INFINITE", SUBSCRIBE_RATE_INFINITE);
569
-
570
- rb_define_method(rb_cSubscribe, "initialize", rb_winevt_subscribe_initialize, 0);
571
- rb_define_method(rb_cSubscribe, "subscribe", rb_winevt_subscribe_subscribe, -1);
572
- rb_define_method(rb_cSubscribe, "next", rb_winevt_subscribe_next, 0);
573
- rb_define_method(rb_cSubscribe, "each", rb_winevt_subscribe_each, 0);
574
- rb_define_method(rb_cSubscribe, "bookmark", rb_winevt_subscribe_get_bookmark, 0);
575
- /*
576
- * @since 0.7.0
577
- */
578
- rb_define_method(rb_cSubscribe, "read_existing_events?", rb_winevt_subscribe_read_existing_events_p, 0);
579
- /*
580
- * @since 0.7.0
581
- */
582
- rb_define_method(rb_cSubscribe, "read_existing_events=", rb_winevt_subscribe_set_read_existing_events, 1);
583
- rb_define_method(rb_cSubscribe, "rate_limit", rb_winevt_subscribe_get_rate_limit, 0);
584
- rb_define_method(rb_cSubscribe, "rate_limit=", rb_winevt_subscribe_set_rate_limit, 1);
585
- rb_define_method(
586
- rb_cSubscribe, "render_as_xml?", rb_winevt_subscribe_render_as_xml_p, 0);
587
- rb_define_method(
588
- rb_cSubscribe, "render_as_xml=", rb_winevt_subscribe_set_render_as_xml, 1);
589
- /*
590
- * @since 0.7.3
591
- */
592
- rb_define_method(
593
- rb_cSubscribe, "preserve_qualifiers?", rb_winevt_subscribe_get_preserve_qualifiers_p, 0);
594
- /*
595
- * @since 0.7.3
596
- */
597
- rb_define_method(
598
- rb_cSubscribe, "preserve_qualifiers=", rb_winevt_subscribe_set_preserve_qualifiers, 1);
599
- }
1
+ #include <winevt_c.h>
2
+
3
+ /* clang-format off */
4
+ /*
5
+ * Document-class: Winevt::EventLog::Subscribe
6
+ *
7
+ * Subscribe Windows EventLog channel.
8
+ *
9
+ * @example
10
+ * require 'winevt'
11
+ *
12
+ * @subscribe = Winevt::EventLog::Subscribe.new
13
+ * @subscribe.tail = true
14
+ * @subscribe.rate_limit = 80
15
+ * @subscribe.subscribe(
16
+ * "Application", "*[System[(Level <= 4) and TimeCreated[timediff(@SystemTime) <= 86400000]]]"
17
+ * )
18
+ * while true do
19
+ * @subscribe.each do |eventlog, message, string_inserts|
20
+ * puts ({eventlog: eventlog, data: message})
21
+ * end
22
+ * sleep(0.1)
23
+ * end
24
+ *
25
+ * @see https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtsubscribe
26
+ */
27
+ /* clang-format on */
28
+
29
+ static void subscribe_free(void* ptr);
30
+
31
+ static const rb_data_type_t rb_winevt_subscribe_type = { "winevt/subscribe",
32
+ {
33
+ 0,
34
+ subscribe_free,
35
+ 0,
36
+ },
37
+ NULL,
38
+ NULL,
39
+ RUBY_TYPED_FREE_IMMEDIATELY };
40
+
41
+ static void
42
+ close_handles(struct WinevtSubscribe* winevtSubscribe)
43
+ {
44
+ if (winevtSubscribe->signalEvent) {
45
+ CloseHandle(winevtSubscribe->signalEvent);
46
+ winevtSubscribe->signalEvent = NULL;
47
+ }
48
+
49
+ if (winevtSubscribe->subscription) {
50
+ EvtClose(winevtSubscribe->subscription);
51
+ winevtSubscribe->subscription = NULL;
52
+ }
53
+
54
+ if (winevtSubscribe->bookmark) {
55
+ EvtClose(winevtSubscribe->bookmark);
56
+ winevtSubscribe->bookmark = NULL;
57
+ }
58
+
59
+ for (int i = 0; i < winevtSubscribe->count; i++) {
60
+ if (winevtSubscribe->hEvents[i]) {
61
+ EvtClose(winevtSubscribe->hEvents[i]);
62
+ winevtSubscribe->hEvents[i] = NULL;
63
+ }
64
+ }
65
+ winevtSubscribe->count = 0;
66
+
67
+ if (winevtSubscribe->remoteHandle) {
68
+ EvtClose(winevtSubscribe->remoteHandle);
69
+ winevtSubscribe->remoteHandle = NULL;
70
+ }
71
+ }
72
+
73
+ static void
74
+ subscribe_free(void* ptr)
75
+ {
76
+ struct WinevtSubscribe* winevtSubscribe = (struct WinevtSubscribe*)ptr;
77
+ close_handles(winevtSubscribe);
78
+
79
+ xfree(ptr);
80
+ }
81
+
82
+ static VALUE
83
+ rb_winevt_subscribe_alloc(VALUE klass)
84
+ {
85
+ VALUE obj;
86
+ struct WinevtSubscribe* winevtSubscribe;
87
+ obj = TypedData_Make_Struct(
88
+ klass, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
89
+ return obj;
90
+ }
91
+
92
+ /*
93
+ * Initalize Subscribe class.
94
+ *
95
+ * @return [Subscribe]
96
+ *
97
+ */
98
+ static VALUE
99
+ rb_winevt_subscribe_initialize(VALUE self)
100
+ {
101
+ struct WinevtSubscribe* winevtSubscribe;
102
+
103
+ TypedData_Get_Struct(
104
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
105
+
106
+ winevtSubscribe->rateLimit = SUBSCRIBE_RATE_INFINITE;
107
+ winevtSubscribe->lastTime = 0;
108
+ winevtSubscribe->currentRate = 0;
109
+ winevtSubscribe->renderAsXML = TRUE;
110
+ winevtSubscribe->readExistingEvents = TRUE;
111
+ winevtSubscribe->preserveQualifiers = FALSE;
112
+ winevtSubscribe->localeInfo = &default_locale;
113
+
114
+ return Qnil;
115
+ }
116
+
117
+ /*
118
+ * This method specifies whether read existing events or not.
119
+ *
120
+ * @param rb_read_existing_events_p [Boolean]
121
+ */
122
+ static VALUE
123
+ rb_winevt_subscribe_set_read_existing_events(VALUE self, VALUE rb_read_existing_events_p)
124
+ {
125
+ struct WinevtSubscribe* winevtSubscribe;
126
+
127
+ TypedData_Get_Struct(
128
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
129
+
130
+ winevtSubscribe->readExistingEvents = RTEST(rb_read_existing_events_p);
131
+
132
+ return Qnil;
133
+ }
134
+
135
+ /*
136
+ * This method returns whether read existing events or not.
137
+ *
138
+ * @return [Boolean]
139
+ */
140
+ static VALUE
141
+ rb_winevt_subscribe_read_existing_events_p(VALUE self)
142
+ {
143
+ struct WinevtSubscribe* winevtSubscribe;
144
+
145
+ TypedData_Get_Struct(
146
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
147
+
148
+ return winevtSubscribe->readExistingEvents ? Qtrue : Qfalse;
149
+ }
150
+
151
+ /*
152
+ * Subscribe into a Windows EventLog channel.
153
+ *
154
+ * @overload subscribe(path, query, bookmark=nil, session=nil)
155
+ * @param path [String] Subscribe Channel
156
+ * @param query [String] Query string for channel
157
+ * @param bookmark [Bookmark] bookmark Bookmark class instance.
158
+ * @param session [Session] Session information for remoting access.
159
+ * @return [Boolean]
160
+ *
161
+ */
162
+ static VALUE
163
+ rb_winevt_subscribe_subscribe(int argc, VALUE* argv, VALUE self)
164
+ {
165
+ VALUE rb_path, rb_query, rb_bookmark, rb_session;
166
+ EVT_HANDLE hSubscription = NULL, hBookmark = NULL;
167
+ HANDLE hSignalEvent;
168
+ EVT_HANDLE hRemoteHandle = NULL;
169
+ DWORD len, flags = 0L;
170
+ DWORD err = ERROR_SUCCESS;
171
+ VALUE wpathBuf, wqueryBuf, wBookmarkBuf;
172
+ PWSTR path, query, bookmarkXml;
173
+ DWORD status = ERROR_SUCCESS;
174
+ struct WinevtSession* winevtSession;
175
+ struct WinevtSubscribe* winevtSubscribe;
176
+
177
+ hSignalEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
178
+
179
+ TypedData_Get_Struct(
180
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
181
+
182
+ rb_scan_args(argc, argv, "22", &rb_path, &rb_query, &rb_bookmark, &rb_session);
183
+ Check_Type(rb_path, T_STRING);
184
+ Check_Type(rb_query, T_STRING);
185
+
186
+ if (rb_obj_is_kind_of(rb_bookmark, rb_cString)) {
187
+ // bookmarkXml : To wide char
188
+ len = MultiByteToWideChar(
189
+ CP_UTF8, 0, RSTRING_PTR(rb_bookmark), RSTRING_LEN(rb_bookmark), NULL, 0);
190
+ bookmarkXml = ALLOCV_N(WCHAR, wBookmarkBuf, len + 1);
191
+ MultiByteToWideChar(CP_UTF8,
192
+ 0,
193
+ RSTRING_PTR(rb_bookmark),
194
+ RSTRING_LEN(rb_bookmark),
195
+ bookmarkXml,
196
+ len);
197
+ bookmarkXml[len] = L'\0';
198
+ hBookmark = EvtCreateBookmark(bookmarkXml);
199
+ ALLOCV_END(wBookmarkBuf);
200
+ if (hBookmark == NULL) {
201
+ status = GetLastError();
202
+ raise_system_error(rb_eWinevtQueryError, status);
203
+ }
204
+ }
205
+ if (rb_obj_is_kind_of(rb_session, rb_cSession)) {
206
+ winevtSession = EventSession(rb_session);
207
+ hRemoteHandle = connect_to_remote(winevtSession->server,
208
+ winevtSession->domain,
209
+ winevtSession->username,
210
+ winevtSession->password,
211
+ winevtSession->flags);
212
+
213
+ err = GetLastError();
214
+ if (err != ERROR_SUCCESS) {
215
+ raise_system_error(rb_eRuntimeError, err);
216
+ }
217
+ }
218
+
219
+ // path : To wide char
220
+ len =
221
+ MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_path), RSTRING_LEN(rb_path), NULL, 0);
222
+ path = ALLOCV_N(WCHAR, wpathBuf, len + 1);
223
+ MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_path), RSTRING_LEN(rb_path), path, len);
224
+ path[len] = L'\0';
225
+
226
+ // query : To wide char
227
+ len = MultiByteToWideChar(
228
+ CP_UTF8, 0, RSTRING_PTR(rb_query), RSTRING_LEN(rb_query), NULL, 0);
229
+ query = ALLOCV_N(WCHAR, wqueryBuf, len + 1);
230
+ MultiByteToWideChar(
231
+ CP_UTF8, 0, RSTRING_PTR(rb_query), RSTRING_LEN(rb_query), query, len);
232
+ query[len] = L'\0';
233
+
234
+ if (hBookmark) {
235
+ flags |= EvtSubscribeStartAfterBookmark;
236
+ } else if (winevtSubscribe->readExistingEvents) {
237
+ flags |= EvtSubscribeStartAtOldestRecord;
238
+ } else {
239
+ flags |= EvtSubscribeToFutureEvents;
240
+ }
241
+
242
+ hSubscription =
243
+ EvtSubscribe(hRemoteHandle, hSignalEvent, path, query, hBookmark, NULL, NULL, flags);
244
+ if (!hSubscription) {
245
+ if (hBookmark != NULL) {
246
+ EvtClose(hBookmark);
247
+ }
248
+ if (hSignalEvent != NULL) {
249
+ CloseHandle(hSignalEvent);
250
+ }
251
+ status = GetLastError();
252
+ if (rb_obj_is_kind_of(rb_session, rb_cSession)) {
253
+ rb_raise(rb_eRemoteHandlerError, "Remoting subscription is not working. errCode: %ld\n", status);
254
+ } else {
255
+ raise_system_error(rb_eWinevtQueryError, status);
256
+ }
257
+ }
258
+
259
+ if (winevtSubscribe->subscription != NULL) {
260
+ // should be disgarded the old event subscription handle.
261
+ EvtClose(winevtSubscribe->subscription);
262
+ }
263
+
264
+ ALLOCV_END(wpathBuf);
265
+ ALLOCV_END(wqueryBuf);
266
+
267
+ winevtSubscribe->signalEvent = hSignalEvent;
268
+ winevtSubscribe->subscription = hSubscription;
269
+ winevtSubscribe->remoteHandle = hRemoteHandle;
270
+ if (hBookmark) {
271
+ winevtSubscribe->bookmark = hBookmark;
272
+ } else {
273
+ winevtSubscribe->bookmark = EvtCreateBookmark(NULL);
274
+ if (winevtSubscribe->bookmark == NULL) {
275
+ if (hSubscription != NULL) {
276
+ EvtClose(hSubscription);
277
+ }
278
+ if (hSignalEvent != NULL) {
279
+ CloseHandle(hSignalEvent);
280
+ }
281
+ status = GetLastError();
282
+ raise_system_error(rb_eWinevtQueryError, status);
283
+ }
284
+ }
285
+
286
+ return Qtrue;
287
+ }
288
+
289
+ BOOL
290
+ is_rate_limit_exceeded(struct WinevtSubscribe* winevtSubscribe)
291
+ {
292
+ time_t now;
293
+
294
+ if (winevtSubscribe->rateLimit == SUBSCRIBE_RATE_INFINITE)
295
+ return FALSE;
296
+
297
+ time(&now);
298
+
299
+ if (now <= winevtSubscribe->lastTime) {
300
+ if (winevtSubscribe->currentRate >= winevtSubscribe->rateLimit) {
301
+ return TRUE;
302
+ }
303
+ } else {
304
+ winevtSubscribe->currentRate = 0;
305
+ }
306
+
307
+ return FALSE;
308
+ }
309
+
310
+ void
311
+ update_to_reflect_rate_limit_state(struct WinevtSubscribe* winevtSubscribe, ULONG count)
312
+ {
313
+ time_t lastTime = 0;
314
+
315
+ if (winevtSubscribe->rateLimit == SUBSCRIBE_RATE_INFINITE)
316
+ return;
317
+
318
+ time(&lastTime);
319
+ winevtSubscribe->lastTime = lastTime;
320
+ winevtSubscribe->currentRate += count;
321
+ }
322
+
323
+ /*
324
+ * Handle the next values. Since v0.6.0, this method is used for
325
+ * testing only. Please use #each instead.
326
+ *
327
+ * @return [Boolean]
328
+ *
329
+ * @see each
330
+ */
331
+
332
+ static VALUE
333
+ rb_winevt_subscribe_next(VALUE self)
334
+ {
335
+ EVT_HANDLE hEvents[SUBSCRIBE_ARRAY_SIZE];
336
+ ULONG count = 0;
337
+ DWORD status = ERROR_SUCCESS;
338
+ struct WinevtSubscribe* winevtSubscribe;
339
+
340
+ TypedData_Get_Struct(
341
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
342
+
343
+ if (is_rate_limit_exceeded(winevtSubscribe)) {
344
+ return Qfalse;
345
+ }
346
+
347
+ /* If subscription handle is NULL, it should return false. */
348
+ if (!winevtSubscribe->subscription) {
349
+ return Qfalse;
350
+ }
351
+
352
+ if (!EvtNext(winevtSubscribe->subscription,
353
+ SUBSCRIBE_ARRAY_SIZE,
354
+ hEvents,
355
+ INFINITE,
356
+ 0,
357
+ &count)) {
358
+ status = GetLastError();
359
+ if (ERROR_CANCELLED == status) {
360
+ return Qfalse;
361
+ }
362
+ if (ERROR_NO_MORE_ITEMS != status) {
363
+ return Qfalse;
364
+ }
365
+ }
366
+
367
+ if (status == ERROR_SUCCESS) {
368
+ winevtSubscribe->count = count;
369
+ for (int i = 0; i < count; i++) {
370
+ winevtSubscribe->hEvents[i] = hEvents[i];
371
+ EvtUpdateBookmark(winevtSubscribe->bookmark, winevtSubscribe->hEvents[i]);
372
+ }
373
+
374
+ update_to_reflect_rate_limit_state(winevtSubscribe, count);
375
+
376
+ return Qtrue;
377
+ }
378
+
379
+ return Qfalse;
380
+ }
381
+
382
+ static VALUE
383
+ rb_winevt_subscribe_render(VALUE self, EVT_HANDLE event)
384
+ {
385
+ struct WinevtSubscribe* winevtSubscribe;
386
+
387
+ TypedData_Get_Struct(
388
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
389
+
390
+ if (winevtSubscribe->renderAsXML) {
391
+ return render_to_rb_str(event, EvtRenderEventXml);
392
+ } else {
393
+ return render_system_event(event, winevtSubscribe->preserveQualifiers);
394
+ }
395
+ }
396
+
397
+ static VALUE
398
+ rb_winevt_subscribe_message(EVT_HANDLE event, LocaleInfo* localeInfo, EVT_HANDLE hRemote)
399
+ {
400
+ WCHAR* wResult;
401
+ VALUE utf8str;
402
+
403
+ wResult = get_description(event, localeInfo->langID, hRemote);
404
+ utf8str = wstr_to_rb_str(CP_UTF8, wResult, -1);
405
+ free(wResult);
406
+
407
+ return utf8str;
408
+ }
409
+
410
+ static VALUE
411
+ rb_winevt_subscribe_string_inserts(EVT_HANDLE event)
412
+ {
413
+ return get_values(event);
414
+ }
415
+
416
+ static VALUE
417
+ rb_winevt_subscribe_close_handle(VALUE self)
418
+ {
419
+ struct WinevtSubscribe* winevtSubscribe;
420
+
421
+ TypedData_Get_Struct(
422
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
423
+
424
+ for (int i = 0; i < winevtSubscribe->count; i++) {
425
+ if (winevtSubscribe->hEvents[i] != NULL) {
426
+ EvtClose(winevtSubscribe->hEvents[i]);
427
+ winevtSubscribe->hEvents[i] = NULL;
428
+ }
429
+ }
430
+
431
+ return Qnil;
432
+ }
433
+
434
+ static VALUE
435
+ rb_winevt_subscribe_each_yield(VALUE self)
436
+ {
437
+ RETURN_ENUMERATOR(self, 0, 0);
438
+ struct WinevtSubscribe* winevtSubscribe;
439
+
440
+ TypedData_Get_Struct(
441
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
442
+
443
+ for (int i = 0; i < winevtSubscribe->count; i++) {
444
+ rb_yield_values(3,
445
+ rb_winevt_subscribe_render(self, winevtSubscribe->hEvents[i]),
446
+ rb_winevt_subscribe_message(winevtSubscribe->hEvents[i], winevtSubscribe->localeInfo,
447
+ winevtSubscribe->remoteHandle),
448
+ rb_winevt_subscribe_string_inserts(winevtSubscribe->hEvents[i]));
449
+ }
450
+
451
+ return Qnil;
452
+ }
453
+
454
+ /*
455
+ * Enumerate to obtain Windows EventLog contents.
456
+ *
457
+ * This method yields the following:
458
+ * (Stringified EventLog, Stringified detail message, Stringified
459
+ * insert values)
460
+ *
461
+ * @yield (String,String,String)
462
+ *
463
+ */
464
+ static VALUE
465
+ rb_winevt_subscribe_each(VALUE self)
466
+ {
467
+ RETURN_ENUMERATOR(self, 0, 0);
468
+
469
+ while (rb_winevt_subscribe_next(self)) {
470
+ rb_ensure(
471
+ rb_winevt_subscribe_each_yield, self, rb_winevt_subscribe_close_handle, self);
472
+ }
473
+
474
+ return Qnil;
475
+ }
476
+
477
+ /*
478
+ * This method renders bookmark content which is related to Subscribe class instance.
479
+ *
480
+ * @return [String]
481
+ */
482
+ static VALUE
483
+ rb_winevt_subscribe_get_bookmark(VALUE self)
484
+ {
485
+ struct WinevtSubscribe* winevtSubscribe;
486
+
487
+ TypedData_Get_Struct(
488
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
489
+
490
+ return render_to_rb_str(winevtSubscribe->bookmark, EvtRenderBookmark);
491
+ }
492
+
493
+ /*
494
+ * This method returns rate limit value.
495
+ *
496
+ * @since 0.6.0
497
+ * @return [Integer]
498
+ */
499
+ static VALUE
500
+ rb_winevt_subscribe_get_rate_limit(VALUE self)
501
+ {
502
+ struct WinevtSubscribe* winevtSubscribe;
503
+
504
+ TypedData_Get_Struct(
505
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
506
+
507
+ return INT2NUM(winevtSubscribe->rateLimit);
508
+ }
509
+
510
+ /*
511
+ * This method specifies rate limit value.
512
+ *
513
+ * @since 0.6.0
514
+ * @param rb_rate_limit [Integer] rate_limit value
515
+ */
516
+ static VALUE
517
+ rb_winevt_subscribe_set_rate_limit(VALUE self, VALUE rb_rate_limit)
518
+ {
519
+ struct WinevtSubscribe* winevtSubscribe;
520
+ DWORD rateLimit;
521
+
522
+ TypedData_Get_Struct(
523
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
524
+
525
+ rateLimit = NUM2LONG(rb_rate_limit);
526
+
527
+ if ((rateLimit != SUBSCRIBE_RATE_INFINITE) && (rateLimit < 10 || rateLimit % 10)) {
528
+ rb_raise(rb_eArgError, "Specify a multiples of 10 or RATE_INFINITE constant");
529
+ } else {
530
+ winevtSubscribe->rateLimit = rateLimit;
531
+ }
532
+
533
+ return Qnil;
534
+ }
535
+
536
+ /*
537
+ * This method returns whether render as xml or not.
538
+ *
539
+ * @since 0.6.0
540
+ * @return [Boolean]
541
+ */
542
+ static VALUE
543
+ rb_winevt_subscribe_render_as_xml_p(VALUE self)
544
+ {
545
+ struct WinevtSubscribe* winevtSubscribe;
546
+
547
+ TypedData_Get_Struct(
548
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
549
+
550
+ return winevtSubscribe->renderAsXML ? Qtrue : Qfalse;
551
+ }
552
+
553
+ /*
554
+ * This method specifies whether render as xml or not.
555
+ *
556
+ * @since 0.6.0
557
+ * @param rb_render_as_xml [Boolean]
558
+ */
559
+ static VALUE
560
+ rb_winevt_subscribe_set_render_as_xml(VALUE self, VALUE rb_render_as_xml)
561
+ {
562
+ struct WinevtSubscribe* winevtSubscribe;
563
+
564
+ TypedData_Get_Struct(
565
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
566
+
567
+ winevtSubscribe->renderAsXML = RTEST(rb_render_as_xml);
568
+
569
+ return Qnil;
570
+ }
571
+
572
+ /*
573
+ * This method specifies whether preserving qualifiers key or not.
574
+ *
575
+ * @since 0.7.3
576
+ * @param rb_preserve_qualifiers [Boolean]
577
+ */
578
+ static VALUE
579
+ rb_winevt_subscribe_set_preserve_qualifiers(VALUE self, VALUE rb_preserve_qualifiers)
580
+ {
581
+ struct WinevtSubscribe* winevtSubscribe;
582
+
583
+ TypedData_Get_Struct(
584
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
585
+
586
+ winevtSubscribe->preserveQualifiers = RTEST(rb_preserve_qualifiers);
587
+
588
+ return Qnil;
589
+ }
590
+
591
+ /*
592
+ * This method returns whether preserving qualifiers or not.
593
+ *
594
+ * @since 0.7.3
595
+ * @return [Integer]
596
+ */
597
+ static VALUE
598
+ rb_winevt_subscribe_get_preserve_qualifiers_p(VALUE self)
599
+ {
600
+ struct WinevtSubscribe* winevtSubscribe;
601
+
602
+ TypedData_Get_Struct(
603
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
604
+
605
+ return winevtSubscribe->preserveQualifiers ? Qtrue : Qfalse;
606
+ }
607
+
608
+ /*
609
+ * This method specifies locale with [String].
610
+ *
611
+ * @since 0.8.0
612
+ * @param rb_locale_str [String]
613
+ */
614
+ static VALUE
615
+ rb_winevt_subscribe_set_locale(VALUE self, VALUE rb_locale_str)
616
+ {
617
+ struct WinevtSubscribe* winevtSubscribe;
618
+ LocaleInfo* locale_info = &default_locale;
619
+
620
+ TypedData_Get_Struct(
621
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
622
+
623
+ locale_info = get_locale_info_from_rb_str(rb_locale_str);
624
+
625
+ winevtSubscribe->localeInfo = locale_info;
626
+
627
+ return Qnil;
628
+ }
629
+
630
+ /*
631
+ * This method obtains specified locale with [String].
632
+ *
633
+ * @since 0.8.0
634
+ */
635
+ static VALUE
636
+ rb_winevt_subscribe_get_locale(VALUE self)
637
+ {
638
+ struct WinevtSubscribe* winevtSubscribe;
639
+
640
+ TypedData_Get_Struct(
641
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
642
+
643
+ if (winevtSubscribe->localeInfo->langCode) {
644
+ return rb_str_new2(winevtSubscribe->localeInfo->langCode);
645
+ } else {
646
+ return rb_str_new2(default_locale.langCode);
647
+ }
648
+ }
649
+
650
+ /*
651
+ * This method cancels channel subscription.
652
+ *
653
+ * @return [Boolean]
654
+ * @since 0.9.1
655
+ */
656
+ static VALUE
657
+ rb_winevt_subscribe_cancel(VALUE self)
658
+ {
659
+ struct WinevtSubscribe* winevtSubscribe;
660
+ BOOL result = FALSE;
661
+
662
+ TypedData_Get_Struct(
663
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
664
+
665
+ if (winevtSubscribe->subscription) {
666
+ result = EvtCancel(winevtSubscribe->subscription);
667
+ }
668
+
669
+ if (result) {
670
+ return Qtrue;
671
+ } else {
672
+ return Qfalse;
673
+ }
674
+ }
675
+
676
+ /*
677
+ * This method closes channel handles forcibly.
678
+ *
679
+ * @since 0.9.1
680
+ */
681
+ static VALUE
682
+ rb_winevt_subscribe_close(VALUE self)
683
+ {
684
+ struct WinevtSubscribe* winevtSubscribe;
685
+
686
+ TypedData_Get_Struct(
687
+ self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
688
+
689
+ close_handles(winevtSubscribe);
690
+
691
+ return Qnil;
692
+ }
693
+
694
+
695
+ void
696
+ Init_winevt_subscribe(VALUE rb_cEventLog)
697
+ {
698
+ rb_cSubscribe = rb_define_class_under(rb_cEventLog, "Subscribe", rb_cObject);
699
+
700
+ rb_define_alloc_func(rb_cSubscribe, rb_winevt_subscribe_alloc);
701
+
702
+ /*
703
+ * For Subscribe#rate_limit=. It represents unspecified rate limit.
704
+ * @since 0.6.0
705
+ */
706
+ rb_define_const(rb_cSubscribe, "RATE_INFINITE", SUBSCRIBE_RATE_INFINITE);
707
+
708
+ rb_define_method(rb_cSubscribe, "initialize", rb_winevt_subscribe_initialize, 0);
709
+ rb_define_method(rb_cSubscribe, "subscribe", rb_winevt_subscribe_subscribe, -1);
710
+ rb_define_method(rb_cSubscribe, "next", rb_winevt_subscribe_next, 0);
711
+ rb_define_method(rb_cSubscribe, "each", rb_winevt_subscribe_each, 0);
712
+ rb_define_method(rb_cSubscribe, "bookmark", rb_winevt_subscribe_get_bookmark, 0);
713
+ /*
714
+ * @since 0.7.0
715
+ */
716
+ rb_define_method(rb_cSubscribe, "read_existing_events?", rb_winevt_subscribe_read_existing_events_p, 0);
717
+ /*
718
+ * @since 0.7.0
719
+ */
720
+ rb_define_method(rb_cSubscribe, "read_existing_events=", rb_winevt_subscribe_set_read_existing_events, 1);
721
+ rb_define_method(rb_cSubscribe, "rate_limit", rb_winevt_subscribe_get_rate_limit, 0);
722
+ rb_define_method(rb_cSubscribe, "rate_limit=", rb_winevt_subscribe_set_rate_limit, 1);
723
+ rb_define_method(
724
+ rb_cSubscribe, "render_as_xml?", rb_winevt_subscribe_render_as_xml_p, 0);
725
+ rb_define_method(
726
+ rb_cSubscribe, "render_as_xml=", rb_winevt_subscribe_set_render_as_xml, 1);
727
+ /*
728
+ * @since 0.7.3
729
+ */
730
+ rb_define_method(
731
+ rb_cSubscribe, "preserve_qualifiers?", rb_winevt_subscribe_get_preserve_qualifiers_p, 0);
732
+ /*
733
+ * @since 0.7.3
734
+ */
735
+ rb_define_method(
736
+ rb_cSubscribe, "preserve_qualifiers=", rb_winevt_subscribe_set_preserve_qualifiers, 1);
737
+ /*
738
+ * @since 0.8.0
739
+ */
740
+ rb_define_method(
741
+ rb_cSubscribe, "locale", rb_winevt_subscribe_get_locale, 0);
742
+ /*
743
+ * @since 0.8.0
744
+ */
745
+ rb_define_method(
746
+ rb_cSubscribe, "locale=", rb_winevt_subscribe_set_locale, 1);
747
+ /*
748
+ * @since 0.9.1
749
+ */
750
+ rb_define_method(
751
+ rb_cSubscribe, "cancel", rb_winevt_subscribe_cancel, 0);
752
+ /*
753
+ * @since 0.9.1
754
+ */
755
+ rb_define_method(
756
+ rb_cSubscribe, "close", rb_winevt_subscribe_close, 0);
757
+ }