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
[] = {
65 struct passphrase_cb_info_s
{
70 static char *create_boundary (void);
72 static void dump_mimeinfo (const char *text
, MimeInfo
*x
)
74 g_message ("** MimeInfo[%s] %p level=%d",
75 text
, x
, x
? x
->level
:0 );
79 g_message ("** enc=`%s' enc_type=%d mime_type=%d",
80 x
->encoding
, x
->encoding_type
, x
->mime_type
);
81 g_message ("** cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'",
82 x
->content_type
, x
->charset
, x
->name
, x
->boundary
);
83 g_message ("** cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d",
84 x
->content_disposition
, x
->filename
, x
->fpos
, x
->size
,
86 dump_mimeinfo (".main", x
->main
);
87 dump_mimeinfo (".sub", x
->sub
);
88 dump_mimeinfo (".next", x
->next
);
89 g_message ("** MimeInfo[.parent] %p", x
);
90 dump_mimeinfo (".children", x
->children
);
91 dump_mimeinfo (".plaintext", x
->plaintext
);
94 static void dump_part ( MimeInfo
*mimeinfo
, FILE *fp
)
96 unsigned int size
= mimeinfo
->size
;
99 if (fseek (fp
, mimeinfo
->fpos
, SEEK_SET
)) {
100 g_warning ("dump_part: fseek error");
104 g_message ("** --- begin dump_part ----");
105 while (size
-- && (c
= getc (fp
)) != EOF
)
108 g_warning ("dump_part: read error");
109 g_message ("** --- end dump_part ----");
113 rfc2015_disable_all (void)
115 /* FIXME: set a flag, so that we don't bother the user with failed
121 rfc2015_secure_remove (const char *fname
)
125 /* fixme: overwrite the file first */
131 sig_status_to_string (GpgmeSigStat status
)
136 case GPGME_SIG_STAT_NONE
:
137 result
= _("Oops: Signature not verified");
139 case GPGME_SIG_STAT_NOSIG
:
140 result
= _("No signature found");
142 case GPGME_SIG_STAT_GOOD
:
143 result
= _("Good signature");
145 case GPGME_SIG_STAT_BAD
:
146 result
= _("BAD signature");
148 case GPGME_SIG_STAT_NOKEY
:
149 result
= _("No public key to verify the signature");
151 case GPGME_SIG_STAT_ERROR
:
152 result
= _("Error verifying the signature");
154 case GPGME_SIG_STAT_DIFF
:
155 result
= _("Different results for signatures");
158 result
= _("Error: Unknown status");
166 sig_status_with_name (GpgmeSigStat status
)
171 case GPGME_SIG_STAT_NONE
:
172 result
= _("Oops: Signature not verified");
174 case GPGME_SIG_STAT_NOSIG
:
175 result
= _("No signature found");
177 case GPGME_SIG_STAT_GOOD
:
178 result
= _("Good signature from \"%s\"");
180 case GPGME_SIG_STAT_BAD
:
181 result
= _("BAD signature from \"%s\"");
183 case GPGME_SIG_STAT_NOKEY
:
184 result
= _("No public key to verify the signature");
186 case GPGME_SIG_STAT_ERROR
:
187 result
= _("Error verifying the signature");
189 case GPGME_SIG_STAT_DIFF
:
190 result
= _("Different results for signatures");
193 result
= _("Error: Unknown status");
201 sig_status_for_key(GString
*str
, GpgmeCtx ctx
, GpgmeSigStat status
,
202 GpgmeKey key
, const gchar
*fpr
)
207 uid
= gpgme_key_get_string_attr (key
, GPGME_ATTR_USERID
, NULL
, idx
);
209 g_string_sprintfa (str
, "%s\n",
210 sig_status_to_string (status
));
211 if ((fpr
!= NULL
) && (*fpr
!= '\0'))
212 g_string_sprintfa (str
, "Key fingerprint: %s\n", fpr
);
213 g_string_append (str
, _("Cannot find user ID for this key."));
216 g_string_sprintfa (str
, sig_status_with_name (status
), uid
);
217 g_string_append (str
, "\n");
220 uid
= gpgme_key_get_string_attr (key
, GPGME_ATTR_USERID
,
224 g_string_sprintfa (str
, _(" aka \"%s\"\n"),
230 sig_status_full (GpgmeCtx ctx
)
239 struct tm
*ctime_val
;
243 str
= g_string_new ("");
245 fpr
= gpgme_get_sig_status (ctx
, sig_idx
, &status
, &created
);
246 while (fpr
!= NULL
) {
248 ctime_val
= localtime (&created
);
249 strftime (ctime_str
, sizeof (ctime_str
), "%c",
251 g_string_sprintfa (str
,
252 _("Signature made %s\n"),
255 err
= gpgme_get_sig_key (ctx
, sig_idx
, &key
);
257 g_string_sprintfa (str
, "%s\n",
258 sig_status_to_string (status
));
259 if ((fpr
!= NULL
) && (*fpr
!= '\0'))
260 g_string_sprintfa (str
,
261 _("Key fingerprint: %s\n"),
264 sig_status_for_key (str
, ctx
, status
, key
, fpr
);
265 gpgme_key_unref (key
);
267 g_string_append (str
, "\n\n");
269 fpr
= gpgme_get_sig_status (ctx
, ++sig_idx
, &status
, &created
);
273 g_string_free (str
, FALSE
);
277 static void check_signature (MimeInfo
*mimeinfo
, MimeInfo
*partinfo
, FILE *fp
)
281 GpgmeData sig
= NULL
, text
= NULL
;
282 GpgmeSigStat status
= GPGME_SIG_STAT_NONE
;
283 GpgmegtkSigStatus statuswindow
= NULL
;
284 const char *result
= NULL
;
286 if (prefs_common
.gpg_signature_popup
)
287 statuswindow
= gpgmegtk_sig_status_create ();
289 err
= gpgme_new (&ctx
);
291 g_warning ("gpgme_new failed: %s", gpgme_strerror (err
));
295 /* don't include the last character (LF). It does not belong to the
297 err
= gpgme_data_new_from_filepart (&text
, NULL
, fp
,
298 mimeinfo
->children
->fpos
,
299 mimeinfo
->children
->size
?
300 (mimeinfo
->children
->size
- 1) : 0 );
302 err
= gpgme_data_new_from_filepart (&sig
, NULL
, fp
,
303 partinfo
->fpos
, partinfo
->size
);
305 g_message ("gpgme_data_new_from_filepart failed: %s",
306 gpgme_strerror (err
));
310 err
= gpgme_op_verify (ctx
, sig
, text
, &status
);
312 g_message ("gpgme_op_verify failed: %s", gpgme_strerror (err
));
314 /* FIXME: check what the heck this sig_status_full stuff is.
315 * it should better go into sigstatus.c */
316 g_free (partinfo
->sigstatus_full
);
317 partinfo
->sigstatus_full
= sig_status_full (ctx
);
320 result
= gpgmegtk_sig_status_to_string(status
);
321 debug_print("verification status: %s\n", result
);
322 if (prefs_common
.gpg_signature_popup
)
323 gpgmegtk_sig_status_update (statuswindow
,ctx
);
325 g_assert (!err
); /* FIXME: Hey: this may indeed happen */
326 g_free (partinfo
->sigstatus
);
327 partinfo
->sigstatus
= g_strdup (result
);
329 gpgme_data_release (sig
);
330 gpgme_data_release (text
);
332 if (prefs_common
.gpg_signature_popup
)
333 gpgmegtk_sig_status_destroy (statuswindow
);
337 passphrase_cb (void *opaque
, const char *desc
, void *r_hd
)
339 struct passphrase_cb_info_s
*info
= opaque
;
340 GpgmeCtx ctx
= info
? info
->c
: NULL
;
344 /* FIXME: cleanup by looking at *r_hd */
348 gpgmegtk_set_passphrase_grab (prefs_common
.passphrase_grab
);
349 g_message ("%% requesting passphrase for `%s': ", desc
);
350 pass
= gpgmegtk_passphrase_mbox (desc
);
352 g_message ("%% cancel passphrase entry");
356 g_message ("%% sending passphrase");
362 * Copy a gpgme data object to a temporary file and
363 * return this filename
366 copy_gpgmedata_to_temp (GpgmeData data
, guint
*length
)
375 tmp
= g_strdup_printf("%s%cgpgtmp.%08x",
376 get_mime_tmp_dir(), G_DIR_SEPARATOR
, ++id
);
378 if ((fp
= fopen(tmp
, "w")) == NULL
) {
379 FILE_OP_ERROR(tmp
, "fopen");
384 err
= gpgme_data_rewind ( data
);
386 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err
));
388 while (!(err
= gpgme_data_read (data
, buf
, 100, &nread
))) {
389 fwrite ( buf
, nread
, 1, fp
);
392 if (err
!= GPGME_EOF
)
393 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err
));
402 pgp_decrypt (MimeInfo
*partinfo
, FILE *fp
)
406 GpgmeData cipher
= NULL
, plain
= NULL
;
407 struct passphrase_cb_info_s info
;
409 memset (&info
, 0, sizeof info
);
411 err
= gpgme_new (&ctx
);
413 g_message ("gpgme_new failed: %s", gpgme_strerror (err
));
417 err
= gpgme_data_new_from_filepart (&cipher
, NULL
, fp
,
418 partinfo
->fpos
, partinfo
->size
);
420 g_message ("gpgme_data_new_from_filepart failed: %s",
421 gpgme_strerror (err
));
425 err
= gpgme_data_new (&plain
);
427 g_message ("gpgme_new failed: %s", gpgme_strerror (err
));
431 if (!getenv("GPG_AGENT_INFO")) {
433 gpgme_set_passphrase_cb (ctx
, passphrase_cb
, &info
);
436 err
= gpgme_op_decrypt (ctx
, cipher
, plain
);
439 gpgme_data_release (cipher
);
441 g_warning ("** decryption failed: %s", gpgme_strerror (err
));
442 gpgme_data_release (plain
);
446 g_message ("** decryption succeeded");
452 MimeInfo
* rfc2015_find_signature (MimeInfo
*mimeinfo
)
459 if (g_strcasecmp (mimeinfo
->content_type
, "multipart/signed"))
462 g_message ("** multipart/signed encountered");
464 /* check that we have at least 2 parts of the correct type */
465 for (partinfo
= mimeinfo
->children
;
466 partinfo
!= NULL
; partinfo
= partinfo
->next
) {
467 if (++n
> 1 && !g_strcasecmp (partinfo
->content_type
,
468 "application/pgp-signature"))
475 gboolean
rfc2015_has_signature (MimeInfo
*mimeinfo
)
477 return rfc2015_find_signature (mimeinfo
) != NULL
;
480 void rfc2015_check_signature (MimeInfo
*mimeinfo
, FILE *fp
)
484 partinfo
= rfc2015_find_signature (mimeinfo
);
489 g_message ("** yep, it is a pgp signature");
490 dump_mimeinfo ("gpg-signature", partinfo
);
491 dump_part (partinfo
, fp
);
492 dump_mimeinfo ("signed text", mimeinfo
->children
);
493 dump_part (mimeinfo
->children
, fp
);
496 check_signature (mimeinfo
, partinfo
, fp
);
499 int rfc2015_is_encrypted (MimeInfo
*mimeinfo
)
503 if (g_strcasecmp (mimeinfo
->content_type
, "multipart/encrypted"))
505 /* fixme: we should schek the protocol parameter */
510 name_cmp(const char *a
, const char *b
)
512 for( ; *a
&& *b
; a
++, b
++) {
514 && toupper(*(unsigned char *)a
) != toupper(*(unsigned char *)b
))
522 headerp(char *p
, char **names
)
531 if(p2
[-1] == ' ' || p2
[-1] == '\t') {
540 for(i
= 0 ; names
[i
] != NULL
; i
++) {
541 if(!name_cmp (names
[i
], p
))
546 return names
[i
] != NULL
;
550 void rfc2015_decrypt_message (MsgInfo
*msginfo
, MimeInfo
*mimeinfo
, FILE *fp
)
563 g_return_if_fail (mimeinfo
->mime_type
== MIME_MULTIPART
);
565 g_message ("** multipart/encrypted encountered");
568 if (fseek(fp
, mimeinfo
->fpos
, SEEK_SET
) < 0)
570 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
571 if (buf
[0] == '\r' || buf
[0] == '\n') break;
573 procmime_scan_multipart_message(mimeinfo
, fp
);
575 /* check that we have the 2 parts */
577 for (partinfo
= mimeinfo
->children
; partinfo
; partinfo
= partinfo
->next
) {
578 if (++n
== 1 && !g_strcasecmp (partinfo
->content_type
,
579 "application/pgp-encrypted")) {
580 /* Fixme: check that the version is 1 */
583 else if (n
== 2 && !g_strcasecmp (partinfo
->content_type
,
584 "application/octet-stream")) {
586 g_warning ("** oops: pgp_encrypted with more than 2 parts");
591 if (!ver_okay
|| !partinfo
) {
592 msginfo
->decryption_failed
= 1;
593 /* fixme: remove the stuff, that the above procmime_scan_multiparts()
594 * has appended to mimeino */
598 g_message ("** yep, it is pgp encrypted");
600 plain
= pgp_decrypt (partinfo
, fp
);
602 msginfo
->decryption_failed
= 1;
606 fname
= g_strdup_printf("%s%cplaintext.%08x",
607 get_mime_tmp_dir(), G_DIR_SEPARATOR
, ++id
);
609 if ((dstfp
= fopen(fname
, "w")) == NULL
) {
610 FILE_OP_ERROR(fname
, "fopen");
612 msginfo
->decryption_failed
= 1;
616 /* write the orginal header to the new file */
617 if (fseek(fp
, mimeinfo
->fpos
, SEEK_SET
) < 0)
620 while (fgets(buf
, sizeof(buf
), fp
)) {
621 if (headerp (buf
, content_names
))
623 if (buf
[0] == '\r' || buf
[0] == '\n')
628 err
= gpgme_data_rewind (plain
);
630 g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err
));
632 while (!(err
= gpgme_data_read (plain
, buf
, sizeof(buf
), &nread
))) {
633 fwrite (buf
, nread
, 1, dstfp
);
636 if (err
!= GPGME_EOF
) {
637 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err
));
642 msginfo
->plaintext_file
= fname
;
643 msginfo
->decryption_failed
= 0;
648 * plain contains an entire mime object.
649 * Encrypt it and return an GpgmeData object with the encrypted version of
650 * the file or NULL in case of error.
653 pgp_encrypt ( GpgmeData plain
, GpgmeRecipients rset
)
657 GpgmeData cipher
= NULL
;
659 err
= gpgme_new (&ctx
);
661 err
= gpgme_data_new (&cipher
);
663 gpgme_set_armor (ctx
, 1);
664 err
= gpgme_op_encrypt (ctx
, rset
, plain
, cipher
);
668 g_warning ("** encryption failed: %s", gpgme_strerror (err
));
669 gpgme_data_release (cipher
);
673 g_message ("** encryption succeeded");
682 * Encrypt the file by extracting all recipients and finding the
683 * encryption keys for all of them. The file content is then replaced
684 * by the encrypted one. */
686 rfc2015_encrypt (const char *file
, GSList
*recp_list
)
690 int i
, clineidx
, saved_last
;
691 char *clines
[3] = {NULL
};
693 GpgmeData header
= NULL
;
694 GpgmeData plain
= NULL
;
695 GpgmeData cipher
= NULL
;
696 GpgmeRecipients rset
= NULL
;
698 int mime_version_seen
= 0;
699 char *boundary
= create_boundary ();
701 /* Create the list of recipients */
702 rset
= gpgmegtk_recipient_selection (recp_list
);
704 g_warning ("error creating recipient list" );
708 /* Open the source file */
709 if ((fp
= fopen(file
, "rb")) == NULL
) {
710 FILE_OP_ERROR(file
, "fopen");
714 err
= gpgme_data_new (&header
);
716 err
= gpgme_data_new (&plain
);
718 g_message ("gpgme_data_new failed: %s", gpgme_strerror (err
));
722 /* get the content header lines from the source */
725 while (!err
&& fgets(buf
, sizeof(buf
), fp
)) {
726 /* fixme: check for overlong lines */
727 if (headerp (buf
, content_names
)) {
728 if (clineidx
>= DIM (clines
)) {
729 g_message ("rfc2015_encrypt: too many content lines");
732 clines
[clineidx
++] = g_strdup (buf
);
738 if (*buf
== ' ' || *buf
== '\t') {
739 char *last
= clines
[clineidx
-1];
740 clines
[clineidx
-1] = g_strconcat (last
, buf
, NULL
);
746 if (headerp (buf
, mime_version_name
))
747 mime_version_seen
= 1;
749 if (buf
[0] == '\r' || buf
[0] == '\n')
751 err
= gpgme_data_write (header
, buf
, strlen (buf
));
754 FILE_OP_ERROR (file
, "fgets");
758 /* write them to the temp data and add the rest of the message */
759 for (i
= 0; !err
&& i
< clineidx
; i
++) {
760 g_message ("%% %s:%d: cline=`%s'", __FILE__
,__LINE__
, clines
[i
]);
761 err
= gpgme_data_write (plain
, clines
[i
], strlen (clines
[i
]));
764 err
= gpgme_data_write (plain
, "\r\n", 2);
765 while (!err
&& fgets(buf
, sizeof(buf
), fp
)) {
766 err
= gpgme_data_write (plain
, buf
, strlen (buf
));
769 FILE_OP_ERROR (file
, "fgets");
773 g_warning ("** gpgme_data_write failed: %s", gpgme_strerror (err
));
777 cipher
= pgp_encrypt (plain
, rset
);
778 gpgme_data_release (plain
); plain
= NULL
;
779 gpgme_recipients_release (rset
); rset
= NULL
;
783 /* we have the encrypted message available in cipher and now we
784 * are going to rewrite the source file. To be sure that file has
785 * been truncated we use an approach which should work everywhere:
786 * close the file and then reopen it for writing. It is important
787 * that this works, otherwise it may happen that parts of the
788 * plaintext are still in the file (The encrypted stuff is, due to
789 * compression, usually shorter than the plaintext).
791 * Yes, there is a race condition here, but everyone, who is so
792 * stupid to store the temp file with the plaintext in a public
793 * directory has to live with this anyway. */
795 FILE_OP_ERROR(file
, "fclose");
798 if ((fp
= fopen(file
, "wb")) == NULL
) {
799 FILE_OP_ERROR(file
, "fopen");
803 /* Write the header, append new content lines, part 1 and part 2 header */
804 err
= gpgme_data_rewind (header
);
806 g_message ("gpgme_data_rewind failed: %s", gpgme_strerror (err
));
809 while (!(err
= gpgme_data_read (header
, buf
, BUFFSIZE
, &nread
))) {
810 fwrite (buf
, nread
, 1, fp
);
812 if (err
!= GPGME_EOF
) {
813 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err
));
817 FILE_OP_ERROR (file
, "fwrite");
820 gpgme_data_release (header
); header
= NULL
;
822 if (!mime_version_seen
)
823 fputs ("MIME-Version: 1\r\n", fp
);
826 "Content-Type: multipart/encrypted;"
827 " protocol=\"application/pgp-encrypted\";\r\n"
828 " boundary=\"%s\"\r\n"
831 "Content-Type: application/pgp-encrypted\r\n"
836 "Content-Type: application/octet-stream\r\n"
838 boundary
, boundary
, boundary
);
840 /* append the encrypted stuff */
841 err
= gpgme_data_rewind (cipher
);
843 g_warning ("** gpgme_data_rewind on cipher failed: %s",
844 gpgme_strerror (err
));
848 while (!(err
= gpgme_data_read (cipher
, buf
, BUFFSIZE
, &nread
))) {
849 fwrite (buf
, nread
, 1, fp
);
851 if (err
!= GPGME_EOF
) {
852 g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err
));
856 /* and the final boundary */
864 FILE_OP_ERROR (file
, "fwrite");
868 gpgme_data_release (cipher
);
874 gpgme_data_release (header
);
875 gpgme_data_release (plain
);
876 gpgme_data_release (cipher
);
877 gpgme_recipients_release (rset
);
879 return -1; /* error */
883 set_signers (GpgmeCtx ctx
, PrefsAccount
*ac
)
885 GSList
*key_list
= NULL
;
886 GpgmeCtx list_ctx
= NULL
;
887 const char *keyid
= NULL
;
895 switch (ac
->sign_key
) {
896 case SIGN_KEY_DEFAULT
:
897 return 0; /* nothing to do */
899 case SIGN_KEY_BY_FROM
:
903 case SIGN_KEY_CUSTOM
:
904 keyid
= ac
->sign_key_id
;
908 g_assert_not_reached ();
911 err
= gpgme_new (&list_ctx
);
914 err
= gpgme_op_keylist_start (list_ctx
, keyid
, 1);
917 while ( !(err
= gpgme_op_keylist_next (list_ctx
, &key
)) ) {
918 key_list
= g_slist_append (key_list
, key
);
920 if (err
!= GPGME_EOF
)
922 if (key_list
== NULL
) {
923 g_warning ("no keys found for keyid \"%s\"", keyid
);
925 gpgme_signers_clear (ctx
);
926 for (p
= key_list
; p
!= NULL
; p
= p
->next
) {
927 err
= gpgme_signers_add (ctx
, (GpgmeKey
) p
->data
);
934 g_message ("** set_signers failed: %s", gpgme_strerror (err
));
935 for (p
= key_list
; p
!= NULL
; p
= p
->next
)
936 gpgme_key_unref ((GpgmeKey
) p
->data
);
937 g_slist_free (key_list
);
939 gpgme_release (list_ctx
);
944 * plain contains an entire mime object. Sign it and return an
945 * GpgmeData object with the signature of it or NULL in case of error.
948 pgp_sign (GpgmeData plain
, PrefsAccount
*ac
)
952 GpgmeData sig
= NULL
;
953 struct passphrase_cb_info_s info
;
955 memset (&info
, 0, sizeof info
);
957 err
= gpgme_new (&ctx
);
960 err
= gpgme_data_new (&sig
);
964 if (!getenv("GPG_AGENT_INFO")) {
966 gpgme_set_passphrase_cb (ctx
, passphrase_cb
, &info
);
968 gpgme_set_textmode (ctx
, 1);
969 gpgme_set_armor (ctx
, 1);
970 err
= set_signers (ctx
, ac
);
973 err
= gpgme_op_sign (ctx
, plain
, sig
, GPGME_SIG_MODE_DETACH
);
977 g_message ("** signing failed: %s", gpgme_strerror (err
));
978 gpgme_data_release (sig
);
982 g_message ("** signing succeeded");
990 * Sign the file and replace its content with the signed one.
993 rfc2015_sign (const char *file
, PrefsAccount
*ac
)
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 g_message ("gpgme_data_new failed: %s", 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 g_message ("rfc2015_sign: too many content lines");
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 g_message ("** gpgme_data_write failed: %s", gpgme_strerror (err
));
1075 sigdata
= pgp_sign (plain
, ac
);
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 g_message ("** gpgme_data_rewind failed: %s", 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 g_message ("** gpgme_data_read failed: %s", 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 g_message ("** gpgme_data_rewind on plain failed: %s",
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 g_message ("** gpgme_data_read failed: %s", 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 g_message ("** gpgme_data_rewind on sigdata failed: %s",
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 g_message ("** gpgme_data_read failed: %s", 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 */