src/etpan_mime.c
changeset 115 cab016770f6f
parent 114 7a006f74365d
child 116 e4d286e7b300
     1.1 --- a/src/etpan_mime.c	Sun Mar 15 13:06:38 2015 +0100
     1.2 +++ b/src/etpan_mime.c	Sun Mar 15 14:21:29 2015 +0100
     1.3 @@ -18,6 +18,7 @@
     1.4      time_t now;
     1.5      char name[MAX_MESSAGE_ID];
     1.6      long value;
     1.7 +    int r;
     1.8   
     1.9      id[MAX_MESSAGE_ID - 1] = 0;
    1.10      name[MAX_MESSAGE_ID - 1] = 0;
    1.11 @@ -25,7 +26,9 @@
    1.12      now = time(NULL);
    1.13  
    1.14      value = random();
    1.15 -    gethostname(name, MAX_MESSAGE_ID - 1);
    1.16 +    r = gethostname(name, MAX_MESSAGE_ID - 1);
    1.17 +    if (r == -1)
    1.18 +        return NULL;
    1.19      
    1.20      if (boundary_prefix == NULL)
    1.21          boundary_prefix = "";
    1.22 @@ -44,9 +47,15 @@
    1.23      )
    1.24  {
    1.25  	struct mailmime * build_info;
    1.26 -	clist * list;
    1.27 +	clist * list = NULL;
    1.28  	int r;
    1.29  	int mime_type;
    1.30 +    char * attr_name = NULL;
    1.31 +    char * attr_value = NULL;
    1.32 +    struct mailmime_parameter * param = NULL;
    1.33 +    clist * parameters = NULL;
    1.34 +    char * boundary = NULL;
    1.35 +
    1.36  
    1.37  	list = NULL;
    1.38  
    1.39 @@ -56,108 +65,130 @@
    1.40  	else {
    1.41  		switch (content->ct_type->tp_type) {
    1.42  			case MAILMIME_TYPE_DISCRETE_TYPE:
    1.43 -			mime_type = MAILMIME_SINGLE;
    1.44 -			break;
    1.45 +                mime_type = MAILMIME_SINGLE;
    1.46 +                break;
    1.47  
    1.48  			case MAILMIME_TYPE_COMPOSITE_TYPE:
    1.49 -			switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
    1.50 -				case MAILMIME_COMPOSITE_TYPE_MULTIPART:
    1.51 -				mime_type = MAILMIME_MULTIPLE;
    1.52 -				break;
    1.53 +                switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
    1.54 +                    case MAILMIME_COMPOSITE_TYPE_MULTIPART:
    1.55 +                        mime_type = MAILMIME_MULTIPLE;
    1.56 +                        break;
    1.57  
    1.58 -				case MAILMIME_COMPOSITE_TYPE_MESSAGE:
    1.59 -				if (strcasecmp(content->ct_subtype, "rfc822") == 0)
    1.60 -					mime_type = MAILMIME_MESSAGE;
    1.61 -				else
    1.62 -					mime_type = MAILMIME_SINGLE;
    1.63 -				break;
    1.64 +                    case MAILMIME_COMPOSITE_TYPE_MESSAGE:
    1.65 +                        if (strcasecmp(content->ct_subtype, "rfc822") == 0)
    1.66 +                            mime_type = MAILMIME_MESSAGE;
    1.67 +                        else
    1.68 +                            mime_type = MAILMIME_SINGLE;
    1.69 +                        break;
    1.70  
    1.71 -				default:
    1.72 -				goto err;
    1.73 -			}
    1.74 -			break;
    1.75 +                    default:
    1.76 +                        goto enomem;
    1.77 +                }
    1.78 +                break;
    1.79  
    1.80  			default:
    1.81 -			goto err;
    1.82 +                goto enomem;
    1.83  		}
    1.84  	}
    1.85  
    1.86  	if (mime_type == MAILMIME_MULTIPLE) {
    1.87 -		char * attr_name;
    1.88 -		char * attr_value;
    1.89 -		struct mailmime_parameter * param;
    1.90 -		clist * parameters;
    1.91 -		char * boundary;
    1.92 -
    1.93  		list = clist_new();
    1.94 +        assert(list);
    1.95  		if (list == NULL)
    1.96 -			goto err;
    1.97 +			goto enomem;
    1.98  
    1.99  		attr_name = strdup("boundary");
   1.100 +        assert(attr_name);
   1.101 +        if (attr_name == NULL)
   1.102 +            goto enomem;
   1.103 +
   1.104  		boundary = generate_boundary(boundary_prefix);
   1.105 +        assert(boundary);
   1.106  		attr_value = boundary;
   1.107 -		if (attr_name == NULL) {
   1.108 -			free(attr_name);
   1.109 -			goto free_list;
   1.110 -		}
   1.111 +		if (attr_value == NULL)
   1.112 +			goto enomem;
   1.113  
   1.114  		param = mailmime_parameter_new(attr_name, attr_value);
   1.115 -		if (param == NULL) {
   1.116 -			free(attr_value);
   1.117 -			free(attr_name);
   1.118 -			goto free_list;
   1.119 -		}
   1.120 +        assert(param);
   1.121 +		if (param == NULL)
   1.122 +			goto enomem;
   1.123 +        attr_name = NULL;
   1.124 +        attr_value = NULL;
   1.125  
   1.126  		if (content->ct_parameters == NULL) {
   1.127  			parameters = clist_new();
   1.128 -			if (parameters == NULL) {
   1.129 -				mailmime_parameter_free(param);
   1.130 -				goto free_list;
   1.131 -			}
   1.132 +            assert(parameters);
   1.133 +			if (parameters == NULL)
   1.134 +				goto enomem;
   1.135  		}
   1.136 -		else
   1.137 +		else {
   1.138  			parameters = content->ct_parameters;
   1.139 +        }
   1.140  
   1.141  		r = clist_append(parameters, param);
   1.142 -		if (r) {
   1.143 -			clist_free(parameters);
   1.144 -			mailmime_parameter_free(param);
   1.145 -			goto free_list;
   1.146 -		}
   1.147 +		if (r)
   1.148 +			goto enomem;
   1.149 +        param = NULL;
   1.150  
   1.151  		if (content->ct_parameters == NULL)
   1.152  			content->ct_parameters = parameters;
   1.153  	}
   1.154  
   1.155 -	build_info = mailmime_new(mime_type,
   1.156 -		NULL, 0, mime_fields, content,
   1.157 -		NULL, NULL, NULL, list,
   1.158 -		NULL, NULL);
   1.159 -	if (build_info == NULL) {
   1.160 -		clist_free(list);
   1.161 -		return NULL;
   1.162 -	}
   1.163 +    build_info = mailmime_new(mime_type, NULL, 0, mime_fields, content, NULL,
   1.164 +            NULL, NULL, list, NULL, NULL);
   1.165 +	if (build_info == NULL)
   1.166 +		goto enomem;
   1.167  
   1.168  	return build_info;
   1.169  
   1.170 -	free_list:
   1.171 -	clist_free(list);
   1.172 -	err:
   1.173 +enomem:
   1.174 +    if (list)
   1.175 +        clist_free(list);
   1.176 +    free(attr_name);
   1.177 +    free(attr_value);
   1.178 +    if (content->ct_parameters == NULL)
   1.179 +        if (parameters)
   1.180 +            clist_free(parameters);
   1.181 +
   1.182  	return NULL;
   1.183  }
   1.184  
   1.185  struct mailmime * get_pgp_encrypted_part(void)
   1.186  {
   1.187 -	struct mailmime * mime;
   1.188 -	struct mailmime_fields * mime_fields;
   1.189 -	struct mailmime_content * content;
   1.190 +	struct mailmime * mime = NULL;
   1.191 +	struct mailmime_fields * mime_fields = NULL;
   1.192 +	struct mailmime_content * content = NULL;
   1.193 +    int r;
   1.194  
   1.195  	content = mailmime_content_new_with_str("application/pgp-encrypted");
   1.196 +    if (content == NULL)
   1.197 +        goto enomem;
   1.198 +
   1.199      mime_fields = mailmime_fields_new_empty();
   1.200 +    if (mime_fields == NULL)
   1.201 +        goto enomem;
   1.202 +
   1.203  	mime = part_new_empty(content, mime_fields, NULL, 1);
   1.204 -    mailmime_set_body_text(mime, "Version: 1\n", 10);
   1.205 +    if (mime == NULL)
   1.206 +        goto enomem;
   1.207 +    mime_fields = NULL;
   1.208 +    content = NULL;
   1.209 +
   1.210 +    r = mailmime_set_body_text(mime, "Version: 1\n", 10);
   1.211 +    if (r != 0)
   1.212 +        goto enomem;
   1.213  
   1.214  	return mime;
   1.215 +
   1.216 +enomem:
   1.217 +    if (content)
   1.218 +        mailmime_content_free(content);
   1.219 +    if (mime_fields)
   1.220 +        mailmime_fields_free(mime_fields);
   1.221 +    if (mime)
   1.222 +        mailmime_free(mime);
   1.223 +
   1.224 +    return NULL;
   1.225  }
   1.226  
   1.227  struct mailmime * get_text_part(
   1.228 @@ -169,38 +200,81 @@
   1.229      )
   1.230  {
   1.231      char * disposition_name = NULL;
   1.232 -	struct mailmime_fields * mime_fields;
   1.233 -	struct mailmime * mime;
   1.234 -	struct mailmime_content * content;
   1.235 -	struct mailmime_parameter * param;
   1.236 -	struct mailmime_disposition * disposition;
   1.237 +	struct mailmime_fields * mime_fields = NULL;
   1.238 +	struct mailmime * mime = NULL;
   1.239 +	struct mailmime_content * content = NULL;
   1.240 +	struct mailmime_parameter * param = NULL;
   1.241 +	struct mailmime_disposition * disposition = NULL;
   1.242  	struct mailmime_mechanism * encoding = NULL;
   1.243 +    int r;
   1.244      
   1.245 -    if (filename != NULL)
   1.246 +    if (filename != NULL) {
   1.247          disposition_name = strdup(filename);
   1.248 +        if (disposition_name == NULL)
   1.249 +            goto enomem;
   1.250 +    }
   1.251  
   1.252 -    if (encoding_type)
   1.253 +    if (encoding_type) {
   1.254          encoding = mailmime_mechanism_new(encoding_type, NULL);
   1.255 +        if (encoding == NULL)
   1.256 +            goto enomem;
   1.257 +    }
   1.258  
   1.259 -	disposition = mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
   1.260 -		disposition_name, NULL, NULL, NULL, (size_t) -1);
   1.261 +    disposition =
   1.262 +            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
   1.263 +                    disposition_name, NULL, NULL, NULL, (size_t) -1);
   1.264 +    if (disposition == NULL)
   1.265 +        goto enomem;
   1.266 +    disposition_name = NULL;
   1.267  
   1.268 -	mime_fields = mailmime_fields_new_with_data(encoding,
   1.269 -		NULL, NULL, disposition, NULL);
   1.270 +    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
   1.271 +            disposition, NULL);
   1.272 +    if (mime_fields == NULL)
   1.273 +        goto enomem;
   1.274 +    encoding = NULL;
   1.275 +    disposition = NULL;
   1.276  
   1.277  	content = mailmime_content_new_with_str(mime_type);
   1.278 +    if (content == NULL)
   1.279 +        goto enomem;
   1.280      
   1.281      if (encoding_type != MAILMIME_MECHANISM_7BIT) {
   1.282          param = mailmime_param_new_with_data("charset", "utf-8");
   1.283 -        clist_append(content->ct_parameters, param);
   1.284 +        r = clist_append(content->ct_parameters, param);
   1.285 +        if (r != 0)
   1.286 +            goto enomem;
   1.287      }
   1.288  
   1.289  	mime = part_new_empty(content, mime_fields, NULL, 1);
   1.290 +    if (mime == NULL)
   1.291 +        goto enomem;
   1.292 +    content = NULL;
   1.293 +    mime_fields = NULL;
   1.294  
   1.295 -    if (text)
   1.296 -        mailmime_set_body_text(mime, (char *) text, length);
   1.297 +    if (text) {
   1.298 +        r = mailmime_set_body_text(mime, (char *) text, length);
   1.299 +        if (r != 0)
   1.300 +            goto enomem;
   1.301 +    }
   1.302  	
   1.303  	return mime;
   1.304 +
   1.305 +enomem:
   1.306 +    free(disposition_name);
   1.307 +    if (mime_fields)
   1.308 +        mailmime_fields_free(mime_fields);
   1.309 +    if (mime)
   1.310 +        mailmime_free(mime);
   1.311 +    if (content)
   1.312 +        mailmime_content_free(content);
   1.313 +    if (param)
   1.314 +        mailmime_parameter_free(param);
   1.315 +    if (disposition)
   1.316 +        mailmime_disposition_free(disposition);
   1.317 +    if (encoding)
   1.318 +        mailmime_mechanism_free(encoding);
   1.319 +
   1.320 +    return NULL;
   1.321  }
   1.322  
   1.323  struct mailmime * get_file_part(
   1.324 @@ -210,31 +284,70 @@
   1.325          size_t length
   1.326      )
   1.327  {
   1.328 -    char * disposition_name;
   1.329 +    char * disposition_name = NULL;
   1.330      int encoding_type;
   1.331 -    struct mailmime_disposition * disposition;
   1.332 -    struct mailmime_mechanism * encoding;
   1.333 -    struct mailmime_content * content;
   1.334 -    struct mailmime * mime;
   1.335 -    struct mailmime_fields * mime_fields;
   1.336 +    struct mailmime_disposition * disposition = NULL;
   1.337 +    struct mailmime_mechanism * encoding = NULL;
   1.338 +    struct mailmime_content * content = NULL;
   1.339 +    struct mailmime * mime = NULL;
   1.340 +    struct mailmime_fields * mime_fields = NULL;
   1.341 +    int r;
   1.342  
   1.343 -    disposition_name = NULL;
   1.344      if (filename != NULL) {
   1.345          disposition_name = strdup(filename);
   1.346 +        if (disposition_name == NULL)
   1.347 +            goto enomem;
   1.348      }
   1.349 +
   1.350      disposition =
   1.351 -        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
   1.352 -                disposition_name, NULL, NULL, NULL, (size_t) -1);
   1.353 +            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
   1.354 +                    disposition_name, NULL, NULL, NULL, (size_t) -1);
   1.355 +    if (disposition == NULL)
   1.356 +        goto enomem;
   1.357 +    disposition_name = NULL;
   1.358 +
   1.359      content = mailmime_content_new_with_str(mime_type);
   1.360 +    if (content == NULL)
   1.361 +        goto enomem;
   1.362  
   1.363      encoding_type = MAILMIME_MECHANISM_BASE64;
   1.364      encoding = mailmime_mechanism_new(encoding_type, NULL);
   1.365 -    mime_fields = mailmime_fields_new_with_data(encoding,
   1.366 -        NULL, NULL, disposition, NULL);
   1.367 +    if (encoding == NULL)
   1.368 +        goto enomem;
   1.369 +
   1.370 +    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
   1.371 +            disposition, NULL);
   1.372 +    if (mime_fields == NULL)
   1.373 +        goto enomem;
   1.374 +    encoding = NULL;
   1.375 +    disposition = NULL;
   1.376 +
   1.377      mime = part_new_empty(content, mime_fields, NULL, 1);
   1.378 -    mailmime_set_body_text(mime, data, length);
   1.379 +    if (mime == NULL)
   1.380 +        goto enomem;
   1.381 +    content = NULL;
   1.382 +    mime_fields = NULL;
   1.383 +
   1.384 +    r = mailmime_set_body_text(mime, data, length);
   1.385 +    if (r != 0)
   1.386 +        goto enomem;
   1.387  
   1.388      return mime;
   1.389 +
   1.390 +enomem:
   1.391 +    free(disposition_name);
   1.392 +    if (disposition)
   1.393 +        mailmime_disposition_free(disposition);
   1.394 +    if (encoding)
   1.395 +        mailmime_mechanism_free(encoding);
   1.396 +    if (content)
   1.397 +        mailmime_content_free(content);
   1.398 +    if (mime_fields)
   1.399 +        mailmime_fields_free(mime_fields);
   1.400 +    if (mime)
   1.401 +        mailmime_free(mime);
   1.402 +
   1.403 +    return NULL;
   1.404  }
   1.405  
   1.406  struct mailmime * part_multiple_new(
   1.407 @@ -242,29 +355,30 @@
   1.408          const char * boundary_prefix
   1.409      )
   1.410  {
   1.411 -    struct mailmime_fields * mime_fields;
   1.412 -    struct mailmime_content * content;
   1.413 -    struct mailmime * mp;
   1.414 +    struct mailmime_fields *mime_fields = NULL;
   1.415 +    struct mailmime_content *content = NULL;
   1.416 +    struct mailmime *mp = NULL;
   1.417      
   1.418      mime_fields = mailmime_fields_new_empty();
   1.419      if (mime_fields == NULL)
   1.420 -        goto err;
   1.421 +        goto enomem;
   1.422      
   1.423      content = mailmime_content_new_with_str(type);
   1.424      if (content == NULL)
   1.425 -        goto free_fields;
   1.426 +        goto enomem;
   1.427      
   1.428      mp = part_new_empty(content, mime_fields, boundary_prefix, 0);
   1.429      if (mp == NULL)
   1.430 -        goto free_content;
   1.431 +        goto enomem;
   1.432      
   1.433      return mp;
   1.434      
   1.435 -free_content:
   1.436 -    mailmime_content_free(content);
   1.437 -free_fields:
   1.438 -    mailmime_fields_free(mime_fields);
   1.439 -err:
   1.440 +enomem:
   1.441 +    if (content)
   1.442 +        mailmime_content_free(content);
   1.443 +    if (mime_fields)
   1.444 +        mailmime_fields_free(mime_fields);
   1.445 +
   1.446      return NULL;
   1.447  }
   1.448