1 |
/* wptMAPI.cpp - MAPI interface for sending keys. |
2 |
* Copyright (C) 2003, 2004, 2005, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT 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 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <stdio.h> |
26 |
#include <mapi.h> |
27 |
|
28 |
#include "resource.h" |
29 |
#include "wptTypes.h" |
30 |
#include "wptErrors.h" |
31 |
#include "wptW32API.h" |
32 |
#include "wptGPG.h" |
33 |
#include "wptVersion.h" |
34 |
#include "wptCommonCtl.h" |
35 |
#include "wptKeyManager.h" |
36 |
|
37 |
|
38 |
static LPMAPILOGON mapi_logon = NULL; |
39 |
static LPMAPILOGOFF mapi_logoff = NULL; |
40 |
static LPMAPISENDDOCUMENTS mapi_send_documents = NULL; |
41 |
static LPMAPISENDMAIL mapi_send_mail = NULL; |
42 |
static HINSTANCE hlib = NULL; |
43 |
static int init = 0; |
44 |
|
45 |
#define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name) |
46 |
|
47 |
|
48 |
/* Load MAPI library and set function pointers. |
49 |
Return value: 0 on success. */ |
50 |
int |
51 |
mapi_init (void) |
52 |
{ |
53 |
if (init) |
54 |
return 0; |
55 |
|
56 |
hlib = LoadLibrary ("MAPI32.DLL"); |
57 |
if (!hlib) |
58 |
return -1; |
59 |
|
60 |
mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon"); |
61 |
mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff"); |
62 |
mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments"); |
63 |
mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail"); |
64 |
if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail) |
65 |
return -1; |
66 |
init = 1; |
67 |
|
68 |
return 0; |
69 |
} |
70 |
|
71 |
|
72 |
/* Free library and cleanup. */ |
73 |
void |
74 |
mapi_deinit (void) |
75 |
{ |
76 |
if (hlib) { |
77 |
FreeLibrary (hlib); |
78 |
hlib = NULL; |
79 |
init = 0; |
80 |
} |
81 |
} |
82 |
|
83 |
|
84 |
/* Send the file given in @ascfile via the MAPI mechanism. */ |
85 |
int |
86 |
mapi_send_ascfile (char *ascfile) |
87 |
{ |
88 |
LHANDLE hd; |
89 |
int rc; |
90 |
|
91 |
if (!init) |
92 |
return 0; |
93 |
|
94 |
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
95 |
if (rc != SUCCESS_SUCCESS) { |
96 |
MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK); |
97 |
goto fail; |
98 |
} |
99 |
rc = mapi_send_documents (0, ";", ascfile, NULL, 0); |
100 |
if (rc == MAPI_E_USER_ABORT) |
101 |
rc = SUCCESS_SUCCESS; |
102 |
if (rc != SUCCESS_SUCCESS) |
103 |
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK); |
104 |
|
105 |
fail: |
106 |
mapi_logoff (hd, 0, 0, 0); |
107 |
return rc; |
108 |
} |
109 |
|
110 |
|
111 |
static void |
112 |
free_mapi_msg (MapiMessage *msg) |
113 |
{ |
114 |
if (!msg) |
115 |
return; |
116 |
safe_free (msg->lpszSubject); |
117 |
safe_free (msg->lpszNoteText); |
118 |
safe_free (msg); |
119 |
} |
120 |
|
121 |
|
122 |
static void |
123 |
free_recip_tab (MapiRecipDesc *recip, size_t n) |
124 |
{ |
125 |
size_t i; |
126 |
|
127 |
if (!recip) |
128 |
return; |
129 |
if (!n) |
130 |
return; |
131 |
for (i=0; i < n; i++) |
132 |
safe_free (recip[i].lpszName); |
133 |
safe_free (recip); |
134 |
} |
135 |
|
136 |
|
137 |
static void |
138 |
free_files_tab (MapiFileDesc *files, size_t n) |
139 |
{ |
140 |
size_t i; |
141 |
|
142 |
if (!files) |
143 |
return; |
144 |
if (!n) |
145 |
return; |
146 |
for (i=0; i < n; i++) { |
147 |
safe_free (files[i].lpszFileName); |
148 |
safe_free (files[i].lpszPathName); |
149 |
} |
150 |
safe_free (files); |
151 |
} |
152 |
|
153 |
|
154 |
/* Same as mapi_send_pubkey but there is an additional note. */ |
155 |
int |
156 |
mapi_send_pubkey_ext (winpt_key_t key, const char *keyfile, int flags) |
157 |
{ |
158 |
LHANDLE hd; |
159 |
MapiMessage *msg; |
160 |
MapiRecipDesc *recip; |
161 |
MapiFileDesc *attch; |
162 |
gpgme_key_t to = key->ctx; |
163 |
char *p, *kinf; |
164 |
const char *s; |
165 |
int rc; |
166 |
|
167 |
if (!init) |
168 |
return 0; |
169 |
|
170 |
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
171 |
if (rc != SUCCESS_SUCCESS) { |
172 |
MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK); |
173 |
return rc; |
174 |
} |
175 |
|
176 |
msg = (MapiMessage *)calloc (1, sizeof *msg); |
177 |
if (!msg) |
178 |
BUG (0); |
179 |
p = msg->lpszSubject = strdup ("OpenPGP Public Key"); |
180 |
if (!p) |
181 |
BUG (0); |
182 |
|
183 |
s = "Attached is this OpenPGP public key:\n%s\n\n" |
184 |
"Import this key via the clipboard or the Key Manager to\n" |
185 |
"exchange encrypted mails with the key holder and to be able\n" |
186 |
"to verify its signatures.\n" |
187 |
"\n" |
188 |
"If you don't have WinPT, you can download it at http://www.winpt.org"; |
189 |
kinf = km_key_get_info (key, 0); |
190 |
p = (char*)malloc (strlen (s) + strlen (kinf) + 2); |
191 |
sprintf (p, s, kinf); |
192 |
free_if_alloc (kinf); |
193 |
|
194 |
p = msg->lpszNoteText = p; |
195 |
if (!p) |
196 |
BUG (0); |
197 |
|
198 |
/* If the key was signed, we assume it shall be sent back to the owner. */ |
199 |
if (flags) { |
200 |
recip = (MapiRecipDesc *)calloc (1, sizeof *recip); |
201 |
if (!recip) |
202 |
BUG (0); |
203 |
recip[0].ulRecipClass = MAPI_TO; |
204 |
p = recip[0].lpszName = strdup (to->uids->uid); |
205 |
if (!p) |
206 |
BUG (0); |
207 |
msg->lpRecips = recip; |
208 |
msg->nRecipCount = 1; |
209 |
} |
210 |
else { |
211 |
msg->lpRecips = recip = NULL; |
212 |
msg->nRecipCount = 0; |
213 |
} |
214 |
|
215 |
msg->nFileCount = 1; |
216 |
attch = (MapiFileDesc *)calloc (1, sizeof *attch); |
217 |
if (!attch) |
218 |
BUG (0); |
219 |
attch[0].lpszFileName = strdup (keyfile); |
220 |
attch[0].lpszPathName = strdup (keyfile); |
221 |
msg->lpFiles = attch; |
222 |
|
223 |
rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0); |
224 |
if (rc == MAPI_E_USER_ABORT) |
225 |
rc = SUCCESS_SUCCESS; |
226 |
if (rc != SUCCESS_SUCCESS) |
227 |
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR); |
228 |
|
229 |
free_recip_tab (recip, 1); |
230 |
free_files_tab (attch, 1); |
231 |
free_mapi_msg (msg); |
232 |
mapi_logoff (hd, 0, 0, 0); |
233 |
|
234 |
return 0; |
235 |
} |
236 |
|
237 |
|
238 |
|
239 |
int |
240 |
mapi_send_pubkey (const char *keyid, char *keyfile) |
241 |
{ |
242 |
winpt_key_s key; |
243 |
|
244 |
memset (&key, 0, sizeof (key)); |
245 |
if (winpt_get_pubkey (keyid, &key)) |
246 |
return -1; |
247 |
return mapi_send_pubkey_ext (&key, keyfile, 0); |
248 |
} |