1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
kwrited is a write(1) receiver for KDE.
Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
Copyright 2008 by George Kiagiadakis <gkiagia@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "kwrited.h"
#include <QDebug>
#include <kptydevice.h>
#include <kuser.h>
#include <knotification.h>
#include <klocalizedstring.h>
#include <kaboutdata.h>
#if defined(BUILD_AS_EXECUTABLE)
# include <QApplication>
# include <QSessionManager>
# include <signal.h>
# include <sys/types.h>
# include <unistd.h>
#else
# include <kpluginfactory.h>
# include <kpluginloader.h>
#endif
static inline KAboutData aboutData()
{
return KAboutData("kwrited", i18n("kwrited"), PROJECT_VERSION);
}
#if defined(BUILD_AS_EXECUTABLE)
static uid_t original_euid;
static gid_t original_egid;
static void sigterm_handler(int signal)
{
Q_UNUSED(signal)
QApplication::quit();
}
int main(int argc, char **argv)
{
//drop elevated privileges temporarily
original_euid = geteuid();
original_egid = getegid();
seteuid(getuid());
setegid(getgid());
//install a signal handler to exit gracefully
//(so that pty->logout() is called in ~KWrited())
signal(SIGTERM, sigterm_handler);
signal(SIGINT, sigterm_handler);
signal(SIGHUP, sigterm_handler);
KAboutData::setApplicationData(aboutData());
QApplication::setDesktopSettingsAware(false);
QApplication a(argc, argv);
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
QGuiApplication::setFallbackSessionManagementEnabled(false);
#endif
auto disableSessionManagement = [](QSessionManager &sm) {
sm.setRestartHint(QSessionManager::RestartNever);
};
QObject::connect(&a, &QGuiApplication::commitDataRequest, disableSessionManagement);
QObject::connect(&a, &QGuiApplication::saveStateRequest, disableSessionManagement);
KWrited w;
return a.exec();
}
#else // BUILD_AS_EXECUTABLE
KWritedModule::KWritedModule(QObject* parent, const QList<QVariant>&)
: KDEDModule(parent)
{
pro = new KWrited;
}
KWritedModule::~KWritedModule()
{
delete pro;
}
K_PLUGIN_FACTORY_WITH_JSON(KWritedFactory,
"kwrited.json",
registerPlugin<KWritedModule>();
)
#endif //BUILD_AS_EXECUTABLE
KWrited::KWrited() : QObject()
{
pty = new KPtyDevice();
pty->open();
#if defined(BUILD_AS_EXECUTABLE)
dup2(pty->slaveFd(), 0); //HACK: login() from glibc requires this to login correctly
//restore elevated privileges
seteuid(original_euid);
setegid(original_egid);
#endif
pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY"));
#if defined(BUILD_AS_EXECUTABLE)
//drop privileges again
seteuid(getuid());
setegid(getgid());
#endif
connect(pty, SIGNAL(readyRead()), this, SLOT(block_in()));
//qDebug() << "listening on device" << pty->ttyName();
}
KWrited::~KWrited()
{
#if defined(BUILD_AS_EXECUTABLE)
//restore elevated privileges
seteuid(original_euid);
setegid(original_egid);
#endif
pty->logout();
#if defined(BUILD_AS_EXECUTABLE)
//drop privileges again
seteuid(getuid());
setegid(getgid());
#endif
delete pty;
}
void KWrited::block_in()
{
QByteArray buf = pty->readAll();
QString msg = QString::fromLocal8Bit( buf.constData(), buf.size() );
msg.remove('\r');
msg.remove('\a');
KNotification *notification = new KNotification("NewMessage", 0, KNotification::Persistent);
#if !defined(BUILD_AS_EXECUTABLE)
notification->setComponentName( QStringLiteral("kwrited") );
#endif
notification->setText( msg );
connect(notification, SIGNAL(closed()), notification, SLOT(deleteLater()) );
notification->sendEvent();
}
#if !defined(BUILD_AS_EXECUTABLE)
#include "kwrited.moc"
#endif
|