summaryrefslogblamecommitdiffstats
path: root/src/pvs.cpp
blob: 2069e361f0d20f89a4f185b6e4cde39b448736a3 (plain) (tree)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629




















































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                        
/*
 # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 */

#include "pvs.h"
#include "src/util/dispatcher.h"
#include "src/net/pvsMsg.h"
#include "src/net/pvsServiceDiscovery.h"
#include "src/net/pvsDiscoveredServer.h"

// D-Bus
#include "pvsadaptor.h"

extern PVS *mainClient;

// !!STATION LOCK TESTING!!
//#define lock_test_unlock_time 3
// uncomment the following line to test locking in a local context
// it disables the lock of a station #(lock_test_unlock_time) seconds after it has been locked
//#define lock_test

PVS::PVS() :
        QObject()
{
    _pvsServerConnection = new PVSServerConnection(this);
    _locked = false;
    _vncAllowed = false;
    _vncRequested = false;
    readPolicyFiles();
    loadCommands();
    _blankScreen = NULL;
    _vncPort = -1;

    // add a notify to the allow file, so we get informed when the file is changed
    QString watchPath("/home/");
    watchPath.append(getUserName().append(QString("/.pvs/.allow")));
    _notify = new QFileSystemWatcher(this);
    _notify->addPath(QString(watchPath.toUtf8().data()));

    connect(_notify, SIGNAL(fileChanged(const QString &)), this,
            SLOT(fileChanged(const QString &)));

    // connect to D-Bus
    new PvsAdaptor(this);
    QDBusConnection dbus = QDBusConnection::sessionBus();
    dbus.registerObject("/", this);
    dbus.registerService("org.openslx.pvs");

    _sdClient = new PVSServiceDiscovery(this);

    _chat = new PVSChatClient();
    _chat->setServerConnection(_pvsServerConnection);
    _chat->getChatMsgHandler().addListener(this, &PVS::chat_receive);
    _chat->getChatCommandHandler().addListener(this, &PVS::UpdateChatClients);

    _timerLockTest = 0;
    _timerLockDelay = 0;

    //add signalhandling for sigterm signals
    struct sigaction act;
    act.sa_handler = &PVS::signalHandler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;

    //register signals with actions
    sigaction(SIGTERM, &act, 0);
    sigaction(SIGHUP, &act, 0);
    sigaction(SIGINT, &act, 0);
    sigaction(SIGQUIT, &act, 0);

}

PVS::~PVS()
{
    // make sure we dont leave a locked computer
    if (_notify)
        disconnect(_notify, SIGNAL(fileChanged(const QString &)), this,
                   SLOT(fileChanged(const QString &)));
    unlock();
    delete _sdClient;
}

void PVS::connectToHost(PVSDiscoveredServer* server, QString passwd)
{
    _pvsServerConnection->connectToServer(server, passwd);
}

/**
 * onCommand Callback method
 * handles incoming commands
 */
