/* * Clear out the Lua cache. */ int udf_cask_info_clear_cache(char *name, char * params, cf_dyn_buf * out) { cf_debug(AS_INFO, "UDF CASK INFO CLEAR CACHE"); mod_lua_wrlock(&mod_lua); as_module_event e = { .type = AS_MODULE_EVENT_CLEAR_CACHE }; as_module_update(&mod_lua, &e); mod_lua_unlock(&mod_lua); cf_dyn_buf_append_string(out, "ok"); return 0; }
int udf_cask_smd_accept_fn(char *module, as_smd_item_list_t *items, void *udata, uint32_t accept_opt) { if (accept_opt & AS_SMD_ACCEPT_OPT_CREATE) { cf_debug(AS_UDF, "(doing nothing in UDF accept cb for module creation)"); return 0; } cf_debug(AS_UDF, "UDF CASK accept fn : n items %zu", items->num_items); // For each item in the list, see if the current version // is different from the curretly stored version // and if the new item is new, write to the storage directory for (int i = 0; i < items->num_items ; i++) { as_smd_item_t *item = items->item[i]; if (item->action == AS_SMD_ACTION_SET) { json_error_t json_err; json_t *item_obj = json_loads(item->value, 0 /*flags*/, &json_err); /*item->key is name */ json_t *content64_obj = json_object_get(item_obj, "content64"); const char *content64_str = json_string_value(content64_obj); // base 64 decode it uint32_t encoded_len = strlen(content64_str); uint32_t decoded_len = cf_b64_decoded_buf_size(encoded_len) + 1; char *content_str = cf_malloc(decoded_len); if (! cf_b64_validate_and_decode(content64_str, encoded_len, (uint8_t*)content_str, &decoded_len)) { cf_info(AS_UDF, "invalid script on accept, will not register %s", item->key); cf_free(content_str); json_decref(content64_obj); json_decref(item_obj); continue; } content_str[decoded_len] = 0; cf_debug(AS_UDF, "pushing to %s, %d bytes [%s]", item->key, decoded_len, content_str); mod_lua_wrlock(&mod_lua); // content_gen is actually a hash. Not sure if it's filled out or what. unsigned char content_gen[256] = {0}; int e = file_write(item->key, (uint8_t *) content_str, decoded_len, content_gen); cf_free(content_str); json_decref(content64_obj); json_decref(item_obj); if ( e ) { mod_lua_unlock(&mod_lua); cf_info(AS_UDF, "invalid script on accept, will not register %s", item->key); continue; } // Update the cache as_module_event ame = { .type = AS_MODULE_EVENT_FILE_ADD, .data.filename = item->key }; as_module_update(&mod_lua, &ame); mod_lua_unlock(&mod_lua); } else if (item->action == AS_SMD_ACTION_DELETE) { cf_debug(AS_UDF, "received DELETE SMD action %d key %s", item->action, item->key); mod_lua_wrlock(&mod_lua); file_remove(item->key); // fixes potential cache issues as_module_event e = { .type = AS_MODULE_EVENT_FILE_REMOVE, .data.filename = item->key }; as_module_update(&mod_lua, &e); mod_lua_unlock(&mod_lua); } else {
int udf_cask_smd_accept_fn(char *module, as_smd_item_list_t *items, void *udata, uint32_t accept_opt) { if (accept_opt & AS_SMD_ACCEPT_OPT_CREATE) { cf_debug(AS_UDF, "(doing nothing in UDF accept cb for module creation)"); return 0; } cf_debug(AS_UDF, "UDF CASK accept fn : n items %d", items->num_items); // For each item in the list, see if the current version // is different from the curretly stored version // and if the new item is new, write to the storage directory for (int i = 0; i < items->num_items ; i++) { as_smd_item_t *item = items->item[i]; if (item->action == AS_SMD_ACTION_SET) { json_error_t json_err; json_t *item_obj = json_loads(item->value, 0 /*flags*/, &json_err); /*item->key is name */ json_t *content64_obj = json_object_get(item_obj, "content64"); const char *content64_str = json_string_value(content64_obj); // base 64 decode it int content_len = strlen(content64_str); char *content_str = cf_malloc(content_len); // base64_decode function does not add '\0' at the end , if input string is of length%3 = 0 // content_len is greater than required size. // It leads adding junk characters at the end of LUA file. // Zeroing out the string, so that it will have '\0' at end in all cases. memset (content_str, 0, content_len); int e = base64_decode((uint8_t *)content64_str, (uint8_t *)content_str, &content_len, true); if (e) { cf_info(AS_UDF, "invalid script on accept, will not register %s", item->key); cf_free(content_str); json_decref(content64_obj); json_decref(item_obj); continue; } cf_debug(AS_UDF, "pushing to %s, %d bytes [%s]", item->key, content_len, content_str); mod_lua_wrlock(&mod_lua); // content_gen is actually a hash. Not sure if it's filled out or what. unsigned char content_gen[256] = {0}; e = file_write(item->key, (uint8_t *) content_str, content_len, content_gen); cf_free(content_str); json_decref(content64_obj); json_decref(item_obj); if ( e ) { mod_lua_unlock(&mod_lua); cf_info(AS_UDF, "invalid script on accept, will not register %s", item->key); continue; } // Update the cache as_module_event ame = { .type = AS_MODULE_EVENT_FILE_ADD, .data.filename = item->key }; as_module_update(&mod_lua, &ame); mod_lua_unlock(&mod_lua); } else if (item->action == AS_SMD_ACTION_DELETE) { cf_debug(AS_UDF, "received DELETE SMD action %d key %s", item->action, item->key); mod_lua_wrlock(&mod_lua); file_remove(item->key); // fixes potential cache issues as_module_event e = { .type = AS_MODULE_EVENT_FILE_REMOVE, .data.filename = item->key }; as_module_update(&mod_lua, &e); mod_lua_unlock(&mod_lua); } else {