Skip to content

Commit 004c57e

Browse files
committed
query: subscribe: utils: Provide an option to turn on/off for expanding SID
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent dace409 commit 004c57e

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

ext/winevt/winevt_c.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ EVT_HANDLE connect_to_remote(LPWSTR computerName, LPWSTR domain,
4646
DWORD *error_code);
4747
WCHAR* get_description(EVT_HANDLE handle, LANGID langID, EVT_HANDLE hRemote);
4848
VALUE get_values(EVT_HANDLE handle);
49-
VALUE render_system_event(EVT_HANDLE handle, BOOL preserve_qualifiers);
49+
VALUE render_system_event(EVT_HANDLE handle, BOOL preserve_qualifiers, BOOL expandSID);
5050
LocaleInfo* get_locale_info_from_rb_str(VALUE rb_locale_str);
5151

5252
#ifdef __cplusplus
@@ -101,6 +101,7 @@ struct WinevtQuery
101101
LONG timeout;
102102
BOOL renderAsXML;
103103
BOOL preserveQualifiers;
104+
BOOL expandSID;
104105
LocaleInfo *localeInfo;
105106
EVT_HANDLE remoteHandle;
106107
};
@@ -122,6 +123,7 @@ struct WinevtSubscribe
122123
DWORD currentRate;
123124
BOOL renderAsXML;
124125
BOOL preserveQualifiers;
126+
BOOL expandSID;
125127
LocaleInfo* localeInfo;
126128
EVT_HANDLE remoteHandle;
127129
};

ext/winevt/winevt_query.c

+45-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
153153
winevtQuery->preserveQualifiers = FALSE;
154154
winevtQuery->localeInfo = &default_locale;
155155
winevtQuery->remoteHandle = hRemoteHandle;
156+
winevtQuery->expandSID = TRUE;
156157

157158
ALLOCV_END(wchannelBuf);
158159
ALLOCV_END(wpathBuf);
@@ -274,7 +275,8 @@ rb_winevt_query_render(VALUE self, EVT_HANDLE event)
274275
if (winevtQuery->renderAsXML) {
275276
return render_to_rb_str(event, EvtRenderEventXml);
276277
} else {
277-
return render_system_event(event, winevtQuery->preserveQualifiers);
278+
return render_system_event(event, winevtQuery->preserveQualifiers,
279+
winevtQuery->expandSID);
278280
}
279281
}
280282

@@ -535,6 +537,40 @@ rb_winevt_query_get_locale(VALUE self)
535537
}
536538
}
537539

540+
/*
541+
* This method specifies whether expanding SID or not.
542+
*
543+
* @param rb_expand_sid_p [Boolean]
544+
*/
545+
static VALUE
546+
rb_winevt_query_set_expand_sid(VALUE self, VALUE rb_expand_sid_p)
547+
{
548+
struct WinevtQuery* winevtQuery;
549+
550+
TypedData_Get_Struct(
551+
self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);
552+
553+
winevtQuery->expandSID = RTEST(rb_expand_sid_p);
554+
555+
return Qnil;
556+
}
557+
558+
/*
559+
* This method returns whether expanding SID or not.
560+
*
561+
* @return [Boolean]
562+
*/
563+
static VALUE
564+
rb_winevt_query_expand_sid_p(VALUE self)
565+
{
566+
struct WinevtQuery* winevtQuery;
567+
568+
TypedData_Get_Struct(
569+
self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);
570+
571+
return winevtQuery->expandSID ? Qtrue : Qfalse;
572+
}
573+
538574
/*
539575
* This method cancels channel query.
540576
*
@@ -683,6 +719,14 @@ Init_winevt_query(VALUE rb_cEventLog)
683719
* @since 0.8.0
684720
*/
685721
rb_define_method(rb_cQuery, "locale=", rb_winevt_query_set_locale, 1);
722+
/*
723+
* @since 0.10.3
724+
*/
725+
rb_define_method(rb_cQuery, "expand_sid?", rb_winevt_query_expand_sid_p, 0);
726+
/*
727+
* @since 0.10.3
728+
*/
729+
rb_define_method(rb_cQuery, "expand_sid=", rb_winevt_query_set_expand_sid, 1);
686730
/*
687731
* @since 0.9.1
688732
*/

