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"
48 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
50 static char *content_names[] = {
52 "Content-Disposition",
53 "Content-Transfer-Encoding",
57 static char *mime_version_name[] = {
63 struct passphrase_cb_info_s {
68 static char *create_boundary (void);
70 static void dump_mimeinfo (const char *text, MimeInfo *x)
72 g_message ("** MimeInfo[%s] %p level=%d",
73 text, x, x? x->level:0 );
77 g_message ("** enc=`%s' enc_type=%d mime_type=%d",
78 x->encoding, x->encoding_type, x->mime_type );
79 g_message ("** cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'",
80 x->content_type, x->charset, x->name, x->boundary );
81 g_message ("** cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d",
82 x->content_disposition, x->filename, x->fpos, x->size,
84 dump_mimeinfo (".main", x->main );
85 dump_mimeinfo (".sub", x->sub );
86 dump_mimeinfo (".next", x->next );
87 g_message ("** MimeInfo[.parent] %p", x );
88 dump_mimeinfo (".children", x->children );
89 dump_mimeinfo (".plaintext", x->plaintext );
92 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
94 unsigned int size = mimeinfo->size;
97 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
98 g_warning ("dump_part: fseek error");
102 g_message ("** --- begin dump_part ----");
103 while (size-- && (c = getc (fp)) != EOF)
106 g_warning ("dump_part: read error");
107 g_message ("** --- end dump_part ----");
111 rfc2015_disable_all (void)
113 /* FIXME: set a flag, so that we don't bother the user with failed
119 rfc2015_secure_remove (const char *fname)
123 /* fixme: overwrite the file first */
129 sig_status_to_string (GpgmeSigStat status)
134 case GPGME_SIG_STAT_NONE:
135 result = _("Oops: Signature not verified");
137 case GPGME_SIG_STAT_NOSIG:
138 result = _("No signature found");
140 case GPGME_SIG_STAT_GOOD:
141 result = _("Good signature");
143 case GPGME_SIG_STAT_BAD:
144 result = _("BAD signature");
146 case GPGME_SIG_STAT_NOKEY:
147 result = _("No public key to verify the signature");
149 case GPGME_SIG_STAT_ERROR:
150 result = _("Error verifying the signature");
152 case GPGME_SIG_STAT_DIFF:
153 result = _("Different results for signatures");
156 result = _("Error: Unknown status");
164 sig_status_with_name (GpgmeSigStat status)
169 case GPGME_SIG_STAT_NONE:
170 result = _("Oops: Signature not verified");
172 case GPGME_SIG_STAT_NOSIG:
173 result = _("No signature found");
175 case GPGME_SIG_STAT_GOOD:
176 result = _("Good signature from \"%s\"");
178 case GPGME_SIG_STAT_BAD:
179 result = _("BAD signature from \"%s\"");
181 case GPGME_SIG_STAT_NOKEY:
182 result = _("No public key to verify the signature");
184 case GPGME_SIG_STAT_ERROR:
185 result = _("Error verifying the signature");
187 case GPGME_SIG_STAT_DIFF:
188 result = _("Different results for signatures");
191 result = _("Error: Unknown status");
199 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
200 GpgmeKey key, const gchar *fpr)
205 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
207 g_string_sprintfa (str, "%s\n",
208 sig_status_to_string (status));
209 if ((fpr != NULL) && (*fpr != '\0'))
210 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
211 g_string_append (str, _("Cannot find user ID for this key."));
214 g_string_sprintfa (str, sig_status_with_name (status), uid);
215 g_string_append (str, "\n");
218 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
222 g_string_sprintfa (str, _(" aka \"%s\"\n"),
228 sig_status_full (GpgmeCtx ctx)
237 struct tm *ctime_val;
241 str = g_string_new ("");
243 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
244 while (fpr != NULL) {
246 ctime_val = localtime (&created);
247 strftime (ctime_str, sizeof (ctime_str), "%c",
249 g_string_sprintfa (str,
250 _("Signature made %s\n"),
253 err = gpgme_get_sig_key (ctx, sig_idx, &key);
255 g_string_sprintfa (str, "%s\n",
256 sig_status_to_string (status));
257 if ((fpr != NULL) && (*fpr != '\0'))
258 g_string_sprintfa (str,
259 _("Key fingerprint: %s\n"),
262 sig_status_for_key (str, ctx, status, key, fpr);
263 gpgme_key_unref (key);
265 g_string_append (str, "\n\n");
267 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
271 g_string_free (str, FALSE);
275 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
279 GpgmeData sig = NULL, text = NULL;
280 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
281 const char *result = NULL;
283 err = gpgme_new (&ctx);
285 g_warning ("gpgme_new failed: %s", gpgme_strerror (err));
289 /* don't include the last character (LF). It does not belong to the
291 err = gpgme_data_new_from_filepart (&text, NULL, fp,
292 mimeinfo->children->fpos,
293 mimeinfo->children->size ?
294 (mimeinfo->children->size - 1) : 0 );
296 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
297 partinfo->fpos, partinfo->size);
299 g_message ("gpgme_data_new_from_filepart failed: %s",
300 gpgme_strerror (err));
304 err = gpgme_op_verify (ctx, sig, text, &status);
306 g_message ("gpgme_op_verify failed: %s", gpgme_strerror (err));
308 g_free (partinfo->sigstatus_full);
309 partinfo->sigstatus_full = sig_status_full (ctx);
312 result = sig_status_to_string(status);
313 debug_print("verification status: %s\n", result);
316 g_free (partinfo->sigstatus);
317 partinfo->sigstatus = g_strdup (result);
319 gpgme_data_release (sig);
320 gpgme_data_release (text);
325 passphrase_cb (void *opaque, const char *desc, void *r_hd)
327 struct passphrase_cb_info_s *info = opaque;
328 GpgmeCtx ctx = info ? info->c : NULL;
332 /* FIXME: cleanup by looking at *r_hd */
336 gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
337 g_message ("%% requesting passphrase for `%s': ", desc );
338 pass = gpgmegtk_passphrase_mbox (desc);
340 g_message ("%% cancel passphrase entry");
344 g_message ("%% sending passphrase");
350 * Copy a gpgme data object to a temporary file and
351 * return this filename
354 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
363 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
364 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
366 if ((fp = fopen(tmp, "w")) == NULL) {
367 FILE_OP_ERROR(tmp, "fopen");
372 err = gpgme_data_rewind ( data );
374 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
376 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
377 fwrite ( buf, nread, 1, fp );
380 if (err != GPGME_EOF)
381 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
390 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
394 GpgmeData cipher = NULL, plain = NULL;
395 struct passphrase_cb_info_s info;
397 memset (&info, 0, sizeof info);
399 err = gpgme_new (&ctx);
401 g_message ("gpgme_new failed: %s", gpgme_strerror (err));
405 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
406 partinfo->fpos, partinfo->size);
408 g_message ("gpgme_data_new_from_filepart failed: %s",
409 gpgme_strerror (err));
413 err = gpgme_data_new (&plain);
415 g_message ("gpgme_new failed: %s", gpgme_strerror (err));
419 if (!getenv("GPG_AGENT_INFO")) {
421 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
424 err = gpgme_op_decrypt (ctx, cipher, plain);
427 gpgme_data_release (cipher);
429 g_warning ("** decryption failed: %s", gpgme_strerror (err));
430 gpgme_data_release (plain);
434 g_message ("** decryption succeeded");
440 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
447 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
450 g_message ("** multipart/signed encountered");
452 /* check that we have at least 2 parts of the correct type */
453 for (partinfo = mimeinfo->children;
454 partinfo != NULL; partinfo = partinfo->next) {
455 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
456 "application/pgp-signature"))
463 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
465 return rfc2015_find_signature (mimeinfo) != NULL;
468 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
472 partinfo = rfc2015_find_signature (mimeinfo);
477 g_message ("** yep, it is a pgp signature");
478 dump_mimeinfo ("gpg-signature", partinfo );
479 dump_part (partinfo, fp );
480 dump_mimeinfo ("signed text", mimeinfo->children );
481 dump_part (mimeinfo->children, fp);
484 check_signature (mimeinfo, partinfo, fp);
487 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
491 if (strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
493 /* fixme: we should schek the protocol parameter */
498 name_cmp(const char *a, const char *b)
500 for( ; *a && *b; a++, b++) {
502 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
510 headerp(char *p, char **names)
519 if(p2[-1] == ' ' || p2[-1] == '\t') {
528 for(i = 0 ; names[i] != NULL; i++) {
529 if(!name_cmp (names[i], p))
534 return names[i] != NULL;
538 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
551 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
553 g_message ("** multipart/encrypted encountered");
556 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
558 while (fgets(buf, sizeof(buf), fp) != NULL)
559 if (buf[0] == '\r' || buf[0] == '\n') break;
561 procmime_scan_multipart_message(mimeinfo, fp);
563 /* check that we have the 2 parts */
565 for (partinfo = mimeinfo->children; partinfo; partinfo = partinfo->next) {
566 if (++n == 1 && !strcmp (partinfo->content_type,
567 "application/pgp-encrypted")) {
568 /* Fixme: check that the version is 1 */
571 else if (n == 2 && !strcmp (partinfo->content_type,
572 "application/octet-stream")) {
574 g_warning ("** oops: pgp_encrypted with more than 2 parts");
579 if (!ver_okay || !partinfo) {
580 msginfo->decryption_failed = 1;
581 /* fixme: remove the stuff, that the above procmime_scan_multiparts()
582 * has appended to mimeino */
586 g_message ("** yep, it is pgp encrypted");
588 plain = pgp_decrypt (partinfo, fp);
590 msginfo->decryption_failed = 1;
594 fname = g_strdup_printf("%s%cplaintext.%08x",
595 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
597 if ((dstfp = fopen(fname, "w")) == NULL) {
598 FILE_OP_ERROR(fname, "fopen");
600 msginfo->decryption_failed = 1;
604 /* write the orginal header to the new file */
605 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
608 while (fgets(buf, sizeof(buf), fp)) {
609 if (headerp (buf, content_names))
611 if (buf[0] == '\r' || buf[0] == '\n')
616 err = gpgme_data_rewind (plain);
618 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
620 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
621 fwrite (buf, nread, 1, dstfp);
624 if (err != GPGME_EOF) {
625 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
630 msginfo->plaintext_file = fname;
631 msginfo->decryption_failed = 0;
636 * plain contains an entire mime object.
637 * Encrypt it and return an GpgmeData object with the encrypted version of
638 * the file or NULL in case of error.
641 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
645 GpgmeData cipher = NULL;
647 err = gpgme_new (&ctx);
649 err = gpgme_data_new (&cipher);
651 gpgme_set_armor (ctx, 1);
652 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
656 g_warning ("** encryption failed: %s", gpgme_strerror (err));
657 gpgme_data_release (cipher);
661 g_message ("** encryption succeeded");
670 * Encrypt the file by extracting all recipients and finding the
671 * encryption keys for all of them. The file content is then replaced
672 * by the encrypted one. */
674 rfc2015_encrypt (const char *file, GSList *recp_list)
678 int i, clineidx, saved_last;
679 char *clines[3] = {NULL};
681 GpgmeData header = NULL;
682 GpgmeData plain = NULL;
683 GpgmeData cipher = NULL;
684 GpgmeRecipients rset = NULL;
686 int mime_version_seen = 0;
687 char *boundary = create_boundary ();
689 /* Create the list of recipients */
690 rset = gpgmegtk_recipient_selection (recp_list);
692 g_warning ("error creating recipient list" );
696 /* Open the source file */
697 if ((fp = fopen(file, "rb")) == NULL) {
698 FILE_OP_ERROR(file, "fopen");
702 err = gpgme_data_new (&header);
704 err = gpgme_data_new (&plain);
706 g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
710 /* get the content header lines from the source */
713 while (!err && fgets(buf, sizeof(buf), fp)) {
714 /* fixme: check for overlong lines */
715 if (headerp (buf, content_names)) {
716 if (clineidx >= DIM (clines)) {
717 g_message ("rfc2015_encrypt: too many content lines");
720 clines[clineidx++] = g_strdup (buf);
726 if (*buf == ' ' || *buf == '\t') {
727 char *last = clines[clineidx-1];
728 clines[clineidx-1] = g_strconcat (last, buf, NULL);
734 if (headerp (buf, mime_version_name))
735 mime_version_seen = 1;
737 if (buf[0] == '\r' || buf[0] == '\n')
739 err = gpgme_data_write (header, buf, strlen (buf));
742 FILE_OP_ERROR (file, "fgets");
746 /* write them to the temp data and add the rest of the message */
747 for (i = 0; !err && i < clineidx; i++) {
748 g_message ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
749 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
752 err = gpgme_data_write (plain, "\r\n", 2);
753 while (!err && fgets(buf, sizeof(buf), fp)) {
754 err = gpgme_data_write (plain, buf, strlen (buf));
757 FILE_OP_ERROR (file, "fgets");
761 g_warning ("** gpgme_data_write failed: %s", gpgme_strerror (err));
765 cipher = pgp_encrypt (plain, rset);
766 gpgme_data_release (plain); plain = NULL;
767 gpgme_recipients_release (rset); rset = NULL;
771 /* we have the encrypted message available in cipher and now we
772 * are going to rewrite the source file. To be sure that file has
773 * been truncated we use an approach which should work everywhere:
774 * close the file and then reopen it for writing. It is important
775 * that this works, otherwise it may happen that parts of the
776 * plaintext are still in the file (The encrypted stuff is, due to
777 * compression, usually shorter than the plaintext).
779 * Yes, there is a race condition here, but everyone, who is so
780 * stupid to store the temp file with the plaintext in a public
781 * directory has to live with this anyway. */
783 FILE_OP_ERROR(file, "fclose");
786 if ((fp = fopen(file, "wb")) == NULL) {
787 FILE_OP_ERROR(file, "fopen");
791 /* Write the header, append new content lines, part 1 and part 2 header */
792 err = gpgme_data_rewind (header);
794 g_message ("gpgme_data_rewind failed: %s", gpgme_strerror (err));
797 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
798 fwrite (buf, nread, 1, fp);
800 if (err != GPGME_EOF) {
801 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
805 FILE_OP_ERROR (file, "fwrite");
808 gpgme_data_release (header); header = NULL;
810 if (!mime_version_seen)
811 fputs ("MIME-Version: 1\r\n", fp);
814 "Content-Type: multipart/encrypted;"
815 " protocol=\"application/pgp-encrypted\";\r\n"
816 " boundary=\"%s\"\r\n"
819 "Content-Type: application/pgp-encrypted\r\n"
824 "Content-Type: application/octet-stream\r\n"
826 boundary, boundary, boundary);
828 /* append the encrypted stuff */
829 err = gpgme_data_rewind (cipher);
831 g_warning ("** gpgme_data_rewind on cipher failed: %s",
832 gpgme_strerror (err));
836 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
837 fwrite (buf, nread, 1, fp);
839 if (err != GPGME_EOF) {
840 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
844 /* and the final boundary */
852 FILE_OP_ERROR (file, "fwrite");
856 gpgme_data_release (cipher);
862 gpgme_data_release (header);
863 gpgme_data_release (plain);
864 gpgme_data_release (cipher);
865 gpgme_recipients_release (rset);
867 return -1; /* error */
871 set_signers (GpgmeCtx ctx, PrefsAccount *ac)
873 GSList *key_list = NULL;
874 GpgmeCtx list_ctx = NULL;
875 const char *keyid = NULL;
883 switch (ac->sign_key) {
884 case SIGN_KEY_DEFAULT:
885 return 0; /* nothing to do */
887 case SIGN_KEY_BY_FROM:
891 case SIGN_KEY_CUSTOM:
892 keyid = ac->sign_key_id;
896 g_assert_not_reached ();
899 err = gpgme_new (&list_ctx);
902 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
905 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
906 key_list = g_slist_append (key_list, key);
908 if (err != GPGME_EOF)
910 if (key_list == NULL) {
911 g_warning ("no keys found for keyid \"%s\"", keyid);
913 gpgme_signers_clear (ctx);
914 for (p = key_list; p != NULL; p = p->next) {
915 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
922 g_message ("** set_signers failed: %s", gpgme_strerror (err));
923 for (p = key_list; p != NULL; p = p->next)
924 gpgme_key_unref ((GpgmeKey) p->data);
925 g_slist_free (key_list);
927 gpgme_release (list_ctx);
932 * plain contains an entire mime object. Sign it and return an
933 * GpgmeData object with the signature of it or NULL in case of error.
936 pgp_sign (GpgmeData plain, PrefsAccount *ac)
940 GpgmeData sig = NULL;
941 struct passphrase_cb_info_s info;
943 memset (&info, 0, sizeof info);
945 err = gpgme_new (&ctx);
948 err = gpgme_data_new (&sig);
952 if (!getenv("GPG_AGENT_INFO")) {
954 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
956 gpgme_set_textmode (ctx, 1);
957 gpgme_set_armor (ctx, 1);
958 err = set_signers (ctx, ac);
961 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
965 g_message ("** signing failed: %s", gpgme_strerror (err));
966 gpgme_data_release (sig);
970 g_message ("** signing succeeded");
978 * Sign the file and replace its content with the signed one.
981 rfc2015_sign (const char *file, PrefsAccount *ac)
985 int i, clineidx, saved_last;
986 char *clines[3] = {NULL};
988 GpgmeData header = NULL;
989 GpgmeData plain = NULL;
990 GpgmeData sigdata = NULL;
992 int mime_version_seen = 0;
993 char *boundary = create_boundary ();
995 /* Open the source file */
996 if ((fp = fopen(file, "rb")) == NULL) {
997 FILE_OP_ERROR(file, "fopen");
1001 err = gpgme_data_new (&header);
1003 err = gpgme_data_new (&plain);
1005 g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
1009 /* get the content header lines from the source */
1012 while (!err && fgets(buf, sizeof(buf), fp)) {
1013 /* fixme: check for overlong lines */
1014 if (headerp (buf, content_names)) {
1015 if (clineidx >= DIM (clines)) {
1016 g_message ("rfc2015_sign: too many content lines");
1019 clines[clineidx++] = g_strdup (buf);
1025 if (*buf == ' ' || *buf == '\t') {
1026 char *last = clines[clineidx - 1];
1027 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1033 if (headerp (buf, mime_version_name))
1034 mime_version_seen = 1;
1036 if (buf[0] == '\r' || buf[0] == '\n')
1038 err = gpgme_data_write (header, buf, strlen (buf));
1041 FILE_OP_ERROR (file, "fgets");
1045 /* write them to the temp data and add the rest of the message */
1046 for (i = 0; !err && i < clineidx; i++) {
1047 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1050 err = gpgme_data_write (plain, "\r\n", 2 );
1051 while (!err && fgets(buf, sizeof(buf), fp)) {
1052 err = gpgme_data_write (plain, buf, strlen (buf));
1055 FILE_OP_ERROR (file, "fgets");
1059 g_message ("** gpgme_data_write failed: %s", gpgme_strerror (err));
1063 sigdata = pgp_sign (plain, ac);
1067 /* we have the signed message available in sigdata and now we are
1068 * going to rewrite the original file. To be sure that file has
1069 * been truncated we use an approach which should work everywhere:
1070 * close the file and then reopen it for writing. */
1072 FILE_OP_ERROR(file, "fclose");
1075 if ((fp = fopen(file, "wb")) == NULL) {
1076 FILE_OP_ERROR(file, "fopen");
1080 /* Write the rfc822 header and add new content lines */
1081 err = gpgme_data_rewind (header);
1083 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
1084 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1085 fwrite (buf, nread, 1, fp);
1087 if (err != GPGME_EOF) {
1088 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1092 FILE_OP_ERROR (file, "fwrite");
1095 gpgme_data_release (header);
1098 if (!mime_version_seen)
1099 fputs ("MIME-Version: 1\r\n", fp);
1100 fprintf (fp, "Content-Type: multipart/signed; "
1101 "protocol=\"application/pgp-signature\";\r\n"
1102 " boundary=\"%s\"\r\n", boundary );
1104 /* Part 1: signed material */
1105 fprintf (fp, "\r\n--%s\r\n", boundary);
1106 err = gpgme_data_rewind (plain);
1108 g_message ("** gpgme_data_rewind on plain failed: %s",
1109 gpgme_strerror (err));
1112 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1113 fwrite (buf, nread, 1, fp);
1115 if (err != GPGME_EOF) {
1116 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1120 /* Part 2: signature */
1121 fprintf (fp, "\r\n--%s\r\n", boundary);
1122 fputs ("Content-Type: application/pgp-signature\r\n"
1125 err = gpgme_data_rewind (sigdata);
1127 g_message ("** gpgme_data_rewind on sigdata failed: %s",
1128 gpgme_strerror (err));
1132 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1133 fwrite (buf, nread, 1, fp);
1135 if (err != GPGME_EOF) {
1136 g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
1140 /* Final boundary */
1141 fprintf (fp, "\r\n--%s--\r\n\r\n", boundary);
1144 FILE_OP_ERROR (file, "fwrite");
1148 gpgme_data_release (header);
1149 gpgme_data_release (plain);
1150 gpgme_data_release (sigdata);
1157 gpgme_data_release (header);
1158 gpgme_data_release (plain);
1159 gpgme_data_release (sigdata);
1161 return -1; /* error */
1166 * Create a new boundary in a way that it is very unlikely that this
1167 * will occur in the following text. It would be easy to ensure
1168 * uniqueness if everything is either quoted-printable or base64
1169 * encoded (note that conversion is allowed), but because MIME bodies
1170 * may be nested, it may happen that the same boundary has already
1171 * been used. We avoid scanning the message for conflicts and hope the
1174 * boundary := 0*69<bchars> bcharsnospace
1175 * bchars := bcharsnospace / " "
1176 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
1177 * "+" / "_" / "," / "-" / "." /
1178 * "/" / ":" / "=" / "?"
1182 create_boundary (void)
1184 static char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1185 "abcdefghijklmnopqrstuvwxyz"
1186 "1234567890'()+_,./:=?";
1193 /* We make the boundary depend on the pid, so that all running
1194 * processed generate different values even when they have been
1195 * started within the same second and srand48(time(NULL)) has been
1196 * used. I can't see whether this is really an advantage but it
1197 * doesn't do any harm.
1200 for(i = 0; i < sizeof(buf) - 1; i++) {
1201 buf[i] = tbl[(lrand48() ^ pid) % (sizeof(tbl) - 1)]; /* fill with random */
1202 if(buf[i] == '=' && equal == -1)
1207 /* now make sure that we do have the sequence "=." in it which cannot
1208 * be matched by quoted-printable or base64 encoding */
1209 if(equal != -1 && (equal+1) < i)
1216 return g_strdup(buf);
1219 #endif /* USE_GPGME */