-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessenger.hpp
2302 lines (2071 loc) · 69.9 KB
/
messenger.hpp
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
//
// Created by imper on 2/14/22.
//
#ifndef PRIVACY_PROTECTION_MESSENGER_MESSENGER_HPP
# define PRIVACY_PROTECTION_MESSENGER_MESSENGER_HPP
# include <inet-comm>
# include <deque>
# include <sys/syslog.h>
# include <md5.h>
# include <sys/stat.h>
# include <memory>
# include <sys/wait.h>
# include <utility>
# include <vector>
# include <mariadb/conncpp.hpp>
# include <lua5.3/lua.hpp>
# include "constants.hpp"
# define CONFIG_FILE CONFIG_DIR "/config.lua"
# define LUA_REGISTER_USER_FUNC "permit_register_user"
# define LUA_SET_PASSWORD_FUNC "permit_set_password"
# define LUA_SET_DISPLAY_NAME_FUNC "permit_set_display_name"
# define LUA_GET_DISPLAY_NAME_FUNC "permit_get_display_name"
# define LUA_BEGIN_SESSION_FUNC "permit_begin_session"
# define LUA_GET_PUBKEY_FUNC "permit_get_pubkey"
# define LUA_SEND_MESSAGE_FUNC "permit_send_message"
# define LUA_CHECK_ONLINE_STATUS_FUNC "permit_check_online_status"
# define LUA_FIND_USERS_BY_DISPLAY_NAME_FUNC "permit_find_users_by_display_name"
# define LUA_FIND_USERS_BY_LOGIN_FUNC "permit_find_users_by_login"
# define E_DERANGED "Server is deranged. This is a bug report it!"
# define E_SUCCESS "Success."
# define E_USER_ALREADY_EXISTS "This user already exists."
# define E_INCORRECT_LOGIN "Incorrect login."
# define E_INCORRECT_PASSWORD "Incorrect password."
# define E_TOO_LONG_LOGIN "Too long login string."
# define E_TOO_LONG_PASSWORD "Too long password string."
# define E_TOO_SHORT_PASSWORD "Too short password."
# define E_TOO_LONG_DISPLAY_NAME "Too long display name."
# define E_USER_NOT_FOUND "User not found."
# define E_MESSAGE_NOT_FOUND "Message not found."
# define E_NO_PERMISSION "No permission."
#define HANDLE_ERRORS_ON_CLIENT case HEADER::e_incorrect_login: \
{ \
status = E_INCORRECT_LOGIN; \
return false; \
} \
case HEADER::e_incorrect_password: \
{ \
status = E_INCORRECT_PASSWORD; \
return false; \
} \
case HEADER::e_too_long_login: \
{ \
status = E_TOO_LONG_LOGIN; \
return false; \
} \
case HEADER::e_too_long_password: \
{ \
status = E_TOO_LONG_PASSWORD; \
return false; \
} \
case HEADER::e_too_short_password: \
{ \
status = E_TOO_SHORT_PASSWORD; \
return false; \
} \
case HEADER::e_too_long_display_name: \
{ \
status = E_TOO_LONG_DISPLAY_NAME; \
return false; \
} \
case HEADER::e_no_permission: \
{ \
status = E_NO_PERMISSION; \
return false; \
} \
case HEADER::e_user_already_exists: \
{ \
status = E_USER_ALREADY_EXISTS; \
return false; \
} \
case HEADER::e_user_not_found: \
{ \
status = E_USER_NOT_FOUND; \
return false; \
} \
case HEADER::e_message_not_found: \
{ \
status = E_MESSAGE_NOT_FOUND; \
return false; \
} \
default: \
{ \
LOG << E_DERANGED "\n" << ENDENTLN; \
status = E_DERANGED; \
return false; \
}
# define SUCCESS 0
# undef LOG
# undef ERR
# define LOG (inet::__detail__::_log_ << PRINT_PREFIX)
# define ERR (inet::__detail__::_err_ << PRINT_PREFIX)
namespace msg
{
static bool verbose = false;
namespace __detail__ __attribute__((visibility("hidden")))
{
static const unsigned char cov_2_char[64] = {
/* from crypto/des/fcrypt.c */
0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
};
static const char ascii_dollar[] = {0x24, 0x00};
typedef enum : int
{
passwd_unset = 0,
passwd_md_5,
passwd_apr_1,
passwd_sha_256,
passwd_sha_512,
passwd_aix_md_5
} passwd_modes;
inline static int random_bytes(char** data, int size);
inline static char* md_5_crypt(const char* passwd, const char* magic, const char* salt);
inline static char* sha_crypt(const char* passwd, const char* magic, const char* salt);
inline static bool contains(const char* str, const char* substr);
}
struct HEADER
{
enum signal : int
{
s_zero = 0,
s_register_user,
s_set_password,
s_set_display_name,
s_get_display_name,
s_begin_session,
s_end_session,
s_get_pubkey,
s_send_message,
s_query_incoming,
s_check_online_status,
s_find_users_by_display_name,
s_find_users_by_login
};
signal sig = s_zero;
inline static const char* signal_to_name(signal sig)
{
switch (sig)
{
CASE_TO_STR(s_register_user)
CASE_TO_STR(s_set_password)
CASE_TO_STR(s_set_display_name)
CASE_TO_STR(s_get_display_name)
CASE_TO_STR(s_begin_session)
CASE_TO_STR(s_end_session)
CASE_TO_STR(s_get_pubkey)
CASE_TO_STR(s_send_message)
CASE_TO_STR(s_query_incoming)
CASE_TO_STR(s_check_online_status)
CASE_TO_STR(s_find_users_by_display_name)
CASE_TO_STR(s_find_users_by_login)
default:
return _STR(s_zero);
}
}
inline static signal signal_from_name(const std::string& name)
{
if (name == _STR(s_register_user))
return s_register_user;
else if (name == _STR(s_set_password))
return s_set_password;
else if (name == _STR(s_set_display_name))
return s_set_display_name;
else if (name == _STR(s_get_display_name))
return s_get_display_name;
else if (name == _STR(s_begin_session))
return s_begin_session;
else if (name == _STR(s_end_session))
return s_end_session;
else if (name == _STR(s_get_pubkey))
return s_get_pubkey;
else if (name == _STR(s_send_message))
return s_send_message;
else if (name == _STR(s_query_incoming))
return s_query_incoming;
else if (name == _STR(s_check_online_status))
return s_check_online_status;
else if (name == _STR(s_find_users_by_display_name))
return s_find_users_by_display_name;
else if (name == _STR(s_find_users_by_login))
return s_find_users_by_login;
else return s_zero;
}
size_t login_size = 0;
size_t password_size = 0;
size_t display_name_size = 0;
size_t data_size = 0;
enum error : int
{
e_no_permission = 0,
e_success,
e_user_already_exists,
e_incorrect_login,
e_incorrect_password,
e_too_long_login,
e_too_long_password,
e_too_short_password,
e_too_long_display_name,
e_user_not_found,
e_message_not_found
};
error err = e_no_permission;
};
struct MESSAGE
{
std::string* source = nullptr;
std::string* destination = nullptr;
std::vector<uint8_t>* data = nullptr;
size_t source_size = 0;
size_t destination_size = 0;
size_t data_size = 0;
MESSAGE() = default;
MESSAGE(const MESSAGE& msg)
: source(msg.source ? new std::remove_pointer_t<decltype(source)>(*msg.source) : nullptr),
destination(msg.destination ? new std::remove_pointer_t<decltype(destination)>(*msg.destination) : nullptr),
data(msg.data ? new std::remove_pointer_t<decltype(data)>(*msg.data) : nullptr),
source_size(msg.source->size()),
destination_size(msg.destination->size()),
data_size(msg.data->size())
{ }
~MESSAGE()
{
delete source;
delete destination;
delete data;
}
};
class messenger_io : public inet::inet_io
{
public:
explicit messenger_io(const inet::inet_io& io) : inet::inet_io(io)
{ }
inline bool read(HEADER& header)
{
return read(&header, sizeof header) == sizeof header;
}
inline bool read(MESSAGE& message)
{
if (read(&message, sizeof message) == sizeof message && message.data_size > 0)
{
if (message.source_size > 0)
{
message.source = new std::string(message.source_size, 0);
if (read(message.source->data(), message.source_size) != message.source_size)
return false;
}
if (message.destination_size)
{
message.destination = new std::string(message.destination_size, 0);
if (read(message.destination->data(), message.destination_size) != message.destination_size)
return false;
}
message.data = new std::vector<uint8_t>(message.data_size, 0);
return read(message.data->data(), message.data_size) == message.data_size;
}
return false;
}
template <template <typename> typename Container, typename T>
inline bool read(Container<T>& cont)
{
size_t size;
if (read(&size, sizeof size) != sizeof size) return false;
if (size > 0)
{
cont.resize(size, 0);
return read(cont.data(), size) == size;
}
return true;
}
inline ssize_t read(void* data, size_t size) override
{
return inet_io::read(data, size);
}
inline bool write(const HEADER& header)
{
return write(&header, sizeof header) == sizeof header;
}
inline bool write(MESSAGE& message)
{
if (message.source) message.source_size = message.source->size();
if (message.destination) message.destination_size = message.destination->size();
if (message.data && !message.data->empty())
{
message.data_size = message.data->size();
write(&message, sizeof message);
if (message.source_size > 0) write(message.source->data(), message.source_size);
if (message.destination_size > 0) write(message.destination->data(), message.destination_size);
write(message.data->data(), message.data_size);
return true;
}
return false;
}
template <template <typename> typename Container, typename T>
inline void write(const Container<T>& cont)
{
size_t size = cont.size();
write(&size, sizeof size);
if (size > 0) write(cont.data(), size);
}
template <template <typename> typename Container, typename T>
inline void write_raw(const Container<T>& cont)
{
if (cont.size() > 0) write(cont.data(), cont.size());
}
template <typename T>
inline bool write(const T& obj)
{
return write(&obj, sizeof obj);
}
inline ssize_t write(const void* data, int size) override
{
return inet_io::write(data, size);
}
};
inline static std::pair<std::vector<uint8_t>, std::vector<uint8_t>> generate_cert()
{
std::string error;
auto pkey = inet::generate_pkey(error);
if (!pkey)
{
ERR << "An error occurred while generating public key: " << error << ENDENTLN;
EVP_PKEY_free(pkey);
return {{ },
{ }};
}
auto cert = inet::generate_cert(error, pkey, COUNTRY, ORGANIZATION, CERTIFICATE_NAME);
if (!cert)
{
ERR << "An error occurred while generating certificate: " << error << ENDENTLN;
EVP_PKEY_free(pkey);
X509_free(cert);
return {{ },
{ }};
}
return {inet::convert(cert), inet::convert(pkey)};
}
class client : public inet::client, private messenger_io
{
public:
inline static client* create_client(const inet::inet_address& server_address)
{
auto certpair = generate_cert();
if (certpair.first.empty() || certpair.second.empty())
return nullptr;
return new client(
server_address,
{inet::input_stream::mkstream(certpair.first), inet::input_stream::mkstream(certpair.second)}
);
}
inline bool register_user(const std::string& login, const std::string& password, const std::string& display_name, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_register_user, login.size(), password.size(), display_name.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(display_name);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool set_password(const std::string& login, const std::string& password, const std::string& new_password, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_set_password, login.size(), password.size(), 0, new_password.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(new_password);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool set_display_name(const std::string& login, const std::string& password, const std::string& display_name, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_set_display_name, login.size(), password.size(), display_name.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(display_name);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool get_display_name(
const std::string& login, const std::string& password, const std::string& target, std::string& display_name, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_get_display_name, login.size(), password.size(), 0, target.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(target);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
messenger_io::read(display_name);
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool begin_session(const std::string& login, const std::string& password, const std::vector<uint8_t>& pubkey, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_begin_session, login.size(), password.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write(pubkey);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool end_session(const std::string& login, const std::string& password, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_end_session, login.size(), password.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool get_pubkey(
const std::string& login, const std::string& password, const std::string& target, std::vector<uint8_t>& pubkey, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_get_pubkey, login.size(), password.size(), 0, target.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(target);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
messenger_io::read(pubkey);
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool send_message(
const std::string& login, const std::string& password, MESSAGE& message, std::string& status)
{
size_t message_size = sizeof message;
message.source = nullptr;
if (message.destination) message_size += message.destination->size();
if (message.data) message_size += message.data->size();
messenger_io::write(HEADER{HEADER::s_send_message, login.size(), password.size(), 0, message_size});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write(message);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool query_incoming(
const std::string& login, const std::string& password, MESSAGE& message, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_query_incoming, login.size(), password.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
return messenger_io::read(message);
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool check_online_status(
const std::string& login, const std::string& password, const std::string& another_user, bool& online_status, std::string& status)
{
messenger_io::write(HEADER{HEADER::s_check_online_status, login.size(), password.size(), 0, another_user.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(another_user);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
messenger_io::read(&online_status, sizeof online_status);
status = E_SUCCESS;
return true;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool find_users_by_display_name(
const std::string& login, const std::string& password, const std::string& display_name, std::list<std::string>& list,
std::string& status)
{
messenger_io::write(HEADER{HEADER::s_find_users_by_display_name, login.size(), password.size(), display_name.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(display_name);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
size_t amount = 0;
if (messenger_io::read(&amount, sizeof amount) == amount)
{
amount = std::min(MAX_USER_ENTRIES_AMOUNT, amount);
for (size_t i = 0; i < amount; ++i)
{
std::string entry;
if (!messenger_io::read(entry)) return false;
list.push_back(entry);
}
return true;
}
return false;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
inline bool find_users_by_login(
const std::string& login, const std::string& password, const std::string& another_user, std::list<std::string>& list,
std::string& status)
{
messenger_io::write(HEADER{HEADER::s_find_users_by_login, login.size(), password.size(), 0, another_user.size()});
messenger_io::write_raw(login);
messenger_io::write_raw(password);
messenger_io::write_raw(another_user);
HEADER res;
if (messenger_io::read(res))
{
switch (res.err)
{
case HEADER::e_success:
{
status = E_SUCCESS;
size_t amount = 0;
if (messenger_io::read(&amount, sizeof amount))
{
amount = std::min(MAX_USER_ENTRIES_AMOUNT, amount);
for (size_t i = 0; i < amount; ++i)
{
std::string entry;
if (!messenger_io::read(entry)) return false;
list.push_back(entry);
}
return true;
}
return false;
}
HANDLE_ERRORS_ON_CLIENT
}
}
return false;
}
private:
inline explicit client(const inet::inet_address& server_address, inet::loader&& crt)
: messenger_io(inet::inet_io()), inet::client(server_address, std::move(crt))
{
this->messenger_io::ssl = this->inet::client::ssl;
this->messenger_io::socket = this->inet::client::socket;
this->messenger_io::success = this->inet::client::success;
}
};
class MESSAGES
{
public:
MESSAGES() = default;
MESSAGES(const MESSAGES&) = delete;
MESSAGES(MESSAGES&&) = delete;
void put_message(const std::string& user, const MESSAGE& message)
{
incoming[user].push_back(message);
}
bool message_available(const std::string& user)
{
auto user_it = incoming.find(user);
if (user_it != incoming.end())
{
return !user_it->second.empty();
}
return false;
}
MESSAGE invoke_message(const std::string& user)
{
auto res = incoming[user].front();
incoming[user].pop_front();
return res;
}
private:
std::map<std::string, std::deque<MESSAGE>> incoming;
};
class server : private inet::server
{
public:
inline static server* create_server(
int max_clients, const inet::inet_address& address, const std::string& db_login, const std::string& db_password)
{
auto certpair = generate_cert();
if (certpair.first.empty() || certpair.second.empty())
return nullptr;
return new server(
max_clients, address, db_login, db_password,
{inet::input_stream::mkstream(certpair.first), inet::input_stream::mkstream(certpair.second)}
);
}
inline bool run()
{
return inet::server::run(true);
}
private:
struct USER_DATA
{
std::string salt;
std::string password;
std::string display_name;
};
struct USER_STATUS
{
bool is_session_running = false;
std::vector<uint8_t> pubkey;
};
public:
class mariadb_user_manager
{
public:
inline mariadb_user_manager(const std::string& login, const std::string& password, std::string table_name)
: table_name(std::move(table_name))
{
try
{
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString url("jdbc:mariadb://localhost:3306/" DATABASE_NAME);
sql::Properties properties(
{{"user", login},
{"password", password}}
);
connection = std::unique_ptr<sql::Connection>(driver->connect(url, properties));
if (verbose) ::syslog(LOG_DEBUG, "Connected to " DATABASE_NAME " database.");
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::mariadb_user_manager(login, password, table_name)) ": " << e.what() << ENDENTLN;
::exit(e.getErrorCode());
}
}
inline int32_t create()
{
try
{
std::unique_ptr<sql::PreparedStatement> statement(
connection->prepareStatement(
"create table " + table_name +
" ( "
"login varchar(" MACRO_STR(MAX_LOGIN) ") NOT NULL PRIMARY KEY,"
"display_name varchar(" MACRO_STR(MAX_DISPLAY_NAME) ") NOT NULL,"
"salt varchar(16) NOT NULL,"
"password varchar(106) NOT NULL"
" );"
)
);
statement->executeQuery();
if (verbose) ::syslog(LOG_DEBUG, "Table \"%s\" created.", table_name.c_str());
return SUCCESS;
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::create()) ": " << e.what() << ENDENTLN;
return e.getErrorCode();
}
}
inline int32_t save_user(const std::pair<std::string, USER_DATA>& userpair)
{
return save_user(userpair.first, userpair.second);
}
inline int32_t save_user(const std::string& login, const USER_DATA& userdata)
{
try
{
std::unique_ptr<sql::PreparedStatement> statement(
connection->prepareStatement(
"insert into " + table_name +
" ( login, display_name, salt, password ) values( '" + login + "','" +
userdata.display_name + "','"
+ userdata.salt + "','" + userdata.password + "' );"
)
);
statement->executeQuery();
if (verbose) ::syslog(LOG_DEBUG, "User \"%s\" saved.", login.c_str());
return SUCCESS;
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::save_user(login, userdata)) ": " << e.what() << ENDENTLN;
return e.getErrorCode();
}
}
inline int32_t update_user(const std::pair<std::string, USER_DATA>& userpair)
{
return update_user(userpair.first, userpair.second);
}
inline int32_t update_user(const std::string& login, const USER_DATA& userdata)
{
try
{
std::unique_ptr<sql::PreparedStatement> statement(
connection->prepareStatement(
"update " + table_name + " set display_name='" + userdata.display_name + "', password='"
+ userdata.password + "', salt='" + userdata.salt + "' where login='" + login + "';"
)
);
statement->executeQuery();
if (verbose) ::syslog(LOG_DEBUG, "User \"%s\" updated.", login.c_str());
return SUCCESS;
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::update_user(login, userdata)) ": " << e.what() << ENDENTLN;
return e.getErrorCode();
}
}
inline int32_t load_user(const std::string& login)
{
try
{
std::unique_ptr<sql::Statement> statement(connection->createStatement());
std::unique_ptr<sql::ResultSet> res(
statement->executeQuery("select * from " + table_name + " where login='" + login + "'")
);
res->next();
std::string salt = res->getString(3).c_str();
std::string password = res->getString(4).c_str();
std::string display_name = res->getString(2).c_str();
if (!salt.empty() || !password.empty() || !display_name.empty())
{
users.insert({login, USER_DATA{salt, password, display_name}});
if (verbose) ::syslog(LOG_DEBUG, "User \"%s\" loaded.", login.c_str());
}
return SUCCESS;
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::load_user(login)) ": " << e.what() << ENDENTLN;
return e.getErrorCode();
}
}
static inline bool unload_user(const std::string& login)
{
auto user = users.find(login);
if (user != users.end())
{
users.erase(user);
if (verbose) ::syslog(LOG_DEBUG, "User \"%s\" unloaded.", login.c_str());
return true;
}
return false;
}
inline ~mariadb_user_manager()
{
try
{
connection->close();
if (verbose) ::syslog(LOG_DEBUG, "Disconnected from " DATABASE_NAME " database.");
}
catch (sql::SQLException& e)
{
ERR << "Error in " _STR(mariadb_user_manager::mariadb_user_manager()) ": " << e.what() << ENDENTLN;
}
}
private:
std::unique_ptr<sql::Connection> connection = nullptr;
std::string table_name;
};
private:
static std::map<std::string, USER_STATUS> statuses;
static std::map<std::string, USER_DATA> users;
static MESSAGES incoming;
static std::recursive_mutex mutex;
std::unique_ptr<mariadb_user_manager> db_user_manager = nullptr;
lua_State* lua = luaL_newstate();
class scope_indicator
{
public:
inline explicit scope_indicator(const std::string& scope) : scope(scope)
{
if (verbose)
{
mutex.lock();
++spaces_cnt;
std::string spaces;
for (int i = 0; i < spaces_cnt; ++i) spaces += " ";
::syslog(LOG_DEBUG, "%s{>>} Entered scope \"%s\"", spaces.c_str(), scope.c_str());
mutex.unlock();
}
}
inline ~scope_indicator()
{
if (verbose)
{
mutex.lock();
std::string spaces;
for (int i = 0; i < spaces_cnt; ++i) spaces += " ";
::syslog(LOG_DEBUG, "%s{<<} Exited scope \"%s\"", spaces.c_str(), scope.c_str());
--spaces_cnt;
mutex.unlock();
}
}
private:
std::string scope;
static int spaces_cnt;
static std::mutex mutex;