void PVS::onCommand(PVSMsg cmdMessage)
{
    QString message = cmdMessage.getMessage();
    QString ident = cmdMessage.getIdent();
    if (ident.compare("LOCKSTATION") == 0)
    {
        if (_blankScreen)
        {
            return;
        }
        else
        {
            _lockMsg = message;
            //TODO:@Javier: lock it eith message.
            // Sent message.
            if(message.size() > 0) // Lock client with message.
            {
                message = message + "\n \n WARNING: Your screen will be blocked in 10 seconds.";
                emit showMessage("Lock Message", message, true);
                _timerLockDelay = startTimer(10*1000); // 10 sec.
            }
            else // lock client without message.
            {
                lock();
            }
        }

        return;
    }
    if (ident.compare("UNLOCKSTATION") == 0)
    {
        _timerLockDelay = 0;
        if (!_blankScreen)
        {
            return;
        }
        else
        {
            ConsoleLog writeLine("Unlocked.");
            unlock();
        }
        return;
    }
    if (ident.compare("VNCREQUEST") == 0)
    {
        // a vnc connection was requested, so lets check if this is against our policies
    	_vncRequested = (message.compare("YES") == 0);
    	if (!_vncRequested) return;
		getVNCAllow();
        return;
    }
    if (ident.compare("PROJECT") == 0)
    {
        std::vector<QString> connDetails;
        std::string buf;
        std::stringstream ss(message.toStdString());
        while (ss >> buf)
            connDetails.push_back(QString::fromStdString(buf));

        if(connDetails.size() == 4) {
            int port, quality;
            bool fullscreen, smooth;
            fullscreen = smooth = true;
            port = connDetails[1].toInt();
            quality = connDetails[3].toInt();
            if(port > 0) {
                emit project(connDetails[0], port, connDetails[2], fullscreen, smooth, quality);
                lock_inputs();
                ConsoleLog writeNetwork(QString("Received Projection Information: Projsrc: ").append(connDetails[0]).append( " port: ").append(connDetails[1]).append(" pass:").append(connDetails[2]));
            }
            ConsoleLog writeNetwork(QString("Received illegal port! (").append(port).append(")"));
			}
    }
    if (ident.compare("UNPROJECT") == 0)
    {
        emit unproject();
        unlock();
        return;
    }

#ifdef never
    // prototype
    if (command.compare(<<string>>) == 0)
    {
        // do stuff
        return;
    }
#endif
}

/**
 * onLogin Callback method
 * handles the servers login messages (see PVSListenServer)
 */
void PVS::onLogin(PVSMsg login)
{
    if (login.getIdent().compare(QString("ACCEPTED")) == 0)
    {
        ConsoleLog writeNetwork("Server accepted connection.");
        login.getMessage();
    }
    else if (login.getIdent().compare(QString("DENIED")) == 0)
    {

        ConsoleLog writeNetwork(QString("Sent Username."));
    }
}

void PVS::chat_send(QString nick_to, QString nick_from, QString msg)
{
    _chat->send(nick_to, msg);
}

void PVS::UpdateChatClients(PVSChatMsg chatCommand)
{
    if(chatCommand.getCommand().compare("clientToAdd") == 0)
    {
        _chat->addClient(chatCommand.getUsername(), chatCommand.getIp());
        emit chat_client_add(chatCommand.getUsername());
    }

    if(chatCommand.getCommand().compare("clientToRemove") == 0)
    {
        _chat->removeClient(chatCommand.getUsername());
        emit chat_client_remove(chatCommand.getUsername());
    }

    if(chatCommand.getCommand().compare("assignedName") == 0)
    {
        _chat->setSource(chatCommand.getUsername());
    }
}

void PVS::chat_receive(PVSChatMsg msg)
{
	emit chat_receive(msg.getNickFrom(), msg.getNickTo(), msg.getMsg());
}

/**
 * onDaemon Callback method
 * handles incoming UDS Communicator messages
 */
void PVS::onDaemonCommand(PVSMsg msg)
{
    if (msg.getIdent().compare(QString("stop")) == 0)
    {
        stop();
        unlock();
        exit(0);
    }
}

void PVS::onMessage(PVSMsg msg)
{
	QString ident = msg.getIdent();
	QString message = msg.getMessage();

	if (ident.compare("BROADCAST") == 0)
	{
		emit showMessage(tr("Message"), message, true);
	}
}

/**
 * load custom commands by adding callbacks to the dispatchers
 */
void PVS::loadCommands()
{
    if (_pvsServerConnection)
    {
        _pvsServerConnection->addCommandHandler("*", this, &PVS::onCommand);
        _pvsServerConnection->addLoginHandler("*", this, &PVS::onLogin);
        _pvsServerConnection->addChatHandler("*", this, &PVS::onMessage);
    }
}

void PVS::quit()
{
    stop();
    unlock();
    exit(0);
}

/**
 * stops the client
 */
void PVS::stop()
{
    _pvsServerConnection->disconnectFromServer();
    stopVNCScript();
}

