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.
38 #include "procheader.h"
44 #include "prefs_common.h"
45 #include "passphrase.h"
46 #include "select-keys.h"
47 #include "sigstatus.h"
50 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
52 static char *content_names[] = {
54 "Content-Disposition",
55 "Content-Transfer-Encoding",
59 static char *mime_version_name[] = {
64 static char *create_boundary (void);
67 static void dump_mimeinfo (const char *text, MimeInfo *x)
69 debug_print ("MimeInfo[%s] %p level=%d\n",
70 text, x, x? x->level:0 );
74 debug_print (" enc=`%s' enc_type=%d mime_type=%d\n",
75 x->encoding, x->encoding_type, x->mime_type );
76 debug_print (" cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
77 x->content_type, x->charset, x->name, x->boundary );
78 debug_print (" cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
79 x->content_disposition, x->filename, x->fpos, x->size,
81 dump_mimeinfo (".main", x->main );
82 dump_mimeinfo (".sub", x->sub );
83 dump_mimeinfo (".next", x->next );
84 debug_print ("MimeInfo[.parent] %p\n", x );
85 dump_mimeinfo (".children", x->children );
86 dump_mimeinfo (".plaintext", x->plaintext );
89 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
91 unsigned int size = mimeinfo->size;
94 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
95 debug_print ("dump_part: fseek error\n");
99 debug_print ("--- begin dump_part ----\n");
100 while (size-- && (c = getc (fp)) != EOF)
103 debug_print ("dump_part: read error\n");
104 debug_print ("--- end dump_part ----\n");
109 rfc2015_disable_all (void)
111 /* FIXME: set a flag, so that we don't bother the user with failed
117 rfc2015_secure_remove (const char *fname)
121 /* fixme: overwrite the file first */
127 sig_status_to_string (GpgmeSigStat status)
132 case GPGME_SIG_STAT_NONE:
133 result = _("Oops: Signature not verified");
135 case GPGME_SIG_STAT_NOSIG:
136 result = _("No signature found");
138 case GPGME_SIG_STAT_GOOD:
139 result = _("Good signature");
141 case GPGME_SIG_STAT_BAD:
142 result = _("BAD signature");
144 case GPGME_SIG_STAT_NOKEY:
145 result = _("No public key to verify the signature");
147 case GPGME_SIG_STAT_ERROR:
148 result = _("Error verifying the signature");
150 case GPGME_SIG_STAT_DIFF:
151 result = _("Different results for signatures");
154 result = _("Error: Unknown status");
162 sig_status_with_name (GpgmeSigStat status)
167 case GPGME_SIG_STAT_NONE:
168 result = _("Oops: Signature not verified");
170 case GPGME_SIG_STAT_NOSIG:
171 result = _("No signature found");
173 case GPGME_SIG_STAT_GOOD:
174 result = _("Good signature from \"%s\"");
176 case GPGME_SIG_STAT_BAD:
177 result = _("BAD signature from \"%s\"");
179 case GPGME_SIG_STAT_NOKEY:
180 result = _("No public key to verify the signature");
182 case GPGME_SIG_STAT_ERROR:
183 result = _("Error verifying the signature");
185 case GPGME_SIG_STAT_DIFF:
186 result = _("Different results for signatures");
189 result = _("Error: Unknown status");
197 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
198 GpgmeKey key, const gchar *fpr)
203 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
205 g_string_sprintfa (str, "%s\n",
206 sig_status_to_string (status));
207 if ((fpr != NULL) && (*fpr != '\0'))
208 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
209 g_string_append (str, _("Cannot find user ID for this key."));
212 g_string_sprintfa (str, sig_status_with_name (status), uid);
213 g_string_append (str, "\n");
216 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
220 g_string_sprintfa (str, _(" aka \"%s\"\n"),
226 sig_status_full (GpgmeCtx ctx)
235 struct tm *ctime_val;
239 str = g_string_new ("");
241 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
242 while (fpr != NULL) {
244 ctime_val = localtime (&created);
245 strftime (ctime_str, sizeof (ctime_str), "%c",
247 g_string_sprintfa (str,
248 _("Signature made %s\n"),
251 err = gpgme_get_sig_key (ctx, sig_idx, &key);
253 g_string_sprintfa (str, "%s\n",
254 sig_status_to_string (status));
255 if ((fpr != NULL) && (*fpr != '\0'))
256 g_string_sprintfa (str,
257 _("Key fingerprint: %s\n"),
260 sig_status_for_key (str, ctx, status, key, fpr);
261 gpgme_key_unref (key);
263 g_string_append (str, "\n\n");
265 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
269 g_string_free (str, FALSE);
273 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
277 GpgmeData sig = NULL, text = NULL;
278 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
279 GpgmegtkSigStatus statuswindow = NULL;
280 const char *result = NULL;
282 if (prefs_common.gpg_signature_popup)
283 statuswindow = gpgmegtk_sig_status_create ();
285 err = gpgme_new (&ctx);
287 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
291 /* don't include the last character (LF). It does not belong to the
293 err = gpgme_data_new_from_filepart (&text, NULL, fp,
294 mimeinfo->children->fpos,
295 mimeinfo->children->size ?
296 (mimeinfo->children->size - 1) : 0 );
298 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
299 partinfo->fpos, partinfo->size);
301 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
302 gpgme_strerror (err));
306 err = gpgme_op_verify (ctx, sig, text, &status);
308 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
310 /* FIXME: check what the heck this sig_status_full stuff is.
311 * it should better go into sigstatus.c */
312 g_free (partinfo->sigstatus_full);
313 partinfo->sigstatus_full = sig_status_full (ctx);
316 result = gpgmegtk_sig_status_to_string(status);
317 debug_print("verification status: %s\n", result);
318 if (prefs_common.gpg_signature_popup)
319 gpgmegtk_sig_status_update (statuswindow,ctx);
321 g_assert (!err); /* FIXME: Hey: this may indeed happen */
322 g_free (partinfo->sigstatus);
323 partinfo->sigstatus = g_strdup (result);
325 gpgme_data_release (sig);
326 gpgme_data_release (text);
328 if (prefs_common.gpg_signature_popup)
329 gpgmegtk_sig_status_destroy (statuswindow);
333 * Copy a gpgme data object to a temporary file and
334 * return this filename
338 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
347 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
348 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
350 if ((fp = fopen(tmp, "wb")) == NULL) {
351 FILE_OP_ERROR(tmp, "fopen");
356 err = gpgme_data_rewind ( data );
358 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
360 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
361 fwrite ( buf, nread, 1, fp );
364 if (err != GPGME_EOF)
365 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
375 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
379 GpgmeData cipher = NULL, plain = NULL;
380 struct passphrase_cb_info_s info;
382 memset (&info, 0, sizeof info);
384 err = gpgme_new (&ctx);
386 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
390 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
391 partinfo->fpos, partinfo->size);
393 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
394 gpgme_strerror (err));
398 err = gpgme_data_new (&plain);
400 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
404 if (!getenv("GPG_AGENT_INFO")) {
406 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
409 err = gpgme_op_decrypt (ctx, cipher, plain);
412 gpgme_data_release (cipher);
414 gpgmegtk_free_passphrase();
415 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
416 gpgme_data_release (plain);
420 debug_print ("** decryption succeeded");
426 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
433 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
436 debug_print ("** multipart/signed encountered");
438 /* check that we have at least 2 parts of the correct type */
439 for (partinfo = mimeinfo->children;
440 partinfo != NULL; partinfo = partinfo->next) {
441 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
442 "application/pgp-signature"))
449 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
451 return rfc2015_find_signature (mimeinfo) != NULL;
454 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
458 partinfo = rfc2015_find_signature (mimeinfo);
463 g_message ("** yep, it is a pgp signature");
464 dump_mimeinfo ("gpg-signature", partinfo );
465 dump_part (partinfo, fp );
466 dump_mimeinfo ("signed text", mimeinfo->children );
467 dump_part (mimeinfo->children, fp);
470 check_signature (mimeinfo, partinfo, fp);
473 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
477 if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
479 /* fixme: we should schek the protocol parameter */
483 gboolean rfc2015_msg_is_encrypted (gchar *file)
489 if ((fp = fopen(file, "r")) == NULL)
492 mimeinfo = procmime_scan_mime_header(fp);
498 ret = rfc2015_is_encrypted(mimeinfo);
499 procmime_mimeinfo_free_all(mimeinfo);
500 return ret != 0 ? TRUE : FALSE;
504 name_cmp(const char *a, const char *b)
506 for( ; *a && *b; a++, b++) {
508 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
516 headerp(char *p, char **names)
525 if(p2[-1] == ' ' || p2[-1] == '\t') {
534 for(i = 0 ; names[i] != NULL; i++) {
535 if(!name_cmp (names[i], p))
540 return names[i] != NULL;
544 #define DECRYPTION_ABORT() \
546 procmime_mimeinfo_free_all(tmpinfo); \
547 msginfo->decryption_failed = 1; \
551 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
554 MimeInfo *tmpinfo, *partinfo;
563 g_return_if_fail (msginfo != NULL);
564 g_return_if_fail (mimeinfo != NULL);
565 g_return_if_fail (fp != NULL);
566 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
568 debug_print ("** decrypting multipart/encrypted message");
571 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
573 tmpinfo = procmime_scan_mime_header(fp);
574 if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
578 procmime_scan_multipart_message(tmpinfo, fp);
580 /* check that we have the 2 parts */
581 partinfo = tmpinfo->children;
582 if (!partinfo || !partinfo->next) {
585 if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
586 /* Fixme: check that the version is 1 */
589 partinfo = partinfo->next;
591 !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
593 g_warning ("oops: pgp_encrypted with more than 2 parts");
599 debug_print ("** yep, it is pgp encrypted");
601 plain = pgp_decrypt (partinfo, fp);
606 fname = g_strdup_printf("%s%cplaintext.%08x",
607 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
609 if ((dstfp = fopen(fname, "wb")) == NULL) {
610 FILE_OP_ERROR(fname, "fopen");
615 /* write the orginal header to the new file */
616 if (fseek(fp, tmpinfo->fpos, SEEK_SET) < 0)
619 while (fgets(buf, sizeof(buf), fp)) {
620 if (headerp (buf, content_names))
622 if (buf[0] == '\r' || buf[0] == '\n')
627 err = gpgme_data_rewind (plain);
629 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
631 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
632 fwrite (buf, nread, 1, dstfp);
635 if (err != GPGME_EOF) {
636 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
640 procmime_mimeinfo_free_all(tmpinfo);
642 msginfo->plaintext_file = fname;
643 msginfo->decryption_failed = 0;
646 #undef DECRYPTION_ABORT
650 * plain contains an entire mime object.
651 * Encrypt it and return an GpgmeData object with the encrypted version of
652 * the file or NULL in case of error.
655 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
659 GpgmeData cipher = NULL;
661 err = gpgme_new (&ctx);
663 err = gpgme_data_new (&cipher);
665 gpgme_set_armor (ctx, 1);
666 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
670 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
671 gpgme_data_release (cipher);
675 debug_print ("** encryption succeeded");
683 * Create and return a list of keys matching a key id
686 GSList *rfc2015_create_signers_list (const char *keyid)
688 GSList *key_list = NULL;
689 GpgmeCtx list_ctx = NULL;
694 err = gpgme_new (&list_ctx);
697 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
700 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
701 key_list = g_slist_append (key_list, key);
703 if (err != GPGME_EOF)
706 if (key_list == NULL) {
707 debug_print ("no keys found for keyid \"%s\"\n", keyid);
712 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
713 for (p = key_list; p != NULL; p = p->next)
714 gpgme_key_unref ((GpgmeKey) p->data);
715 g_slist_free (key_list);
718 gpgme_release (list_ctx);
719 return err ? NULL : key_list;
723 * Encrypt the file by extracting all recipients and finding the
724 * encryption keys for all of them. The file content is then replaced
725 * by the encrypted one. */
727 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
731 int i, clineidx, saved_last;
732 char *clines[3] = {NULL};
734 GpgmeData header = NULL;
735 GpgmeData plain = NULL;
736 GpgmeData cipher = NULL;
737 GpgmeRecipients rset = NULL;
739 int mime_version_seen = 0;
740 char *boundary = create_boundary ();
742 /* Create the list of recipients */
743 rset = gpgmegtk_recipient_selection (recp_list);
745 debug_print ("error creating recipient list\n" );
749 /* Open the source file */
750 if ((fp = fopen(file, "rb")) == NULL) {
751 FILE_OP_ERROR(file, "fopen");
755 err = gpgme_data_new (&header);
757 err = gpgme_data_new (&plain);
759 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
763 /* get the content header lines from the source */
766 while (!err && fgets(buf, sizeof(buf), fp)) {
767 /* fixme: check for overlong lines */
768 if (headerp (buf, content_names)) {
769 if (clineidx >= DIM (clines)) {
770 debug_print ("rfc2015_encrypt: too many content lines\n");
773 clines[clineidx++] = g_strdup (buf);
779 if (*buf == ' ' || *buf == '\t') {
780 char *last = clines[clineidx-1];
781 clines[clineidx-1] = g_strconcat (last, buf, NULL);
787 if (headerp (buf, mime_version_name))
788 mime_version_seen = 1;
790 if (buf[0] == '\r' || buf[0] == '\n')
792 err = gpgme_data_write (header, buf, strlen (buf));
795 FILE_OP_ERROR (file, "fgets");
799 /* write them to the temp data and add the rest of the message */
800 for (i = 0; !err && i < clineidx; i++) {
801 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
802 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
805 err = gpgme_data_write (plain, "\r\n", 2);
806 while (!err && fgets(buf, sizeof(buf), fp)) {
807 err = gpgme_data_write (plain, buf, strlen (buf));
810 FILE_OP_ERROR (file, "fgets");
814 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
818 cipher = pgp_encrypt (plain, rset);
819 gpgme_data_release (plain); plain = NULL;
820 gpgme_recipients_release (rset); rset = NULL;
824 /* we have the encrypted message available in cipher and now we
825 * are going to rewrite the source file. To be sure that file has
826 * been truncated we use an approach which should work everywhere:
827 * close the file and then reopen it for writing. It is important
828 * that this works, otherwise it may happen that parts of the
829 * plaintext are still in the file (The encrypted stuff is, due to
830 * compression, usually shorter than the plaintext).
832 * Yes, there is a race condition here, but everyone, who is so
833 * stupid to store the temp file with the plaintext in a public
834 * directory has to live with this anyway. */
836 FILE_OP_ERROR(file, "fclose");
839 if ((fp = fopen(file, "wb")) == NULL) {
840 FILE_OP_ERROR(file, "fopen");
844 /* Write the header, append new content lines, part 1 and part 2 header */
845 err = gpgme_data_rewind (header);
847 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
850 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
851 fwrite (buf, nread, 1, fp);
853 if (err != GPGME_EOF) {
854 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
858 FILE_OP_ERROR (file, "fwrite");
861 gpgme_data_release (header); header = NULL;
863 if (!mime_version_seen)
864 fputs ("MIME-Version: 1\r\n", fp);
868 "Content-Type: text/plain; charset=us-ascii\r\n"
869 "Content-Transfer-Encoding: 7bit\r\n"
873 "Content-Type: multipart/encrypted;"
874 " protocol=\"application/pgp-encrypted\";\r\n"
875 " boundary=\"%s\"\r\n"
878 "Content-Type: application/pgp-encrypted\r\n"
883 "Content-Type: application/octet-stream\r\n"
885 boundary, boundary, boundary);
888 /* append the encrypted stuff */
889 err = gpgme_data_rewind (cipher);
891 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
892 gpgme_strerror (err));
896 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
897 fwrite (buf, nread, 1, fp);
899 if (err != GPGME_EOF) {
900 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
904 /* and the final boundary */
905 if (!ascii_armored) {
914 FILE_OP_ERROR (file, "fwrite");
918 gpgme_data_release (cipher);
924 gpgme_data_release (header);
925 gpgme_data_release (plain);
926 gpgme_data_release (cipher);
927 gpgme_recipients_release (rset);
929 return -1; /* error */
933 * plain contains an entire mime object. Sign it and return an
934 * GpgmeData object with the signature of it or NULL in case of error.
937 pgp_sign (GpgmeData plain, GSList *key_list)
942 GpgmeData sig = NULL;
943 struct passphrase_cb_info_s info;
945 memset (&info, 0, sizeof info);
947 err = gpgme_new (&ctx);
950 err = gpgme_data_new (&sig);
954 if (!getenv("GPG_AGENT_INFO")) {
956 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
958 gpgme_set_textmode (ctx, 1);
959 gpgme_set_armor (ctx, 1);
960 gpgme_signers_clear (ctx);
961 for (p = key_list; p != NULL; p = p->next) {
962 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
966 for (p = key_list; p != NULL; p = p->next)
967 gpgme_key_unref ((GpgmeKey) p->data);
968 g_slist_free (key_list);
972 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
976 gpgmegtk_free_passphrase();
977 debug_print ("signing failed: %s\n", gpgme_strerror (err));
978 gpgme_data_release (sig);
982 debug_print ("signing succeeded\n");
990 * Sign the file and replace its content with the signed one.
993 rfc2015_sign (const char *file, GSList *key_list)
997 int i, clineidx, saved_last;
998 char *clines[3] = {NULL};
1000 GpgmeData header = NULL;
1001 GpgmeData plain = NULL;
1002 GpgmeData sigdata = NULL;
1004 int mime_version_seen = 0;
1005 char *boundary = create_boundary ();
1007 /* Open the source file */
1008 if ((fp = fopen(file, "rb")) == NULL) {
1009 FILE_OP_ERROR(file, "fopen");
1013 err = gpgme_data_new (&header);
1015 err = gpgme_data_new (&plain);
1017 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1021 /* get the content header lines from the source */
1024 while (!err && fgets(buf, sizeof(buf), fp)) {
1025 /* fixme: check for overlong lines */
1026 if (headerp (buf, content_names)) {
1027 if (clineidx >= DIM (clines)) {
1028 debug_print ("rfc2015_sign: too many content lines\n");
1031 clines[clineidx++] = g_strdup (buf);
1037 if (*buf == ' ' || *buf == '\t') {
1038 char *last = clines[clineidx - 1];
1039 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1045 if (headerp (buf, mime_version_name))
1046 mime_version_seen = 1;
1048 if (buf[0] == '\r' || buf[0] == '\n')
1050 err = gpgme_data_write (header, buf, strlen (buf));
1053 FILE_OP_ERROR (file, "fgets");
1057 /* write them to the temp data and add the rest of the message */
1058 for (i = 0; !err && i < clineidx; i++) {
1059 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1062 err = gpgme_data_write (plain, "\r\n", 2 );
1063 while (!err && fgets(buf, sizeof(buf), fp)) {
1064 err = gpgme_data_write (plain, buf, strlen (buf));
1067 FILE_OP_ERROR (file, "fgets");
1071 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1075 sigdata = pgp_sign (plain, key_list);
1079 /* we have the signed message available in sigdata and now we are
1080 * going to rewrite the original file. To be sure that file has
1081 * been truncated we use an approach which should work everywhere:
1082 * close the file and then reopen it for writing. */
1084 FILE_OP_ERROR(file, "fclose");
1087 if ((fp = fopen(file, "wb")) == NULL) {
1088 FILE_OP_ERROR(file, "fopen");
1092 /* Write the rfc822 header and add new content lines */
1093 err = gpgme_data_rewind (header);
1095 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1096 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1097 fwrite (buf, nread, 1, fp);
1099 if (err != GPGME_EOF) {
1100 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1104 FILE_OP_ERROR (file, "fwrite");
1107 gpgme_data_release (header);
1110 if (!mime_version_seen)
1111 fputs ("MIME-Version: 1\r\n", fp);
1112 fprintf (fp, "Content-Type: multipart/signed; "
1113 "protocol=\"application/pgp-signature\";\r\n"
1114 " boundary=\"%s\"\r\n", boundary );
1116 /* Part 1: signed material */
1117 fprintf (fp, "\r\n--%s\r\n", boundary);
1118 err = gpgme_data_rewind (plain);
1120 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1121 gpgme_strerror (err));
1124 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1125 fwrite (buf, nread, 1, fp);
1127 if (err != GPGME_EOF) {
1128 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1132 /* Part 2: signature */
1133 fprintf (fp, "\r\n--%s\r\n", boundary);
1134 fputs ("Content-Type: application/pgp-signature\r\n"
1137 err = gpgme_data_rewind (sigdata);
1139 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1140 gpgme_strerror (err));
1144 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1145 fwrite (buf, nread, 1, fp);
1147 if (err != GPGME_EOF) {
1148 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1152 /* Final boundary */
1153 fprintf (fp, "\r\n--%s--\r\n\r\n", boundary);
1156 FILE_OP_ERROR (file, "fwrite");
1160 gpgme_data_release (header);
1161 gpgme_data_release (plain);
1162 gpgme_data_release (sigdata);
1169 gpgme_data_release (header);
1170 gpgme_data_release (plain);
1171 gpgme_data_release (sigdata);
1173 return -1; /* error */
1178 * Create a new boundary in a way that it is very unlikely that this
1179 * will occur in the following text. It would be easy to ensure
1180 * uniqueness if everything is either quoted-printable or base64
1181 * encoded (note that conversion is allowed), but because MIME bodies
1182 * may be nested, it may happen that the same boundary has already
1183 * been used. We avoid scanning the message for conflicts and hope the
1186 * boundary := 0*69<bchars> bcharsnospace
1187 * bchars := bcharsnospace / " "
1188 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
1189 * "+" / "_" / "," / "-" / "." /
1190 * "/" / ":" / "=" / "?"
1194 create_boundary (void)
1196 static char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1197 "abcdefghijklmnopqrstuvwxyz"
1198 "1234567890'()+_,./:=?";
1205 /* We make the boundary depend on the pid, so that all running
1206 * processed generate different values even when they have been
1207 * started within the same second and srand48(time(NULL)) has been
1208 * used. I can't see whether this is really an advantage but it
1209 * doesn't do any harm.
1212 for(i = 0; i < sizeof(buf) - 1; i++) {
1213 buf[i] = tbl[(lrand48() ^ pid) % (sizeof(tbl) - 1)]; /* fill with random */
1214 if(buf[i] == '=' && equal == -1)
1219 /* now make sure that we do have the sequence "=." in it which cannot
1220 * be matched by quoted-printable or base64 encoding */
1221 if(equal != -1 && (equal+1) < i)
1228 return g_strdup(buf);
1231 #endif /* USE_GPGME */