-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell.cpp
325 lines (296 loc) · 8.68 KB
/
Shell.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <sstream>
#include <fstream>
#include <iostream>
#include "Parser.h"
#include "Shell.h"
#include "Client.h"
std::map<std::string, Spatch::Ssh::Shell::parsingCallback> Spatch::Ssh::Shell::_commands = std::map<std::string, Spatch::Ssh::Shell::parsingCallback>();
std::map<std::string, Spatch::Ssh::Shell::parsingCallback> Spatch::Ssh::Shell::_admincommands = std::map<std::string, Spatch::Ssh::Shell::parsingCallback>();
Spatch::Ssh::Shell::Shell(Spatch::Configuration::Config& conf, Spatch::Ssh::Client& client)
: _conf(conf), _client(client)
{
if (_commands.empty())
{
_commands["list"] = &Spatch::Ssh::Shell::list;
_commands["connect"] = &Spatch::Ssh::Shell::connect;
_commands["help"] = &Spatch::Ssh::Shell::help;
_commands["top"] = &Spatch::Ssh::Shell::top;
}
if (_admincommands.empty())
{
_admincommands["list"] = &Spatch::Ssh::Shell::adminList;
_admincommands["reload"] = &Spatch::Ssh::Shell::reloadConfig;
}
}
Spatch::Ssh::Shell::~Shell()
{
}
void Spatch::Ssh::Shell::onWelcome()
{
writeString("Welcome on Spatch Server.\n");
writeString("Spatch is an SSH Proxy.\n\n");
printHelp();
printPrompt();
}
void Spatch::Ssh::Shell::printPrompt()
{
writeString("Spatch > ");
}
void Spatch::Ssh::Shell::printHelp()
{
std::stringstream ss;
ss << "Here is a list of available commands:" << std::endl;
ss << "\tlist accesses" << std::endl;
ss << "\tconnect [ACCESS ID]" << std::endl;
ss << "\ttop [ACCESS ID]" << std::endl;
ss << "\thelp" << std::endl;
if (_client.getUser()->getPrivilege() == Spatch::Configuration::User::Privilege::Admin)
{
ss << "\t--- ADMIN commands ---" << std::endl;
ss << "\tadmin list users" << std::endl;
ss << "\tadmin list servers" << std::endl;
ss << "\tadmin list accesses [USER ID]" << std::endl;
ss << "\tadmin reload" << std::endl;
}
writeString(ss.str());
}
int Spatch::Ssh::Shell::onRead()
{
char buf[2048];
int sz = 0;
sz = read(_client.getSlaveFd(), buf, 2048);
if (sz <= 0)
return sz;
buf[sz - 1] = '\0';
std::string input(buf);
std::istringstream iss(input);
std::string token;
iss >> token;
if (token.length() == 0)
{
printPrompt();
return 0;
}
for (auto p : _commands)
{
if (token == p.first)
return ((this->*p.second)(iss));
}
if (_client.getUser()->getPrivilege() == Spatch::Configuration::User::Privilege::Admin)
{
if (token == "admin")
{
iss >> token;
for (auto p : _admincommands)
{
if (token == p.first)
return ((this->*p.second)(iss));
}
}
}
writeString("Error: Command not found.\n");
printPrompt();
return sz;
}
int Spatch::Ssh::Shell::writeString(const std::string &str)
{
return (write(_client.getSlaveFd(), str.c_str(), str.length()));
}
/*
* User parsing function
*/
int Spatch::Ssh::Shell::list(std::istringstream &iss)
{
std::string token;
iss >> token;
if (token == "accesses")
listAccesses();
else
writeString("Error: Command not found.\n");
printPrompt();
return 0;
}
int Spatch::Ssh::Shell::listAccesses()
{
std::stringstream ss;
ss << "The list of your available accesses:" << std::endl;
for (auto a : _conf.getAccesses())
{
if (a->getUser() == _client.getUser())
{
ss << "\t[" << a->getId() << "] ";
if (a->getServer()->getName() != "")
ss << "[" << a->getServer()->getName() << "] ";
else
ss << "[" << a->getServer()->getIp() << "] ";
if (a->getCredentialType() == Spatch::Configuration::Access::Provided)
ss << "as [" << a->getCredential()->getUsername() << "]" << std::endl;
else if (a->getCredentialType() == Spatch::Configuration::Access::Asked)
ss << "with asked credentials" << std::endl;
else
ss << "with your own credentials" << std::endl;
}
}
writeString(ss.str());
}
int Spatch::Ssh::Shell::connect(std::istringstream &iss)
{
std::string id;
iss >> id;
for (auto a : _conf.getAccesses())
{
if (a->getUser() == _client.getUser())
{
if (a->getId() == id)
{
_client.proxify(a);
return 0;
}
}
}
printPrompt();
return 0;
}
int Spatch::Ssh::Shell::top(std::istringstream &iss)
{
std::string id;
iss >> id;
for (auto a : _conf.getAccesses())
{
if (a->getUser() == _client.getUser())
{
if (a->getId() == id)
{
_client.proxify(a, "top");
return 0;
}
}
}
printPrompt();
return 0;
}
int Spatch::Ssh::Shell::help(std::istringstream &iss)
{
printHelp();
printPrompt();
return 0;
}
/*
* Admin parsing function
*/
int Spatch::Ssh::Shell::adminList(std::istringstream &iss)
{
std::string token;
iss >> token;
if (token == "servers")
adminListServers();
else if (token == "users")
adminListUsers();
else if (token == "accesses")
adminListAccesses(iss);
else
writeString("Error: Command not found.\n");
printPrompt();
return 0;
}
int Spatch::Ssh::Shell::adminListServers()
{
std::stringstream ss;
ss << "Here is the list of available servers: " << std::endl;
for (std::shared_ptr<Spatch::Configuration::Server> s : _conf.getServers())
{
ss << "\t[" << s->getId() << "] [";
if (s->getName().length())
ss << s->getName();
else
ss << "unnamed";
ss << "] [" << s->getIp() << ":" << s->getPort() << "]" << std::endl;
}
writeString(ss.str());
return 0;
}
int Spatch::Ssh::Shell::adminListUsers()
{
std::stringstream ss;
ss << "Here is the list of registered users: " << std::endl;
ss << "\t--- Basic users ---" << std::endl;
for (std::shared_ptr<Spatch::Configuration::User> u : _conf.getUsers())
{
if (u->getPrivilege() == Spatch::Configuration::User::Privilege::BasicUser)
{
ss << "\t[" << u->getId() << "] ";
ss << "[" << u->getUsername() << ":" << u->getPassword() << "]" << std::endl;
}
}
ss << "\t--- Admins ---" << std::endl;
for (std::shared_ptr<Spatch::Configuration::User> u : _conf.getUsers())
{
if (u->getPrivilege() == Spatch::Configuration::User::Privilege::Admin)
{
ss << "\t[" << u->getId() << "] ";
ss << "[" << u->getUsername() << ":" << u->getPassword() << "]" << std::endl;
}
}
writeString(ss.str());
return 0;
}
int Spatch::Ssh::Shell::adminListAccesses(std::istringstream &iss)
{
std::stringstream ss;
std::string token;
std::shared_ptr<Spatch::Configuration::User> user;
iss >> token;
for (std::shared_ptr<Spatch::Configuration::User> u : _conf.getUsers())
{
if (u->getId() == token)
{
user = u;
break;
}
}
if (!user)
{
writeString("Error: User Id not found.");
return 0;
}
ss << "The list of " << user->getUsername() << "'s accesses:" << std::endl;
for (auto a : _conf.getAccesses())
{
if (a->getUser() == user)
{
ss << "\t[" << a->getId() << "] ";
if (a->getServer()->getName() != "")
ss << "[" << a->getServer()->getName() << "] ";
else
ss << "[" << a->getServer()->getIp() << "] ";
if (a->getCredentialType() == Spatch::Configuration::Access::Provided)
ss << "as [" << a->getCredential()->getUsername() << "]" << std::endl;
else if (a->getCredentialType() == Spatch::Configuration::Access::Asked)
ss << "with asked credentials" << std::endl;
else
ss << "with its own credentials" << std::endl;
}
}
writeString(ss.str());
return 0;
}
int Spatch::Ssh::Shell::reloadConfig(std::istringstream &)
{
std::filebuf fb;
std::istream is(&fb);
Spatch::Parsing::Parser parser(_conf);
_conf.clearAll();
if (fb.open("spatch.conf",std::ios::in))
{
parser.parse(is);
fb.close();
}
writeString("Config reloaded !\n");
printPrompt();
return 0;
}