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

Diff of /trunk/Src/wptMAPI.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by twoaday, Mon Mar 7 13:21:36 2005 UTC revision 244 by twoaday, Mon Jul 3 14:10:21 2006 UTC
# Line 1  Line 1 
1  /* wptMAPI.cpp  /* wptMAPI.cpp - MAPI interface for sending keys.
2   *      Copyright (C) 2003, 2004 Timo Schulz   *      Copyright (C) 2003-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * 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   * 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   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    #ifdef HAVE_CONFIG_H
21  #include <windows.h>  #include <config.h>
22  #include <stdio.h>  #endif
23  #include <mapi.h>  
24    #include <windows.h>
25  extern HINSTANCE glob_hinst;  #include <stdio.h>
26    #include <mapi.h>
27  #include "../resource.h"  
28  #include "wptTypes.h"  #include "resource.h"
29  #include "wptErrors.h"  #include "wptTypes.h"
30  #include "wptW32API.h"  #include "wptErrors.h"
31  #include "wptGPG.h"  #include "wptW32API.h"
32    #include "wptGPG.h"
33    #include "wptVersion.h"
34  static LPMAPILOGON          mapi_logon = NULL;  #include "wptCommonCtl.h"
35  static LPMAPILOGOFF         mapi_logoff = NULL;      #include "wptKeyManager.h"
36  static LPMAPISENDDOCUMENTS  mapi_send_documents = NULL;  
37  static LPMAPISENDMAIL       mapi_send_mail = NULL;  
38  static HINSTANCE            hlib = NULL;  static LPMAPILOGON          mapi_logon = NULL;
39  static int                  init = 0;  static LPMAPILOGOFF         mapi_logoff = NULL;    
40    static LPMAPISENDDOCUMENTS  mapi_send_documents = NULL;
41  #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)  static LPMAPISENDMAIL       mapi_send_mail = NULL;
42    static HINSTANCE            hlib = NULL;
43    static int                  init = 0;
44  int  
45  mapi_init (void)  #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)
46  {  
47      if (init)  
48          return 0;  /* Table with all valid MAPI errors. */
49    struct mapi_err_s {
50      hlib = LoadLibrary ("MAPI32.DLL");      int err_no;
51      if (!hlib)      const char *msg;
52          return -1;  } mapi_errors[] = {
53        {MAPI_E_FAILURE, "One or more unspecified errors occurred. "},
54      mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");      {MAPI_E_AMBIGUOUS_RECIPIENT, "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. "},
55      mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");      {MAPI_E_ATTACHMENT_NOT_FOUND, "The specified attachment was not found. "},
56      mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");      {MAPI_E_ATTACHMENT_OPEN_FAILURE, "The specified attachment could not be opened. "},
57      mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");      {MAPI_E_BAD_RECIPTYPE, "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. "},
58      if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)      {MAPI_E_INSUFFICIENT_MEMORY, "There was insufficient memory to proceed." },
59          return -1;      {MAPI_E_INVALID_RECIPS, "One or more recipients were invalid or did not resolve to any address."},
60      init = 1;      {MAPI_E_LOGIN_FAILURE, "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed."},
61        {MAPI_E_TEXT_TOO_LARGE, "The text in the message was too large."},
62      return 0;      {MAPI_E_TOO_MANY_FILES, "There were too many file attachments."},
63  } /* mapi_init */      {MAPI_E_TOO_MANY_RECIPIENTS, "There were too many recipients."},
64        {MAPI_E_UNKNOWN_RECIPIENT, "A recipient did not appear in the address list."},
65        {MAPI_E_USER_ABORT, "The user canceled one of the dialog boxes."},
66  void      {SUCCESS_SUCCESS, "The call succeeded and the message was sent."},
67  mapi_deinit (void)      {0, NULL}
68  {  };
69      if (hlib) {  
70          FreeLibrary (hlib);  
71          hlib = NULL;  /* Return a human readable MAPI error based on the given error @err. */
72          init = 0;  const char*
73      }  mapi_strerror (int err)
74  } /* mapi_deinit */  {
75        int i;
76    
77  int      for (i=0; mapi_errors[i].msg; i++) {
78  mapi_send_ascfile (char * ascfile)          if (err == mapi_errors[i].err_no)
79  {              return mapi_errors[i].msg;
80      LHANDLE hd;      }
81      int rc;      return mapi_errors[0].msg;
82    }
83      if (!init)  
84          return 0;  
85    /* Load MAPI library and set function pointers.
86      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);     Return value: 0 on success. */
87      if (rc != SUCCESS_SUCCESS) {  int
88          MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);  mapi_init (void)
89          goto fail;  {
90      }      if (init)
91      rc = mapi_send_documents (0, ";", ascfile, NULL, 0);          return 0;
92      if (rc == MAPI_E_USER_ABORT)  
93          rc = SUCCESS_SUCCESS;      hlib = LoadLibrary ("MAPI32.DLL");
94      if (rc != SUCCESS_SUCCESS)      if (!hlib)
95          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);          return -1;
96    
97  fail:      mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");
98      mapi_logoff (hd, 0, 0, 0);      mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");
99      return rc;      mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");
100  }      mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");
101        if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)
102            return -1;
103  int      init = 1;
104  mapi_send_pubkey (const char * keyid, char * keyfile)  
105  {      return 0;
106      LHANDLE hd;  }
107      const char * fmt;  
108      char * keyinf = NULL;  
109      int rc;  /* Free library and cleanup. */
110    void
111      if (!init)  mapi_deinit (void)
112          return 0;  {
113        if (!hlib)
114      fmt = _("GPG Public Key of %s");          return;
115      keyinf = new char[strlen (fmt) + strlen (keyid) + 2];      FreeLibrary (hlib);
116      if (!keyinf)      hlib = NULL;
117          BUG (0);      init = 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);  /* Send the file given in @ascfile via the MAPI mechanism. */
122          goto fail;  int
123      }  mapi_send_ascfile (char *ascfile)
124      rc = mapi_send_documents (0, ";", keyfile, keyinf, 0);  {
125      if (rc == MAPI_E_USER_ABORT)      LHANDLE hd;
126          rc = SUCCESS_SUCCESS;      int rc;
127      if (rc != SUCCESS_SUCCESS)  
128          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);      if (!init)
129            return 0;
130  fail:  
131      mapi_logoff (hd, 0, 0, 0);      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
132      free_if_alloc (keyinf);      if (rc != SUCCESS_SUCCESS) {
133      return rc;          MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
134  } /* mapi_send_pubkey */          goto fail;
135        }
136        rc = mapi_send_documents (0, ";", ascfile, NULL, 0);
137  static void      if (rc == MAPI_E_USER_ABORT)
138  free_mapi_msg (MapiMessage * msg)          rc = SUCCESS_SUCCESS;
139  {      if (rc != SUCCESS_SUCCESS)
140      if (!msg)          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);
141          return;  
142      safe_free (msg->lpszSubject);  fail:
143      safe_free (msg->lpszNoteText);      mapi_logoff (hd, 0, 0, 0);
144      safe_free (msg);      return rc;
145  } /* free_mapi_msg */  }
146    
147    
148  static void  static void
149  free_recip_tab (MapiRecipDesc * recip, size_t n)  free_mapi_msg (MapiMessage *msg)
150  {  {
151      size_t i;      if (!msg)
152            return;
153      if (!recip)      safe_free (msg->lpszSubject);
154          return;      safe_free (msg->lpszNoteText);
155      if (!n)      safe_free (msg);
156          return;  }
157      for (i=0; i < n; i++)  
158          safe_free (recip[i].lpszName);  
159      safe_free (recip);  static void
160  } /* free_recip_tab */  free_recip_tab (MapiRecipDesc *recip, DWORD n)
161    {
162        DWORD i;
163  static void  
164  free_files_tab (MapiFileDesc * files, size_t n)      if (!recip)
165  {          return;
166      size_t i;      if (!n)
167            return;
168      if (!files)      for (i=0; i < n; i++)
169          return;          safe_free (recip[i].lpszName);
170      if (!n)      safe_free (recip);
171          return;  }
172      for (i=0; i < n; i++) {  
173          safe_free (files[i].lpszFileName);  
174          safe_free (files[i].lpszPathName);  static void
175      }  free_files_tab (MapiFileDesc *files, DWORD n)
176      safe_free (files);  {
177  } /* free_files_tab */      DWORD i;
178    
179        if (!files)
180            return;
181  static gpgme_recipients_t      if (!n)
182  conv_recipients (gpgme_recipients_t rset)          return;
183  {      for (i=0; i < n; i++) {
184      gpgme_recipients_t r;          safe_free (files[i].lpszFileName);
185      gpgme_error_t rc;          safe_free (files[i].lpszPathName);
186      void * ctx=NULL;      }
187      const char * s;      safe_free (files);
188    }
189      /* we need to convert the recipients to email addresses so  
190         GPG can handle them. */  
191      rc = gpgme_recipients_new (&r);  /* Same as mapi_send_pubkey but there is an additional note. */
192      if (rc)  int
193          return NULL;  mapi_send_pubkey_ext (winpt_key_t key, const char *keyfile, int flags)
194      gpgme_recipients_enum_open (rset, &ctx);  {
195        LHANDLE hd;
196      while ((s=gpgme_recipients_enum_read (rset, &ctx))) {      MapiMessage *msg;
197          char * p, * q, * buf;      MapiRecipDesc *recip;
198          if (!(p = strchr (s, '<')) || !(q = strchr (s, '>')))      MapiFileDesc *attch;
199              continue;      char *p, *kinf;
200          buf = (char * )calloc (1, (q-s)-(p-s)+2);      const char *s;
201          strncpy (buf, s+(p-s)+1, (q-s)-(p-s)-1);      int rc;
202          gpgme_recipients_add_name (r, buf);  
203          safe_free (buf);      if (!init)
204      }          return 0;
205      return r;  
206  } /* conv_recipients */      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
207        if (rc != SUCCESS_SUCCESS) {
208            MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
209  static char *          return rc;
210  secure_attachment (gpgme_recipients_t rset, const char * fname)      }
211  {  
212      char tmpdir[512+32], * p;      msg = (MapiMessage *)calloc (1, sizeof *msg);
213      gpgme_recipients_t addrs;      if (!msg)
214      gpgme_ctx_t ctx;          BUG (0);
215      gpgme_error_t rc;      p = msg->lpszSubject = strdup ("OpenPGP Public Key");
216        if (!p)
217      if (strlen (fname) > 200)          BUG (0);
218          BUG (0);  
219      GetTempPath (sizeof tmpdir-200, tmpdir);      s = "Attached is this OpenPGP public key:\n%s\n\n"
220      p = strrchr (fname, '\\');          "Import this key via the clipboard or the Key Manager to\n"
221      if (!p)          "exchange encrypted mails with the key holder and to be able\n"
222          strcat (tmpdir, fname);          "to verify its signatures.\n"
223      else          "\n"
224          strcat (tmpdir, fname+(p-fname)+1);          "If you don't have WinPT, you can download it at http://www.winpt.org";
225      strcat (tmpdir, ".asc");      kinf = km_key_get_info (key, 0);
226        p = (char*)malloc (strlen (s) + strlen (kinf) + 2);
227      rc = gpgme_new (&ctx);      sprintf (p, s, kinf);
228      if (rc)      free_if_alloc (kinf);
229          return NULL;  
230      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);      p = msg->lpszNoteText = p;
231      gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);      if (!p)
232      addrs = conv_recipients (rset);          BUG (0);
233      if (!addrs) {  
234          msg_box (NULL, _("No valid mail addresses found."), _("Secure Attachment"), MB_ERR);      /* If the key was signed, we assume it shall be sent back to the owner. */
235          gpgme_release (NULL);      if (flags) {
236          return NULL;          recip = (MapiRecipDesc *)calloc (1, sizeof *recip);
237      }          if (!recip)
238      rc = gpgme_op_file_encrypt (ctx, addrs, fname, tmpdir);              BUG (0);
239      if (rc)          recip[0].ulRecipClass = MAPI_TO;
240          log_box (_("Secure Attachment"), MB_ERR, _("Could not encrypt '%s'"), fname);          p = recip[0].lpszName = strdup (key->ext->uids->uid);
241      gpgme_recipients_release (addrs);          if (!p)    
242      gpgme_release (ctx);              BUG (0);
243      return strdup (tmpdir);          msg->lpRecips = recip;
244  } /* secure_attachment */          msg->nRecipCount = 1;
245        }
246        else {
247  static char *          msg->lpRecips = recip = NULL;
248  secure_message (gpgme_recipients_t rset, const char * data)          msg->nRecipCount = 0;
249  {      }
250      gpgme_recipients_t addrs;  
251      gpgme_error_t rc;      msg->nFileCount = 1;
252      gpgme_data_t in, out;      attch = (MapiFileDesc *)calloc (1, sizeof *attch);
253      gpgme_ctx_t ctx;      if (!attch)
254      char * p;          BUG (0);
255      size_t n=0;      attch[0].lpszFileName = strdup (keyfile);
256        attch[0].lpszPathName = strdup (keyfile);
257      rc = gpgme_new (&ctx);      msg->lpFiles = attch;
258      if (rc)  
259          return NULL;      rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0);
260      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);      if (rc == MAPI_E_USER_ABORT)
261      gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);          rc = SUCCESS_SUCCESS;
262        if (rc != SUCCESS_SUCCESS)
263      addrs = conv_recipients (rset);          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);
264      rc = gpgme_data_new_from_mem (&in, data, strlen (data), 1);  
265      if (rc) {      free_recip_tab (recip, 1);
266          gpgme_release (ctx);      free_files_tab (attch, 1);
267          return NULL;      free_mapi_msg (msg);
268      }      mapi_logoff (hd, 0, 0, 0);
269      gpgme_data_new (&out);  
270            return 0;
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);  int
276    mapi_send_pubkey (const char *keyid, char *keyfile)
277      gpgme_data_release (in);  {
278      gpgme_release (ctx);      winpt_key_s key;
279      gpgme_recipients_release (addrs);  
280        memset (&key, 0, sizeof (key));
281      return p;      if (winpt_get_pubkey (keyid, &key))
282  } /* secure_message */          return -1;
283        return mapi_send_pubkey_ext (&key, keyfile, 0);
284    }
 int  
 mapi_send_message (gpgme_recipients_t rset, const char * msgtxt,  
                    const char * subject, const char ** files, size_t nfiles)  
 {  
     LHANDLE hd;  
     MapiMessage * msg;  
     MapiRecipDesc * recip;  
     MapiFileDesc * attch;  
     char * p;  
     const char * s;  
     void * ctx=NULL;  
     size_t n, i=0;  
     int rc;  
   
     if (!init)  
         return 0;  
   
     rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);  
     if (rc != SUCCESS_SUCCESS) {  
         MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK);  
         return rc;  
     }  
   
     msg = (MapiMessage *)calloc (1, sizeof * msg);  
     p = msg->lpszSubject = strdup (subject);  
     if (!p)  
         BUG (0);  
     p = msg->lpszNoteText = secure_message (rset, msgtxt);  
     if (!p)  
         BUG (0);  
     n = msg->nRecipCount = gpgme_recipients_count (rset);  
     recip = (MapiRecipDesc *)calloc (n+1, sizeof * recip);  
       
     gpgme_recipients_enum_open (rset, &ctx);  
     while ((s = gpgme_recipients_enum_read (rset, &ctx))) {  
         if (!i)  
             recip[i].ulRecipClass = MAPI_TO;  
         else  
             recip[i].ulRecipClass = MAPI_CC;      
         p = recip[i].lpszName = strdup (s);  
         if (!p)  
             BUG (0);  
         i++;  
     }  
     msg->lpRecips = recip;  
   
     if (nfiles) {  
         msg->nFileCount = nfiles;  
         attch = (MapiFileDesc *)calloc (nfiles+1, sizeof * attch);  
         for (i=0; i < nfiles; i++) {  
             char * p = secure_attachment (rset, *files);  
             if (!p)  
                 continue;  
             attch[i].lpszFileName = strdup (*files);          
             attch[i].lpszPathName = strdup (p);  
             files++;  
             safe_free (p);  
         }  
         msg->lpFiles = attch;  
     }  
   
     rc = mapi_send_mail (hd, 0, msg, 0, 0);  
     if (rc == MAPI_E_USER_ABORT)  
         rc = SUCCESS_SUCCESS;  
     if (rc != SUCCESS_SUCCESS)  
         MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);  
   
     free_recip_tab (recip, n);  
     free_files_tab (attch, nfiles);  
     free_mapi_msg (msg);  
     mapi_logoff (hd, 0, 0, 0);  
   
     return 0;  
 } /* mapi_send_message */  
   
   
 static int  
 add_recipient (gpgme_recipients_t * r_rset, const char * addr)  
 {  
     gpgme_keycache_t pub = keycache_get_ctx (1);  
     gpgme_key_t key;  
     gpgme_error_t rc;  
     const char * s;  
   
     if (!*r_rset)  
         gpgme_recipients_new (&(*r_rset));  
     rc = gpgme_keycache_find_key (pub, addr, 0, &key);  
     if (rc) {  
         log_box (_("Add Recipient"), MB_ERR, _("Could not find key for '%s'"), addr);  
         return -1;  
     }  
     s = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, 0);  
     if (s)  
         gpgme_recipients_add_name (*r_rset, s);  
     return 0;  
 } /* add_recipient */  
   
   
 static int  
 add_all_recipients (HWND dlg, int itemid, gpgme_recipients_t * r_rset)  
 {      
     char buf[1024], * p;  
     int n=0;  
   
     n = GetDlgItemText (dlg, itemid, buf, sizeof buf-10);  
     if (!n)  
         return -1;  
     p = strtok (buf, ";,");  
     while (p != NULL) {  
         add_recipient (&*r_rset, p);  
         p = strtok (NULL, ";,");  
     }  
     return 0;  
 } /* add_all_recipients */  
   
   
 BOOL CALLBACK  
 winpt_mail_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
 {  
     static gpgme_recipients_t rset=NULL;  
     char subject[128];  
     char * msgbuf;  
     int n;  
   
     switch (msg) {  
     case WM_INITDIALOG:  
         center_window (dlg);  
         SetForegroundWindow (dlg);  
         break;  
   
     case WM_COMMAND:  
         switch (LOWORD (wparam)) {  
         case IDOK:  
             add_all_recipients (dlg, IDC_PMAIL_TO, &rset);  
             add_all_recipients (dlg, IDC_PMAIL_CC, &rset);  
             if (!gpgme_recipients_count (rset)) {  
                 msg_box (dlg, _("Please enter a recipient."), _("Mail"), MB_ERR);  
                 return FALSE;  
             }  
             n=GetDlgItemText (dlg, IDC_PMAIL_SUBJECT, subject, sizeof subject-1);  
             if (!n)  
                 strcpy (subject, "");  
             n = SendDlgItemMessage (dlg, IDC_PMAIL_MSG, WM_GETTEXTLENGTH, 0, 0);  
             if (!n) {  
                 msg_box (dlg, _("Please enter a message."), _("Mail"), MB_ERR);  
                 return FALSE;  
             }  
             msgbuf = (char * )calloc (1, n+2);  
             GetDlgItemText (dlg, IDC_PMAIL_MSG, msgbuf, n+1);  
             mapi_send_message (rset, msgbuf, subject, NULL, 0);  
             safe_free (msgbuf);  
             EndDialog (dlg, TRUE);  
             break;  
   
         case IDCANCEL:  
             EndDialog (dlg, FALSE);  
             break;  
         }  
         break;  
     }  
   
     return FALSE;  
 } /* winpt_mail_proc */  

Legend:
Removed from v.5  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26