~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/mon/AuthMonitor.cc

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-07-16 09:56:24 UTC
  • mfrom: (0.3.11)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20120716095624-azr2w4hbhei1rxmx
Tags: upstream-0.48
ImportĀ upstreamĀ versionĀ 0.48

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 * 
13
13
 */
14
14
 
 
15
#include <sstream>
15
16
 
16
17
#include "AuthMonitor.h"
17
18
#include "Monitor.h"
31
32
#include "osd/osd_types.h"
32
33
 
33
34
#include "common/config.h"
34
 
#include <sstream>
 
35
#include "include/assert.h"
35
36
 
36
37
#define dout_subsys ceph_subsys_mon
37
38
#undef dout_prefix
222
223
  for (vector<Incremental>::iterator p = pending_auth.begin();
223
224
       p != pending_auth.end();
224
225
       p++)
225
 
    p->encode(bl);
 
226
    p->encode(bl, mon->get_quorum_features());
226
227
}
227
228
 
228
229
bool AuthMonitor::preprocess_query(PaxosServiceMessage *m)
430
431
  if (m->cmd.size() > 1) {
431
432
    if (m->cmd[1] == "add" ||
432
433
        m->cmd[1] == "del" ||
433
 
        m->cmd[1] == "caps" ||
434
 
        m->cmd[1] == "list") {
 
434
        m->cmd[1] == "get-or-create" ||
 
435
        m->cmd[1] == "get-or-create-key" ||
 
436
        m->cmd[1] == "caps") {
435
437
      return false;
436
438
    }
437
 
    else if (m->cmd[1] == "export") {
 
439
 
 
440
    MonSession *session = m->get_session();
 
441
    if (!session ||
 
442
        (!session->caps.get_allow_all() &&
 
443
         !mon->_allowed_command(session, m->cmd))) {
 
444
      mon->reply_command(m, -EACCES, "access denied", rdata, paxos->get_version());
 
445
      return true;
 
446
    }
 
447
 
 
448
    if (m->cmd[1] == "export") {
438
449
      KeyRing keyring;
439
450
      export_keyring(keyring);
440
451
      if (m->cmd.size() > 2) {
444
455
          if (keyring.get_auth(ename, eauth)) {
445
456
            KeyRing kr;
446
457
            kr.add(ename, eauth);
447
 
            ::encode(kr, rdata);
 
458
            kr.encode_plaintext(rdata);
448
459
            ss << "export " << eauth;
449
460
            r = 0;
450
461
          } else {
456
467
          r = -EINVAL;
457
468
        }
458
469
      } else {
459
 
        ::encode(keyring, rdata);
 
470
        keyring.encode_plaintext(rdata);
460
471
        ss << "exported master keyring";
461
472
        r = 0;
462
473
      }
474
485
          r = -ENOENT;
475
486
        } else {
476
487
          keyring.add(entity, entity_auth);
477
 
          ::encode(keyring, rdata);
 
488
          keyring.encode_plaintext(rdata);
478
489
          ss << "exported keyring for " << m->cmd[2];
479
490
          r = 0;
480
491
        }
481
492
      }
482
493
    }
