-
Notifications
You must be signed in to change notification settings - Fork 2
/
GreeterConnection.cpp
221 lines (177 loc) · 6.18 KB
/
GreeterConnection.cpp
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
/*
* Copyright (c) 2016 Michal Srb <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <vector>
#include "helper.h"
#include "Configuration.h"
#include "GreeterConnection.h"
#include "GreeterManager.h"
#include "Log.h"
#include "Xvnc.h"
GreeterConnection::GreeterConnection(GreeterManager &greeterManager, std::string display, std::string xauthFilename, NewSessionHandler newSessionHandler, OpenSessionHandler openSessionHandler)
: m_greeterManager(greeterManager)
, m_newSessionHandler(newSessionHandler)
, m_openSessionHandler(openSessionHandler)
, m_in(&m_greeterOut)
, m_out(&m_greeterIn)
{
int greeterStdinPipe[2];
if (pipe(greeterStdinPipe) < 0) {
throw_errno();
}
int greeterStdoutPipe[2];
if (pipe(greeterStdoutPipe) < 0) {
throw_errno();
}
pid_t pid = fork();
if (pid < 0) {
throw_errno();
}
if (pid == 0) {
// Set the pipes to stdin and stdout
close(greeterStdinPipe[1]);
close(greeterStdoutPipe[0]);
close(0);
close(1);
dup2(greeterStdinPipe[0], 0);
dup2(greeterStdoutPipe[1], 1);
// Remove all signal handlers
sigset_t sigmask;
sigemptyset(&sigmask);
sigprocmask(SIG_SETMASK, &sigmask, nullptr);
// Prepare arguments and environment
std::string greeterBinary = Configuration::options["greeter"].as<std::string>();
const char *const argv[] = { greeterBinary.c_str(), nullptr };
std::string envDisplay = "DISPLAY=" + display;
std::string envXauthority = "XAUTHORITY=" + xauthFilename;
const char *const envp[] = { envDisplay.c_str(), envXauthority.c_str(), nullptr };
// Execute
if (execve(greeterBinary.c_str(), const_cast<char *const *>(argv), const_cast<char *const *>(envp)) < 0) { // Note: const_cast is ok here, the execve's envp parameter could be const, but it isn't for compability reasons.
exit(1);
}
}
m_greeterPID = pid;
close(greeterStdinPipe[0]);
close(greeterStdoutPipe[1]);
m_greeterStdin = greeterStdinPipe[1];
m_greeterStdout = greeterStdoutPipe[0];
m_greeterIn.open(boost::iostreams::file_descriptor_sink(m_greeterStdin, boost::iostreams::never_close_handle));
m_greeterOut.open(boost::iostreams::file_descriptor_source(m_greeterStdout, boost::iostreams::never_close_handle));
Log::debug() << "Spawned greeter (pid: " << m_greeterPID << ", display: " << display << ")" << std::endl;
}
GreeterConnection::~GreeterConnection()
{
Log::debug() << "Terminating greeter " << (m_dead ? "(already dead)" : "") << "(pid: " << m_greeterPID << ")" << std::endl;
if (!m_dead) {
kill(m_greeterPID, SIGTERM);
}
close(m_greeterStdin);
close(m_greeterStdout);
}
void GreeterConnection::update()
{
if (m_dead) {
throw std::runtime_error("Greeter died unexpectedly.");
}
int currentSessionListVersion = m_greeterManager.sessionListVersion();
if (m_lastSentSessionListVersion < currentSessionListVersion) {
m_lastSentSessionListVersion = currentSessionListVersion;
sendSessions();
}
}
void GreeterConnection::prepareSelect(ReadSelector &readSelector)
{
readSelector.addFD(m_greeterStdout, std::bind(&GreeterConnection::receive, this));
}
void GreeterConnection::askForPassword(GreeterConnection::PasswordHandler passwordHandler)
{
m_passwordHandler = passwordHandler;
m_out << "GET PASSWORD\n";
m_out.flush();
}
void GreeterConnection::askForCredentials(GreeterConnection::CredentialsHandler credentialsHandler)
{
m_credentialsHandler = credentialsHandler;
m_out << "GET CREDENTIALS\n";
m_out.flush();
}
void GreeterConnection::showError(std::string error)
{
m_out << "ERROR\n";
m_out << error;
m_out << "\nEND ERROR\n";
m_out.flush();
}
void GreeterConnection::sendSessions()
{
auto sessions = m_greeterManager.sessionList();
int count = std::count_if(sessions.begin(), sessions.end(), [](const XvncManager::XvncMap::value_type & pair) {
return pair.second->visible();
});
m_out << "SESSIONS\n" << count << "\n";
for (auto iter : sessions) {
if (iter.second->visible()) {
m_out << iter.first << " " << iter.second->sessionUsername() << " " << iter.second->desktopName() << "\n";
}
}
m_out.flush();
}
void GreeterConnection::receive()
{
std::string cmd;
m_in >> cmd;
if (cmd == "NEW") {
m_newSessionHandler();
return;
}
if (cmd == "OPEN") {
int id;
m_in >> id;
m_openSessionHandler(id);
return;
}
if (cmd == "PASSWORD") {
std::string password;
m_in >> password;
m_passwordHandler(password);
return;
}
if (cmd == "CREDENTIALS") {
std::string username;
m_in >> username;
std::string password;
m_in >> password;
m_credentialsHandler(username, password);
return;
}
}
void GreeterConnection::markDead()
{
Log::debug() << "Greeter died (pid: " << m_greeterPID << ")" << std::endl;
m_dead = true;
}