forked from inflex/alterMIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filename-filters.c
287 lines (216 loc) · 6.18 KB
/
filename-filters.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
/*------------------------------------------------------------------------
Module: /extra/development/xamime/xamime_working/ripmime/filename-filters.c
Author: Paul L Daniels
Project: ripMIME
State: Release
Creation Date: 01 Jan 03
Description: Filename Filters is a module which is designed to check and 'safety-enhance'
filenames which are passed to it. This may include things like removing
directory risers ( ../.. ), root directory attempts ( / ), and parameter passing.
------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <ctype.h>
#include "pldstr.h"
#include "logger.h"
#include "filename-filters.h"
#ifndef FL
#define FL __FILE__, __LINE__
#endif
#define FNFILTER_DEBUG_PEDANTIC 10
#define FNFILTER_DEBUG_NORMAL 1
// Debug precodes
#define FNFILTER_DPEDANTIC ((glb.debug >= FNFILTER_DEBUG_PEDANTIC))
#define FNFILTER_DNORMAL ((glb.debug >= FNFILTER_DEBUG_NORMAL ))
#define DFN if ((glb.debug >= FNFILTER_DEBUG_NORMAL))
struct FNFILTER_globals {
int debug;
int verbose;
int paranoid;
int x_mac;
};
static struct FNFILTER_globals glb;
int FNFILTER_init( void )
{
glb.debug = 0;
glb.verbose = 0;
glb.paranoid = 0;
glb.x_mac = 0;
return 0;
}
int FNFILTER_set_debug( int level )
{
glb.debug = level;
return glb.debug;
}
int FNFILTER_set_verbose( int level )
{
glb.verbose = level;
return glb.verbose;
}
int FNFILTER_set_mac( int level )
{
glb.x_mac = level;
return glb.x_mac;
}
int FNFILTER_set_paranoid( int level )
{
glb.paranoid = level;
return glb.paranoid;
}
/*------------------------------------------------------------------------
Procedure: quick_clean_filename ID:1
Purpose: Removes non-7bit characers from the filename
Input: char *fname: Null terminated string
Output:
Errors:
------------------------------------------------------------------------*/
int FNFILTER_paranoid_filter( char *fname, int size )
{
char tmp[1024];
char *p;
/* Scan for . and .. filenames
** 20040727-12H54
** Patch supplied by Marco Ariano
** Patch modified by Paul L Daniels
**
*/
if ((1 == size)&&('.' == *fname))
{
*fname = '_';
return 0;
} else if ((2 == size)&&(0 == strncmp(fname,"..",2))) {
snprintf(fname,3,"__");
return 0;
}
/* scan out any directory separators */
p = strrchr(fname,'/');
if (p)
{
// Check to see that this seperator isn't the -last- char in the string
if (*(p+1) == '\0') *p = '\0';
else
{
p++;
PLD_strncpy(tmp,p,sizeof(tmp));
PLD_strncpy(fname,tmp,size);
}
}
else if ( (p = strrchr(fname,'\\')))
{
// Check to see that this seperator isn't the -last- char in the string
if (*(p+1) == '\0') *p = '\0';
else
{
p++;
PLD_strncpy(tmp,p,sizeof(tmp));
PLD_strncpy(fname,tmp,size);
}
}
if ( glb.paranoid > 0 )
{
// If we're really paranoid, then we go along and convert anything we don't like
// the look of into 7-bit
//
// These days we shouldn't be using this any more as there are many filenames
// which require > 7-bit charsets.
while (*fname)
{
if( !isalnum((int)*fname) && (*fname != '.') ) *fname='_';
if( (*fname < ' ')||(*fname > '~') ) *fname='_';
fname++;
}
}
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_decode_filename ID:1
Purpose: Removed spurilous characters from filename strings.
Input: char *fname: null terminated character string
Output:
Errors:
------------------------------------------------------------------------*/
int FNFILTER_filter( char *fname, int size )
{
int fnl;
char tmp[1024];
char *p;
if (fname == NULL) return 0;
fnl = strlen(fname);
DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: fname[%d chars] = '%s'\n", FL, fnl, fname );
/** If we're handling a Mac file, prefilter **/
if (glb.x_mac == 1)
{
char *q = fname;
DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Filtering x-mac filename '%s'",FL,fname);
while (*q)
{
if (*q == '/') *q = '-'; /** Convert the Mac / separator to a hyphen **/
q++;
}
DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: x-mac filename is now '%s'",FL,fname);
}
/* We only look at trimming the quotes off a filename if it has more than 2 chars
* because obviously we'll need to strip off 2 chars (leading and finishing quote)
*/
if ( fnl > 2 )
{
/* if the MIME_filename starts and ends with "'s */
if ((fname[0] == '\"') && (fname[fnl-1] == '\"'))
{
if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Trimming quotes off filename\n", FL );
/* reduce the file namelength by two*/
fnl = fnl -2; // 17-11-2002: was =-2, thanks to Vasily Chernikov for spotting the glaring error!
/* shuffle the MIME_filename chars down */
memmove(fname,fname+1,fnl);
/* terminate the string */
fname[fnl] = '\0';
if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Trimming filename done, fname = '%s'\n", FL, fname );
} /* if */
} /* if */
p = strrchr(fname,'/');
if (p)
{
p++;
PLD_strncpy( tmp, p, sizeof(tmp) );
PLD_strncpy( fname, tmp, size);
} else {
// Check for Windows/DOS backslash seperator
p = strrchr( fname, '\\' );
if ( p )
{
if ( *(p+1) != '"' )
{
p++;
PLD_strncpy( tmp, p, sizeof(tmp) );
PLD_strncpy( fname, tmp, size );
}
}
}
// Scan for ? symbols - these are often used to make the email client pass paremeters to the filename
// Check though to see that the previous character is not a '=' symbol, because if it is, then we
// actually have an ISO encoded filename
p = strchr( fname, '?' );
if (p != NULL)
{
if (p > fname)
{
if (*(p-1) != '=')
{
*p = '\0';
} else {
// leave the ? alone, as it's part of an ISO encoded filename
}
} else {
// First char of the filename is a '?', change this to a hypen.
*p = '-';
}
}
if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Starting paranoia filter\n", FL );
FNFILTER_paranoid_filter( fname, strlen( fname ) );
if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: paranoia filter done. Filename='%s'\n", FL, fname );
return 0;
}