blob: 7864df89feac39fb35ce9ac0093778176a02d592 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP ACLs declaration
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/chunk.h>
20#include <common/compat.h>
21#include <common/config.h>
22#include <common/debug.h>
23#include <common/http.h>
24#include <common/memory.h>
25#include <common/standard.h>
26#include <common/version.h>
27
28#include <types/global.h>
29
30#include <proto/acl.h>
31#include <proto/arg.h>
32#include <proto/auth.h>
33#include <proto/pattern.h>
34
35
36/* We use the pre-parsed method if it is known, and store its number as an
37 * integer. If it is unknown, we use the pointer and the length.
38 */
39static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
40{
41 int len, meth;
42
43 len = strlen(text);
44 meth = find_http_meth(text, len);
45
46 pattern->val.i = meth;
47 if (meth == HTTP_METH_OTHER) {
48 pattern->ptr.str = (char *)text;
49 pattern->len = len;
50 }
51 else {
52 pattern->ptr.str = NULL;
53 pattern->len = 0;
54 }
55 return 1;
56}
57
58/* See above how the method is stored in the global pattern */
59static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
60{
61 int icase;
62 struct pattern_list *lst;
63 struct pattern *pattern;
64
65 list_for_each_entry(lst, &expr->patterns, list) {
66 pattern = &lst->pat;
67
68 /* well-known method */
69 if (pattern->val.i != HTTP_METH_OTHER) {
70 if (smp->data.u.meth.meth == pattern->val.i)
71 return pattern;
72 else
73 continue;
74 }
75
76 /* Other method, we must compare the strings */
77 if (pattern->len != smp->data.u.meth.str.data)
78 continue;
79
80 icase = expr->mflags & PAT_MF_IGNORE_CASE;
81 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
82 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0))
83 return pattern;
84 }
85 return NULL;
86}
87
88/************************************************************************/
89/* All supported ACL keywords must be declared here. */
90/************************************************************************/
91
92/* Note: must not be declared <const> as its list will be overwritten.
93 * Please take care of keeping this list alphabetically sorted.
94 */
95static struct acl_kw_list acl_kws = {ILH, {
96 { "base", "base", PAT_MATCH_STR },
97 { "base_beg", "base", PAT_MATCH_BEG },
98 { "base_dir", "base", PAT_MATCH_DIR },
99 { "base_dom", "base", PAT_MATCH_DOM },
100 { "base_end", "base", PAT_MATCH_END },
101 { "base_len", "base", PAT_MATCH_LEN },
102 { "base_reg", "base", PAT_MATCH_REG },
103 { "base_sub", "base", PAT_MATCH_SUB },
104
105 { "cook", "req.cook", PAT_MATCH_STR },
106 { "cook_beg", "req.cook", PAT_MATCH_BEG },
107 { "cook_dir", "req.cook", PAT_MATCH_DIR },
108 { "cook_dom", "req.cook", PAT_MATCH_DOM },
109 { "cook_end", "req.cook", PAT_MATCH_END },
110 { "cook_len", "req.cook", PAT_MATCH_LEN },
111 { "cook_reg", "req.cook", PAT_MATCH_REG },
112 { "cook_sub", "req.cook", PAT_MATCH_SUB },
113
114 { "hdr", "req.hdr", PAT_MATCH_STR },
115 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
116 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
117 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
118 { "hdr_end", "req.hdr", PAT_MATCH_END },
119 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
120 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
121 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
122
123 /* these two declarations uses strings with list storage (in place
124 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
125 * and delete functions are relative to the list management. The parse
126 * and match method are related to the corresponding fetch methods. This
127 * is very particular ACL declaration mode.
128 */
129 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
130 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
131
132 { "path", "path", PAT_MATCH_STR },
133 { "path_beg", "path", PAT_MATCH_BEG },
134 { "path_dir", "path", PAT_MATCH_DIR },
135 { "path_dom", "path", PAT_MATCH_DOM },
136 { "path_end", "path", PAT_MATCH_END },
137 { "path_len", "path", PAT_MATCH_LEN },
138 { "path_reg", "path", PAT_MATCH_REG },
139 { "path_sub", "path", PAT_MATCH_SUB },
140
141 { "req_ver", "req.ver", PAT_MATCH_STR },
142 { "resp_ver", "res.ver", PAT_MATCH_STR },
143
144 { "scook", "res.cook", PAT_MATCH_STR },
145 { "scook_beg", "res.cook", PAT_MATCH_BEG },
146 { "scook_dir", "res.cook", PAT_MATCH_DIR },
147 { "scook_dom", "res.cook", PAT_MATCH_DOM },
148 { "scook_end", "res.cook", PAT_MATCH_END },
149 { "scook_len", "res.cook", PAT_MATCH_LEN },
150 { "scook_reg", "res.cook", PAT_MATCH_REG },
151 { "scook_sub", "res.cook", PAT_MATCH_SUB },
152
153 { "shdr", "res.hdr", PAT_MATCH_STR },
154 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
155 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
156 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
157 { "shdr_end", "res.hdr", PAT_MATCH_END },
158 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
159 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
160 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
161
162 { "url", "url", PAT_MATCH_STR },
163 { "url_beg", "url", PAT_MATCH_BEG },
164 { "url_dir", "url", PAT_MATCH_DIR },
165 { "url_dom", "url", PAT_MATCH_DOM },
166 { "url_end", "url", PAT_MATCH_END },
167 { "url_len", "url", PAT_MATCH_LEN },
168 { "url_reg", "url", PAT_MATCH_REG },
169 { "url_sub", "url", PAT_MATCH_SUB },
170
171 { "urlp", "urlp", PAT_MATCH_STR },
172 { "urlp_beg", "urlp", PAT_MATCH_BEG },
173 { "urlp_dir", "urlp", PAT_MATCH_DIR },
174 { "urlp_dom", "urlp", PAT_MATCH_DOM },
175 { "urlp_end", "urlp", PAT_MATCH_END },
176 { "urlp_len", "urlp", PAT_MATCH_LEN },
177 { "urlp_reg", "urlp", PAT_MATCH_REG },
178 { "urlp_sub", "urlp", PAT_MATCH_SUB },
179
180 { /* END */ },
181}};
182
183__attribute__((constructor))
184static void __http_acl_init(void)
185{
186 acl_register_keywords(&acl_kws);
187}
188
189/*
190 * Local variables:
191 * c-indent-level: 8
192 * c-basic-offset: 8
193 * End:
194 */