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
|
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <libwebsockets.h>
#include <private-lib-core.h>
#include <sqlite3.h>
/*
* we get one of these per matching result from the query
*/
static int
lws_struct_sq3_deser_cb(void *priv, int cols, char **cv, char **cn)
{
lws_struct_args_t *a = (lws_struct_args_t *)priv;
char *u = lwsac_use_zero(&a->ac, a->dest_len, a->ac_block_size);
lws_dll2_owner_t *o = (lws_dll2_owner_t *)a->cb_arg;
const lws_struct_map_t *map = a->map_st[0];
int n, mems = (int)(ssize_t)a->map_entries_st[0];
long long li;
size_t lim;
char **pp;
char *s;
if (!u) {
lwsl_err("OOM\n");
return 1;
}
lws_dll2_add_tail((lws_dll2_t *)((char *)u + a->toplevel_dll2_ofs), o);
while (mems--) {
for (n = 0; n < cols; n++) {
if (!cv[n] || strcmp(cn[n], map->colname))
continue;
switch (map->type) {
case LSMT_SIGNED:
if (map->aux == sizeof(signed char)) {
signed char *pc;
pc = (signed char *)(u + map->ofs);
*pc = (signed char)atoi(cv[n]);
break;
}
if (map->aux == sizeof(short)) {
short *ps;
ps = (short *)(u + map->ofs);
*ps = (short)atoi(cv[n]);
break;
}
if (map->aux == sizeof(int)) {
int *pi;
pi = (int *)(u + map->ofs);
*pi = (int)atoll(cv[n]); /* 32-bit OS */
break;
}
if (map->aux == sizeof(long)) {
long *pl;
pl = (long *)(u + map->ofs);
*pl = (long)atoll(cv[n]); /* 32-bit OS */
break;
}
{
long long *pll;
pll = (long long *)(u + map->ofs);
*pll = atoll(cv[n]);
}
break;
case LSMT_UNSIGNED:
if (map->aux == sizeof(unsigned char)) {
unsigned char *pc;
pc = (unsigned char *)(u + map->ofs);
*pc = (unsigned char)(unsigned int)atoi(cv[n]);
break;
}
if (map->aux == sizeof(unsigned short)) {
unsigned short *ps;
ps = (unsigned short *)(u + map->ofs);
*ps = (unsigned short)atoi(cv[n]);
break;
}
if (map->aux == sizeof(unsigned int)) {
unsigned int *pi;
pi = (unsigned int *)(u + map->ofs);
*pi = (unsigned int)atoi(cv[n]);
break;
}
if (map->aux == sizeof(unsigned long)) {
unsigned long *pl;
pl = (unsigned long *)(u + map->ofs);
*pl = (unsigned long)atol(cv[n]);
break;
}
{
unsigned long long *pll;
pll = (unsigned long long *)(u + map->ofs);
*pll = (unsigned long long)atoll(cv[n]);
}
break;
case LSMT_BOOLEAN:
li = 0;
if (!strcmp(cv[n], "true") ||
!strcmp(cv[n], "TRUE") || cv[n][0] == '1')
li = 1;
if (map->aux == sizeof(char)) {
char *pc;
pc = (char *)(u + map->ofs);
*pc = (char)li;
break;
}
if (map->aux == sizeof(int)) {
int *pi;
pi = (int *)(u + map->ofs);
*pi = (int)li;
} else {
uint64_t *p64;
p64 = (uint64_t *)(u + map->ofs);
*p64 = (uint64_t)li;
}
break;
case LSMT_STRING_CHAR_ARRAY:
s = (char *)(u + map->ofs);
lim = map->aux;
lws_strncpy(s, cv[n], lim);
break;
case LSMT_STRING_PTR:
pp = (char **)(u + map->ofs);
lim = strlen(cv[n]);
s = lwsac_use(&a->ac, lim + 1, a->ac_block_size);
if (!s)
return 1;
*pp = s;
memcpy(s, cv[n], lim);
s[lim] = '\0';
break;
default:
break;
}
}
map++;
}
return 0;
}
/*
* Call this with an LSM_SCHEMA map, its colname is the table name and its
* type information describes the toplevel type. Schema is dereferenced and
* put in args before the actual sq3 query, which is given the child map.
*/
int
lws_struct_sq3_deserialize(sqlite3 *pdb, const char *filter, const char *order,
const lws_struct_map_t *schema, lws_dll2_owner_t *o,
struct lwsac **ac, int start, int _limit)
{
int limit = _limit < 0 ? -_limit : _limit;
char s[768], results[512], where[250];
lws_struct_args_t a;
int n, m;
if (!order)
order = "_lws_idx";
memset(&a, 0, sizeof(a));
a.ac = *ac;
a.cb_arg = o; /* lws_dll2_owner tracking query result objects */
a.map_st[0] = schema->child_map;
a.map_entries_st[0] = schema->child_map_size;
a.dest_len = schema->aux; /* size of toplevel object to allocate */
a.toplevel_dll2_ofs = schema->ofs;
lws_dll2_owner_clear(o);
/*
* Explicitly list the columns instead of use *, so we can skip blobs
*/
m = 0;
for (n = 0; n < (int)schema->child_map_size; n++)
m += lws_snprintf(&results[m], sizeof(results) - (unsigned int)n - 1,
"%s%c", schema->child_map[n].colname,
n + 1 == (int)schema->child_map_size ? ' ' : ',');
where[0] = '\0';
lws_snprintf(where, sizeof(where), " where _lws_idx >= %llu %s",
(unsigned long long)start, filter ? filter : "");
lws_snprintf(s, sizeof(s) - 1, "select %s "
"from %s %s order by %s %slimit %d;", results,
schema->colname, where, order,
_limit < 0 ? "desc " : "", limit);
if (sqlite3_exec(pdb, s, lws_struct_sq3_deser_cb, &a, NULL) != SQLITE_OK) {
lwsl_err("%s: %s: fail %s\n", __func__, sqlite3_errmsg(pdb), s);
lwsac_free(&a.ac);
return -1;
}
*ac = a.ac;
return 0;
}
/*
* This takes a struct and turns it into an sqlite3 UPDATE, using the given
* schema... which has one LSM_SCHEMA_DLL2 entry wrapping the actual schema
*/
static int
_lws_struct_sq3_ser_one(sqlite3 *pdb, const lws_struct_map_t *schema,
uint32_t idx, void *st)
{
const lws_struct_map_t *map = schema->child_map;
int n, m, pk = 0, nentries = (int)(ssize_t)schema->child_map_size, nef = 0, did;
size_t sql_est = 46 + strlen(schema->colname) + 1;
/* "insert into (_lws_idx, ) values (00000001,);" ...
* plus the table name */
uint8_t *stb = (uint8_t *)st;
const char *p;
char *sql;
/*
* Figure out effective number of columns, exluding BLOB.
*
* The first UNSIGNED is a hidden index. Blobs are not handled by
* lws_struct except to create the column in the schema.
*/
pk = 0;
nef = 0;
for (n = 0; n < nentries; n++) {
if (!pk && map[n].type == LSMT_UNSIGNED) {
pk = 1;
continue;
}
if (map[n].type == LSMT_BLOB_PTR)
continue;
nef++;
}
/*
* Figure out an estimate for the length of the populated sqlite
* command, and then malloc it up
*/
for (n = 0; n < nentries; n++) {
sql_est += strlen(map[n].colname) + 2;
switch (map[n].type) {
case LSMT_SIGNED:
case LSMT_UNSIGNED:
case LSMT_BOOLEAN:
switch (map[n].aux) {
case 1:
sql_est += 3 + 2;
break;
case 2:
sql_est += 5 + 2;
break;
case 4:
sql_est += 10 + 2;
break;
case 8:
sql_est += 20 + 2;
break;
}
if (map[n].type == LSMT_SIGNED)
sql_est++; /* minus sign */
break;
case LSMT_STRING_CHAR_ARRAY:
sql_est += (unsigned int)lws_sql_purify_len((const char *)st +
map[n].ofs) + 2;
break;
case LSMT_STRING_PTR:
p = *((const char * const *)&stb[map[n].ofs]);
sql_est += (unsigned int)((p ? lws_sql_purify_len(p) : 0) + 2);
break;
case LSMT_BLOB_PTR:
/* we don't deal with blobs actually */
sql_est -= strlen(map[n].colname) + 2;
break;
default:
lwsl_err("%s: unsupported type\n", __func__);
assert(0);
break;
}
}
sql = malloc(sql_est);
if (!sql)
return -1;
m = lws_snprintf(sql, sql_est, "insert into %s(_lws_idx, ",
schema->colname);
/*
* First explicit integer type is primary key autoincrement, should
* not be specified
*/
pk = 0;
did = 0;
for (n = 0; n < nentries; n++) {
if (!pk && map[n].type == LSMT_UNSIGNED) {
pk = 1;
continue;
}
if (map[n].type == LSMT_BLOB_PTR)
continue;
did++;
m += lws_snprintf(sql + m, sql_est - (unsigned int)m,
did == nef ? "%s" : "%s, ",
map[n].colname);
}
m += lws_snprintf(sql + m, sql_est - (unsigned int)m, ") values(%u, ", idx);
pk = 0;
did = 0;
for (n = 0; n < nentries; n++) {
uint64_t uu64;
size_t q;
if (!pk && map[n].type == LSMT_UNSIGNED) {
pk = 1;
continue;
}
switch (map[n].type) {
case LSMT_SIGNED:
case LSMT_UNSIGNED:
case LSMT_BOOLEAN:
uu64 = 0;
for (q = 0; q < map[n].aux; q++)
uu64 |= ((uint64_t)stb[map[n].ofs + q] <<
(q << 3));
if (map[n].type == LSMT_SIGNED)
m += lws_snprintf(sql + m, sql_est - (unsigned int)m, "%lld",
(long long)(int64_t)uu64);
else
m += lws_snprintf(sql + m, sql_est - (unsigned int)m, "%llu",
(unsigned long long)uu64);
break;
case LSMT_STRING_CHAR_ARRAY:
sql[m++] = '\'';
lws_sql_purify(sql + m, (const char *)&stb[map[n].ofs],
sql_est - (size_t)(ssize_t)m - 4);
m += (int)(ssize_t)strlen(sql + m);
sql[m++] = '\'';
break;
case LSMT_STRING_PTR:
p = *((const char * const *)&stb[map[n].ofs]);
sql[m++] = '\'';
if (p) {
lws_sql_purify(sql + m, p, sql_est - (unsigned int)m - 4);
m += (int)(ssize_t)strlen(sql + m);
}
sql[m++] = '\'';
break;
case LSMT_BLOB_PTR:
continue;
default:
lwsl_err("%s: unsupported type\n", __func__);
assert(0);
break;
}
did++;
if (did != nef) {
if (sql_est - (unsigned int)m < 6)
return -1;
sql[m++] = ',';
sql[m++] = ' ';
}
}
lws_snprintf(sql + m, sql_est - (unsigned int)m, ");");
n = sqlite3_exec(pdb, sql, NULL, NULL, NULL);
if (n != SQLITE_OK) {
lwsl_err("%s\n", sql);
free(sql);
lwsl_err("%s: %s: fail\n", __func__, sqlite3_errmsg(pdb));
return -1;
}
free(sql);
return 0;
}
int
lws_struct_sq3_serialize(sqlite3 *pdb, const lws_struct_map_t *schema,
lws_dll2_owner_t *owner, uint32_t manual_idx)
{
uint32_t idx = manual_idx;
lws_start_foreach_dll(struct lws_dll2 *, p, owner->head) {
void *item = (void *)((uint8_t *)p - schema->ofs_clist);
if (_lws_struct_sq3_ser_one(pdb, schema, idx++, item))
return 1;
} lws_end_foreach_dll(p);
return 0;
}
int
lws_struct_sq3_create_table(sqlite3 *pdb, const lws_struct_map_t *schema)
{
const lws_struct_map_t *map = schema->child_map;
int map_size = (int)(ssize_t)schema->child_map_size, subsequent = 0;
char s[2048], *p = s, *end = &s[sizeof(s) - 1],
*pri = " primary key autoincrement", *use;
p += lws_snprintf(p, (unsigned int)lws_ptr_diff(end, p),
"create table if not exists %s (_lws_idx integer, ",
schema->colname);
while (map_size--) {
if (map->type > LSMT_STRING_PTR && map->type != LSMT_BLOB_PTR) {
map++;
continue;
}
if (subsequent && (end - p) > 4) {
*p++ = ',';
*p++ = ' ';
}
subsequent = 1;
if (map->type == LSMT_BLOB_PTR) {
p += lws_snprintf(p, (unsigned int)lws_ptr_diff(end, p), "%s blob", map->colname);
} else {
if (map->type < LSMT_STRING_CHAR_ARRAY) {
use = "";
if (map->colname[0] != '_') /* _lws_idx is not primary key */
use = pri;
p += lws_snprintf(p, (unsigned int)lws_ptr_diff(end, p), "%s integer%s",
map->colname, use);
if (map->colname[0] != '_')
pri = "";
} else
p += lws_snprintf(p, (unsigned int)lws_ptr_diff(end, p), "%s varchar",
map->colname);
}
map++;
}
p += lws_snprintf(p, (unsigned int)lws_ptr_diff(end, p), ");");
if (sqlite3_exec(pdb, s, NULL, NULL, NULL) != SQLITE_OK) {
lwsl_err("%s: %s: fail\n", __func__, sqlite3_errmsg(pdb));
return -1;
}
return 0;
}
int
lws_struct_sq3_open(struct lws_context *context, const char *sqlite3_path,
char create_if_missing, sqlite3 **pdb)
{
#if !defined(WIN32)
uid_t uid = 0;
gid_t gid = 0;
#endif
if (sqlite3_open_v2(sqlite3_path, pdb,
SQLITE_OPEN_READWRITE |
(create_if_missing ? SQLITE_OPEN_CREATE : 0),
NULL) != SQLITE_OK) {
lwsl_info("%s: Unable to open db %s: %s\n",
__func__, sqlite3_path, sqlite3_errmsg(*pdb));
return 1;
}
#if !defined(WIN32)
lws_get_effective_uid_gid(context, &uid, &gid);
if (uid)
if (chown(sqlite3_path, uid, gid))
lwsl_err("%s: failed to chown %s\n", __func__, sqlite3_path);
chmod(sqlite3_path, 0600);
lwsl_debug("%s: created %s owned by %u:%u mode 0600\n", __func__,
sqlite3_path, (unsigned int)uid, (unsigned int)gid);
#else
lwsl_debug("%s: created %s\n", __func__, sqlite3_path);
#endif
sqlite3_extended_result_codes(*pdb, 1);
return 0;
}
int
lws_struct_sq3_close(sqlite3 **pdb)
{
int n;
if (!*pdb)
return 0;
n = sqlite3_close(*pdb);
if (n != SQLITE_OK) {
/*
* trouble...
*/
lwsl_err("%s: failed to close: %d\n", __func__, n);
return 1;
}
*pdb = NULL;
return 0;
}
|