-
Notifications
You must be signed in to change notification settings - Fork 2
/
altermime.c
477 lines (396 loc) · 15.5 KB
/
altermime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*------------------------------------------------------------------------
Module: /extra/development/altermime-0.1.1/altermime.c
Author: Paul L Daniels
Project: AlterMime
State: Development
Creation Date: 02/05/2001
Description: Altermime is a program/object which will allow arbitary alterations to a given MIME encoded mailpack, such as disclaimer additions and attachment-nullification.
------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "mime_alter.h"
#include "logger.h"
/* Global DEFINES */
char ALTERMIMEAPP_VERSION[]="alterMIME v0.3.13 (November-2008) by Paul L Daniels - http://www.pldaniels.com/altermime\n";
char ALTERMIMEAPP_USAGE[]="altermime --input=<input mime pack> ( --input=- for stdin )\n"
" [--disclaimer=<disclaimer file>]\n"
" [--disclaimer-html=<HTML disclaimer file>]\n"
" [--disclaimer-b64=<BASE64 encoded dislcaimer>]\n"
" [--htmltoo]\n"
#ifdef DISPOS
" [--textpos=<positioning code>]\n"
" [--htmlpos=<positioning code>]\n"
#endif
#ifdef ALTERMIME_PRETEXT
/*" [--pretext] Insert disclaimer files in pretext mode.\n"*/ // By implication, pretext is inserted based on the file status below.
" [--pretext=<pretext file>]\n"
" [--pretext-html=<pretext HTML file>]\n"
" [--pretext-b64=<BASE64 encoded pretext >]\n"
#endif
" [--force-into-b64]\n"
" [--force-for-bad-html]\n"
" [--multipart-insert]\n"
" [--remove=<remove file name (regex)>] (if filename contains a /, matches on mime-type )\n"
" [--removeall]\n"
" [--replace=<filename to replace> --with=<replace with>]\n"
" [--xheader=\"...\"\n"
" [--alter-header=\"...\" --alter-with=\"...\" --alter-mode=<prefix|suffix|replace>]\n"
" [--altersigned]\n"
" [--tmpdir=<directory>\n"
" [--no-qmail-bounce]\n"
" [--verbose]\n"
" [--log-stdout]\n"
" [--log-stderr]\n"
" [--log-syslog]\n"
" [--debug]\n"
" [--version]\n\n"
"Option Descriptions:\n"
"\t--input=, Sets the mailpack file to be the filename supplied,\n"
"\t\tif the filename is a single '-' (hyphen) then the mailpack\n"
"\t\tis sourced via stdin and outputted via stdout.\n"
"\n"
"\t--disclaimer=, Set the plaintext disclaimer source file.\n"
"\t--disclaimer-html=, Set the HTML disclaimer source file.\n"
"\t--disclaimer-b64=, Set the BASE64 encoded disclaimer source file (implies --force-into-b64).\n"
"\n"
"\t--htmltoo, Sets alterMIME to insert the plaintext disclaimer into\n"
"\t--force-into-b64, Sets alterMIME to insert disclaimers into BASE64 encoded text segments\n"
"\t--force-for-bad-html, Force adding of the HTML disclaimer even when HTML is not correctly formatted\n"
"\t\tthe HTML portion of the email body ( if there is no explicitly\n"
"\t\tdefined HTML dislcaimer, see --disclaimer-html )\n"
"\n"
"\t--disclaimer-attachment=<filename> (use $CID in the disclaimer text to reference)\n"
"\n"
"\t--remove=, Remove any attachments which match the filename supplied,\n"
"\t\tif the filename text contains a forward-slash '/', then the\n"
"\t\tmatching will occur based on content-type headers rather than\n"
"\t\tby filename.\n"
"\t--removeall, Remove all attachments\n"
"\t--replace=, Replace attachments matching the given filename. Requires to\n"
"\t\tbe used with --with.\n"
"\t--with=, Replace the attachments specified by --replace with the file\n"
"\t\tspecified.\n"
"\t--xheader=, Insert a header line as specified into the first set of headers.\n"
"\t--alter-header=\"...\" --alter-with=\"...\" --alter-mode=(prefix|suffix|replace)\n"
"\t\tAlter an existing header in the mailpack. This function modifies the\n"
"\t\tvalue of the header, as apposed to the header name.\n"
"\t--altersigned, Force alterMIME to modify 'signed' emails\n"
"\t--tmpdir=<directory>, where to store temporary files when processing from STDIN\n"
"\t--no-qmail-bounce, Don't search into email bodies for attachment headers\n"
"\t--verbose, Describe details of the process occuring\n"
"\t--log-stdout, Send all output messages to stdout\n"
"\t--log-stderr, Send all output messages to stderr\n"
"\t--log-syslog, Send all output messages to syslog\n"
"\t--debug, Provide greater verbosity and debugging information\n"
"\t--version, display the alterMIME version string\n"
"\n";
/* Global variables / settings */
char ALTERMIMEAPP_default_remove_prefix[]="removed";
char ALTERMIMEAPP_removeall_filename[]=".*";
struct ALTERMIMEAPP_globals {
char tmpdir[1024];
char *input_file;
int input_is_stdin;
char *disclaimer_file;
char *disclaimer_html_file;
char *disclaimer_b64_file;
char *disclaimer_attachment;
int disclaimer_insert;
char *pretext_file;
char *pretext_html_file;
char *pretext_b64_file;
int pretext_insert;
char *remove_filename;
char *replace;
char *with;
char *xheader;
char *alter_header;
char *alter_with;
int alter_mode;
int verbose;
};
/*------------------------------------------------------------------------
Procedure: parse_args ID:1
Purpose: Parses the command line arguments and sets up internal parameters
Input: argc: The number of arguments
argv: Array of strings
Output: none
Errors:
------------------------------------------------------------------------*/
int ALTERMIMEAPP_parse_args( struct ALTERMIMEAPP_globals *glb, int argc, char **argv )
{
int i;
char *p=NULL;
char *q=NULL;
for (i = 1; i < argc; i++){
if ((argv[i][0] == '-')&&(argv[i][1] == '-'))
{
p = argv[i];
p += 2;
if (strncmp(p,"input=",6)==0) {
glb->input_file = p +strlen("input=");
} else if (strncmp(p,"tmpdir=",7)==0) {
snprintf(glb->tmpdir, sizeof(glb->tmpdir), "%s" ,p +strlen("tmpdir="));
} else if (strncmp(p,"htmltoo",7)==0) {
AM_set_HTMLtoo(1);
} else if (strncmp(p,"force-into-b64",sizeof("force-into-b64"))==0) {
AM_set_force_into_b64(1);
} else if (strncmp(p,"force-for-bad-html",sizeof("force-for-bad-html"))==0) {
AM_set_force_for_bad_html(1);
} else if (strncmp(p,"multipart-insert", strlen("multipart-insert"))==0) {
AM_set_multipart_insert(1);
} else if (strncmp(p,"pretext=", strlen("pretext="))==0) {
glb->pretext_file = p +strlen("pretext=");
glb->pretext_insert = 1;
//LOGGER_log("%s:%d:ALTERMIMEAPP_parse_args:DEBUG: pretext set to '%s'", FL, glb->pretext_file);
} else if (strncmp(p,"pretext-html=",strlen("pretext-html="))==0) {
glb->pretext_html_file = p +strlen("pretext-html=");
AM_set_HTMLtoo(1);
glb->pretext_insert = 1;
} else if (strncmp(p,"pretext-b64=",strlen("pretext-b64="))==0) {
glb->pretext_html_file = p +strlen("pretext-b64=");
AM_set_force_into_b64(1);
glb->pretext_insert = 1;
} else if (strncmp(p,"disclaimer=",11)==0) {
glb->disclaimer_file = p +strlen("disclaimer=");
AM_set_disclaimer_plain( glb->disclaimer_file, AM_DISCLAIMER_TYPE_FILENAME );
glb->disclaimer_insert = 1;
} else if (strncmp(p,"disclaimer-html=",16)==0) {
glb->disclaimer_html_file = p +strlen("disclaimer-html=");
AM_set_disclaimer_HTML( glb->disclaimer_html_file, AM_DISCLAIMER_TYPE_FILENAME );
glb->disclaimer_insert = 1;
AM_set_HTMLtoo(1);
} else if (strncmp(p,"disclaimer-b64=",strlen("disclaimer-b64="))==0) {
glb->disclaimer_b64_file = p +strlen("disclaimer-b64=");
AM_set_disclaimer_b64( glb->disclaimer_b64_file, AM_DISCLAIMER_TYPE_FILENAME );
glb->disclaimer_insert = 1;
AM_set_force_into_b64(1);
} else if (strncmp(p,"disclaimer-attachment=",strlen("disclaimer-attachment="))==0) {
glb->disclaimer_attachment = p +strlen("disclaimer-attachment=");
AM_set_disclaimer_attachment( glb->disclaimer_attachment, 0);
} else if (strncmp(p,"remove=",7)==0) {
glb->remove_filename = p +strlen("remove=");
} else if (strncmp(p, "remove_prefix=",13)==0) {
LOGGER_log("--remove_prefix is depricated, ignoring");
//glb->remove_prefix = p +strlen("remove_prefix=");
//
} else if (strncmp(p, "removeall",9)==0) {
glb->remove_filename = ALTERMIMEAPP_removeall_filename;
} else if (strncmp(p, "altersigned",11)==0) {
AM_set_altersigned(1);
} else if (strncmp(p, "replace",7)==0) {
glb->replace = p +strlen("replace=");
} else if (strncmp(p, "with",4)==0) {
glb->with = p +strlen("with=");
} else if (strncmp(p, "xheader",7)==0) {
glb->xheader = p +strlen("xheader=");
} else if (strncmp(p, "version",7)==0) {
fprintf(stdout,"%s",ALTERMIMEAPP_VERSION);
exit(0);
} else if (strncmp(p, "debug", 5)==0) {
AM_set_debug(1);
} else if (strncmp(p, "no-qmail-bounce",15)==0) {
AM_set_header_long_search(0);
} else if (strncmp(p, "verbose",7)==0) {
AM_set_verbose(1);
} else if (strncmp(p, "alter-header", strlen("alter-header"))==0) {
glb->alter_header = p +strlen("alter-header=");
if (*glb->alter_header == '\"') glb->alter_header++;
} else if (strncmp(p, "alter-with", strlen("alter-with"))==0) {
glb->alter_with = p +strlen("alter-with=");
if (*glb->alter_with == '\"') glb->alter_with++;
} else if (strncmp(p, "alter-mode", strlen("alter-mode"))==0) {
q = p +strlen("alter-mode=");
if (*q == '\"') q++;
glb->alter_mode = AM_HEADER_ADJUST_MODE_NONE;
if (strncmp( q, "prefix", strlen("prefix"))==0) { glb->alter_mode = AM_HEADER_ADJUST_MODE_PREFIX; }
else if (strncmp( q, "suffix", strlen("suffix"))==0) { glb->alter_mode = AM_HEADER_ADJUST_MODE_SUFFIX; }
else if (strncmp( q, "replace", strlen("replace"))==0) { glb->alter_mode = AM_HEADER_ADJUST_MODE_REPLACE; }
else { LOGGER_log("ERROR: Unknown header alter mode '%s'. Please use either of prefix, suffix or replace.", q); }
}
else if (strncmp(p, "log-stdout",strlen("log-stdout"))==0) { LOGGER_set_output_mode(_LOGGER_STDOUT); }
else if (strncmp(p, "log-stderr",strlen("log-stderr"))==0) { LOGGER_set_output_mode(_LOGGER_STDERR); }
else if (strncmp(p, "log-syslog",strlen("log-syslog"))==0) { LOGGER_set_output_mode(_LOGGER_SYSLOG); LOGGER_set_syslog_mode( LOG_MAIL|LOG_INFO ); }
else
{
LOGGER_log("Error, unknown parameter \"%s\"\n",p);
LOGGER_log("%s",ALTERMIMEAPP_USAGE);
exit(1);
} /* if-ELSE */
} /* if argv == '--' */
} /* for */
return 0;
}
/*-----------------------------------------------------------------\
Function Name : ALTERMIMEAPP_init
Returns Type : int
----Parameter List
1. struct ALTERMIMEAPP_globals *glb ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int ALTERMIMEAPP_init( struct ALTERMIMEAPP_globals *glb )
{
glb->input_is_stdin = 0;
glb->input_file = NULL;
glb->disclaimer_file = NULL;
glb->disclaimer_html_file = NULL;
glb->disclaimer_b64_file = NULL;
glb->disclaimer_insert = 0;
#ifdef ALTERMIME_PRETEXT
glb->pretext_insert = 0;
glb->pretext_file = NULL;
glb->pretext_html_file = NULL;
glb->pretext_b64_file = NULL;
#endif
glb->remove_filename = NULL;
glb->replace = NULL;
glb->with = NULL;
glb->xheader = NULL;
glb->verbose = 0;
glb->alter_header = NULL;
glb->alter_with = NULL;
glb->alter_mode = AM_HEADER_ADJUST_MODE_NONE;
snprintf(glb->tmpdir, sizeof(glb->tmpdir),"/tmp");
return 0;
}
/*-----------------------------------------------------------------\
Function Name : main
Returns Type : int
----Parameter List
1. int argc,
2. char **argv ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int main( int argc, char **argv )
{
struct ALTERMIMEAPP_globals glb;
FILE *f;
LOGGER_set_output_mode(_LOGGER_STDOUT);
ALTERMIMEAPP_init( &glb );
ALTERMIMEAPP_parse_args(&glb,argc, argv);
if (!glb.input_file) {
LOGGER_log("Error: No input file specified\n");
LOGGER_log(ALTERMIMEAPP_USAGE);
exit(1);
}
if (((!glb.replace)&&(glb.with))||((glb.replace)&&(!glb.with)))
{
LOGGER_log("Error: Both --replace= and --with= must be set\n");
exit(1);
}
if( glb.input_file && !(glb.alter_mode||glb.replace||glb.disclaimer_file||glb.remove_filename||glb.xheader||glb.pretext_file||glb.pretext_html_file)) {
LOGGER_log("Error: Must specify an action for the input file.\n");
LOGGER_log( ALTERMIMEAPP_USAGE);
exit(1);
}
if ((strcmp(glb.input_file,"-") == 0) && (/**glb.disclaimer_file||**/glb.replace||glb.xheader)) {
LOGGER_log("Error: reading/writing from stdin/stdout not implemented for --xheader,--disclaimer, or --replace.\n");
LOGGER_log(ALTERMIMEAPP_USAGE);
exit(1);
}
if (glb.input_file[0]=='-') {
char scratch[1024];
size_t rc;
glb.input_is_stdin = 1;
snprintf(scratch,sizeof(scratch),"%s/%ld%d.amtmp", glb.tmpdir, time(NULL), getpid());
// free(glb.input_file);
glb.input_file = strdup(scratch);
f = fopen(glb.input_file, "w");
if (!f) {
LOGGER_log("%s:%d:ERROR: Cannot open '%s' for writing as a temporary file for alterMIME (%s)", FL, scratch, strerror(errno));
exit(1);
} else {
do {
rc = fread(scratch, 1, sizeof(scratch), stdin);
if (rc == -1) {
LOGGER_log("%s:%d:ERROR: Cannot read from '%s' (%s)", FL, glb.input_file, strerror(errno));
exit(1);
}
if (rc) {
rc = fwrite(scratch, 1, rc, f);
if (rc == -1) {
LOGGER_log("%s:%d:ERROR: Cannot write to '%s' (%s)", FL, glb.input_file, strerror(errno));
exit(1);
}
}
} while (rc);
fclose(f);
}
}
if ((glb.alter_mode != AM_HEADER_ADJUST_MODE_NONE)&&(glb.alter_with != NULL)&&(glb.alter_header != NULL))
{
AM_alter_header( glb.input_file, glb.alter_header, glb.alter_with, glb.alter_mode );
}
if ((glb.replace)&&(glb.with)) AM_attachment_replace( glb.input_file, glb.replace, glb.with);
#ifdef ALTERMIME_PRETEXT
AM_set_pretext(0);
#endif
if (glb.disclaimer_file) AM_add_disclaimer( glb.input_file );
if (glb.remove_filename) AM_nullify_attachment(glb.input_file, glb.remove_filename);
if (glb.xheader) AM_insert_Xheader( glb.input_file, glb.xheader);
AM_done();
#ifdef ALTERMIME_PRETEXT
if (glb.pretext_insert) {
//LOGGER_log("%s:%d:alterMIME: PRETEXT STAGE",FL);
//LOGGER_log("%s:%d:alterMIME: PRETEXT STAGE",FL);
//LOGGER_log("%s:%d:alterMIME: PRETEXT STAGE",FL);
//LOGGER_log("%s:%d:alterMIME: PRETEXT STAGE",FL);
AM_init();
//AM_set_debug(1);
AM_set_pretext(1);
if (glb.pretext_file) AM_set_disclaimer_plain( glb.pretext_file, AM_DISCLAIMER_TYPE_FILENAME );
if (glb.pretext_html_file) {
AM_set_disclaimer_HTML( glb.pretext_html_file, AM_DISCLAIMER_TYPE_FILENAME );
AM_set_HTMLtoo(1);
}
if (glb.pretext_b64_file) AM_set_disclaimer_b64( glb.pretext_b64_file, AM_DISCLAIMER_TYPE_FILENAME );
AM_add_disclaimer( glb.input_file );
}
#endif
if (glb.input_is_stdin) {
char scratch[1024];
size_t rc;
FILE *f;
f = fopen(glb.input_file, "r");
if (!f) {
LOGGER_log("%s:%d:ERROR: Cannot open alterMIME temporary file '%s' for reading (%s)", FL, scratch, strerror(errno));
exit(1);
}
do {
rc = fread(scratch, 1, sizeof(scratch), f);
if (rc == -1) {
LOGGER_log("%s:%d:ERROR: Cannot read from '%s' (%s)", FL, glb.input_file, strerror(errno));
exit(1);
}
if (rc) {
rc = fwrite(scratch, 1, rc, stdout);
if (rc == -1) {
LOGGER_log("%s:%d:ERROR: Cannot write to '%s' (%s)", FL, glb.input_file, strerror(errno));
exit(1);
}
}
} while (rc);
fclose(f);
unlink(glb.input_file);
}
return 0;
}
/*-------------------------------------------------END. */