summaryrefslogtreecommitdiffstats
path: root/src/common/cmdparse.cc
blob: b8cf316340de5c8b0e685eee7c56ae03b018ffbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2013 Inktank Storage, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "include/common_fwd.h"
#include "common/cmdparse.h"
#include "common/Formatter.h"
#include "common/debug.h"
#include "common/strtol.h"
#include "json_spirit/json_spirit.h"

using std::is_same_v;
using std::ostringstream;
using std::string;
using std::stringstream;
using std::string_view;
using std::vector;

/**
 * Given a cmddesc like "foo baz name=bar,type=CephString",
 * return the prefix "foo baz".
 */
namespace TOPNSPC::common {
std::string cmddesc_get_prefix(const std::string_view &cmddesc)
{
  string tmp(cmddesc); // FIXME: stringstream ctor can't take string_view :(
  stringstream ss(tmp);
  std::string word;
  std::ostringstream result;
  bool first = true;
  while (std::getline(ss, word, ' ')) {
    if (word.find_first_of(",=") != string::npos) {
      break;
    }

    if (!first) {
      result << " ";
    }
    result << word;
    first = false;
  }

  return result.str();
}

using arg_desc_t = std::map<std::string_view, std::string_view>;

// Snarf up all the key=val,key=val pairs, put 'em in a dict.
arg_desc_t cmddesc_get_args(const string_view cmddesc)
{
  arg_desc_t arg_desc;
  for_each_substr(cmddesc, ",", [&](auto kv) {
      // key=value; key by itself implies value is bool true
      // name="name" means arg dict will be titled 'name'
      auto equal = kv.find('=');
      if (equal == kv.npos) {
	// it should be the command
	return;
      }
      auto key = kv.substr(0, equal);
      auto val = kv.substr(equal + 1);
      arg_desc[key] = val;
    });
  return arg_desc;
}

std::string cmddesc_get_prenautilus_compat(const std::string &cmddesc)
{
  std::vector<std::string> out;
  stringstream ss(cmddesc);
  std::string word;
  bool changed = false;
  while (std::getline(ss, word, ' ')) {
    // if no , or =, must be a plain word to put out
    if (word.find_first_of(",=") == string::npos) {
      out.push_back(word);
      continue;
    }
    auto desckv = cmddesc_get_args(word);
    auto j = desckv.find("type");
    if (j != desckv.end() && j->second == "CephBool") {
      // Instruct legacy clients or mons to send --foo-bar string in place
      // of a 'true'/'false' value
      std::ostringstream oss;
      oss << std::string("--") << desckv["name"];
      std::string val = oss.str();
      std::replace(val.begin(), val.end(), '_', '-');
      desckv["type"] = "CephChoices";
      desckv["strings"] = val;
      std::ostringstream fss;
      for (auto k = desckv.begin(); k != desckv.end(); ++k) {
	if (k != desckv.begin()) {
	  fss << ",";
	}
	fss << k->first << "=" << k->second;
      }
      out.push_back(fss.str());
      changed = true;
    } else {
      out.push_back(word);
    }
  }
  if (!changed) {
    return cmddesc;
  }
  std::string o;
  for (auto i = out.begin(); i != out.end(); ++i) {
    if (i != out.begin()) {
      o += " ";
    }
    o += *i;
  }
  return o;
}

/**
 * Read a command description list out of cmd, and dump it to f.
 * A signature description is a set of space-separated words;
 * see MonCommands.h for more info.
 */

void
dump_cmd_to_json(Formatter *f, uint64_t features, const string& cmd)
{
  // put whole command signature in an already-opened container
  // elements are: "name", meaning "the typeless name that means a literal"
  // an object {} with key:value pairs representing an argument

  stringstream ss(cmd);
  std::string word;

  while (std::getline(ss, word, ' ')) {
    // if no , or =, must be a plain word to put out
    if (word.find_first_of(",=") == string::npos) {
      f->dump_string("arg", word);
      continue;
    }
    // accumulate descriptor keywords in desckv
    auto desckv = cmddesc_get_args(word);
    // name the individual desc object based on the name key
    f->open_object_section(desckv["name"]);

    // Compatibility for pre-nautilus clients that don't know about CephBool
    std::string val;
    if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
      auto i = desckv.find("type");
      if (i != desckv.end() && i->second == "CephBool") {
        // Instruct legacy clients to send --foo-bar string in place
        // of a 'true'/'false' value
        std::ostringstream oss;
        oss << std::string("--") << desckv["name"];
        val = oss.str();
        std::replace(val.begin(), val.end(), '_', '-');

        desckv["type"] = "CephChoices";
        desckv["strings"] = val;
      }
    }

    // dump all the keys including name into the array
    for (auto [key, value] : desckv) {
      f->dump_string(key, value);
    }
    f->close_section(); // attribute object for individual desc
  }
}

