1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #if defined(WIN32) || defined(OS2)
18 #define NEED_ENHANCED_ESCAPES
26 /* A bunch of functions in util.c scan strings looking for certain characters.
27 * To make that more efficient we encode a lookup table.
29 #define T_ESCAPE_SHELL_CMD (0x01)
30 #define T_ESCAPE_PATH_SEGMENT (0x02)
31 #define T_OS_ESCAPE_PATH (0x04)
32 #define T_ESCAPE_ECHO (0x08)
33 #define T_ESCAPE_URLENCODED (0x10)
34 #define T_ESCAPE_XML (0x20)
35 #define T_ESCAPE_LDAP_DN (0x40)
36 #define T_ESCAPE_LDAP_FILTER (0x80)
38 int main(int argc, char *argv[])
43 printf("/* this file is automatically generated by gen_test_char, "
44 "do not edit. \"make include/private/apr_escape_test_char.h\" to regenerate. */\n"
45 "#define T_ESCAPE_SHELL_CMD (%u)\n"
46 "#define T_ESCAPE_PATH_SEGMENT (%u)\n"
47 "#define T_OS_ESCAPE_PATH (%u)\n"
48 "#define T_ESCAPE_ECHO (%u)\n"
49 "#define T_ESCAPE_URLENCODED (%u)\n"
50 "#define T_ESCAPE_XML (%u)\n"
51 "#define T_ESCAPE_LDAP_DN (%u)\n"
52 "#define T_ESCAPE_LDAP_FILTER (%u)\n"
54 "static const unsigned char test_char_table[256] = {",
56 T_ESCAPE_PATH_SEGMENT,
62 T_ESCAPE_LDAP_FILTER);
64 for (c = 0; c < 256; ++c) {
69 /* escape_shell_cmd */
70 #ifdef NEED_ENHANCED_ESCAPES
71 /* Win32/OS2 have many of the same vulnerable characters
72 * as Unix sh, plus the carriage return and percent char.
73 * The proper escaping of these characters varies from unix
74 * since Win32/OS2 use carets or doubled-double quotes,
75 * and neither lf nor cr can be escaped. We escape unix
76 * specific as well, to assure that cross-compiled unix
77 * applications behave similiarly when invoked on win32/os2.
79 * Rem please keep in-sync with apr's list in win32/filesys.c
81 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n\r%", c)) {
82 flags |= T_ESCAPE_SHELL_CMD;
85 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) {
86 flags |= T_ESCAPE_SHELL_CMD;
90 if (!isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) {
91 flags |= T_ESCAPE_PATH_SEGMENT;
94 if (!isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) {
95 flags |= T_OS_ESCAPE_PATH;
98 if (!isalnum(c) && !strchr(".-*_ ", c)) {
99 flags |= T_ESCAPE_URLENCODED;
102 /* For logging, escape all control characters,
103 * double quotes (because they delimit the request in the log file)
104 * backslashes (because we use backslash for escaping)
105 * and 8-bit chars with the high bit set
107 if (c && (!isprint(c) || c == '"' || c == '\\' || iscntrl(c))) {
108 flags |= T_ESCAPE_ECHO;
111 if (strchr("<>&\"", c)) {
112 flags |= T_ESCAPE_XML;
115 /* LDAP DN escaping (RFC4514) */
116 if (!isprint(c) || strchr("\"+,;<>\\", c)) {
117 flags |= T_ESCAPE_LDAP_DN;
120 /* LDAP filter escaping (RFC4515) */
121 if (!isprint(c) || strchr("*()\\", c)) {
122 flags |= T_ESCAPE_LDAP_FILTER;
125 printf("%u%c", flags, (c < 255) ? ',' : ' ');