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