blob: a05992b45d8510def80dd63051ace5a8aa38a762 [file] [log] [blame]
vishwabmcba0bd5f2015-09-30 16:50:23 +05301#include <stdio.h>
2#include <dlfcn.h>
3#include <iostream>
4#include <unistd.h>
5#include <assert.h>
6#include <dirent.h>
Chris Austen0ba649e2015-10-13 12:28:13 -05007#include <systemd/sd-bus.h>
vishwabmcba0bd5f2015-09-30 16:50:23 +05308#include <string.h>
9#include <stdlib.h>
10#include <map>
Deepak Kodihalli84b3a082017-07-21 23:44:44 -050011#include <memory>
Andrew Geissler93c679b2017-04-02 10:06:43 -050012#include <phosphor-logging/log.hpp>
Chris Austen0ba649e2015-10-13 12:28:13 -050013#include <sys/time.h>
14#include <errno.h>
Brad Bishop35518682016-07-22 08:35:41 -040015#include <mapper.h>
Chris Austen0012e9b2015-10-22 01:37:46 -050016#include "sensorhandler.h"
Tom Joseph9a61b4f2016-07-11 06:56:11 -050017#include <vector>
18#include <algorithm>
19#include <iterator>
Patrick Williams4b9efaa2016-08-12 21:59:51 -050020#include <ipmiwhitelist.hpp>
Deepak Kodihalli84b3a082017-07-21 23:44:44 -050021#include <sdbusplus/bus.hpp>
22#include <sdbusplus/bus/match.hpp>
23#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
24#include "sensorhandler.h"
25#include "ipmid.hpp"
26#include "settings.hpp"
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +053027#include <host-cmd-manager.hpp>
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053028#include <host-ipmid/ipmid-host-cmd.hpp>
29#include <timer.hpp>
Chris Austen0ba649e2015-10-13 12:28:13 -050030
Andrew Geissler93c679b2017-04-02 10:06:43 -050031using namespace phosphor::logging;
Deepak Kodihalli84b3a082017-07-21 23:44:44 -050032namespace sdbusRule = sdbusplus::bus::match::rules;
Andrew Geissler93c679b2017-04-02 10:06:43 -050033
Chris Austen0ba649e2015-10-13 12:28:13 -050034sd_bus *bus = NULL;
vishwab9f559a2016-01-13 01:53:08 -060035sd_bus_slot *ipmid_slot = NULL;
Andrew Geissler93c679b2017-04-02 10:06:43 -050036sd_event *events = nullptr;
Chris Austen30195fa2015-11-13 14:39:19 -060037
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +053038// Need this to use new sdbusplus compatible interfaces
39sdbusPtr sdbusp;
40
41// Global Host Bound Command manager
42using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>;
43cmdManagerPtr cmdManager;
44
45// Command and handler tuple. Used when clients ask the command to be put
46// into host message queue
47using CommandHandler = phosphor::host::command::CommandHandler;
48
Tom Joseph9a61b4f2016-07-11 06:56:11 -050049// Initialise restricted mode to true
50bool restricted_mode = true;
51
Chris Austen41a4b312015-10-25 03:45:42 -050052FILE *ipmiio, *ipmidbus, *ipmicmddetails;
vishwabmcba0bd5f2015-09-30 16:50:23 +053053
Chris Austen99497312015-10-22 13:00:16 -050054void print_usage(void) {
55 fprintf(stderr, "Options: [-d mask]\n");
56 fprintf(stderr, " mask : 0x01 - Print ipmi packets\n");
57 fprintf(stderr, " mask : 0x02 - Print DBUS operations\n");
58 fprintf(stderr, " mask : 0x04 - Print ipmi command details\n");
59 fprintf(stderr, " mask : 0xFF - Print all trace\n");
60}
61
Jeremy Kerre41081f2015-10-27 12:11:36 +080062const char * DBUS_INTF = "org.openbmc.HostIpmi";
vishwabmcba0bd5f2015-09-30 16:50:23 +053063
Jeremy Kerre41081f2015-10-27 12:11:36 +080064const char * FILTER = "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'";
Chris Austen0ba649e2015-10-13 12:28:13 -050065
vishwabmcba0bd5f2015-09-30 16:50:23 +053066typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t;
67typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t;
68
69// Global data structure that contains the IPMI command handler's registrations.
70std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map;
71
Nan Li36c0cb62016-03-31 11:16:08 +080072// IPMI Spec, shared Reservation ID.
73unsigned short g_sel_reserve = 0xFFFF;
74
75unsigned short get_sel_reserve_id(void)
76{
77 return g_sel_reserve;
78}
Chris Austen0ba649e2015-10-13 12:28:13 -050079
Deepak Kodihalli84b3a082017-07-21 23:44:44 -050080namespace internal
81{
82
83constexpr auto restrictionModeIntf =
84 "xyz.openbmc_project.Control.Security.RestrictionMode";
85
86namespace cache
87{
88
89std::unique_ptr<settings::Objects> objects = nullptr;
90
91} // namespace cache
92} // namespace internal
93
Chris Austen0ba649e2015-10-13 12:28:13 -050094#ifndef HEXDUMP_COLS
95#define HEXDUMP_COLS 16
96#endif
97
Chris Austen99497312015-10-22 13:00:16 -050098void hexdump(FILE *s, void *mem, size_t len)
Chris Austen0ba649e2015-10-13 12:28:13 -050099{
100 unsigned int i, j;
Chris Austen120f7322015-10-14 23:27:31 -0500101
Chris Austen0ba649e2015-10-13 12:28:13 -0500102 for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
103 {
104 /* print offset */
105 if(i % HEXDUMP_COLS == 0)
106 {
Chris Austen99497312015-10-22 13:00:16 -0500107 fprintf(s,"0x%06x: ", i);
Chris Austen0ba649e2015-10-13 12:28:13 -0500108 }
Chris Austen120f7322015-10-14 23:27:31 -0500109
Chris Austen0ba649e2015-10-13 12:28:13 -0500110 /* print hex data */
111 if(i < len)
112 {
Chris Austen99497312015-10-22 13:00:16 -0500113 fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]);
Chris Austen0ba649e2015-10-13 12:28:13 -0500114 }
115 else /* end of block, just aligning for ASCII dump */
116 {
Chris Austen99497312015-10-22 13:00:16 -0500117 fprintf(s," ");
Chris Austen0ba649e2015-10-13 12:28:13 -0500118 }
Chris Austen120f7322015-10-14 23:27:31 -0500119
Chris Austen0ba649e2015-10-13 12:28:13 -0500120 /* print ASCII dump */
121 if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
122 {
123 for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
124 {
125 if(j >= len) /* end of block, not really printing */
126 {
Chris Austen99497312015-10-22 13:00:16 -0500127 fputc(' ', s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500128 }
129 else if(isprint(((char*)mem)[j])) /* printable char */
130 {
Chris Austen99497312015-10-22 13:00:16 -0500131 fputc(0xFF & ((char*)mem)[j], s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500132 }
133 else /* other char */
134 {
Chris Austen99497312015-10-22 13:00:16 -0500135 fputc('.',s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500136 }
137 }
Chris Austen99497312015-10-22 13:00:16 -0500138 fputc('\n',s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500139 }
140 }
141}
142
143
vishwabmcba0bd5f2015-09-30 16:50:23 +0530144// Method that gets called by shared libraries to get their command handlers registered
Tom05732372016-09-06 17:21:23 +0530145void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_context_t context,
146 ipmid_callback_t handler, ipmi_cmd_privilege_t priv)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530147{
148 // Pack NetFn and Command in one.
149 auto netfn_and_cmd = std::make_pair(netfn, cmd);
150
151 // Pack Function handler and Data in another.
152 auto handler_and_context = std::make_pair(handler, context);
153
154 // Check if the registration has already been made..
155 auto iter = g_ipmid_router_map.find(netfn_and_cmd);
156 if(iter != g_ipmid_router_map.end())
157 {
158 fprintf(stderr,"ERROR : Duplicate registration for NetFn [0x%X], Cmd:[0x%X]\n",netfn, cmd);
159 }
160 else
161 {
162 // This is a fresh registration.. Add it to the map.
163 g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context);
164 }
165
166 return;
167}
168
169// Looks at the map and calls corresponding handler functions.
170ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
171 ipmi_response_t response, ipmi_data_len_t data_len)
172{
173 // return from the Command handlers.
174 ipmi_ret_t rc = IPMI_CC_INVALID;
175
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500176 // If restricted mode is true and command is not whitelisted, don't
177 // execute the command
178 if(restricted_mode)
179 {
180 if (!std::binary_search(whitelist.cbegin(), whitelist.cend(),
181 std::make_pair(netfn, cmd)))
182 {
183 printf("Net function:[0x%X], Command:[0x%X] is not whitelisted\n",
184 netfn, cmd);
185 rc = IPMI_CC_INSUFFICIENT_PRIVILEGE;
186 memcpy(response, &rc, IPMI_CC_LEN);
187 *data_len = IPMI_CC_LEN;
188 return rc;
189 }
190 }
191
vishwabmcba0bd5f2015-09-30 16:50:23 +0530192 // Walk the map that has the registered handlers and invoke the approprite
193 // handlers for matching commands.
194 auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd));
195 if(iter == g_ipmid_router_map.end())
196 {
Patrick Venture03f84ba2017-09-20 09:15:33 -0700197 /* By default should only print on failure to find wildcard command. */
198#ifdef __IPMI_DEBUG__
Chris Austen99497312015-10-22 13:00:16 -0500199 fprintf(stderr, "No registered handlers for NetFn:[0x%X], Cmd:[0x%X]"
vishwabmcba0bd5f2015-09-30 16:50:23 +0530200 " trying Wilcard implementation \n",netfn, cmd);
Patrick Venture03f84ba2017-09-20 09:15:33 -0700201#endif
vishwabmcba0bd5f2015-09-30 16:50:23 +0530202
203 // Now that we did not find any specific [NetFn,Cmd], tuple, check for
204 // NetFn, WildCard command present.
205 iter = g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD));
206 if(iter == g_ipmid_router_map.end())
207 {
Chris Austen99497312015-10-22 13:00:16 -0500208 fprintf(stderr, "No Registered handlers for NetFn:[0x%X],Cmd:[0x%X]\n",netfn, IPMI_CMD_WILDCARD);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530209
210 // Respond with a 0xC1
211 memcpy(response, &rc, IPMI_CC_LEN);
212 *data_len = IPMI_CC_LEN;
213 return rc;
214 }
215 }
216
217#ifdef __IPMI_DEBUG__
218 // We have either a perfect match -OR- a wild card atleast,
219 printf("Calling Net function:[0x%X], Command:[0x%X]\n", netfn, cmd);
220#endif
221
222 // Extract the map data onto appropriate containers
223 auto handler_and_context = iter->second;
224
225 // Creating a pointer type casted to char* to make sure we advance 1 byte
226 // when we advance pointer to next's address. advancing void * would not
227 // make sense.
228 char *respo = &((char *)response)[IPMI_CC_LEN];
229
230 // Response message from the plugin goes into a byte post the base response
231 rc = (handler_and_context.first) (netfn, cmd, request, respo,
232 data_len, handler_and_context.second);
Chris Austen120f7322015-10-14 23:27:31 -0500233
vishwabmcba0bd5f2015-09-30 16:50:23 +0530234 // Now copy the return code that we got from handler and pack it in first
235 // byte.
236 memcpy(response, &rc, IPMI_CC_LEN);
Chris Austen120f7322015-10-14 23:27:31 -0500237
vishwabmcba0bd5f2015-09-30 16:50:23 +0530238 // Data length is now actual data + completion code.
239 *data_len = *data_len + IPMI_CC_LEN;
240
241 return rc;
242}
243
vishwabmcba0bd5f2015-09-30 16:50:23 +0530244
vishwabmcba0bd5f2015-09-30 16:50:23 +0530245
vishwabmcba0bd5f2015-09-30 16:50:23 +0530246
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800247static int send_ipmi_message(sd_bus_message *req, unsigned char seq, unsigned char netfn, unsigned char lun, unsigned char cmd, unsigned char cc, unsigned char *buf, unsigned char len) {
vishwabmcba0bd5f2015-09-30 16:50:23 +0530248
Chris Austen0ba649e2015-10-13 12:28:13 -0500249 sd_bus_error error = SD_BUS_ERROR_NULL;
250 sd_bus_message *reply = NULL, *m=NULL;
Jeremy Kerre41081f2015-10-27 12:11:36 +0800251 const char *dest, *path;
Chris Austen0ba649e2015-10-13 12:28:13 -0500252 int r, pty;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530253
Jeremy Kerre41081f2015-10-27 12:11:36 +0800254 dest = sd_bus_message_get_sender(req);
255 path = sd_bus_message_get_path(req);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530256
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +0530257 r = sd_bus_message_new_method_call(bus,&m,dest,path,
258 DBUS_INTF,
259 "sendMessage");
Chris Austen0ba649e2015-10-13 12:28:13 -0500260 if (r < 0) {
261 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
262 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530263 }
264
vishwabmcba0bd5f2015-09-30 16:50:23 +0530265
Chris Austenabfb5e82015-10-13 12:29:24 -0500266 // Responses in IPMI require a bit set. So there ya go...
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800267 netfn |= 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530268
Chris Austen0ba649e2015-10-13 12:28:13 -0500269
270 // Add the bytes needed for the methods to be called
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800271 r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc);
Chris Austen0ba649e2015-10-13 12:28:13 -0500272 if (r < 0) {
273 fprintf(stderr, "Failed add the netfn and others : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600274 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500275 }
Chris Austen120f7322015-10-14 23:27:31 -0500276
Chris Austen0ba649e2015-10-13 12:28:13 -0500277 r = sd_bus_message_append_array(m, 'y', buf, len);
278 if (r < 0) {
279 fprintf(stderr, "Failed to add the string of response bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600280 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500281 }
282
283
284
285 // Call the IPMI responder on the bus so the message can be sent to the CEC
286 r = sd_bus_call(bus, m, 0, &error, &reply);
287 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600288 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
Chris Austen6bd23962015-12-07 21:31:48 -0600289 fprintf(stderr, "Dest: %s, Path: %s\n", dest, path);
Chris Austen169395e2015-12-02 20:56:15 -0600290 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500291 }
292
293 r = sd_bus_message_read(reply, "x", &pty);
Chris Austen0ba649e2015-10-13 12:28:13 -0500294 if (r < 0) {
295 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen0ba649e2015-10-13 12:28:13 -0500296 }
297
Chris Austen169395e2015-12-02 20:56:15 -0600298final:
Chris Austen0ba649e2015-10-13 12:28:13 -0500299 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600300 m = sd_bus_message_unref(m);
301 reply = sd_bus_message_unref(reply);
Chris Austen0ba649e2015-10-13 12:28:13 -0500302
Chris Austen0ba649e2015-10-13 12:28:13 -0500303 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Chris Austen0ba649e2015-10-13 12:28:13 -0500304}
305
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500306void cache_restricted_mode()
307{
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500308 restricted_mode = false;
309 using namespace sdbusplus::xyz::openbmc_project::Control::Security::server;
310 using namespace internal;
311 using namespace internal::cache;
312 sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
313 const auto& restrictionModeSetting =
Deepak Kodihallie6027092017-08-27 08:13:37 -0500314 objects->map.at(restrictionModeIntf).front();
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500315 auto method = dbus.new_method_call(
316 objects->service(restrictionModeSetting,
317 restrictionModeIntf).c_str(),
318 restrictionModeSetting.c_str(),
319 "org.freedesktop.DBus.Properties",
320 "Get");
321 method.append(restrictionModeIntf, "RestrictionMode");
322 auto resp = dbus.call(method);
323 if (resp.is_method_error())
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500324 {
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500325 log<level::ERR>("Error in RestrictionMode Get");
326 // Fail-safe to true.
327 restricted_mode = true;
328 return;
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500329 }
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500330 sdbusplus::message::variant<std::string> result;
331 resp.read(result);
332 auto restrictionMode =
333 RestrictionMode::convertModesFromString(result.get<std::string>());
334 if(RestrictionMode::Modes::Whitelist == restrictionMode)
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500335 {
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500336 restricted_mode = true;
337 }
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500338}
339
340static int handle_restricted_mode_change(sd_bus_message *m, void *user_data,
341 sd_bus_error *ret_error)
342{
343 cache_restricted_mode();
344 return 0;
345}
346
Chris Austen0ba649e2015-10-13 12:28:13 -0500347static int handle_ipmi_command(sd_bus_message *m, void *user_data, sd_bus_error
348 *ret_error) {
349 int r = 0;
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800350 unsigned char sequence, netfn, lun, cmd;
Chris Austen0ba649e2015-10-13 12:28:13 -0500351 const void *request;
352 size_t sz;
353 size_t resplen =MAX_IPMI_BUFFER;
354 unsigned char response[MAX_IPMI_BUFFER];
355
Chris Austen0ba649e2015-10-13 12:28:13 -0500356 memset(response, 0, MAX_IPMI_BUFFER);
357
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800358 r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd);
Chris Austen0ba649e2015-10-13 12:28:13 -0500359 if (r < 0) {
360 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
361 return -1;
362 }
363
364 r = sd_bus_message_read_array(m, 'y', &request, &sz );
365 if (r < 0) {
366 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
367 return -1;
368 }
369
Chris Austen99497312015-10-22 13:00:16 -0500370 fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd);
371 hexdump(ipmiio, (void*)request, sz);
Chris Austen0ba649e2015-10-13 12:28:13 -0500372
Chris Austen120f7322015-10-14 23:27:31 -0500373 // Allow the length field to be used for both input and output of the
Chris Austen0ba649e2015-10-13 12:28:13 -0500374 // ipmi call
375 resplen = sz;
376
Chris Austen120f7322015-10-14 23:27:31 -0500377 // Now that we have parsed the entire byte array from the caller
vishwabmcba0bd5f2015-09-30 16:50:23 +0530378 // we can call the ipmi router to do the work...
Chris Austen0ba649e2015-10-13 12:28:13 -0500379 r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen);
380 if(r != 0)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530381 {
Chris Austen0ba649e2015-10-13 12:28:13 -0500382 fprintf(stderr,"ERROR:[0x%X] handling NetFn:[0x%X], Cmd:[0x%X]\n",r, netfn, cmd);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530383 resplen = 0;
384 }
385 else
386 {
387 resplen = resplen - 1; // first byte is for return code.
vishwabmcba0bd5f2015-09-30 16:50:23 +0530388 }
389
Chris Austen99497312015-10-22 13:00:16 -0500390 fprintf(ipmiio, "IPMI Response:\n");
391 hexdump(ipmiio, (void*)response, resplen);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530392
Chris Austen0ba649e2015-10-13 12:28:13 -0500393 // Send the response buffer from the ipmi command
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800394 r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0],
Ratan Guptae0cc8552018-01-22 14:23:04 +0530395 ((unsigned char *)response) + 1, resplen);
Chris Austen0ba649e2015-10-13 12:28:13 -0500396 if (r < 0) {
397 fprintf(stderr, "Failed to send the response message\n");
398 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530399 }
400
vishwabmcba0bd5f2015-09-30 16:50:23 +0530401
Chris Austen0ba649e2015-10-13 12:28:13 -0500402 return 0;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530403}
404
Chris Austen0ba649e2015-10-13 12:28:13 -0500405
vishwabmcba0bd5f2015-09-30 16:50:23 +0530406//----------------------------------------------------------------------
407// handler_select
408// Select all the files ending with with .so. in the given diretcory
409// @d: dirent structure containing the file name
410//----------------------------------------------------------------------
411int handler_select(const struct dirent *entry)
412{
413 // To hold ".so" from entry->d_name;
414 char dname_copy[4] = {0};
415
416 // We want to avoid checking for everything and isolate to the ones having
Adriana Kobylak87e080b2016-07-10 13:16:53 -0500417 // .so.* or .so in them.
418 // Check for versioned libraries .so.*
419 if(strstr(entry->d_name, IPMI_PLUGIN_SONAME_EXTN))
420 {
421 return 1;
422 }
423 // Check for non versioned libraries .so
424 else if(strstr(entry->d_name, IPMI_PLUGIN_EXTN))
vishwabmcba0bd5f2015-09-30 16:50:23 +0530425 {
426 // It is possible that .so could be anywhere in the string but unlikely
Chris Austen120f7322015-10-14 23:27:31 -0500427 // But being careful here. Get the base address of the string, move
vishwabmcba0bd5f2015-09-30 16:50:23 +0530428 // until end and come back 3 steps and that gets what we need.
429 strcpy(dname_copy, (entry->d_name + strlen(entry->d_name)-strlen(IPMI_PLUGIN_EXTN)));
430 if(strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0)
431 {
432 return 1;
433 }
434 }
435 return 0;
436}
437
438// This will do a dlopen of every .so in ipmi_lib_path and will dlopen everything so that they will
Chris Austen120f7322015-10-14 23:27:31 -0500439// register a callback handler
vishwabmcba0bd5f2015-09-30 16:50:23 +0530440void ipmi_register_callback_handlers(const char* ipmi_lib_path)
441{
442 // For walking the ipmi_lib_path
443 struct dirent **handler_list;
444 int num_handlers = 0;
445
446 // This is used to check and abort if someone tries to register a bad one.
447 void *lib_handler = NULL;
448
449 if(ipmi_lib_path == NULL)
450 {
451 fprintf(stderr,"ERROR; No handlers to be registered for ipmi.. Aborting\n");
452 assert(0);
453 }
454 else
455 {
456 // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid"
457 // 2: Scan the directory for the files that end with .so
Chris Austen120f7322015-10-14 23:27:31 -0500458 // 3: For each one of them, just do a 'dlopen' so that they register
vishwabmcba0bd5f2015-09-30 16:50:23 +0530459 // the handlers for callback routines.
460
461 std::string handler_fqdn = ipmi_lib_path;
Chris Austen120f7322015-10-14 23:27:31 -0500462
vishwabmcba0bd5f2015-09-30 16:50:23 +0530463 // Append a "/" since we need to add the name of the .so. If there is
464 // already a .so, adding one more is not any harm.
465 handler_fqdn += "/";
466
467 num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort);
Nan Li36c0cb62016-03-31 11:16:08 +0800468 if (num_handlers < 0)
469 return;
Jeremy Kerr5e8f85e2015-10-27 13:43:54 +0800470
vishwabmcba0bd5f2015-09-30 16:50:23 +0530471 while(num_handlers--)
472 {
Chris Austen54030262015-10-13 12:30:46 -0500473 handler_fqdn = ipmi_lib_path;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530474 handler_fqdn += handler_list[num_handlers]->d_name;
Chris Austen54030262015-10-13 12:30:46 -0500475 printf("Registering handler:[%s]\n",handler_fqdn.c_str());
476
vishwabmcba0bd5f2015-09-30 16:50:23 +0530477 lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW);
Nan Li36c0cb62016-03-31 11:16:08 +0800478
vishwabmcba0bd5f2015-09-30 16:50:23 +0530479 if(lib_handler == NULL)
480 {
Chris Austen120f7322015-10-14 23:27:31 -0500481 fprintf(stderr,"ERROR opening [%s]: %s\n",
482 handler_fqdn.c_str(), dlerror());
vishwabmcba0bd5f2015-09-30 16:50:23 +0530483 }
484 // Wipe the memory allocated for this particular entry.
485 free(handler_list[num_handlers]);
486 }
Nan Li36c0cb62016-03-31 11:16:08 +0800487
vishwabmcba0bd5f2015-09-30 16:50:23 +0530488 // Done with all registration.
489 free(handler_list);
490 }
491
492 // TODO : What to be done on the memory that is given by dlopen ?.
493 return;
494}
495
Chris Austen30195fa2015-11-13 14:39:19 -0600496sd_bus *ipmid_get_sd_bus_connection(void) {
497 return bus;
498}
499
Andrew Geissler93c679b2017-04-02 10:06:43 -0500500sd_event *ipmid_get_sd_event_connection(void) {
501 return events;
502}
503
vishwab9f559a2016-01-13 01:53:08 -0600504sd_bus_slot *ipmid_get_sd_bus_slot(void) {
505 return ipmid_slot;
506}
507
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +0530508// Calls host command manager to do the right thing for the command
509void ipmid_send_cmd_to_host(CommandHandler&& cmd) {
510 return cmdManager->execute(std::move(cmd));
511}
512
513cmdManagerPtr& ipmid_get_host_cmd_manager() {
514 return cmdManager;
515}
516
517sdbusPtr& ipmid_get_sdbus_plus_handler() {
518 return sdbusp;
519}
520
vishwabmcba0bd5f2015-09-30 16:50:23 +0530521int main(int argc, char *argv[])
522{
Chris Austen0ba649e2015-10-13 12:28:13 -0500523 int r;
Chris Austen99497312015-10-22 13:00:16 -0500524 unsigned long tvalue;
525 int c;
526
527
528
529 // This file and subsequient switch is for turning on levels
530 // of trace
531 ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w");
532
533 while ((c = getopt (argc, argv, "h:d:")) != -1)
534 switch (c) {
535 case 'd':
536 tvalue = strtoul(optarg, NULL, 16);
537 if (1&tvalue) {
538 ipmiio = stdout;
539 }
540 if (2&tvalue) {
541 ipmidbus = stdout;
542 }
543 if (4&tvalue) {
544 ipmicmddetails = stdout;
545 }
546 break;
547 case 'h':
548 case '?':
549 print_usage();
550 return 1;
551 }
Chris Austen0ba649e2015-10-13 12:28:13 -0500552
553
Chris Austen0ba649e2015-10-13 12:28:13 -0500554 /* Connect to system bus */
555 r = sd_bus_open_system(&bus);
556 if (r < 0) {
557 fprintf(stderr, "Failed to connect to system bus: %s\n",
558 strerror(-r));
559 goto finish;
560 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530561
Andrew Geissler93c679b2017-04-02 10:06:43 -0500562 /* Get an sd event handler */
563 r = sd_event_default(&events);
564 if (r < 0)
565 {
566 log<level::ERR>("Failure to create sd_event handler",
567 entry("ERROR=%s", strerror(-r)));
568 goto finish;
569 }
570
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +0530571 // Now create the Host Bound Command manager. Need sdbusplus
572 // to use the generated bindings
573 sdbusp = std::make_unique<sdbusplus::bus::bus>(bus);
574 cmdManager = std::make_unique<phosphor::host::command::Manager>(
575 *sdbusp, events);
Andrew Geissler93c679b2017-04-02 10:06:43 -0500576
Chris Austen30195fa2015-11-13 14:39:19 -0600577 // Register all the handlers that provider implementation to IPMI commands.
578 ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
579
vishwa36993272015-11-20 12:43:49 -0600580 // Watch for BT messages
vishwab9f559a2016-01-13 01:53:08 -0600581 r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL);
Chris Austen0ba649e2015-10-13 12:28:13 -0500582 if (r < 0) {
583 fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER);
584 goto finish;
585 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530586
Andrew Geissler93c679b2017-04-02 10:06:43 -0500587 // Attach the bus to sd_event to service user requests
588 sd_bus_attach_event(bus, events, SD_EVENT_PRIORITY_NORMAL);
589
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500590 {
591 using namespace internal;
592 using namespace internal::cache;
593 sdbusplus::bus::bus dbus{bus};
594 objects = std::make_unique<settings::Objects>(
595 dbus,
596 std::vector<settings::Interface>({restrictionModeIntf}));
597 // Initialize restricted mode
598 cache_restricted_mode();
599 // Wait for changes on Restricted mode
600 sdbusplus::bus::match_t restrictedModeMatch(
601 dbus,
602 sdbusRule::propertiesChanged(
Deepak Kodihallie6027092017-08-27 08:13:37 -0500603 objects->map.at(restrictionModeIntf).front(),
604 restrictionModeIntf),
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500605 handle_restricted_mode_change);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530606
Deepak Kodihalli84b3a082017-07-21 23:44:44 -0500607 for (;;) {
608 /* Process requests */
609 r = sd_event_run(events, (uint64_t)-1);
610 if (r < 0)
611 {
612 log<level::ERR>("Failure in processing request",
613 entry("ERROR=%s", strerror(-r)));
614 goto finish;
615 }
Chris Austen0ba649e2015-10-13 12:28:13 -0500616 }
617 }
618
619finish:
Andrew Geissler93c679b2017-04-02 10:06:43 -0500620 sd_event_unref(events);
621 sd_bus_detach_event(bus);
vishwab9f559a2016-01-13 01:53:08 -0600622 sd_bus_slot_unref(ipmid_slot);
Chris Austen0ba649e2015-10-13 12:28:13 -0500623 sd_bus_unref(bus);
624 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
625
vishwabmcba0bd5f2015-09-30 16:50:23 +0530626}