Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
dbus: Add configuration options
Browse files Browse the repository at this point in the history
Make bus, bus name, and write access controllable via configuration
file.
Note that changing the bus to system usually requires additional setup
in the D-Bus configuration to allow access.
  • Loading branch information
flynd committed Oct 14, 2013
1 parent 452543d commit e8226b7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
11 changes: 6 additions & 5 deletions dbus/src/dbusinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ typedef std::map<DBusTimeout*,TimeoutHandler::Ptr> TimeoutMap;
class DbusInterface::Private
{
public:
Private(DbusInterface* interface, Licq::MainLoop& mainLoop, DbusCallback* callback)
: myInterface(interface), myMainLoop(mainLoop), myCallback(callback)
Private(DbusInterface* interface, Licq::MainLoop& mainLoop, DbusCallback* callback, bool systemBus)
: myInterface(interface), myMainLoop(mainLoop), myCallback(callback), mySystemBus(systemBus)
{ /* Empty */ }

// Callbacks from libdbus
Expand All @@ -113,6 +113,7 @@ class DbusInterface::Private
TimeoutMap myTimeouts;
int myNextWatchId;
int myNextTimeoutId;
bool mySystemBus;
};

} // namespace LicqDbus
Expand Down Expand Up @@ -435,8 +436,8 @@ std::string DbusInterface::decodeObjectPathPart(const std::string& s)
return ret;
}

DbusInterface::DbusInterface(Licq::MainLoop& mainLoop, DbusCallback* callback)
: myPrivate(new Private(this, mainLoop, callback))
DbusInterface::DbusInterface(Licq::MainLoop& mainLoop, DbusCallback* callback, bool systemBus)
: myPrivate(new Private(this, mainLoop, callback, systemBus))
{
LICQ_D();
d->myConn = NULL;
Expand All @@ -463,7 +464,7 @@ bool DbusInterface::connect()
// Connect to DBus
DBusError e;
dbus_error_init(&e);
d->myConn = dbus_bus_get_private(DBUS_BUS_SESSION, &e);
d->myConn = dbus_bus_get_private(d->mySystemBus ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &e);

if (dbus_error_is_set(&e))
{
Expand Down
2 changes: 1 addition & 1 deletion dbus/src/dbusinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class DbusInterface : private boost::noncopyable
*
* @param mainLoop Mainloop to service D-Bus
*/
DbusInterface(Licq::MainLoop& mainLoop, DbusCallback* callback);
DbusInterface(Licq::MainLoop& mainLoop, DbusCallback* callback, bool systemBus = false);

/**
* Destructor
Expand Down
46 changes: 35 additions & 11 deletions dbus/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <licq/contactlist/user.h>
#include <licq/contactlist/usermanager.h>
#include <licq/daemon.h>
#include <licq/inifile.h>
#include <licq/logging/log.h>
#include <licq/plugin/pluginmanager.h>
#include <licq/pluginsignal.h>
Expand All @@ -42,7 +43,9 @@
using namespace LicqDbus;

Plugin::Plugin()
: myConn(NULL)
: myConn(NULL),
myDbusName("org.licq"),
myReadOnly(false)
{
// Empty
}
Expand All @@ -56,9 +59,23 @@ int Plugin::run()
{
setSignalMask(Licq::PluginSignal::SignalUser);

bool useSystemBus = false;
Licq::IniFile conf("dbus.conf");
if (conf.loadFile())
{
conf.setSection("DBus");
conf.get("BusName", myDbusName, myDbusName);
conf.get("ReadOnly", myReadOnly, myReadOnly);

std::string bus;
conf.get("Bus", bus);
if (bus == "system")
useSystemBus = true;
}

myMainLoop.addRawFile(getReadPipe(), this);

myConn = new DbusInterface(myMainLoop, this);
myConn = new DbusInterface(myMainLoop, this, useSystemBus);
myConn->connect();

// Timer to try reconnecting if connection is lost
Expand Down Expand Up @@ -249,7 +266,7 @@ void Plugin::processSignal(const Licq::PluginSignal* sig)

void Plugin::dbusConnected()
{
if (!myConn->requestName("org.licq"))
if (!myConn->requestName(myDbusName))
Licq::gLog.warning("Failed to claim name on message bus");

// Signal clients that we are up and running
Expand All @@ -270,7 +287,7 @@ int Plugin::dbusMethod(const char* path, const char* iface, const char* member,
return DbusInterface::MethodReplied;
}

if (strcmp(member, "Shutdown") == 0)
if (!myReadOnly && strcmp(member, "Shutdown") == 0)
{
myConn->sendReply(msgref, NULL);
myConn->flush();
Expand Down Expand Up @@ -323,7 +340,7 @@ int Plugin::dbusMethod(const char* path, const char* iface, const char* member,
return DbusInterface::MethodReplied;
}

if (strcmp(member, "SetStatus") == 0 && userId.isOwner())
if (!myReadOnly && strcmp(member, "SetStatus") == 0 && userId.isOwner())
{
// Make sure owner exists
if (!Licq::gUserManager.userExists(userId))
Expand Down Expand Up @@ -388,12 +405,16 @@ std::string Plugin::dbusIntrospect(const char* path)
return "<node name=\"Core\"/><node name=\"ContactList\"/>";

if (strcmp(p, "/Core") == 0)
return "<interface name=\"org.licq.Core\">"
"<method name=\"GetVersion\"><arg type=\"s\" direction=\"out\"/></method>"
"<method name=\"Shutdown\"/>"
{
std::string s = "<interface name=\"org.licq.Core\">";
if (!myReadOnly)
s += "<method name=\"Shutdown\"/>";
s += "<method name=\"GetVersion\"><arg type=\"s\" direction=\"out\"/></method>"
"<signal name=\"Started\"/>"
"<signal name=\"Shutdown\"/>"
"</interface>";
return s;
}

// Everything below is for ContactList tree
if (strncmp(p, "/ContactList", 12) != 0)
Expand Down Expand Up @@ -432,13 +453,16 @@ std::string Plugin::dbusIntrospect(const char* path)
std::string s;

if (userId.isOwner())
{
s = "<interface name=\"org.licq.Account\">"
"<method name=\"SetStatus\">"
"<arg name=\"StatusBits\" type=\"u\" direction=\"in\"/>"
"</method>"
"<method name=\"GetContacts\">"
"<arg name=\"Contacts\" type=\"ao\" direction=\"out\"/>"
"</method>";
if (!myReadOnly)
s += "<method name=\"SetStatus\">"
"<arg name=\"StatusBits\" type=\"u\" direction=\"in\"/>"
"</method>";
}
else
s = "<interface name=\"org.licq.Contact\">";

Expand Down
2 changes: 2 additions & 0 deletions dbus/src/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Plugin : public Licq::GeneralPluginHelper, public Licq::MainLoopCallback,

DbusInterface* myConn;
Licq::MainLoop myMainLoop;
std::string myDbusName;
bool myReadOnly;
};


Expand Down

0 comments on commit e8226b7

Please sign in to comment.