/[winpt]/trunk/Src/wptMAPI.cpp
ViewVC logotype

Contents of /trunk/Src/wptMAPI.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations)
Mon Mar 7 13:21:36 2005 UTC (19 years, 11 months ago) by twoaday
File size: 11119 byte(s)
2005-03-03  Timo Schulz  <twoaday@g10code.com>
                                                                                
        * wptCardDlg.cpp (card_changepin_dlg_proc): Add item to re-type the
        new PIN. Suggested by Achim.
        Support to show the unmasked PIN.
        Modified TAB-order.
        * wptPINDlg.cpp (pin_cb_dlg_proc): Show unmasked PIN.
 
        * Fixed wrong GPG --command-fd strings. Thanks to Achim.
 
2005-03-04  Timo Schulz  <twoaday@g10code.com>
 
        * GPG asks twice for the new PIN. Thanks to Achim.
        * wptCardDlg.cpp (card_changepin_dlg_proc): Reset the 'safety' pin also.        Only check the passphrase if the backup flag is enabled. Again thanks to        Achim.
 
2005-03-06  Timo Schulz  <twoaday@freakmail.de>
 
        * wptKeySignDlg.cpp (do_fill_seckeylist): Skip secret keys without
        a public key. Noted by Kurt Fitzner.
 