ext/winevt/winevt_subscribe.c

+45-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ rb_winevt_subscribe_initialize(VALUE self)
110110
winevtSubscribe->readExistingEvents = TRUE;
111111
winevtSubscribe->preserveQualifiers = FALSE;
112112
winevtSubscribe->localeInfo = &default_locale;
113+
winevtSubscribe->expandSID = TRUE;
113114

114115
return Qnil;
115116
}
@@ -417,7 +418,8 @@ rb_winevt_subscribe_render(VALUE self, EVT_HANDLE event)
417418
if (winevtSubscribe->renderAsXML) {
418419
return render_to_rb_str(event, EvtRenderEventXml);
419420
} else {
420-
return render_system_event(event, winevtSubscribe->preserveQualifiers);
421+
return render_system_event(event, winevtSubscribe->preserveQualifiers,
422+
winevtSubscribe->expandSID);
421423
}
422424
}
423425

@@ -674,6 +676,40 @@ rb_winevt_subscribe_get_locale(VALUE self)
674676
}
675677
}
676678

679+
/*
680+
* This method specifies whether expanding SID or not.
681+
*
682+
* @param rb_expand_sid_p [Boolean]
683+
*/
684+
static VALUE
685+
rb_winevt_subscribe_set_expand_sid(VALUE self, VALUE rb_expand_sid_p)
686+
{
687+
struct WinevtSubscribe* winevtSubscribe;
688+
689+
TypedData_Get_Struct(
690+
self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
691+
692+
winevtSubscribe->expandSID = RTEST(rb_expand_sid_p);
693+
694+
return Qnil;
695+
}
696+
697+
/*
698+
* This method returns whether expanding SID or not.
699+
*
700+
* @return [Boolean]
701+
*/
702+
static VALUE
703+
rb_winevt_subscribe_expand_sid_p(VALUE self)
704+
{
705+
struct WinevtSubscribe* winevtSubscribe;
706+
707+
TypedData_Get_Struct(
708+
self, struct WinevtSubscribe, &rb_winevt_subscribe_type, winevtSubscribe);
709+
710+
return winevtSubscribe->expandSID ? Qtrue : Qfalse;
711+
}
712+
677713
/*
678714
* This method cancels channel subscription.
679715
*
@@ -771,6 +807,14 @@ Init_winevt_subscribe(VALUE rb_cEventLog)
771807
*/
772808
rb_define_method(
773809
rb_cSubscribe, "locale=", rb_winevt_subscribe_set_locale, 1);
810+
/*
811+
* @since 0.10.3
812+
*/
813+
rb_define_method(rb_cSubscribe, "expand_sid?", rb_winevt_subscribe_expand_sid_p, 0);
814+
/*
815+
* @since 0.10.3
816+
*/
817+
rb_define_method(rb_cSubscribe, "expand_sid=", rb_winevt_subscribe_set_expand_sid, 1);
774818
/*
775819
* @since 0.9.1
776820
*/

ext/winevt/winevt_utils.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static int ExpandSIDWString(PSID sid, CHAR **out_expanded)
646646
}
647647

648648
VALUE
649-
render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers)
649+
render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers, BOOL expandSID_p)
650650
{
651651
DWORD status = ERROR_SUCCESS;
652652
EVT_HANDLE hContext = NULL;
@@ -837,7 +837,8 @@ render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers)
837837
if (EvtVarTypeNull != pRenderedValues[EvtSystemUserID].Type) {
838838
if (ConvertSidToStringSid(pRenderedValues[EvtSystemUserID].SidVal, &pwsSid)) {
839839
CHAR *expandSID;
840-
if (ExpandSIDWString(pRenderedValues[EvtSystemUserID].SidVal,
840+
if (expandSID_p &&
841+
ExpandSIDWString(pRenderedValues[EvtSystemUserID].SidVal,
841842
&expandSID) == 0) {
842843
rbstr = rb_utf8_str_new_cstr(expandSID);
843844
} else {

0 commit comments

Comments
 (0)