483
 
    else if ((m->cmd[1] == "print-key" || m->cmd[1] == "print_key") &&
 
494
    else if ((m->cmd[1] == "print-key" || m->cmd[1] == "print_key" || m->cmd[1] == "get-key") &&
484
495
             m->cmd.size() == 3) {
485
496
      EntityName ename;
486
497
      if (!ename.from_str(m->cmd[2])) {
497
508
      ss << auth.key;
498
509
      r = 0;      
499
510
    }
 
511
    else if (m->cmd[1] == "list") {
 
512
      mon->key_server.list_secrets(ss);
 
513
      r = 0;
 
514
      goto done;
 
515
    }
500
516
    else {
501
517
      auth_usage(ss);
502
518
      r = -EINVAL;
535
551
bool AuthMonitor::prepare_command(MMonCommand *m)
536
552
{
537
553
  stringstream ss;
 
554
  bufferlist rdata;
538
555
  string rs;
539
556
  int err = -EINVAL;
540
557
 
 
558
  MonSession *session = m->get_session();
 
559
  if (!session ||
 
560
      (!session->caps.get_allow_all() &&
 
561
       !mon->_allowed_command(session, m->cmd))) {
 
562
    mon->reply_command(m, -EACCES, "access denied", rdata, paxos->get_version());
 
563
    return true;
 
564
  }
 
565
 
541
566
  // nothing here yet
542
567
  if (m->cmd.size() > 1) {
543
568
    if (m->cmd[1] == "import") {
604
629
      paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
605
630
      return true;
606
631
    }
 
632
    else if ((m->cmd[1] == "get-or-create-key" ||
 
633
              m->cmd[1] == "get-or-create") &&
 
634
             m->cmd.size() >= 3) {
 
635
      // auth get-or-create <name> [mon osdcapa osd osdcapb ...]
 
636
      EntityName entity;
 
637
      if (!entity.from_str(m->cmd[2])) {
 
638
        ss << "bad entity name";
 
639
        err = -EINVAL;
 
640
        goto done;
 
641
      }
 
642
 
 
643
      // do we have it?
 
644
      EntityAuth entity_auth;
 
645
      if (mon->key_server.get_auth(entity, entity_auth)) {
 
646
        for (unsigned i=3; i + 1<m->cmd.size(); i += 2) {
 
647
          string sys = m->cmd[i];
 
648
          bufferlist cap;
 
649
          ::encode(m->cmd[i+1], cap);
 
650
          if (entity_auth.caps.count(sys) == 0 ||
 
651
              !entity_auth.caps[sys].contents_equal(cap)) {
 
652
            ss << "key for " << entity << " exists but cap " << sys << " does not match";
 
653
            err = -EINVAL;
 
654
            goto done;
 
655
          }
 
656
        }
 
657
 
 
658
        if (m->cmd[1] == "get-or-create-key") {
 
659
          ss << entity_auth.key;
 
660
        } else {
 
661
          KeyRing kr;
 
662
          kr.add(entity, entity_auth.key);
 
663
          kr.encode_plaintext(rdata);
 
664
        }
 
665
        err = 0;
 
666
        goto done;
 
667
      }
 
668
 
 
669
      // ...or are we about to?
 
670
      for (vector<Incremental>::iterator p = pending_auth.begin();
 
671
           p != pending_auth.end();
 
672
           ++p) {
 
673
        if (p->inc_type == AUTH_DATA) {
 
674
          KeyServerData::Incremental auth_inc;
 
675
          bufferlist::iterator q = p->auth_data.begin();
 
676
          ::decode(auth_inc, q);
 
677
          if (auth_inc.op == KeyServerData::AUTH_INC_ADD &&
 
678
              auth_inc.name == entity) {
 
679
            paxos->wait_for_commit(new C_RetryMessage(this, m));
 
680
            return true;
 
681
          }
 
682
        }
 
683
      }
 
684
 
 
685
      // create it
 
686
      KeyServerData::Incremental auth_inc;
 
687
      auth_inc.op = KeyServerData::AUTH_INC_ADD;
 
688
      auth_inc.name = entity;
 
689
      auth_inc.auth.key.create(g_ceph_context, CEPH_CRYPTO_AES);
 
690
      for (unsigned i=3; i + 1<m->cmd.size(); i += 2)
 
691
        ::encode(m->cmd[i+1], auth_inc.auth.caps[m->cmd[i]]);
 
692
 
 
693
      push_cephx_inc(auth_inc);
 
694
 
 
695
      if (m->cmd[1] == "get-or-create-key") {
 
696
        ss << auth_inc.auth.key;
 
697
      } else {
 
698
        KeyRing kr;
 
699
        kr.add(entity, auth_inc.auth.key);
 
700
        kr.encode_plaintext(rdata);
 
701
      }
 
702
 
 
703
      getline(ss, rs);
 
704
      paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, rdata, paxos->get_version()));
 
705
      return true;
 
706
    }
607
707
    else if (m->cmd[1] == "caps" && m->cmd.size() >= 3) {
608
708
      KeyServerData::Incremental auth_inc;
609
709
      if (!auth_inc.name.from_str(m->cmd[2])) {
648
748
      paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
649
749
      return true;
650
750
    }
651
 
    else if (m->cmd[1] == "list") {
652
 
      mon->key_server.list_secrets(ss);
653
 
      err = 0;
654
 
      goto done;
655
 
    }
656
751
    else {
657
752
      auth_usage(ss);
658
753
    }
662
757
 
663
758
done:
664
759
  getline(ss, rs, '\0');
665
 
  mon->reply_command(m, err, rs, paxos->get_version());
 
760
  mon->reply_command(m, err, rs, rdata, paxos->get_version());
666
761
  return false;
667
762
}
668
763