1 |
twoaday |
1 |
/* GPGOE.c - GnuPG for Outlook Express |
2 |
twoaday |
23 |
* Copyright (C) 2001, 2002, 2003, 2006, 2007 Timo Schulz |
3 |
twoaday |
1 |
* |
4 |
|
|
* This file is part of GPGOE. |
5 |
|
|
* |
6 |
|
|
* GPGOE is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* GPGOE is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <stdio.h> |
23 |
|
|
#include "gpgme.h" |
24 |
|
|
#include "GPGOE.h" |
25 |
|
|
|
26 |
|
|
/* Global DLL module handle. */ |
27 |
|
|
HINSTANCE mod_hinst_dll; |
28 |
|
|
|
29 |
twoaday |
16 |
/* Outlook Express old window procedure. */ |
30 |
|
|
WNDPROC oe_proc_old; |
31 |
|
|
|
32 |
twoaday |
1 |
/* DLL hook handle for the CBT function. */ |
33 |
|
|
static HHOOK ctb_hook = NULL; |
34 |
|
|
|
35 |
|
|
/* Outlook Express window handle. */ |
36 |
|
|
static HWND oe_hwnd = NULL; |
37 |
|
|
|
38 |
twoaday |
18 |
/* We need a shared section to define variables which |
39 |
|
|
will keep their values in all address spaces. */ |
40 |
twoaday |
19 |
#ifndef __GNUC__ |
41 |
|
|
#pragma data_seg(".SHARDAT") |
42 |
twoaday |
18 |
#endif |
43 |
twoaday |
19 |
/* Supported GPGOE feature modes. */ |
44 |
|
|
int gpgoe_active_modes ATTR_SEC = 0; |
45 |
|
|
|
46 |
twoaday |
24 |
/* FIXME: we currently limit the passphrase to N octets. */ |
47 |
|
|
char gpgoe_pass_cache[HASH_BUCKETS][MAX_PASS_LEN]; |
48 |
twoaday |
18 |
#ifndef __GNUC__ |
49 |
|
|
#pragma data_seg() |
50 |
|
|
#endif |
51 |
twoaday |
1 |
|
52 |
twoaday |
18 |
|
53 |
twoaday |
1 |
/* Main DLL entry point. */ |
54 |
|
|
BOOL WINAPI |
55 |
|
|
DllMain (HINSTANCE hinst_dll, DWORD reason, LPVOID reserved) |
56 |
|
|
{ |
57 |
|
|
switch (reason) { |
58 |
|
|
case DLL_PROCESS_ATTACH: |
59 |
|
|
mod_hinst_dll = hinst_dll; |
60 |
|
|
break; |
61 |
|
|
|
62 |
|
|
case DLL_PROCESS_DETACH: |
63 |
|
|
mod_hinst_dll = NULL; |
64 |
|
|
break; |
65 |
|
|
|
66 |
|
|
case DLL_THREAD_ATTACH: |
67 |
|
|
break; |
68 |
|
|
|
69 |
|
|
case DLL_THREAD_DETACH: |
70 |
|
|
break; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
return TRUE; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
twoaday |
23 |
#if 0 |
78 |
twoaday |
12 |
/* Quick and dirty check if the dialog is a common dialog |
79 |
twoaday |
16 |
and the 'File {Open, Save}' style. */ |
80 |
twoaday |
12 |
static int |
81 |
twoaday |
16 |
is_common_file_dlg (HWND h) |
82 |
twoaday |
12 |
{ |
83 |
|
|
HWND button = GetDlgItem (h, 1040); |
84 |
|
|
char wclass[200]; |
85 |
|
|
|
86 |
|
|
if (button == NULL) |
87 |
|
|
return 0; |
88 |
|
|
if (GetClassName (button, wclass, 200) > 0 |
89 |
|
|
&& !stricmp (wclass, "Button")) |
90 |
|
|
return -1; |
91 |
|
|
return 0; |
92 |
|
|
} |
93 |
twoaday |
23 |
#endif |
94 |
twoaday |
12 |
|
95 |
|
|
|
96 |
twoaday |
1 |
/* CTB hook procedure. |
97 |
|
|
Monitors the creation of all windows and subclass the window |
98 |
twoaday |
16 |
if it belongs to the Outlook Express (message) class. */ |
99 |
twoaday |
1 |
static LRESULT CALLBACK |
100 |
|
|
ctb_proc (int code, WPARAM wparam, LPARAM lparam) |
101 |
|
|
{ |
102 |
|
|
HWND hwnd; |
103 |
|
|
char wclass[256]; |
104 |
|
|
|
105 |
|
|
if (code < 0) |
106 |
|
|
return CallNextHookEx (ctb_hook, code, wparam, lparam); |
107 |
|
|
|
108 |
|
|
hwnd = (HWND)wparam; |
109 |
|
|
switch (code) { |
110 |
|
|
case HCBT_CREATEWND: |
111 |
twoaday |
23 |
GetClassName (hwnd, wclass, DIM (wclass) - 1); |
112 |
twoaday |
1 |
if (!strcmp (wclass, "ATH_Note")) { |
113 |
|
|
oe_proc_old = (WNDPROC) GetWindowLong (hwnd, GWL_WNDPROC); |
114 |
|
|
if (!oe_proc_old) |
115 |
|
|
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
116 |
|
|
"Could not get window procedure ec=%d", |
117 |
|
|
(int)GetLastError ()); |
118 |
|
|
else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc)) |
119 |
|
|
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
120 |
|
|
"Could not set window procedure: ec=%d", |
121 |
|
|
(int)GetLastError ()); |
122 |
|
|
else |
123 |
|
|
oe_hwnd = hwnd; |
124 |
|
|
} |
125 |
twoaday |
16 |
|
126 |
twoaday |
18 |
if ((gpgoe_active_modes & GPGOE_MODE_PLAINREPLY) && |
127 |
|
|
!strcmp (wclass, "Outlook Express Browser Class")) { |
128 |
twoaday |
16 |
oe_main_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC); |
129 |
|
|
if (!oe_main_proc_old) |
130 |
|
|
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
131 |
|
|
"Could not get window procedure ec=%d", |
132 |
|
|
(int)GetLastError ()); |
133 |
|
|
else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_main_proc)) |
134 |
|
|
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
135 |
|
|
"Could not set window procedure: ec=%d", |
136 |
|
|
(int)GetLastError ()); |
137 |
|
|
} |
138 |
twoaday |
1 |
break; |
139 |
twoaday |
12 |
|
140 |
twoaday |
19 |
case HCBT_ACTIVATE: |
141 |
twoaday |
23 |
#if 0 /* Disable the attachment related code for now. */ |
142 |
twoaday |
19 |
if (plugin_active != NULL && |
143 |
twoaday |
12 |
WaitForSingleObject (plugin_active, 0) == WAIT_OBJECT_0) { |
144 |
|
|
|
145 |
|
|
if (GetClassName (hwnd, wclass, sizeof (wclass)-1) <= 0 || |
146 |
twoaday |
16 |
!strstr (wclass, "#32770") || !is_common_file_dlg (hwnd)) |
147 |
twoaday |
12 |
break; |
148 |
|
|
of_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC); |
149 |
|
|
SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)of_proc); |
150 |
|
|
} |
151 |
twoaday |
19 |
#endif |
152 |
twoaday |
12 |
break; |
153 |
twoaday |
1 |
} |
154 |
|
|
return CallNextHookEx (ctb_hook, code, wparam, lparam); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
twoaday |
16 |
/* Initialize the hooks; a named mutex prevents that the |
159 |
|
|
initialisation is done twice. |
160 |
|
|
Return value: 0 on success. */ |
161 |
twoaday |
1 |
int |
162 |
|
|
gpgoe_initialize (void) |
163 |
|
|
{ |
164 |
twoaday |
19 |
reset_pass_cache (); |
165 |
twoaday |
1 |
CreateMutex (NULL, TRUE, "gpgoe"); |
166 |
|
|
if (GetLastError () == ERROR_ALREADY_EXISTS) |
167 |
|
|
return 0; |
168 |
|
|
ctb_hook = SetWindowsHookEx (WH_CBT, ctb_proc, mod_hinst_dll, 0); |
169 |
|
|
return ctb_hook? 0 : (int)GetLastError (); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
|
173 |
twoaday |
16 |
/* Deinitialize the hooks and close all handles. |
174 |
|
|
Return value: 0 on success. */ |
175 |
twoaday |
1 |
int |
176 |
|
|
gpgoe_remove (void) |
177 |
|
|
{ |
178 |
|
|
HANDLE hd; |
179 |
twoaday |
16 |
int rc; |
180 |
twoaday |
19 |
|
181 |
twoaday |
1 |
hd = CreateMutex (NULL, TRUE, "gpgoe"); |
182 |
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS) { |
183 |
|
|
oe_hwnd = NULL; |
184 |
|
|
CloseHandle (hd); |
185 |
|
|
} |
186 |
|
|
SetWindowLong (oe_hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc_old); |
187 |
|
|
rc = UnhookWindowsHookEx (ctb_hook); |
188 |
twoaday |
19 |
|
189 |
|
|
/* reset global variables. */ |
190 |
|
|
reset_pass_cache (); |
191 |
|
|
gpgoe_active_modes = 0; |
192 |
|
|
|
193 |
twoaday |
1 |
return rc ? 0 : (int)GetLastError (); |
194 |
|
|
} |
195 |
twoaday |
18 |
|
196 |
|
|
|
197 |
|
|
/* Change the active plug-in modes, enable mode @mode. */ |
198 |
|
|
void |
199 |
|
|
gpgoe_set_active_modes (int mode) |
200 |
|
|
{ |
201 |
|
|
gpgoe_active_modes = mode; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
|
205 |
|
|
int |
206 |
|
|
gpgoe_get_active_modes (void) |
207 |
|
|
{ |
208 |
|
|
return gpgoe_active_modes; |
209 |
|
|
} |