-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cpp
More file actions
executable file
·704 lines (573 loc) · 15 KB
/
Configuration.cpp
File metadata and controls
executable file
·704 lines (573 loc) · 15 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/**
* This file is a part of IC Login (c) Daniel Bratell 2000
*
* That the source code is publicized on Internet does not mean that
* it's free to use. You are allowed to be inspired by it, but if you
* do patches of your own you must submit them to the original author.
*
* Also, you mustn't redistribute the source code. If you want to
* make other people see it, point them to
*
* http://www.lysator.liu.se/~bratell/iclogin/
*
* Original Author: Daniel Bratell <bratell@lysator.liu.se>
*/
// Configuration.cpp: implementation of the CConfiguration class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "iclogin.h"
#include "Configuration.h"
#include "ServiceMaster.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define CONFIGURATIONVERSION 1
const _TCHAR* const CONFIGURATIONVERSIONKEY = _T("ConfigurationVersion");
const _TCHAR* const LOGINHOSTKEY = _T("LoginHost");
const _TCHAR* const USERNAMEKEY = _T("Username");
const _TCHAR* const PASSWORDKEY = _T("Password");
const _TCHAR* const LOGINATINTERVALKEY = _T("LoginAtInterval");
const _TCHAR* const LOGININTERVALKEY = _T("LoginInterval");
const _TCHAR* const LOGINFAILEDINTERVALKEY = _T("LoginFailedInterval");
const _TCHAR* const UPDATETIMERSINTERVALKEY = _T("UpdateTimersInterval");
const _TCHAR* const LOGINATSTARTUPKEY = _T("LoginAtStartup");
const _TCHAR* const HIDEPASSWORDKEY = _T("HidePassword");
const _TCHAR* const STARTHIDDENKEY = _T("StartHidden");
const _TCHAR* const ISCONFIGUREDKEY = _T("IsConfigured");
const _TCHAR* const AUTOSTARTKEY = _T("AutoStart");
const _TCHAR* const MINIMIZEMEMORYUSAGEKEY = _T("MinimizeMemoryUsage");
const _TCHAR* const RESTARTDHCPWHENNEEDEDKEY = _T("RestartDHCPWhenNeeded");
const _TCHAR* const LOGOUTTIMEBEFORERESTARTKEY = _T("LogoutTimeBeforeRestartDHCP");
const _TCHAR* const LOGTOFILEKEY = _T("LogToFile");
const _TCHAR* const LOGFILEKEY = _T("LogFile");
const _TCHAR* const LOGLEVELKEY = _T("LogLevel");
const _TCHAR* const NEVERUSEPROXYKEY = _T("NeverUseProxy");
const _TCHAR* const EXTENDEDLOGGEDINKEY = _T("ExtendedLoggedInTest");
#ifdef ICLOGIN_SERVICE
const _TCHAR* const SERVICENAMEKEY = _T("ServiceName");
const _TCHAR* const SERVICEDISPLAYNAMEKEY = _T("ServiceDisplayName");
const _TCHAR* const VISIBLEASSERVICE = _T("VisibleAsService");
const _TCHAR* const RUNASSERVICE = _T("RunAsService");
const _TCHAR* const SERVICEPOPUPERROR = _T("ServicePopupError");
#endif
const _TCHAR* const PROGRAMNAMEINRUN = _T("IC Login");
const _TCHAR* const COMPANYKEY = _T("Sarasas");
const _TCHAR* const APPKEY = _T("IC Login");
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#if 0
CConfiguration::CConfiguration()
{
}
CConfiguration::~CConfiguration()
{
}
#endif
void CConfiguration::CleanRegistry()
{
SetAutoStart(false); // To remove us from the Run key.
CRegKey regkey1;
if(regkey1.Create(HKEY_LOCAL_MACHINE, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Create(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
regkey2.RecurseDeleteKey(APPKEY);
// Should check if COMPANYKEY is empty and delete it too
// in that case.
CString dummy;
bool rv = RegEnumKey(regkey2, 0, dummy.GetBuffer(MAX_PATH+1), MAX_PATH+1) ==
ERROR_NO_MORE_ITEMS;
dummy.ReleaseBuffer();
if(rv)
{
if(regkey2.Close() == ERROR_SUCCESS)
{
// Delete the COMPANYKEY too
regkey1.DeleteSubKey(COMPANYKEY);
}
}
}
}
}
const int CConfiguration::GetRegistryConfigurationVersion()
{
return GetIntData(CONFIGURATIONVERSIONKEY, 0);
}
void CConfiguration::SetRegistryConfigurationVersionToCurrent()
{
SetIntData(CONFIGURATIONVERSIONKEY, CONFIGURATIONVERSION);
}
/**
* The time in seconds between two logins
*/
const int CConfiguration::GetLoginInterval()
{
return GetIntData(LOGININTERVALKEY, 600);
}
void CConfiguration::SetLoginInterval(int interval)
{
SetIntData(LOGININTERVALKEY, interval);
}
/**
* The time in seconds between two logins if previous attempt failed
*/
const int CConfiguration::GetLoginFailedInterval()
{
return GetIntData(LOGINFAILEDINTERVALKEY, 30);
}
void CConfiguration::SetLoginFailedInterval(int interval)
{
SetIntData(LOGINFAILEDINTERVALKEY, interval);
}
/**
* The time in seconds between two updates of the display on screen
*/
const int CConfiguration::GetUpdateTimersInterval()
{
return GetIntData(UPDATETIMERSINTERVALKEY, 5);
}
void CConfiguration::SetUpdateTimersInterval(int interval)
{
SetIntData(UPDATETIMERSINTERVALKEY, interval);
}
const bool CConfiguration::GetLoginAtInterval()
{
return (GetIntData(LOGINATINTERVALKEY, 1) != 0);
}
void CConfiguration::SetLoginAtInterval(bool do_it)
{
SetIntData(LOGINATINTERVALKEY, do_it?1:0);
}
const bool CConfiguration::GetStartHidden()
{
return (GetIntData(STARTHIDDENKEY, 1) != 0);
}
void CConfiguration::SetStartHidden(bool do_it)
{
SetIntData(STARTHIDDENKEY, do_it?1:0);
}
const bool CConfiguration::GetIsConfigured()
{
return (GetIntData(ISCONFIGUREDKEY, 0) != 0);
}
void CConfiguration::SetIsConfigured(bool do_it)
{
SetIntData(ISCONFIGUREDKEY, do_it?1:0);
}
const CString CConfiguration::GetLoginHost()
{
return GetStringData(LOGINHOSTKEY, _T("login1.telia.com"));
}
void CConfiguration::SetLoginHost(const CString &host)
{
SetStringData(LOGINHOSTKEY, host);
}
const bool CConfiguration::GetLoginAtStartup()
{
return (GetIntData(LOGINATSTARTUPKEY, 1) != 0);
}
void CConfiguration::SetLoginAtStartup(const bool do_it)
{
SetIntData(LOGINATSTARTUPKEY, do_it?1:0);
}
const CString CConfiguration::GetUsername()
{
return GetStringData(USERNAMEKEY, _T(""));
}
void CConfiguration::SetUsername(const CString &name)
{
SetStringData(USERNAMEKEY, name);
}
/**
* A little obscured. Just to make it unreadable by the casual eye.
* This is by no means secure. I just liked playing with numbers
* which is the reason it looks so complicated. (A XOR or
* Base64-encode would have done)
*/
const CString CConfiguration::GetPassword()
{
CString password = GetStringData(PASSWORDKEY, _T(""));
if(GetRegistryConfigurationVersion()<1)
{
return password;
}
// Must be a multiple of three
if(password.GetLength() % 3)
{
return "";
}
if(password.GetLength() > 90)
{
// Could cause trouble
return "";
}
CString decoded_password;
for(int i = 0; i < password.GetLength()/3; i++)
{
// First three bits
unsigned char first = (unsigned char)(password[3*i]-i);
if(first<'a' || first>('a'+7))
{
return "";
}
// Second two bits
unsigned char second = (unsigned char)(password[3*i+1]-i);
if(second<'g' || second > ('g'+3))
{
return "";
}
// The last three bits
unsigned char third = (unsigned char)(password[3*i+2]-i);
if(third<'b' || third > ('b'+7))
{
return "";
}
unsigned char decoded_char =
(unsigned char)((first-'a')<<5 |
((second-'g')<<3) |
(third-'b'));
decoded_password += decoded_char;
}
return decoded_password;
}
/**
* A little obscured. Just to make it unreadable by the casual eye.
* This is by no means secure.
*/
void CConfiguration::SetPassword(const CString &password)
{
// Easy way to "see" password with this test. If the password
// was encrypted this would be a security hole.
if(GetRegistryConfigurationVersion()<1)
{
SetStringData(PASSWORDKEY, password);
}
CString obscured_password;
for(int i = 0; i < password.GetLength(); i++)
{
char letter = password[i];
obscured_password += (unsigned char)(((letter & 0xe0) >> 5) + 'a'+i);
obscured_password += (unsigned char)(((letter & 0x18) >> 3) + 'g'+i);
obscured_password += (unsigned char)((letter & 0x07) + 'b'+i);
}
SetStringData(PASSWORDKEY, obscured_password);
}
const bool CConfiguration::GetHidePassword()
{
return (GetIntData(HIDEPASSWORDKEY, 0) != 0);
}
void CConfiguration::SetNeverUseProxy(const bool do_it)
{
SetIntData(NEVERUSEPROXYKEY, do_it?1:0);
}
const bool CConfiguration::GetNeverUseProxy()
{
return (GetIntData(NEVERUSEPROXYKEY, 0) != 0);
}
void CConfiguration::SetHidePassword(const bool do_it)
{
SetIntData(HIDEPASSWORDKEY, do_it?1:0);
}
const bool CConfiguration::GetLogToFile()
{
return (GetIntData(LOGTOFILEKEY, 0) != 0);
}
void CConfiguration::SetLogToFile(const bool do_it)
{
SetIntData(LOGTOFILEKEY, do_it?1:0);
}
const bool CConfiguration::GetMinimizeMemoryUsage()
{
return (GetIntData(MINIMIZEMEMORYUSAGEKEY, 1) != 0);
}
void CConfiguration::SetMinimizeMemoryUsage(const bool do_it)
{
SetIntData(MINIMIZEMEMORYUSAGEKEY, do_it?1:0);
}
const bool CConfiguration::GetRestartDHCPWhenNeeded()
{
return (GetIntData(RESTARTDHCPWHENNEEDEDKEY, 0) != 0);
}
void CConfiguration::SetRestartDHCPWhenNeeded(const bool do_it)
{
SetIntData(RESTARTDHCPWHENNEEDEDKEY, do_it?1:0);
}
/**
* Time in seconds
*/
const int CConfiguration::GetLogoutTimeBeforeRestartDHCP()
{
return GetIntData(LOGOUTTIMEBEFORERESTARTKEY, 1800);
}
/**
* Time in seconds
*/
void CConfiguration::SetLogoutTimeBeforeRestartDHCP(const int time)
{
SetIntData(LOGOUTTIMEBEFORERESTARTKEY, time);
}
const CString CConfiguration::GetLogFile()
{
return GetStringData(LOGFILEKEY, _T("iclogin.log"));
}
void CConfiguration::SetLogFile(const CString &filename)
{
SetStringData(LOGFILEKEY, filename);
}
const int CConfiguration::GetLogLevel()
{
return GetIntData(LOGLEVELKEY, CLog::LOG_INFO);
}
void CConfiguration::SetLogLevel(int loglevel)
{
SetIntData(LOGLEVELKEY, loglevel);
}
#ifdef ICLOGIN_SERVICE
const CString CConfiguration::GetServiceName()
{
return GetStringData(SERVICENAMEKEY, _T("ICLogin"));
}
void CConfiguration::SetServiceName(const CString &name)
{
SetStringData(SERVICENAMEKEY, name);
}
const CString CConfiguration::GetServiceLongName()
{
return GetStringData(SERVICEDISPLAYNAMEKEY, _T("IC Login Service"));
}
void CConfiguration::SetServiceLongName(const CString &name)
{
SetStringData(SERVICEDISPLAYNAMEKEY, name);
}
#if 0
bool CConfiguration::GetExtendedLoggedInTest()
{
return (GetIntData(EXTENDEDLOGGEDINKEY, 0) != 0);
}
void CConfiguration::SetExtendedLoggedInTest(bool do_it)
{
SetIntData(EXTENDEDLOGGEDINKEY, do_it?1:0);
}
#endif
const bool CConfiguration::GetVisibleAsService()
{
return (GetIntData(VISIBLEASSERVICE, 0) != 0);
}
void CConfiguration::SetVisibleAsService(const bool be_it)
{
SetIntData(VISIBLEASSERVICE, be_it?1:0);
}
const bool CConfiguration::GetServicePopupError()
{
return (GetIntData(SERVICEPOPUPERROR, 0) != 0);
}
void CConfiguration::SetServicePopupError(const bool do_it)
{
SetIntData(SERVICEPOPUPERROR, do_it?1:0);
}
const bool CConfiguration::GetRunAsService()
{
return (GetIntData(RUNASSERVICE, 0) != 0);
}
void CConfiguration::SetRunAsService(const bool do_it)
{
CServiceMaster::RemoveService();
if(do_it)
{
if(CServiceMaster::InstallService())
{
SetIntData(RUNASSERVICE, 1);
}
}
else
{
SetIntData(RUNASSERVICE, 0);
}
}
#endif // ICLOGIN_SERVICE
const bool CConfiguration::GetAutoStart()
{
// Could also check the run key.
return (GetIntData(AUTOSTARTKEY, 0) != 0);
}
void CConfiguration::SetAutoStart(const bool do_it)
{
HKEY start_key = GetRegKey(LocalMachine);
CRegKey regkey1;
if(regkey1.Create(start_key, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Create(regkey1, _T("Microsoft")) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Create(regkey2, _T("Windows")) == ERROR_SUCCESS)
{
CRegKey regkey4;
if(regkey4.Create(regkey3, _T("CurrentVersion")) == ERROR_SUCCESS)
{
CRegKey regkey5;
if(regkey5.Create(regkey4, _T("Run")) == ERROR_SUCCESS)
{
if(do_it)
{
CString ourpath;
BOOL rv = GetModuleFileName(NULL, ourpath.GetBuffer(2000), 2000);
ourpath.ReleaseBuffer();
if(rv)
{
ourpath = "\""+ourpath+"\"";
regkey5.SetValue(ourpath, PROGRAMNAMEINRUN);
}
SetIntData(AUTOSTARTKEY, 1);
}
else
{
SetIntData(AUTOSTARTKEY, 0);
regkey5.DeleteValue(PROGRAMNAMEINRUN);
}
}
}
}
}
}
}
/**
* Try to write, and if successable, retur true.
*/
bool CConfiguration::IsWritable()
{
CRegKey regkey1;
if(regkey1.Create(HKEY_LOCAL_MACHINE, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Create(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Create(regkey2, APPKEY) == ERROR_SUCCESS)
{
static const _TCHAR* key = _T("DummyToSeeIfWritable");
if(regkey3.SetValue(4711, key) == ERROR_SUCCESS)
{
regkey3.DeleteValue(key);
return true;
}
}
}
}
return false;
}
int CConfiguration::GetIntData(const CString& key,
int default_data,
RegistryPart registry_part /* =LocalMachine */)
{
int rv = default_data; // Default;
HKEY start_key = GetRegKey(registry_part);
CRegKey regkey1;
if(regkey1.Open(start_key, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Open(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Open(regkey2, APPKEY) == ERROR_SUCCESS)
{
DWORD data_in_reg;
if(regkey3.QueryValue(data_in_reg, key) == ERROR_SUCCESS)
{
return data_in_reg;
}
}
}
}
return rv;
}
void CConfiguration::SetIntData(const CString& key,
int value,
RegistryPart registry_part /* =LocalMachine */)
{
HKEY start_key = GetRegKey(registry_part);
CRegKey regkey1;
if(regkey1.Create(start_key, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Create(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Create(regkey2, APPKEY) == ERROR_SUCCESS)
{
if(regkey3.SetValue(value, key) != ERROR_SUCCESS)
{
ASSERT(false);
}
}
}
}
}
CString CConfiguration::GetStringData(const CString& key,
const CString& default_data,
RegistryPart registry_part /*=LocalMachine */)
{
CString rv = default_data; // Default;
HKEY start_key = GetRegKey(registry_part);
CRegKey regkey1;
if(regkey1.Open(start_key, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Open(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Open(regkey2, APPKEY) == ERROR_SUCCESS)
{
DWORD data_size;
if(regkey3.QueryValue(NULL, key, &data_size) == ERROR_SUCCESS)
{
if(data_size > 0)
{
regkey3.QueryValue(rv.GetBuffer(data_size), key, &data_size);
rv.ReleaseBuffer();
}
}
}
}
}
return rv;
}
void CConfiguration::SetStringData(const CString& key,
const CString& value,
RegistryPart registry_part /*=LocalMachine*/)
{
HKEY start_key = GetRegKey(registry_part);
CRegKey regkey1;
if(regkey1.Create(start_key, _T("Software")) == ERROR_SUCCESS)
{
CRegKey regkey2;
if(regkey2.Create(regkey1, COMPANYKEY) == ERROR_SUCCESS)
{
CRegKey regkey3;
if(regkey3.Create(regkey2, APPKEY) == ERROR_SUCCESS)
{
if(regkey3.SetValue(value, key) != ERROR_SUCCESS)
{
ASSERT(false);
}
}
}
}
}
inline HKEY CConfiguration::GetRegKey(RegistryPart registry_part)
{
switch(registry_part)
{
case LocalMachine:
return HKEY_LOCAL_MACHINE;
break;
default:
ASSERT(registry_part == CurrentUser);
return HKEY_CURRENT_USER;
}
}