]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
11fdf7f2 | 3 | |
7c673cae FG |
4 | /* |
5 | * Ceph - scalable distributed file system | |
6 | * | |
7 | * Copyright (C) 2013 eNovance SAS <licensing@enovance.com> | |
8 | * | |
9 | * This is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License version 2.1, as published by the Free Software | |
12 | * Foundation. See file COPYING. | |
13 | * | |
14 | */ | |
11fdf7f2 | 15 | |
7c673cae FG |
16 | #include <string.h> |
17 | #include <limits.h> | |
18 | ||
19 | #include <iostream> | |
20 | #include <map> | |
21 | ||
22 | #include "include/types.h" | |
23 | ||
24 | #include "rgw_cors_s3.h" | |
25 | #include "rgw_user.h" | |
26 | ||
27 | #define dout_context g_ceph_context | |
28 | #define dout_subsys ceph_subsys_rgw | |
29 | ||
20effc67 | 30 | using namespace std; |
7c673cae FG |
31 | |
32 | void RGWCORSRule_S3::to_xml(XMLFormatter& f) { | |
33 | ||
34 | f.open_object_section("CORSRule"); | |
35 | /*ID if present*/ | |
36 | if (id.length() > 0) { | |
37 | f.dump_string("ID", id); | |
38 | } | |
39 | /*AllowedMethods*/ | |
40 | if (allowed_methods & RGW_CORS_GET) | |
41 | f.dump_string("AllowedMethod", "GET"); | |
42 | if (allowed_methods & RGW_CORS_PUT) | |
43 | f.dump_string("AllowedMethod", "PUT"); | |
44 | if (allowed_methods & RGW_CORS_DELETE) | |
45 | f.dump_string("AllowedMethod", "DELETE"); | |
46 | if (allowed_methods & RGW_CORS_HEAD) | |
47 | f.dump_string("AllowedMethod", "HEAD"); | |
48 | if (allowed_methods & RGW_CORS_POST) | |
49 | f.dump_string("AllowedMethod", "POST"); | |
50 | if (allowed_methods & RGW_CORS_COPY) | |
51 | f.dump_string("AllowedMethod", "COPY"); | |
52 | /*AllowedOrigins*/ | |
53 | for(set<string>::iterator it = allowed_origins.begin(); | |
54 | it != allowed_origins.end(); | |
55 | ++it) { | |
56 | string host = *it; | |
57 | f.dump_string("AllowedOrigin", host); | |
58 | } | |
59 | /*AllowedHeader*/ | |
60 | for(set<string>::iterator it = allowed_hdrs.begin(); | |
61 | it != allowed_hdrs.end(); ++it) { | |
62 | f.dump_string("AllowedHeader", *it); | |
63 | } | |
64 | /*MaxAgeSeconds*/ | |
65 | if (max_age != CORS_MAX_AGE_INVALID) { | |
66 | f.dump_unsigned("MaxAgeSeconds", max_age); | |
67 | } | |
68 | /*ExposeHeader*/ | |
69 | for(list<string>::iterator it = exposable_hdrs.begin(); | |
70 | it != exposable_hdrs.end(); ++it) { | |
71 | f.dump_string("ExposeHeader", *it); | |
72 | } | |
73 | f.close_section(); | |
74 | } | |
75 | ||
76 | bool RGWCORSRule_S3::xml_end(const char *el) { | |
77 | XMLObjIter iter = find("AllowedMethod"); | |
78 | XMLObj *obj; | |
79 | /*Check all the allowedmethods*/ | |
80 | obj = iter.get_next(); | |
81 | if (obj) { | |
82 | for( ; obj; obj = iter.get_next()) { | |
83 | const char *s = obj->get_data().c_str(); | |
b3b6e05e | 84 | ldpp_dout(dpp, 10) << "RGWCORSRule::xml_end, el : " << el << ", data : " << s << dendl; |
7c673cae FG |
85 | if (strcasecmp(s, "GET") == 0) { |
86 | allowed_methods |= RGW_CORS_GET; | |
87 | } else if (strcasecmp(s, "POST") == 0) { | |
88 | allowed_methods |= RGW_CORS_POST; | |
89 | } else if (strcasecmp(s, "DELETE") == 0) { | |
90 | allowed_methods |= RGW_CORS_DELETE; | |
91 | } else if (strcasecmp(s, "HEAD") == 0) { | |
92 | allowed_methods |= RGW_CORS_HEAD; | |
93 | } else if (strcasecmp(s, "PUT") == 0) { | |
94 | allowed_methods |= RGW_CORS_PUT; | |
95 | } else if (strcasecmp(s, "COPY") == 0) { | |
96 | allowed_methods |= RGW_CORS_COPY; | |
97 | } else { | |
98 | return false; | |
99 | } | |
100 | } | |
101 | } | |
102 | /*Check the id's len, it should be less than 255*/ | |
103 | XMLObj *xml_id = find_first("ID"); | |
104 | if (xml_id != NULL) { | |
105 | string data = xml_id->get_data(); | |
106 | if (data.length() > 255) { | |
b3b6e05e | 107 | ldpp_dout(dpp, 0) << "RGWCORSRule has id of length greater than 255" << dendl; |
7c673cae FG |
108 | return false; |
109 | } | |
b3b6e05e | 110 | ldpp_dout(dpp, 10) << "RGWCORRule id : " << data << dendl; |
7c673cae FG |
111 | id = data; |
112 | } | |
113 | /*Check if there is atleast one AllowedOrigin*/ | |
114 | iter = find("AllowedOrigin"); | |
115 | if (!(obj = iter.get_next())) { | |
b3b6e05e | 116 | ldpp_dout(dpp, 0) << "RGWCORSRule does not have even one AllowedOrigin" << dendl; |
7c673cae FG |
117 | return false; |
118 | } | |
119 | for( ; obj; obj = iter.get_next()) { | |
b3b6e05e | 120 | ldpp_dout(dpp, 10) << "RGWCORSRule - origin : " << obj->get_data() << dendl; |
7c673cae FG |
121 | /*Just take the hostname*/ |
122 | string host = obj->get_data(); | |
123 | if (validate_name_string(host) != 0) | |
124 | return false; | |
125 | allowed_origins.insert(allowed_origins.end(), host); | |
126 | } | |
127 | /*Check of max_age*/ | |
128 | iter = find("MaxAgeSeconds"); | |
129 | if ((obj = iter.get_next())) { | |
130 | char *end = NULL; | |
131 | ||
132 | unsigned long long ull = strtoull(obj->get_data().c_str(), &end, 10); | |
11fdf7f2 | 133 | if (*end != '\0') { |
b3b6e05e | 134 | ldpp_dout(dpp, 0) << "RGWCORSRule's MaxAgeSeconds " << obj->get_data() << " is an invalid integer" << dendl; |
11fdf7f2 TL |
135 | return false; |
136 | } | |
7c673cae FG |
137 | if (ull >= 0x100000000ull) { |
138 | max_age = CORS_MAX_AGE_INVALID; | |
139 | } else { | |
140 | max_age = (uint32_t)ull; | |
141 | } | |
b3b6e05e | 142 | ldpp_dout(dpp, 10) << "RGWCORSRule : max_age : " << max_age << dendl; |
7c673cae FG |
143 | } |
144 | /*Check and update ExposeHeader*/ | |
145 | iter = find("ExposeHeader"); | |
146 | if ((obj = iter.get_next())) { | |
147 | for(; obj; obj = iter.get_next()) { | |
b3b6e05e | 148 | ldpp_dout(dpp, 10) << "RGWCORSRule - exp_hdr : " << obj->get_data() << dendl; |
7c673cae FG |
149 | exposable_hdrs.push_back(obj->get_data()); |
150 | } | |
151 | } | |
152 | /*Check and update AllowedHeader*/ | |
153 | iter = find("AllowedHeader"); | |
154 | if ((obj = iter.get_next())) { | |
155 | for(; obj; obj = iter.get_next()) { | |
b3b6e05e | 156 | ldpp_dout(dpp, 10) << "RGWCORSRule - allowed_hdr : " << obj->get_data() << dendl; |
7c673cae FG |
157 | string s = obj->get_data(); |
158 | if (validate_name_string(s) != 0) | |
159 | return false; | |
160 | allowed_hdrs.insert(allowed_hdrs.end(), s); | |
161 | } | |
162 | } | |
163 | return true; | |
164 | } | |
165 | ||
166 | void RGWCORSConfiguration_S3::to_xml(ostream& out) { | |
167 | XMLFormatter f; | |
168 | f.open_object_section_in_ns("CORSConfiguration", XMLNS_AWS_S3); | |
169 | for(list<RGWCORSRule>::iterator it = rules.begin(); | |
170 | it != rules.end(); ++it) { | |
171 | (static_cast<RGWCORSRule_S3 &>(*it)).to_xml(f); | |
172 | } | |
173 | f.close_section(); | |
174 | f.flush(out); | |
175 | } | |
176 | ||
177 | bool RGWCORSConfiguration_S3::xml_end(const char *el) { | |
178 | XMLObjIter iter = find("CORSRule"); | |
179 | RGWCORSRule_S3 *obj; | |
180 | if (!(obj = static_cast<RGWCORSRule_S3 *>(iter.get_next()))) { | |
b3b6e05e | 181 | ldpp_dout(dpp, 0) << "CORSConfiguration should have atleast one CORSRule" << dendl; |
7c673cae FG |
182 | return false; |
183 | } | |
184 | for(; obj; obj = static_cast<RGWCORSRule_S3 *>(iter.get_next())) { | |
185 | rules.push_back(*obj); | |
186 | } | |
187 | return true; | |
188 | } | |
189 | ||
190 | class CORSRuleID_S3 : public XMLObj { | |
191 | public: | |
192 | CORSRuleID_S3() {} | |
193 | ~CORSRuleID_S3() override {} | |
194 | }; | |
195 | ||
196 | class CORSRuleAllowedOrigin_S3 : public XMLObj { | |
197 | public: | |
198 | CORSRuleAllowedOrigin_S3() {} | |
199 | ~CORSRuleAllowedOrigin_S3() override {} | |
200 | }; | |
201 | ||
202 | class CORSRuleAllowedMethod_S3 : public XMLObj { | |
203 | public: | |
204 | CORSRuleAllowedMethod_S3() {} | |
205 | ~CORSRuleAllowedMethod_S3() override {} | |
206 | }; | |
207 | ||
208 | class CORSRuleAllowedHeader_S3 : public XMLObj { | |
209 | public: | |
210 | CORSRuleAllowedHeader_S3() {} | |
211 | ~CORSRuleAllowedHeader_S3() override {} | |
212 | }; | |
213 | ||
214 | class CORSRuleMaxAgeSeconds_S3 : public XMLObj { | |
215 | public: | |
216 | CORSRuleMaxAgeSeconds_S3() {} | |
217 | ~CORSRuleMaxAgeSeconds_S3() override {} | |
218 | }; | |
219 | ||
220 | class CORSRuleExposeHeader_S3 : public XMLObj { | |
221 | public: | |
222 | CORSRuleExposeHeader_S3() {} | |
223 | ~CORSRuleExposeHeader_S3() override {} | |
224 | }; | |
225 | ||
226 | XMLObj *RGWCORSXMLParser_S3::alloc_obj(const char *el) { | |
227 | if (strcmp(el, "CORSConfiguration") == 0) { | |
b3b6e05e | 228 | return new RGWCORSConfiguration_S3(dpp); |
7c673cae | 229 | } else if (strcmp(el, "CORSRule") == 0) { |
b3b6e05e | 230 | return new RGWCORSRule_S3(dpp); |
7c673cae FG |
231 | } else if (strcmp(el, "ID") == 0) { |
232 | return new CORSRuleID_S3; | |
233 | } else if (strcmp(el, "AllowedOrigin") == 0) { | |
234 | return new CORSRuleAllowedOrigin_S3; | |
235 | } else if (strcmp(el, "AllowedMethod") == 0) { | |
236 | return new CORSRuleAllowedMethod_S3; | |
237 | } else if (strcmp(el, "AllowedHeader") == 0) { | |
238 | return new CORSRuleAllowedHeader_S3; | |
239 | } else if (strcmp(el, "MaxAgeSeconds") == 0) { | |
240 | return new CORSRuleMaxAgeSeconds_S3; | |
241 | } else if (strcmp(el, "ExposeHeader") == 0) { | |
242 | return new CORSRuleExposeHeader_S3; | |
243 | } | |
244 | return NULL; | |
245 | } | |
246 |