/
Main.cpp
1280 lines (1136 loc) · 44.1 KB
/
Main.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2005-2018, BearWare.dk
*
* Contact Information:
*
* Bjoern D. Rasmussen
* Kirketoften 5
* DK-8260 Viby J
* Denmark
* Email: contact@bearware.dk
* Phone: +45 20 20 54 59
* Web: http://www.bearware.dk
*
* This source code is part of the TeamTalk SDK owned by
* BearWare.dk. Use of this file, or its compiled unit, requires a
* TeamTalk SDK License Key issued by BearWare.dk.
*
* The TeamTalk SDK License Agreement along with its Terms and
* Conditions are outlined in the file License.txt included with the
* TeamTalk SDK distribution.
*
*/
#include <ace/Reactor.h>
#include <ace/TP_Reactor.h>
#include <ace/streams.h>
#include <ace/NT_Service.h>
#include <ace/Init_ACE.h>
#include <teamtalk/ttassert.h>
#include <teamtalk/Log.h>
#include <TeamTalkDefs.h>
#include "ServerGuard.h"
#include <mystd/MyStd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <iostream>
#if !defined(WIN32)
#include <unistd.h>
#endif
#include <teamtalk/server/ServerNode.h>
#include <iostream>
#include <map>
#include <sstream>
#include "AppInfo.h"
using namespace std;
using namespace teamtalk;
#if defined(UNICODE)
typedef wostringstream tostringstream;
#else
typedef ostringstream tostringstream;
#endif
class Service;
int RunServer(
#if defined(BUILD_NT_SERVICE)
Service* service
#endif
);
void PrintCommandArgs();
int ParseArguments(int argc, ACE_TCHAR* argv[]
#if defined(BUILD_NT_SERVICE)
, Service* service
#endif
);
#if defined(BUILD_NT_SERVICE)
BOOL WINAPI ControlHandler(DWORD dwControlType)
{
switch(dwControlType)
{
case CTRL_LOGOFF_EVENT :
return TRUE;
}
return FALSE;
}
#endif
void RunWizard(ServerXML& xmlSettings);
class StopReactorEventHandler : public ACE_Event_Handler
{
public:
StopReactorEventHandler(ACE_Reactor* r)
: ACE_Event_Handler(r)
{
}
int handle_signal(int signum, siginfo_t*,ucontext_t*)
{
reactor()->end_reactor_event_loop();
return 0;
}
};
#if defined(BUILD_NT_SERVICE)
ACE_TString GetWinError(DWORD err)
{
ACE_TString result;
LPTSTR s = NULL;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
0,
(LPTSTR)&s,
0,
NULL);
if(s)
{
result = s;
::LocalFree(s);
}
return result;
}
class Service : public ACE_NT_Service
{
public:
void handle_control (DWORD control_code)
{
if (control_code == SERVICE_CONTROL_SHUTDOWN
|| control_code == SERVICE_CONTROL_STOP)
{
report_status (SERVICE_STOP_PENDING);
reactor()->end_reactor_event_loop();
//reactor ()->notify (this,
// ACE_Event_Handler::EXCEPT_MASK);
}
else
inherited::handle_control (control_code);
}
int svc (void)
{
// As an NT service, we come in here in a different thread than the
// one which created the reactor. So in order to do anything, we
// need to own the reactor. If we are not a service, report_status
// will return -1.
if(RunServer(this) < 0)
return -1;
return 0;
}
//hack
void report_status_foo(DWORD d1, DWORD d2 = 0)
{
this->report_status(d1,d2);
}
private:
typedef ACE_NT_Service inherited;
};
ACE_NT_SERVICE_DEFINE (TeamTalk,
Service,
ACE_TEXT(TEAMTALK_DESCRIPTION));
#endif
ACE_TString bindip, settingsfile, logfile = ACE_TEXT(TEAMTALK_LOGFILE), pidfile;
int tcpport = 0;
int udpport = 0;
bool verbose = false;
bool daemon_pid = false;
//setting files
ServerXML xmlSettings(TEAMTALK_XML_ROOTNAME);
bool bDaemon = false;
bool bNonDaemon = false;
int rxloss = 0, txloss = 0;
int ACE_TMAIN(int argc, ACE_TCHAR* argv[])
{
ACE::init();
int exitcode = EXIT_FAILURE;
#if defined(BUILD_NT_SERVICE)
Service service;
service.reactor(ACE_Reactor::instance());
service.name(ACE_TEXT(TEAMTALK_NAME), ACE_TEXT(TEAMTALK_NAME));
int parse_result = ParseArguments(argc, argv, &service);
if(parse_result > 0)
{
ACE_NT_SERVICE_RUN (TeamTalk,
&service,
rett);
exitcode = rett<0?EXIT_FAILURE:EXIT_SUCCESS;
}
#else
if(argc <= 1)
{
PrintCommandArgs();
exitcode = EXIT_SUCCESS;
}
else
{
int parse_result = ParseArguments(argc, argv);
if(parse_result>0)
{
exitcode = RunServer()<0?EXIT_FAILURE:EXIT_SUCCESS;
}
else
{
exitcode = EXIT_FAILURE;
}
}
#endif
ACE::fini();
return exitcode;
}
void RunEventLoop(ACE_Reactor* tcpReactor, ACE_Reactor* udpReactor,
const ACE_TString& workdir, std::ofstream& logstream,
ACE_INT64 log_maxsize)
{
int ret = ACE_Thread_Manager::instance ()->spawn(event_loop, udpReactor);
if(ret < 0)
TT_LOG(ACE_TEXT("Failed to spawn UDP reactor."));
SyncReactor(*udpReactor);
int log_check = 0;
ACE_Time_Value tm(10,0);
while(tcpReactor->handle_events(tm) >= 0) {
if(++log_check % 10 == 0 && log_maxsize>0 &&
logstream.tellp() >= log_maxsize)
RotateLogfile(workdir, logfile.c_str(), logstream);
tm.set(10, 0);
}
udpReactor->end_reactor_event_loop();
}
int RunServer(
#if defined(BUILD_NT_SERVICE)
Service* service
#endif
)
{
//avoid SIGPIPE
ACE_Sig_Action no_sigpipe ((ACE_SignalHandler) SIG_IGN);
ACE_Sig_Action original_action;
no_sigpipe.register_action (SIGPIPE, &original_action);
int ret = ACE::set_handle_limit(-1);//client handler (must be BIG)
ACE_Select_Reactor selectReactor;
ACE_Reactor tcpReactor(&selectReactor);
ACE_Reactor::instance(&tcpReactor);
ACE_Reactor::instance()->owner (ACE_OS::thr_self ());
ACE_Reactor udpReactor;
#if defined(BUILD_NT_SERVICE)
service->reactor(ACE_Reactor::instance());
#endif
StopReactorEventHandler signalHandler(ACE_Reactor::instance());
ACE_Reactor::instance()->register_handler(SIGTERM, &signalHandler);
ACE_Reactor::instance()->register_handler(SIGINT, &signalHandler);
#if defined(WIN32)
ACE_Reactor::instance()->register_handler(SIGBREAK, &signalHandler);
#endif
//init logger
#if defined(BUILD_NT_SERVICE)
ACE_LOG_MSG->open(ACE_TEXT(TEAMTALK_NAME), ACE_Log_Msg::SYSLOG | ACE_Log_Msg::OSTREAM);
#else
ACE_LOG_MSG->open(ACE_TEXT(TEAMTALK_NAME));
#endif
ACE_TCHAR workdir[512] = {};
ACE_OS::getcwd(workdir, 512);
//log file
std::ofstream logstream;
ACE_INT64 log_maxsize = xmlSettings.GetServerLogMaxSize();
//enable logging
if(log_maxsize != 0)
{
logstream.open(logfile.c_str(), ios::app);
ACE_OSTREAM_TYPE * output = &logstream;
ACE_LOG_MSG->msg_ostream(output, 0);
ACE_LOG_MSG->set_flags(ACE_Log_Msg::OSTREAM);
u_long flag = LM_ERROR | LM_INFO | LM_DEBUG;
ACE_LOG_MSG->priority_mask(flag, ACE_Log_Msg::PROCESS);
}
//create listener
ServerGuard srvguard(xmlSettings);
ServerNode servernode(ACE_TEXT( TEAMTALK_VERSION ), &tcpReactor, &tcpReactor, &udpReactor, &srvguard);
ServerSettings prop = servernode.GetServerProperties();
statchannels_t channels;
if(!ReadServerProperties(xmlSettings, prop, channels))
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024,
ACE_TEXT("Failed to read server properties from settings file %s."), settingsfile.c_str());
TT_SYSLOG(error_msg);
return -1;
}
prop.rxloss = rxloss;
prop.txloss = txloss;
//check for override options
if(tcpport > 0)
{
ACE_INET_Addr addr((u_short)tcpport, prop.tcpaddrs.size() ? prop.tcpaddrs[0].get_host_addr() : NULL);
prop.tcpaddrs.clear();
prop.tcpaddrs.push_back(addr);
}
if(udpport > 0)
{
ACE_INET_Addr addr((u_short)udpport, prop.udpaddrs.size() ? prop.udpaddrs[0].get_host_addr(): NULL);
prop.udpaddrs.clear();
prop.udpaddrs.push_back(addr);
}
if(bindip.length())
{
ACE_INET_Addr tcpaddr(prop.tcpaddrs.size() ? prop.tcpaddrs[0].get_port_number() : tcpport, bindip.c_str());
prop.tcpaddrs.clear();
prop.tcpaddrs.push_back(tcpaddr);
ACE_INET_Addr udpaddr(prop.udpaddrs.size() ? prop.udpaddrs[0].get_port_number() : udpport, bindip.c_str());
prop.udpaddrs.clear();
prop.udpaddrs.push_back(udpaddr);
}
if(!ConfigureServer(servernode, prop, channels))
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024,
ACE_TEXT("Failed to configure the server from settings file %s."), settingsfile.c_str());
TT_SYSLOG(error_msg);
return -1;
}
channels.clear();
bool encrypted = false;
#if defined(ENABLE_TEAMTALKPRO)
encrypted = xmlSettings.GetCertificateFile().size() && xmlSettings.GetPrivateKeyFile().size();
#endif
#if defined(ENABLE_TEAMTALKPRO)
if(!servernode.StartServer(encrypted, SERVER_WELCOME))
#else
if(!servernode.StartServer(false, SERVER_WELCOME))
#endif
{
ACE_TCHAR error_msg[1024];
if(bindip.length() == 0)
ACE_OS::snprintf(error_msg, 1024,
ACE_TEXT("Unable to launch server using TCP port %d UDP port %d.\n")
ACE_TEXT("Make sure the ports are not currently in use."),
(int)prop.tcpaddrs[0].get_port_number(), (int)prop.udpaddrs[0].get_port_number());
else
ACE_OS::snprintf(error_msg, 1024,
ACE_TEXT("Unable to launch server using IP-address %s TCP port %d UDP port %d.\n")
ACE_TEXT("Make sure the ports are not currently in use."),
prop.tcpaddrs[0].get_host_addr(), (int)prop.tcpaddrs[0].get_port_number(),
(int)prop.udpaddrs[0].get_port_number());
TT_SYSLOG(error_msg);
return -1;
}
else
{
TT_LOG(ACE_TEXT("Started ") ACE_TEXT( TEAMTALK_NAME ) ACE_TEXT(" v.") ACE_TEXT( TEAMTALK_VERSION ) ACE_TEXT("."));
if(!verbose)
ACE_LOG_MSG->clr_flags(ACE_Log_Msg::STDERR);
//don't write server events to syslog
ACE_LOG_MSG->clr_flags(ACE_Log_Msg::SYSLOG);
}
#if defined(BUILD_NT_SERVICE)
SetConsoleCtrlHandler(ControlHandler, TRUE);
service->report_status_foo(SERVICE_RUNNING);
RunEventLoop(ACE_Reactor::instance(), &udpReactor, workdir, logstream, log_maxsize);
#else
if(bDaemon)
{
#if defined(WIN32)
cout << "Windows does not support daemon mode. Use -nd parameter instead." << endl;
return -1;
#else
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
TT_SYSLOG(ACE_TEXT("Failed to start ") ACE_TEXT( TEAMTALK_NAME ));
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
{
if(daemon_pid)
cout << TEAMTALK_NAME << " daemon got PID " << pid << endl;
if(pidfile.length())
{
std::ofstream fb(pidfile.c_str(), ios_base::out | ios_base::trunc);
if(fb.fail())
{
cerr << "Failed to open: " << pidfile << endl;
}
fb << pid;
fb.close();
}
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
TT_SYSLOG("No SID assigned to child process.");
/* Log any failure */
exit(EXIT_FAILURE);
}
RunEventLoop(ACE_Reactor::instance(), &udpReactor, workdir, logstream, log_maxsize);
#endif /* WIN32 */
}
else if(bNonDaemon)
{
//TCP commands thread
RunEventLoop(ACE_Reactor::instance(), &udpReactor, workdir, logstream, log_maxsize);
}
#endif /* BUILD_NT_SERVICE */
ACE_LOG_MSG->set_flags(ACE_Log_Msg::STDERR);
servernode.StopServer();
TT_LOG(ACE_TEXT("Stopped ") ACE_TEXT(TEAMTALK_NAME) ACE_TEXT("."));
ACE_Thread_Manager::instance ()->wait ();
udpReactor.close();
tcpReactor.close();
ACE::fini();
return 0;
}
int ParseArguments(int argc, ACE_TCHAR* argv[]
#if defined(BUILD_NT_SERVICE)
, Service* service
#endif
)
{
std::map<ACE_TString,ACE_TString> args;
for(int i=0;i<argc;i++)
{
ACE_TString str(argv[i]);
pair<ACE_TString,ACE_TString> newPair;
newPair.first = str;
if ((str == ACE_TEXT("-wd") ||
str == ACE_TEXT("-tcpport") ||
str == ACE_TEXT("-udpport") ||
str == ACE_TEXT("-ip") ||
str == ACE_TEXT("-c") ||
str == ACE_TEXT("-l") ||
str == ACE_TEXT("-pid-file") ||
str == ACE_TEXT("-rxloss") ||
str == ACE_TEXT("-txloss")))
{
if(i+1 >= argc)
{
cerr << "Missing option for parameter " << str << endl;
return 0;
}
newPair.second = argv[i+1];
i++;
}
else
{
newPair.second = ACE_TEXT("");
}
args.insert(newPair);
}
std::map<ACE_TString,ACE_TString>::iterator ite;
if(args.find(ACE_TEXT("--version")) != args.end())
{
cout << TEAMTALK_NAME << " version " << TEAMTALK_VERSION_FRIENDLY << endl;
return 0;
}
if(args.find(ACE_TEXT("--help")) != args.end() ||
args.find(ACE_TEXT("-help")) != args.end() ||
args.find(ACE_TEXT("/help")) != args.end() ||
args.find(ACE_TEXT("-h")) != args.end() ||
args.find(ACE_TEXT("/h")) != args.end() ||
args.find(ACE_TEXT("-?")) != args.end() ||
args.find(ACE_TEXT("/?")) != args.end()
)
{
PrintCommandArgs();
return 0;
}
if(args.find(ACE_TEXT("-d")) != args.end())
{
bDaemon = true;
}
if(args.find(ACE_TEXT("-nd")) != args.end())
{
bNonDaemon = true;
}
if(args.find(ACE_TEXT("-daemon-pid")) != args.end())
{
daemon_pid = true;
}
if((ite = args.find(ACE_TEXT("-pid-file"))) != args.end())
{
pidfile = (*ite).second.c_str();
}
if( (ite = args.find(ACE_TEXT("-tcpport"))) != args.end())
{
tcpport = string2i((*ite).second.c_str());
}
if( (ite = args.find(ACE_TEXT("-udpport"))) != args.end())
{
udpport = string2i((*ite).second.c_str());
}
if( (ite = args.find(ACE_TEXT("-ip"))) != args.end())
{
bindip = (*ite).second;
}
if( (ite = args.find(ACE_TEXT("-verbose"))) != args.end())
{
verbose = true;
}
if( (ite = args.find(ACE_TEXT("-rxloss"))) != args.end())
{
rxloss = ACE_OS::atoi((*ite).second.c_str());
}
if( (ite = args.find(ACE_TEXT("-txloss"))) != args.end())
{
txloss = ACE_OS::atoi((*ite).second.c_str());
}
if( (ite = args.find(ACE_TEXT("-wd"))) != args.end())
{
ACE_TString workdir = (*ite).second;
int ret = ACE_OS::chdir(workdir.c_str());
if(ret != 0)
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("%s failed to change to directory \"%s\"."),
ACE_TEXT(TEAMTALK_NAME), workdir.c_str());
TT_SYSLOG(error_msg);
return -1;
}
}
if( (ite = args.find(ACE_TEXT("-c"))) != args.end())
{
settingsfile = ite->second;
}
if( (ite = args.find(ACE_TEXT("-l"))) != args.end())
{
logfile = ite->second;
}
#if defined(BUILD_NT_SERVICE)
else
{
//load config file from same directory as
//executable file when running as NT service.
ACE_TCHAR bufPath[MAX_PATH] = {};
if(GetModuleFileName(NULL, bufPath, MAX_PATH)>0)
{
TCHAR* pChr = ACE_OS::strrchr(bufPath, '\\');
TTASSERT(pChr);
if(pChr)
{
pChr++;
*pChr = 0;
ACE_OS::chdir(bufPath);
}
settingsfile = bufPath;
settingsfile += ACE_TEXT( TEAMTALK_SETTINGSFILE );
}
}
//pick out options related to NT services
if( (ite = args.find(ACE_TEXT("-i"))) != args.end())
{
if(service->insert(SERVICE_AUTO_START)<0)
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("Failed to install ") ACE_TEXT(TEAMTALK_NAME) ACE_TEXT("\nError: %s"),
GetWinError(ACE_OS::last_error()).c_str());
TT_SYSLOG(error_msg);
}
else
{
TT_LOG(ACE_TEXT(TEAMTALK_NAME) ACE_TEXT(" installed successfully."));
}
return 0;
}
if( (ite = args.find(ACE_TEXT("-u"))) != args.end())
{
if(service->remove()<0)
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("Failed to remove ") ACE_TEXT(TEAMTALK_NAME) ACE_TEXT("\nError: %s"),
GetWinError(ACE_OS::last_error()).c_str());
TT_SYSLOG(error_msg);
}
else
{
TT_LOG(ACE_TEXT(TEAMTALK_NAME) ACE_TEXT(" uninstalled successfully."));
}
return 0;
}
if( (ite = args.find(ACE_TEXT("-s"))) != args.end())
{
if(service->start_svc()<0)
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("Failed to start ") ACE_TEXT(TEAMTALK_NAME) ACE_TEXT("\nError: %s"),
GetWinError(ACE_OS::last_error()).c_str());
TT_SYSLOG(error_msg);
}
else
{
TT_LOG(ACE_TEXT(TEAMTALK_NAME) ACE_TEXT(" started successfully."));
}
return 0;
}
if( (ite = args.find(ACE_TEXT("-e"))) != args.end())
{
if(service->stop_svc()<0)
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("Failed to stop ") ACE_TEXT(TEAMTALK_NAME) ACE_TEXT("\nError: %s"),
GetWinError(ACE_OS::last_error()).c_str());
TT_SYSLOG(error_msg);
}
else
{
TT_LOG(ACE_TEXT(TEAMTALK_NAME) ACE_TEXT(" stopped successfully."));
}
return 0;
}
#endif
if(!LoadConfig(xmlSettings, settingsfile))
{
ACE_TCHAR error_msg[1024];
ACE_OS::snprintf(error_msg, 1024, ACE_TEXT("Failed to load settings file %s."), settingsfile.c_str());
TT_SYSLOG(error_msg);
return -1;
}
if( (ite = args.find(ACE_TEXT("-wizard"))) != args.end())
{
RunWizard(xmlSettings);
}
#if !defined(BUILD_NT_SERVICE)
if(!bNonDaemon && !bDaemon)
{
TT_LOG(ACE_TEXT("Missing either -d or -nd parameter in order to start."));
return 0;
}
#endif
return 1; //GO
}
void PrintCommandArgs()
{
cout << endl;
cout << TEAMTALK_NAME << " version " << TEAMTALK_VERSION_FRIENDLY << endl;
cout << "Compiled on " __DATE__ " " __TIME__ "." << endl;
cout << endl;
cout << "Copyright (c) 2002-2018, BearWare.dk" << endl;
cout << endl;
cout << "Usage: " << TEAMTALK_EXE << " [OPTIONS]" << endl << endl;
#if defined(BUILD_NT_SERVICE)
cout << "Windows NT service install options:" << endl << endl;
cout << " -i Install " << TEAMTALK_NAME << " as NT service." << endl;
cout << " -u Uninstall " << TEAMTALK_NAME << " from NT services." << endl;
cout << " -s Start NT service (must already be installed)." << endl;
cout << " -e Stop NT service (must already be running)." << endl;
cout << endl;
cout << "Windows NT service startup arguments:" << endl << endl;
#else
cout << "Valid options:" << endl;
#endif
#if !defined(BUILD_NT_SERVICE)
#if !defined(WIN32)
cout << " -d Start " << TEAMTALK_NAME << " as daemon." << endl;
cout << " -daemon-pid Print PID of daemon started with -d option." << endl;
cout << " -pid-file [FILE] Write PID of daemon started with -d option to file." << endl;
#endif
cout << " -nd Start " << TEAMTALK_NAME << " as non-daemon." << endl;
#endif
cout << " -wizard Run the setup-wizard to configure the server." << endl;
cout << " -c [FILE] Instead of loading " << TEAMTALK_SETTINGSFILE << " from current directory" << endl;
cout << " use this specified file." << endl;
cout << " -l [FILE] If logging is enabled save to the specified filename instead" << endl;
cout << " of writing to " << TEAMTALK_LOGFILE << " in current directory." << endl;
cout << " -wd [DIR] Set working directory (where " << TEAMTALK_SETTINGSFILE << endl;
cout << " is located and where the log file " << TEAMTALK_LOGFILE << endl;
cout << " will be stored)." << endl;
cout << " Current directory is the default location of " << endl;
cout << " " << TEAMTALK_SETTINGSFILE << " and log file." << endl;
cout << " -tcpport [PORT] Override the <tcpport> setting in " << TEAMTALK_SETTINGSFILE << "." << endl;
cout << " -udpport [PORT] Override the <udpport> setting in " << TEAMTALK_SETTINGSFILE << "." << endl;
cout << " -ip [IPADDR] Override <bind-ip> setting in " << TEAMTALK_SETTINGSFILE << "." << endl;
cout << " -verbose Output log information to console." << endl;
cout << " --version Displays version info." << endl;
cout << " --help Displays this message." << endl;
#if !defined(BUILD_NT_SERVICE)
cout << endl;
cout << "Examples: " << TEAMTALK_EXE << " -nd" << endl;
cout << " " << TEAMTALK_EXE << " -wd /home/bill/srv1 -ip 192.168.0.2 -nd" << endl;
cout << " " << TEAMTALK_EXE << " -c /home/bill/srv1/" << TEAMTALK_SETTINGSFILE << " -l " << TEAMTALK_LOGFILE << " -nd" << endl;
#endif
cout << endl;
cout << "Kind regards go to the people behind the ACE Framework, Speex, OPUS" << endl;
cout << "and WebM projects!" << endl;
cout << endl;
cout << "Report bugs to contact@bearware.dk" << endl;
cout << endl;
}
bool inputTrue(const std::string& input)
{
std::string tmp = str2lower(input);
return tmp == "y" || tmp == "yes";
}
std::string printGetString(const std::string& input)
{
cout << "(\"" << input << "\") ";
std::string tmp;
std::getline(cin, tmp);
return tmp.empty()? input : tmp.c_str();
}
bool printGetBool(bool value)
{
cout << "[Y/N] ";
std::string input = printGetString(value?"Y":"N");
return inputTrue(input);
}
int printGetInt(int value)
{
ostringstream os;
os << value;
std::string sval = printGetString(os.str().c_str());
istringstream is(sval.c_str());
is >> value;
return value;
}
_INT64 printGetInt64(_INT64 value)
{
ostringstream os;
os << value;
std::string sval = printGetString(os.str().c_str());
istringstream is(sval.c_str());
is >> value;
return value;
}
void RunWizard(ServerXML& xmlSettings)
{
cout << TEAMTALK_NAME << " " << TEAMTALK_VERSION_FRIENDLY << " configurator" << endl;
cout << endl;
cout << "Do you want to configure your " TEAMTALK_NAME "? ";
if(!printGetBool(true))
return;
cout << endl;
cout << "Configuring file: " << xmlSettings.GetFileName() << endl;
ACE_TString servername, motd, filesroot;
std::vector<std::string> bindips;
ACE_TString certfile, keyfile;
int maxusers, max_logins_per_ip = 0;
bool autosave = true;
_INT64 diskquota = 0, maxdiskusage = 0, log_maxsize = 0;
int tcpport = DEFAULT_TCPPORT, udpport = DEFAULT_UDPPORT, max_login_attempts = 0, logindelay = 0;
servername = Utf8ToUnicode(xmlSettings.GetServerName().c_str());
motd = Utf8ToUnicode(xmlSettings.GetMessageOfTheDay().c_str());
filesroot = Utf8ToUnicode(xmlSettings.GetFilesRoot().c_str());
bindips = xmlSettings.GetBindIPs();;
tcpport = xmlSettings.GetHostTcpPort()==UNDEFINED?DEFAULT_TCPPORT:xmlSettings.GetHostTcpPort();
udpport = xmlSettings.GetHostUdpPort()==UNDEFINED?DEFAULT_UDPPORT:xmlSettings.GetHostUdpPort();
maxusers = xmlSettings.GetMaxUsers() == UNDEFINED?MAX_USERS:xmlSettings.GetMaxUsers();
log_maxsize = xmlSettings.GetServerLogMaxSize();
autosave = xmlSettings.GetAutoSave();
diskquota = xmlSettings.GetDefaultDiskQuota();
maxdiskusage = xmlSettings.GetMaxDiskUsage();
max_login_attempts = xmlSettings.GetMaxLoginAttempts();
max_logins_per_ip = xmlSettings.GetMaxLoginsPerIP();
logindelay = xmlSettings.GetLoginDelay();
#if defined(ENABLE_TEAMTALKPRO)
certfile = Utf8ToUnicode(xmlSettings.GetCertificateFile().c_str());
keyfile = Utf8ToUnicode(xmlSettings.GetPrivateKeyFile().c_str());
#endif
cout << endl;
cout << "Ready to configure " << TEAMTALK_NAME << " settings." << endl;
#if !defined(WIN32)
ACE_TCHAR *s;
int utf8_mode = 0;
if (((s = getenv("LC_ALL")) && *s) ||
((s = getenv("LC_CTYPE")) && *s) ||
((s = getenv("LANG")) && *s)) {
if (strstr(s, "UTF-8"))
utf8_mode = 1;
}
if(!utf8_mode)
cout << "Warning: UTF-8 not enabled. Please stick to English characters!" << endl;
#endif
//cout << "Value in parantesis will be used if no input is specified." << endl;
cout << endl;
cout << TEAMTALK_NAME << " name: ";
servername = LocalToUnicode(printGetString(UnicodeToLocal(servername).c_str()).c_str());
cout << "Message of the Day: ";
motd = LocalToUnicode(printGetString(UnicodeToLocal(motd).c_str()).c_str());
cout << "Maximum users allowed on server: ";
maxusers = printGetInt(maxusers);
if(maxusers > MAX_USERS)
{
cout << "Maximum allowed users is " << MAX_USERS << " and the value has been reduced accordingly." << endl;
maxusers = MAX_USERS;
}
cout << "Server should automatically save changes: ";
autosave = printGetBool(autosave);
cout << "Enable file sharing: ";
if(printGetBool(filesroot.length()))
{
ACE_TCHAR buff[1024] = {};
ACE_OS::getcwd(buff, 1024);
#ifdef WIN32
cout << "Directory for file storage, e.g. C:\\MyServerFiles: ";
#else
cout << "Directory for file storage, e.g. /home/bill/srv1/files: ";
#endif
filesroot = LocalToUnicode(printGetString(UnicodeToLocal(filesroot).c_str()).c_str());
if(!filesroot.empty() && ACE_OS::chdir(filesroot.c_str()) == 0)
{
cout << "Disk quota (in KBytes) per channel, 0 = disabled: ";
diskquota = printGetInt64(diskquota/1024)*1024;
cout << "Maximum disk usage (in KBytes) for storing files: ";
maxdiskusage = printGetInt64(maxdiskusage/1024)*1024;
}
else
{
cout << "Directory not found for file storage. File sharing disabled." << endl;
filesroot.clear();
diskquota = 0;
}
ACE_OS::chdir(buff);
}
else
{
filesroot.clear();
diskquota = 0;
}
cout << "Log server activity: ";
log_maxsize = printGetBool(log_maxsize != 0)? (log_maxsize!=0)? log_maxsize : -1 : 0;
cout << "Server should bind to the following TCP port: ";
tcpport = printGetInt(tcpport);
cout << "Server should bind to the following UDP port: ";
udpport = printGetInt(udpport);
cout << "Bind to specific IP-addresses? (required for IPv6) ";
if (printGetBool(bindips.size()))
{
while (true)
{
if (bindips.size())
{
cout << "Currently binding to IP-addresses:" << endl;
for (auto ip : bindips)
{
cout << "\t- " << (ip.empty()? "0.0.0.0" : ip) << endl;
}
cout << "Specify additional IP-addresses? ";
if (!printGetBool(bindips.empty()))
break;
}
cout << "Specify the IP-address to bind to (IPv6 type \"::\" for all interfaces): " << endl;
std::string ip = printGetString("0.0.0.0");
bindips.push_back(ip);
}
}
else
{
bindips.clear();
}
#if defined(ENABLE_TEAMTALKPRO)
cout << "Should server run in encrypted mode? ";
if(printGetBool(certfile.length() && keyfile.length()))
{
cout << "Server certificate file (PEM file) for encryption: ";
certfile = LocalToUnicode(printGetString(UnicodeToLocal(certfile).c_str()).c_str());
if(ACE_OS::filesize(certfile.c_str())<=0)
cout << "File " << certfile << " not found! Continuing configuration..." << endl;
cout << "Server private key file (PEM file) for encryption: ";
keyfile = LocalToUnicode(printGetString(UnicodeToLocal(keyfile).c_str()).c_str());
if(ACE_OS::filesize(keyfile.c_str())<=0)
cout << "File " << keyfile << " not found! Continuing configuration..." << endl;
}
else
{
certfile.clear();
keyfile.clear();
}
#endif
cout << endl << "User authentication." << endl;
cout << endl;
cout << "User account administration." << endl;
int input = 0;
enum UserAccountOptions {LIST_USERACCOUNTS = 1, CREATE_USERACCOUNT, CREATE_USERACCOUNT_FACEBOOK, DELETE_USERACCOUNT, QUIT_USERACCOUNTS};
ACE_CString url = WEBLOGIN_URL;
std::string xml;
while(input != QUIT_USERACCOUNTS)
{
UserAccount user;
std::string tmp;
int count = 0;
while(xmlSettings.GetNextUser(count, user))count++;
cout << endl;
cout << "Currently there's " << count << " user accounts." << endl;
cout << LIST_USERACCOUNTS << ") List user accounts." << endl;
cout << CREATE_USERACCOUNT << ") Create new user account." << endl;
cout << CREATE_USERACCOUNT_FACEBOOK << ") Create Facebook login account." << endl;
cout << DELETE_USERACCOUNT << ") Delete user account." << endl;
cout << QUIT_USERACCOUNTS << ") Quit and proceed server configuration." << endl;
cout << "Select option: ";
switch( (input = printGetInt(QUIT_USERACCOUNTS)) )
{
case LIST_USERACCOUNTS :
cout << endl;
cout << "Active user accounts:" << endl << endl;
count = 0;
while(xmlSettings.GetNextUser(count, user))
{
cout << "User #" << count + 1 << endl;
cout << "Username: " << user.username << endl;
cout << "Password: " << user.passwd << endl;
switch(user.usertype)
{
case USERTYPE_DEFAULT :
cout << "User type: Default user" << endl;
break;
case USERTYPE_ADMIN :
cout << "User type: Administrator" << endl;