1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: fltrname.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2017 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../c-client/mail.h" /* for MAILSTREAM */
25 #include "../../pith/osdep/writ_dir.h"
26 #include "../../pith/osdep/bldpath.h"
31 /*----------------------------------------------------------------------
32 Filter file names for strange characters
34 Args: file -- the file name to check
36 Result: Returns NULL if file name is OK
37 Returns formatted error message if it is not
40 filter_filename(char *file
, int *fatal
, int strict
)
45 static char illegal
[] = {'\177', '\0'};
48 static char illegal
[] = {'"', '#', '$', '%', '&', /*'/',*/ '(', ')','*',
49 ',', ';', '<', '=', '>', '?', '[', ']',
50 '^', '|', '\177', '\0'};
52 static char illegal
[] = {'"', '#', '$', '%', '&', '\'','(', ')','*',
53 ',', ':', ';', '<', '=', '>', '?', '[', ']',
54 '\\', '^', '|', '\177', '\0'};
57 static char error
[ERRORLEN
];
58 char ill_file
[MAXPATH
+1], *ill_char
, *ptr
, e2
[20];
61 for(ptr
= file
; *ptr
== ' '; ptr
++) ; /* leading spaces gone */
63 while(*ptr
&& (unsigned char)(*ptr
) >= ' ' && strchr(illegal
, *ptr
) == 0)
69 ill_char
= "<newline>";
70 } else if(*ptr
== '\r') {
71 ill_char
= "<carriage return>";
72 } else if(*ptr
== '\t') {
74 *fatal
= FALSE
; /* just whitespace */
75 } else if(*ptr
< ' ') {
76 snprintf(e2
, sizeof(e2
), "control-%c", *ptr
+ '@');
78 } else if (*ptr
== '\177') {
84 *fatal
= FALSE
; /* less offensive? */
87 strncpy(error
, ill_char
, sizeof(error
)-1);
88 error
[sizeof(error
)-1] = '\0';
90 else if(ptr
!= file
) {
91 strncpy(ill_file
, file
, MIN(ptr
-file
,sizeof(ill_file
)-1));
92 ill_file
[MIN(ptr
-file
,sizeof(ill_file
)-1)] = '\0';
93 snprintf(error
, sizeof(error
),
94 "Character \"%s\" after \"%.*s\" not allowed in file name",
95 ill_char
, ERRORLEN
-50, ill_file
);
97 snprintf(error
, sizeof(error
),
98 "First character, \"%s\", not allowed in file name",
105 if((i
=is_writable_dir(file
)) == 0 || i
== 1){
106 snprintf(error
, sizeof(error
), "\"%.*s\" is a directory", ERRORLEN
-50, file
);
111 for(ptr
= file
; *ptr
== ' '; ptr
++) ; /* leading spaces gone */
113 if((ptr
[0] == '.' && ptr
[1] == '.') || filename_parent_ref(ptr
)){
114 snprintf(error
, sizeof(error
), "\"..\" not allowed in filename");
119 return((char *)NULL
);