]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/MonCap.cc
b5c5ab6a5bf32763d31558f13c296d20fe7a916a
[ceph.git] / ceph / src / mon / MonCap.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <boost/config/warning_disable.hpp>
16 #include <boost/spirit/include/qi_uint.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/fusion/include/std_pair.hpp>
19 #include <boost/spirit/include/phoenix.hpp>
20 #include <boost/fusion/adapted/struct/adapt_struct.hpp>
21 #include <boost/fusion/include/adapt_struct.hpp>
22 #include <boost/algorithm/string/predicate.hpp>
23
24 #include "MonCap.h"
25 #include "include/stringify.h"
26 #include "include/ipaddr.h"
27 #include "common/debug.h"
28 #include "common/Formatter.h"
29
30 #include <algorithm>
31 #include <regex>
32
33 #include "include/ceph_assert.h"
34
35 using std::list;
36 using std::map;
37 using std::ostream;
38 using std::pair;
39 using std::string;
40 using std::vector;
41
42 using ceph::bufferlist;
43 using ceph::Formatter;
44
45 static inline bool is_not_alnum_space(char c)
46 {
47 return !(isalpha(c) || isdigit(c) || (c == '-') || (c == '_'));
48 }
49
50 static std::string maybe_quote_string(const std::string& str)
51 {
52 if (find_if(str.begin(), str.end(), is_not_alnum_space) == str.end())
53 return str;
54 return string("\"") + str + string("\"");
55 }
56
57 #define dout_subsys ceph_subsys_mon
58
59 ostream& operator<<(ostream& out, const mon_rwxa_t& p)
60 {
61 if (p == MON_CAP_ANY)
62 return out << "*";
63
64 if (p & MON_CAP_R)
65 out << "r";
66 if (p & MON_CAP_W)
67 out << "w";
68 if (p & MON_CAP_X)
69 out << "x";
70 return out;
71 }
72
73 ostream& operator<<(ostream& out, const StringConstraint& c)
74 {
75 switch (c.match_type) {
76 case StringConstraint::MATCH_TYPE_EQUAL:
77 return out << "value " << c.value;
78 case StringConstraint::MATCH_TYPE_PREFIX:
79 return out << "prefix " << c.value;
80 case StringConstraint::MATCH_TYPE_REGEX:
81 return out << "regex " << c.value;
82 default:
83 break;
84 }
85 return out;
86 }
87
88 ostream& operator<<(ostream& out, const MonCapGrant& m)
89 {
90 out << "allow";
91 if (m.service.length()) {
92 out << " service " << maybe_quote_string(m.service);
93 }
94 if (m.command.length()) {
95 out << " command " << maybe_quote_string(m.command);
96 if (!m.command_args.empty()) {
97 out << " with";
98 for (auto p = m.command_args.begin();
99 p != m.command_args.end();
100 ++p) {
101 switch (p->second.match_type) {
102 case StringConstraint::MATCH_TYPE_EQUAL:
103 out << " " << maybe_quote_string(p->first) << "="
104 << maybe_quote_string(p->second.value);
105 break;
106 case StringConstraint::MATCH_TYPE_PREFIX:
107 out << " " << maybe_quote_string(p->first) << " prefix "
108 << maybe_quote_string(p->second.value);
109 break;
110 case StringConstraint::MATCH_TYPE_REGEX:
111 out << " " << maybe_quote_string(p->first) << " regex "
112 << maybe_quote_string(p->second.value);
113 break;
114 default:
115 break;
116 }
117 }
118 }
119 }
120 if (m.profile.length()) {
121 out << " profile " << maybe_quote_string(m.profile);
122 }
123 if (m.allow != 0)
124 out << " " << m.allow;
125 if (m.network.size())
126 out << " network " << m.network;
127 return out;
128 }
129
130
131 // <magic>
132 // fusion lets us easily populate structs via the qi parser.
133
134 typedef map<string,StringConstraint> kvmap;
135
136 BOOST_FUSION_ADAPT_STRUCT(MonCapGrant,
137 (std::string, service)
138 (std::string, profile)
139 (std::string, command)
140 (kvmap, command_args)
141 (mon_rwxa_t, allow)
142 (std::string, network))
143
144 BOOST_FUSION_ADAPT_STRUCT(StringConstraint,
145 (StringConstraint::MatchType, match_type)
146 (std::string, value))
147
148 // </magic>
149
150 void MonCapGrant::parse_network()
151 {
152 network_valid = ::parse_network(network.c_str(), &network_parsed,
153 &network_prefix);
154 }
155
156 void MonCapGrant::expand_profile(const EntityName& name) const
157 {
158 // only generate this list once
159 if (!profile_grants.empty())
160 return;
161
162 if (profile == "read-only") {
163 // grants READ-ONLY caps monitor-wide
164 // 'auth' requires MON_CAP_X even for RO, which we do not grant here.
165 profile_grants.push_back(mon_rwxa_t(MON_CAP_R));
166 return;
167 }
168
169 if (profile == "read-write") {
170 // grants READ-WRITE caps monitor-wide
171 // 'auth' requires MON_CAP_X for all operations, which we do not grant.
172 profile_grants.push_back(mon_rwxa_t(MON_CAP_R | MON_CAP_W));
173 return;
174 }
175
176 if (profile == "mon") {
177 profile_grants.push_back(MonCapGrant("mon", MON_CAP_ALL));
178 profile_grants.push_back(MonCapGrant("log", MON_CAP_ALL));
179 }
180 if (profile == "osd") {
181 profile_grants.push_back(MonCapGrant("osd", MON_CAP_ALL));
182 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
183 profile_grants.push_back(MonCapGrant("pg", MON_CAP_R | MON_CAP_W));
184 profile_grants.push_back(MonCapGrant("log", MON_CAP_W));
185 }
186 if (profile == "mds") {
187 profile_grants.push_back(MonCapGrant("mds", MON_CAP_ALL));
188 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
189 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R));
190 // This command grant is checked explicitly in MRemoveSnaps handling
191 profile_grants.push_back(MonCapGrant("osd pool rmsnap"));
192 profile_grants.push_back(MonCapGrant("osd blacklist"));
193 profile_grants.push_back(MonCapGrant("log", MON_CAP_W));
194 }
195 if (profile == "mgr") {
196 profile_grants.push_back(MonCapGrant("mgr", MON_CAP_ALL));
197 profile_grants.push_back(MonCapGrant("log", MON_CAP_R | MON_CAP_W));
198 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R | MON_CAP_W));
199 profile_grants.push_back(MonCapGrant("mds", MON_CAP_R | MON_CAP_W));
200 profile_grants.push_back(MonCapGrant("fs", MON_CAP_R | MON_CAP_W));
201 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R | MON_CAP_W));
202 profile_grants.push_back(MonCapGrant("auth", MON_CAP_R | MON_CAP_X));
203 profile_grants.push_back(MonCapGrant("config-key", MON_CAP_R | MON_CAP_W));
204 profile_grants.push_back(MonCapGrant("config", MON_CAP_R | MON_CAP_W));
205 // cephadm orchestrator provisions new daemon keys and updates caps
206 profile_grants.push_back(MonCapGrant("auth get-or-create"));
207 profile_grants.push_back(MonCapGrant("auth caps"));
208 profile_grants.push_back(MonCapGrant("auth rm"));
209 // tell commands (this is a bit of a kludge)
210 profile_grants.push_back(MonCapGrant("smart"));
211 }
212 if (profile == "osd" || profile == "mds" || profile == "mon" ||
213 profile == "mgr") {
214 StringConstraint constraint(StringConstraint::MATCH_TYPE_PREFIX,
215 string("daemon-private/") + stringify(name) +
216 string("/"));
217 std::string prefix = string("daemon-private/") + stringify(name) + string("/");
218 profile_grants.push_back(MonCapGrant("config-key get", "key", constraint));
219 profile_grants.push_back(MonCapGrant("config-key put", "key", constraint));
220 profile_grants.push_back(MonCapGrant("config-key set", "key", constraint));
221 profile_grants.push_back(MonCapGrant("config-key exists", "key", constraint));
222 profile_grants.push_back(MonCapGrant("config-key delete", "key", constraint));
223 }
224 if (profile == "bootstrap-osd") {
225 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R)); // read monmap
226 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R)); // read osdmap
227 profile_grants.push_back(MonCapGrant("mon getmap"));
228 profile_grants.push_back(MonCapGrant("osd new"));
229 profile_grants.push_back(MonCapGrant("osd purge-new"));
230 }
231 if (profile == "bootstrap-mds") {
232 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R)); // read monmap
233 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R)); // read osdmap
234 profile_grants.push_back(MonCapGrant("mon getmap"));
235 profile_grants.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mds keys
236 profile_grants.back().command_args["entity"] = StringConstraint(
237 StringConstraint::MATCH_TYPE_PREFIX, "mds.");
238 profile_grants.back().command_args["caps_mon"] = StringConstraint(
239 StringConstraint::MATCH_TYPE_EQUAL, "allow profile mds");
240 profile_grants.back().command_args["caps_osd"] = StringConstraint(
241 StringConstraint::MATCH_TYPE_EQUAL, "allow rwx");
242 profile_grants.back().command_args["caps_mds"] = StringConstraint(
243 StringConstraint::MATCH_TYPE_EQUAL, "allow");
244 }
245 if (profile == "bootstrap-mgr") {
246 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R)); // read monmap
247 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R)); // read osdmap
248 profile_grants.push_back(MonCapGrant("mon getmap"));
249 profile_grants.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mgr keys
250 profile_grants.back().command_args["entity"] = StringConstraint(
251 StringConstraint::MATCH_TYPE_PREFIX, "mgr.");
252 profile_grants.back().command_args["caps_mon"] = StringConstraint(
253 StringConstraint::MATCH_TYPE_EQUAL, "allow profile mgr");
254 }
255 if (profile == "bootstrap-rgw") {
256 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R)); // read monmap
257 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R)); // read osdmap
258 profile_grants.push_back(MonCapGrant("mon getmap"));
259 profile_grants.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mds keys
260 profile_grants.back().command_args["entity"] = StringConstraint(
261 StringConstraint::MATCH_TYPE_PREFIX, "client.rgw.");
262 profile_grants.back().command_args["caps_mon"] = StringConstraint(
263 StringConstraint::MATCH_TYPE_EQUAL, "allow rw");
264 profile_grants.back().command_args["caps_osd"] = StringConstraint(
265 StringConstraint::MATCH_TYPE_EQUAL, "allow rwx");
266 }
267 if (profile == "bootstrap-rbd" || profile == "bootstrap-rbd-mirror") {
268 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R)); // read monmap
269 profile_grants.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other rbd keys
270 profile_grants.back().command_args["entity"] = StringConstraint(
271 StringConstraint::MATCH_TYPE_PREFIX, "client.");
272 profile_grants.back().command_args["caps_mon"] = StringConstraint(
273 StringConstraint::MATCH_TYPE_EQUAL,
274 (profile == "bootstrap-rbd-mirror" ? "profile rbd-mirror" :
275 "profile rbd"));
276 profile_grants.back().command_args["caps_osd"] = StringConstraint(
277 StringConstraint::MATCH_TYPE_REGEX,
278 "^([ ,]*profile(=|[ ]+)['\"]?rbd[^ ,'\"]*['\"]?([ ]+pool(=|[ ]+)['\"]?[^,'\"]+['\"]?)?)+$");
279 }
280 if (profile == "fs-client") {
281 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
282 profile_grants.push_back(MonCapGrant("mds", MON_CAP_R));
283 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R));
284 profile_grants.push_back(MonCapGrant("pg", MON_CAP_R));
285 }
286 if (profile == "simple-rados-client") {
287 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
288 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R));
289 profile_grants.push_back(MonCapGrant("pg", MON_CAP_R));
290 }
291 if (boost::starts_with(profile, "rbd")) {
292 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
293 profile_grants.push_back(MonCapGrant("osd", MON_CAP_R));
294 profile_grants.push_back(MonCapGrant("pg", MON_CAP_R));
295
296 // exclusive lock dead-client blacklisting (IP+nonce required)
297 profile_grants.push_back(MonCapGrant("osd blacklist"));
298 profile_grants.back().command_args["blacklistop"] = StringConstraint(
299 StringConstraint::MATCH_TYPE_EQUAL, "add");
300 profile_grants.back().command_args["addr"] = StringConstraint(
301 StringConstraint::MATCH_TYPE_REGEX, "^[^/]+/[0-9]+$");
302
303 }
304 if (profile == "rbd-mirror") {
305 StringConstraint constraint(StringConstraint::MATCH_TYPE_PREFIX,
306 "rbd/mirror/");
307 profile_grants.push_back(MonCapGrant("config-key get", "key", constraint));
308 } else if (profile == "rbd-mirror-peer") {
309 StringConstraint constraint(StringConstraint::MATCH_TYPE_REGEX,
310 "rbd/mirror/[^/]+");
311 profile_grants.push_back(MonCapGrant("config-key get", "key", constraint));
312
313 constraint = StringConstraint(StringConstraint::MATCH_TYPE_PREFIX,
314 "rbd/mirror/peer/");
315 profile_grants.push_back(MonCapGrant("config-key set", "key", constraint));
316 }
317 else if (profile == "crash") {
318 // TODO: we could limit this to getting the monmap and mgrmap...
319 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
320 }
321 if (profile == "role-definer") {
322 // grants ALL caps to the auth subsystem, read-only on the
323 // monitor subsystem and nothing else.
324 profile_grants.push_back(MonCapGrant("mon", MON_CAP_R));
325 profile_grants.push_back(MonCapGrant("auth", MON_CAP_ALL));
326 }
327 }
328
329 mon_rwxa_t MonCapGrant::get_allowed(CephContext *cct,
330 EntityName name,
331 const std::string& s, const std::string& c,
332 const map<string,string>& c_args) const
333 {
334 if (profile.length()) {
335 expand_profile(name);
336 mon_rwxa_t a;
337 for (auto p = profile_grants.begin();
338 p != profile_grants.end(); ++p)
339 a = a | p->get_allowed(cct, name, s, c, c_args);
340 return a;
341 }
342 if (service.length()) {
343 if (service != s)
344 return 0;
345 return allow;
346 }
347 if (command.length()) {
348 if (command != c)
349 return 0;
350 for (map<string,StringConstraint>::const_iterator p = command_args.begin(); p != command_args.end(); ++p) {
351 map<string,string>::const_iterator q = c_args.find(p->first);
352 // argument must be present if a constraint exists
353 if (q == c_args.end())
354 return 0;
355 switch (p->second.match_type) {
356 case StringConstraint::MATCH_TYPE_EQUAL:
357 if (p->second.value != q->second)
358 return 0;
359 break;
360 case StringConstraint::MATCH_TYPE_PREFIX:
361 if (q->second.find(p->second.value) != 0)
362 return 0;
363 break;
364 case StringConstraint::MATCH_TYPE_REGEX:
365 try {
366 std::regex pattern(
367 p->second.value, std::regex::extended);
368 if (!std::regex_match(q->second, pattern))
369 return 0;
370 } catch(const std::regex_error&) {
371 return 0;
372 }
373 break;
374 default:
375 break;
376 }
377 }
378 return MON_CAP_ALL;
379 }
380 // we don't allow config-key service to be accessed with blanket caps other
381 // than '*' (i.e., 'any'), and that should have been checked by the caller
382 // via 'is_allow_all()'.
383 if (s == "config-key") {
384 return 0;
385 }
386 return allow;
387 }
388
389 ostream& operator<<(ostream&out, const MonCap& m)
390 {
391 for (vector<MonCapGrant>::const_iterator p = m.grants.begin(); p != m.grants.end(); ++p) {
392 if (p != m.grants.begin())
393 out << ", ";
394 out << *p;
395 }
396 return out;
397 }
398
399 bool MonCap::is_allow_all() const
400 {
401 for (vector<MonCapGrant>::const_iterator p = grants.begin(); p != grants.end(); ++p)
402 if (p->is_allow_all())
403 return true;
404 return false;
405 }
406
407 void MonCap::set_allow_all()
408 {
409 grants.clear();
410 grants.push_back(MonCapGrant(MON_CAP_ANY));
411 text = "allow *";
412 }
413
414 bool MonCap::is_capable(
415 CephContext *cct,
416 EntityName name,
417 const string& service,
418 const string& command, const map<string,string>& command_args,
419 bool op_may_read, bool op_may_write, bool op_may_exec,
420 const entity_addr_t& addr) const
421 {
422 if (cct)
423 ldout(cct, 20) << "is_capable service=" << service << " command=" << command
424 << (op_may_read ? " read":"")
425 << (op_may_write ? " write":"")
426 << (op_may_exec ? " exec":"")
427 << " addr " << addr
428 << " on cap " << *this
429 << dendl;
430
431 mon_rwxa_t allow = 0;
432 for (vector<MonCapGrant>::const_iterator p = grants.begin();
433 p != grants.end(); ++p) {
434 if (cct)
435 ldout(cct, 20) << " allow so far " << allow << ", doing grant " << *p
436 << dendl;
437
438 if (p->network.size() &&
439 (!p->network_valid ||
440 !network_contains(p->network_parsed,
441 p->network_prefix,
442 addr))) {
443 continue;
444 }
445
446 if (p->is_allow_all()) {
447 if (cct)
448 ldout(cct, 20) << " allow all" << dendl;
449 return true;
450 }
451
452 // check enumerated caps
453 allow = allow | p->get_allowed(cct, name, service, command, command_args);
454 if ((!op_may_read || (allow & MON_CAP_R)) &&
455 (!op_may_write || (allow & MON_CAP_W)) &&
456 (!op_may_exec || (allow & MON_CAP_X))) {
457 if (cct)
458 ldout(cct, 20) << " match" << dendl;
459 return true;
460 }
461 }
462 return false;
463 }
464
465 void MonCap::encode(bufferlist& bl) const
466 {
467 ENCODE_START(4, 4, bl); // legacy MonCaps was 3, 3
468 encode(text, bl);
469 ENCODE_FINISH(bl);
470 }
471
472 void MonCap::decode(bufferlist::const_iterator& bl)
473 {
474 std::string s;
475 DECODE_START(4, bl);
476 decode(s, bl);
477 DECODE_FINISH(bl);
478 parse(s, NULL);
479 }
480
481 void MonCap::dump(Formatter *f) const
482 {
483 f->dump_string("text", text);
484 }
485
486 void MonCap::generate_test_instances(list<MonCap*>& ls)
487 {
488 ls.push_back(new MonCap);
489 ls.push_back(new MonCap);
490 ls.back()->parse("allow *");
491 ls.push_back(new MonCap);
492 ls.back()->parse("allow rwx");
493 ls.push_back(new MonCap);
494 ls.back()->parse("allow service foo x");
495 ls.push_back(new MonCap);
496 ls.back()->parse("allow command bar x");
497 ls.push_back(new MonCap);
498 ls.back()->parse("allow service foo r, allow command bar x");
499 ls.push_back(new MonCap);
500 ls.back()->parse("allow command bar with k1=v1 x");
501 ls.push_back(new MonCap);
502 ls.back()->parse("allow command bar with k1=v1 k2=v2 x");
503 }
504
505 // grammar
506 namespace qi = boost::spirit::qi;
507 namespace ascii = boost::spirit::ascii;
508 namespace phoenix = boost::phoenix;
509
510
511 template <typename Iterator>
512 struct MonCapParser : qi::grammar<Iterator, MonCap()>
513 {
514 MonCapParser() : MonCapParser::base_type(moncap)
515 {
516 using qi::char_;
517 using qi::int_;
518 using qi::ulong_long;
519 using qi::lexeme;
520 using qi::alnum;
521 using qi::_val;
522 using qi::_1;
523 using qi::_2;
524 using qi::_3;
525 using qi::eps;
526 using qi::lit;
527
528 quoted_string %=
529 lexeme['"' >> +(char_ - '"') >> '"'] |
530 lexeme['\'' >> +(char_ - '\'') >> '\''];
531 unquoted_word %= +char_("a-zA-Z0-9_./-");
532 str %= quoted_string | unquoted_word;
533 network_str %= +char_("/.:a-fA-F0-9][");
534
535 spaces = +(lit(' ') | lit('\n') | lit('\t'));
536
537 // command := command[=]cmd [k1=v1 k2=v2 ...]
538 str_match = '=' >> qi::attr(StringConstraint::MATCH_TYPE_EQUAL) >> str;
539 str_prefix = spaces >> lit("prefix") >> spaces >>
540 qi::attr(StringConstraint::MATCH_TYPE_PREFIX) >> str;
541 str_regex = spaces >> lit("regex") >> spaces >>
542 qi::attr(StringConstraint::MATCH_TYPE_REGEX) >> str;
543 kv_pair = str >> (str_match | str_prefix | str_regex);
544 kv_map %= kv_pair >> *(spaces >> kv_pair);
545 command_match = -spaces >> lit("allow") >> spaces >> lit("command") >> (lit('=') | spaces)
546 >> qi::attr(string()) >> qi::attr(string())
547 >> str
548 >> -(spaces >> lit("with") >> spaces >> kv_map)
549 >> qi::attr(0)
550 >> -(spaces >> lit("network") >> spaces >> network_str);
551
552 // service foo rwxa
553 service_match %= -spaces >> lit("allow") >> spaces >> lit("service") >> (lit('=') | spaces)
554 >> str >> qi::attr(string()) >> qi::attr(string())
555 >> qi::attr(map<string,StringConstraint>())
556 >> spaces >> rwxa
557 >> -(spaces >> lit("network") >> spaces >> network_str);
558
559 // profile foo
560 profile_match %= -spaces >> -(lit("allow") >> spaces)
561 >> lit("profile") >> (lit('=') | spaces)
562 >> qi::attr(string())
563 >> str
564 >> qi::attr(string())
565 >> qi::attr(map<string,StringConstraint>())
566 >> qi::attr(0)
567 >> -(spaces >> lit("network") >> spaces >> network_str);
568
569 // rwxa
570 rwxa_match %= -spaces >> lit("allow") >> spaces
571 >> qi::attr(string()) >> qi::attr(string()) >> qi::attr(string())
572 >> qi::attr(map<string,StringConstraint>())
573 >> rwxa
574 >> -(spaces >> lit("network") >> spaces >> network_str);
575
576 // rwxa := * | [r][w][x]
577 rwxa =
578 (lit("*")[_val = MON_CAP_ANY]) |
579 (lit("all")[_val = MON_CAP_ANY]) |
580 ( eps[_val = 0] >>
581 ( lit('r')[_val |= MON_CAP_R] ||
582 lit('w')[_val |= MON_CAP_W] ||
583 lit('x')[_val |= MON_CAP_X]
584 )
585 );
586
587 // grant := allow ...
588 grant = -spaces >> (rwxa_match | profile_match | service_match | command_match) >> -spaces;
589
590 // moncap := grant [grant ...]
591 grants %= (grant % (*lit(' ') >> (lit(';') | lit(',')) >> *lit(' ')));
592 moncap = grants [_val = phoenix::construct<MonCap>(_1)];
593
594 }
595 qi::rule<Iterator> spaces;
596 qi::rule<Iterator, unsigned()> rwxa;
597 qi::rule<Iterator, string()> quoted_string;
598 qi::rule<Iterator, string()> unquoted_word;
599 qi::rule<Iterator, string()> str, network_str;
600
601 qi::rule<Iterator, StringConstraint()> str_match, str_prefix, str_regex;
602 qi::rule<Iterator, pair<string, StringConstraint>()> kv_pair;
603 qi::rule<Iterator, map<string, StringConstraint>()> kv_map;
604
605 qi::rule<Iterator, MonCapGrant()> rwxa_match;
606 qi::rule<Iterator, MonCapGrant()> command_match;
607 qi::rule<Iterator, MonCapGrant()> service_match;
608 qi::rule<Iterator, MonCapGrant()> profile_match;
609 qi::rule<Iterator, MonCapGrant()> grant;
610 qi::rule<Iterator, std::vector<MonCapGrant>()> grants;
611 qi::rule<Iterator, MonCap()> moncap;
612 };
613
614 bool MonCap::parse(const string& str, ostream *err)
615 {
616 auto iter = str.begin();
617 auto end = str.end();
618
619 MonCapParser<string::const_iterator> exp;
620 bool r = qi::parse(iter, end, exp, *this);
621 if (r && iter == end) {
622 text = str;
623 for (auto& g : grants) {
624 g.parse_network();
625 }
626 return true;
627 }
628
629 // Make sure no grants are kept after parsing failed!
630 grants.clear();
631
632 if (err) {
633 if (iter != end)
634 *err << "mon capability parse failed, stopped at '"
635 << std::string(iter, end)
636 << "' of '" << str << "'";
637 else
638 *err << "mon capability parse failed, stopped at end of '" << str << "'";
639 }
640
641 return false;
642 }
643