void
dump_cmd_and_help_to_json(Formatter *jf,
			  uint64_t features,
			  const string& secname,
			  const string& cmdsig,
			  const string& helptext)
{
      jf->open_object_section(secname);
      jf->open_array_section("sig");
      dump_cmd_to_json(jf, features, cmdsig);
      jf->close_section(); // sig array
      jf->dump_string("help", helptext);
      jf->close_section(); // cmd
}

void
dump_cmddesc_to_json(Formatter *jf,
		     uint64_t features,
		     const string& secname,
		     const string& cmdsig,
		     const string& helptext,
		     const string& module,
		     const string& perm,
		     uint64_t flags)
{
      jf->open_object_section(secname);
      jf->open_array_section("sig");
      dump_cmd_to_json(jf, features, cmdsig);
      jf->close_section(); // sig array
      jf->dump_string("help", helptext);
      jf->dump_string("module", module);
      jf->dump_string("perm", perm);
      jf->dump_int("flags", flags);
      jf->close_section(); // cmd
}

void cmdmap_dump(const cmdmap_t &cmdmap, Formatter *f)
{
  ceph_assert(f != nullptr);

  class dump_visitor : public boost::static_visitor<void>
  {
    Formatter *f;
    std::string const &key;
    public:
    dump_visitor(Formatter *f_, std::string const &key_)
      : f(f_), key(key_)
    {
    }

    void operator()(const std::string &operand) const
    {
      f->dump_string(key, operand);
    }

    void operator()(const bool &operand) const
    {
      f->dump_bool(key, operand);
    }

    void operator()(const int64_t &operand) const
    {
      f->dump_int(key, operand);
    }

    void operator()(const double &operand) const
    {
      f->dump_float(key, operand);
    }

    void operator()(const std::vector<std::string> &operand) const
    {
      f->open_array_section(key);
      for (const auto& i : operand) {
        f->dump_string("item", i);
      }
      f->close_section();
    }

    void operator()(const std::vector<int64_t> &operand) const
    {
      f->open_array_section(key);
      for (const auto i : operand) {
        f->dump_int("item", i);
      }
      f->close_section();
    }

    void operator()(const std::vector<double> &operand) const
    {
      f->open_array_section(key);
      for (const auto i : operand) {
        f->dump_float("item", i);
      }
      f->close_section();
    }
  };

  //f->open_object_section("cmdmap");
  for (const auto &i : cmdmap) {
    boost::apply_visitor(dump_visitor(f, i.first), i.second);
  }
  //f->close_section();
}


/** Parse JSON in vector cmd into a map from field to map of values
 * (use mValue/mObject)
 * 'cmd' should not disappear over lifetime of map
 * 'mapp' points to the caller's map
 * 'ss' captures any errors during JSON parsing; if function returns
 * false, ss is valid */

bool
cmdmap_from_json(const vector<string>& cmd, cmdmap_t *mapp, std::ostream& ss)
{
  json_spirit::mValue v;

  string fullcmd;
  // First, join all cmd strings
  for (auto& c : cmd)
    fullcmd += c;

  try {
    if (!json_spirit::read(fullcmd, v))
      throw std::runtime_error("unparseable JSON " + fullcmd);
    if (v.type() != json_spirit::obj_type)
      throw std::runtime_error("not JSON object " + fullcmd);

    // allocate new mObject (map) to return
    // make sure all contents are simple types (not arrays or objects)
    json_spirit::mObject o = v.get_obj();
    for (auto it = o.begin(); it != o.end(); ++it) {

      // ok, marshal it into our string->cmd_vartype map, or throw an
      // exception if it's not a simple datatype.  This is kind of
      // annoying, since json_spirit has a boost::variant inside it
      // already, but it's not public.  Oh well.

      switch (it->second.type()) {

      case json_spirit::obj_type:
      default:
	throw std::runtime_error("JSON array/object not allowed " + fullcmd);
        break;

      case json_spirit::array_type:
	{
	  // array is a vector of values.  Unpack it to a vector
	  // of strings, doubles, or int64_t, the only types we handle.
	  const vector<json_spirit::mValue>& spvals = it->second.get_array();
	  if (spvals.empty()) {
	    // if an empty array is acceptable, the caller should always check for
	    // vector<string> if the expected value of "vector<int64_t>" in the
	    // cmdmap is missing.
	    (*mapp)[it->first] = vector<string>();
	  } else if (spvals.front().type() == json_spirit::str_type) {
	    vector<string> outv;
	    for (const auto& sv : spvals) {
	      if (sv.type() != json_spirit::str_type) {
		throw std::runtime_error("Can't handle arrays of multiple types");
	      }
	      outv.push_back(sv.get_str());
	    }
	    (*mapp)[it->first] = std::move(outv);
	  } else if (spvals.front().type() == json_spirit::int_type) {
	    vector<int64_t> outv;
	    for (const auto& sv : spvals) {
	      if (spvals.front().type() != json_spirit::int_type) {
		throw std::runtime_error("Can't handle arrays of multiple types");
	      }
	      outv.push_back(sv.get_int64());
	    }
	    (*mapp)[it->first] = std::move(outv);
	  } else if (spvals.front().type() == json_spirit::real_type) {
	    vector<double> outv;
	    for (const auto& sv : spvals) {
	      if (spvals.front().type() != json_spirit::real_type) {
		throw std::runtime_error("Can't handle arrays of multiple types");
	      }
	      outv.push_back(sv.get_real());
	    }
	    (*mapp)[it->first] = std::move(outv);
	  } else {
	    throw std::runtime_error("Can't handle arrays of types other than "
				     "int, string, or double");
	  }
	}
	break;
      case json_spirit::str_type:
	(*mapp)[it->first] = it->second.get_str();
	break;

      case json_spirit::bool_type:
	(*mapp)[it->first] = it->second.get_bool();
	break;

      case json_spirit::int_type:
	(*mapp)[it->first] = it->second.get_int64();
	break;

      case json_spirit::real_type:
	(*mapp)[it->first] = it->second.get_real();
	break;
      }
    }
    return true;
  } catch (const std::runtime_error &e) {
    ss << e.what();
    return false;
  }
}

