From c7b5e1df086bcc37ff40163ee67571f076cf6683 Mon Sep 17 00:00:00 2001 From: markus schnalke Date: Sat, 7 Jul 2012 12:31:38 +0200 Subject: [PATCH] send: Sign and encrypt messages automatically, based on Sign: and Enc: Hdrs. Currently, the value of the header fields is ignored. The sole presence of such a header caused signing or encryption. --- config/config.c | 4 ++- h/mh.h | 2 ++ man/mh-profile.man5 | 24 ++++++++++++++++++ sbr/readconfig.c | 2 ++ uip/mhparam.c | 2 ++ uip/send.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) diff --git a/config/config.c b/config/config.c index 935c92d..52ef436 100644 --- a/config/config.c +++ b/config/config.c @@ -94,8 +94,10 @@ char *nsequence = "Sequence-Negation"; /* profile entries for storage locations */ char *nmhstorage = "nmh-storage"; -/* Default attachment header field name */ +/* Default header field names */ char *attach_hdr = "Attach"; +char *sign_hdr = "Sign"; +char *enc_hdr = "Enc"; /* the tool to query the mime type of a file */ char *mimetypequery = "Mime-Type-Query"; diff --git a/h/mh.h b/h/mh.h index bee5518..1331a14 100644 --- a/h/mh.h +++ b/h/mh.h @@ -273,6 +273,8 @@ extern char *mailstore; /* name of mail storage directory */ ** on any system. */ extern char *attach_hdr; +extern char *sign_hdr; +extern char *enc_hdr; extern char *components; extern char *context; extern char *curfolder; diff --git a/man/mh-profile.man5 b/man/mh-profile.man5 index a3f5ae2..1499c06 100644 --- a/man/mh-profile.man5 +++ b/man/mh-profile.man5 @@ -182,6 +182,30 @@ If you like to type a lot, name it `X-MH-Attachment'. (profile, default: `Attach') .RE .PP +.BR Sign-Header : +Sign +.RS 5 +The (pseudo) header in draft messages, that requests the message to be +signed automatically by +.BR send , +using +.BR mhsign . +If you like to type a lot, name it `X-MH-Sign-This-Message'. +(profile, default: `Sign') +.RE +.PP +.BR Enc-Header : +Enc +.RS 5 +The (pseudo) header in draft messages, that requests the message to be +signed and encrypted by +.BR send , +using +.BR mhsign . +If you like to type a lot, name it `X-MH-Encrypt-This-Message'. +(profile, default: `Enc') +.RE +.PP .BR Mime-Type-Query : file \-b \-\-mime .RS 5 diff --git a/sbr/readconfig.c b/sbr/readconfig.c index 1b00bf9..d93d04b 100644 --- a/sbr/readconfig.c +++ b/sbr/readconfig.c @@ -17,6 +17,8 @@ struct procstr { static struct procstr procs[] = { { "attachment-header", &attach_hdr }, + { "sign-header", &sign_hdr }, + { "enc-header", &enc_hdr }, { "context", &context }, { "mh-sequences", &mh_seq }, { "draft-folder", &draftfolder }, diff --git a/uip/mhparam.c b/uip/mhparam.c index 87b4d17..4e1fc21 100644 --- a/uip/mhparam.c +++ b/uip/mhparam.c @@ -34,6 +34,8 @@ struct proc { static struct proc procs [] = { { "attachment-header", &attach_hdr }, + { "sign-header", &sign_hdr }, + { "enc-header", &enc_hdr }, { "context", &context }, { "mh-sequences", &mh_seq }, { "editor", &defaulteditor }, diff --git a/uip/send.c b/uip/send.c index 0660e95..76280aa 100644 --- a/uip/send.c +++ b/uip/send.c @@ -46,6 +46,7 @@ static void armed_done(int) NORETURN; static void anno(struct stat *); static int sendaux(char **, int, char *, struct stat *); static int attach(char *); +static int signandenc(char *); static void clean_up_temporary_files(void); static int get_line(void); static void make_mime_composition_file_entry(char *); @@ -261,6 +262,26 @@ sendsbr(char **vec, int vecp, char *drft, struct stat *st) break; } + /* + ** Sign and encrypt the message as needed. + ** Use the mhbuild composition file for the draft if there was + ** a successful conversion because that now contains the MIME + ** message. A nice side effect of this is that it leaves the + ** original draft file untouched so that it can be retrieved + ** and modified if desired. + */ + switch (signandenc(drft)) { + case OK: + drft = composition_file_name; + break; + + case NOTOK: + return (NOTOK); + + case DONE: + break; + } + done=armed_done; switch (setjmp(env)) { case OK: @@ -434,6 +455,53 @@ attach(char *draft_file_name) return (OK); } +static int +signandenc(char *draft_file_name) +{ + char buf[BUFSIZ]; + int dosign = 0; + int doenc = 0; + + if (!(draft_file = fopen(draft_file_name, "r"))) { + adios(NULL, "can't open draft file `%s'.", draft_file_name); + } + + /* We'll grow the buffer as needed. */ + field = (char *)mh_xmalloc(field_size = 256); + + /* Scan the draft file for an attachment header field name. */ + while (get_line() != EOF && *field != '\0' && *field != '-') { + if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 && + field[strlen(sign_hdr)] == ':') { + dosign = 1; + } + if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 && + field[strlen(enc_hdr)] == ':') { + doenc = 1; + } + } + if (!dosign && !doenc) { + return DONE; + } + + strcpy(composition_file_name, draft_file_name); + + /* We're ready to roll! */ + sprintf(buf, "mhsign -m%s '%s'", doenc ? " -e" : "", + draft_file_name); + if (system(buf) != 0) { + /* some problem */ + return (NOTOK); + } + /* Remove the automatically created backup of mhsign. */ + snprintf(buf, sizeof buf, "%s.orig", draft_file_name); + if (unlink(buf) == -1) { + advise(NULL, "unable to remove original draft file."); + } + + return (OK); +} + static void clean_up_temporary_files(void) { -- 1.7.10.4