summaryrefslogtreecommitdiff
path: root/smsd/log-event.c
blob: 1a482a4505a6c03af9618ca33dd82a4559311638 (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
/**
 * Windows event log logging backend.
 */

#include <winsock2.h>
#include <windows.h>
#include <winreg.h>
#include <stdio.h>
#include "smsd-event.h"
#include "core.h"

gboolean eventlog_deregister(void)
{
	LONG ret;

	ret = RegDeleteKey(
		HKEY_LOCAL_MACHINE,
		"System\\CurrentControlSet\\Services\\EventLog\\Application\\GammuSMSD"
		);

	return (ret == ERROR_SUCCESS);
}

gboolean eventlog_register(void)
{
	LONG ret;
	HKEY hKey;
	DWORD data;

	SECURITY_DESCRIPTOR SD;
	SECURITY_ATTRIBUTES SA;

	char program_name[MAX_PATH];

	if (GetModuleFileName(NULL, program_name, sizeof(program_name)) == 0)
		return FALSE;

	if (!InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION)) {
		return FALSE;
	}

	if(!SetSecurityDescriptorDacl(&SD, TRUE, 0, FALSE)) {
		return FALSE;
	}

	SA.nLength = sizeof(SA);
	SA.lpSecurityDescriptor = &SD;
	SA.bInheritHandle = FALSE;

	ret = RegCreateKeyEx(
		HKEY_LOCAL_MACHINE,
		"System\\CurrentControlSet\\Services\\EventLog\\Application\\GammuSMSD",
		0,
		NULL,
		REG_OPTION_NON_VOLATILE,
		KEY_WRITE,
		&SA,
		&hKey,
		NULL);

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to create registry key!\n");
		return FALSE;
	}

	data = 3;
	ret = RegSetValueEx(
		hKey,
		"CategoryCount",
		0,
		REG_DWORD,
		(BYTE *)&data,
		sizeof(DWORD));

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to write CategoryCount to registry!\n");
		return FALSE;
	}

	ret = RegSetValueEx(
		hKey,
		"CategoryMessageFile",
		0,
		REG_SZ,
		(BYTE *)program_name,
		strlen(program_name) + 1);

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to write CategoryMessageFile to registry!\n");
		return FALSE;
	}

	ret = RegSetValueEx(
		hKey,
		"EventMessageFile",
		0,
		REG_SZ,
		(BYTE *)program_name,
		(DWORD)strlen(program_name) + 1);

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to write EventMessageFile to registry!\n");
		return FALSE;
	}

	ret = RegSetValueEx(
		hKey,
		"ParameterMessageFile",
		0,
		REG_SZ,
		(BYTE *)program_name,
		(DWORD)strlen(program_name) + 1);

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to write ParameterMessageFile to registry!\n");
		return FALSE;
	}

	data = EVENTLOG_ERROR_TYPE | EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE;
	ret = RegSetValueEx(
		hKey,
		"TypesSupported",
		0,
		REG_DWORD,
		(BYTE *)&data,
		sizeof(DWORD));

	if (ret != ERROR_SUCCESS) {
		fprintf(stderr, "Failed to write TypesSupported to registry!\n");
		return FALSE;
	}

	RegCloseKey(hKey);

	return TRUE;
}

void *eventlog_init(void)
{
	HANDLE handle;
	handle = RegisterEventSource(NULL, "gammu-smsd");
	if (handle == NULL) {
	    fprintf(stderr, "Error opening event log!\n");
	}
	return (void *)handle;
}

void eventlog_log(void *handle, int level, const char *message)
{
	LPCTSTR lpstrings[1];
	WORD evtype = EVENTLOG_ERROR_TYPE;
	WORD eventcat = 0;
	DWORD eventid = 0;

	switch (level) {
		case DEBUG_ERROR:
			evtype = EVENTLOG_ERROR_TYPE;
			eventid = EVENT_MSG_ERROR;
			eventcat = EVENT_CAT_SMSD;
			break;
		case DEBUG_INFO:
			evtype = EVENTLOG_SUCCESS;
			eventid = EVENT_MSG_INFO;
			eventcat = EVENT_CAT_SMSD;
			break;
		case DEBUG_NOTICE:
			eventid = EVENT_MSG_NOTICE;
			evtype = EVENTLOG_INFORMATION_TYPE;
			eventcat = EVENT_CAT_SMSD;
			break;
		case DEBUG_SQL:
			eventid = EVENT_MSG_SQL;
			evtype = EVENTLOG_INFORMATION_TYPE;
			eventcat = EVENT_CAT_SQL;
			break;
		case DEBUG_GAMMU:
			eventid = EVENT_MSG_GAMMU;
			evtype = EVENTLOG_INFORMATION_TYPE;
			eventcat = EVENT_CAT_GAMMU;
			break;
		default:
			eventid = EVENT_MSG_OTHER;
			evtype = EVENTLOG_INFORMATION_TYPE;
			eventcat = EVENT_CAT_SMSD;
			break;
	}
	lpstrings[0] = message;
	/*
	 * @todo: 1024 is probably wrong, we should use mc to get proper
	 * event identifiers.
	 */
	ReportEvent(handle, evtype, eventcat, eventid, NULL, 1, 0,
		lpstrings, NULL);
}

void eventlog_close(void *handle)
{
	DeregisterEventSource(handle);
}

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */