| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | // Qt-Security score:significant reason:default |
| 5 | |
| 6 | #include "qdbusserver.h" |
| 7 | #include "qdbusconnection_p.h" |
| 8 | #include "qdbusconnectionmanager_p.h" |
| 9 | #include "qdbusutil_p.h" |
| 10 | |
| 11 | #include <QtCore/private/qlocking_p.h> |
| 12 | |
| 13 | #ifndef QT_NO_DBUS |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | /*! |
| 18 | \class QDBusServer |
| 19 | \inmodule QtDBus |
| 20 | |
| 21 | \brief The QDBusServer class provides peer-to-peer communication |
| 22 | between processes on the same computer. |
| 23 | */ |
| 24 | |
| 25 | /*! |
| 26 | Constructs a QDBusServer with the given \a address, and the given |
| 27 | \a parent. |
| 28 | */ |
| 29 | QDBusServer::QDBusServer(const QString &address, QObject *parent) |
| 30 | : QObject(parent), d(nullptr) |
| 31 | { |
| 32 | if (address.isEmpty()) |
| 33 | return; |
| 34 | |
| 35 | if (!qdbus_loadLibDBus()) |
| 36 | return; |
| 37 | |
| 38 | QDBusConnectionManager *instance = QDBusConnectionManager::instance(); |
| 39 | if (!instance) |
| 40 | return; |
| 41 | |
| 42 | instance->createServer(address, server: this); |
| 43 | Q_ASSERT(d != nullptr); |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | Constructs a QDBusServer with the given \a parent. The server will listen |
| 48 | for connections in \c {/tmp} (on Unix systems) or on a TCP port bound to |
| 49 | localhost (elsewhere). |
| 50 | */ |
| 51 | QDBusServer::QDBusServer(QObject *parent) |
| 52 | : QDBusServer( |
| 53 | #ifdef Q_OS_UNIX |
| 54 | // Use Unix sockets on Unix systems only |
| 55 | QStringLiteral("unix:tmpdir=/tmp" ), |
| 56 | #else |
| 57 | QStringLiteral("tcp:" ), |
| 58 | #endif |
| 59 | parent) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | /*! |
| 64 | Destructs a QDBusServer |
| 65 | */ |
| 66 | QDBusServer::~QDBusServer() |
| 67 | { |
| 68 | if (!d) |
| 69 | return; |
| 70 | |
| 71 | auto manager = QDBusConnectionManager::instance(); |
| 72 | if (!manager) |
| 73 | return; |
| 74 | |
| 75 | QWriteLocker writeLocker(&d->lock); |
| 76 | manager->removeConnections(names: d->serverConnectionNames); |
| 77 | d->serverConnectionNames.clear(); |
| 78 | |
| 79 | d->serverObject = nullptr; |
| 80 | d->ref.storeRelaxed(newValue: 0); |
| 81 | d->deleteLater(); |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | Returns \c true if this QDBusServer object is connected. |
| 86 | |
| 87 | If it isn't connected, you need to call the constructor again. |
| 88 | */ |
| 89 | bool QDBusServer::isConnected() const |
| 90 | { |
| 91 | return d && d->server && q_dbus_server_get_is_connected(server: d->server); |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | Returns the last error that happened in this server. |
| 96 | |
| 97 | This function is provided for low-level code. |
| 98 | */ |
| 99 | QDBusError QDBusServer::lastError() const |
| 100 | { |
| 101 | return d ? d->lastError : QDBusError(QDBusError::Disconnected, QDBusUtil::disconnectedErrorMessage()); |
| 102 | } |
| 103 | |
| 104 | /*! |
| 105 | Returns the address this server is associated with. |
| 106 | */ |
| 107 | QString QDBusServer::address() const |
| 108 | { |
| 109 | QString addr; |
| 110 | if (d && d->server) { |
| 111 | char *c = q_dbus_server_get_address(server: d->server); |
| 112 | addr = QString::fromUtf8(utf8: c); |
| 113 | q_dbus_free(memory: c); |
| 114 | } |
| 115 | |
| 116 | return addr; |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | \since 5.3 |
| 121 | |
| 122 | If \a value is set to true, an incoming connection can proceed even if the |
| 123 | connecting client is not authenticated as a user. |
| 124 | |
| 125 | By default, this value is false. |
| 126 | |
| 127 | \sa isAnonymousAuthenticationAllowed() |
| 128 | */ |
| 129 | void QDBusServer::setAnonymousAuthenticationAllowed(bool value) |
| 130 | { |
| 131 | if (!d) |
| 132 | return; |
| 133 | |
| 134 | d->anonymousAuthenticationAllowed = value; |
| 135 | } |
| 136 | |
| 137 | /*! |
| 138 | \since 5.3 |
| 139 | |
| 140 | Returns true if anonymous authentication is allowed. |
| 141 | |
| 142 | \sa setAnonymousAuthenticationAllowed() |
| 143 | */ |
| 144 | bool QDBusServer::isAnonymousAuthenticationAllowed() const |
| 145 | { |
| 146 | if (!d) |
| 147 | return false; |
| 148 | |
| 149 | return d->anonymousAuthenticationAllowed; |
| 150 | } |
| 151 | |
| 152 | /*! |
| 153 | \fn void QDBusServer::newConnection(const QDBusConnection &connection) |
| 154 | |
| 155 | This signal is emitted when a new client connection \a connection is |
| 156 | established to the server. |
| 157 | */ |
| 158 | |
| 159 | QT_END_NAMESPACE |
| 160 | |
| 161 | #include "moc_qdbusserver.cpp" |
| 162 | |
| 163 | #endif // QT_NO_DBUS |
| 164 | |