1 |
/* wptMAPI.cpp - MAPI interface for sending keys. |
2 |
* Copyright (C) 2003-2008 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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <stdio.h> |
22 |
#include <mapi.h> |
23 |
|
24 |
#include "resource.h" |
25 |
#include "wptTypes.h" |
26 |
#include "wptErrors.h" |
27 |
#include "wptW32API.h" |
28 |
#include "wptGPG.h" |
29 |
#include "wptVersion.h" |
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptKeylist.h" |
32 |
#include "wptKeyManager.h" |
33 |
|
34 |
|
35 |
static LPMAPILOGON mapi_logon = NULL; |
36 |
static LPMAPILOGOFF mapi_logoff = NULL; |
37 |
static LPMAPISENDDOCUMENTS mapi_send_documents = NULL; |
38 |
static LPMAPISENDMAIL mapi_send_mail = NULL; |
39 |
static HINSTANCE hlib = NULL; |
40 |
static int init = 0; |
41 |
|
42 |
#define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name) |
43 |
|
44 |
|
45 |
/* Table with all valid MAPI errors. */ |
46 |
struct mapi_err_s { |
47 |
int err_no; |
48 |
const char *msg; |
49 |
} mapi_errors[] = { |
50 |
{MAPI_E_FAILURE, "One or more unspecified errors occurred. "}, |
51 |
{MAPI_E_AMBIGUOUS_RECIPIENT, "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. "}, |
52 |
{MAPI_E_ATTACHMENT_NOT_FOUND, "The specified attachment was not found. "}, |
53 |
{MAPI_E_ATTACHMENT_OPEN_FAILURE, "The specified attachment could not be opened. "}, |
54 |
{MAPI_E_BAD_RECIPTYPE, "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. "}, |
55 |
{MAPI_E_INSUFFICIENT_MEMORY, "There was insufficient memory to proceed." }, |
56 |
{MAPI_E_INVALID_RECIPS, "One or more recipients were invalid or did not resolve to any address."}, |
57 |
{MAPI_E_LOGIN_FAILURE, "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed."}, |
58 |
{MAPI_E_TEXT_TOO_LARGE, "The text in the message was too large."}, |
59 |
{MAPI_E_TOO_MANY_FILES, "There were too many file attachments."}, |
60 |
{MAPI_E_TOO_MANY_RECIPIENTS, "There were too many recipients."}, |
61 |
{MAPI_E_UNKNOWN_RECIPIENT, "A recipient did not appear in the address list."}, |
62 |
{MAPI_E_USER_ABORT, "The user canceled one of the dialog boxes."}, |
63 |
{SUCCESS_SUCCESS, "The call succeeded and the message was sent."}, |
64 |
{0, NULL} |
65 |
}; |
66 |
|
67 |
|
68 |
/* Return a human readable MAPI error based on the given error @err. */ |
69 |
const char* |
70 |
mapi_strerror (int err) |
71 |
{ |
72 |
for (int i=0; mapi_errors[i].msg; i++) { |
73 |
if (err == mapi_errors[i].err_no) |
74 |
return mapi_errors[i].msg; |
75 |
} |
76 |
return mapi_errors[0].msg; |
77 |
} |
78 |
|
79 |
|
80 |
/* Load MAPI library and set function pointers. |
81 |
Return value: 0 on success. */ |
82 |
int |
83 |
mapi_init (void) |
84 |
{ |
85 |
if (init) |
86 |
return 0; |
87 |
|
88 |
hlib = LoadLibrary ("MAPI32.DLL"); |
89 |
if (!hlib) |
90 |
return -1; |
91 |
|
92 |
mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon"); |
93 |
mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff"); |
94 |
mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, |
95 |
"MAPISendDocuments"); |
96 |
mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail"); |
97 |
if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail) |
98 |
return -1; |
99 |
init = 1; |
100 |
|
101 |
return 0; |
102 |
} |
103 |
|
104 |
|
105 |
/* Free library and cleanup. */ |
106 |
void |
107 |
mapi_deinit (void) |
108 |
{ |
109 |
if (!hlib) |
110 |
return; |
111 |
FreeLibrary (hlib); |
112 |
hlib = NULL; |
113 |
init = 0; |
114 |
} |
115 |
|
116 |
|
117 |
/* Send the file given in @ascfile via the MAPI mechanism. */ |
118 |
int |
119 |
mapi_send_ascfile (char *ascfile) |
120 |
{ |
121 |
LHANDLE hd; |
122 |
ULONG rc; |
123 |
|
124 |
if (!init) |
125 |
return 0; |
126 |
|
127 |
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
128 |
if (rc != SUCCESS_SUCCESS) { |
129 |
MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN); |
130 |
goto fail; |
131 |
} |
132 |
rc = mapi_send_documents (0, ";", ascfile, NULL, 0); |
133 |
if (rc == MAPI_E_USER_ABORT) |
134 |
rc = SUCCESS_SUCCESS; |
135 |
if (rc != SUCCESS_SUCCESS) |
136 |
MessageBox (NULL, _("Could not send mail."), ascfile, MB_ICONERROR|MB_OK); |
137 |
|
138 |
fail: |
139 |
mapi_logoff (hd, 0, 0, 0); |
140 |
return rc; |
141 |
} |
142 |
|
143 |
|
144 |
static void |
145 |
free_mapi_msg (MapiMessage *msg) |
146 |
{ |
147 |
if (!msg) |
148 |
return; |
149 |
safe_free (msg->lpszSubject); |
150 |
safe_free (msg->lpszNoteText); |
151 |
safe_free (msg); |
152 |
} |
153 |
|
154 |
|
155 |
static void |
156 |
free_recip_tab (MapiRecipDesc *recip, DWORD n) |
157 |
{ |
158 |
DWORD i; |
159 |
|
160 |
if (!recip || !n) |
161 |
return; |
162 |
for (i=0; i < n; i++) |
163 |
safe_free (recip[i].lpszName); |
164 |
safe_free (recip); |
165 |
} |
166 |
|
167 |
|
168 |
static void |
169 |
free_files_tab (MapiFileDesc *files, DWORD n) |
170 |
{ |
171 |
DWORD i; |
172 |
|
173 |
if (!files) |
174 |
return; |
175 |
if (!n) |
176 |
return; |
177 |
for (i=0; i < n; i++) { |
178 |
safe_free (files[i].lpszFileName); |
179 |
safe_free (files[i].lpszPathName); |
180 |
} |
181 |
safe_free (files); |
182 |
} |
183 |
|
184 |
|
185 |
/* Same as mapi_send_pubkey but there is an additional note. */ |
186 |
int |
187 |
mapi_send_pubkey_ext (winpt_key_t key, const char *keyfile, int flags) |
188 |
{ |
189 |
LHANDLE hd; |
190 |
MapiMessage *msg; |
191 |
MapiRecipDesc *recip; |
192 |
MapiFileDesc *attch; |
193 |
char *p, *kinf; |
194 |
const char *s; |
195 |
ULONG rc; |
196 |
|
197 |
if (!init) |
198 |
return 0; |
199 |
|
200 |
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
201 |
if (rc != SUCCESS_SUCCESS) { |
202 |
MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN); |
203 |
return rc; |
204 |
} |
205 |
|
206 |
msg = (MapiMessage *)calloc (1, sizeof *msg); |
207 |
msg->lpszSubject = strdup (_("OpenPGP Public Key")); |
208 |
|
209 |
s = _("Attached is this OpenPGP public key:\n%s\n\n" |
210 |
"Import this key via the clipboard or the Key Manager to\n" |
211 |
"exchange encrypted mails with the key holder and to be able\n" |
212 |
"to verify its signatures.\n" |
213 |
"\n" |
214 |
"If you don't have WinPT, you can download it at http://winpt.gnupt.de"); |
215 |
kinf = km_key_get_info (key, 0); |
216 |
p = (char*)malloc (strlen (s) + strlen (kinf) + 2); |
217 |
sprintf (p, s, kinf); |
218 |
free_if_alloc (kinf); |
219 |
|
220 |
msg->lpszNoteText = p; |
221 |
/* If the key was signed, we assume it shall be sent back to the owner. */ |
222 |
if (flags) { |
223 |
recip = (MapiRecipDesc *)calloc (1, sizeof *recip); |
224 |
recip[0].ulRecipClass = MAPI_TO; |
225 |
recip[0].lpszName = strdup (key->ext->uids->uid); |
226 |
msg->lpRecips = recip; |
227 |
msg->nRecipCount = 1; |
228 |
} |
229 |
else { |
230 |
msg->lpRecips = recip = NULL; |
231 |
msg->nRecipCount = 0; |
232 |
} |
233 |
|
234 |
msg->nFileCount = 1; |
235 |
attch = (MapiFileDesc *)calloc (1, sizeof *attch); |
236 |
attch[0].lpszFileName = strdup (keyfile); |
237 |
attch[0].lpszPathName = strdup (keyfile); |
238 |
msg->lpFiles = attch; |
239 |
|
240 |
rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0); |
241 |
if (rc == MAPI_E_USER_ABORT) |
242 |
rc = SUCCESS_SUCCESS; |
243 |
if (rc != SUCCESS_SUCCESS) |
244 |
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR); |
245 |
|
246 |
free_recip_tab (recip, 1); |
247 |
free_files_tab (attch, 1); |
248 |
free_mapi_msg (msg); |
249 |
mapi_logoff (hd, 0, 0, 0); |
250 |
|
251 |
return 0; |
252 |
} |
253 |
|
254 |
|
255 |
|
256 |
int |
257 |
mapi_send_pubkey (const char *keyid, char *keyfile) |
258 |
{ |
259 |
winpt_key_s key; |
260 |
|
261 |
memset (&key, 0, sizeof (key)); |
262 |
if (winpt_get_pubkey (keyid, &key)) |
263 |
return -1; |
264 |
return mapi_send_pubkey_ext (&key, keyfile, 0); |
265 |
} |