class stringify_visitor : public boost::static_visitor<string>
{
  public:
    template <typename T>
    string operator()(T &operand) const
      {
	ostringstream oss;
	oss << operand;
	return oss.str();
      }
};

string 
cmd_vartype_stringify(const cmd_vartype &v)
{
  return boost::apply_visitor(stringify_visitor(), v);
}


void
handle_bad_get(CephContext *cct, const string& k, const char *tname)
{
  ostringstream errstr;
  int status;
  const char *typestr = abi::__cxa_demangle(tname, 0, 0, &status);
  if (status != 0) 
    typestr = tname;
  errstr << "bad boost::get: key " << k << " is not type " << typestr;
  lderr(cct) << errstr.str() << dendl;

  ostringstream oss;
  oss << BackTrace(1);
  lderr(cct) << oss.str() << dendl;

  if (status == 0)
    free((char *)typestr);
}

long parse_pos_long(const char *s, std::ostream *pss)
{
  if (*s == '-' || *s == '+') {
    if (pss)
      *pss << "expected numerical value, got: " << s;
    return -EINVAL;
  }

  string err;
  long r = strict_strtol(s, 10, &err);
  if ((r == 0) && !err.empty()) {
    if (pss)
      *pss << err;
    return -1;
  }
  if (r < 0) {
    if (pss)
      *pss << "unable to parse positive integer '" << s << "'";
    return -1;
  }
  return r;
}

int parse_osd_id(const char *s, std::ostream *pss)
{
  // osd.NNN?
  if (strncmp(s, "osd.", 4) == 0) {
    s += 4;
  }

  // NNN?
  ostringstream ss;
  long id = parse_pos_long(s, &ss);
  if (id < 0) {
    *pss << ss.str();
    return id;
  }
  if (id > 0xffff) {
    *pss << "osd id " << id << " is too large";
    return -ERANGE;
  }
  return id;
}

