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
|
#include "hybbx/storage.h"
#include "hybbx/auth.h"
#include "hybbx/config.h"
#include "hybbx/limits.h"
#include "hybbx/util.h"
#include "storage_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HYBBX_SQL_DEFAULT_USER_DB "users.db"
#define HYBBX_SQL_DEFAULT_MAIL_DB "mail.db"
static char *hybbx_strdup(const char *s)
{
size_t len;
char *copy;
if (s == NULL) {
return NULL;
}
len = strlen(s) + 1;
copy = malloc(len);
if (copy != NULL) {
memcpy(copy, s, len);
}
return copy;
}
void hybbx_storage_sql_config_defaults(hybbx_storage_sql_config_t *cfg)
{
if (cfg == NULL) {
return;
}
cfg->user_db[0] = '\0';
cfg->mail_db[0] = '\0';
cfg->backup_interval_sec = HYBBX_STORAGE_BACKUP_INTERVAL_DEFAULT_SEC;
cfg->backup_path[0] = '\0';
}
void hybbx_storage_sql_config_apply(hybbx_storage_sql_config_t *cfg,
const hybbx_config_t *config,
const char *storage_path)
{
const char *user_db;
const char *mail_db;
const char *backup_path;
hybbx_storage_sql_config_defaults(cfg);
if (storage_path == NULL || storage_path[0] == '\0') {
return;
}
user_db = HYBBX_SQL_DEFAULT_USER_DB;
mail_db = HYBBX_SQL_DEFAULT_MAIL_DB;
backup_path = "";
if (config != NULL) {
user_db = hybbx_config_get(config, "storage", "user_db",
HYBBX_SQL_DEFAULT_USER_DB);
mail_db = hybbx_config_get(config, "storage", "mail_db",
HYBBX_SQL_DEFAULT_MAIL_DB);
backup_path = hybbx_config_get(config, "storage", "backup_path", "");
cfg->backup_interval_sec = hybbx_config_get_uint(
config, "storage", "backup_interval",
HYBBX_STORAGE_BACKUP_INTERVAL_DEFAULT_SEC, 60u, 86400u);
}
(void)hybbx_path_join(cfg->user_db, sizeof(cfg->user_db), storage_path,
user_db);
(void)hybbx_path_join(cfg->mail_db, sizeof(cfg->mail_db), storage_path,
mail_db);
if (backup_path != NULL && backup_path[0] != '\0') {
char resolved[HYBBX_PATH_MAX];
if (hybbx_path_resolve(resolved, sizeof(resolved), backup_path) ==
HYBBX_OK) {
hybbx_strlcpy(cfg->backup_path, resolved, sizeof(cfg->backup_path));
} else {
(void)hybbx_path_join(cfg->backup_path, sizeof(cfg->backup_path),
storage_path, backup_path);
}
}
}
const hybbx_storage_sql_config_t *hybbx_storage_sql_config(
const hybbx_storage_t *storage)
{
if (storage == NULL) {
return NULL;
}
return &storage->sql_cfg;
}
void hybbx_storage_backup_tick(hybbx_storage_t *storage)
{
hybbx_storage_sql_backup_tick(storage);
}
static void storage_dispatch_sqlite(hybbx_storage_t *storage,
const hybbx_storage_sql_config_t *sql_cfg)
{
hybbx_storage_sql_config_defaults(&storage->sql_cfg);
if (sql_cfg != NULL) {
storage->sql_cfg = *sql_cfg;
} else if (storage->path != NULL) {
char user_db[HYBBX_PATH_MAX];
char mail_db[HYBBX_PATH_MAX];
(void)hybbx_path_join(user_db, sizeof(user_db), storage->path,
HYBBX_SQL_DEFAULT_USER_DB);
(void)hybbx_path_join(mail_db, sizeof(mail_db), storage->path,
HYBBX_SQL_DEFAULT_MAIL_DB);
hybbx_strlcpy(storage->sql_cfg.user_db, user_db,
sizeof(storage->sql_cfg.user_db));
hybbx_strlcpy(storage->sql_cfg.mail_db, mail_db,
sizeof(storage->sql_cfg.mail_db));
}
}
hybbx_storage_t *hybbx_storage_open(const hybbx_storage_options_t *options)
{
hybbx_storage_t *storage;
hybbx_result_t rc;
if (options == NULL || options->path == NULL) {
return NULL;
}
storage = calloc(1, sizeof(*storage));
if (storage == NULL) {
return NULL;
}
storage->backend = options->backend;
storage->path = hybbx_strdup(options->path);
if (storage->path == NULL) {
free(storage);
return NULL;
}
if (options->guest_prefix != NULL && options->guest_prefix[0] != '\0') {
hybbx_strlcpy(storage->guest_prefix, options->guest_prefix,
sizeof(storage->guest_prefix));
} else {
hybbx_strlcpy(storage->guest_prefix, HYBBX_AUTH_DEFAULT_GUEST_PREFIX,
sizeof(storage->guest_prefix));
}
if (storage->backend == HYBBX_STORAGE_SQLITE) {
storage_dispatch_sqlite(storage, options->sql_cfg);
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
rc = hybbx_storage_flatfile_open(storage);
break;
case HYBBX_STORAGE_SQLITE:
case HYBBX_STORAGE_MYSQL:
case HYBBX_STORAGE_MARIADB:
rc = hybbx_storage_sql_open(storage);
break;
default:
rc = HYBBX_ERR_UNSUPPORTED;
break;
}
if (rc != HYBBX_OK) {
hybbx_storage_close(storage);
return NULL;
}
return storage;
}
void hybbx_storage_close(hybbx_storage_t *storage)
{
if (storage == NULL) {
return;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
hybbx_storage_flatfile_close(storage);
break;
case HYBBX_STORAGE_SQLITE:
case HYBBX_STORAGE_MYSQL:
case HYBBX_STORAGE_MARIADB:
hybbx_storage_sql_close(storage);
break;
default:
break;
}
free(storage->path);
free(storage);
}
hybbx_storage_backend_kind_t hybbx_storage_backend(const hybbx_storage_t *storage)
{
if (storage == NULL) {
return HYBBX_STORAGE_FLATFILE;
}
return storage->backend;
}
const char *hybbx_storage_root_path(const hybbx_storage_t *storage)
{
if (storage == NULL || storage->path == NULL || storage->path[0] == '\0') {
return NULL;
}
return storage->path;
}
hybbx_result_t hybbx_storage_register_user(hybbx_storage_t *storage,
const hybbx_user_registration_t *reg,
hybbx_user_record_t *out)
{
if (storage == NULL || reg == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_register_user(storage, reg, out);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_register_user(storage, reg, out);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_find_user(hybbx_storage_t *storage,
const char *username,
hybbx_user_record_t *out)
{
if (storage == NULL || username == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_find_user(storage, username, out);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_find_user(storage, username, out);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_resolve_user(hybbx_storage_t *storage,
const char *name,
hybbx_user_record_t *out)
{
if (storage == NULL || name == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_resolve_user(storage, name, out);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_resolve_user(storage, name, out);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_count_level(hybbx_storage_t *storage,
hybbx_user_level_t level,
size_t *count)
{
if (storage == NULL || count == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_count_level(storage, level, count);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_count_level(storage, level, count);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_foreach_user(hybbx_storage_t *storage,
hybbx_storage_user_fn fn,
void *ctx)
{
if (storage == NULL || fn == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_foreach_user(storage, fn, ctx);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_foreach_user(storage, fn, ctx);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_session_begin(hybbx_storage_t *storage,
const hybbx_user_record_t *user,
const char *transport,
hybbx_session_record_t *out)
{
if (storage == NULL || user == NULL || transport == NULL || out == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_session_begin(storage, user, transport, out);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_session_begin(storage, user, transport, out);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_session_end(hybbx_storage_t *storage,
uint64_t session_id)
{
if (storage == NULL || session_id == 0) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_session_end(storage, session_id);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_session_end(storage, session_id);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_update_user(hybbx_storage_t *storage,
const hybbx_user_record_t *user)
{
if (storage == NULL || user == NULL) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_update_user(storage, user);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_update_user(storage, user);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
hybbx_result_t hybbx_storage_delete_user(hybbx_storage_t *storage,
uint64_t user_id)
{
if (storage == NULL || user_id == 0) {
return HYBBX_ERR_INVALID;
}
switch (storage->backend) {
case HYBBX_STORAGE_FLATFILE:
return hybbx_storage_flatfile_delete_user(storage, user_id);
case HYBBX_STORAGE_SQLITE:
return hybbx_storage_sql_delete_user(storage, user_id);
default:
return HYBBX_ERR_UNSUPPORTED;
}
}
|