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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
|
#include "hybbx/storage.h"
#include "hybbx/auth.h"
#include "hybbx/password.h"
#include "hybbx/security.h"
#include "hybbx/config.h"
#include "hybbx/util.h"
#include "hybbx/limits.h"
#include "hybbx/log.h"
#include "storage_private.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
struct flatfile_state {
char users_dir[HYBBX_PATH_MAX];
char legacy_users_path[HYBBX_PATH_MAX];
char sessions_path[HYBBX_PATH_MAX];
char session_next_path[HYBBX_PATH_MAX];
char user_next_path[HYBBX_PATH_MAX];
};
typedef struct user_shard {
hybbx_user_record_t users[HYBBX_USERS_PER_FILE];
size_t count;
} user_shard_t;
static int mkdir_p(const char *path)
{
char buf[HYBBX_PATH_MAX];
size_t len;
size_t i;
if (path == NULL || path[0] == '\0') {
return -1;
}
len = strlen(path);
if (len >= sizeof(buf)) {
return -1;
}
memcpy(buf, path, len + 1);
for (i = 1; i < len; i++) {
if (buf[i] == '/') {
buf[i] = '\0';
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
return -1;
}
buf[i] = '/';
}
}
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
return -1;
}
return 0;
}
static hybbx_result_t read_counter(const char *path, uint64_t *value)
{
FILE *fp;
unsigned long long n = 0;
fp = fopen(path, "r");
if (fp == NULL) {
*value = 0;
return HYBBX_OK;
}
if (fscanf(fp, "%llu", &n) != 1) {
fclose(fp);
return HYBBX_ERR_IO;
}
fclose(fp);
*value = (uint64_t)n;
return HYBBX_OK;
}
static hybbx_result_t write_counter(const char *path, uint64_t value)
{
FILE *fp;
fp = fopen(path, "w");
if (fp == NULL) {
return HYBBX_ERR_IO;
}
fprintf(fp, "%llu\n", (unsigned long long)value);
fclose(fp);
return HYBBX_OK;
}
static hybbx_result_t bump_counter(const char *path, uint64_t *value)
{
hybbx_result_t rc;
rc = read_counter(path, value);
if (rc != HYBBX_OK) {
return rc;
}
(*value)++;
return write_counter(path, *value);
}
static hybbx_result_t append_line(const char *path, const char *line)
{
FILE *fp;
fp = fopen(path, "a");
if (fp == NULL) {
return HYBBX_ERR_IO;
}
fprintf(fp, "%s\n", line);
fclose(fp);
return HYBBX_OK;
}
#define HYBBX_USER_NICK_SUFFIX ".nick"
static hybbx_result_t user_nickname_path(const struct flatfile_state *state,
const char *username,
char *out, size_t out_len)
{
char name[HYBBX_USER_NAME_MAX + 8];
int n;
if (state == NULL || username == NULL || username[0] == '\0' ||
out == NULL || out_len == 0) {
return HYBBX_ERR_INVALID;
}
n = snprintf(name, sizeof(name), "%s%s", username, HYBBX_USER_NICK_SUFFIX);
if (n < 0 || (size_t)n >= sizeof(name)) {
return HYBBX_ERR_INVALID;
}
return hybbx_path_join(out, out_len, state->users_dir, name);
}
static hybbx_result_t read_nickname_file(const struct flatfile_state *state,
const char *username,
char *nickname,
size_t nickname_len)
{
char path[HYBBX_PATH_MAX];
FILE *fp;
char line[HYBBX_USER_NICKNAME_MAX];
char *nl;
hybbx_result_t rc;
if (nickname == NULL || nickname_len == 0) {
return HYBBX_ERR_INVALID;
}
nickname[0] = '\0';
rc = user_nickname_path(state, username, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
fp = fopen(path, "r");
if (fp == NULL) {
return HYBBX_ERR_NOT_FOUND;
}
if (fgets(line, sizeof(line), fp) == NULL) {
fclose(fp);
return HYBBX_ERR_IO;
}
fclose(fp);
nl = strchr(line, '\n');
if (nl != NULL) {
*nl = '\0';
}
nl = strchr(line, '\r');
if (nl != NULL) {
*nl = '\0';
}
if (line[0] == '\0') {
return HYBBX_ERR_NOT_FOUND;
}
hybbx_strlcpy(nickname, line, nickname_len);
return HYBBX_OK;
}
static hybbx_result_t write_nickname_file(const struct flatfile_state *state,
const char *username,
const char *nickname)
{
char path[HYBBX_PATH_MAX];
FILE *fp;
hybbx_result_t rc;
if (state == NULL || username == NULL || username[0] == '\0' ||
nickname == NULL || nickname[0] == '\0') {
return HYBBX_ERR_INVALID;
}
rc = user_nickname_path(state, username, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
fp = fopen(path, "w");
if (fp == NULL) {
return HYBBX_ERR_IO;
}
fprintf(fp, "%s\n", nickname);
if (fclose(fp) != 0) {
return HYBBX_ERR_IO;
}
return HYBBX_OK;
}
static void remove_nickname_file(const struct flatfile_state *state,
const char *username)
{
char path[HYBBX_PATH_MAX];
if (state == NULL || username == NULL || username[0] == '\0') {
return;
}
if (user_nickname_path(state, username, path, sizeof(path)) == HYBBX_OK) {
(void)remove(path);
}
}
static int str_ieq(const char *a, const char *b)
{
if (a == NULL || b == NULL) {
return 0;
}
while (*a != '\0' && *b != '\0') {
char ca = (char)(*a >= 'A' && *a <= 'Z' ? *a + 32 : *a);
char cb = (char)(*b >= 'A' && *b <= 'Z' ? *b + 32 : *b);
if (ca != cb) {
return 0;
}
a++;
b++;
}
return *a == '\0' && *b == '\0';
}
static int file_exists(const char *path)
{
struct stat st;
return path != NULL && stat(path, &st) == 0 && S_ISREG(st.st_mode);
}
static hybbx_result_t user_shard_path(const struct flatfile_state *state,
unsigned shard_index,
char *out, size_t out_len)
{
char name[32];
int n;
if (state == NULL || out == NULL || out_len == 0 || shard_index == 0) {
return HYBBX_ERR_INVALID;
}
if (shard_index == 1) {
n = snprintf(name, sizeof(name), "users.ini");
} else {
n = snprintf(name, sizeof(name), "users%u.ini", shard_index);
}
if (n < 0 || (size_t)n >= sizeof(name)) {
return HYBBX_ERR_INVALID;
}
return hybbx_path_join(out, out_len, state->users_dir, name);
}
static unsigned last_user_shard_index(const struct flatfile_state *state)
{
char path[HYBBX_PATH_MAX];
unsigned index;
for (index = 1; index < 10000u; index++) {
if (user_shard_path(state, index, path, sizeof(path)) != HYBBX_OK) {
break;
}
if (!file_exists(path)) {
break;
}
}
if (index == 1) {
return 0;
}
return index - 1;
}
static int parse_legacy_user_line(const char *line, hybbx_user_record_t *out)
{
unsigned long long id = 0;
unsigned long long active = 0;
long long created = 0;
char username[HYBBX_USER_NAME_MAX];
char level_name[32];
char full_name[HYBBX_USER_FULL_NAME_MAX];
char country[HYBBX_USER_COUNTRY_MAX];
char location[HYBBX_USER_LOCATION_MAX];
char email[HYBBX_USER_EMAIL_MAX];
char password[HYBBX_USER_PASSWORD_MAX];
int fields;
if (line == NULL || out == NULL) {
return 0;
}
full_name[0] = '\0';
country[0] = '\0';
location[0] = '\0';
email[0] = '\0';
password[0] = '\0';
fields = sscanf(line,
"%llu|%63[^|]|%31[^|]|%llu|%lld|"
"%127[^|]|%63[^|]|%127[^|]|%127[^|]|%95[^|]",
&id, username, level_name, &active, &created,
full_name, country, location, email, password);
if (fields < 5) {
return 0;
}
memset(out, 0, sizeof(*out));
out->id = (uint64_t)id;
hybbx_strlcpy(out->username, username, sizeof(out->username));
out->level = hybbx_user_level_parse(level_name);
out->active = (int)active;
out->created_at = (time_t)created;
if (fields >= 9) {
hybbx_strlcpy(out->full_name, full_name, sizeof(out->full_name));
hybbx_strlcpy(out->country, country, sizeof(out->country));
hybbx_strlcpy(out->location, location, sizeof(out->location));
hybbx_strlcpy(out->email, email, sizeof(out->email));
}
if (fields >= 10) {
hybbx_strlcpy(out->password, password, sizeof(out->password));
}
return 1;
}
static int parse_user_section(const hybbx_config_t *cfg, const char *section,
hybbx_user_record_t *out)
{
const char *value;
unsigned long long n;
if (cfg == NULL || section == NULL || out == NULL) {
return 0;
}
if (strncmp(section, HYBBX_USER_INI_SECTION_PREFIX,
strlen(HYBBX_USER_INI_SECTION_PREFIX)) != 0) {
return 0;
}
memset(out, 0, sizeof(*out));
value = hybbx_config_get(cfg, section, "id", NULL);
if (value == NULL || value[0] == '\0') {
return 0;
}
n = strtoull(value, NULL, 10);
if (n == 0) {
return 0;
}
out->id = (uint64_t)n;
value = hybbx_config_get(cfg, section, "username", NULL);
if (value == NULL || value[0] == '\0') {
return 0;
}
hybbx_strlcpy(out->username, value, sizeof(out->username));
hybbx_username_normalize(out->username);
value = hybbx_config_get(cfg, section, "nickname", NULL);
if (value != NULL) {
hybbx_strlcpy(out->nickname, value, sizeof(out->nickname));
}
value = hybbx_config_get(cfg, section, "level", "user");
out->level = hybbx_user_level_parse(value);
out->active = hybbx_config_get_bool(cfg, section, "active", 0);
value = hybbx_config_get(cfg, section, "created", NULL);
if (value != NULL && value[0] != '\0') {
out->created_at = (time_t)strtoll(value, NULL, 10);
}
value = hybbx_config_get(cfg, section, "fullname", NULL);
if (value != NULL) {
hybbx_strlcpy(out->full_name, value, sizeof(out->full_name));
}
value = hybbx_config_get(cfg, section, "country", NULL);
if (value != NULL) {
hybbx_strlcpy(out->country, value, sizeof(out->country));
}
value = hybbx_config_get(cfg, section, "location", NULL);
if (value != NULL) {
hybbx_strlcpy(out->location, value, sizeof(out->location));
}
value = hybbx_config_get(cfg, section, "email", NULL);
if (value != NULL) {
hybbx_strlcpy(out->email, value, sizeof(out->email));
}
value = hybbx_config_get(cfg, section, "password", NULL);
if (value != NULL) {
hybbx_strlcpy(out->password, value, sizeof(out->password));
}
value = hybbx_config_get(cfg, section, "last_login", NULL);
if (value != NULL && value[0] != '\0') {
out->last_login_at = (time_t)strtoll(value, NULL, 10);
}
return 1;
}
typedef struct section_list_ctx {
char names[HYBBX_USERS_PER_FILE][HYBBX_CONFIG_SECTION_MAX];
size_t count;
} section_list_ctx_t;
static void section_list_collect(const char *section, const char *key,
const char *value, void *ctx)
{
section_list_ctx_t *list = (section_list_ctx_t *)ctx;
size_t i;
(void)key;
(void)value;
if (section == NULL || list == NULL) {
return;
}
if (strncmp(section, HYBBX_USER_INI_SECTION_PREFIX,
strlen(HYBBX_USER_INI_SECTION_PREFIX)) != 0) {
return;
}
for (i = 0; i < list->count; i++) {
if (strcmp(list->names[i], section) == 0) {
return;
}
}
if (list->count >= HYBBX_USERS_PER_FILE) {
return;
}
hybbx_strlcpy(list->names[list->count], section,
sizeof(list->names[list->count]));
list->count++;
}
static hybbx_result_t load_user_shard(const char *path, user_shard_t *shard)
{
hybbx_config_t cfg;
section_list_ctx_t sections;
size_t i;
hybbx_result_t rc;
if (path == NULL || shard == NULL) {
return HYBBX_ERR_INVALID;
}
memset(shard, 0, sizeof(*shard));
memset(§ions, 0, sizeof(sections));
rc = hybbx_config_load(&cfg, path);
if (rc != HYBBX_OK) {
return rc;
}
hybbx_config_foreach(&cfg, section_list_collect, §ions);
for (i = 0; i < sections.count; i++) {
hybbx_user_record_t user;
if (!parse_user_section(&cfg, sections.names[i], &user)) {
continue;
}
if (shard->count >= HYBBX_USERS_PER_FILE) {
hybbx_config_free(&cfg);
return HYBBX_ERR_IO;
}
shard->users[shard->count++] = user;
}
hybbx_config_free(&cfg);
return HYBBX_OK;
}
static hybbx_result_t save_user_shard(const struct flatfile_state *state,
unsigned shard_index,
const user_shard_t *shard)
{
char path[HYBBX_PATH_MAX];
FILE *fp;
size_t i;
hybbx_result_t rc;
if (state == NULL || shard == NULL || shard->count > HYBBX_USERS_PER_FILE) {
return HYBBX_ERR_INVALID;
}
rc = user_shard_path(state, shard_index, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
fp = fopen(path, "w");
if (fp == NULL) {
return HYBBX_ERR_IO;
}
fprintf(fp, "; HyBBX user file (max %u users)\n",
(unsigned)HYBBX_USERS_PER_FILE);
fprintf(fp, "; shard %u\n\n", shard_index);
for (i = 0; i < shard->count; i++) {
const hybbx_user_record_t *user = &shard->users[i];
fprintf(fp, "[user.%llu]\n", (unsigned long long)user->id);
fprintf(fp, "id = %llu\n", (unsigned long long)user->id);
fprintf(fp, "username = %s\n", user->username);
fprintf(fp, "nickname = %s\n", user->nickname);
fprintf(fp, "level = %s\n", hybbx_user_level_name(user->level));
fprintf(fp, "active = %s\n", hybbx_bool_to_string(user->active));
fprintf(fp, "created = %lld\n", (long long)user->created_at);
fprintf(fp, "fullname = %s\n", user->full_name);
fprintf(fp, "country = %s\n", user->country);
fprintf(fp, "location = %s\n", user->location);
fprintf(fp, "email = %s\n", user->email);
fprintf(fp, "password = %s\n", user->password);
fprintf(fp, "last_login = %lld\n", (long long)user->last_login_at);
if (user->nickname[0] != '\0') {
(void)write_nickname_file(state, user->username, user->nickname);
}
}
if (fclose(fp) != 0) {
return HYBBX_ERR_IO;
}
return HYBBX_OK;
}
static hybbx_result_t find_user_shard(const struct flatfile_state *state,
uint64_t user_id,
unsigned *shard_index,
size_t *slot_index)
{
unsigned shard;
unsigned last;
char path[HYBBX_PATH_MAX];
user_shard_t loaded;
size_t i;
hybbx_result_t rc;
if (state == NULL || user_id == 0 || shard_index == NULL ||
slot_index == NULL) {
return HYBBX_ERR_INVALID;
}
last = last_user_shard_index(state);
for (shard = 1; shard <= last; shard++) {
if (user_shard_path(state, shard, path, sizeof(path)) != HYBBX_OK) {
continue;
}
if (!file_exists(path)) {
continue;
}
rc = load_user_shard(path, &loaded);
if (rc != HYBBX_OK) {
return rc;
}
for (i = 0; i < loaded.count; i++) {
if (loaded.users[i].id == user_id) {
*shard_index = shard;
*slot_index = i;
return HYBBX_OK;
}
}
}
return HYBBX_ERR_NOT_FOUND;
}
static hybbx_result_t append_user_record(struct flatfile_state *state,
const hybbx_user_record_t *user)
{
unsigned shard;
unsigned last;
char path[HYBBX_PATH_MAX];
user_shard_t loaded;
hybbx_result_t rc;
if (state == NULL || user == NULL) {
return HYBBX_ERR_INVALID;
}
last = last_user_shard_index(state);
if (last == 0) {
shard = 1;
memset(&loaded, 0, sizeof(loaded));
} else {
rc = user_shard_path(state, last, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
rc = load_user_shard(path, &loaded);
if (rc != HYBBX_OK) {
return rc;
}
if (loaded.count >= HYBBX_USERS_PER_FILE) {
shard = last + 1;
memset(&loaded, 0, sizeof(loaded));
} else {
shard = last;
}
}
if (loaded.count >= HYBBX_USERS_PER_FILE) {
return HYBBX_ERR_BUSY;
}
loaded.users[loaded.count++] = *user;
return save_user_shard(state, shard, &loaded);
}
static hybbx_result_t ensure_default_sysop(hybbx_storage_t *storage)
{
struct flatfile_state *state;
size_t sysop_count = 0;
hybbx_user_record_t sysop;
char plain_password[HYBBX_PASSWORD_MAX_LEN + 1];
uint64_t user_id;
time_t now = time(NULL);
hybbx_result_t rc;
if (storage == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
rc = hybbx_storage_flatfile_count_level(storage, HYBBX_LEVEL_SYSOP,
&sysop_count);
if (rc != HYBBX_OK) {
return rc;
}
if (sysop_count > 0) {
return HYBBX_OK;
}
rc = bump_counter(state->user_next_path, &user_id);
if (rc != HYBBX_OK) {
return rc;
}
memset(&sysop, 0, sizeof(sysop));
sysop.id = user_id;
hybbx_strlcpy(sysop.username, "sysop", sizeof(sysop.username));
hybbx_strlcpy(sysop.nickname, HYBBX_DEFAULT_SYSOP_USERNAME,
sizeof(sysop.nickname));
sysop.level = HYBBX_LEVEL_SYSOP;
sysop.active = 1;
sysop.created_at = now;
hybbx_strlcpy(sysop.full_name, "System Operator", sizeof(sysop.full_name));
if (hybbx_password_generate_alnum(plain_password, sizeof(plain_password),
HYBBX_SYSOP_INIT_PASSWORD_MIN,
HYBBX_SYSOP_INIT_PASSWORD_MAX) != HYBBX_OK) {
return HYBBX_ERR_IO;
}
if (hybbx_password_hash(plain_password,
sysop.password, sizeof(sysop.password)) != HYBBX_OK) {
return HYBBX_ERR_IO;
}
rc = append_user_record(state, &sysop);
if (rc != HYBBX_OK) {
return rc;
}
hybbx_log_info("[storage] created default Sysop at %s/users/users.ini (login: %s / %s)",
storage->path, sysop.nickname, plain_password);
hybbx_security_log_write("sysop_created user=%s", sysop.nickname);
return HYBBX_OK;
}
static hybbx_result_t foreach_user(struct flatfile_state *state,
hybbx_result_t (*fn)(const hybbx_user_record_t *user,
void *ctx),
void *ctx)
{
unsigned shard;
unsigned last;
char path[HYBBX_PATH_MAX];
user_shard_t loaded;
size_t i;
hybbx_result_t rc;
if (state == NULL || fn == NULL) {
return HYBBX_ERR_INVALID;
}
last = last_user_shard_index(state);
for (shard = 1; shard <= last; shard++) {
rc = user_shard_path(state, shard, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
rc = load_user_shard(path, &loaded);
if (rc != HYBBX_OK) {
return rc;
}
for (i = 0; i < loaded.count; i++) {
rc = fn(&loaded.users[i], ctx);
if (rc != HYBBX_OK) {
return rc;
}
}
}
return HYBBX_OK;
}
typedef struct find_user_ctx {
const char *username;
hybbx_user_record_t *out;
int found;
} find_user_ctx_t;
static hybbx_result_t find_user_cb(const hybbx_user_record_t *user, void *ctx)
{
find_user_ctx_t *fctx = (find_user_ctx_t *)ctx;
if (str_ieq(user->username, fctx->username)) {
*fctx->out = *user;
fctx->found = 1;
return HYBBX_ERR_BUSY;
}
return HYBBX_OK;
}
typedef struct find_nickname_ctx {
const char *nickname;
hybbx_user_record_t *out;
int found;
} find_nickname_ctx_t;
static hybbx_result_t find_nickname_cb(const hybbx_user_record_t *user, void *ctx)
{
find_nickname_ctx_t *fctx = (find_nickname_ctx_t *)ctx;
if (user->nickname[0] != '\0' && str_ieq(user->nickname, fctx->nickname)) {
*fctx->out = *user;
fctx->found = 1;
return HYBBX_ERR_BUSY;
}
return HYBBX_OK;
}
typedef struct identity_taken_ctx {
const char *username;
const char *nickname;
int taken;
} identity_taken_ctx_t;
static hybbx_result_t identity_taken_cb(const hybbx_user_record_t *user,
void *ctx)
{
identity_taken_ctx_t *ictx = (identity_taken_ctx_t *)ctx;
if (str_ieq(user->username, ictx->username) ||
str_ieq(user->username, ictx->nickname) ||
(user->nickname[0] != '\0' &&
(str_ieq(user->nickname, ictx->nickname) ||
str_ieq(user->nickname, ictx->username)))) {
ictx->taken = 1;
return HYBBX_ERR_BUSY;
}
return HYBBX_OK;
}
typedef struct count_level_ctx {
hybbx_user_level_t level;
size_t count;
} count_level_ctx_t;
static hybbx_result_t count_level_cb(const hybbx_user_record_t *user, void *ctx)
{
count_level_ctx_t *cctx = (count_level_ctx_t *)ctx;
if (user->level == cctx->level) {
cctx->count++;
}
return HYBBX_OK;
}
typedef struct migrate_passwords_ctx {
hybbx_storage_t *storage;
hybbx_result_t last_error;
} migrate_passwords_ctx_t;
static hybbx_result_t migrate_passwords_cb(const hybbx_user_record_t *user,
void *ctx)
{
migrate_passwords_ctx_t *mctx = (migrate_passwords_ctx_t *)ctx;
hybbx_user_record_t updated;
hybbx_result_t rc;
if (!hybbx_password_is_plain(user->password)) {
return HYBBX_OK;
}
updated = *user;
rc = hybbx_password_hash(user->password, updated.password,
sizeof(updated.password));
if (rc != HYBBX_OK) {
mctx->last_error = rc;
return rc;
}
rc = hybbx_storage_update_user(mctx->storage, &updated);
if (rc != HYBBX_OK) {
mctx->last_error = rc;
return rc;
}
hybbx_log_info("[storage] upgraded plain password to sha256 for user %s",
user->username);
return HYBBX_OK;
}
static hybbx_result_t migrate_legacy_users_dat(hybbx_storage_t *storage)
{
struct flatfile_state *state;
FILE *fp;
char line[HYBBX_CONFIG_LINE_MAX];
user_shard_t shard;
unsigned shard_index = 1;
hybbx_result_t rc;
if (storage == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
if (!file_exists(state->legacy_users_path)) {
return HYBBX_OK;
}
if (last_user_shard_index(state) > 0) {
return HYBBX_OK;
}
fp = fopen(state->legacy_users_path, "r");
if (fp == NULL) {
return HYBBX_ERR_IO;
}
memset(&shard, 0, sizeof(shard));
while (fgets(line, sizeof(line), fp) != NULL) {
hybbx_user_record_t user;
char *nl;
nl = strchr(line, '\n');
if (nl != NULL) {
*nl = '\0';
}
if (!parse_legacy_user_line(line, &user)) {
continue;
}
if (shard.count >= HYBBX_USERS_PER_FILE) {
rc = save_user_shard(state, shard_index, &shard);
if (rc != HYBBX_OK) {
fclose(fp);
return rc;
}
shard_index++;
memset(&shard, 0, sizeof(shard));
}
shard.users[shard.count++] = user;
}
fclose(fp);
if (shard.count > 0) {
rc = save_user_shard(state, shard_index, &shard);
if (rc != HYBBX_OK) {
return rc;
}
}
{
char backup[HYBBX_PATH_MAX];
if (hybbx_path_join(backup, sizeof(backup), storage->path,
"users.dat.migrated") == HYBBX_OK) {
(void)rename(state->legacy_users_path, backup);
}
}
hybbx_log_info("[storage] migrated legacy users.dat to INI user files");
return HYBBX_OK;
}
typedef struct migrate_identities_ctx {
hybbx_storage_t *storage;
struct flatfile_state *state;
hybbx_result_t last_error;
} migrate_identities_ctx_t;
static hybbx_result_t migrate_identities_cb(const hybbx_user_record_t *user,
void *ctx)
{
migrate_identities_ctx_t *mctx = (migrate_identities_ctx_t *)ctx;
hybbx_user_record_t updated;
char stored_username[HYBBX_USER_NAME_MAX];
int changed = 0;
hybbx_result_t rc;
if (user == NULL || mctx == NULL) {
return HYBBX_OK;
}
updated = *user;
hybbx_strlcpy(stored_username, updated.username, sizeof(stored_username));
hybbx_username_normalize(updated.username);
if (!str_ieq(stored_username, updated.username)) {
changed = 1;
}
if (updated.nickname[0] == '\0') {
if (read_nickname_file(mctx->state, updated.username, updated.nickname,
sizeof(updated.nickname)) != HYBBX_OK) {
hybbx_nickname_infer(stored_username, updated.nickname,
sizeof(updated.nickname));
}
changed = 1;
}
if (!changed) {
(void)write_nickname_file(mctx->state, updated.username,
updated.nickname);
return HYBBX_OK;
}
rc = hybbx_storage_flatfile_update_user(mctx->storage, &updated);
if (rc != HYBBX_OK) {
mctx->last_error = rc;
return rc;
}
return HYBBX_OK;
}
static hybbx_result_t migrate_user_identities(hybbx_storage_t *storage)
{
struct flatfile_state *state;
migrate_identities_ctx_t ctx;
if (storage == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
memset(&ctx, 0, sizeof(ctx));
ctx.storage = storage;
ctx.state = state;
ctx.last_error = HYBBX_OK;
return foreach_user(state, migrate_identities_cb, &ctx);
}
static hybbx_result_t migrate_plain_passwords(hybbx_storage_t *storage)
{
struct flatfile_state *state;
migrate_passwords_ctx_t ctx;
if (storage == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
ctx.storage = storage;
ctx.last_error = HYBBX_OK;
return foreach_user(state, migrate_passwords_cb, &ctx);
}
hybbx_result_t hybbx_storage_flatfile_open(hybbx_storage_t *storage)
{
struct flatfile_state *state;
hybbx_result_t rc;
if (storage == NULL || storage->path == NULL) {
return HYBBX_ERR_INVALID;
}
if (mkdir_p(storage->path) != 0) {
hybbx_log_warn("[storage] cannot create data path '%s'",
storage->path);
return HYBBX_ERR_IO;
}
state = calloc(1, sizeof(*state));
if (state == NULL) {
return HYBBX_ERR_NOMEM;
}
rc = hybbx_path_join(state->users_dir, sizeof(state->users_dir),
storage->path, "users");
if (rc != HYBBX_OK) {
free(state);
return rc;
}
if (mkdir_p(state->users_dir) != 0) {
free(state);
return HYBBX_ERR_IO;
}
rc = hybbx_path_join(state->legacy_users_path, sizeof(state->legacy_users_path),
storage->path, "users.dat");
if (rc != HYBBX_OK) {
free(state);
return rc;
}
rc = hybbx_path_join(state->sessions_path, sizeof(state->sessions_path),
storage->path, "sessions.dat");
if (rc != HYBBX_OK) {
free(state);
return rc;
}
rc = hybbx_path_join(state->session_next_path, sizeof(state->session_next_path),
storage->path, "session.next");
if (rc != HYBBX_OK) {
free(state);
return rc;
}
rc = hybbx_path_join(state->user_next_path, sizeof(state->user_next_path),
storage->path, "user.next");
if (rc != HYBBX_OK) {
free(state);
return rc;
}
storage->backend_data = state;
rc = migrate_legacy_users_dat(storage);
if (rc != HYBBX_OK) {
hybbx_storage_flatfile_close(storage);
return rc;
}
rc = ensure_default_sysop(storage);
if (rc != HYBBX_OK) {
hybbx_storage_flatfile_close(storage);
return rc;
}
rc = migrate_user_identities(storage);
if (rc != HYBBX_OK) {
hybbx_storage_flatfile_close(storage);
return rc;
}
rc = migrate_plain_passwords(storage);
if (rc != HYBBX_OK) {
hybbx_storage_flatfile_close(storage);
return rc;
}
return HYBBX_OK;
}
void hybbx_storage_flatfile_close(hybbx_storage_t *storage)
{
if (storage == NULL) {
return;
}
free(storage->backend_data);
storage->backend_data = NULL;
}
hybbx_result_t hybbx_storage_flatfile_find_user(hybbx_storage_t *storage,
const char *username,
hybbx_user_record_t *out)
{
struct flatfile_state *state;
find_user_ctx_t ctx;
hybbx_result_t rc;
if (storage == NULL || username == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
memset(out, 0, sizeof(*out));
ctx.username = username;
ctx.out = out;
ctx.found = 0;
rc = foreach_user(state, find_user_cb, &ctx);
if (rc == HYBBX_ERR_BUSY) {
return HYBBX_OK;
}
if (rc != HYBBX_OK) {
return rc;
}
return HYBBX_ERR_NOT_FOUND;
}
hybbx_result_t hybbx_storage_flatfile_resolve_user(hybbx_storage_t *storage,
const char *name,
hybbx_user_record_t *out)
{
char normalized[HYBBX_USER_NAME_MAX];
struct flatfile_state *state;
find_nickname_ctx_t nctx;
hybbx_result_t rc;
if (storage == NULL || name == NULL || out == NULL || name[0] == '\0') {
return HYBBX_ERR_INVALID;
}
hybbx_strlcpy(normalized, name, sizeof(normalized));
hybbx_username_normalize(normalized);
rc = hybbx_storage_flatfile_find_user(storage, normalized, out);
if (rc == HYBBX_OK) {
return HYBBX_OK;
}
if (rc != HYBBX_ERR_NOT_FOUND) {
return rc;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
memset(out, 0, sizeof(*out));
nctx.nickname = name;
nctx.out = out;
nctx.found = 0;
rc = foreach_user(state, find_nickname_cb, &nctx);
if (rc == HYBBX_ERR_BUSY) {
return HYBBX_OK;
}
if (rc != HYBBX_OK) {
return rc;
}
return HYBBX_ERR_NOT_FOUND;
}
hybbx_result_t hybbx_storage_flatfile_count_level(hybbx_storage_t *storage,
hybbx_user_level_t level,
size_t *count)
{
struct flatfile_state *state;
count_level_ctx_t ctx;
hybbx_result_t rc;
if (storage == NULL || count == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
ctx.level = level;
ctx.count = 0;
rc = foreach_user(state, count_level_cb, &ctx);
if (rc != HYBBX_OK) {
return rc;
}
*count = ctx.count;
return HYBBX_OK;
}
hybbx_result_t hybbx_storage_flatfile_foreach_user(hybbx_storage_t *storage,
hybbx_storage_user_fn fn,
void *ctx)
{
struct flatfile_state *state;
if (storage == NULL || fn == NULL) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
return foreach_user(state, fn, ctx);
}
hybbx_result_t hybbx_storage_flatfile_register_user(hybbx_storage_t *storage,
const hybbx_user_registration_t *reg,
hybbx_user_record_t *out)
{
struct flatfile_state *state;
hybbx_user_record_t existing;
uint64_t user_id;
time_t now = time(NULL);
hybbx_result_t rc;
if (storage == NULL || reg == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
if (!hybbx_registration_valid(reg, storage->guest_prefix)) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
rc = hybbx_storage_flatfile_find_user(storage, reg->username, &existing);
if (rc == HYBBX_OK) {
return HYBBX_ERR_BUSY;
}
if (rc != HYBBX_ERR_NOT_FOUND) {
return rc;
}
{
identity_taken_ctx_t taken;
memset(&taken, 0, sizeof(taken));
taken.username = reg->username;
taken.nickname = reg->nickname;
rc = foreach_user(state, identity_taken_cb, &taken);
if (rc == HYBBX_ERR_BUSY || taken.taken) {
return HYBBX_ERR_BUSY;
}
if (rc != HYBBX_OK) {
return rc;
}
}
rc = bump_counter(state->user_next_path, &user_id);
if (rc != HYBBX_OK) {
return rc;
}
memset(out, 0, sizeof(*out));
out->id = user_id;
hybbx_strlcpy(out->username, reg->username, sizeof(out->username));
hybbx_username_normalize(out->username);
hybbx_strlcpy(out->nickname, reg->nickname, sizeof(out->nickname));
out->level = HYBBX_LEVEL_USER;
out->active = 0;
out->created_at = now;
hybbx_strlcpy(out->full_name, reg->full_name, sizeof(out->full_name));
hybbx_strlcpy(out->country, reg->country, sizeof(out->country));
hybbx_strlcpy(out->location, reg->location, sizeof(out->location));
hybbx_strlcpy(out->email, reg->email, sizeof(out->email));
if (reg->password[0] != '\0') {
if (!hybbx_password_plain_valid(reg->password)) {
return HYBBX_ERR_INVALID;
}
rc = hybbx_password_hash(reg->password, out->password,
sizeof(out->password));
if (rc != HYBBX_OK) {
return rc;
}
}
return append_user_record(state, out);
}
hybbx_result_t hybbx_storage_flatfile_update_user(hybbx_storage_t *storage,
const hybbx_user_record_t *user)
{
struct flatfile_state *state;
unsigned shard_index;
size_t slot_index;
char path[HYBBX_PATH_MAX];
user_shard_t shard;
hybbx_result_t rc;
if (storage == NULL || user == NULL || user->id == 0) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
rc = find_user_shard(state, user->id, &shard_index, &slot_index);
if (rc != HYBBX_OK) {
return rc;
}
rc = user_shard_path(state, shard_index, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
rc = load_user_shard(path, &shard);
if (rc != HYBBX_OK) {
return rc;
}
if (slot_index >= shard.count) {
return HYBBX_ERR_NOT_FOUND;
}
shard.users[slot_index] = *user;
hybbx_username_normalize(shard.users[slot_index].username);
return save_user_shard(state, shard_index, &shard);
}
hybbx_result_t hybbx_storage_flatfile_delete_user(hybbx_storage_t *storage,
uint64_t user_id)
{
struct flatfile_state *state;
unsigned shard_index;
size_t slot_index;
char path[HYBBX_PATH_MAX];
user_shard_t shard;
hybbx_result_t rc;
if (storage == NULL || user_id == 0) {
return HYBBX_ERR_INVALID;
}
state = storage->backend_data;
if (state == NULL) {
return HYBBX_ERR_INVALID;
}
rc = find_user_shard(state, user_id, &shard_index, &slot_index);
if (rc != HYBBX_OK) {
return rc;
}
rc = user_shard_path(state, shard_index, path, sizeof(path));
if (rc != HYBBX_OK) {
return rc;
}
rc = load_user_shard(path, &shard);
if (rc != HYBBX_OK) {
return rc;
}
if (slot_index >= shard.count) {
return HYBBX_ERR_NOT_FOUND;
}
remove_nickname_file(state, shard.users[slot_index].username);
if (slot_index + 1 < shard.count) {
memmove(&shard.users[slot_index], &shard.users[slot_index + 1],
(shard.count - slot_index - 1) * sizeof(shard.users[0]));
}
shard.count--;
return save_user_shard(state, shard_index, &shard);
}
hybbx_result_t hybbx_storage_flatfile_session_begin(hybbx_storage_t *storage,
const hybbx_user_record_t *user,
const char *transport,
hybbx_session_record_t *out)
{
struct flatfile_state *state = storage->backend_data;
char line[384];
time_t now = time(NULL);
hybbx_result_t rc;
if (state == NULL || user == NULL || transport == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
memset(out, 0, sizeof(*out));
rc = bump_counter(state->session_next_path, &out->session_id);
if (rc != HYBBX_OK) {
return rc;
}
out->user_id = user->id;
hybbx_strlcpy(out->username, user->username, sizeof(out->username));
hybbx_strlcpy(out->transport, transport, sizeof(out->transport));
out->connected_at = now;
out->active = 1;
snprintf(line, sizeof(line), "%llu|%llu|%s|%s|%lld|0|1",
(unsigned long long)out->session_id,
(unsigned long long)out->user_id,
out->username, out->transport, (long long)out->connected_at);
return append_line(state->sessions_path, line);
}
hybbx_result_t hybbx_storage_flatfile_session_end(hybbx_storage_t *storage,
uint64_t session_id)
{
struct flatfile_state *state = storage->backend_data;
char line[384];
time_t now = time(NULL);
if (state == NULL || session_id == 0) {
return HYBBX_ERR_INVALID;
}
snprintf(line, sizeof(line), "%llu|0|_|_|0|%lld|0",
(unsigned long long)session_id, (long long)now);
return append_line(state->sessions_path, line);
}
|