]> granicus.if.org Git - icinga2/blob - lib/icinga/usergroup.cpp
Update copyright headers for 2016
[icinga2] / lib / icinga / usergroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "icinga/usergroup.hpp"
21 #include "icinga/usergroup.tcpp"
22 #include "config/objectrule.hpp"
23 #include "config/configitem.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/logger.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
29 #include <boost/foreach.hpp>
30
31 using namespace icinga;
32
33 REGISTER_TYPE(UserGroup);
34
35 INITIALIZE_ONCE(&UserGroup::RegisterObjectRuleHandler);
36
37 void UserGroup::RegisterObjectRuleHandler(void)
38 {
39         ObjectRule::RegisterType("UserGroup");
40 }
41
42 bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr& group)
43 {
44         String group_name = group->GetName();
45
46         CONTEXT("Evaluating rule for group '" + group_name + "'");
47
48         ScriptFrame frame;
49         if (group->GetScope())
50                 group->GetScope()->CopyTo(frame.Locals);
51         frame.Locals->Set("user", user);
52
53         if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool())
54                 return false;
55
56         Log(LogDebug, "UserGroup")
57             << "Assigning membership for group '" << group_name << "' to user '" << user->GetName() << "'";
58
59         Array::Ptr groups = user->GetGroups();
60         groups->Add(group_name);
61
62         return true;
63 }
64
65 void UserGroup::EvaluateObjectRules(const User::Ptr& user)
66 {
67         CONTEXT("Evaluating group membership for user '" + user->GetName() + "'");
68
69         BOOST_FOREACH(const ConfigItem::Ptr& group, ConfigItem::GetItems("UserGroup"))
70         {
71                 if (!group->GetFilter())
72                         continue;
73
74                 EvaluateObjectRule(user, group);
75         }
76 }
77
78 std::set<User::Ptr> UserGroup::GetMembers(void) const
79 {
80         boost::mutex::scoped_lock lock(m_UserGroupMutex);
81         return m_Members;
82 }
83
84 void UserGroup::AddMember(const User::Ptr& user)
85 {
86         user->AddGroup(GetName());
87
88         boost::mutex::scoped_lock lock(m_UserGroupMutex);
89         m_Members.insert(user);
90 }
91
92 void UserGroup::RemoveMember(const User::Ptr& user)
93 {
94         boost::mutex::scoped_lock lock(m_UserGroupMutex);
95         m_Members.erase(user);
96 }
97
98 bool UserGroup::ResolveGroupMembership(const User::Ptr& user, bool add, int rstack) {
99
100         if (add && rstack > 20) {
101                 Log(LogWarning, "UserGroup")
102                     << "Too many nested groups for group '" << GetName() << "': User '"
103                     << user->GetName() << "' membership assignment failed.";
104
105                 return false;
106         }
107
108         Array::Ptr groups = GetGroups();
109
110         if (groups && groups->GetLength() > 0) {
111                 ObjectLock olock(groups);
112
113                 BOOST_FOREACH(const String& name, groups) {
114                         UserGroup::Ptr group = UserGroup::GetByName(name);
115
116                         if (group && !group->ResolveGroupMembership(user, add, rstack + 1))
117                                 return false;
118                 }
119         }
120
121         if (add)
122                 AddMember(user);
123         else
124                 RemoveMember(user);
125
126         return true;
127 }
128