1 #include "processing.hpp"
2 
3 #include <boost/algorithm/string/predicate.hpp>
4 
5 #include <iostream>
6 
7 bool getWellKnown(
8     const boost::container::flat_map<std::string, std::string>& owners,
9     const std::string& request, std::string& wellKnown)
10 {
11     // If it's already a well known name, just return
12     if (!boost::starts_with(request, ":"))
13     {
14         wellKnown = request;
15         return true;
16     }
17 
18     auto it = owners.find(request);
19     if (it == owners.end())
20     {
21         return false;
22     }
23     wellKnown = it->second;
24     return true;
25 }
26 
27 bool needToIntrospect(const std::string& processName,
28                       const AllowDenyList& allowList)
29 {
30     auto inAllowList =
31         std::find_if(allowList.begin(), allowList.end(),
32                      [&processName](const auto& prefix) {
33                          return boost::starts_with(processName, prefix);
34                      }) != allowList.end();
35 
36     return inAllowList;
37 }
38 
39 void processNameChangeDelete(
40     boost::container::flat_map<std::string, std::string>& nameOwners,
41     const std::string& wellKnown, const std::string& oldOwner,
42     InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
43     sdbusplus::asio::object_server& server)
44 {
45     if (boost::starts_with(oldOwner, ":"))
46     {
47         auto it = nameOwners.find(oldOwner);
48         if (it != nameOwners.end())
49         {
50             nameOwners.erase(it);
51         }
52     }
53     // Connection removed
54     InterfaceMapType::iterator pathIt = interfaceMap.begin();
55     while (pathIt != interfaceMap.end())
56     {
57         // If an associations interface is being removed,
58         // also need to remove the corresponding associations
59         // objects and properties.
60         auto ifaces = pathIt->second.find(wellKnown);
61         if (ifaces != pathIt->second.end())
62         {
63             auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
64                                    assocDefsInterface);
65             if (assoc != ifaces->second.end())
66             {
67                 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
68             }
69 
70             // Instead of checking if every single path is the endpoint of an
71             // association that needs to be moved to pending, only check when
72             // we own this path as well, which would be because of an
73             // association.
74             if ((pathIt->second.size() == 2) &&
75                 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
76                  pathIt->second.end()))
77             {
78                 // Remove the 2 association D-Bus paths and move the
79                 // association to pending.
80                 moveAssociationToPending(pathIt->first, assocMaps, server);
81             }
82         }
83         pathIt->second.erase(wellKnown);
84         if (pathIt->second.empty())
85         {
86             // If the last connection to the object is gone,
87             // delete the top level object
88             pathIt = interfaceMap.erase(pathIt);
89             continue;
90         }
91         pathIt++;
92     }
93 }
94 
95 void processInterfaceAdded(InterfaceMapType& interfaceMap,
96                            const sdbusplus::message::object_path& objPath,
97                            const InterfacesAdded& intfAdded,
98                            const std::string& wellKnown,
99                            AssociationMaps& assocMaps,
100                            sdbusplus::asio::object_server& server)
101 {
102     auto& ifaceList = interfaceMap[objPath.str];
103 
104     for (const auto& interfacePair : intfAdded)
105     {
106         ifaceList[wellKnown].emplace(interfacePair.first);
107 
108         if (interfacePair.first == assocDefsInterface)
109         {
110             const std::variant<std::vector<Association>>* variantAssociations =
111                 nullptr;
112             for (const auto& interface : interfacePair.second)
113             {
114                 if (interface.first == assocDefsProperty)
115                 {
116                     variantAssociations = &(interface.second);
117                 }
118             }
119             if (variantAssociations == nullptr)
120             {
121                 std::cerr << "Illegal association found on " << wellKnown
122                           << "\n";
123                 continue;
124             }
125             std::vector<Association> associations =
126                 std::get<std::vector<Association>>(*variantAssociations);
127             associationChanged(server, associations, objPath.str, wellKnown,
128                                interfaceMap, assocMaps);
129         }
130     }
131 
132     // To handle the case where an object path is being created
133     // with 2 or more new path segments, check if the parent paths
134     // of this path are already in the interface map, and add them
135     // if they aren't with just the default freedesktop interfaces.
136     // This would be done via introspection if they would have
137     // already existed at startup.  While we could also introspect
138     // them now to do the work, we know there aren't any other
139     // interfaces or we would have gotten signals for them as well,
140     // so take a shortcut to speed things up.
141     //
142     // This is all needed so that mapper operations can be done
143     // on the new parent paths.
144     using iface_map_iterator = InterfaceMapType::iterator;
145     using name_map_iterator = InterfaceMapType::mapped_type::iterator;
146 
147     static const InterfaceNames defaultIfaces{
148         "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
149         "org.freedesktop.DBus.Properties"};
150 
151     std::string parent = objPath.str;
152     auto pos = parent.find_last_of('/');
153 
154     while (pos != std::string::npos)
155     {
156         parent = parent.substr(0, pos);
157 
158         std::pair<iface_map_iterator, bool> parentEntry =
159             interfaceMap.try_emplace(parent);
160 
161         std::pair<name_map_iterator, bool> ifaceEntry =
162             parentEntry.first->second.try_emplace(wellKnown, defaultIfaces);
163 
164         if (!ifaceEntry.second)
165         {
166             // Entry was already there for this name so done.
167             break;
168         }
169 
170         pos = parent.find_last_of('/');
171     }
172 
173     // The new interface might have an association pending
174     checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
175 }
176