/**
 * used to unlock the client after 3 seconds
 * this a maintainance method
 * uncomment the define to use it
 */
void PVS::timerEvent(QTimerEvent *event)
{
#ifdef lock_test
    if (_locked && event->timerId()== _timerLockTest) unlock();
#endif
    if(_timerLockDelay == event->timerId())
    {
        if (lock()) ConsoleLog writeLine(QString("Locked."));
        _timerLockDelay = 0;
    }
    killTimer(event->timerId());
}

bool PVS::allowExists()
{
    return policyFileExists(QString(".allow"));
}

/**
 *  check whether we want to allow vnc connections to this client
 */
bool PVS::getVNCAllow()
{
    if (allowExists())
    {
        if (getAllowed())
        {
            _vncAllowed = true;
        }
        else
        {
            _vncAllowed = false;
        }
    }
    else
    {
        ConsoleLog writeError("No .allow file found.");
        _vncAllowed = false;
        //make sure the vncsever is off
		ConsoleLog writeError("Shutting down vnc-server because we have no .allow file.");
    }

    if (_vncAllowed && _vncRequested)
    {
        // then boot up the vnc-server and set password and port
        int result = bootVNC();
        if (result == 0)
        {
            // now inform the server
            _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "PORT", int2String(_vncPort)));
            _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "PASSWORD", _vncPassword));
            _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "RWPASSWORD", _vncRwPassword));
            _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "VNC","YES"));
            ConsoleLog writeNetwork(QString("Sent password, port and granted access."));
            // show message on clientgui
            emit showMessage(tr("VNC connection"), tr("The host ")
                    .append(_pvsServerConnection->getServerName()
                            .append(tr(" requested your screen!"))));
        }
        _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "VNCSRVRESULT", int2String(result)));

    }
    else if (!_vncAllowed)
    {
    	stopVNCScript();
        _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "VNC","NO"));
    }
    return _vncAllowed;
}

/**
 *  read the policy files
 */
void PVS::readPolicyFiles()
{
    getVNCAllow();
    // Add more policyfiles here if any
}

/**
 *  callback which gets invoked when the watched file is changed
 */
void PVS::fileChanged(const QString& path)
{
    getVNCAllow();
}

/**
 *  lock the client
 */
bool PVS::lock()
{

#ifdef lock_test
    _timerLockTest = startTimer(lock_test_unlock_time * 1000);
    printf("will auto-unlock in %d secs\n", int(lock_test_unlock_time));
#endif

    if (!_blankScreen)
    {
        _blankScreen = new BlankScreen();
        if (_blankScreen)
            return _locked = _blankScreen->lock();
        else
            return false;
    }
    return true;
}

/**
 *  lock the client inputs only
 */
bool PVS::lock_inputs()
{

#ifdef lock_test
    _timerLockTest = startTimer(lock_test_unlock_time * 1000);
    printf("will auto-unlock in %d secs\n", int(lock_test_unlock_time));
#endif

    if (!_blankScreen)
    {
        _blankScreen = new BlankScreen();
        if (_blankScreen)
            return _locked = _blankScreen->lock_inputs();
        else
            return false;
    }
    return true;
}

/**
 *  unlock the client
 */
bool PVS::unlock()
{
    if (_blankScreen)
    {
        if (_blankScreen->unlock())
        {
            delete _blankScreen;
            _blankScreen = NULL;
        }
    }
    return false;
}

void PVS::setScriptPath(QString path)
{
    _vncScriptPath = path;
    _vncScriptName = getFilenameFromPath(_vncScriptPath);
}

bool PVS::gotVNCScript()
{
    if (fileExists(_vncScriptPath))
        return true;
    if (policyFileExists(_vncScriptName))
        return true;
    return false;

}
/**
 * setup password and port
 * the local_test define is used to enable local testing modes without invoking the vnc-script
 */
