2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Werner Koch (dd9jn)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "procheader.h"
43 #include "prefs_common.h"
44 #include "passphrase.h"
45 #include "select-keys.h"
46 #include "sigstatus.h"
49 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
51 static char *content_names[] = {
53 "Content-Disposition",
54 "Content-Transfer-Encoding",
58 static char *mime_version_name[] = {
64 struct passphrase_cb_info_s {
69 static char *create_boundary (void);
71 static void dump_mimeinfo (const char *text, MimeInfo *x)
73 g_message ("** MimeInfo[%s] %p level=%d",
74 text, x, x? x->level:0 );
78 g_message ("** enc=`%s' enc_type=%d mime_type=%d",
79 x->encoding, x->encoding_type, x->mime_type );
80 g_message ("** cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'",
81 x->content_type, x->charset, x->name, x->boundary );
82 g_message ("** cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d",
83 x->content_disposition, x->filename, x->fpos, x->size,
85 dump_mimeinfo (".main", x->main );
86 dump_mimeinfo (".sub", x->sub );
87 dump_mimeinfo (".next", x->next );
88 g_message ("** MimeInfo[.parent] %p", x );
89 dump_mimeinfo (".children", x->children );
90 dump_mimeinfo (".plaintext", x->plaintext );
93 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
95 unsigned int size = mimeinfo->size;
98 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
99 g_warning ("dump_part: fseek error");
103 g_message ("** --- begin dump_part ----");
104 while (size-- && (c = getc (fp)) != EOF)
107 g_warning ("dump_part: read error");
108 g_message ("** --- end dump_part ----");
112 rfc2015_disable_all (void)
114 /* FIXME: set a flag, so that we don't bother the user with failed
120 rfc2015_secure_remove (const char *fname)
124 /* fixme: overwrite the file first */
130 sig_status_to_string (GpgmeSigStat status)
135 case GPGME_SIG_STAT_NONE:
136 result = _("Oops: Signature not verified");
138 case GPGME_SIG_STAT_NOSIG:
139 result = _("No signature found");
141 case GPGME_SIG_STAT_GOOD:
142 result = _("Good signature");
144 case GPGME_SIG_STAT_BAD:
145 result = _("BAD signature");
147 case GPGME_SIG_STAT_NOKEY:
148 result = _("No public key to verify the signature");
150 case GPGME_SIG_STAT_ERROR:
151 result = _("Error verifying the signature");
153 case GPGME_SIG_STAT_DIFF:
154 result = _("Different results for signatures");
157 result = _("Error: Unknown status");
165 sig_status_with_name (GpgmeSigStat status)
170 case GPGME_SIG_STAT_NONE:
171 result = _("Oops: Signature not verified");
173 case GPGME_SIG_STAT_NOSIG:
174 result = _("No signature found");
176 case GPGME_SIG_STAT_GOOD:
177 result = _("Good signature from \"%s\"");
179 case GPGME_SIG_STAT_BAD:
180 result = _("BAD signature from \"%s\"");
182 case GPGME_SIG_STAT_NOKEY:
183 result = _("No public key to verify the signature");
185 case GPGME_SIG_STAT_ERROR:
186 result = _("Error verifying the signature");
188 case GPGME_SIG_STAT_DIFF:
189 result = _("Different results for signatures");
192 result = _("Error: Unknown status");
200 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
201 GpgmeKey key, const gchar *fpr)
206 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
208 g_string_sprintfa (str, "%s\n",
209 sig_status_to_string (status));
210 if ((fpr != NULL) && (*fpr != '\0'))
211 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
212 g_string_append (str, _("Cannot find user ID for this key."));
215 g_string_sprintfa (str, sig_status_with_name (status), uid);
216 g_string_append (str, "\n");
219 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
223 g_string_sprintfa (str, _(" aka \"%s\"\n"),
229 sig_status_full (GpgmeCtx ctx)
238 struct tm *ctime_val;
242 str = g_string_new ("");
244 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
245 while (fpr != NULL) {
247 ctime_val = localtime (&created);
248 strftime (ctime_str, sizeof (ctime_str), "%c",
250 g_string_sprintfa (str,
251 _("Signature made %s\n"),
254 err = gpgme_get_sig_key (ctx, sig_idx, &key);
256 g_string_sprintfa (str, "%s\n",
257 sig_status_to_string (status));
258 if ((fpr != NULL) && (*fpr != '\0'))
259 g_string_sprintfa (str,
260 _("Key fingerprint: %s\n"),
263 sig_status_for_key (str, ctx, status, key, fpr);
264 gpgme_key_unref (key);
266 g_string_append (str, "\n\n");
268 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
272 g_string_free (str, FALSE);
276 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
280 GpgmeData sig = NULL, text = NULL;
281 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
282 GpgmegtkSigStatus statuswindow;
283 const char *result = NULL;
285 statuswindow = gpgmegtk_sig_status_create ();
287 err = gpgme_new (&ctx);
289 g_warning ("gpgme_new failed: %s", gpgme_strerror (err));
293 /* don't include the last character (LF). It does not belong to the
295 err = gpgme_data_new_from_filepart (&text, NULL, fp,
296 mimeinfo->children->fpos,
297 mimeinfo->children->size ?
298 (mimeinfo->children->size - 1) : 0 );
300 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
301 partinfo->fpos, partinfo->size);
303 g_message ("gpgme_data_new_from_filepart failed: %s",
304 gpgme_strerror (err));
308 err = gpgme_op_verify (ctx, sig, text, &status);
310 g_message ("gpgme_op_verify failed: %s", gpgme_strerror (err));
312 /* FIXME: check what the heck this sig_status_full stuff is.
313 * it should better go into sigstatus.c */
314 g_free (partinfo->sigstatus_full);
315 partinfo->sigstatus_full = sig_status_full (ctx);
318 result = gpgmegtk_sig_status_to_string(status);
319 debug_print("verification status: %s\n", result);
320 gpgmegtk_sig_status_update(statuswindow,ctx);
322 g_assert (!err); /* FIXME: Hey: this may indeed happen */
323 g_free (partinfo->sigstatus);
324 partinfo->sigstatus = g_strdup (result);
326 gpgme_data_release (sig);
327 gpgme_data_release (text);
329 gpgmegtk_sig_status_destroy(statuswindow);
333 passphrase_cb (void *opaque, const char *desc, void *r_hd)
335 struct passphrase_cb_info_s *info = opaque;
336 GpgmeCtx ctx = info ? info->c : NULL;
340 /* FIXME: cleanup by looking at *r_hd */
344 gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
345 g_message ("%% requesting passphrase for `%s': ", desc );
346 pass = gpgmegtk_passphrase_mbox (desc);
348 g_message ("%% cancel passphrase entry");
352 g_message ("%% sending passphrase");
358 * Copy a gpgme data object to a temporary file and
359 * return this filename
362 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
371 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
372 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
374 if ((fp = fopen(tmp, "w")) == NULL) {
375 FILE_OP_ERROR(tmp, "fopen");
380 err = gpgme_data_rewind ( data );
382 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
384 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
385 fwrite ( buf, nread, 1, fp );
388 if (err != GPGME_EOF)
389 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
398 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
402 GpgmeData cipher = NULL, plain = NULL;
403 struct passphrase_cb_info_s info;
405 memset (&info, 0, sizeof info);
407 err = gpgme_new (&ctx);
409 g_message ("gpgme_new failed: %s", gpgme_strerror (err));
413 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
414 partinfo->fpos, partinfo->size);
416 g_message ("gpgme_data_new_from_filepart failed: %s",
417 gpgme_strerror (err));
421 err = gpgme_data_new (&plain);
423 g_message ("gpgme_new failed: %s", gpgme_strerror (err));
427 if (!getenv("GPG_AGENT_INFO")) {
429 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
432 err = gpgme_op_decrypt (ctx, cipher, plain);
435 gpgme_data_release (cipher);
437 g_warning ("** decryption failed: %s", gpgme_strerror (err));
438 gpgme_data_release (plain);
442 g_message ("** decryption succeeded");
448 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
455 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
458 g_message ("** multipart/signed encountered");
460 /* check that we have at least 2 parts of the correct type */
461 for (partinfo = mimeinfo->children;
462 partinfo != NULL; partinfo = partinfo->next) {
463 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
464 "application/pgp-signature"))
471 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
473 return rfc2015_find_signature (mimeinfo) != NULL;
476 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
480 partinfo = rfc2015_find_signature (mimeinfo);
485 g_message ("** yep, it is a pgp signature");
486 dump_mimeinfo ("gpg-signature", partinfo );
487 dump_part (partinfo, fp );
488 dump_mimeinfo ("signed text", mimeinfo->children );
489 dump_part (mimeinfo->children, fp);
492 check_signature (mimeinfo, partinfo, fp);
495 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
499 if (strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
501 /* fixme: we should schek the protocol parameter */
506 name_cmp(const char *a, const char *b)
508 for( ; *a && *b; a++, b++) {
510 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
518 headerp(char *p, char **names)
527 if(p2[-1] == ' ' || p2[-1] == '\t') {
536 for(i = 0 ; names[i] != NULL; i++) {
537 if(!name_cmp (names[i], p))
542 return names[i] != NULL;
546 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
559 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
561 g_message ("** multipart/encrypted encountered");
564 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
566 while (fgets(buf, sizeof(buf), fp) != NULL)
567 if (buf[0] == '\r' || buf[0] == '\n') break;
569 procmime_scan_multipart_message(mimeinfo, fp);
571 /* check that we have the 2 parts */
573 for (partinfo = mimeinfo->children; partinfo; partinfo = partinfo->next) {
574 if (++n == 1 && !strcmp (partinfo->content_type,
575 "application/pgp-encrypted")) {
576 /* Fixme: check that the version is 1 */
579 else if (n == 2 && !strcmp (partinfo->content_type,
580 "application/octet-stream")) {
582 g_warning ("** oops: pgp_encrypted with more than 2 parts");
587 if (!ver_okay || !partinfo) {
588 msginfo->decryption_failed = 1;
589 /* fixme: remove the stuff, that the above procmime_scan_multiparts()
590 * has appended to mimeino */
594 g_message ("** yep, it is pgp encrypted");
596 plain = pgp_decrypt (partinfo, fp);
598 msginfo->decryption_failed = 1;
602 fname = g_strdup_printf("%s%cplaintext.%08x",
603 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
605 if ((dstfp = fopen(fname, "w")) == NULL) {
606 FILE_OP_ERROR(fname, "fopen");
608 msginfo->decryption_failed = 1;
612 /* write the orginal header to the new file */
613 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
616 while (fgets(buf, sizeof(buf), fp)) {
617 if (headerp (buf, content_names))
619 if (buf[0] == '\r' || buf[0] == '\n')
624 err = gpgme_data_rewind (plain);
626 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
628 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
629 fwrite (buf, nread, 1, dstfp);
632 if (err != GPGME_EOF) {
633 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
638 msginfo->plaintext_file = fname;
639 msginfo->decryption_failed = 0;
644 * plain contains an entire mime object.
645 * Encrypt it and return an GpgmeData object with the encrypted version of
646 * the file or NULL in case of error.
649 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
653 GpgmeData cipher = NULL;
655 err = gpgme_new (&ctx);
657 err = gpgme_data_new (&cipher);
659 gpgme_set_armor (ctx, 1);
660 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
664 g_warning ("** encryption failed: %s", gpgme_strerror (err));
665 gpgme_data_release (cipher);
669 g_message ("** encryption succeeded");
678 * Encrypt the file by extracting all recipients and finding the
679 * encryption keys for all of them. The file content is then replaced
680 * by the encrypted one. */
682 rfc2015_encrypt (const char *file, GSList *recp_list)
686 int i, clineidx, saved_last;
687 char *clines[3] = {NULL};
689 GpgmeData header = NULL;
690 GpgmeData plain = NULL;
691 GpgmeData cipher = NULL;
692 GpgmeRecipients rset = NULL;
694 int mime_version_seen = 0;
695 char *boundary = create_boundary ();
697 /* Create the list of recipients */
698 rset = gpgmegtk_recipient_selection (recp_list);
700 g_warning ("error creating recipient list" );
704 /* Open the source file */
705 if ((fp = fopen(file, "rb")) == NULL) {
706 FILE_OP_ERROR(file, "fopen");
710 err = gpgme_data_new (&header);
712 err = gpgme_data_new (&plain);
714 g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
718 /* get the content header lines from the source */
721 while (!err && fgets(buf, sizeof(buf), fp)) {
722 /* fixme: check for overlong lines */
723 if (headerp (buf, content_names)) {
724 if (clineidx >= DIM (clines)) {
725 g_message ("rfc2015_encrypt: too many content lines");
728 clines[clineidx++] = g_strdup (buf);
734 if (*buf == ' ' || *buf == '\t') {
735 char *last = clines[clineidx-1];
736 clines[clineidx-1] = g_strconcat (last, buf, NULL);
742 if (headerp (buf, mime_version_name))
743 mime_version_seen = 1;
745 if (buf[0] == '\r' || buf[0] == '\n')
747 err = gpgme_data_write (header, buf, strlen (buf));
750 FILE_OP_ERROR (file, "fgets");
754 /* write them to the temp data and add the rest of the message */
755 for (i = 0; !err && i < clineidx; i++) {
756 g_message ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
757 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
760 err = gpgme_data_write (plain, "\r\n", 2);
761 while (!err && fgets(buf, sizeof(buf), fp)) {
762 err = gpgme_data_write (plain, buf, strlen (buf));
765 FILE_OP_ERROR (file, "fgets");
769 g_warning ("** gpgme_data_write failed: %s", gpgme_strerror (err));
773 cipher = pgp_encrypt (plain, rset);
774 gpgme_data_release (plain); plain = NULL;
775 gpgme_recipients_release (rset); rset = NULL;
779 /* we have the encrypted message available in cipher and now we
780 * are going to rewrite the source file. To be sure that file has
781 * been truncated we use an approach which should work everywhere:
782 * close the file and then reopen it for writing. It is important
783 * that this works, otherwise it may happen that parts of the
784 * plaintext are still in the file (The encrypted stuff is, due to
785 * compression, usually shorter than the plaintext).
787 * Yes, there is a race condition here, but everyone, who is so
788 * stupid to store the temp file with the plaintext in a public
789 * directory has to live with this anyway. */
791 FILE_OP_ERROR(file, "fclose");
794 if ((fp = fopen(file, "wb")) == NULL) {
795 FILE_OP_ERROR(file, "fopen");
799 /* Write the header, append new content lines, part 1 and part 2 header */
800 err = gpgme_data_rewind (header);
802 g_message ("gpgme_data_rewind failed: %s", gpgme_strerror (err));
805 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
806 fwrite (buf, nread, 1, fp);
808 if (err != GPGME_EOF) {
809 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
813 FILE_OP_ERROR (file, "fwrite");
816 gpgme_data_release (header); header = NULL;
818 if (!mime_version_seen)
819 fputs ("MIME-Version: 1\r\n", fp);
822 "Content-Type: multipart/encrypted;"
823 " protocol=\"application/pgp-encrypted\";\r\n"
824 " boundary=\"%s\"\r\n"
827 "Content-Type: application/pgp-encrypted\r\n"
832 "Content-Type: application/octet-stream\r\n"
834 boundary, boundary, boundary);
836 /* append the encrypted stuff */
837 err = gpgme_data_rewind (cipher);
839 g_warning ("** gpgme_data_rewind on cipher failed: %s",
840 gpgme_strerror (err));
844 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
845 fwrite (buf, nread, 1, fp);
847 if (err != GPGME_EOF) {
848 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
852 /* and the final boundary */
860 FILE_OP_ERROR (file, "fwrite");
864 gpgme_data_release (cipher);
870 gpgme_data_release (header);
871 gpgme_data_release (plain);
872 gpgme_data_release (cipher);
873 gpgme_recipients_release (rset);
875 return -1; /* error */
879 set_signers (GpgmeCtx ctx, PrefsAccount *ac)
881 GSList *key_list = NULL;
882 GpgmeCtx list_ctx = NULL;
883 const char *keyid = NULL;
891 switch (ac->sign_key) {
892 case SIGN_KEY_DEFAULT:
893 return 0; /* nothing to do */
895 case SIGN_KEY_BY_FROM:
899 case SIGN_KEY_CUSTOM:
900 keyid = ac->sign_key_id;
904 g_assert_not_reached ();
907 err = gpgme_new (&list_ctx);
910 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
913 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
914 key_list = g_slist_append (key_list, key);
916 if (err != GPGME_EOF)
918 if (key_list == NULL) {
919 g_warning ("no keys found for keyid \"%s\"", keyid);
921 gpgme_signers_clear (ctx);
922 for (p = key_list; p != NULL; p = p->next) {
923 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
930 g_message ("** set_signers failed: %s", gpgme_strerror (err));
931 for (p = key_list; p != NULL; p = p->next)
932 gpgme_key_unref ((GpgmeKey) p->data);
933 g_slist_free (key_list);
935 gpgme_release (list_ctx);
940 * plain contains an entire mime object. Sign it and return an
941 * GpgmeData object with the signature of it or NULL in case of error.
944 pgp_sign (GpgmeData plain, PrefsAccount *ac)
948 GpgmeData sig = NULL;
949 struct passphrase_cb_info_s info;
951 memset (&info, 0, sizeof info);
953 err = gpgme_new (&ctx);
956 err = gpgme_data_new (&sig);
960 if (!getenv("GPG_AGENT_INFO")) {
962 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
964 gpgme_set_textmode (ctx, 1);
965 gpgme_set_armor (ctx, 1);
966 err = set_signers (ctx, ac);
969 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
973 g_message ("** signing failed: %s", gpgme_strerror (err));
974 gpgme_data_release (sig);
978 g_message ("** signing succeeded");
986 * Sign the file and replace its content with the signed one.
989 rfc2015_sign (const char *file, PrefsAccount *ac)
993 int i, clineidx, saved_last;
994 char *clines[3] = {NULL};
996 GpgmeData header = NULL;
997 GpgmeData plain = NULL;
998 GpgmeData sigdata = NULL;
1000 int mime_version_seen = 0;
1001 char *boundary = create_boundary ();
1003 /* Open the source file */
1004 if ((fp = fopen(file, "rb")) == NULL) {
1005 FILE_OP_ERROR(file, "fopen");
1009 err = gpgme_data_new (&header);
1011 err = gpgme_data_new (&plain);
1013 g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
1017 /* get the content header lines from the source */
1020 while (!err && fgets(buf, sizeof(buf), fp)) {
1021 /* fixme: check for overlong lines */
1022 if (headerp (buf, content_names)) {
1023 if (clineidx >= DIM (clines)) {
1024 g_message ("rfc2015_sign: too many content lines");
1027 clines[clineidx++] = g_strdup (buf);
1033 if (*buf == ' ' || *buf == '\t') {
1034 char *last = clines[clineidx - 1];
1035 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1041 if (headerp (buf, mime_version_name))
1042 mime_version_seen = 1;
1044 if (buf[0] == '\r' || buf[0] == '\n')
1046 err = gpgme_data_write (header, buf, strlen (buf));
1049 FILE_OP_ERROR (file, "fgets");
1053 /* write them to the temp data and add the rest of the message */
1054 for (i = 0; !err && i < clineidx; i++) {
1055 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1058 err = gpgme_data_write (plain, "\r\n", 2 );
1059 while (!err && fgets(buf, sizeof(buf), fp)) {
1060 err = gpgme_data_write (plain, buf, strlen (buf));
1063 FILE_OP_ERROR (file, "fgets");
1067 g_message ("** gpgme_data_write failed: %s", gpgme_strerror (err));
1071 sigdata = pgp_sign (plain, ac);
1075 /* we have the signed message available in sigdata and now we are
1076 * going to rewrite the original file. To be sure that file has
1077 * been truncated we use an approach which should work everywhere:
1078 * close the file and then reopen it for writing. */
1080 FILE_OP_ERROR(file, "fclose");
1083 if ((fp = fopen(file, "wb")) == NULL) {
1084 FILE_OP_ERROR(file, "fopen");
1088 /* Write the rfc822 header and add new content lines */
1089 err = gpgme_data_rewind (header);
1091 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
1092 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1093 fwrite (buf, nread, 1, fp);
1095 if (err != GPGME_EOF) {
1096 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1100 FILE_OP_ERROR (file, "fwrite");
1103 gpgme_data_release (header);
1106 if (!mime_version_seen)
1107 fputs ("MIME-Version: 1\r\n", fp);
1108 fprintf (fp, "Content-Type: multipart/signed; "
1109 "protocol=\"application/pgp-signature\";\r\n"
1110 " boundary=\"%s\"\r\n", boundary );
1112 /* Part 1: signed material */
1113 fprintf (fp, "\r\n--%s\r\n", boundary);
1114 err = gpgme_data_rewind (plain);
1116 g_message ("** gpgme_data_rewind on plain failed: %s",
1117 gpgme_strerror (err));
1120 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1121 fwrite (buf, nread, 1, fp);
1123 if (err != GPGME_EOF) {
1124 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1128 /* Part 2: signature */
1129 fprintf (fp, "\r\n--%s\r\n", boundary);
1130 fputs ("Content-Type: application/pgp-signature\r\n"
1133 err = gpgme_data_rewind (sigdata);
1135 g_message ("** gpgme_data_rewind on sigdata failed: %s",
1136 gpgme_strerror (err));
1140 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1141 fwrite (buf, nread, 1, fp);
1143 if (err != GPGME_EOF) {
1144 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1148 /* Final boundary */
1149 fprintf (fp, "\r\n--%s--\r\n\r\n", boundary);
1152 FILE_OP_ERROR (file, "fwrite");
1156 gpgme_data_release (header);
1157 gpgme_data_release (plain);
1158 gpgme_data_release (sigdata);
1165 gpgme_data_release (header);
1166 gpgme_data_release (plain);
1167 gpgme_data_release (sigdata);
1169 return -1; /* error */
1174 * Create a new boundary in a way that it is very unlikely that this
1175 * will occur in the following text. It would be easy to ensure
1176 * uniqueness if everything is either quoted-printable or base64
1177 * encoded (note that conversion is allowed), but because MIME bodies
1178 * may be nested, it may happen that the same boundary has already
1179 * been used. We avoid scanning the message for conflicts and hope the
1182 * boundary := 0*69<bchars> bcharsnospace
1183 * bchars := bcharsnospace / " "
1184 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
1185 * "+" / "_" / "," / "-" / "." /
1186 * "/" / ":" / "=" / "?"
1190 create_boundary (void)
1192 static char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1193 "abcdefghijklmnopqrstuvwxyz"
1194 "1234567890'()+_,./:=?";
1201 /* We make the boundary depend on the pid, so that all running
1202 * processed generate different values even when they have been
1203 * started within the same second and srand48(time(NULL)) has been
1204 * used. I can't see whether this is really an advantage but it
1205 * doesn't do any harm.
1208 for(i = 0; i < sizeof(buf) - 1; i++) {
1209 buf[i] = tbl[(lrand48() ^ pid) % (sizeof(tbl) - 1)]; /* fill with random */
1210 if(buf[i] == '=' && equal == -1)
1215 /* now make sure that we do have the sequence "=." in it which cannot
1216 * be matched by quoted-printable or base64 encoding */
1217 if(equal != -1 && (equal+1) < i)
1224 return g_strdup(buf);
1227 #endif /* USE_GPGME */