1 /* wptMAPI.cpp
2 * Copyright (C) 2003, 2004 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
21 #include <windows.h>
22 #include <stdio.h>
23 #include <mapi.h>
24
25 extern HINSTANCE glob_hinst;
26
27 #include "../resource.h"
28 #include "wptTypes.h"
29 #include "wptErrors.h"
30 #include "wptW32API.h"
31 #include "wptGPG.h"
32
33
34 static LPMAPILOGON mapi_logon = NULL;
35 static LPMAPILOGOFF mapi_logoff = NULL;
36 static LPMAPISENDDOCUMENTS mapi_send_documents = NULL;
37 static LPMAPISENDMAIL mapi_send_mail = NULL;
38 static HINSTANCE hlib = NULL;
39 static int init = 0;
40
41 #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)
42
43
44 int
45 mapi_init (void)
46 {
47 if (init)
48 return 0;
49
50 hlib = LoadLibrary ("MAPI32.DLL");
51 if (!hlib)
52 return -1;
53
54 mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");
55 mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");
56 mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");
57 mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");
58 if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)
59 return -1;
60 init = 1;
61
62 return 0;
63 } /* mapi_init */
64
65
66 void
67 mapi_deinit (void)
68 {
69 if (hlib) {
70 FreeLibrary (hlib);
71 hlib = NULL;
72 init = 0;
73 }
74 } /* mapi_deinit */
75
76
77 int
78 mapi_send_ascfile (char * ascfile)
79 {
80 LHANDLE hd;
81 int rc;
82
83 if (!init)
84 return 0;
85
86 rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
87 if (rc != SUCCESS_SUCCESS) {
88 MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);
89 goto fail;
90 }
91 rc = mapi_send_documents (0, ";", ascfile, NULL, 0);
92 if (rc == MAPI_E_USER_ABORT)
93 rc = SUCCESS_SUCCESS;
94 if (rc != SUCCESS_SUCCESS)
95 MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);
96
97 fail:
98 mapi_logoff (hd, 0, 0, 0);
99 return rc;
100 }
101
102
103 int
104 mapi_send_pubkey (const char * keyid, char * keyfile)
105 {
106 LHANDLE hd;
107 const char * fmt;
108 char * keyinf = NULL;
109 int rc;
110
111 if (!init)
112 return 0;
113
114 fmt = _("GPG Public Key of %s");
115 keyinf = new char[strlen (fmt) + strlen (keyid) + 2];
116 if (!keyinf)
117 BUG (0);
118 sprintf (keyinf, fmt, keyid);
119 rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
120 if (rc != SUCCESS_SUCCESS) {
121 MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);
122 goto fail;
123 }
124 rc = mapi_send_documents (0, ";", keyfile, keyinf, 0);
125 if (rc == MAPI_E_USER_ABORT)
126 rc = SUCCESS_SUCCESS;
127 if (rc != SUCCESS_SUCCESS)
128 MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);
129
130 fail:
131 mapi_logoff (hd, 0, 0, 0);
132 free_if_alloc (keyinf);
133 return rc;
134 } /* mapi_send_pubkey */
135
136
137 static void
138 free_mapi_msg (MapiMessage * msg)
139 {
140 if (!msg)
141 return;
142 safe_free (msg->lpszSubject);
143 safe_free (msg->lpszNoteText);
144 safe_free (msg);
145 } /* free_mapi_msg */
146
147
148 static void
149 free_recip_tab (MapiRecipDesc * recip, size_t n)
150 {
151 size_t i;
152
153 if (!recip)
154 return;
155 if (!n)
156 return;
157 for (i=0; i < n; i++)
158 safe_free (recip[i].lpszName);
159 safe_free (recip);
160 } /* free_recip_tab */
161
162
163 static void
164 free_files_tab (MapiFileDesc * files, size_t n)
165 {
166 size_t i;
167
168 if (!files)
169 return;
170 if (!n)
171 return;
172 for (i=0; i < n; i++) {
173 safe_free (files[i].lpszFileName);
174 safe_free (files[i].lpszPathName);
175 }
176 safe_free (files);
177 } /* free_files_tab */
178
179
180
181 static gpgme_recipients_t
182 conv_recipients (gpgme_recipients_t rset)
183 {
184 gpgme_recipients_t r;
185 gpgme_error_t rc;
186 void * ctx=NULL;
187 const char * s;
188
189 /* we need to convert the recipients to email addresses so
190 GPG can handle them. */
191 rc = gpgme_recipients_new (&r);
192 if (rc)
193 return NULL;
194 gpgme_recipients_enum_open (rset, &ctx);
195
196 while ((s=gpgme_recipients_enum_read (rset, &ctx))) {
197 char * p, * q, * buf;
198 if (!(p = strchr (s, '<')) || !(q = strchr (s, '>')))
199 continue;
200 buf = (char * )calloc (1, (q-s)-(p-s)+2);
201 strncpy (buf, s+(p-s)+1, (q-s)-(p-s)-1);
202 gpgme_recipients_add_name (r, buf);
203 safe_free (buf);
204 }
205 return r;
206 } /* conv_recipients */
207
208
209 static char *
210 secure_attachment (gpgme_recipients_t rset, const char * fname)
211 {
212 char tmpdir[512+32], * p;
213 gpgme_recipients_t addrs;
214 gpgme_ctx_t ctx;
215 gpgme_error_t rc;
216
217 if (strlen (fname) > 200)
218 BUG (0);
219 GetTempPath (sizeof tmpdir-200, tmpdir);
220 p = strrchr (fname, '\\');
221 if (!p)
222 strcat (tmpdir, fname);
223 else
224 strcat (tmpdir, fname+(p-fname)+1);
225 strcat (tmpdir, ".asc");
226
227 rc = gpgme_new (&ctx);
228 if (rc)
229 return NULL;
230 gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);
231 gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);
232 addrs = conv_recipients (rset);
233 if (!addrs) {
234 msg_box (NULL, _("No valid mail addresses found."), _("Secure Attachment"), MB_ERR);
235 gpgme_release (NULL);
236 return NULL;
237 }
238 rc = gpgme_op_file_encrypt (ctx, addrs, fname, tmpdir);
239 if (rc)
240 log_box (_("Secure Attachment"), MB_ERR, _("Could not encrypt '%s'"), fname);
241 gpgme_recipients_release (addrs);
242 gpgme_release (ctx);
243 return strdup (tmpdir);
244 } /* secure_attachment */
245
246
247 static char *
248 secure_message (gpgme_recipients_t rset, const char * data)
249 {
250 gpgme_recipients_t addrs;
251 gpgme_error_t rc;
252 gpgme_data_t in, out;
253 gpgme_ctx_t ctx;
254 char * p;
255 size_t n=0;
256
257 rc = gpgme_new (&ctx);
258 if (rc)
259 return NULL;
260 gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);
261 gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);
262
263 addrs = conv_recipients (rset);
264 rc = gpgme_data_new_from_mem (&in, data, strlen (data), 1);
265 if (rc) {
266 gpgme_release (ctx);
267 return NULL;
268 }
269 gpgme_data_new (&out);
270
271 rc = gpgme_op_encrypt (ctx, addrs, in, out);
272 if (rc)
273 log_box (_("Secure Message"), MB_ERR, "Could not encrypt the data");
274
275 p = gpgme_data_release_and_return_string (out);
276
277 gpgme_data_release (in);
278 gpgme_release (ctx);
279 gpgme_recipients_release (addrs);
280
281 return p;
282 } /* secure_message */
283
284
285 int
286 mapi_send_message (gpgme_recipients_t rset, const char * msgtxt,
287 const char * subject, const char ** files, size_t nfiles)
288 {
289 LHANDLE hd;
290 MapiMessage * msg;
291 MapiRecipDesc * recip;
292 MapiFileDesc * attch;
293 char * p;
294 const char * s;
295 void * ctx=NULL;
296 size_t n, i=0;
297 int rc;
298
299 if (!init)
300 return 0;
301
302 rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
303 if (rc != SUCCESS_SUCCESS) {
304 MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK);
305 return rc;
306 }
307
308 msg = (MapiMessage *)calloc (1, sizeof * msg);
309 p = msg->lpszSubject = strdup (subject);
310 if (!p)
311 BUG (0);
312 p = msg->lpszNoteText = secure_message (rset, msgtxt);
313 if (!p)
314 BUG (0);
315 n = msg->nRecipCount = gpgme_recipients_count (rset);
316 recip = (MapiRecipDesc *)calloc (n+1, sizeof * recip);
317
318 gpgme_recipients_enum_open (rset, &ctx);
319 while ((s = gpgme_recipients_enum_read (rset, &ctx))) {
320 if (!i)
321 recip[i].ulRecipClass = MAPI_TO;
322 else
323 recip[i].ulRecipClass = MAPI_CC;
324 p = recip[i].lpszName = strdup (s);
325 if (!p)
326 BUG (0);
327 i++;
328 }
329 msg->lpRecips = recip;
330
331 if (nfiles) {
332 msg->nFileCount = nfiles;
333 attch = (MapiFileDesc *)calloc (nfiles+1, sizeof * attch);
334 for (i=0; i < nfiles; i++) {
335 char * p = secure_attachment (rset, *files);
336 if (!p)
337 continue;
338 attch[i].lpszFileName = strdup (*files);
339 attch[i].lpszPathName = strdup (p);
340 files++;
341 safe_free (p);
342 }
343 msg->lpFiles = attch;
344 }
345
346 rc = mapi_send_mail (hd, 0, msg, 0, 0);
347 if (rc == MAPI_E_USER_ABORT)
348 rc = SUCCESS_SUCCESS;
349 if (rc != SUCCESS_SUCCESS)
350 MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);
351
352 free_recip_tab (recip, n);
353 free_files_tab (attch, nfiles);
354 free_mapi_msg (msg);
355 mapi_logoff (hd, 0, 0, 0);
356
357 return 0;
358 } /* mapi_send_message */
359
360
361 static int
362 add_recipient (gpgme_recipients_t * r_rset, const char * addr)
363 {
364 gpgme_keycache_t pub = keycache_get_ctx (1);
365 gpgme_key_t key;
366 gpgme_error_t rc;
367 const char * s;
368
369 if (!*r_rset)
370 gpgme_recipients_new (&(*r_rset));
371 rc = gpgme_keycache_find_key (pub, addr, 0, &key);
372 if (rc) {
373 log_box (_("Add Recipient"), MB_ERR, _("Could not find key for '%s'"), addr);
374 return -1;
375 }
376 s = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, 0);
377 if (s)
378 gpgme_recipients_add_name (*r_rset, s);
379 return 0;
380 } /* add_recipient */
381
382
383 static int
384 add_all_recipients (HWND dlg, int itemid, gpgme_recipients_t * r_rset)
385 {
386 char buf[1024], * p;
387 int n=0;
388
389 n = GetDlgItemText (dlg, itemid, buf, sizeof buf-10);
390 if (!n)
391 return -1;
392 p = strtok (buf, ";,");
393 while (p != NULL) {
394 add_recipient (&*r_rset, p);
395 p = strtok (NULL, ";,");
396 }
397 return 0;
398 } /* add_all_recipients */
399
400
401 BOOL CALLBACK
402 winpt_mail_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
403 {
404 static gpgme_recipients_t rset=NULL;
405 char subject[128];
406 char * msgbuf;
407 int n;
408
409 switch (msg) {
410 case WM_INITDIALOG:
411 center_window (dlg);
412 SetForegroundWindow (dlg);
413 break;
414
415 case WM_COMMAND:
416 switch (LOWORD (wparam)) {
417 case IDOK:
418 add_all_recipients (dlg, IDC_PMAIL_TO, &rset);
419 add_all_recipients (dlg, IDC_PMAIL_CC, &rset);
420 if (!gpgme_recipients_count (rset)) {
421 msg_box (dlg, _("Please enter a recipient."), _("Mail"), MB_ERR);
422 return FALSE;
423 }
424 n=GetDlgItemText (dlg, IDC_PMAIL_SUBJECT, subject, sizeof subject-1);
425 if (!n)
426 strcpy (subject, "");
427 n = SendDlgItemMessage (dlg, IDC_PMAIL_MSG, WM_GETTEXTLENGTH, 0, 0);
428 if (!n) {
429 msg_box (dlg, _("Please enter a message."), _("Mail"), MB_ERR);
430 return FALSE;
431 }
432 msgbuf = (char * )calloc (1, n+2);
433 GetDlgItemText (dlg, IDC_PMAIL_MSG, msgbuf, n+1);
434 mapi_send_message (rset, msgbuf, subject, NULL, 0);
435 safe_free (msgbuf);
436 EndDialog (dlg, TRUE);
437 break;
438
439 case IDCANCEL:
440 EndDialog (dlg, FALSE);
441 break;
442 }
443 break;
444 }
445
446 return FALSE;
447 } /* winpt_mail_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26