-
Notifications
You must be signed in to change notification settings - Fork 26
/
mime.c
3335 lines (2999 loc) · 129 KB
/
mime.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------
**
**
** mime.c
**
** Written by Paul L Daniels, originally for the Xamime project
** (http://www.xamime.com) but since spawned off to the ripMIME/alterMIME
** family of email parsing tools.
**
** Copyright PLD, 1999,2000,2001,2002,2003
** Licence: BSD
** For more information on the licence and copyrights of this code, please
** email [email protected]
** CHANGES
** 2003-Jun-24: PLD: Added subject retaining in the global struct
** this is useful for when you want to retrieve such information
** from an external application - without having to dive into
** the hinfo struct directly. Also, the subject is retained only
** for the /primary/ headers, all subsequent headers which [may]
** contain 'subject:' are ignored.
**
** 2020-Aug-05 ad-pro: Add new function to generate file names:
** randinfix, randprefix, randpostfix
** add random number after the counter.
** I have found a bug, that ripmime override extracted files if
** few copies are started for the same mail.
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#ifdef MEMORY_DEBUG
#define DEBUG_MEMORY 1
#include "xmalloc.h"
#endif
#include "pldstr.h"
#include "boundary-stack.h"
#include "ffget.h"
#include "mime.h"
#include "tnef/tnef_api.h"
#include "ripOLE/ole.h"
#include "libmime-decoders.h"
#include "uuencode.h"
#include "filename-filters.h"
#include "logger.h"
#include "strstack.h"
#include "MIME_headers.h"
int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss );
int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss );
int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_level, struct SS_object *ss );
int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss );
int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss );
int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss );
// Predefined filenames
#define MIME_BLANKZONE_FILENAME_DEFAULT "_blankzone_"
#define MIME_HEADERS_FILENAME "_headers_"
#ifndef FL
#define FL __FILE__, __LINE__
#endif
#define _ENC_UNKNOWN 0
#define _ENC_BASE64 1
#define _ENC_PLAINTEXT 2
#define _ENC_QUOTED 3
#define _ENC_EMBEDDED 4
#define _ENC_NOFILE -1
#define _MIME_CHARS_PER_LINE 32
#define _MIME_MAX_CHARS_PER_LINE 76
#define _RECURSION_LEVEL_DEFAULT 20
#define _BOUNDARY_CRASH 2
// BASE64 / UUDEC and other binary writing routines use the write buffer (now in v1.2.16.3+)
// The "limit" define is a check point marker which indicates that on our next run through
// either the BASE64 or UUDEC routines, we need to flush the buffer to disk
#define MIME_MIME_READ_BUFFER_SIZE (8 *1024)
#define _MIME_WRITE_BUFFER_SIZE (8 *1024)
#define _MIME_WRITE_BUFFER_LIMIT (_MIME_WRITE_BUFFER_SIZE -4)
// Debug precodes
#define MIME_DPEDANTIC ((glb.debug >= _MIME_DEBUG_PEDANTIC))
#define MIME_DNORMAL ((glb.debug >= _MIME_DEBUG_NORMAL ))
#define MIME_VERBOSE ((glb.verbosity > 0 ))
#define MIME_VERBOSE_12 ((glb.verbosity_12x_style > 0 ))
#define DMIME if (glb.debug >= _MIME_DEBUG_NORMAL)
#define FL __FILE__,__LINE__
/* our base 64 decoder table */
static unsigned char b64[256]={
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,\
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\
128, 0, 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, 128, 128, 128, 128, 128,\
128, 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, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \
};
struct MIME_globals {
int header_defect_count;
int filecount;
char blankfileprefix[_MIME_STRLEN_MAX];
int blankfileprefix_expliticly_set;
int verbosity;
int verbosity_12x_style;
int verbosity_contenttype;
int verbose_defects;
int report_mime;
int debug;
int quiet;
int syslogging;
int stderrlogging;
int unique_names;
int rename_method;
char headersname[_MIME_STRLEN_MAX];
char tempdirectory[_MIME_STRLEN_MAX];
int save_headers;
int attachment_count;
int current_line;
int no_nameless;
int name_by_type;
int mailbox_format;
int decode_uu;
int decode_tnef;
int decode_b64;
int decode_qp;
int decode_mht;
int decode_ole;
int multiple_filenames;
int header_longsearch;
int max_recursion_level;
int blankzone_saved;
int blankzone_save_option;
char blankzone_filename[_MIMEH_STRLEN_MAX +1];
int (*filename_decoded_reporter)(char *, char *); // Pointer to the reporting function for filenames as they are decoded
// First subject is an important item, because this
// represents the "main" subject of the email, as
// seen by the MUA. We have to store this locally
// rather than rely purely on hinfo, because other
// wise, any consequent parsing of sub-message bodies
// will result in the clobbering of the hinfo struct
char subject[_MIME_STRLEN_MAX];
};
static struct MIME_globals glb;
//char OK[]="OKAY";
static char scratch[1024];
/* File pointer for the headers output */
FILE *headers;
/*-----------------------------------------------------------------\
Function Name : MIME_version
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_version( void )
{
fprintf(stderr,"ripMIME: %s\n", LIBMIME_VERSION);
MIMEH_version();
#ifdef RIPOLE
OLE_version();
#endif
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_name_by_type
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Used to set (on/off) the name-by-type feature, which will
use the content-type paramter to name the attachment rather
than just the default textfile* naming
0 = don't name by type
1 = name by type.
We may consider adding more levels to this to account for
the different types of content-types.
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_name_by_type( int level )
{
glb.name_by_type = level;
return glb.name_by_type;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_debug ID:1
Purpose: Sets the debug level for reporting in MIME
Input: int level : What level of debugging to use, currently there
are only two levels, 0 = none, > 0 = debug info
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_debug( int level )
{
glb.debug = level;
BS_set_debug(level);
TNEF_set_debug(level);
MIMEH_set_debug(level);
MDECODE_set_debug(level);
UUENCODE_set_debug(level);
FNFILTER_set_debug(level);
return glb.debug;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_quiet
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_quiet( int level )
{
glb.quiet = level;
MIME_set_debug(0);
MIME_set_verbosity(0);
return glb.quiet;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_recursion_level
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_recursion_level( int level )
{
glb.max_recursion_level = level;
return glb.max_recursion_level;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_tnef
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_tnef( int level )
{
glb.decode_tnef = level;
TNEF_set_decode( level );
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_ole
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_ole( int level )
{
glb.decode_ole = level;
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_uudecode
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_uudecode( int level )
{
glb.decode_uu = level;
UUENCODE_set_decode( level );
return glb.decode_uu;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_base64
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_base64( int level )
{
glb.decode_b64 = level;
return glb.decode_b64;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_qp
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_qp( int level )
{
glb.decode_qp = level;
MDECODE_set_decode_qp(level);
return glb.decode_qp;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_doubleCR
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_doubleCR( int level )
{
MIMEH_set_doubleCR_save(level); /** 20041106-0859:PLD: Was '0' **/
return MIMEH_get_doubleCR_save();
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_decode_mht
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_decode_mht( int level )
{
glb.decode_mht = level;
return glb.decode_mht;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_header_longsearch
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_header_longsearch( int level )
{
glb.header_longsearch = level;
return glb.header_longsearch;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_tmpdir ID:1
Purpose: Sets the internal Temporary directory name.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_tmpdir( char *setto )
{
PLD_strncpy(glb.tempdirectory,setto, _MIME_STRLEN_MAX);
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_glb.blankfileprefix ID:1
Purpose: Sets the filename prefix which is to be used for any files which are saved and do not have a defined filename.
Input: char *prefix : \0 terminated character array defining the filename prefix
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_blankfileprefix( char *prefix )
{
PLD_strncpy( glb.blankfileprefix, prefix, _MIME_STRLEN_MAX );
glb.blankfileprefix_expliticly_set = 1;
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_get_glb.blankfileprefix ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
char *MIME_get_blankfileprefix( void )
{
return glb.blankfileprefix;
}
/*-------------------------------------------------------
* MIME_setglb.verbosity
*
* By default, MIME reports nothing as its working
* Setting the verbosity level > 0 means that it'll
* report things like the name of the files it's
* writing/extracting.
*
*/
int MIME_set_verbosity( int level )
{
glb.verbosity = level;
MIMEH_set_verbosity( level );
TNEF_set_verbosity( level );
FNFILTER_set_verbose( level );
UUENCODE_set_verbosity( level );
MDECODE_set_verbose( level );
BS_set_verbose( level );
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_verbosity_12x_style
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_verbosity_12x_style( int level )
{
glb.verbosity_12x_style = level;
MIME_set_verbosity( level );
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_verbosity_contenttype
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_verbosity_contenttype( int level )
{
glb.verbosity_contenttype = level;
MIMEH_set_verbosity_contenttype( level );
UUENCODE_set_verbosity_contenttype( level );
TNEF_set_verbosity_contenttype( level );
return glb.verbosity_contenttype;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_verbose_defects
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_verbose_defects( int level )
{
glb.verbose_defects = level;
return glb.verbose_defects;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_report_MIME
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_report_MIME( int level )
{
glb.report_mime = level;
/**MIMEH_set_report_MIME(glb.report_mime); - not yet implemented **/
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_filename_report_fn
Returns Type : int
----Parameter List
1. int (*ptr_to_fn)(char *,
2. char *) ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) )
{
glb.filename_decoded_reporter = ptr_to_fn;
UUENCODE_set_filename_report_fn( ptr_to_fn );
TNEF_set_filename_report_fn( ptr_to_fn );
return 0;
}
/*-------------------------------------------------------
* MIME_set_dumpheaders
*
* By default MIME wont dump the headers to a text file
* but at times this is useful esp for checking
* for new styles of viruses like the KAK.worm
*
* Anything > 0 will make the headers be saved
*
*/
int MIME_set_dumpheaders( int level )
{
glb.save_headers = level;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_multiple_filenames
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_multiple_filenames( int level )
{
glb.multiple_filenames = level;
return 0;
}
/*------------------------------------------------------
* MIME_set_headersname
*
* by default, the headers would be dropped to a file
* called '_headers_'. Using this call we can make
* it pretty much anything we like
*/
int MIME_set_headersname( char *fname )
{
PLD_strncpy(glb.headersname, fname, _MIME_STRLEN_MAX);
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_get_headersname ID:1
Purpose: Returns a pointer to the current glb.headersname string.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
char *MIME_get_headersname( void )
{
return glb.headersname;
}
/*-----------------------------------------------------------------\
Function Name : *MIME_get_subject
Returns Type : char
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *MIME_get_subject( void )
{
return glb.subject;
}
#ifdef RIPMIME_BLANKZONE
/* The blankzone functions are responsbile for telling ripMIME what to do
about the block of data which resides between the first/main headers
and the first attachment/block of a MIME encoded email.
Normally this would come out as textfile0, -except- for non-MIME
encoded emails which would actually have their real body saved as
textfile0.
There are potential kludge options for doing this, but I am going to try
and do it right.
*/
int MIME_set_blankzone_save_option( int option )
{
switch ( option ) {
case MIME_BLANKZONE_SAVE_TEXTFILE:
glb.blankzone_save_option = option;
break;
case MIME_BLANKZONE_SAVE_FILENAME:
glb.blankzone_save_option = option;
snprintf( glb.blankzone_filename, _MIME_STRLEN_MAX, "%s", MIME_BLANKZONE_FILENAME_DEFAULT );
break;
default:
LOGGER_log("%s:%d:MIME_set_blankzone_save_option:WARNING: Unknown option for saving method (%d). Setting to '%s'",FL, option, MIME_BLANKZONE_FILENAME_DEFAULT );
glb.blankzone_save_option = MIME_BLANKZONE_SAVE_FILENAME;
snprintf( glb.blankzone_filename, _MIME_STRLEN_MAX, "%s", MIME_BLANKZONE_FILENAME_DEFAULT );
}
return glb.blankzone_save_option;
}
/*-----------------------------------------------------------------\
Function Name : MIME_set_blankzone_filename
Returns Type : int
----Parameter List
1. char *filename ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_set_blankzone_filename( char *filename )
{
PLD_strncpy( glb.blankzone_filename, filename, _MIME_STRLEN_MAX);
return 0;
}
/*-----------------------------------------------------------------\
Function Name : *MIME_get_blankzone_filename
Returns Type : char
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *MIME_get_blankzone_filename( void )
{
return glb.blankzone_filename;
}
#endif
/*------------------------------------------------------------------------
Procedure: MIME_setglb.no_nameless ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_no_nameless( int level )
{
glb.no_nameless = level;
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_uniquenames ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_uniquenames( int level )
{
glb.unique_names = level;
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_noparanoid ID:1
Purpose: If set, will prevent MIME from clobbering what it considers
to be non-safe characters in the file name.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_paranoid( int level )
{
FNFILTER_set_paranoid( level );
return level;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_mailboxformat ID:1
Purpose: If sets the value for the _mailboxformat variable
in MIME, this indicates to functions later on
that they should be aware of possible mailbox
format specifiers.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_mailboxformat( int level )
{
glb.mailbox_format = level;
MIMEH_set_mailbox( level );
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_renamemethod ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_set_renamemethod( int method )
{
if (( method >= _MIME_RENAME_METHOD_INFIX ) && ( method <= _MIME_RENAME_METHOD_RANDPOSTFIX ))
{
glb.rename_method = method;
}
else
{
LOGGER_log("%s:%d:MIME_set_renamemethod:ERROR: selected method not within %d > x > %d range", FL, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX );
return -1;
}
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIME_get_header_defect_count
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIME_get_header_defect_count( void )
{
return glb.header_defect_count;
}
/*------------------------------------------------------------------------
Procedure: MIME_getglb.attachment_count ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_get_attachment_count( void )
{
return glb.attachment_count;
}
int get_random_value(void) {
int randval;
FILE *fp;
fp = fopen("/dev/urandom", "r");
fread(&randval, sizeof(randval), 1, fp);
fclose(fp);
if (randval < 0) { randval = randval *( -1); };
return randval;
}
/*------------------------------------------------------------------------
Procedure: MIME_test_uniquename ID:1
Purpose: Checks to see that the filename specified is unique. If it's not
unique, it will modify the filename
Input: char *path: Path in which to look for similar filenames
char *fname: Current filename
int method: Method of altering the filename (infix, postfix, prefix, randinfix, randpostfix, randprefix)
Output:
Errors:
------------------------------------------------------------------------*/
int MIME_test_uniquename( char *path, char *fname, int method )
{
struct stat buf;
char newname[_MIME_STRLEN_MAX +1];
char scr[_MIME_STRLEN_MAX +1]; /** Scratch var **/
char *frontname, *extention;
int cleared = 0;
int count = 1;
int randval = get_random_value();
if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Start (%s)",FL,fname);
frontname = extention = NULL; // shuts the compiler up
if (method == _MIME_RENAME_METHOD_INFIX)
{
PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
frontname = scr;
extention = strrchr(scr,'.');
if (extention)
{
*extention = '\0';
extention++;
}
else
{
method = _MIME_RENAME_METHOD_POSTFIX;
}
}
if (method == _MIME_RENAME_METHOD_RANDINFIX)
{
PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
frontname = scr;
extention = strrchr(scr,'.');
if (extention)
{
*extention = '\0';
extention++;
}
else