summaryrefslogtreecommitdiffstats
path: root/src/wsman.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/wsman.c')
-rw-r--r--src/wsman.c401
1 files changed, 342 insertions, 59 deletions
diff --git a/src/wsman.c b/src/wsman.c
index 90c8433..58a690a 100644
--- a/src/wsman.c
+++ b/src/wsman.c
@@ -48,16 +48,263 @@
#include <errno.h>
#include <time.h>
-#include <u/libu.h>
#include <wsman-client-api.h>
#include <wsman-debug.h>
-#include "wsman-client-options.h"
+// #include "wsman-client-options.h"
+
+
+
+static const char **wsman_argv = NULL;
+
+
+static int server_port = 0;
+static char *cafile = NULL;
+static char *username = NULL;
+static char *password = NULL;
+static char *server = "localhost";
+static char *agent = NULL;
+static char *url_path = NULL;
+static char *authentication_method = NULL;
+static char verify_peer = 1;
+static char verify_host = 1;
+static int transport_timeout = 0;
+static char *proxy = NULL;
+static char *proxy_upwd = NULL;
+
+
+static int debug_level = -1;
+static char *test_case = NULL;
+static int enum_max_elements = 0;
+char enum_optimize = 0;
+char enum_estimate = 0;
+char dump_request = 0;
+char step = 0;
+char request_only = 0;
+char cim_extensions = 0;
+char cim_references = 0;
+char cim_associators = 0;
+static char *enum_mode = NULL;
+static char *binding_enum_mode = NULL;
+static char *enum_context = NULL;
+
+
+static char *cim_namespace = NULL;
+static char *fragment = NULL;
+static char *wsm_filter = NULL;
+static char *wsm_dialect = NULL;
+static char *input = NULL;
+
+static unsigned long operation_timeout = 0;
+static unsigned long max_envelope_size = 0;
+
+static char *_action = NULL;
+static char *config_file = NULL;
+static char *output_file = NULL;
+static char *resource_uri = NULL;
+static char *invoke_method = NULL;
+static char **properties = NULL;
+
+struct _WsActions {
+ char *action;
+ int value;
+};
+typedef struct _WsActions WsActions;
+
+WsActions action_data[] = {
+ {"get", WSMAN_ACTION_TRANSFER_GET},
+ {"put", WSMAN_ACTION_TRANSFER_PUT},
+ {"create", WSMAN_ACTION_TRANSFER_CREATE},
+ {"delete", WSMAN_ACTION_TRANSFER_DELETE},
+ {"enumerate", WSMAN_ACTION_ENUMERATION},
+ {"pull", WSMAN_ACTION_PULL},
+ {"release", WSMAN_ACTION_RELEASE},
+ {"invoke", WSMAN_ACTION_CUSTOM},
+ {"identify", WSMAN_ACTION_IDENTIFY},
+ {"test", WSMAN_ACTION_TEST},
+ {NULL, 0},
+};
+
+char wsman_parse_options(int argc, char **argv)
+{
+
+ char retval = 0;
+ u_error_t *error = NULL;
+
+ u_option_entry_t options[] = {
+ {"debug", 'd', U_OPTION_ARG_INT, &debug_level,
+ "Set the verbosity of debugging output.", "1-6"},
+ {"cafile", 'c', U_OPTION_ARG_STRING, &cafile,
+ "Certificate file", "<filename>"},
+ {"username", 'u', U_OPTION_ARG_STRING, &username,
+ "User name", "<username>"},
+ {"path", 'g', U_OPTION_ARG_STRING, &url_path,
+ "Path", "<path>"},
+ {"input", 'J', U_OPTION_ARG_STRING, &input,
+ "File with resource for Create and Put operations in XML, can be a SOAP envelope",
+ "<filename>"},
+ {"password", 'p', U_OPTION_ARG_STRING, &password,
+ "Password", "<password>"},
+ {"hostname", 'h', U_OPTION_ARG_STRING, &server,
+ "Host name", "<hostname>"},
+ {"port", 'P', U_OPTION_ARG_INT, &server_port,
+ "Server Port", "<port>"},
+ {"proxy", 'X', U_OPTION_ARG_STRING, &proxy,
+ "Proxy name", "<proxy>"},
+ {"proxyauth", 'Y', U_OPTION_ARG_STRING, &proxy_upwd,
+ "Proxy user:pwd", "<proxyauth>"},
+ {"auth", 'y', U_OPTION_ARG_STRING, &authentication_method,
+ "Authentication Method", "<basic|digest|gss>"},
+ {"method", 'a', U_OPTION_ARG_STRING, &invoke_method,
+ "Method (Works only with 'invoke')", "<custom method>"},
+ {"prop", 'k', U_OPTION_ARG_STRING_ARRAY, &properties,
+ "Properties with key value pairs (For 'put', 'invoke' and 'create')",
+ "<key=val>"},
+ {"config-file", 'C', U_OPTION_ARG_STRING, &config_file,
+ "Alternate configuration file", "<file>"},
+ {"out-file", 'O', U_OPTION_ARG_STRING, &output_file,
+ "Write output to file", "<file>"},
+ {"noverifypeer", 'V', U_OPTION_ARG_NONE, &verify_peer,
+ "Not to verify peer certificate", NULL},
+ {"noverifyhost", 'v', U_OPTION_ARG_NONE, &verify_host,
+ "Not to verify hostname", NULL},
+ {"transport-timeout", 'I', U_OPTION_ARG_INT, &transport_timeout,
+ "Transport timeout in seconds", "<time in sec>"},
+ {NULL}
+ };
+
+
+
+ u_option_entry_t request_options[] = {
+ {"filter", 'x', U_OPTION_ARG_STRING, &wsm_filter,
+ "Filter", "<filter>"},
+ {"dialect", 'D', U_OPTION_ARG_STRING, &wsm_dialect,
+ "Filter Dialect", "<dialect>"},
+ {"operation-timeout", 't', U_OPTION_ARG_INT, &operation_timeout,
+ "Operation timeout in seconds", "<time in sec>"},
+ {"max-envelope-size", 'e', U_OPTION_ARG_INT,
+ &max_envelope_size,
+ "maximal envelope size", "<size>"},
+ {"fragment", 'F', U_OPTION_ARG_STRING, &fragment,
+ "Fragment (Supported Dialects: XPATH)", "<fragment>"},
+ {NULL}
+ };
+
+ u_option_entry_t enum_options[] = {
+
+ {"max-elements", 'm', U_OPTION_ARG_INT, &enum_max_elements,
+ "Max Elements Per Pull/Optimized Enumeration",
+ "<max number of elements>"},
+ {"optimize", 'o', U_OPTION_ARG_NONE, &enum_optimize,
+ "Optimize enumeration results", NULL},
+ {"estimate-count", 'E', U_OPTION_ARG_NONE, &enum_estimate,
+ "Return estimation of total items", NULL},
+ {"enum-mode", 'M', U_OPTION_ARG_STRING, &enum_mode,
+ "Enumeration Mode", "epr|objepr"},
+ {"enum-context", 'U', U_OPTION_ARG_STRING, &enum_context,
+ "Enumeration Context (For use with Pull and Release)",
+ "<enum context>"},
+ {NULL}
+ };
+
+ u_option_entry_t cim_options[] = {
+
+ {"namespace", 'N', U_OPTION_ARG_STRING, &cim_namespace,
+ "CIM Namespace (default is root/cimv2)", "<namespace>"},
+ {"binding-enum-mode", 'B', U_OPTION_ARG_STRING,
+ &binding_enum_mode,
+ "CIM binding Enumeration Mode", "none|include|exclude"},
+ {"cim-extensions", 'T', U_OPTION_ARG_NONE, &cim_extensions,
+ "Show CIM Extensions", NULL},
+ {"references", 'W', U_OPTION_ARG_NONE, &cim_references,
+ "CIM References", NULL},
+ {"associators", 'w', U_OPTION_ARG_NONE, &cim_associators,
+ "CIM Associators", NULL},
+ {NULL}
+ };
+
+ u_option_entry_t test_options[] = {
+ {"from-file", 'f', U_OPTION_ARG_STRING, &test_case,
+ "Send request from file", "<file name>"},
+ {"print-request", 'R', U_OPTION_ARG_NONE, &dump_request,
+ "print request on stdout", NULL},
+ {"request", 'Q', U_OPTION_ARG_NONE, &request_only,
+ "Only output reqest. Not send it.", NULL},
+ {"step", 'S', U_OPTION_ARG_NONE, &step,
+ "Do not perform multiple operations (do not pull data when enumerating)",
+ NULL},
+ //{ "print-response", 'N', 0, G_OPTION_ARG_NONE, &dump_response, "print all responses to stdout", NULL},
+ {NULL}
+ };
+
+ u_option_group_t *enum_group;
+ u_option_group_t *test_group;
+ u_option_group_t *cim_group;
+ u_option_group_t *req_flag_group;
+
+ u_option_context_t *opt_ctx;
+ opt_ctx = u_option_context_new("<action> <Resource Uri>");
+ enum_group = u_option_group_new("enumeration", "Enumeration",
+ "Enumeration Options");
+ test_group = u_option_group_new("tests", "Tests", "Test Cases");
+ cim_group = u_option_group_new("cim", "CIM", "CIM Options");
+ req_flag_group =
+ u_option_group_new("flags", "Flags", "Request Flags");
+
+ u_option_group_add_entries(enum_group, enum_options);
+ u_option_group_add_entries(test_group, test_options);
+ u_option_group_add_entries(cim_group, cim_options);
+ u_option_group_add_entries(req_flag_group, request_options);
+
+ u_option_context_set_ignore_unknown_options(opt_ctx, FALSE);
+ u_option_context_add_main_entries(opt_ctx, options, "wsman");
+ u_option_context_add_group(opt_ctx, enum_group);
+ u_option_context_add_group(opt_ctx, test_group);
+ u_option_context_add_group(opt_ctx, cim_group);
+ u_option_context_add_group(opt_ctx, req_flag_group);
+
+ retval = u_option_context_parse(opt_ctx, &argc, &argv, &error);
+ u_option_context_free(opt_ctx);
+
+ if (error) {
+ if (error->message)
+ printf("%s\n", error->message);
+ u_error_free(error);
+ return FALSE;
+ }
+
+ if (argc > 2) {
+ _action = argv[1];
+ resource_uri = argv[2];
+ } else {
+ if (argv[1] && (strcmp(argv[1], "identify") == 0 ||
+ strcmp(argv[1], "test") == 0)) {
+ _action = argv[1];
+ } else {
+ fprintf(stderr,
+ "Error: operation can not be completed."
+ " Action or/and Resource Uri missing.\n");
+ return FALSE;
+ }
+ }
+ u_error_free(error);
+
+ // set default options
+ if (server_port == 0) {
+ server_port = cafile ? 8888 : 8889;
+ }
+ if (url_path == NULL) {
+ url_path = "/wsman";
+ }
+ return TRUE;
+}
+
+
static void wsman_output(WsManClient * cl, WsXmlDocH doc)
{
FILE *f = stdout;
- const char *filename = wsman_options_get_output_file();
+ const char *filename = output_file;
WS_LASTERR_Code err;
err = wsmc_get_last_error(cl);
@@ -121,6 +368,49 @@ request_usr_pwd( wsman_auth_type_t auth,
*password = u_strdup_printf ("%s", pw);
}
+static hash_t *wsman_options_get_properties(void)
+{
+ int c = 0;
+ hash_t *h = hash_create(HASHCOUNT_T_MAX, 0, 0);
+
+ while (properties != NULL && properties[c] != NULL) {
+ char *cc[3];
+ u_tokenize1(cc, 2, properties[c], '=');
+ if (!hash_alloc_insert(h, cc[0], cc[1])) {
+ debug("hash_alloc_insert failed");
+ }
+ c++;
+ }
+ return h;
+}
+
+static int wsman_options_get_action(void)
+{
+ int op = 0;
+ int i;
+ for (i = 0; action_data[i].action != NULL; i++) {
+ if (strcmp(action_data[i].action, _action) == 0) {
+ op = action_data[i].value;
+ break;
+ }
+ }
+ return op;
+}
+static int wsman_read_client_config(dictionary * ini)
+{
+ if (iniparser_find_entry(ini, "client")) {
+ agent = iniparser_getstr(ini, "client:agent");
+ server_port = server_port ?
+ server_port : iniparser_getint(ini, "client:port", 80);
+ authentication_method = authentication_method ?
+ authentication_method :
+ iniparser_getstr(ini, "client:authentication_method");
+ } else {
+ return 0;
+ }
+ return 1;
+}
+
int main(int argc, char **argv)
{
@@ -141,7 +431,7 @@ int main(int argc, char **argv)
- filename = (char *) wsman_options_get_config_file();
+ filename = (char *) config_file;
if (filename) {
ini = iniparser_load(filename);
if (ini == NULL) {
@@ -161,14 +451,14 @@ int main(int argc, char **argv)
// wsmc_transport_init(NULL);
options = wsmc_options_init();
- debug("Certificate: %s", wsman_options_get_cafile());
+ debug("Certificate: %s", cafile);
- cl = wsmc_create(wsman_options_get_server(),
- wsman_options_get_server_port(),
- wsman_options_get_path(),
- wsman_options_get_cafile()? "https" :
- "http", wsman_options_get_username(),
- wsman_options_get_password());
+ cl = wsmc_create(server,
+ server_port,
+ url_path,
+ cafile? "https" : "http",
+ username,
+ password);
wsmc_transport_set_auth_request_func(cl , &request_usr_pwd );
@@ -178,26 +468,26 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
// transport options
- wsman_transport_set_auth_method(cl, wsman_options_get_auth_method());
- if (wsman_options_get_proxy()) {
- wsman_transport_set_proxy(cl, wsman_options_get_proxy());
- if (wsman_options_get_proxy_upwd()) {
- wsman_transport_set_proxyauth(cl, wsman_options_get_proxy_upwd());
+ wsman_transport_set_auth_method(cl, authentication_method);
+ if (proxy) {
+ wsman_transport_set_proxy(cl, proxy);
+ if (proxy_upwd) {
+ wsman_transport_set_proxyauth(cl, proxy_upwd);
}
}
- if (wsman_options_get_cafile()) {
- wsman_transport_set_cafile(cl, wsman_options_get_cafile());
+ if (cafile) {
+ wsman_transport_set_cafile(cl, cafile);
}
- wsman_transport_set_verify_peer(cl, wsman_options_get_verify_peer());
- wsman_transport_set_verify_host(cl, wsman_options_get_verify_host());
- wsman_transport_set_timeout(cl, wsman_options_get_transport_timeout());
+ wsman_transport_set_verify_peer(cl, verify_peer);
+ wsman_transport_set_verify_host(cl, verify_host);
+ wsman_transport_set_timeout(cl, transport_timeout);
// library options
- wsman_debug_set_level(wsman_options_get_debug_level());
+ wsman_debug_set_level(debug_level);
/*
* Setup Resource URI and Selectors
*/
- resource_uri_with_selectors = wsman_options_get_resource_uri();
+ resource_uri_with_selectors = resource_uri;
if (resource_uri_with_selectors &&
strcmp(resource_uri_with_selectors,CIM_ALL_AVAILABLE_CLASSES) != 0) {
wsmc_set_options_from_uri(resource_uri_with_selectors,
@@ -210,37 +500,34 @@ int main(int argc, char **argv)
}
op = wsman_options_get_action();
- if (wsman_options_get_dump_request()) {
+ if (dump_request) {
wsmc_set_action_option(options, FLAG_DUMP_REQUEST);
}
- if (wsman_options_get_max_envelope_size()) {
- options->max_envelope_size =
- wsman_options_get_max_envelope_size();
+ if (max_envelope_size) {
+ options->max_envelope_size = max_envelope_size;
}
- if (wsman_options_get_operation_timeout()) {
- options->timeout = wsman_options_get_operation_timeout();
+ if (operation_timeout) {
+ options->timeout = operation_timeout;
}
- if (wsman_options_get_fragment()) {
- options->fragment = wsman_options_get_fragment();
+ if (fragment) {
+ options->fragment = fragment;
}
- if (wsman_options_get_filter()) {
- options->filter = wsman_options_get_filter();
+ if (wsm_filter) {
+ options->filter = wsm_filter;
}
- if (wsman_options_get_dialect()) {
- options->dialect = wsman_options_get_dialect();
+ if (wsm_dialect) {
+ options->dialect = wsm_dialect;
}
options->properties = wsman_options_get_properties();
- options->cim_ns = wsman_options_get_cim_namespace();
- if (wsman_options_get_cim_ext()) {
+ options->cim_ns = cim_namespace;
+ if (cim_extensions) {
wsmc_set_action_option(options, FLAG_CIM_EXTENSIONS);
}
switch (op) {
case WSMAN_ACTION_TEST:
- rqstDoc = wsmc_read_file(cl,
- wsman_options_get_input_file
- (), "UTF-8", 0);
+ rqstDoc = wsmc_read_file(cl, input, "UTF-8", 0);
wsman_send_request(cl, rqstDoc);
doc = wsmc_build_envelope_from_response(cl);
wsman_output(cl, doc);
@@ -258,7 +545,7 @@ int main(int argc, char **argv)
case WSMAN_ACTION_CUSTOM:
doc = wsmc_action_invoke(cl, resource_uri, options,
- wsman_options_get_invoke_method(),
+ invoke_method,
NULL);
wsman_output(cl, doc);
if (doc) {
@@ -273,10 +560,8 @@ int main(int argc, char **argv)
}
break;
case WSMAN_ACTION_TRANSFER_CREATE:
- if (wsman_options_get_input_file()) {
- resource = wsmc_read_file(cl,
- wsman_options_get_input_file
- (), "UTF-8", 0);
+ if (input) {
+ resource = wsmc_read_file(cl, input, "UTF-8", 0);
doc =
wsmc_action_create(cl, resource_uri, options,
resource);
@@ -290,11 +575,9 @@ int main(int argc, char **argv)
}
break;
case WSMAN_ACTION_TRANSFER_PUT:
- if (wsman_options_get_input_file()) {
+ if (input) {
printf("input file provided\n");
- resource = wsmc_read_file(cl,
- wsman_options_get_input_file
- (), "UTF-8", 0);
+ resource = wsmc_read_file(cl, input, "UTF-8", 0);
doc =
wsmc_action_put(cl, resource_uri, options,
resource);
@@ -319,7 +602,7 @@ int main(int argc, char **argv)
case WSMAN_ACTION_PULL:
doc =
wsmc_action_pull(cl, resource_uri, options,
- wsman_options_get_enum_context());
+ enum_context);
wsman_output(cl, doc);
if (doc) {
ws_xml_destroy_doc(doc);
@@ -328,7 +611,7 @@ int main(int argc, char **argv)
case WSMAN_ACTION_RELEASE:
doc =
wsmc_action_release(cl, resource_uri, options,
- wsman_options_get_enum_context());
+ enum_context);
wsman_output(cl, doc);
if (doc) {
ws_xml_destroy_doc(doc);
@@ -336,9 +619,9 @@ int main(int argc, char **argv)
break;
case WSMAN_ACTION_ENUMERATION:
- enumeration_mode = wsman_options_get_enum_mode();
+ enumeration_mode = enum_mode;
binding_enumeration_mode =
- wsman_options_get_binding_enum_mode();
+ binding_enum_mode;
if (enumeration_mode) {
if (strcmp(enumeration_mode, "epr") == 0)
@@ -363,23 +646,23 @@ int main(int argc, char **argv)
wsmc_set_action_option(options,
FLAG_POLYMORPHISM_NONE);
}
- if (wsman_options_get_cim_ref()) {
+ if (cim_references) {
wsmc_set_action_option(options,
FLAG_CIM_REFERENCES);
options->dialect = WSM_ASSOCIATION_FILTER_DIALECT;
}
- if (wsman_options_get_cim_assoc()) {
+ if (cim_associators) {
wsmc_set_action_option(options,
FLAG_CIM_ASSOCIATORS);
options->dialect = WSM_ASSOCIATION_FILTER_DIALECT;
}
- if (wsman_options_get_optimize_enum()) {
+ if (enum_optimize) {
wsmc_set_action_option(options,
FLAG_ENUMERATION_OPTIMIZATION);
}
- options->max_elements = wsman_options_get_max_elements();
+ options->max_elements = enum_max_elements;
- if (wsman_options_get_estimate_enum()) {
+ if (enum_estimate) {
wsmc_set_action_option(options,
FLAG_ENUMERATION_COUNT_ESTIMATION);
}
@@ -398,7 +681,7 @@ int main(int argc, char **argv)
break;
}
- if (wsman_options_get_step_request())
+ if (step)
break;
while (enumContext != NULL && enumContext[0] != 0) {