/* SPDX-License-Identifier: GPL-2.0 */ /** \file blob.c Macros and functions for blob handling. */ #include #include #include #include "server_cmd.lsg.h" #include "para.h" #include "error.h" #include "crypt.h" #include "string.h" #include "afh.h" #include "afs.h" #include "ipc.h" #include "portable_io.h" #include "sideband.h" #include "command.h" #include "fd.h" /** * Compare two osl objects pointing to unsigned integers of 32 bit size. * * \param obj1 Pointer to the first integer. * \param obj2 Pointer to the second integer. * * \return The values required for an osl compare function. */ static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2) { uint32_t d1 = read_u32(obj1->data); uint32_t d2 = read_u32(obj2->data); if (d1 < d2) return 1; if (d1 > d2) return -1; return 0; } static struct osl_column_description blob_cols[] = { [BLOBCOL_ID] = { .storage_type = OSL_MAPPED_STORAGE, .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE, .name = "id", .data_size = 4, .compare_function = uint32_compare }, [BLOBCOL_NAME] = { .storage_type = OSL_MAPPED_STORAGE, .storage_flags = OSL_RBTREE | OSL_UNIQUE, .name = "name", .compare_function = string_compare }, [BLOBCOL_DEF] = { .storage_type = OSL_DISK_STORAGE, .storage_flags = 0, .name = "definition" } }; /** Define an osl table description for a blob table. */ #define DEFINE_BLOB_TABLE_DESC(table_name) \ static struct osl_table_description table_name ## _table_desc = { \ .name = #table_name, \ .num_columns = NUM_BLOB_COLUMNS, \ .flags = OSL_LARGE_TABLE, \ .column_descriptions = blob_cols \ }; /** Define a pointer to an osl blob table with a canonical name. */ #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table; /** Define a blob table. */ #define INIT_BLOB_TABLE(table_name) \ DEFINE_BLOB_TABLE_DESC(table_name); \ DEFINE_BLOB_TABLE_PTR(table_name); /* doxygen isn't smart enough to recognize these */ /** \cond blob_table */ INIT_BLOB_TABLE(lyrics); INIT_BLOB_TABLE(images); INIT_BLOB_TABLE(moods); INIT_BLOB_TABLE(playlists); /** \endcond blob_table */ static int print_blob(struct osl_table *table, struct osl_row *row, const char *name, void *data) { struct afs_callback_arg *aca = data; bool l_given = SERVER_CMD_OPT_GIVEN(LSMOOD, LONG, aca->lpr); struct osl_object obj; uint32_t id; int ret; if (!l_given) { para_printf(&aca->pbout, "%s\n", name); return 0; } ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj)); if (ret < 0) { afs_error(aca, "cannot list %s\n", name); return ret; } id = read_u32(obj.data); para_printf(&aca->pbout, "%u\t%s\n", id, name); return 1; } static int com_lsblob_callback(const struct lls_command * const cmd, struct osl_table *table, struct afs_callback_arg *aca) { bool i_given, r_given; struct pattern_match_data pmd = { .table = table, .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME, .match_col_num = BLOBCOL_NAME, .data = aca, .action = print_blob, }; int ret; ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr); pmd.lpr = aca->lpr; assert(ret >= 0); i_given = SERVER_CMD_OPT_GIVEN(LSMOOD, ID_SORT, aca->lpr); r_given = SERVER_CMD_OPT_GIVEN(LSMOOD, REVERSE, aca->lpr); if (r_given) pmd.pm_flags |= PM_REVERSE_LOOP; if (i_given) pmd.loop_col_num = BLOBCOL_ID; else pmd.loop_col_num = BLOBCOL_NAME; ret = for_each_matching_row(&pmd); if (ret < 0) goto out; if (pmd.num_matches == 0 && lls_num_inputs(aca->lpr) > 0) ret = -E_NO_MATCH; out: lls_free_parse_result(aca->lpr, cmd); return ret; } static int com_lsblob(afs_callback *f, const struct lls_command * const cmd, struct command_context *cc, struct lls_parse_result *lpr) { return send_lls_callback_request(f, cc->afs_fd, cmd, lpr, cc); } struct catblob_data { struct afs_callback_arg *aca; int shmid; void *area; size_t bytes_used; /* >0 iff area is allocated and attached */ }; static int cat_blob(struct osl_table *table, struct osl_row *row, const char *name, void *data) { int ret = 0, ret2; struct osl_object obj; struct catblob_data *cbd = data; size_t bytes_left, shmmax = shm_get_shmmax(); ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj)); if (ret < 0) return (ret == osl(-E_OSL_EMPTY))? 0 : ret; assert(obj.size > 0); bytes_left = obj.size; again: if (cbd->bytes_used == 0) { /* no shm area allocated yet */ ret = shm_new(shmmax); if (ret < 0) goto close_object; cbd->shmid = ret; ret = shm_attach(cbd->shmid, ATTACH_RW, &cbd->area); if (ret < 0) goto destroy_area; cbd->bytes_used = sizeof(struct callback_result); } if (cbd->bytes_used < shmmax) { size_t copy = PARA_MIN(shmmax - cbd->bytes_used, bytes_left); void *src = obj.data + (obj.size - bytes_left); memcpy(cbd->area + cbd->bytes_used, src, copy); cbd->bytes_used += copy; bytes_left -= copy; } if (cbd->bytes_used == shmmax) { /* notify command handler */ struct callback_result *cr = cbd->area; cr->result_size = cbd->bytes_used - sizeof(struct callback_result); cr->band = SBD_OUTPUT; assert(shm_detach(cbd->area) >= 0); PARA_INFO_LOG("passing size %zu shmid\n", cbd->bytes_used); ret = write_all(cbd->aca->fd, (char *)&cbd->shmid, sizeof(int)); if (ret < 0) goto destroy_area; cbd->bytes_used = 0; if (bytes_left > 0) goto again; } ret = 0; goto close_object; destroy_area: shm_destroy(cbd->shmid); close_object: ret2 = osl(osl_close_disk_object(&obj)); ret = (ret < 0)? ret : ret2; if (ret < 0) afs_error(cbd->aca, "%s: %s\n", name, para_strerror(-ret)); return ret; } static int com_catblob_callback(const struct lls_command * const cmd, struct osl_table *table, struct afs_callback_arg *aca) { int ret; struct catblob_data cbd = {.aca = aca}; struct pattern_match_data pmd = { .table = table, .loop_col_num = BLOBCOL_NAME, .match_col_num = BLOBCOL_NAME, .pm_flags = PM_SKIP_EMPTY_NAME, .data = &cbd, .action = cat_blob }; ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr); assert(ret >= 0); pmd.lpr = aca->lpr; ret = for_each_matching_row(&pmd); if (ret < 0) goto out; if (cbd.bytes_used > 0) { /* write last part of the result */ struct callback_result *cr = cbd.area; assert(cbd.bytes_used > sizeof(struct callback_result)); cr->result_size = cbd.bytes_used - sizeof(struct callback_result); cr->band = SBD_OUTPUT; PARA_INFO_LOG("passing size %zu shmid\n", cbd.bytes_used); ret = write_all(aca->fd, (char *)&cbd.shmid, sizeof(int)); assert(shm_detach(cbd.area) >= 0); if (ret < 0) shm_destroy(cbd.shmid); } if (pmd.num_matches == 0) ret = -E_NO_MATCH; out: lls_free_parse_result(aca->lpr, cmd); return ret; } static int com_catblob(afs_callback *f, const struct lls_command * const cmd, struct command_context *cc, struct lls_parse_result *lpr) { char *errctx; int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx)); if (ret < 0) { send_errctx(cc, errctx); return ret; } return send_lls_callback_request(f, cc->afs_fd, cmd, lpr, cc); } static int remove_blob(struct osl_table *table, struct osl_row *row, const char *name, void *data) { struct afs_callback_arg *aca = data; int ret = osl(osl_del_row(table, row)); if (ret < 0) { afs_error(aca, "cannot remove %s\n", name); return ret; } return 1; } static int com_rmblob_callback(const struct lls_command * const cmd, struct osl_table *table, struct afs_callback_arg *aca) { int ret; struct pattern_match_data pmd = { .table = table, .loop_col_num = BLOBCOL_NAME, .match_col_num = BLOBCOL_NAME, .pm_flags = PM_SKIP_EMPTY_NAME, .data = aca, .action = remove_blob }; ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr); assert(ret >= 0); pmd.lpr = aca->lpr; ret = for_each_matching_row(&pmd); if (ret < 0) goto out; if (pmd.num_matches == 0) ret = -E_NO_MATCH; else { para_printf(&aca->pbout, "removed %u blob(s)\n", pmd.num_matches); ret = afs_event(BLOB_REMOVE, table); } out: lls_free_parse_result(aca->lpr, cmd); return ret; } static int com_rmblob(afs_callback *f, const struct lls_command * const cmd, struct command_context *cc, struct lls_parse_result *lpr) { char *errctx; int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx)); if (ret < 0) { send_errctx(cc, errctx); return ret; } return send_lls_callback_request(f, cc->afs_fd, cmd, lpr, cc); } static int com_addblob_callback(__a_unused const struct lls_command * const cmd, struct osl_table *table, struct afs_callback_arg *aca) { struct osl_object objs[NUM_BLOB_COLUMNS]; char *name = aca->query.data; size_t name_len = strlen(name) + 1; uint32_t id = (uint32_t)-1; /* STFU, gcc */ char id_buf[sizeof(id)]; unsigned num_rows; int ret; ret = osl(osl_get_num_rows(table, &num_rows)); if (ret < 0) goto out; if (!num_rows) { /* this is the first entry ever added */ /* * Insert dummy row containing the next free ID. Since we are * about to insert the first blob with ID 1, the next free ID * will be 2. */ id = 2U; write_u32(id_buf, id); objs[BLOBCOL_ID].data = id_buf; objs[BLOBCOL_ID].size = sizeof(id_buf); objs[BLOBCOL_NAME].data = ""; objs[BLOBCOL_NAME].size = 1; objs[BLOBCOL_DEF].data = ""; objs[BLOBCOL_DEF].size = 1; ret = osl(osl_add_row(table, objs)); if (ret < 0) goto out; } else { /* check if name already exists */ struct osl_row *row; struct osl_object obj = {.data = name, .size = name_len}; ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row)); if (ret < 0 && ret != osl(-E_OSL_RB_KEY_NOT_FOUND)) goto out; if (ret >= 0) { /* we already have a blob with this name */ ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj)); if (ret < 0) goto out; id = read_u32(obj.data); obj.data = name + name_len; obj.size = aca->query.size - name_len; ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj)); goto out; } /* new blob, get id of the dummy row and increment it */ obj.data = ""; obj.size = 1; ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row)); if (ret < 0) goto out; ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj)); if (ret < 0) goto out; id = read_u32(obj.data) + 1; write_u32(id_buf, id); obj.data = &id_buf; ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj)); if (ret < 0) goto out; } id--; write_u32(id_buf, id); objs[BLOBCOL_ID].data = &id_buf; objs[BLOBCOL_ID].size = sizeof(id_buf); objs[BLOBCOL_NAME].data = name; objs[BLOBCOL_NAME].size = name_len; objs[BLOBCOL_DEF].data = name + name_len; objs[BLOBCOL_DEF].size = aca->query.size - name_len; ret = osl(osl_add_row(table, objs)); if (ret < 0) goto out; ret = afs_event(BLOB_ADD, table); out: if (ret < 0) afs_error(aca, "cannot add %s\n", name); else para_printf(&aca->pbout, "added %s as id %u\n", name, id); return ret; } /* Write input from fd to dynamically allocated buffer, but maximal 10M. */ static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj) { size_t max_size = 10 * 1024 * 1024; int ret; struct iovec iov; obj->data = NULL; obj->size = 0; again: do { ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov); } while (ret == 0); if (ret < 0) { free(obj->data); obj->data = NULL; obj->size = 0; return ret; } if (iov.iov_len == 0) /* end of blob */ return 1; if (!obj->data) { obj->data = iov.iov_base; obj->size = iov.iov_len; } else { obj->data = para_realloc(obj->data, obj->size + iov.iov_len); memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len); obj->size += iov.iov_len; free(iov.iov_base); max_size -= iov.iov_len; } goto again; return 1; } /* * Read blob from a file descriptor and send it to afs. * * This function is called from the addblob command handlers to instruct the * afs process to store the input in a blob table. Input is read and decrypted * from the file descriptor given by cc and appended to a buffer which also contains * the name of the blob to create. The combined buffer is made available to the * afs process via the callback method. */ static int stdin_command(struct command_context *cc, struct lls_parse_result *lpr, afs_callback *f) { struct osl_object query, stdin_obj; int ret; size_t len = strlen(lls_input(0, lpr)); ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false); if (ret < 0) return ret; ret = fd2buf(&cc->scc, &stdin_obj); if (ret < 0) return ret; query.size = len + 1 + stdin_obj.size; query.data = alloc(query.size); memcpy(query.data, lls_input(0, lpr), len + 1); if (stdin_obj.size > 0) memcpy((char *)query.data + len + 1, stdin_obj.data, stdin_obj.size); free(stdin_obj.data); ret = send_callback_request(f, cc->afs_fd, &query, afs_cb_result_handler, cc); free(query.data); return ret; } static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd, struct command_context *cc, struct lls_parse_result *lpr) { char *errctx; int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx)); if (ret < 0) { send_errctx(cc, errctx); return ret; } if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */ return -E_BLOB_SYNTAX; return stdin_command(cc, lpr, f); } static int com_mvblob_callback(const struct lls_command * const cmd, struct osl_table *table, struct afs_callback_arg *aca) { const char *src, *dest; struct osl_object obj; struct osl_row *row; int ret; ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr); assert(ret >= 0); src = lls_input(0, aca->lpr); dest = lls_input(1, aca->lpr); obj.data = (char *)src; obj.size = strlen(src) + 1; ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row)); if (ret < 0) { afs_error(aca, "cannot find source blob %s\n", src); goto out; } obj.data = (char *)dest; obj.size = strlen(dest) + 1; ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj)); if (ret < 0) { afs_error(aca, "cannot rename blob %s to %s\n", src, dest); goto out; } ret = afs_event(BLOB_RENAME, table); out: lls_free_parse_result(aca->lpr, cmd); return ret; } static int com_mvblob(afs_callback *f, const struct lls_command * const cmd, struct command_context *cc, struct lls_parse_result *lpr) { char *errctx; int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx)); if (ret < 0) { send_errctx(cc, errctx); return ret; } return send_lls_callback_request(f, cc->afs_fd, cmd, lpr, cc); } #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \ static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \ { \ const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \ return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \ } \ static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \ { \ const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \ return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \ } \ EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name); static int blob_get_name_by_id(struct osl_table *table, uint32_t id, char **name) { struct osl_row *row; struct osl_object obj = {.data = &id, .size = sizeof(id)}; int ret; if (name) *name = NULL; if (!id) return 1; ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row)); if (ret < 0) return ret; ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj)); if (ret < 0) return ret; if (*(char *)obj.data == '\0') return -E_DUMMY_ROW; if (name) *name = (char *)obj.data; return 1; } /** Define the \p get_name_by_id function for this blob type. */ #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \ int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \ { \ return blob_get_name_by_id(table_name ## _table, id, name); \ } static int blob_get_def_by_name(struct osl_table *table, const char *name, struct osl_object *def) { struct osl_row *row; struct osl_object obj = {.data = (void *)name, .size = strlen(name) + 1}; int ret; def->data = NULL; if (!*name) return 1; ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row)); if (ret < 0) return ret; return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def)); } /** Define the \p get_def_by_id function for this blob type. */ #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \ int cmd_prefix ## _get_def_by_name(const char *name, struct osl_object *def) \ { \ return blob_get_def_by_name(table_name ## _table, name, def); \ } static int blob_get_name_and_def_by_row(struct osl_table *table, const struct osl_row *row, char **name, struct osl_object *def) { struct osl_object obj; int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj)); if (ret < 0) return ret; *name = obj.data; return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def)); } /** Define the \p get_name_and_def_by_row function for this blob type. */ #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \ int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \ char **name, struct osl_object *def) \ { \ return blob_get_name_and_def_by_row(table_name ## _table, \ row, name, def); \ } /** Define the \p close function for this blob type. */ #define DEFINE_BLOB_CLOSE(table_name) \ static void table_name ## _close(void) \ { \ osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \ table_name ## _table = NULL; \ } /** Define the \p create function for this blob type. */ #define DEFINE_BLOB_CREATE(table_name) \ static int table_name ## _create(const char *dir) \ { \ table_name ## _table_desc.dir = dir; \ return osl(osl_create_table(&table_name ## _table_desc)); \ } static int blob_open(struct osl_table **table, struct osl_table_description *desc, const char *dir) { desc->dir = dir; return osl(osl_open_table(desc, table)); } #define DEFINE_BLOB_OPEN(table_name) \ static int table_name ## _open(const char *dir) \ { \ return blob_open(&table_name ## _table, \ &table_name ## _table_desc, dir); \ } /** Blob tables map integers to blobs. */ #define DEFINE_BLOB_AFS_TABLE_OPS(table_name, ehandler) \ const struct afs_table_operations table_name ## _ops = { \ .open = table_name ## _open, \ .close = table_name ## _close, \ .create = table_name ## _create, \ .event_handler = ehandler, \ }; /** Define all functions for this blob type. */ #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name, ehandler) \ DEFINE_BLOB_OPEN(table_name) \ DEFINE_BLOB_CLOSE(table_name) \ DEFINE_BLOB_CREATE(table_name) \ DEFINE_BLOB_AFS_TABLE_OPS(table_name, ehandler) \ DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \ DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \ DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \ DEFINE_BLOB_COMMAND(rm, RM, table_name, short_name, c_short_name) \ DEFINE_BLOB_COMMAND(mv, MV, table_name, short_name, c_short_name) \ DEFINE_GET_NAME_BY_ID(table_name, short_name); \ DEFINE_GET_DEF_BY_NAME(table_name, short_name); \ DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, short_name); \ /* doxygen isn't smart enough to recognize these */ /** \cond blob_function */ DEFINE_BLOB_FUNCTIONS(lyrics, lyr, LYR, NULL); DEFINE_BLOB_FUNCTIONS(images, img, IMG, NULL); DEFINE_BLOB_FUNCTIONS(moods, mood, MOOD, moods_event_handler); DEFINE_BLOB_FUNCTIONS(playlists, pl, PL, playlists_event_handler); /** \endcond blob_function */