| 1 |
/* Mixmaster version 2.9 -- (C) 1999 - 2002 Anonymizer Inc. and others.
|
| 2 |
|
| 3 |
Mixmaster may be redistributed and modified under certain conditions.
|
| 4 |
This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
|
| 5 |
ANY KIND, either express or implied. See the file COPYRIGHT for
|
| 6 |
details.
|
| 7 |
|
| 8 |
MIME functions
|
| 9 |
$Id: mime.c,v 1.3.2.2 2002/12/16 22:38:07 rabbi Exp $ */
|
| 10 |
|
| 11 |
|
| 12 |
#include "mix3.h"
|
| 13 |
#include <ctype.h>
|
| 14 |
#include <string.h>
|
| 15 |
|
| 16 |
#define hex(i) (isdigit(i) ? (i) - '0' : tolower(i) - 'a' + 10)
|
| 17 |
|
| 18 |
#define hexdigit(i) ((byte)(i < 10 ? i + '0' : i - 10 + 'A'))
|
| 19 |
|
| 20 |
static void encode_word(BUFFER *in)
|
| 21 |
{
|
| 22 |
BUFFER *out;
|
| 23 |
int i;
|
| 24 |
|
| 25 |
out = buf_new();
|
| 26 |
for (i = 0; i < in->length; i++)
|
| 27 |
if (in->data[i] < 32 || in->data[i] >= 127 || in->data[i] == '='
|
| 28 |
|| in->data[i] == '?' || in->data[i] == '_') {
|
| 29 |
buf_appendc(out, '=');
|
| 30 |
buf_appendc(out, hexdigit(in->data[i] / 16));
|
| 31 |
buf_appendc(out, hexdigit(in->data[i] % 16));
|
| 32 |
} else if (in->data[i] == ' ')
|
| 33 |
buf_appendc(out, '_');
|
| 34 |
else
|
| 35 |
buf_appendc(out, in->data[i]);
|
| 36 |
buf_move(in, out);
|
| 37 |
buf_free(out);
|
| 38 |
}
|
| 39 |
|
| 40 |
void body_encode(BUFFER *in, int transport, BUFFER *hdr)
|
| 41 |
{
|
| 42 |
BUFFER *out;
|
| 43 |
int c, l=0, encoding = 0;
|
| 44 |
out = buf_new();
|
| 45 |
|
| 46 |
buf_clear(hdr);
|
| 47 |
|
| 48 |
l = in->ptr;
|
| 49 |
while ((c = buf_getc(in)) != -1 && encoding != 2) {
|
| 50 |
if (c >= 160)
|
| 51 |
encoding = 1;
|
| 52 |
else if (c == ' ') {
|
| 53 |
if (buf_getc(in) == '\n')
|
| 54 |
encoding = 1;
|
| 55 |
buf_ungetc(in);
|
| 56 |
} else if ((c < 32 && c != ' ' && c != '\n' && c != '\t') ||
|
| 57 |
(c >= 127 && c < 160)) {
|
| 58 |
encoding = 2;
|
| 59 |
}
|
| 60 |
}
|
| 61 |
in->ptr = l;
|
| 62 |
|
| 63 |
if (encoding == 2) {
|
| 64 |
buf_sets(hdr, "Content-Transfer-Encoding: base64\n");
|
| 65 |
encode(in, 76);
|
| 66 |
} else {
|
| 67 |
|
| 68 |
#if 0
|
| 69 |
if (encoding == 0)
|
| 70 |
buf_sets(hdr, "Content-Transfer-Encoding: 7bit\n");
|
| 71 |
#endif
|
| 72 |
if (encoding != 0 && transport == MIME_8BIT)
|
| 73 |
buf_sets(hdr, "Content-Transfer-Encoding: 8bit\n");
|
| 74 |
if (encoding == 0 || transport == MIME_8BIT) {
|
| 75 |
buf_rest(out, in); /* transparent */
|
| 76 |
buf_move(in, out);
|
| 77 |
} else {
|
| 78 |
buf_sets(hdr, "Content-Transfer-Encoding: quoted-printable\n");
|
| 79 |
l = 0;
|
| 80 |
while ((c = buf_getc(in)) != -1) {
|
| 81 |
if (c == '\n') {
|
| 82 |
buf_nl(out);
|
| 83 |
l = 0;
|
| 84 |
}
|
| 85 |
else if (c < 32 || c >= 127 || c == '=') {
|
| 86 |
if (l > 73) {
|
| 87 |
buf_appends(out, "=\n");
|
| 88 |
l = 0;
|
| 89 |
}
|
| 90 |
buf_appendc(out, '=');
|
| 91 |
buf_appendc(out, hexdigit(c / 16));
|
| 92 |
buf_appendc(out, hexdigit(c % 16));
|
| 93 |
l += 3;
|
| 94 |
} else if (c == ' ') {
|
| 95 |
if (buf_getc(in) == '\n') {
|
| 96 |
buf_appendc(out, '=');
|
| 97 |
buf_appendc(out, hexdigit(c / 16));
|
| 98 |
buf_appendc(out, hexdigit(c % 16));
|
| 99 |
buf_nl(out);
|
| 100 |
l = 0;
|
| 101 |
} else {
|
| 102 |
buf_appendc(out, c);
|
| 103 |
buf_ungetc(in);
|
| 104 |
l++;
|
| 105 |
}
|
| 106 |
} else {
|
| 107 |
buf_appendc(out, c);
|
| 108 |
l++;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
buf_move(in, out);
|
| 112 |
}
|
| 113 |
}
|
| 114 |
buf_free(out);
|
| 115 |
}
|
| 116 |
|
| 117 |
int mail_encode(BUFFER *in, int encoding)
|
| 118 |
{
|
| 119 |
BUFFER *out, *line, *tmp;
|
| 120 |
|
| 121 |
out = buf_new();
|
| 122 |
line = buf_new();
|
| 123 |
tmp = buf_new();
|
| 124 |
|
| 125 |
while (buf_getline(in, line) == 0) {
|
| 126 |
hdr_encode(line, 255);
|
| 127 |
buf_cat(out, line);
|
| 128 |
buf_nl(out);
|
| 129 |
}
|
| 130 |
if (in->ptr < in->length) {
|
| 131 |
/* no newline if only encoding header lines */
|
| 132 |
if (encoding == 0) {
|
| 133 |
buf_nl(out);
|
| 134 |
buf_rest(out, in);
|
| 135 |
}
|
| 136 |
else {
|
| 137 |
body_encode(in, encoding, line);
|
| 138 |
buf_cat(out, line);
|
| 139 |
buf_nl(out);
|
| 140 |
buf_cat(out, in);
|
| 141 |
}
|
| 142 |
}
|
| 143 |
buf_move(in, out);
|
| 144 |
buf_free(line);
|
| 145 |
buf_free(tmp);
|
| 146 |
buf_free(out);
|
| 147 |
return (0);
|
| 148 |
}
|
| 149 |
|
| 150 |
int hdr_encode(BUFFER *in, int n)
|
| 151 |
{
|
| 152 |
int i;
|
| 153 |
int encodeword = 0, encode = 0;
|
| 154 |
BUFFER *out, *word, *space;
|
| 155 |
|
| 156 |
out = buf_new();
|
| 157 |
word = buf_new();
|
| 158 |
space = buf_new();
|
| 159 |
for (i = 0; i <= in->length; i++) {
|
| 160 |
if (isspace(in->data[i]) || in->data[i] == '\0') {
|
| 161 |
if (word->length) {
|
| 162 |
if (encodeword) {
|
| 163 |
if (encode == 0) {
|
| 164 |
buf_cat(out, space);
|
| 165 |
buf_clear(space);
|
| 166 |
buf_appends(out, "=?");
|
| 167 |
buf_appends(out, MIMECHARSET);
|
| 168 |
buf_appends(out, "?Q?");
|
| 169 |
encode = 1;
|
| 170 |
} else {
|
| 171 |
buf_cat(space, word);
|
| 172 |
buf_move(word, space);
|
| 173 |
}
|
| 174 |
encode_word(word);
|
| 175 |
}
|
| 176 |
if (encode && !encodeword) {
|
| 177 |
encode = 0;
|
| 178 |
buf_appends(out, "?=");
|
| 179 |
}
|
| 180 |
buf_cat(out, space);
|
| 181 |
buf_cat(out, word);
|
| 182 |
encodeword = 0;
|
| 183 |
buf_clear(space);
|
| 184 |
buf_clear(word);
|
| 185 |
}
|
| 186 |
buf_appendc(space, in->data[i]);
|
| 187 |
} else {
|
| 188 |
if (in->data[i] < 32 || in->data[i] >= 127)
|
| 189 |
encodeword = 1;
|
| 190 |
buf_appendc(word, in->data[i]);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
if (encode)
|
| 194 |
buf_appends(out, "?=");
|
| 195 |
|
| 196 |
buf_move(in, out);
|
| 197 |
while (n > 0 && in->length - in->ptr > n) {
|
| 198 |
for (i = 1; i < in->length - in->ptr; i++)
|
| 199 |
if (isspace(in->data[in->length - i]))
|
| 200 |
break;
|
| 201 |
buf_get(in, out, in->length - i);
|
| 202 |
buf_appends(out, "\n\t");
|
| 203 |
}
|
| 204 |
buf_rest(out, in);
|
| 205 |
buf_move(in, out);
|
| 206 |
buf_free(out);
|
| 207 |
buf_free(space);
|
| 208 |
buf_free(word);
|
| 209 |
return (0);
|
| 210 |
}
|
| 211 |
|
| 212 |
void addprintable(BUFFER *out, int c)
|
| 213 |
{
|
| 214 |
if (c == '\n')
|
| 215 |
buf_appendc(out, (char) c);
|
| 216 |
else if (c == '\t')
|
| 217 |
buf_appends(out, " ");
|
| 218 |
else if (c == '\014')
|
| 219 |
buf_appends(out, "^L");
|
| 220 |
else if (c == '\r') ;
|
| 221 |
else if (c <= 31 || (c >= 128 && c <= 128 + 31))
|
| 222 |
buf_appendc(out, '?');
|
| 223 |
else
|
| 224 |
buf_appendc(out, (char) c);
|
| 225 |
}
|
| 226 |
|
| 227 |
void addprintablebuf(BUFFER *out, BUFFER *in)
|
| 228 |
{
|
| 229 |
int c;
|
| 230 |
|
| 231 |
while ((c = buf_getc(in)) != -1)
|
| 232 |
addprintable(out, c);
|
| 233 |
}
|
| 234 |
|
| 235 |
int decode_line(BUFFER *line)
|
| 236 |
{
|
| 237 |
BUFFER *out;
|
| 238 |
unsigned int i;
|
| 239 |
int c, softbreak = 0;
|
| 240 |
|
| 241 |
out = buf_new();
|
| 242 |
for (i = 0; line->data[i] != '\0'; i++) {
|
| 243 |
if (line->data[i] == '=') {
|
| 244 |
if (isxdigit(line->data[i + 1]) && isxdigit(line->data[i + 2])) {
|
| 245 |
c = hex(line->data[i + 1]) * 16 + hex(line->data[i + 2]);
|
| 246 |
i += 2;
|
| 247 |
addprintable(out, c);
|
| 248 |
} else if (line->data[i + 1] == '\0') {
|
| 249 |
softbreak = 1;
|
| 250 |
break;
|
| 251 |
}
|
| 252 |
} else
|
| 253 |
addprintable(out, line->data[i]);
|
| 254 |
}
|
| 255 |
|
| 256 |
buf_move(line, out);
|
| 257 |
buf_free(out);
|
| 258 |
return (softbreak);
|
| 259 |
}
|
| 260 |
|
| 261 |
int decode_header(BUFFER *in)
|
| 262 |
{
|
| 263 |
int encoded = 0;
|
| 264 |
int c;
|
| 265 |
int err = 0;
|
| 266 |
int last = 0;
|
| 267 |
BUFFER *out;
|
| 268 |
|
| 269 |
out = buf_new();
|
| 270 |
for (in->ptr = 0; in->data[in->ptr] != '\0'; in->ptr++) {
|
| 271 |
if (encoded == 'q' && in->data[in->ptr] == '=' &&
|
| 272 |
isxdigit(in->data[in->ptr + 1])
|
| 273 |
&& isxdigit(in->data[in->ptr + 2])) {
|
| 274 |
c = hex(in->data[in->ptr + 1]) * 16 + hex(in->data[in->ptr + 2]);
|
| 275 |
in->ptr += 2;
|
| 276 |
addprintable(out, c);
|
| 277 |
} else if (encoded == 'q' && in->data[in->ptr] == '_')
|
| 278 |
buf_appendc(out, ' ');
|
| 279 |
else if (in->data[in->ptr] == '=' && in->data[in->ptr + 1] == '?' &&
|
| 280 |
in->data[in->ptr + 2] != '\0') {
|
| 281 |
if (last > 0 && out->length > last) {
|
| 282 |
out->data[last] = '\0';
|
| 283 |
out->length = last;
|
| 284 |
}
|
| 285 |
in->ptr++;
|
| 286 |
while (in->data[++in->ptr] != '?')
|
| 287 |
if (in->data[in->ptr] == 0) {
|
| 288 |
err = -1;
|
| 289 |
goto end;
|
| 290 |
}
|
| 291 |
if (in->data[in->ptr + 1] != '\0' && in->data[in->ptr + 2] == '?') {
|
| 292 |
encoded = tolower(in->data[in->ptr + 1]);
|
| 293 |
in->ptr += 2;
|
| 294 |
if (encoded == 'b') {
|
| 295 |
BUFFER *tmp;
|
| 296 |
|
| 297 |
tmp = buf_new();
|
| 298 |
decode(in, tmp);
|
| 299 |
addprintablebuf(out, tmp);
|
| 300 |
last = out->length;
|
| 301 |
buf_free(tmp);
|
| 302 |
} else if (encoded != 'q')
|
| 303 |
err = 1;
|
| 304 |
} else {
|
| 305 |
err = -1;
|
| 306 |
goto end;
|
| 307 |
}
|
| 308 |
} else if (encoded && in->data[in->ptr] == '?' &&
|
| 309 |
in->data[in->ptr + 1] == '=') {
|
| 310 |
in->ptr++;
|
| 311 |
last = out->length;
|
| 312 |
encoded = 0;
|
| 313 |
} else {
|
| 314 |
addprintable(out, in->data[in->ptr]);
|
| 315 |
if (!encoded || !isspace(in->data[in->ptr]))
|
| 316 |
last = out->length;
|
| 317 |
}
|
| 318 |
}
|
| 319 |
end:
|
| 320 |
if (err == -1)
|
| 321 |
buf_set(out, in);
|
| 322 |
|
| 323 |
buf_move(in, out);
|
| 324 |
buf_free(out);
|
| 325 |
return (err);
|
| 326 |
}
|
| 327 |
|
| 328 |
#define delimclose 2
|
| 329 |
|
| 330 |
int boundary(BUFFER *line, BUFFER *boundary)
|
| 331 |
{
|
| 332 |
int c;
|
| 333 |
|
| 334 |
if (boundary->length == 0 || !bufleft(line, "--") ||
|
| 335 |
!strleft(line->data + 2, boundary->data))
|
| 336 |
return (0);
|
| 337 |
line->ptr = boundary->length + 2;
|
| 338 |
for (;;) {
|
| 339 |
c = buf_getc(line);
|
| 340 |
if (c == -1)
|
| 341 |
return (1);
|
| 342 |
if (c == '-' && buf_getc(line) == '-')
|
| 343 |
return (delimclose);
|
| 344 |
if (!isspace(c))
|
| 345 |
return (0);
|
| 346 |
}
|
| 347 |
}
|
| 348 |
|
| 349 |
#define pgpenc 1
|
| 350 |
#define pgpsig 2
|
| 351 |
|
| 352 |
/*
|
| 353 |
* decodes non-multipart quoted printable message
|
| 354 |
*/
|
| 355 |
int qp_decode_message(BUFFER *msg)
|
| 356 |
{
|
| 357 |
BUFFER *out, *line, *field, *content;
|
| 358 |
out = buf_new();
|
| 359 |
line = buf_new();
|
| 360 |
field = buf_new();
|
| 361 |
content = buf_new();
|
| 362 |
|
| 363 |
buf_rewind(msg);
|
| 364 |
|
| 365 |
/* copy over headers without decoding */
|
| 366 |
while (buf_getheader(msg, field, content) == 0) {
|
| 367 |
if (bufieq(field, "content-transfer-encoding")
|
| 368 |
&& bufieq(content, "quoted-printable")) {
|
| 369 |
continue; /* no longer quoted-printable */
|
| 370 |
} else {
|
| 371 |
buf_appendheader(out, field, content);
|
| 372 |
}
|
| 373 |
}
|
| 374 |
|
| 375 |
buf_nl(out);
|
| 376 |
|
| 377 |
/* copy over body, quoted-printable decoding as we go */
|
| 378 |
while (buf_getline(msg, line) != -1) {
|
| 379 |
int softbreak;
|
| 380 |
softbreak = decode_line(line);
|
| 381 |
buf_cat(out, line);
|
| 382 |
if (!softbreak)
|
| 383 |
buf_nl(out);
|
| 384 |
}
|
| 385 |
buf_move(msg, out);
|
| 386 |
buf_free(out);
|
| 387 |
buf_free(line);
|
| 388 |
buf_free(field);
|
| 389 |
buf_free(content);
|
| 390 |
return 0;
|
| 391 |
}
|
| 392 |
|
| 393 |
|
| 394 |
int entity_decode(BUFFER *msg, int message, int mptype, BUFFER *data)
|
| 395 |
{
|
| 396 |
BUFFER *out, *line, *field, *content, *type, *subtype, *disposition,
|
| 397 |
*mboundary, *part, *sigdata;
|
| 398 |
int ret = 0, ptype = 0, partno = 0;
|
| 399 |
int p, encoded = 0;
|
| 400 |
|
| 401 |
out = buf_new();
|
| 402 |
line = buf_new();
|
| 403 |
field = buf_new();
|
| 404 |
content = buf_new();
|
| 405 |
type = buf_new();
|
| 406 |
subtype = buf_new();
|
| 407 |
disposition = buf_new();
|
| 408 |
mboundary = buf_new();
|
| 409 |
part = buf_new();
|
| 410 |
sigdata = buf_new();
|
| 411 |
|
| 412 |
if (message && bufileft(msg, "From ")) {
|
| 413 |
buf_getline(msg, out); /* envelope from */
|
| 414 |
buf_nl(out);
|
| 415 |
}
|
| 416 |
|
| 417 |
while (buf_getheader(msg, field, content) == 0) {
|
| 418 |
if (bufieq(field, "content-transfer-encoding") &&
|
| 419 |
bufieq(content, "quoted-printable"))
|
| 420 |
encoded = 'q';
|
| 421 |
if (bufieq(field, "content-type")) {
|
| 422 |
get_type(content, type, subtype);
|
| 423 |
if (bufieq(type, "multipart"))
|
| 424 |
get_parameter(content, "boundary", mboundary);
|
| 425 |
if (bufieq(type, "multipart") && bufieq(subtype, "encrypted")) {
|
| 426 |
get_parameter(content, "protocol", line);
|
| 427 |
if (bufieq(line, "application/pgp-encrypted"))
|
| 428 |
ptype = pgpenc;
|
| 429 |
}
|
| 430 |
if (bufieq(type, "multipart") && bufieq(subtype, "signed")) {
|
| 431 |
get_parameter(content, "protocol", line);
|
| 432 |
if (bufieq(line, "application/pgp-signature"))
|
| 433 |
ptype = pgpsig;
|
| 434 |
}
|
| 435 |
}
|
| 436 |
if (bufieq(field, "content-disposition"))
|
| 437 |
buf_set(disposition, content);
|
| 438 |
if (message) {
|
| 439 |
decode_header(content);
|
| 440 |
buf_appendheader(out, field, content);
|
| 441 |
}
|
| 442 |
}
|
| 443 |
|
| 444 |
if (message)
|
| 445 |
buf_nl(out);
|
| 446 |
|
| 447 |
if (bufifind(disposition, "attachment")) {
|
| 448 |
buf_appendf(out, "[-- %b attachment", type);
|
| 449 |
get_parameter(disposition, "filename", line);
|
| 450 |
if (line->length)
|
| 451 |
buf_appendf(out, " (%b)", line);
|
| 452 |
buf_appends(out, " --]\n");
|
| 453 |
}
|
| 454 |
|
| 455 |
if (mboundary->length) {
|
| 456 |
/* multipart */
|
| 457 |
while (buf_getline(msg, line) > -1 && !boundary(line, mboundary))
|
| 458 |
; /* ignore preamble */
|
| 459 |
while (buf_getline(msg, line) != -1) {
|
| 460 |
p = boundary(line, mboundary);
|
| 461 |
if (p) {
|
| 462 |
if (part->data[part->length - 1] == '\n')
|
| 463 |
part->data[--(part->length)] = '\0';
|
| 464 |
partno++;
|
| 465 |
if (ptype == pgpsig && partno == 1)
|
| 466 |
buf_set(sigdata, part);
|
| 467 |
ret += entity_decode(part, 0, ptype, sigdata);
|
| 468 |
buf_cat(out, part);
|
| 469 |
buf_clear(part);
|
| 470 |
if (p == delimclose)
|
| 471 |
break;
|
| 472 |
if (bufieq(subtype, "alternative") && ret > 0)
|
| 473 |
break;
|
| 474 |
if (bufieq(subtype, "mixed"))
|
| 475 |
buf_appends(out,
|
| 476 |
"[-------------------------------------------------------------------------]\n");
|
| 477 |
} else {
|
| 478 |
buf_cat(part, line);
|
| 479 |
buf_nl(part);
|
| 480 |
}
|
| 481 |
}
|
| 482 |
} else if (mptype == pgpenc && bufieq(type, "application") &&
|
| 483 |
bufieq(subtype, "pgp-encrypted")) {
|
| 484 |
/* application/pgp-encrypted part of multipart/encrypted */
|
| 485 |
; /* skip */
|
| 486 |
} else if (mptype == pgpenc && bufieq(type, "application") &&
|
| 487 |
bufieq(subtype, "octet-stream")) {
|
| 488 |
/* application/octet-stream part of multipart/encrypted */
|
| 489 |
int ok = 0;
|
| 490 |
buf_getline(msg, line);
|
| 491 |
if (bufleft(line, info_beginpgp)) {
|
| 492 |
if (buffind(line, "(SIGNED)")) {
|
| 493 |
buf_getline(msg, line);
|
| 494 |
buf_appends(out, "[-- OpenPGP message with signature --]\n");
|
| 495 |
if (bufleft(line, info_pgpsig))
|
| 496 |
buf_appendf(out, "[%s]\n",
|
| 497 |
line->data + sizeof(info_pgpsig) - 1);
|
| 498 |
else
|
| 499 |
buf_appends(out, "[Signature invalid]\n");
|
| 500 |
} else
|
| 501 |
buf_appends(out, "[-- OpenPGP message --]\n");
|
| 502 |
while (buf_getline(msg, line) != -1) {
|
| 503 |
if (bufleft(line, info_endpgp)) {
|
| 504 |
ret += entity_decode(part, 0, 0, NULL);
|
| 505 |
buf_cat(out, part);
|
| 506 |
buf_appends(out, "[-- End OpenPGP message --]\n");
|
| 507 |
ok = 1, ret++;
|
| 508 |
break;
|
| 509 |
}
|
| 510 |
buf_cat(part, line);
|
| 511 |
buf_nl(part);
|
| 512 |
}
|
| 513 |
}
|
| 514 |
if (!ok) {
|
| 515 |
buf_appends(out, "[-- Bad OpenPGP message --]\n");
|
| 516 |
buf_cat(out, msg);
|
| 517 |
}
|
| 518 |
} else if (mptype == pgpsig && bufeq(type, "application") &&
|
| 519 |
bufieq(subtype, "pgp-signature")) {
|
| 520 |
buf_rest(part, msg);
|
| 521 |
if (pgp_decrypt(part, NULL, data, PGPPUBRING, NULL) == PGP_SIGOK)
|
| 522 |
buf_appendf(out, "[-- OpenPGP signature from:\n %b --]\n", data);
|
| 523 |
else
|
| 524 |
buf_appends(out, "[-- Invalid OpenPGP signature --]\n");
|
| 525 |
} else if (type->length == 0 || bufieq(type, "text")) {
|
| 526 |
while (buf_getline(msg, line) != -1) {
|
| 527 |
int softbreak;
|
| 528 |
softbreak = encoded ? decode_line(line) : 0;
|
| 529 |
buf_cat(out, line);
|
| 530 |
if (!softbreak)
|
| 531 |
buf_nl(out);
|
| 532 |
}
|
| 533 |
ret++;
|
| 534 |
} else {
|
| 535 |
buf_appendf(out, "[-- %b/%b message part --]\n", type, subtype);
|
| 536 |
buf_cat(out, msg);
|
| 537 |
}
|
| 538 |
|
| 539 |
buf_move(msg, out);
|
| 540 |
buf_free(line);
|
| 541 |
buf_free(out);
|
| 542 |
buf_free(field);
|
| 543 |
buf_free(content);
|
| 544 |
buf_free(type);
|
| 545 |
buf_free(subtype);
|
| 546 |
buf_free(disposition);
|
| 547 |
buf_free(mboundary);
|
| 548 |
buf_free(part);
|
| 549 |
buf_free(sigdata);
|
| 550 |
return (0);
|
| 551 |
}
|
| 552 |
|
| 553 |
void mimedecode(BUFFER *msg)
|
| 554 |
{
|
| 555 |
entity_decode(msg, 1, 0, NULL);
|
| 556 |
}
|
| 557 |
|
| 558 |
int attachfile(BUFFER *message, BUFFER *filename)
|
| 559 |
{
|
| 560 |
BUFFER *type, *attachment;
|
| 561 |
FILE *f;
|
| 562 |
int ret = -1;
|
| 563 |
|
| 564 |
type = buf_new();
|
| 565 |
attachment = buf_new();
|
| 566 |
|
| 567 |
if ((bufiright(filename, ".txt") || !bufifind(filename, ".")) &&(strlen(DEFLTENTITY) != 0))
|
| 568 |
buf_sets(type, DEFLTENTITY);
|
| 569 |
else if (bufiright(filename, ".htm") || bufiright(filename, ".html"))
|
| 570 |
buf_sets(type, "text/html");
|
| 571 |
else if (bufiright(filename, ".jpeg"))
|
| 572 |
buf_sets(type, "image/jpeg");
|
| 573 |
else if (bufiright(filename, ".gif"))
|
| 574 |
buf_sets(type, "image/gif");
|
| 575 |
else if (bufiright(filename, ".pcm"))
|
| 576 |
buf_sets(type, "audio/basic");
|
| 577 |
else if (bufiright(filename, ".mpg") || bufiright(filename, ".mpeg"))
|
| 578 |
buf_sets(type, "video/mpeg");
|
| 579 |
else if (bufiright(filename, ".ps"))
|
| 580 |
buf_sets(type, "application/postscript");
|
| 581 |
else
|
| 582 |
buf_sets(type, "application/octet-stream");
|
| 583 |
|
| 584 |
f = fopen(filename->data, "r");
|
| 585 |
if (f) {
|
| 586 |
buf_read(attachment, f);
|
| 587 |
fclose(f);
|
| 588 |
ret = mime_attach(message, attachment, type);
|
| 589 |
}
|
| 590 |
|
| 591 |
buf_free(attachment);
|
| 592 |
buf_free(type);
|
| 593 |
return(ret);
|
| 594 |
}
|
| 595 |
|
| 596 |
int mime_attach(BUFFER *message, BUFFER *attachment, BUFFER *attachtype)
|
| 597 |
{
|
| 598 |
BUFFER *out, *part, *line, *type, *subtype, *mboundary, *field, *content;
|
| 599 |
int mimeheader = 0, multipart = 0, versionheader = 0;
|
| 600 |
|
| 601 |
out = buf_new();
|
| 602 |
line = buf_new();
|
| 603 |
part = buf_new();
|
| 604 |
type = buf_new();
|
| 605 |
subtype = buf_new();
|
| 606 |
mboundary = buf_new();
|
| 607 |
field = buf_new();
|
| 608 |
content = buf_new();
|
| 609 |
|
| 610 |
buf_rewind(message);
|
| 611 |
while (buf_getheader(message, field, content) == 0) {
|
| 612 |
if (bufieq(field, "mime-version"))
|
| 613 |
versionheader = 1;
|
| 614 |
if (bufieq(field, "content-type")) {
|
| 615 |
get_type(content, type, subtype);
|
| 616 |
if (bufieq(type, "multipart") && bufieq(subtype, "mixed")) {
|
| 617 |
multipart = 1;
|
| 618 |
get_parameter(content, "boundary", mboundary);
|
| 619 |
}
|
| 620 |
}
|
| 621 |
if (bufileft(field, "content-"))
|
| 622 |
mimeheader = 1;
|
| 623 |
}
|
| 624 |
|
| 625 |
if (mimeheader && !multipart) {
|
| 626 |
buf_rewind(message);
|
| 627 |
while (buf_getheader(message, field, content) == 0) {
|
| 628 |
if (bufileft(field, "content-"))
|
| 629 |
buf_appendheader(part, field, content);
|
| 630 |
else
|
| 631 |
buf_appendheader(out, field, content);
|
| 632 |
}
|
| 633 |
} else {
|
| 634 |
buf_ungetc(message);
|
| 635 |
buf_append(out, message->data, message->ptr);
|
| 636 |
buf_getc(message);
|
| 637 |
}
|
| 638 |
|
| 639 |
if (!versionheader)
|
| 640 |
buf_appends(out, "MIME-Version: 1.0\n");
|
| 641 |
|
| 642 |
if (!multipart) {
|
| 643 |
buf_setrnd(mboundary, 18);
|
| 644 |
encode(mboundary, 0);
|
| 645 |
buf_appendf(out, "Content-Type: multipart/mixed; boundary=\"%b\"\n",
|
| 646 |
mboundary);
|
| 647 |
}
|
| 648 |
buf_nl(out);
|
| 649 |
|
| 650 |
if (multipart) {
|
| 651 |
while (buf_getline(message, line) != -1) {
|
| 652 |
if (boundary(line, mboundary) == delimclose)
|
| 653 |
break;
|
| 654 |
buf_cat(out, line);
|
| 655 |
buf_nl(out);
|
| 656 |
}
|
| 657 |
} else {
|
| 658 |
buf_appendf(out, "--%b\n", mboundary);
|
| 659 |
if (part->length) {
|
| 660 |
buf_cat(out, part); /* body part header */
|
| 661 |
}
|
| 662 |
else {
|
| 663 |
if (strlen(DEFLTENTITY))
|
| 664 |
buf_appendf(out, "Content-Type: %s\n", DEFLTENTITY);
|
| 665 |
}
|
| 666 |
|
| 667 |
buf_nl(out);
|
| 668 |
buf_cat(out, message);
|
| 669 |
buf_nl(out);
|
| 670 |
}
|
| 671 |
|
| 672 |
buf_appendf(out, "--%b\n", mboundary);
|
| 673 |
buf_appendf(out, "Content-Type: %b\n", attachtype);
|
| 674 |
|
| 675 |
body_encode(attachment, MIME_8BIT, line);
|
| 676 |
buf_cat(out, line);
|
| 677 |
buf_nl(out);
|
| 678 |
buf_cat(out, attachment);
|
| 679 |
buf_appendf(out, "\n--%b--\n", mboundary);
|
| 680 |
|
| 681 |
buf_move(message, out);
|
| 682 |
|
| 683 |
buf_free(out);
|
| 684 |
buf_free(line);
|
| 685 |
buf_free(part);
|
| 686 |
buf_free(type);
|
| 687 |
buf_free(subtype);
|
| 688 |
buf_free(mboundary);
|
| 689 |
buf_free(field);
|
| 690 |
buf_free(content);
|
| 691 |
return (1);
|
| 692 |
}
|
| 693 |
|
| 694 |
static int entity_encode(BUFFER *message, BUFFER *out, BUFFER *messagehdr,
|
| 695 |
int encoding)
|
| 696 |
{
|
| 697 |
BUFFER *field, *content, *mboundary, *part, *line, *line2, *tmp;
|
| 698 |
|
| 699 |
field = buf_new();
|
| 700 |
content = buf_new();
|
| 701 |
mboundary = buf_new();
|
| 702 |
part = buf_new();
|
| 703 |
line = buf_new();
|
| 704 |
line2 = buf_new();
|
| 705 |
tmp = buf_new();
|
| 706 |
|
| 707 |
buf_rewind(message);
|
| 708 |
buf_clear(out);
|
| 709 |
buf_clear(messagehdr);
|
| 710 |
|
| 711 |
while (buf_getheader(message, field, content) == 0) {
|
| 712 |
if (bufileft(field, "content-"))
|
| 713 |
buf_appendheader(out, field, content);
|
| 714 |
else if (messagehdr)
|
| 715 |
buf_appendheader(messagehdr, field, content);
|
| 716 |
|
| 717 |
if (bufieq(field, "content-type")) {
|
| 718 |
get_type(content, line, tmp);
|
| 719 |
if (bufieq(line, "multipart"))
|
| 720 |
get_parameter(content, "boundary", mboundary);
|
| 721 |
}
|
| 722 |
}
|
| 723 |
|
| 724 |
buf_nl(out);
|
| 725 |
if (mboundary->length) {
|
| 726 |
while (buf_getline(message, line) != -1) {
|
| 727 |
buf_cat(out, line);
|
| 728 |
buf_nl(out);
|
| 729 |
if (boundary(line, mboundary))
|
| 730 |
break;
|
| 731 |
}
|
| 732 |
while (buf_getline(message, line) != -1) {
|
| 733 |
if (boundary(line, mboundary)) {
|
| 734 |
entity_encode(part, tmp, line2, encoding);
|
| 735 |
buf_cat(out, line2);
|
| 736 |
buf_cat(out, tmp);
|
| 737 |
buf_cat(out, line);
|
| 738 |
buf_nl(out);
|
| 739 |
buf_clear(part);
|
| 740 |
if (boundary(line, mboundary) == delimclose)
|
| 741 |
break;
|
| 742 |
} else {
|
| 743 |
buf_cat(part, line);
|
| 744 |
buf_nl(part);
|
| 745 |
}
|
| 746 |
}
|
| 747 |
} else
|
| 748 |
buf_rest(out, message);
|
| 749 |
buf_rewind(out);
|
| 750 |
mail_encode(out, encoding);
|
| 751 |
|
| 752 |
buf_free(field);
|
| 753 |
buf_free(content);
|
| 754 |
buf_free(mboundary);
|
| 755 |
buf_free(part);
|
| 756 |
buf_free(line);
|
| 757 |
buf_free(line2);
|
| 758 |
buf_free(tmp);
|
| 759 |
return (1);
|
| 760 |
}
|
| 761 |
|
| 762 |
int pgpmime_sign(BUFFER *message, BUFFER *uid, BUFFER *pass, char *secring)
|
| 763 |
{
|
| 764 |
BUFFER *out, *body, *mboundary, *algo;
|
| 765 |
int err;
|
| 766 |
|
| 767 |
out = buf_new();
|
| 768 |
body = buf_new();
|
| 769 |
mboundary = buf_new();
|
| 770 |
algo = buf_new();
|
| 771 |
|
| 772 |
pgp_signhashalgo(algo, uid, secring, pass);
|
| 773 |
|
| 774 |
entity_encode(message, body, out, MIME_7BIT);
|
| 775 |
|
| 776 |
buf_setrnd(mboundary, 18);
|
| 777 |
encode(mboundary, 0);
|
| 778 |
buf_appendf(out, "Content-Type: multipart/signed; boundary=\"%b\";\n",
|
| 779 |
mboundary);
|
| 780 |
buf_appendf(out,
|
| 781 |
"\tmicalg=pgp-%b; protocol=\"application/pgp-signature\"\n",
|
| 782 |
algo);
|
| 783 |
buf_nl(out);
|
| 784 |
|
| 785 |
buf_appendf(out, "--%b\n", mboundary);
|
| 786 |
buf_cat(out, body);
|
| 787 |
buf_nl(out);
|
| 788 |
buf_appendf(out, "--%b\n", mboundary);
|
| 789 |
|
| 790 |
err = pgp_encrypt(PGP_SIGN | PGP_TEXT | PGP_DETACHEDSIG, body, NULL,
|
| 791 |
uid, pass, NULL, secring);
|
| 792 |
|
| 793 |
buf_appends(out, "Content-Type: application/pgp-signature\n");
|
| 794 |
buf_nl(out);
|
| 795 |
buf_cat(out, body);
|
| 796 |
buf_nl(out);
|
| 797 |
buf_appendf(out, "--%b--\n", mboundary);
|
| 798 |
if (err == 0)
|
| 799 |
buf_move(message, out);
|
| 800 |
|
| 801 |
buf_free(out);
|
| 802 |
buf_free(body);
|
| 803 |
buf_free(mboundary);
|
| 804 |
buf_free(algo);
|
| 805 |
return (err);
|
| 806 |
}
|