merge "default" into "keysync" branch keysync
authorRoker <roker@pep-project.org>
Tue, 20 Sep 2016 16:14:41 +0200
branchkeysync
changeset 11697d666711da67
parent 1167 781218f0c0d0
parent 1164 51db748bdf16
child 1171 da88e97231f9
merge "default" into "keysync" branch
src/keymanagement.c
src/mime.c
     1.1 --- a/src/keymanagement.c	Tue Sep 20 11:46:59 2016 +0200
     1.2 +++ b/src/keymanagement.c	Tue Sep 20 16:14:41 2016 +0200
     1.3 @@ -126,6 +126,7 @@
     1.4      }
     1.5  
     1.6      int _no_user_id = EMPTYSTR(identity->user_id);
     1.7 +    int _did_elect_new_key = 0;
     1.8  
     1.9      if (_no_user_id)
    1.10      {
    1.11 @@ -182,6 +183,8 @@
    1.12                  PEP_STATUS status = elect_pubkey(session, identity);
    1.13                  if (status != PEP_STATUS_OK)
    1.14                      goto exit_free;
    1.15 +
    1.16 +                _did_elect_new_key = 1;
    1.17              }
    1.18              else {
    1.19                  identity->comm_type = stored_identity->comm_type;
    1.20 @@ -255,7 +258,7 @@
    1.21  
    1.22          // Identity doesn't get stored if call was just about checking existing
    1.23          // user by address (i.e. no user id given but already stored)
    1.24 -        if (!(_no_user_id && stored_identity))
    1.25 +        if (!(_no_user_id && stored_identity) || _did_elect_new_key)
    1.26          {
    1.27              status = set_identity(session, identity);
    1.28              assert(status == PEP_STATUS_OK);
     2.1 --- a/src/mime.c	Tue Sep 20 11:46:59 2016 +0200
     2.2 +++ b/src/mime.c	Tue Sep 20 16:14:41 2016 +0200
     2.3 @@ -40,112 +40,40 @@
     2.4  #define PATH_SEP '/'
     2.5  #endif
     2.6  
     2.7 +// This function was rewritten to use in-memory buffers instead of
     2.8 +// temporary files when the pgp/mime support was implemented for
     2.9 +// outlook, as the existing code did not work well on windows.
    2.10 +
    2.11  static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
    2.12  {
    2.13      PEP_STATUS status = PEP_STATUS_OK;
    2.14 -    int fd = -1;
    2.15 -    FILE *file = NULL;
    2.16 -    size_t size;
    2.17 -    char *buf = NULL;
    2.18      int col;
    2.19      int r;
    2.20 +	size_t len;
    2.21 +	char* buf = NULL;
    2.22  
    2.23 -    char *template = NULL;
    2.24 -    char *env_tmp = getenv("TEMP");
    2.25 +	MMAPString* buffer;
    2.26  
    2.27 -    if(env_tmp){
    2.28 -        unsigned long tmp_l = strlen(env_tmp);
    2.29 -        if(tmp_l == 0 ) {
    2.30 -            goto err_file;
    2.31 -        } else {
    2.32 -            int need_sep = (env_tmp[tmp_l-1] != PATH_SEP);
    2.33 -            template = calloc(1, tmp_l + 
    2.34 -                                 (need_sep ? 1 : 0) +
    2.35 -                                 sizeof(TMP_TEMPLATE));
    2.36 -            if (template == NULL)
    2.37 -                goto enomem;
    2.38 +	buffer = mmap_string_new(NULL);
    2.39 +	if (buffer == NULL)
    2.40 +		goto enomem;
    2.41 +	
    2.42 +	col = 0;
    2.43 +	r = mailmime_write_mem(buffer, &col, mime);
    2.44 +	assert(r == MAILIMF_NO_ERROR);
    2.45 +	if (r == MAILIMF_ERROR_MEMORY)
    2.46 +		goto enomem;
    2.47 +	else if (r != MAILIMF_NO_ERROR)
    2.48 +		goto err_file;
    2.49  
    2.50 -            memcpy(template, env_tmp, tmp_l);
    2.51 -            if(need_sep)
    2.52 -                template[tmp_l] = PATH_SEP;
    2.53 -            memcpy(template + tmp_l + (need_sep ? 1 : 0), TMP_TEMPLATE, sizeof(TMP_TEMPLATE));
    2.54 -        }
    2.55 -    }else{
    2.56 -        template = strdup("/tmp/" TMP_TEMPLATE);
    2.57 -        if (template == NULL)
    2.58 -            goto enomem;
    2.59 -    }
    2.60 -    assert(template);
    2.61 +	// we overallocate by 1 byte, so we have a terminating 0.
    2.62 +	len = buffer->len;
    2.63 +	buf = calloc(len + 1, 1);
    2.64 +	if (buf == NULL)
    2.65 +		goto enomem;
    2.66  
    2.67 -    *mimetext = NULL;
    2.68 -
    2.69 -    fd = Mkstemp(template);
    2.70 -    assert(fd != -1);
    2.71 -    if (fd == -1)
    2.72 -        goto err_file;
    2.73 -
    2.74 -    r = unlink(template);
    2.75 -    assert(r == 0);
    2.76 -    if (r)
    2.77 -        goto err_file;
    2.78 -
    2.79 -    free(template);
    2.80 -    template = NULL;
    2.81 -
    2.82 -    file = Fdopen(fd, "w+");
    2.83 -    assert(file);
    2.84 -    if (file == NULL) {
    2.85 -        switch (errno) {
    2.86 -            case ENOMEM:
    2.87 -                goto enomem;
    2.88 -            default:
    2.89 -                goto err_file;
    2.90 -        }
    2.91 -    }
    2.92 -
    2.93 -    fd = -1;
    2.94 -
    2.95 -    col = 0;
    2.96 -    r = mailmime_write_file(file, &col, mime);
    2.97 -    assert(r == MAILIMF_NO_ERROR);
    2.98 -    if (r == MAILIMF_ERROR_MEMORY)
    2.99 -        goto enomem;
   2.100 -    else if (r != MAILIMF_NO_ERROR)
   2.101 -        goto err_file;
   2.102 -
   2.103 -    off_t len = ftello(file);
   2.104 -    assert(len != -1);
   2.105 -    if (len == -1 && errno == EOVERFLOW)
   2.106 -        goto err_file;
   2.107 -
   2.108 -    if (len + 1 > SIZE_MAX)
   2.109 -        goto err_buffer;
   2.110 -
   2.111 -    size = (size_t) len;
   2.112 -
   2.113 -    errno = 0;
   2.114 -    rewind(file);
   2.115 -    assert(errno == 0);
   2.116 -    switch (errno) {
   2.117 -        case 0:
   2.118 -            break;
   2.119 -        case ENOMEM:
   2.120 -            goto enomem;
   2.121 -        default:
   2.122 -            goto err_file;
   2.123 -    }
   2.124 -
   2.125 -    buf = calloc(1, size + 1);
   2.126 -    assert(buf);
   2.127 -    if (buf == NULL)
   2.128 -        goto enomem;
   2.129 - 
   2.130 -    size_t _read;
   2.131 -    _read = Fread(buf, size, 1, file);
   2.132 -    assert(_read == size);
   2.133 -
   2.134 -    r = Fclose(file);
   2.135 -    assert(r == 0);
   2.136 +	memcpy(buf, buffer->str, len);
   2.137 +	mmap_string_free(buffer);
   2.138  
   2.139      *mimetext = buf;
   2.140      return PEP_STATUS_OK;
   2.141 @@ -162,18 +90,10 @@
   2.142      status = PEP_OUT_OF_MEMORY;
   2.143  
   2.144  pep_error:
   2.145 -    free(buf);
   2.146 -    free(template);
   2.147 -
   2.148 -    if (file) {
   2.149 -        r = Fclose(file);
   2.150 -        assert(r == 0);
   2.151 -    }
   2.152 -    else if (fd != -1) {
   2.153 -        r = Close(fd);
   2.154 -        assert(r == 0);
   2.155 -    }
   2.156 -
   2.157 +	if (buffer)
   2.158 +		mmap_string_free(buffer);
   2.159 +	if (buf)
   2.160 +		free(buf);
   2.161      return status;
   2.162  }
   2.163  
   2.164 @@ -258,7 +178,11 @@
   2.165  
   2.166      *result = NULL;
   2.167  
   2.168 -    if (blob->mime_type == NULL || blob->mime_type[0] == 0)
   2.169 +// TODO: It seems the pep com server adapter sends an empty string here,
   2.170 +// which leads to a crash later. Thus, we workaround here by treating an
   2.171 +// empty string as NULL. We need to check whether the bug really is here,
   2.172 +// or the pep com server adapter needs to be changed.
   2.173 +    if (blob->mime_type == NULL || blob->mime_type[0] == '\0')
   2.174          mime_type = "application/octet-stream";
   2.175      else
   2.176          mime_type = blob->mime_type;