ENGINE-433: finally the right solution. ONLY replaces subjects from protected headers for now.
authorKrista Bennett <krista@pep-project.org>
Wed, 30 May 2018 11:21:06 +0200
changeset 2700cd9a4efecb62
parent 2699 dd68d5551e11
child 2701 180e30819be3
child 2703 e1ff8dbe65f1
ENGINE-433: finally the right solution. ONLY replaces subjects from protected headers for now.
src/message_api.c
src/mime.c
     1.1 --- a/src/message_api.c	Mon May 28 11:19:20 2018 +0200
     1.2 +++ b/src/message_api.c	Wed May 30 11:21:06 2018 +0200
     1.3 @@ -3131,9 +3131,13 @@
     1.4                  
     1.5                  /* Ensure messages whose maintext is in the attachments
     1.6                     move main text into message struct longmsg et al */
     1.7 -                if (pull_up_attached_main_msg(msg) && msg->shortmsg) {
     1.8 +                /* KG: This IS a src modification of old - we're adding to it
     1.9 +                   w/ memhole subject, but the question is whether or not
    1.10 +                   this is OK overall... */
    1.11 +                pull_up_attached_main_msg(msg);
    1.12 +                if (msg->shortmsg) {
    1.13                      free(src->shortmsg);
    1.14 -                    src->shortmsg = strdup(msg->shortmsg);
    1.15 +                    src->shortmsg = strdup(msg->shortmsg);                    
    1.16                  }
    1.17  
    1.18                  // check for private key in decrypted message attachment while importing
     2.1 --- a/src/mime.c	Mon May 28 11:19:20 2018 +0200
     2.2 +++ b/src/mime.c	Wed May 30 11:21:06 2018 +0200
     2.3 @@ -1433,6 +1433,47 @@
     2.4      return PEP_STATUS_OK;
     2.5  }
     2.6  
     2.7 +// THIS IS A BEST-EFFORT ONLY FUNCTION, AND WE ARE NOT DOING MORE THAN THE
     2.8 +// SUBJECT FOR NOW.
     2.9 +static PEP_STATUS interpret_protected_headers(
    2.10 +        struct mailmime* mime, 
    2.11 +        message* msg
    2.12 +    )
    2.13 +{
    2.14 +    // N.B. this is *very much* enigmail output specific, and right now,
    2.15 +    // we only care about subject replacement.
    2.16 +    const char* header_string = "Content-Type: text/rfc822-headers; protected-headers=\"v1\"\nContent-Disposition: inline\n\n";
    2.17 +    size_t content_length = mime->mm_length;
    2.18 +    size_t header_strlen = strlen(header_string);
    2.19 +    if (header_strlen < content_length) {
    2.20 +        const char* headerblock = mime->mm_mime_start;
    2.21 +        size_t subject_len = 0;
    2.22 +        headerblock = strstr(headerblock, header_string);
    2.23 +        if (headerblock) {
    2.24 +            const char* subj_start = "Subject: ";
    2.25 +            size_t subj_len = strlen(subj_start);
    2.26 +            headerblock = strstr(headerblock, subj_start);
    2.27 +            if (headerblock) {
    2.28 +                headerblock += subj_len;
    2.29 +                char* end_pt = strstr(headerblock, "\n");
    2.30 +                if (end_pt) {
    2.31 +                    if (end_pt != mime->mm_mime_start && *(end_pt - 1) == '\r')
    2.32 +                        end_pt--;
    2.33 +                    subject_len = end_pt - headerblock;
    2.34 +                    char* new_subj = (char*)calloc(subject_len + 1, 1);
    2.35 +                    if (new_subj) {
    2.36 +                        strlcpy(new_subj, headerblock, subject_len + 1);
    2.37 +                        free(msg->shortmsg);
    2.38 +                        msg->shortmsg = new_subj;
    2.39 +                    }    
    2.40 +                } // if there's no endpoint, there's something wrong here so we ignore all
    2.41 +                  // This is best effort.
    2.42 +            }
    2.43 +        }
    2.44 +    }
    2.45 +    return PEP_STATUS_OK;
    2.46 +}
    2.47 +
    2.48  static PEP_STATUS interpret_MIME(
    2.49          struct mailmime *mime,
    2.50          message *msg
    2.51 @@ -1466,6 +1507,11 @@
    2.52                      if (status)
    2.53                          return status;
    2.54                  }
    2.55 +                else if (_is_text_part(content, "rfc822-headers")) {
    2.56 +                    status = interpret_protected_headers(part, msg);
    2.57 +                    if (status)
    2.58 +                        return status;
    2.59 +                }
    2.60                  else if (_is_text_part(content, "html") &&
    2.61                          msg->longmsg_formatted == NULL) {
    2.62                      status = interpret_body(part, &msg->longmsg_formatted,
    2.63 @@ -1525,6 +1571,11 @@
    2.64                  if (status)
    2.65                      return status;
    2.66              }
    2.67 +            else if (_is_text_part(content, "rfc822-headers")) {
    2.68 +                status = interpret_protected_headers(mime, msg);
    2.69 +                if (status)
    2.70 +                    return status;
    2.71 +            }
    2.72              else if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
    2.73                  status = interpret_body(mime, &msg->longmsg, NULL);
    2.74                  if (status)