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-2014 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
)
44 static char illegal
[] = {'\177', '\0'};
47 static char illegal
[] = {'"', '#', '$', '%', '&', /*'/',*/ '(', ')','*',
48 ',', ';', '<', '=', '>', '?', '[', ']',
49 '^', '|', '\177', '\0'};
51 static char illegal
[] = {'"', '#', '$', '%', '&', '\'','(', ')','*',
52 ',', ':', ';', '<', '=', '>', '?', '[', ']',
53 '\\', '^', '|', '\177', '\0'};
56 static char error
[100];
57 char ill_file
[MAXPATH
+1], *ill_char
, *ptr
, e2
[20];
60 for(ptr
= file
; *ptr
== ' '; ptr
++) ; /* leading spaces gone */
62 while(*ptr
&& (unsigned char)(*ptr
) >= ' ' && strchr(illegal
, *ptr
) == 0)
68 ill_char
= "<newline>";
69 } else if(*ptr
== '\r') {
70 ill_char
= "<carriage return>";
71 } else if(*ptr
== '\t') {
73 *fatal
= FALSE
; /* just whitespace */
74 } else if(*ptr
< ' ') {
75 snprintf(e2
, sizeof(e2
), "control-%c", *ptr
+ '@');
77 } else if (*ptr
== '\177') {
83 *fatal
= FALSE
; /* less offensive? */
86 strncpy(error
, ill_char
, sizeof(error
)-1);
87 error
[sizeof(error
)-1] = '\0';
89 else if(ptr
!= file
) {
90 strncpy(ill_file
, file
, MIN(ptr
-file
,sizeof(ill_file
)-1));
91 ill_file
[MIN(ptr
-file
,sizeof(ill_file
)-1)] = '\0';
92 snprintf(error
, sizeof(error
),
93 "Character \"%s\" after \"%.*s\" not allowed in file name",
94 ill_char
, sizeof(error
)-50, ill_file
);
96 snprintf(error
, sizeof(error
),
97 "First character, \"%s\", not allowed in file name",
104 if((i
=is_writable_dir(file
)) == 0 || i
== 1){
105 snprintf(error
, sizeof(error
), "\"%.*s\" is a directory", sizeof(error
)-50, file
);
110 for(ptr
= file
; *ptr
== ' '; ptr
++) ; /* leading spaces gone */
112 if((ptr
[0] == '.' && ptr
[1] == '.') || filename_parent_ref(ptr
)){
113 snprintf(error
, sizeof(error
), "\"..\" not allowed in filename");
118 return((char *)NULL
);