int PVS::bootVNC()
{
    //    #define local_test

    stopVNCScript(); // just to be sure
#ifndef local_test
    _vncPassword = int2String(getRandom(0, 99999999));
    do {
    	_vncRwPassword = int2String(getRandom(0, 99999999));
    } while (_vncRwPassword == _vncPassword);

    _vncPort = getRandom(5901, 6000);
    return startVNCScript(_vncPort, _vncPassword, _vncRwPassword);
#else
    _vncPassword = QString("1234");
    _vncPort = 5901;
    return startVNCScript(_vncPort, _vncPassword);
#endif
    //#undef local_test
}

/**
 * launch the vnc script (if present) to start the vnc-server
 */
int PVS::startVNCScript(int port, QString pass, QString rwpass)
{
    if (gotVNCScript())
    {
        ConsoleLog writeLine("Starting VNC-Script");
        QString commandLine(_vncScriptPath);
        commandLine.append(" ");
        commandLine.append("start");
        commandLine.append(" ");
        commandLine.append(int2String(port));
        commandLine.append(" ");
        commandLine.append(pass);
        commandLine.append(" ");
        commandLine.append(rwpass);
        if (!system(NULL))
            ConsoleLog writeError("No Command processor available");

        int result = system(commandLine.toUtf8().data());
        result = WEXITSTATUS(result);
        if (result != 0)
            ConsoleLog writeError(QString("VNC-Server is not running, code: ") + int2String(result));
        else
            ConsoleLog writeLine("VNC-Server should be running");
        return result;
    }
    else
    {
        ConsoleLog writeError("VNC-Script not found.");
        return 3;
    }
}

/**
 * launch the vnc script (if present) to stop the vnc-server
 */
int PVS::stopVNCScript()
{
    if (gotVNCScript())
    {
        ConsoleLog writeLine("Stopping VNC-Script");
        QString commandLine(_vncScriptPath);
        commandLine.append(" ");
        commandLine.append("stop");
        commandLine.append(" ");
        commandLine.append("dummy");
        commandLine.append(" ");
        commandLine.append("dummy");
        ConsoleLog writeLine("VNC-Server should be stopped");
        int result = system(commandLine.toUtf8().data());
        return WEXITSTATUS(result);
    }
    else
    {
        return 3;
    }
}

void PVS::start()
{
    _pvsServerConnection->sendMessage(PVSMsg(PVSCOMMAND, "PROJECTING", "YES"));
}

void PVS::onConnected(QString name)
{
    emit connected(name);
}

void PVS::onDisconnected()
{
	emit disconnected();
	_chat->clearClients();
	stopVNCScript();
}

QString PVS::isConnected()
{
    return _pvsServerConnection->getServerName();
}

QString PVS::chat_getNickname()
{
    return _chat->getSource();
}

QStringList PVS::chat_getNicknames()
{
	return _chat->getClients();
}

void PVS::pvsConnect(QString host, QString passwd)
{
	_pvsServerConnection->disconnectFromServer();
    _sdClient->connectToSession(host, passwd);
}
void PVS::pvsDisconnect()
{
	_chat->clearClients();
	_sdClient->connectToSession("", "");
	stop();
}


QStringList PVS::getAvailableHosts()
{
    return _sdClient->getAllServers();
}

void PVS::guiAddHost(QString host)
{
	emit addHost(host);
}

void PVS::guiDelHost(QString host)
{
	emit delHost(host);
}

QString PVS::getIpByNick(QString nick)
{
    return _chat->getIPFromUsername(nick);
}

//Handles signals (SIGTERM and SIGHUP)
void PVS::signalHandler(int signal)
{
    ConsoleLog writeLine(QString("Received Signal ").append (int2String(signal)));
    switch (signal) {
        case SIGHUP:
            mainClient->quit();
            break;
        case SIGTERM:
            mainClient->quit();
            break;
        case SIGINT:
            mainClient->quit();
            break;
        case SIGQUIT:
            mainClient->quit();
            break;
    }

}