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