/[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 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 317 by twoaday, Fri May 25 14:53:02 2007 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    
45  int  #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)
46  mapi_init (void)  
47  {  
48      if (init)  /* Table with all valid MAPI errors. */
49          return 0;  struct mapi_err_s {
50        int err_no;
51      hlib = LoadLibrary ("MAPI32.DLL");      const char *msg;
52      if (!hlib)  } mapi_errors[] = {
53          return -1;      {MAPI_E_FAILURE, "One or more unspecified errors occurred. "},
54        {MAPI_E_AMBIGUOUS_RECIPIENT, "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. "},
55      mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");      {MAPI_E_ATTACHMENT_NOT_FOUND, "The specified attachment was not found. "},
56      mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");      {MAPI_E_ATTACHMENT_OPEN_FAILURE, "The specified attachment could not be opened. "},
57      mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");      {MAPI_E_BAD_RECIPTYPE, "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. "},
58      mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");      {MAPI_E_INSUFFICIENT_MEMORY, "There was insufficient memory to proceed." },
59      if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)      {MAPI_E_INVALID_RECIPS, "One or more recipients were invalid or did not resolve to any address."},
60          return -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      init = 1;      {MAPI_E_TEXT_TOO_LARGE, "The text in the message was too large."},
62        {MAPI_E_TOO_MANY_FILES, "There were too many file attachments."},
63      return 0;      {MAPI_E_TOO_MANY_RECIPIENTS, "There were too many recipients."},
64  } /* mapi_init */      {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        {SUCCESS_SUCCESS, "The call succeeded and the message was sent."},
67  void      {0, NULL}
68  mapi_deinit (void)  };
69  {  
70      if (hlib) {  
71          FreeLibrary (hlib);  /* Return a human readable MAPI error based on the given error @err. */
72          hlib = NULL;  const char*
73          init = 0;  mapi_strerror (int err)
74      }  {
75  } /* mapi_deinit */      int i;
76    
77        for (i=0; mapi_errors[i].msg; i++) {
78  int          if (err == mapi_errors[i].err_no)
79  mapi_send_ascfile (char * ascfile)              return mapi_errors[i].msg;
80  {      }
81      LHANDLE hd;      return mapi_errors[0].msg;
82      int rc;  }
83    
84      if (!init)  
85          return 0;  /* Load MAPI library and set function pointers.
86       Return value: 0 on success. */
87      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);  int
88      if (rc != SUCCESS_SUCCESS) {  mapi_init (void)
89          MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);  {
90          goto fail;      if (init)
91      }          return 0;
92      rc = mapi_send_documents (0, ";", ascfile, NULL, 0);  
93      if (rc == MAPI_E_USER_ABORT)      hlib = LoadLibrary ("MAPI32.DLL");
94          rc = SUCCESS_SUCCESS;      if (!hlib)
95      if (rc != SUCCESS_SUCCESS)          return -1;
96          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);  
97        mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");
98  fail:      mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");
99      mapi_logoff (hd, 0, 0, 0);      mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");
100      return rc;      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        init = 1;
104  int  
105  mapi_send_pubkey (const char * keyid, char * keyfile)      return 0;
106  {  }
107      LHANDLE hd;  
108      const char * fmt;  
109      char * keyinf = NULL;  /* Free library and cleanup. */
110      int rc;  void
111    mapi_deinit (void)
112      if (!init)  {
113          return 0;      if (!hlib)
114            return;
115      fmt = _("GPG Public Key of %s");      FreeLibrary (hlib);
116      keyinf = new char[strlen (fmt) + strlen (keyid) + 2];      hlib = NULL;
117      if (!keyinf)      init = 0;
118          BUG (0);  }
119      sprintf (keyinf, fmt, keyid);  
120      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);  
121      if (rc != SUCCESS_SUCCESS) {  /* Send the file given in @ascfile via the MAPI mechanism. */
122          MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);  int
123          goto fail;  mapi_send_ascfile (char *ascfile)
124      }  {
125      rc = mapi_send_documents (0, ";", keyfile, keyinf, 0);      LHANDLE hd;
126      if (rc == MAPI_E_USER_ABORT)      int rc;
127          rc = SUCCESS_SUCCESS;  
128      if (rc != SUCCESS_SUCCESS)      if (!init)
129          MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);          return 0;
130    
131  fail:      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
132      mapi_logoff (hd, 0, 0, 0);      if (rc != SUCCESS_SUCCESS) {
133      free_if_alloc (keyinf);          MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
134      return rc;          goto fail;
135  } /* mapi_send_pubkey */      }
136        rc = mapi_send_documents (0, ";", ascfile, NULL, 0);
137        if (rc == MAPI_E_USER_ABORT)
138  static void          rc = SUCCESS_SUCCESS;
139  free_mapi_msg (MapiMessage * msg)      if (rc != SUCCESS_SUCCESS)
140  {          MessageBox (NULL, _("Could not send mail."), "MAPI", MB_ICONERROR|MB_OK);
141      if (!msg)  
142          return;  fail:
143      safe_free (msg->lpszSubject);      mapi_logoff (hd, 0, 0, 0);
144      safe_free (msg->lpszNoteText);      return rc;
145      safe_free (msg);  }
146  } /* free_mapi_msg */  
147    
148    static void
149  static void  free_mapi_msg (MapiMessage *msg)
150  free_recip_tab (MapiRecipDesc * recip, size_t n)  {
151  {      if (!msg)
152      size_t i;          return;
153        safe_free (msg->lpszSubject);
154      if (!recip)      safe_free (msg->lpszNoteText);
155          return;      safe_free (msg);
156      if (!n)  }
157          return;  
158      for (i=0; i < n; i++)  
159          safe_free (recip[i].lpszName);  static void
160      safe_free (recip);  free_recip_tab (MapiRecipDesc *recip, DWORD n)
161  } /* free_recip_tab */  {
162        DWORD i;
163    
164  static void      if (!recip)
165  free_files_tab (MapiFileDesc * files, size_t n)          return;
166  {      if (!n)
167      size_t i;          return;
168        for (i=0; i < n; i++)
169      if (!files)          safe_free (recip[i].lpszName);
170          return;      safe_free (recip);
171      if (!n)  }
172          return;  
173      for (i=0; i < n; i++) {  
174          safe_free (files[i].lpszFileName);  static void
175          safe_free (files[i].lpszPathName);  free_files_tab (MapiFileDesc *files, DWORD n)
176      }  {
177      safe_free (files);      DWORD i;
178  } /* free_files_tab */  
179        if (!files)
180            return;
181        if (!n)
182  static gpgme_recipients_t          return;
183  conv_recipients (gpgme_recipients_t rset)      for (i=0; i < n; i++) {
184  {          safe_free (files[i].lpszFileName);
185      gpgme_recipients_t r;          safe_free (files[i].lpszPathName);
186      gpgme_error_t rc;      }
187      void * ctx=NULL;      safe_free (files);
188      const char * s;  }
189    
190      /* we need to convert the recipients to email addresses so  
191         GPG can handle them. */  /* Same as mapi_send_pubkey but there is an additional note. */
192      rc = gpgme_recipients_new (&r);  int
193      if (rc)  mapi_send_pubkey_ext (winpt_key_t key, const char *keyfile, int flags)
194          return NULL;  {
195      gpgme_recipients_enum_open (rset, &ctx);      LHANDLE hd;
196        MapiMessage *msg;
197      while ((s=gpgme_recipients_enum_read (rset, &ctx))) {      MapiRecipDesc *recip;
198          char * p, * q, * buf;      MapiFileDesc *attch;
199          if (!(p = strchr (s, '<')) || !(q = strchr (s, '>')))      char *p, *kinf;
200              continue;      const char *s;
201          buf = (char * )calloc (1, (q-s)-(p-s)+2);      int rc;
202          strncpy (buf, s+(p-s)+1, (q-s)-(p-s)-1);  
203          gpgme_recipients_add_name (r, buf);      if (!init)
204          safe_free (buf);          return 0;
205      }  
206      return r;      rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
207  } /* conv_recipients */      if (rc != SUCCESS_SUCCESS) {
208            MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
209            return rc;
210  static char *      }
211  secure_attachment (gpgme_recipients_t rset, const char * fname)  
212  {      msg = (MapiMessage *)calloc (1, sizeof *msg);
213      char tmpdir[512+32], * p;      if (!msg)
214      gpgme_recipients_t addrs;          BUG (0);
215      gpgme_ctx_t ctx;      p = msg->lpszSubject = strdup ("OpenPGP Public Key");
216      gpgme_error_t rc;      if (!p)
217            BUG (0);
218      if (strlen (fname) > 200)  
219          BUG (0);      s = "Attached is this OpenPGP public key:\n%s\n\n"
220      GetTempPath (sizeof tmpdir-200, tmpdir);          "Import this key via the clipboard or the Key Manager to\n"
221      p = strrchr (fname, '\\');          "exchange encrypted mails with the key holder and to be able\n"
222      if (!p)          "to verify its signatures.\n"
223          strcat (tmpdir, fname);          "\n"
224      else          "If you don't have WinPT, you can download it at http://www.winpt.eu";
225          strcat (tmpdir, fname+(p-fname)+1);      kinf = km_key_get_info (key, 0);
226      strcat (tmpdir, ".asc");      p = (char*)malloc (strlen (s) + strlen (kinf) + 2);
227        sprintf (p, s, kinf);
228      rc = gpgme_new (&ctx);      free_if_alloc (kinf);
229      if (rc)  
230          return NULL;      p = msg->lpszNoteText = p;
231      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);      if (!p)
232      gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);          BUG (0);
233      addrs = conv_recipients (rset);  
234      if (!addrs) {      /* If the key was signed, we assume it shall be sent back to the owner. */
235          msg_box (NULL, _("No valid mail addresses found."), _("Secure Attachment"), MB_ERR);      if (flags) {
236          gpgme_release (NULL);          recip = (MapiRecipDesc *)calloc (1, sizeof *recip);
237          return NULL;          if (!recip)
238      }              BUG (0);
239      rc = gpgme_op_file_encrypt (ctx, addrs, fname, tmpdir);          recip[0].ulRecipClass = MAPI_TO;
240      if (rc)          p = recip[0].lpszName = strdup (key->ext->uids->uid);
241          log_box (_("Secure Attachment"), MB_ERR, _("Could not encrypt '%s'"), fname);          if (!p)    
242      gpgme_recipients_release (addrs);              BUG (0);
243      gpgme_release (ctx);          msg->lpRecips = recip;
244      return strdup (tmpdir);          msg->nRecipCount = 1;
245  } /* secure_attachment */      }
246        else {
247            msg->lpRecips = recip = NULL;
248  static char *          msg->nRecipCount = 0;
249  secure_message (gpgme_recipients_t rset, const char * data)      }
250  {  
251      gpgme_recipients_t addrs;      msg->nFileCount = 1;
252      gpgme_error_t rc;      attch = (MapiFileDesc *)calloc (1, sizeof *attch);
253      gpgme_data_t in, out;      if (!attch)
254      gpgme_ctx_t ctx;          BUG (0);
255      char * p;      attch[0].lpszFileName = strdup (keyfile);
256      size_t n=0;      attch[0].lpszPathName = strdup (keyfile);
257        msg->lpFiles = attch;
258      rc = gpgme_new (&ctx);  
259      if (rc)      rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0);
260          return NULL;      if (rc == MAPI_E_USER_ABORT)
261      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);          rc = SUCCESS_SUCCESS;
262      gpgme_control (ctx, GPGME_CTRL_FORCETRUST, 1);      if (rc != SUCCESS_SUCCESS)
263            MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);
264      addrs = conv_recipients (rset);  
265      rc = gpgme_data_new_from_mem (&in, data, strlen (data), 1);      free_recip_tab (recip, 1);
266      if (rc) {      free_files_tab (attch, 1);
267          gpgme_release (ctx);      free_mapi_msg (msg);
268          return NULL;      mapi_logoff (hd, 0, 0, 0);
269      }  
270      gpgme_data_new (&out);      return 0;
271        }
272      rc = gpgme_op_encrypt (ctx, addrs, in, out);  
273      if (rc)  
274          log_box (_("Secure Message"), MB_ERR, "Could not encrypt the data");  
275    int
276      p = gpgme_data_release_and_return_string (out);  mapi_send_pubkey (const char *keyid, char *keyfile)
277    {
278      gpgme_data_release (in);      winpt_key_s key;
279      gpgme_release (ctx);  
280      gpgme_recipients_release (addrs);      memset (&key, 0, sizeof (key));
281        if (winpt_get_pubkey (keyid, &key))
282      return p;          return -1;
283  } /* secure_message */      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.2  
changed lines
  Added in v.317

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26