static int do_connect(const char *host, int port, const char *user, const char *pass, const char *email, int reg_user, int connid) { int fd = -1, authresult, copylen; char ipv6_error[120], ipv4_error[120], errmsg[256]; json_t *jmsg, *jarr; #ifdef UNIX /* try to connect to a local unix socket */ struct sockaddr_un sun; sun.sun_family = AF_UNIX; copylen = strlen(host) + 1; if (copylen > sizeof(sun.sun_path) - 1) copylen = sizeof(sun.sun_path) - 1; memcpy(sun.sun_path, host, copylen); sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd >= 0) { if (connect(fd, (struct sockaddr*)&sun, sizeof(sun)) == -1) { close(fd); fd = -1; } } #endif /* try ipv6 */ if (fd == -1) fd = connect_server(host, port, FALSE, ipv6_error, 120); if (fd == -1) /* no ipv6 connection, try ipv4 */ fd = connect_server(host, port, TRUE, ipv4_error, 120); if (fd == -1) { /* both connection attempts failed: error message */ if (!ipv6_error[0] && !ipv4_error[0]) { snprintf(errmsg, 256, "No address for \"%s\" found!", host); print_error(errmsg); } if (ipv6_error[0]) { snprintf(errmsg, 256, "IPv6: %s", ipv6_error); print_error(errmsg); } if (ipv4_error[0]) { snprintf(errmsg, 256, "IPv4: %s", ipv4_error); print_error(errmsg); } return NO_CONNECTION; } in_connect_disconnect = TRUE; sockfd = fd; jmsg = json_pack("{ss,ss}", "username", user, "password", pass); if (reg_user) { if (email) json_object_set_new(jmsg, "email", json_string(email)); jmsg = send_receive_msg("register", jmsg); } else { if (connid) json_object_set_new(jmsg, "reconnect", json_integer(connid)); jmsg = send_receive_msg("auth", jmsg); } in_connect_disconnect = FALSE; if (!jmsg || json_unpack(jmsg, "{si,si*}", "return", &authresult, "connection", &connection_id) == -1) { if (jmsg) json_decref(jmsg); close(fd); return NO_CONNECTION; } /* the "version" field in the response is optional */ if (json_unpack(jmsg, "{so*}", "version", &jarr) != -1 && json_is_array(jarr) && json_array_size(jarr) >= 3) { nhnet_server_ver.major = json_integer_value(json_array_get(jarr, 0)); nhnet_server_ver.minor = json_integer_value(json_array_get(jarr, 1)); nhnet_server_ver.patchlevel = json_integer_value(json_array_get(jarr, 2)); } json_decref(jmsg); if (host != saved_hostname) strncpy(saved_hostname, host, sizeof(saved_hostname)); if (user != saved_username) strncpy(saved_username, user, sizeof(saved_username)); if (pass != saved_password) strncpy(saved_password, pass, sizeof(saved_password)); saved_port = port; conn_err = FALSE; net_active = TRUE; return authresult; }
struct watchman_trigger_command *w_build_trigger_from_def( w_root_t *root, json_t *trig, char **errmsg) { struct watchman_trigger_command *cmd; json_t *ele, *query, *relative_root; json_int_t jint; const char *name = NULL; cmd = calloc(1, sizeof(*cmd)); if (!cmd) { *errmsg = strdup("no memory"); return NULL; } cmd->definition = trig; json_incref(cmd->definition); query = json_pack("{s:O}", "expression", json_object_get(cmd->definition, "expression")); relative_root = json_object_get(cmd->definition, "relative_root"); if (relative_root) { json_object_set_nocheck(query, "relative_root", relative_root); } cmd->query = w_query_parse(root, query, errmsg); json_decref(query); if (!cmd->query) { w_trigger_command_free(cmd); return NULL; } json_unpack(trig, "{s:u}", "name", &name); if (!name) { *errmsg = strdup("invalid or missing name"); w_trigger_command_free(cmd); return NULL; } cmd->triggername = w_string_new_typed(name, W_STRING_UNICODE); cmd->command = json_object_get(trig, "command"); if (cmd->command) { json_incref(cmd->command); } if (!cmd->command || !json_is_array(cmd->command) || !json_array_size(cmd->command)) { *errmsg = strdup("invalid command array"); w_trigger_command_free(cmd); return NULL; } json_unpack(trig, "{s:b}", "append_files", &cmd->append_files); ele = json_object_get(trig, "stdin"); if (!ele) { cmd->stdin_style = input_dev_null; } else if (json_is_array(ele)) { cmd->stdin_style = input_json; if (!parse_field_list(ele, &cmd->field_list, errmsg)) { w_trigger_command_free(cmd); return NULL; } } else if (json_is_string(ele)) { const char *str = json_string_value(ele); if (!strcmp(str, "/dev/null")) { cmd->stdin_style = input_dev_null; } else if (!strcmp(str, "NAME_PER_LINE")) { cmd->stdin_style = input_name_list; } else { ignore_result(asprintf(errmsg, "invalid stdin value %s", str)); w_trigger_command_free(cmd); return NULL; } } else { *errmsg = strdup("invalid value for stdin"); w_trigger_command_free(cmd); return NULL; } jint = 0; // unlimited unless specified json_unpack(trig, "{s:I}", "max_files_stdin", &jint); if (jint < 0) { *errmsg = strdup("max_files_stdin must be >= 0"); w_trigger_command_free(cmd); return NULL; } cmd->max_files_stdin = (uint32_t)jint; json_unpack(trig, "{s:s}", "stdout", &cmd->stdout_name); json_unpack(trig, "{s:s}", "stderr", &cmd->stderr_name); if (!parse_redirection(&cmd->stdout_name, &cmd->stdout_flags, "stdout", errmsg)) { w_trigger_command_free(cmd); return NULL; } if (!parse_redirection(&cmd->stderr_name, &cmd->stderr_flags, "stderr", errmsg)) { w_trigger_command_free(cmd); return NULL; } // Copy current environment cmd->envht = w_envp_make_ht(); // Set some standard vars w_envp_set(cmd->envht, "WATCHMAN_ROOT", root->root_path); w_envp_set_cstring(cmd->envht, "WATCHMAN_SOCK", get_sock_name()); w_envp_set(cmd->envht, "WATCHMAN_TRIGGER", cmd->triggername); return cmd; }
/* trigger /root triggername [watch patterns] -- cmd to run * Sets up a trigger so that we can execute a command when a change * is detected */ static void cmd_trigger(struct watchman_client *client, json_t *args) { w_root_t *root; struct watchman_trigger_command *cmd, *old; json_t *resp; json_t *trig; char *errmsg = NULL; bool need_save = true; root = resolve_root_or_err(client, args, 1, true); if (!root) { return; } if (json_array_size(args) < 3) { send_error_response(client, "not enough arguments"); goto done; } trig = json_array_get(args, 2); if (json_is_string(trig)) { trig = build_legacy_trigger(root, client, args); if (!trig) { goto done; } } else { // Add a ref so that we don't need to conditionally decref later // for the legacy case later json_incref(trig); } cmd = w_build_trigger_from_def(root, trig, &errmsg); json_decref(trig); if (!cmd) { send_error_response(client, "%s", errmsg); goto done; } resp = make_response(); set_prop(resp, "triggerid", w_string_to_json(cmd->triggername)); w_root_lock(root, "trigger-add"); old = w_ht_val_ptr(w_ht_get(root->commands, w_ht_ptr_val(cmd->triggername))); if (old && json_equal(cmd->definition, old->definition)) { // Same definition: we don't and shouldn't touch things, so that we // preserve the associated trigger clock and don't cause the trigger // to re-run immediately set_unicode_prop(resp, "disposition", "already_defined"); w_trigger_command_free(cmd); cmd = NULL; need_save = false; } else { set_unicode_prop(resp, "disposition", old ? "replaced" : "created"); w_ht_replace(root->commands, w_ht_ptr_val(cmd->triggername), w_ht_ptr_val(cmd)); // Force the trigger to be eligible to run now root->ticks++; root->pending_trigger_tick = root->ticks; } w_root_unlock(root); if (need_save) { w_state_save(); } send_and_dispose_response(client, resp); done: if (errmsg) { free(errmsg); } w_root_delref(root); }
size_t ast_json_array_size(const struct ast_json *array) { return json_array_size((json_t *)array); }
int main(int argc, char *argv[]) { //setHttpAuth("admin:admin"); //setHttpData("{\"ins_api\":{\"version\":\"1.2\",\"type\":\"cli_show\",\"chunk\":\"0\",\"sid\":\"1\",\"input\":\"show version\",\"output_format\":\"json\"}}"); //appendHttpHeader("Content-Type: application/json"); //printf("%s\n",httpFunction("http://192.168.113.50/ins")); OV_Version = OV200; char *oneViewAddress; //main2(); //sleep(40); // Check for Debug Mode if (getenv("OV_DEBUG")) { debug = 1; // debug mode enabled } else { debug = 0; // debug mode disabled } // Peform an initial check to see what parameters have been passed char path[100]; if (argc >1) { oneViewAddress = argv[1]; if (is_valid_ip(oneViewAddress)) { // Create a string based upon the path to the sessionID identifySystem(oneViewAddress); sprintf(path, "/.%s_ov",oneViewAddress); } else { printMessage(YELLOW, "DEBUG", "Invalid IP Address"); return 1; } } if(argc < 3) { printMessage(YELLOW, "DEBUG", "No parameters passed"); fprintf(stderr, "usage: %s ADDRESS COMMAND <parameters>\n", argv[0]); // Somewhat over the top logo fprintf(stderr, " \ _ _ ____ ___ __ ___\n\ | | | | _ \\ / _ \\ _ __ __\\ \\ / (_) _____ __\n\ | |_| | |_) | | | | | '_ \\ / _ \\ \\ / /| |/ _ \\ \\ /\\ / /\n\ | _ | __/ | |_| | | | | __/\\ V / | | __/\\ V V /\n\ |_| |_|_| \\___/|_| |_|\\___| \\_/ |_|\\___| \\_/\\_/ \n\n"); //Print the relevant helps ovCreatePrintHelp(); ovShowPrintHelp(); ovCopyPrintHelp(); ovMessageBusHelp(); return 1; } char url[URL_SIZE]; char *httpData; json_t *root = NULL; json_error_t error; // Determine the action to be executed if (stringMatch(argv[2], "LOGIN")) { // Login to HP OneView SetHttpMethod(DCHTTPPOST); ovLogin(argv, path); return 0; } else if (stringMatch(argv[2], "SHOW")) { // Show/Query information from HP OneView char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovShow(sessionID, argc, argv); return 0; // return sucess } else if (stringMatch(argv[2], "CREATE")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovCreate(sessionID, argv); return 0; // Return success } else if (strstr(argv[2], "COPY")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovCopy(sessionID, argv); return 0; //return success } else if (strstr(argv[2], "CLONE")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID"); return 1; } // DEBUG OVID output //printf("[DEBUG] OVID:\t %s\n",sessionID); if (strstr(argv[3], "SERVER-PROFILES")) { snprintf(url, URL_SIZE, URL_FORMAT, argv[1], "server-profiles"); // Call to HP OneView API httpData = getRequestWithUrlAndHeader(url, sessionID); if(!httpData) return 1; //printf("[DEBUG] JSON: %s\n", httpData); root = json_loads(httpData, 0, &error); // Find Server Profile first //int fieldCount = argc -5; //argv[0] is the path to the program json_t *memberArray = json_object_get(root, "members"); if (json_array_size(memberArray) != 0) { size_t index; json_t *serverProfile = NULL; //char *json_text; json_array_foreach(memberArray, index, serverProfile) { const char *uri = json_string_value(json_object_get(serverProfile, "uri")); char *cloneuri = argv[4]; printf ("%s / %s /n", cloneuri, url); if (uri != NULL) { if (stringMatch(cloneuri, (char *)uri)) { //json_text = json_dumps(serverProfile, JSON_INDENT(4)); //4 is close to a tab //printf("%s\n", json_text); // json_object_del(serverProfile, "uri"); json_object_del(serverProfile, "serialNumber"); json_object_del(serverProfile, "uuid"); json_object_del(serverProfile, "taskUri"); json_object_del(serverProfile, "status"); json_object_del(serverProfile, "inProgress"); json_object_del(serverProfile, "modified"); json_object_del(serverProfile, "eTag"); json_object_del(serverProfile, "created"); json_object_del(serverProfile, "serverHardwareUri"); json_object_del(serverProfile, "enclosureBay"); json_object_del(serverProfile, "enclosureUri"); //json_object_del(serverProfile, "connections"); json_t *connections, *connectionsArray= json_object_get(serverProfile, "connections"); json_array_foreach(connectionsArray, index, connections) { json_object_del(connections, "mac"); json_object_del(connections, "wwnn"); json_object_del(connections, "wwpn"); } int profileCount = atoi(argv[5]); if (profileCount != 0){ char name[100]; //strcat(name, json_string_value(json_object_get(serverProfile, "name"))); //strcat(name, ) //const char *profileName = json_string_value(json_object_get(serverProfile, "name")); char profileName[100]; strcpy(profileName, json_string_value(json_object_get(serverProfile, "name"))); for (int i =0; i <profileCount; i++) { sprintf(name, "%s_%d",profileName, i); printf("%s\n", name); json_string_set( json_object_get(serverProfile, "name"), name); httpData = postRequestWithUrlAndDataAndHeader(url, json_dumps(serverProfile, JSON_ENSURE_ASCII), sessionID); if(!httpData) return 1; } } //printf("%s\n", json_string_value(json_object_get(serverProfile, "name"))); //json_dumps(root, JSON_ENSURE_ASCII) /* httpData = postRequestWithUrlAndDataAndHeader(url, json_dumps(serverProfile, JSON_ENSURE_ASCII), sessionID); if(!httpData) return 1; //free(json_text); json_text = json_dumps(serverProfile, JSON_INDENT(4)); //4 is close to a tab printf("%s\n", json_text); free(json_text); */ } }
static w_query_expr *since_parser(w_query *query, json_t *term) { json_t *jval; struct w_clockspec *spec; struct since_term *sterm; int selected_field = SINCE_OCLOCK; const char *fieldname = "oclock"; if (!json_is_array(term)) { query->errmsg = strdup("\"since\" term must be an array"); return NULL; } if (json_array_size(term) < 2 || json_array_size(term) > 3) { query->errmsg = strdup("\"since\" term has invalid number of parameters"); return NULL; } jval = json_array_get(term, 1); spec = w_clockspec_parse(jval); if (!spec) { query->errmsg = strdup("invalid clockspec for \"since\" term"); return NULL; } if (spec->tag == w_cs_named_cursor) { query->errmsg = strdup("named cursors are not allowed in \"since\" terms"); goto fail; } jval = json_array_get(term, 2); if (jval) { int i; bool valid = false; fieldname = json_string_value(jval); if (!fieldname) { query->errmsg = strdup("field name for \"since\" term must be a string"); goto fail; } for (i = 0; allowed_fields[i].label; i++) { if (!strcmp(allowed_fields[i].label, fieldname)) { selected_field = allowed_fields[i].value; valid = true; break; } } if (!valid) { ignore_result(asprintf(&query->errmsg, "invalid field name \"%s\" for \"since\" term", fieldname)); goto fail; } } switch (selected_field) { case SINCE_CTIME: case SINCE_MTIME: if (spec->tag != w_cs_timestamp) { ignore_result(asprintf(&query->errmsg, "field \"%s\" requires a timestamp value " "for comparison in \"since\" term", fieldname)); goto fail; } break; case SINCE_OCLOCK: case SINCE_CCLOCK: /* we'll work with clocks or timestamps */ break; } sterm = calloc(1, sizeof(*sterm)); if (!sterm) { query->errmsg = strdup("out of memory"); goto fail; } sterm->spec = spec; sterm->field = selected_field; return w_query_expr_new(eval_since, dispose_since, sterm); fail: w_clockspec_free(spec); return NULL; }
void amqp_cache_purge(struct chained_cache *queue[], int index) { struct pkt_primitives *data = NULL; struct pkt_bgp_primitives *pbgp = NULL; struct pkt_nat_primitives *pnat = NULL; struct pkt_mpls_primitives *pmpls = NULL; char *pcust = NULL; struct pkt_vlen_hdr_primitives *pvlen = NULL; struct pkt_bgp_primitives empty_pbgp; struct pkt_nat_primitives empty_pnat; struct pkt_mpls_primitives empty_pmpls; char *empty_pcust = NULL; char src_mac[18], dst_mac[18], src_host[INET6_ADDRSTRLEN], dst_host[INET6_ADDRSTRLEN], ip_address[INET6_ADDRSTRLEN]; char rd_str[SRVBUFLEN], misc_str[SRVBUFLEN], dyn_amqp_routing_key[SRVBUFLEN], *orig_amqp_routing_key = NULL; int i, j, stop, batch_idx, is_routing_key_dyn = FALSE, qn = 0, ret, saved_index = index; int mv_num = 0, mv_num_save = 0; time_t start, duration; pid_t writer_pid = getpid(); #ifdef WITH_JANSSON json_t *array = json_array(); #endif /* setting some defaults */ if (!config.sql_host) config.sql_host = default_amqp_host; if (!config.sql_db) config.sql_db = default_amqp_exchange; if (!config.amqp_exchange_type) config.amqp_exchange_type = default_amqp_exchange_type; if (!config.amqp_vhost) config.amqp_vhost = default_amqp_vhost; if (!config.sql_table) config.sql_table = default_amqp_routing_key; else { if (strchr(config.sql_table, '$')) { is_routing_key_dyn = TRUE; orig_amqp_routing_key = config.sql_table; config.sql_table = dyn_amqp_routing_key; } } if (config.amqp_routing_key_rr) { orig_amqp_routing_key = config.sql_table; config.sql_table = dyn_amqp_routing_key; } p_amqp_set_exchange(&amqpp_amqp_host, config.sql_db); p_amqp_set_routing_key(&amqpp_amqp_host, config.sql_table); p_amqp_set_exchange_type(&amqpp_amqp_host, config.amqp_exchange_type); p_amqp_set_host(&amqpp_amqp_host, config.sql_host); p_amqp_set_vhost(&amqpp_amqp_host, config.amqp_vhost); p_amqp_set_persistent_msg(&amqpp_amqp_host, config.amqp_persistent_msg); p_amqp_set_frame_max(&amqpp_amqp_host, config.amqp_frame_max); p_amqp_set_content_type_json(&amqpp_amqp_host); p_amqp_init_routing_key_rr(&amqpp_amqp_host); p_amqp_set_routing_key_rr(&amqpp_amqp_host, config.amqp_routing_key_rr); empty_pcust = malloc(config.cpptrs.len); if (!empty_pcust) { Log(LOG_ERR, "ERROR ( %s/%s ): Unable to malloc() empty_pcust. Exiting.\n", config.name, config.type); exit_plugin(1); } memset(&empty_pbgp, 0, sizeof(struct pkt_bgp_primitives)); memset(&empty_pnat, 0, sizeof(struct pkt_nat_primitives)); memset(&empty_pmpls, 0, sizeof(struct pkt_mpls_primitives)); memset(empty_pcust, 0, config.cpptrs.len); ret = p_amqp_connect_to_publish(&amqpp_amqp_host); if (ret) return; for (j = 0, stop = 0; (!stop) && P_preprocess_funcs[j]; j++) stop = P_preprocess_funcs[j](queue, &index, j); Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - START (PID: %u) ***\n", config.name, config.type, writer_pid); start = time(NULL); for (j = 0; j < index; j++) { char *json_str; if (queue[j]->valid != PRINT_CACHE_COMMITTED) continue; data = &queue[j]->primitives; if (queue[j]->pbgp) pbgp = queue[j]->pbgp; else pbgp = &empty_pbgp; if (queue[j]->pnat) pnat = queue[j]->pnat; else pnat = &empty_pnat; if (queue[j]->pmpls) pmpls = queue[j]->pmpls; else pmpls = &empty_pmpls; if (queue[j]->pcust) pcust = queue[j]->pcust; else pcust = empty_pcust; if (queue[j]->pvlen) pvlen = queue[j]->pvlen; else pvlen = NULL; if (queue[j]->valid == PRINT_CACHE_FREE) continue; json_str = compose_json(config.what_to_count, config.what_to_count_2, queue[j]->flow_type, &queue[j]->primitives, pbgp, pnat, pmpls, pcust, pvlen, queue[j]->bytes_counter, queue[j]->packet_counter, queue[j]->flow_counter, queue[j]->tcp_flags, &queue[j]->basetime, queue[j]->stitch); #ifdef WITH_JANSSON if (json_str && config.sql_multi_values) { json_t *elem = NULL; char *tmp_str = json_str; int do_free = FALSE; if (json_array_size(array) >= config.sql_multi_values) { json_str = json_dumps(array, 0); json_array_clear(array); mv_num_save = mv_num; mv_num = 0; } else do_free = TRUE; elem = json_loads(tmp_str, 0, NULL); json_array_append_new(array, elem); mv_num++; if (do_free) { free(json_str); json_str = NULL; } } #endif if (json_str) { if (is_routing_key_dyn) { amqp_handle_routing_key_dyn_strings(dyn_amqp_routing_key, SRVBUFLEN, orig_amqp_routing_key, queue[j]); p_amqp_set_routing_key(&amqpp_amqp_host, dyn_amqp_routing_key); } if (config.amqp_routing_key_rr) { p_amqp_handle_routing_key_dyn_rr(dyn_amqp_routing_key, SRVBUFLEN, orig_amqp_routing_key, &amqpp_amqp_host.rk_rr); p_amqp_set_routing_key(&amqpp_amqp_host, dyn_amqp_routing_key); } ret = p_amqp_publish_string(&amqpp_amqp_host, json_str); free(json_str); json_str = NULL; if (!ret) { if (!config.sql_multi_values) qn++; else qn += mv_num_save; } else break; } } #ifdef WITH_JANSSON if (config.sql_multi_values && json_array_size(array)) { char *json_str; json_str = json_dumps(array, 0); json_array_clear(array); json_decref(array); if (json_str) { /* no handling of dyn routing keys here: not compatible */ ret = p_amqp_publish_string(&amqpp_amqp_host, json_str); free(json_str); json_str = NULL; if (!ret) qn += mv_num; } } #endif p_amqp_close(&amqpp_amqp_host, FALSE); duration = time(NULL)-start; Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - END (PID: %u, QN: %u/%u, ET: %u) ***\n", config.name, config.type, writer_pid, qn, saved_index, duration); if (config.sql_trigger_exec) P_trigger_exec(config.sql_trigger_exec); if (empty_pcust) free(empty_pcust); }
const json_t* frontEvent(EventHistory_t* history) { return json_array_get(history->root, json_array_size(history->root) - 1); }