3636static int in_winevtlog_collect (struct flb_input_instance * ins ,
3737 struct flb_config * config , void * in_context );
3838
39+ static wchar_t * convert_to_wide (char * str )
40+ {
41+ int size = 0 ;
42+ wchar_t * buf = NULL ;
43+ DWORD err ;
44+
45+ size = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , NULL , 0 );
46+ if (size == 0 ) {
47+ err = GetLastError ();
48+ flb_error ("[in_winevtlog] Failed MultiByteToWideChar with error code (%d)" , err );
49+ return NULL ;
50+ }
51+
52+ buf = flb_calloc (1 , sizeof (wchar_t ) * size );
53+ if (buf == NULL ) {
54+ flb_errno ();
55+ return NULL ;
56+ }
57+ size = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , buf , size );
58+ if (size == 0 ) {
59+ err = GetLastError ();
60+ flb_error ("[in_winevtlog] Failed MultiByteToWideChar with error code (%d)" , err );
61+ flb_free (buf );
62+ return NULL ;
63+ }
64+
65+ return buf ;
66+ }
67+
68+ static void in_winevtlog_session_destory (struct winevtlog_session * session );
69+
70+ static struct winevtlog_session * in_winevtlog_session_create (struct winevtlog_config * ctx ,
71+ struct flb_config * config ,
72+ int * status )
73+ {
74+ int len ;
75+ struct winevtlog_session * session ;
76+ PWSTR wtmp ;
77+
78+ if (ctx -> remote_server == NULL ) {
79+ * status = WINEVTLOG_SESSION_SERVER_EMPTY ;
80+ return NULL ;
81+ }
82+
83+ session = flb_calloc (1 , sizeof (struct winevtlog_session ));
84+ if (session == NULL ) {
85+ flb_errno ();
86+ * status = WINEVTLOG_SESSION_ALLOC_FAILED ;
87+ return NULL ;
88+ }
89+
90+ if (ctx -> remote_server != NULL ) {
91+ session -> server = convert_to_wide (ctx -> remote_server );
92+ if (session -> server == NULL ) {
93+ in_winevtlog_session_destory (session );
94+ * status = WINEVTLOG_SESSION_FAILED_TO_CONVERT_WIDE ;
95+ return NULL ;
96+ }
97+ }
98+
99+ if (ctx -> remote_domain != NULL ) {
100+ session -> domain = convert_to_wide (ctx -> remote_domain );
101+ if (session -> domain == NULL ) {
102+ in_winevtlog_session_destory (session );
103+ * status = WINEVTLOG_SESSION_FAILED_TO_CONVERT_WIDE ;
104+ return NULL ;
105+ }
106+ }
107+
108+ if (ctx -> remote_username != NULL ) {
109+ session -> username = convert_to_wide (ctx -> remote_username );
110+ if (session -> username == NULL ) {
111+ in_winevtlog_session_destory (session );
112+ * status = WINEVTLOG_SESSION_FAILED_TO_CONVERT_WIDE ;
113+ return NULL ;
114+ }
115+ }
116+
117+ if (ctx -> remote_password != NULL ) {
118+ session -> password = convert_to_wide (ctx -> remote_password );
119+ if (session -> password == NULL ) {
120+ in_winevtlog_session_destory (session );
121+ * status = WINEVTLOG_SESSION_FAILED_TO_CONVERT_WIDE ;
122+ return NULL ;
123+ }
124+ }
125+
126+ session -> flags = EvtRpcLoginAuthDefault ;
127+ * status = WINEVTLOG_SESSION_CREATE_OK ;
128+
129+ return session ;
130+ }
131+
132+ static void in_winevtlog_session_destory (struct winevtlog_session * session )
133+ {
134+ if (session -> server != NULL ) {
135+ flb_free (session -> server );
136+ }
137+
138+ if (session -> domain != NULL ) {
139+ flb_free (session -> domain );
140+ }
141+
142+ if (session -> username != NULL ) {
143+ flb_free (session -> username );
144+ }
145+
146+ if (session -> password != NULL ) {
147+ flb_free (session -> password );
148+ }
149+
150+ flb_free (session );
151+ }
152+
39153static int in_winevtlog_init (struct flb_input_instance * in ,
40154 struct flb_config * config , void * data )
41155{
@@ -46,6 +160,8 @@ static int in_winevtlog_init(struct flb_input_instance *in,
46160 struct mk_list * head ;
47161 struct winevtlog_channel * ch ;
48162 struct winevtlog_config * ctx ;
163+ struct winevtlog_session * session ;
164+ int status = WINEVTLOG_SESSION_CREATE_OK ;
49165
50166 /* Initialize context */
51167 ctx = flb_calloc (1 , sizeof (struct winevtlog_config ));
@@ -72,6 +188,18 @@ static int in_winevtlog_init(struct flb_input_instance *in,
72188 return -1 ;
73189 }
74190
191+ /* Initialize session context */
192+ session = in_winevtlog_session_create (ctx , config , & status );
193+ if (status == WINEVTLOG_SESSION_ALLOC_FAILED ||
194+ status == WINEVTLOG_SESSION_FAILED_TO_CONVERT_WIDE ) {
195+ flb_plg_error (in , "session is not created and invalid with %d" , status );
196+ return -1 ;
197+ }
198+ else if (session == NULL ) {
199+ flb_plg_debug (in , "session is not created. Connect to local machine." );
200+ }
201+ ctx -> session = session ;
202+
75203 /* Set up total reading size threshold */
76204 if (ctx -> total_size_threshold >= MINIMUM_THRESHOLD_SIZE &&
77205 ctx -> total_size_threshold <= MAXIMUM_THRESHOLD_SIZE ) {
@@ -235,6 +363,9 @@ static int in_winevtlog_exit(void *data, struct flb_config *config)
235363 if (ctx -> db ) {
236364 flb_sqldb_close (ctx -> db );
237365 }
366+ if (ctx -> session ) {
367+ in_winevtlog_session_destory (ctx -> session );
368+ }
238369 flb_free (ctx );
239370
240371 return 0 ;
@@ -296,6 +427,26 @@ static struct flb_config_map config_map[] = {
296427 0 , FLB_TRUE , offsetof(struct winevtlog_config , total_size_threshold ),
297428 "Specify reading limit for collecting Windows EventLog per a cycle"
298429 },
430+ {
431+ FLB_CONFIG_MAP_STR , "remote.server" , (char * )NULL ,
432+ 0 , FLB_TRUE , offsetof(struct winevtlog_config , remote_server ),
433+ "Specify server name of remote access for Windows EventLog"
434+ },
435+ {
436+ FLB_CONFIG_MAP_STR , "remote.domain" , (char * )NULL ,
437+ 0 , FLB_TRUE , offsetof(struct winevtlog_config , remote_domain ),
438+ "Specify domain name of remote access for Windows EventLog"
439+ },
440+ {
441+ FLB_CONFIG_MAP_STR , "remote.username" , (char * )NULL ,
442+ 0 , FLB_TRUE , offsetof(struct winevtlog_config , remote_username ),
443+ "Specify username of remote access for Windows EventLog"
444+ },
445+ {
446+ FLB_CONFIG_MAP_STR , "remote.password" , (char * )NULL ,
447+ 0 , FLB_TRUE , offsetof(struct winevtlog_config , remote_password ),
448+ "Specify password of remote access for Windows EventLog"
449+ },
299450 /* EOF */
300451 {0 }
301452};
0 commit comments