summaryrefslogtreecommitdiffstats
path: root/tdm/cryptocardwatcher/watcher.cpp
blob: ecd4e8f3c8aff881649a7d5e4ee8eda2713fbf08 (plain)
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
/*
 * Copyright 2015 Timothy Pearson <[email protected]>
 *
 * This file is part of cryptocardwatcher, the TDE Cryptographic Card Session Monitor
 *
 * cryptocardwatcher 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 3
 * of the License, or (at your option) any later version.
 *
 * cryptocardwatcher 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 cryptocardwatcher. If not, see http://www.gnu.org/licenses/.
 */

#include "watcher.h"

#include <ksslcertificate.h>

#include <tdehardwaredevices.h>
#include <tdecryptographiccarddevice.h>

#include <dmctl.h>
#include <kuser.h>

CardWatcher::CardWatcher() : TQObject() {
	//
}

CardWatcher::~CardWatcher() {
	//
}

void CardWatcher::cryptographicCardInserted(TDECryptographicCardDevice* cdevice) {
	TQString login_name = TQString::null;
	X509CertificatePtrList certList = cdevice->cardX509Certificates();
	if (certList.count() > 0) {
		KSSLCertificate* card_cert = NULL;
		card_cert = KSSLCertificate::fromX509(certList[0]);
		TQStringList cert_subject_parts = TQStringList::split("/", card_cert->getSubject(), false);
		for (TQStringList::Iterator it = cert_subject_parts.begin(); it != cert_subject_parts.end(); ++it ) {
			TQString lcpart = (*it).lower();
			if (lcpart.startsWith("cn=")) {
				login_name = lcpart.right(lcpart.length() - strlen("cn="));
			}
		}
		delete card_cert;
	}

	if (login_name != "") {
		// Determine if user already has an active session
		DM dm;
		SessList sess;
		bool user_active = false;
		bool unused_session_available = false;
		bool unused_session_active = false;
		int unused_session_vt_number = -1;
		if (dm.localSessions(sess)) {
			TQString user, loc;
			for (SessList::ConstIterator it = sess.begin(); it != sess.end(); ++it) {
				DM::sess2Str2(*it, user, loc);
				if (user.startsWith(login_name + ": ")) {
					// Found active session
					user_active = true;

					// Switch VTs
					DM().switchVT((*it).vt);

					break;
				}
				if (user == "Unused") {
					// Found active unused session
					unused_session_available = true;
					unused_session_vt_number = (*it).vt;
					if ((*it).vt == dm.activeVT()) {
						unused_session_active = true;
						break;
					}
				}
			}
		}
		if (!user_active || unused_session_available) {
			if (unused_session_available) {
				if (!unused_session_active) {
					// Switch to unused VT
					DM().switchVT(unused_session_vt_number);
				}
			}
			else {
				// Activate new VT
				DM().startReserve();
			}
		}
	}
}

void CardWatcher::cryptographicCardRemoved(TDECryptographicCardDevice* cdevice) {
	//
}

#include "watcher.moc"