-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppConfigManager.cpp
545 lines (472 loc) · 15.2 KB
/
AppConfigManager.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
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
/*
* AppConfigManager.cpp
* PyRideServer
*
* Created by Xun Wang on 17/08/12.
* Copyright 2012 Galaxy Network. All rights reserved.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <stdio.h>
#include "AppConfigManager.h"
namespace pyride {
static const char rnd_seed[] = "This is my randomn seed generator";
AppConfigManager * AppConfigManager::s_instance = NULL;
AppConfigManager * AppConfigManager::instance()
{
if (!s_instance)
s_instance = new AppConfigManager();
return s_instance;
}
AppConfigManager::AppConfigManager() :
clientID_( (1 << 4) | BlueTeam ), // blue team 1
allowPythonTelnet_( true )
{
RAND_seed( rnd_seed, sizeof( rnd_seed ) );
defaultPose_.x = defaultPose_.y = defaultPose_.theta = 0.0;
}
void AppConfigManager::loadConfigFromFile( const char * fileName )
{
struct stat sb;
if (stat( fileName, &sb ) == -1 || !S_ISREG( sb.st_mode )) {
ERROR_MSG( "LoadConfig: unable to find configuration file %s\n",
fileName );
return;
}
TiXmlDocument doc( fileName );
if (!doc.LoadFile()) {
ERROR_MSG( "LoadConfig: "
"Unable to load user inforamtion file %s: "
"Error %s.\n", fileName, doc.ErrorDesc() );
return;
}
configFileName_ = fileName;
TiXmlNode * rootNode = NULL;
rootNode = doc.FirstChild( "PyRIDE" );
if (!rootNode) {
ERROR_MSG( "LoadConfig: "
"Invalid XML file %s.", fileName );
return;
}
loadRobotInfo( rootNode );
loadUserInfo( rootNode );
loadDeviceInfo( rootNode );
}
void AppConfigManager::loadRobotInfo( TiXmlNode * robotInfoNode )
{
TiXmlElement * teamColourElem = NULL;
TiXmlElement * memberIDElem = NULL;
TiXmlElement * defaultPoseElem = NULL;
TiXmlElement * enablePySvrElem = NULL;
char * colourText = NULL;
char * idText = NULL;
char * posText = NULL;
char * pySvrText = NULL;
int teamColour = -1;
int memberID = -1;
float posx = 0.0, posy = 0.0;
teamColourElem = robotInfoNode->FirstChildElement( "TeamColour" );
memberIDElem = robotInfoNode->FirstChildElement( "MemberID" );
if (!teamColourElem || !memberIDElem) {
ERROR_MSG( "LoadConfig: "
"missing team member info." );
goto getpos;
}
colourText = (char *)teamColourElem->GetText();
idText = (char *)memberIDElem->GetText();
if (colourText == NULL || idText == NULL) {
ERROR_MSG( "LoadConfig: "
"invalid team member info." );
goto getpos;
}
if (strcasecmp( colourText, "blue" ) == 0) {
teamColour = BlueTeam;
}
else if (strcasecmp( colourText, "pink" ) == 0){
teamColour = PinkTeam;
}
memberID = atoi( idText );
if (teamColour == -1 || memberID > 5 || memberID < 0) {
ERROR_MSG( "LoadConfig: "
"invalid team member info." );
goto getpos;
}
clientID_ = (memberID & 0xf) << 4 | teamColour;
getpos:
defaultPoseElem = robotInfoNode->FirstChildElement( "DefaultPosition" );
if (!defaultPoseElem) {
WARNING_MSG( "LoadConfig: "
"missing default position." );
goto getpysvr;
}
posText = (char*)defaultPoseElem->GetText();
if (!posText || sscanf( posText, "%f %f", &posx, &posy ) == 0) {
ERROR_MSG( "LoadConfig: "
"invalid default position." );
goto getpysvr;
}
defaultPose_.x = posx; defaultPose_.y = posy;
getpysvr:
enablePySvrElem = robotInfoNode->FirstChildElement( "RemotePythonAccess" );
pySvrText = (char*)enablePySvrElem->GetText();
allowPythonTelnet_ = (pySvrText && (strcasecmp( pySvrText, "enable" ) == 0));
}
void AppConfigManager::loadUserInfo( TiXmlNode * userInfoNode )
{
if (!userInfoNode)
return;
TiXmlNode * rootNode = NULL;
TiXmlNode * userNode = NULL;
rootNode = userInfoNode->FirstChild( "UserInfo" );
if (!rootNode) {
WARNING_MSG( "LoadConfig: "
"Configuration contains no user access info.\n" );
return;
}
userNode = rootNode->FirstChild( "User" );
int loadErrors = 0;
int currentRecord = 1;
while (userNode) {
UserData * userRecord = parseUserRecord( userNode );
if (userRecord) {
userDataList_.push_back( userRecord );
}
else {
ERROR_MSG( "LoadConfig: Corrupted user record %d.\n", currentRecord );
loadErrors ++;
}
userNode = userNode->NextSibling( "User" );
currentRecord ++;
}
if (loadErrors > 0) {
WARNING_MSG( "LoadConfig: configuration file contains %d bad user record.\n",
loadErrors );
}
}
UserData * AppConfigManager::parseUserRecord( TiXmlNode * userNode )
{
UserData * record = NULL;
TiXmlElement * nameElm = userNode->FirstChildElement( "Name" );
TiXmlElement * codeElm = userNode->FirstChildElement( "Password" );
if (!nameElm || !codeElm) {
ERROR_MSG( "LoadConfig: missing element "
"UserName or Password.\n" );
return record;
}
const char * name = nameElm->GetText();
const char * code = codeElm->GetText();
if (!name) {
ERROR_MSG( "LoadConfig: invalid user name.\n" );
return record;
}
if (code) {
size_t outLen = 0;
unsigned char * hashCode = ::decodeBase64( code, &outLen );
if (hashCode && outLen == SHA256_DIGEST_LENGTH) {
record = new UserData;
record->name = std::string( name );
memcpy( &(record->password), hashCode, SHA256_DIGEST_LENGTH );
record->clientFD = INVALID_SOCKET;
memset( &(record->clientAddr), 0, sizeof( struct sockaddr_in ) );
free( hashCode );
return record;
}
if (hashCode)
free( hashCode );
}
ERROR_MSG( "LoadConfig: invalid user password.\n" );
return record;
}
void AppConfigManager::loadDeviceInfo( TiXmlNode * deviceInfoNode )
{
if (!deviceInfoNode)
return;
TiXmlNode * rootNode = NULL;
TiXmlNode * videoNode = NULL;
rootNode = deviceInfoNode->FirstChild( "DeviceInfo" );
if (!rootNode) {
WARNING_MSG( "LoadConfig: "
"Configuration contains no device info. Use default devices.\n" );
return;
}
videoNode = rootNode->FirstChild( "Video" );
int loadErrors = 0;
int currentRecord = 1;
while (videoNode) {
DeviceInfo * deviceRecord = parseDeviceRecord( videoNode );
if (deviceRecord) {
deviceRecord->index = deviceInfoList_.size();
deviceInfoList_.push_back( deviceRecord );
}
else {
ERROR_MSG( "LoadConfig: Corrupted device record %d.", currentRecord );
loadErrors ++;
}
videoNode = videoNode->NextSibling( "Video" );
currentRecord ++;
}
if (loadErrors > 0) {
WARNING_MSG( "LoadConfig: configration file contains %d bad video device record.",
loadErrors );
}
}
DeviceInfo * AppConfigManager::parseDeviceRecord( TiXmlNode * deviceNode )
{
TiXmlElement * idElem = deviceNode->FirstChildElement( "ID" );
TiXmlElement * nameElem = deviceNode->FirstChildElement( "Name" );
TiXmlElement * labelElem = deviceNode->FirstChildElement( "Label" );
TiXmlElement * activeElem = deviceNode->FirstChildElement( "IsActive" );
if (!idElem) {
ERROR_MSG( "LoadConfig: invalid device record: missing tags." );
return NULL;
}
const char * idText = idElem->GetText();
if (!idText) {
ERROR_MSG( "LoadConfig: invalid device record: mssing ID." );
return NULL;
}
const char * nameText = nameElem->GetText();
const char * labelText = labelElem->GetText();
const char * activeText = activeElem->GetText();
DeviceInfo * newDevice = new DeviceInfo;
newDevice->deviceID = std::string( idText );
if (nameText) {
newDevice->deviceName = std::string( nameText );
}
if (labelText) {
newDevice->deviceLabel = std::string( labelText );
}
if (activeText) {
newDevice->shouldBeActive = (strcasecmp( activeText, "Yes") == 0);
}
else {
newDevice->shouldBeActive = false;
}
return newDevice;
}
bool AppConfigManager::signInUserWithPassword( const unsigned char * code, SOCKET_T fd, struct sockaddr_in & addr, std::string & username )
{
if (fd == INVALID_SOCKET)
return false;
UserData * info = NULL;
bool found = false;
// iterate through all records to find corresponding user/code
UserDataList::iterator iter;
for (iter = userDataList_.begin(); iter != userDataList_.end(); iter++) {
found = (memcmp( &((*iter)->password), code, SHA256_DIGEST_LENGTH ) == 0);
if (found)
break;
}
if (found) {
info = *iter;
if (info->clientFD != INVALID_SOCKET) {
ERROR_MSG( "Sign in user: user %s has already signed in.\n", info->name.c_str() );
return false;
}
else {
INFO_MSG( "User %s has just signed in.\n", info->name.c_str() );
username = info->name;
info->clientFD = fd;
memcpy( &(info->clientAddr), &addr, sizeof( struct sockaddr_in ) );
signedInMap_[fd] = info;
}
}
else {
ERROR_MSG( "Sign in user: unable to find user with the corresponding "
"authentication code.\n" );
}
return found;
}
bool AppConfigManager::signOutUser( SOCKET_T fd, std::string & username )
{
if (fd == INVALID_SOCKET)
return false;
SignedInMap::iterator iter = signedInMap_.find( fd );
if (iter != signedInMap_.end()) {
UserData * info = iter->second;
// remove from map
INFO_MSG( "User %s has just signed out.\n", info->name.c_str() );
username = info->name;
signedInMap_.erase( iter );
info->clientFD = INVALID_SOCKET;
memset( &(info->clientAddr), 0, sizeof( struct sockaddr_in ) );
return true;
}
return false;
}
bool AppConfigManager::findUser( SOCKET_T fd, std::string & username )
{
if (fd == INVALID_SOCKET)
return false;
SignedInMap::iterator iter = signedInMap_.find( fd );
if (iter != signedInMap_.end()) {
UserData * info = iter->second;
username = info->name;
return true;
}
return false;
}
bool AppConfigManager::addUser( const char * name, const char * password )
{
if (!name || !password || strlen( password ) < 4)
return false;
unsigned char code[SHA256_DIGEST_LENGTH];
memset( code, 0, SHA256_DIGEST_LENGTH );
secureSHA256Hash( (unsigned char*)password, strlen( password ), code );
UserDataList::iterator iter;
for (iter = userDataList_.begin(); iter != userDataList_.end(); iter++) {
if ((*iter)->name.compare( name ) == 0)
return false;
if (memcmp( code, (*iter)->password, SHA256_DIGEST_LENGTH ) == 0) // every password must be unique
return false;
}
UserData * newUser = new UserData;
newUser->name = name;
newUser->clientFD = INVALID_SOCKET;
memcpy( newUser->password, code, SHA256_DIGEST_LENGTH );
memset( &(newUser->clientAddr), 0, sizeof( struct sockaddr_in ) );
userDataList_.push_back( newUser );
return true;
}
bool AppConfigManager::delUser( const char * name )
{
if (!name)
return false;
UserDataList::iterator iter;
for (iter = userDataList_.begin(); iter != userDataList_.end(); iter++) {
if ((*iter)->name.compare( name ) == 0) {
userDataList_.erase( iter );
return true;
}
}
return false;
}
bool AppConfigManager::changeUserPassword( const char * name, const char * oldpassword, const char * newpassword )
{
if (!name || !oldpassword || !newpassword || strlen( newpassword ) < 4)
return false;
bool found = false;
UserDataList::iterator iter;
for (iter = userDataList_.begin(); iter != userDataList_.end(); iter++) {
found = ((*iter)->name.compare( name ) == 0);
if (found)
break;
}
if (!found)
return false;
unsigned char code[SHA256_DIGEST_LENGTH];
memset( code, 0, SHA256_DIGEST_LENGTH );
secureSHA256Hash( (unsigned char*)oldpassword, strlen( oldpassword ), code );
if (memcmp( code, (*iter)->password, SHA256_DIGEST_LENGTH ))
return false;
memset( code, 0, SHA256_DIGEST_LENGTH );
secureSHA256Hash( (unsigned char*)newpassword, strlen( newpassword ), code );
memcpy( (*iter)->password, code, SHA256_DIGEST_LENGTH );
return true;
}
void AppConfigManager::fini()
{
this->saveConfig();
for (UserDataList::iterator iter = userDataList_.begin();
iter != userDataList_.end(); iter++)
{
delete *iter;
}
userDataList_.clear();
signedInMap_.clear();
}
int AppConfigManager::listCurrentUsers( std::vector<std::string> & userNameList )
{
userNameList.clear();
SignedInMap::iterator iter = signedInMap_.begin();
while (iter != signedInMap_.end()) {
userNameList.push_back( iter->second->name );
iter++;
}
return userNameList.size();
}
bool AppConfigManager::getOnlineUserClientFD( const char * name, SOCKET_T & fd )
{
if (!name)
return false;
SignedInMap::iterator iter = signedInMap_.begin();
UserData * found = NULL;
while (iter != signedInMap_.end()) {
if (iter->second->name.compare( name ) == 0) {
found = iter->second;
break;
}
iter++;
}
if (found) {
fd = found->clientFD;
return true;
}
else {
return false;
}
}
int AppConfigManager::listAllUsers( std::vector<std::string> & userNameList )
{
userNameList.clear();
UserDataList::iterator iter = userDataList_.begin();
while (iter != userDataList_.end()) {
userNameList.push_back( (*iter)->name );
iter++;
}
return userNameList.size();
}
void AppConfigManager::saveConfig()
{
if (configFileName_ == "") { // save to default file
configFileName_ = DEFAULT_CONFIGURATION_FILE;
}
FILE * outFile = fopen( configFileName_.c_str(), "w" );
if (!outFile) {
ERROR_MSG( "Unable to save configuration to %s.\n", configFileName_.c_str() );
return;
}
fprintf( outFile, "<PyRIDE>\n" );
fprintf( outFile, " <TeamColour> %s </TeamColour>\n", (clientID_ & BlueTeam) ? "blue" : "red" );
fprintf( outFile, " <MemberID> %d </MemberID>\n", (clientID_ >> 4) & 0xf );
fprintf( outFile, " <DefaultPosition> %.1f %.1f </DefaultPosition>\n",
defaultPose_.x, defaultPose_.y );
fprintf( outFile, " <RemotePythonAccess> %s </RemotePythonAccess>\n", allowPythonTelnet_ ? "enable" : "disable" );
char * encodeBuf = NULL;
if (deviceInfoList_.size()) {
fprintf( outFile, " <DeviceInfo>\n" );
for (size_t i = 0; i < deviceInfoList_.size(); i++) {
fprintf( outFile, " <Video>\n" );
fprintf( outFile, " <ID> %s </ID>\n", deviceInfoList_[i]->deviceID.c_str() );
fprintf( outFile, " <Name> %s </Name>\n", deviceInfoList_[i]->deviceName.c_str() );
fprintf( outFile, " <Label> %s </Label>\n", deviceInfoList_[i]->deviceLabel.c_str() );
fprintf( outFile, " <IsActive> %s </IsActive>\n", deviceInfoList_[i]->shouldBeActive ? "Yes" : "No" );
fprintf( outFile, " </Video>\n" );
}
fprintf( outFile, " </DeviceInfo>\n" );
}
if (userDataList_.size()) {
fprintf( outFile, " <UserInfo>\n" );
for (size_t i = 0; i < userDataList_.size(); i++) {
fprintf( outFile, " <User>\n" );
fprintf( outFile, " <Name> %s </Name>\n", userDataList_[i]->name.c_str() );
encodeBuf = ::encodeBase64( userDataList_[i]->password, SHA256_DIGEST_LENGTH );
if (encodeBuf) {
fprintf( outFile, " <Password> %s </Password>\n", encodeBuf );
free( encodeBuf );
}
else {
fprintf( outFile, " <Password> </Password>\n" );
}
fprintf( outFile, " </User>\n" );
}
fprintf( outFile, " </UserInfo>\n" );
}
fprintf( outFile, "</PyRIDE>\n" );
fclose( outFile );
}
} // namespace pyride