Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / auth / AuthMethodList.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) 2004-2009 Sage Weil <sage@newdream.net>
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 <algorithm>
16 #include "common/debug.h"
17 #include "include/str_list.h"
18
19 #include "AuthMethodList.h"
20
21 const static int dout_subsys = ceph_subsys_auth;
22
23
24 AuthMethodList::AuthMethodList(CephContext *cct, string str)
25 {
26   list<string> sup_list;
27   get_str_list(str, sup_list);
28   if (sup_list.empty()) {
29     lderr(cct) << "WARNING: empty auth protocol list" << dendl;
30   }
31   for (list<string>::iterator iter = sup_list.begin(); iter != sup_list.end(); ++iter) {
32     ldout(cct, 5) << "adding auth protocol: " << *iter << dendl;
33     if (iter->compare("cephx") == 0) {
34       auth_supported.push_back(CEPH_AUTH_CEPHX);
35     } else if (iter->compare("none") == 0) {
36       auth_supported.push_back(CEPH_AUTH_NONE);
37     } else {
38       auth_supported.push_back(CEPH_AUTH_UNKNOWN);
39       lderr(cct) << "WARNING: unknown auth protocol defined: " << *iter << dendl;
40     }
41   }
42   if (auth_supported.empty()) {
43     lderr(cct) << "WARNING: no auth protocol defined, use 'cephx' by default" << dendl;
44     auth_supported.push_back(CEPH_AUTH_CEPHX);
45   }
46 }
47
48 bool AuthMethodList::is_supported_auth(int auth_type)
49 {
50   return std::find(auth_supported.begin(), auth_supported.end(), auth_type) != auth_supported.end();
51 }
52
53 int AuthMethodList::pick(const std::set<__u32>& supported)
54 {
55   for (set<__u32>::const_reverse_iterator p = supported.rbegin(); p != supported.rend(); ++p)
56     if (is_supported_auth(*p))
57       return *p;
58   return CEPH_AUTH_UNKNOWN;
59 }
60
61 void AuthMethodList::remove_supported_auth(int auth_type)
62 {
63   for (list<__u32>::iterator p = auth_supported.begin(); p != auth_supported.end(); ) {
64     if (*p == (__u32)auth_type)
65       auth_supported.erase(p++);
66     else 
67       ++p;
68   }
69 }