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