-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkcaicon.c
329 lines (263 loc) · 9.27 KB
/
kcaicon.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2009 Secure Endpoints Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define NOSTRSAFE
#include "credprov.h"
#include <tchar.h>
#include <shellapi.h>
#include <htmlhelp.h>
#include <strsafe.h>
#include <assert.h>
static ATOM message_window_class = 0;
static HWND notifier_window = NULL;
static volatile BOOL notification_icon_added = FALSE;
#define TOKEN_ICON_ID 1
#define TOKEN_MESSAGE_ID WM_USER
static khm_int32
get_default_notifier_action(void)
{
khm_int32 cmd = KHUI_ACTION_OPEN_APP;
khc_read_int32(NULL, L"CredWindow\\NotificationAction", &cmd);
return cmd;
}
static void
prepare_context_menu(HMENU hmenu)
{
khm_int32 cmd;
wchar_t caption[128];
cmd = get_default_notifier_action();
if (cmd == KHUI_ACTION_NEW_CRED)
LoadString(hResModule, IDS_ACT_NEW, caption, ARRAYLENGTH(caption));
else
LoadString(hResModule, IDS_ACT_OPEN, caption, ARRAYLENGTH(caption));
ModifyMenu(hmenu, ID_DEFAULT, MF_STRING|MF_BYCOMMAND, ID_DEFAULT, caption);
SetMenuDefaultItem(hmenu, ID_DEFAULT, FALSE);
}
static void
handle_context_menu(void)
{
POINT pt;
HMENU hMenu;
HMENU hMenuBar;
GetCursorPos(&pt);
hMenuBar = LoadMenu(hResModule, MAKEINTRESOURCE(IDR_CTXMENU));
hMenu = GetSubMenu(hMenuBar, 0);
if (hMenu) {
prepare_context_menu(hMenu);
TrackPopupMenu(hMenu, TPM_NONOTIFY, pt.x, pt.y, 0, notifier_window, NULL);
}
{
NOTIFYICONDATA idata;
ZeroMemory(&idata, sizeof(idata));
Shell_NotifyIcon(NIM_SETFOCUS, &idata);
}
}
static LRESULT CALLBACK
notifier_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == TOKEN_MESSAGE_ID) {
switch (lParam) {
case NIN_SELECT:
case NIN_KEYSELECT:
{
NOTIFYICONDATA idata;
khm_int32 cmd = KHUI_ACTION_OPEN_APP;
khc_read_int32(NULL, L"CredWindow\\NotificationAction", &cmd);
khui_action_trigger(cmd, NULL);
ZeroMemory(&idata, sizeof(idata));
Shell_NotifyIcon(NIM_SETFOCUS, &idata);
}
return 0;
case WM_CONTEXTMENU:
handle_context_menu();
return TRUE;
default:
return 0;
}
}
else if (uMsg == WM_COMMAND) {
switch (LOWORD(wParam)) {
case ID_DEFAULT:
{
khm_int32 cmd;
cmd = get_default_notifier_action();
khui_action_trigger(cmd, NULL);
}
return TRUE;
case ID_SHOWHELP:
{
wchar_t helploc[MAX_PATH + MAX_PATH];
get_help_file(helploc, sizeof(helploc));
StringCbCat(helploc, sizeof(helploc), L"::index.html");
HtmlHelp(notifier_window, helploc, HH_DISPLAY_TOPIC, 0);
}
return TRUE;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
#pragma warning(push)
#pragma warning(disable: 4204)
static void
initialize_if_necessary(void)
{
if (message_window_class == 0) {
WNDCLASSEX c = {
sizeof(WNDCLASSEX), /* cbSize */
0, /* style */
notifier_wnd_proc, /* lpfnWndProc */
0, /* cbClsExtra */
0, /* cbWndExtra */
hInstance, /* hinstance */
NULL, /* hIcon */
NULL, /* hCursor */
NULL, /* hbrBackground */
NULL, /* lpszMenuName */
L"KCACredStateIconNotifier", /* lpszClassName */
NULL, /* hIconSm */
};
message_window_class = RegisterClassEx(&c);
}
if (notifier_window == NULL && message_window_class != 0) {
notifier_window = CreateWindow(MAKEINTATOM(message_window_class),
L"KCACredStateIconNotifierWindow",
0, 0, 0, 0, 0,
HWND_MESSAGE,
NULL,
hInstance,
NULL);
}
assert(notifier_window != NULL);
if (!notification_icon_added && notifier_window != NULL) {
NOTIFYICONDATA idata;
ZeroMemory(&idata, sizeof(idata));
idata.cbSize = sizeof(idata);
idata.hWnd = notifier_window;
idata.uID = TOKEN_ICON_ID;
idata.uFlags = NIF_ICON | NIF_MESSAGE;
idata.uCallbackMessage = TOKEN_MESSAGE_ID;
idata.hIcon = (HICON) LoadImage(hResModule, MAKEINTRESOURCE(IDI_CRED_NONE),
IMAGE_ICON, 0, 0,
LR_DEFAULTSIZE | LR_DEFAULTCOLOR | LR_SHARED);
notification_icon_added = Shell_NotifyIcon(NIM_ADD, &idata);
idata.cbSize = sizeof(idata);
idata.uVersion = NOTIFYICON_VERSION;
Shell_NotifyIcon(NIM_SETVERSION, &idata);
assert(notification_icon_added);
}
}
#pragma warning(pop)
void
kca_remove_icon(void)
{
NOTIFYICONDATA idata;
ZeroMemory(&idata, sizeof(idata));
idata.cbSize = sizeof(idata);
idata.hWnd = notifier_window;
idata.uID = TOKEN_ICON_ID;
Shell_NotifyIcon(NIM_DELETE, &idata);
notification_icon_added = FALSE;
}
static void
set_tooltip_and_icon(UINT tooltip_text, const wchar_t * postfix, UINT icon_id)
{
NOTIFYICONDATA idata;
wchar_t buf[ARRAYLENGTH(idata.szTip)];
ZeroMemory(&idata, sizeof(idata));
idata.cbSize = sizeof(idata);
idata.hWnd = notifier_window;
idata.uID = TOKEN_ICON_ID;
idata.uFlags = NIF_ICON | NIF_TIP;
idata.hIcon = (HICON) LoadImage(hResModule, MAKEINTRESOURCE(icon_id),
IMAGE_ICON, 0, 0,
LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
if (tooltip_text != 0) {
LoadString(hResModule, tooltip_text, buf, ARRAYLENGTH(buf));
}
StringCbPrintf(idata.szTip, sizeof(idata.szTip),
L"%s%s",
(tooltip_text != 0)? buf : L"",
(postfix != NULL)? postfix : L"");
Shell_NotifyIcon(NIM_MODIFY, &idata);
}
struct state_data {
khm_handle credset;
};
#define COLLECT_STR_LEN 256
static khm_int32 KHMAPI
collect_kca_cert_names(khm_handle cred, void * rock)
{
wchar_t *str = (wchar_t *) rock;
wchar_t cert_name[KCDB_MAXCCH_NAME] = L"";
FILETIME ft_now;
FILETIME ft_expire;
khm_size cb;
cb = sizeof(ft_expire);
if (KHM_FAILED(kcdb_cred_get_attr(cred, KCDB_ATTR_EXPIRE, NULL, &ft_expire, &cb)))
return KHM_ERROR_SUCCESS;
GetSystemTimeAsFileTime(&ft_now);
if (CompareFileTime(&ft_now, &ft_expire) >= 0)
return KHM_ERROR_SUCCESS;
cb = sizeof(cert_name);
if (KHM_SUCCEEDED(kcdb_cred_get_attr(cred, attr_id_auth_realm, NULL, cert_name, &cb)) &&
cert_name[0] != L'\0') {
StringCchCat(str, COLLECT_STR_LEN, cert_name);
StringCchCat(str, COLLECT_STR_LEN, L"\n");
} else if ((cb = sizeof(cert_name)) &&
KHM_SUCCEEDED(kcdb_cred_get_attr(cred, attr_id_subj_email, NULL, cert_name, &cb))
&& cert_name[0] != L'\0') {
StringCchCat(str, COLLECT_STR_LEN, cert_name);
StringCchCat(str, COLLECT_STR_LEN, L"\n");
}
return KHM_ERROR_SUCCESS;
}
static khm_int32 KHMAPI
set_state_from_ui_thread(HWND hwnd_main, void * stuff)
{
struct state_data * d = (struct state_data *) stuff;
wchar_t certs[COLLECT_STR_LEN] = L"";
initialize_if_necessary();
if (d->credset)
kcdb_credset_apply(d->credset, collect_kca_cert_names, certs);
if (certs[0] == L'\0') {
set_tooltip_and_icon(IDS_CRED_TT_NONE, NULL, IDI_CRED_NONE);
return KHM_ERROR_SUCCESS;
}
set_tooltip_and_icon(IDS_CRED_TT_GOOD, certs, IDI_CRED_GOOD);
return KHM_ERROR_SUCCESS;
}
void
kca_icon_set_state(khm_handle credset_with_tokens)
{
struct state_data d;
#if KH_VERSION_API < 7
if (pkhui_request_UI_callback == NULL)
return;
#endif
d.credset = credset_with_tokens;
if (notification_icon_added) {
set_state_from_ui_thread(NULL, &d);
} else {
khui_request_UI_callback(set_state_from_ui_thread, &d);
}
}