namespace {
template <typename Func>
bool find_first_in(std::string_view s, const char *delims, Func&& f)
{
  auto pos = s.find_first_not_of(delims);
  while (pos != s.npos) {
    s.remove_prefix(pos);
    auto end = s.find_first_of(delims);
    if (f(s.substr(0, end))) {
      return true;
    }
    pos = s.find_first_not_of(delims, end);
  }
  return false;
}

template<typename T>
T str_to_num(const std::string& s)
{
  if constexpr (is_same_v<T, int>) {
    return std::stoi(s);
  } else if constexpr (is_same_v<T, long>) {
    return std::stol(s);
  } else if constexpr (is_same_v<T, long long>) {
    return std::stoll(s);
  } else if constexpr (is_same_v<T, double>) {
    return std::stod(s);
  }
}

template<typename T>
bool arg_in_range(T value, const arg_desc_t& desc, std::ostream& os) {
  auto range = desc.find("range");
  if (range == desc.end()) {
    return true;
  }
  auto min_max = get_str_list(string(range->second), "|");
  auto min = str_to_num<T>(min_max.front());
  auto max = std::numeric_limits<T>::max();
  if (min_max.size() > 1) {
    max = str_to_num<T>(min_max.back());
  }
  if (value < min || value > max) {
    os << "'" << value << "' out of range: " << min_max;
    return false;
  }
  return true;
}

bool validate_str_arg(std::string_view value,
		      std::string_view type,
		      const arg_desc_t& desc,
		      std::ostream& os)
{
  if (type == "CephIPAddr") {
    entity_addr_t addr;
    if (addr.parse(string(value).c_str())) {
      return true;
    } else {
      os << "failed to parse addr '" << value << "', should be ip:[port]";
      return false;
    }
  } else if (type == "CephChoices") {
    auto choices = desc.find("strings");
    ceph_assert(choices != end(desc));
    auto strings = choices->second;
    if (find_first_in(strings, "|", [=](auto choice) {
	  return (value == choice);
	})) {
      return true;
    } else {
      os << "'" << value << "' not belong to '" << strings << "'";
      return false;
    }
  } else {
    // CephString or other types like CephPgid
    return true;
  }
}

template<bool is_vector,
	 typename T,
	 typename Value = std::conditional_t<is_vector,
					     vector<T>,
					     T>>
bool validate_arg(CephContext* cct,
		  const cmdmap_t& cmdmap,
		  const arg_desc_t& desc,
		  const std::string_view name,
		  const std::string_view type,
		  std::ostream& os)
{
  Value v;
  try {
    if (!cmd_getval(cmdmap, string(name), v)) {
      if constexpr (is_vector) {
	  // an empty list is acceptable.
	  return true;
	} else {
	if (auto req = desc.find("req");
	    req != end(desc) && req->second == "false") {
	  return true;
	} else {
	  os << "missing required parameter: '" << name << "'";
	  return false;
	}
      }
    }
  } catch (const bad_cmd_get& e) {
    return false;
  }
  auto validate = [&](const T& value) {
    if constexpr (is_same_v<std::string, T>) {
      return validate_str_arg(value, type, desc, os);
    } else if constexpr (is_same_v<int64_t, T> ||
			 is_same_v<double, T>) {
      return arg_in_range(value, desc, os);
    }
  };
  if constexpr(is_vector) {
    return find_if_not(begin(v), end(v), validate) == end(v);
  } else {
    return validate(v);
  }
}
} // anonymous namespace

bool validate_cmd(CephContext* cct,
		  const std::string& desc,
		  const cmdmap_t& cmdmap,
		  std::ostream& os)
{
  return !find_first_in(desc, " ", [&](auto desc) {
    auto arg_desc = cmddesc_get_args(desc);
    if (arg_desc.empty()) {
      return false;
    }
    ceph_assert(arg_desc.count("name"));
    ceph_assert(arg_desc.count("type"));
    auto name = arg_desc["name"];
    auto type = arg_desc["type"];
    if (arg_desc.count("n")) {
      if (type == "CephInt") {
	return !validate_arg<true, int64_t>(cct, cmdmap, arg_desc,
					    name, type, os);
      } else if (type == "CephFloat") {
	return !validate_arg<true, double>(cct, cmdmap, arg_desc,
					    name, type, os);
      } else {
	return !validate_arg<true, string>(cct, cmdmap, arg_desc,
					   name, type, os);
      }
    } else {
      if (type == "CephInt") {
	return !validate_arg<false, int64_t>(cct, cmdmap, arg_desc,
					    name, type, os);
      } else if (type == "CephFloat") {
	return !validate_arg<false, double>(cct, cmdmap, arg_desc,
					    name, type, os);
      } else {
	return !validate_arg<false, string>(cct, cmdmap, arg_desc,
					    name, type, os);
      }
    }
  });
}

bool cmd_getval(const cmdmap_t& cmdmap,
		const std::string& k, bool& val)
{
  /*
   * Specialized getval for booleans.  CephBool didn't exist before Nautilus,
   * so earlier clients are sent a CephChoices argdesc instead, and will
   * send us a "--foo-bar" value string for boolean arguments.
   */
  if (cmdmap.count(k)) {
    try {
      val = boost::get<bool>(cmdmap.find(k)->second);
      return true;
    } catch (boost::bad_get&) {
      try {
        std::string expected = "--" + k;
        std::replace(expected.begin(), expected.end(), '_', '-');

        std::string v_str = boost::get<std::string>(cmdmap.find(k)->second);
        if (v_str == expected) {
          val = true;
          return true;
        } else {
          throw bad_cmd_get(k, cmdmap);
        }
      } catch (boost::bad_get&) {
        throw bad_cmd_get(k, cmdmap);
      }
    }
  }
  return false;
}

}