]> granicus.if.org Git - apache/blob - server/gen_test_char.c
Surpress noise about syntax
[apache] / server / gen_test_char.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifdef CROSS_COMPILE
18
19 #include <ctype.h>
20 #define apr_isalnum(c) (isalnum(((unsigned char)(c))))
21 #define apr_isalpha(c) (isalpha(((unsigned char)(c))))
22 #define apr_iscntrl(c) (iscntrl(((unsigned char)(c))))
23 #define apr_isprint(c) (isprint(((unsigned char)(c))))
24 #define APR_HAVE_STDIO_H 1
25 #define APR_HAVE_STRING_H 1
26
27 #else
28
29 #include "apr.h"
30 #include "apr_lib.h"
31
32 #endif
33
34 #if defined(WIN32) || defined(OS2)
35 #define NEED_ENHANCED_ESCAPES
36 #endif
37
38 #if APR_HAVE_STDIO_H
39 #include <stdio.h>
40 #endif
41 #if APR_HAVE_STRING_H
42 #include <string.h>
43 #endif
44
45 /* A bunch of functions in util.c scan strings looking for certain characters.
46  * To make that more efficient we encode a lookup table.
47  */
48 #define T_ESCAPE_SHELL_CMD    (0x01)
49 #define T_ESCAPE_PATH_SEGMENT (0x02)
50 #define T_OS_ESCAPE_PATH      (0x04)
51 #define T_HTTP_TOKEN_STOP     (0x08)
52 #define T_ESCAPE_LOGITEM      (0x10)
53 #define T_ESCAPE_FORENSIC     (0x20)
54 #define T_ESCAPE_URLENCODED   (0x40)
55 #define T_HTTP_CTRLS          (0x80)
56 #define T_URI_RFC3986        (0x100)
57
58 int main(int argc, char *argv[])
59 {
60     unsigned c;
61     unsigned short flags;
62
63     printf("/* this file is automatically generated by gen_test_char, "
64            "do not edit */\n"
65            "#define T_ESCAPE_SHELL_CMD     (%u)\n"
66            "#define T_ESCAPE_PATH_SEGMENT  (%u)\n"
67            "#define T_OS_ESCAPE_PATH       (%u)\n"
68            "#define T_HTTP_TOKEN_STOP      (%u)\n"
69            "#define T_ESCAPE_LOGITEM       (%u)\n"
70            "#define T_ESCAPE_FORENSIC      (%u)\n"
71            "#define T_ESCAPE_URLENCODED    (%u)\n"
72            "#define T_HTTP_CTRLS           (%u)\n"
73            "#define T_URI_RFC3986          (%u)\n"
74            "\n"
75            "static const unsigned short test_char_table[256] = {",
76            T_ESCAPE_SHELL_CMD,
77            T_ESCAPE_PATH_SEGMENT,
78            T_OS_ESCAPE_PATH,
79            T_HTTP_TOKEN_STOP,
80            T_ESCAPE_LOGITEM,
81            T_ESCAPE_FORENSIC,
82            T_ESCAPE_URLENCODED,
83            T_HTTP_CTRLS,
84            T_URI_RFC3986);
85
86     for (c = 0; c < 256; ++c) {
87         flags = 0;
88         if (c % 8 == 0)
89             printf("\n    ");
90
91         /* escape_shell_cmd */
92 #ifdef NEED_ENHANCED_ESCAPES
93         /* Win32/OS2 have many of the same vulnerable characters
94          * as Unix sh, plus the carriage return and percent char.
95          * The proper escaping of these characters varies from unix
96          * since Win32/OS2 use carets or doubled-double quotes,
97          * and neither lf nor cr can be escaped.  We escape unix
98          * specific as well, to assure that cross-compiled unix
99          * applications behave similarly when invoked on win32/os2.
100          *
101          * Rem please keep in-sync with apr's list in win32/filesys.c
102          */
103         if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n\r%", c)) {
104             flags |= T_ESCAPE_SHELL_CMD;
105         }
106 #else
107         if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) {
108             flags |= T_ESCAPE_SHELL_CMD;
109         }
110 #endif
111
112         if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) {
113             flags |= T_ESCAPE_PATH_SEGMENT;
114         }
115
116         if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) {
117             flags |= T_OS_ESCAPE_PATH;
118         }
119
120         if (!apr_isalnum(c) && !strchr(".-*_ ", c)) {
121             flags |= T_ESCAPE_URLENCODED;
122         }
123
124         /* Stop for any non-'token' character, including ctrls, obs-text,
125          * and "tspecials" (RFC2068) a.k.a. "separators" (RFC2616), which
126          * is easer to express as characters remaining in the ASCII token set
127          */
128         if (!c || !(apr_isalnum(c) || strchr("!#$%&'*+-.^_`|~", c))) {
129             flags |= T_HTTP_TOKEN_STOP;
130         }
131
132         /* Catch CTRLs other than VCHAR, HT and SP, and obs-text (RFC7230 3.2)
133          * This includes only the C0 plane, not C1 (which is obs-text itself.)
134          * XXX: We should verify that all ASCII C0 ctrls/DEL corresponding to
135          * the current EBCDIC translation are captured, and ASCII C1 ctrls
136          * corresponding are all permitted (as they fall under obs-text rule)
137          */
138         if (!c || (apr_iscntrl(c) && c != '\t')) {
139             flags |= T_HTTP_CTRLS;
140         }
141
142         /* From RFC3986, the specific sets of gen-delims, sub-delims (2.2),
143          * and unreserved (2.3) that are possible somewhere within a URI.
144          * Spec requires all others to be %XX encoded, including obs-text.
145          */
146         if (c && (strchr(":/?#[]@"                        /* gen-delims */ 
147                          "!$&'()*+,;="                    /* sub-delims */
148                          "-._~", c) || apr_isalnum(c))) { /* unreserved */
149             flags |= T_URI_RFC3986;
150         }
151
152         /* For logging, escape all control characters,
153          * double quotes (because they delimit the request in the log file)
154          * backslashes (because we use backslash for escaping)
155          * and 8-bit chars with the high bit set
156          */
157         if (c && (!apr_isprint(c) || c == '"' || c == '\\' || apr_iscntrl(c))) {
158             flags |= T_ESCAPE_LOGITEM;
159         }
160
161         /* For forensic logging, escape all control characters, top bit set,
162          * :, | (used as delimiters) and % (used for escaping).
163          */
164         if (!apr_isprint(c) || c == ':' || c == '|' || c == '%'
165             || apr_iscntrl(c) || !c) {
166             flags |= T_ESCAPE_FORENSIC;
167         }
168
169         printf("0x%03x%c", flags, (c < 255) ? ',' : ' ');
170     }
171
172     printf("\n};\n");
173
174     return 0;
175 }