int main(int argc, char** argv) { WsManClient *cl; WsXmlDocH doc; client_opt_t *options = NULL; printf ("Test 1: Testin Identify Request:"); cl = wsmc_create( sd[0].server, sd[0].port, sd[0].path, sd[0].scheme, sd[0].username, sd[0].password); wsmc_transport_init(cl, NULL); options = wsmc_options_init(); doc = wsmc_action_identify(cl, options); if (!doc) { printf("\t\t\033[22;31mUNRESOLVED\033[m\n"); goto CONTINUE; } char *xp = ws_xml_get_xpath_value(doc, "/s:Envelope/s:Body/wsmid:IdentifyResponse/wsmid:ProtocolVersion"); if (xp) { if (strcmp(xp,XML_NS_WS_MAN ) == 0) printf("\t\t\033[22;32mPASSED\033[m\n"); else printf("\t\t\033[22;31mFAILED\033[m\n"); u_free(xp); } else { printf("\t\t\033[22;31mFAILED\033[m\n"); } if (doc) { ws_xml_destroy_doc(doc); } CONTINUE: wsmc_options_destroy(options); wsmc_release(cl); return 0; }
static virDrvOpenStatus hypervConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags) { virDrvOpenStatus result = VIR_DRV_OPEN_ERROR; char *plus; hypervPrivate *priv = NULL; char *username = NULL; char *password = NULL; virBuffer query = VIR_BUFFER_INITIALIZER; Msvm_ComputerSystem *computerSystem = NULL; virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR); /* Decline if the URI is NULL or the scheme is NULL */ if (conn->uri == NULL || conn->uri->scheme == NULL) { return VIR_DRV_OPEN_DECLINED; } /* Decline if the scheme is not hyperv */ plus = strchr(conn->uri->scheme, '+'); if (plus == NULL) { if (STRCASENEQ(conn->uri->scheme, "hyperv")) { return VIR_DRV_OPEN_DECLINED; } } else { if (plus - conn->uri->scheme != 6 || STRCASENEQLEN(conn->uri->scheme, "hyperv", 6)) { return VIR_DRV_OPEN_DECLINED; } virReportError(VIR_ERR_INVALID_ARG, _("Transport '%s' in URI scheme is not supported, try again " "without the transport part"), plus + 1); return VIR_DRV_OPEN_ERROR; } /* Require server part */ if (conn->uri->server == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("URI is missing the server part")); return VIR_DRV_OPEN_ERROR; } /* Require auth */ if (auth == NULL || auth->cb == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("Missing or invalid auth pointer")); return VIR_DRV_OPEN_ERROR; } /* Allocate per-connection private data */ if (VIR_ALLOC(priv) < 0) goto cleanup; if (hypervParseUri(&priv->parsedUri, conn->uri) < 0) { goto cleanup; } /* Set the port dependent on the transport protocol if no port is * specified. This allows us to rely on the port parameter being * correctly set when building URIs later on, without the need to * distinguish between the situations port == 0 and port != 0 */ if (conn->uri->port == 0) { if (STRCASEEQ(priv->parsedUri->transport, "https")) { conn->uri->port = 5986; } else { conn->uri->port = 5985; } } /* Request credentials */ if (conn->uri->user != NULL) { if (VIR_STRDUP(username, conn->uri->user) < 0) goto cleanup; } else { username = virAuthGetUsername(conn, auth, "hyperv", "administrator", conn->uri->server); if (username == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed")); goto cleanup; } } password = virAuthGetPassword(conn, auth, "hyperv", username, conn->uri->server); if (password == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed")); goto cleanup; } /* Initialize the openwsman connection */ priv->client = wsmc_create(conn->uri->server, conn->uri->port, "/wsman", priv->parsedUri->transport, username, password); if (priv->client == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not create openwsman client")); goto cleanup; } if (wsmc_transport_init(priv->client, NULL) != 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not initialize openwsman transport")); goto cleanup; } /* FIXME: Currently only basic authentication is supported */ wsman_transport_set_auth_method(priv->client, "basic"); /* Check if the connection can be established and if the server has the * Hyper-V role installed. If the call to hypervGetMsvmComputerSystemList * succeeds than the connection has been established. If the returned list * is empty than the server isn't a Hyper-V server. */ virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_SELECT); virBufferAddLit(&query, "where "); virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_PHYSICAL); if (hypervGetMsvmComputerSystemList(priv, &query, &computerSystem) < 0) { goto cleanup; } if (computerSystem == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("%s is not a Hyper-V server"), conn->uri->server); goto cleanup; } conn->privateData = priv; priv = NULL; result = VIR_DRV_OPEN_SUCCESS; cleanup: hypervFreePrivate(&priv); VIR_FREE(username); VIR_FREE(password); hypervFreeObject(priv, (hypervObject *)computerSystem); return result; }
static int hypervInitConnection(virConnectPtr conn, hypervPrivate *priv, char *username, char *password) { virBuffer query = VIR_BUFFER_INITIALIZER; hypervWqlQuery wqlQuery = HYPERV_WQL_QUERY_INITIALIZER; hypervObject *computerSystem = NULL; int ret = -1; /* Initialize the openwsman connection */ priv->client = wsmc_create(conn->uri->server, conn->uri->port, "/wsman", priv->parsedUri->transport, username, password); if (priv->client == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not create openwsman client")); goto cleanup; } if (wsmc_transport_init(priv->client, NULL) != 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not initialize openwsman transport")); goto cleanup; } /* FIXME: Currently only basic authentication is supported */ wsman_transport_set_auth_method(priv->client, "basic"); wqlQuery.info = Msvm_ComputerSystem_WmiInfo; wqlQuery.query = &query; virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_SELECT); virBufferAddLit(&query, "WHERE "); virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_PHYSICAL); /* try query using V2 namespace (for Hyper-V 2012+) */ priv->wmiVersion = HYPERV_WMI_VERSION_V2; if (hypervEnumAndPull(priv, &wqlQuery, &computerSystem) < 0) { /* rebuild query because hypervEnumAndPull consumes it */ virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_SELECT); virBufferAddLit(&query, "WHERE "); virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_PHYSICAL); /* fall back to V1 namespace (for Hyper-V 2008) */ priv->wmiVersion = HYPERV_WMI_VERSION_V1; if (hypervEnumAndPull(priv, &wqlQuery, &computerSystem) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("%s is not a Hyper-V server"), conn->uri->server); goto cleanup; } } ret = 0; cleanup: hypervFreeObject(priv, computerSystem); return ret; }
void wsmc_handler( WsManClient *cl, WsXmlDocH rqstDoc, void* user_data) { #define curl_err(str) debug("Error = %d (%s); %s", \ r, curl_easy_strerror(r), str); WsManConnection *con = cl->connection; CURL *curl = NULL; CURLcode r; char *upwd = NULL; char *usag = NULL; struct curl_slist *headers=NULL; char *buf = NULL; int len; char *soapact_header = NULL; long http_code; long auth_avail = 0; char *_user = NULL, *_pass = NULL; u_buf_t *response = NULL; //char *soapaction; char *tmp_str = NULL; if (!cl->initialized && wsmc_transport_init(cl, NULL)) { cl->last_error = WS_LASTERR_FAILED_INIT; return; } if (cl->transport == NULL) { cl->transport = init_curl_transport(cl); if (cl->transport == NULL) { return; } } curl = (CURL *)cl->transport; r = curl_easy_setopt(curl, CURLOPT_URL, cl->data.endpoint); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_URL, ...)"); goto DONE; } r = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_handler); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ..)"); goto DONE; } u_buf_create(&response); r = curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_WRITEDATA, ..)"); goto DONE; } char content_type[64]; snprintf(content_type, 64, "Content-Type: application/soap+xml;charset=%s", cl->content_encoding); headers = curl_slist_append(headers, content_type); tmp_str = wsman_transport_get_agent(cl); usag = malloc(12 + strlen(tmp_str) + 1); if (usag == NULL) { r = CURLE_OUT_OF_MEMORY; cl->fault_string = u_strdup("Could not malloc memory"); curl_err("Could not malloc memory"); goto DONE; } sprintf(usag, "User-Agent: %s", tmp_str); free(tmp_str); headers = curl_slist_append(headers, usag); #if 0 soapaction = ws_xml_get_xpath_value(rqstDoc, "/s:Envelope/s:Header/wsa:Action"); if (soapaction) { soapact_header = malloc(12 + strlen(soapaction) + 1); if (soapact_header) { sprintf(soapact_header, "SOAPAction: %s", soapaction); headers = curl_slist_append(headers, soapact_header); } u_free(soapaction); } #endif r = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_HTTPHEADER, ..)"); goto DONE; } ws_xml_dump_memory_enc(rqstDoc, &buf, &len, cl->content_encoding); #if 0 int count = 0; while(count < len) { printf("%c",buf[count++]); } #endif debug("*****set post buf len = %d******",len); r = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ..)"); goto DONE; } r = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("Could not curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, ..)"); goto DONE; } int iDone = 0; while (1) { u_free(_user); u_free(_pass); _user = wsmc_get_user(cl); _pass = wsmc_get_password(cl); if (_user && _pass && cl->data.auth_set) { r = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, cl->data.auth_set); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("curl_easy_setopt(CURLOPT_HTTPAUTH) failed"); goto DONE; } u_free(upwd); upwd = u_strdup_printf( "%s:%s", _user , _pass); if (!upwd) { r = CURLE_OUT_OF_MEMORY; cl->fault_string = u_strdup("Could not malloc memory"); curl_err("Could not malloc memory"); goto DONE; } r = curl_easy_setopt(curl, CURLOPT_USERPWD, upwd); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("curl_easy_setopt(curl, CURLOPT_USERPWD, ..) failed"); goto DONE; } } if (wsman_debug_level_debugged(DEBUG_LEVEL_MESSAGE)) { curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); } r = curl_easy_perform(curl); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("curl_easy_perform failed"); goto DONE; } r = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("curl_easy_getinfo(CURLINFO_RESPONSE_CODE) failed"); goto DONE; } switch (http_code) { case 200: case 400: case 500: // The resource was successfully retrieved or WSMan server // returned a HTTP status code. You can use WinHttpReadData to // read the contents of the server's response. iDone = 1; break; case 401: // The server requires authentication. break; default: // The status code does not indicate success. r = WS_LASTERR_OTHER_ERROR; iDone = 1; break; } if(iDone == 1) { break; } /* we are here because of authentication required */ r = curl_easy_getinfo(curl, CURLINFO_HTTPAUTH_AVAIL, &auth_avail); if (r != CURLE_OK) { cl->fault_string = u_strdup(curl_easy_strerror(r)); curl_err("curl_easy_getinfo(CURLINFO_HTTPAUTH_AVAIL) failed"); goto DONE; } cl->data.auth_set = reauthenticate(cl, cl->data.auth_set, auth_avail, &cl->data.user, &cl->data.pwd); u_buf_clear(response); if (cl->data.auth_set == 0) { /* FIXME: user wants to cancel authentication */ #if LIBCURL_VERSION_NUM >= 0x70D01 r = CURLE_LOGIN_DENIED; #else /* Map the login failure error to CURLE_LOGIN_DENIED (67) so that we * get the same error code in case of login failure */ r = 67; #endif curl_err("user/password wrong or empty."); break; } } #if 0 unsigned char *mbbuf = NULL; iconv_t cd; if(strcmp(cl->content_encoding, "UTF-8")) { cd = iconv_open("UTF-8", cl->content_encoding); if(cd == -1) { cl->last_error = WS_LASTERR_BAD_CONTENT_ENCODING; goto DONE2; } mbbuf = u_zalloc(u_buf_len(response)); size_t outbuf_len = u_buf_len(response); size_t inbuf_len = outbuf_len; char *inbuf = u_buf_ptr(response); char *outbuf = mbbuf; size_t coverted = iconv(cd, &inbuf, &inbuf_len, &outbuf, &outbuf_len); iconv_close(cd); if( coverted == -1) { cl->last_error = WS_LASTERR_BAD_CONTENT_ENCODING; goto DONE2; } u_buf_append(con->response, mbbuf, u_buf_len(response) - inbuf_len); } u_free(mbbuf); #endif u_buf_append(con->response, u_buf_ptr(response), u_buf_len(response)); DONE: curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); cl->response_code = http_code; cl->last_error = convert_to_last_error(r); debug("curl error code: %d.", r); debug("cl->response_code: %d.", cl->response_code); debug("cl->last_error code: %d.", cl->last_error); curl_slist_free_all(headers); u_buf_free(response); u_free(soapact_header); u_free(usag); u_free(upwd); u_free(_pass); u_free(_user); #ifdef _WIN32 ws_xml_free_memory(buf); #else u_free(buf); #endif return; #undef curl_err }
int main(int argc, char** argv) { int i; int choice = 0; WsManClient *cl; WsXmlDocH doc; client_opt_t *options = NULL; WsXmlDocH resource = NULL; if (getenv("OPENWSMAN_TEST_HOST")) { host = getenv("OPENWSMAN_TEST_HOST"); } resource = wsmc_read_file("./requests/create.xml", "UTF-8", 0); if(argc > 1) choice = atoi(argv[1]); for (i = 0; i < ntests; i++) { if(choice && i != choice -1) continue; if (tests[i].selectors) { tests[i].selectors = u_strdup_printf(tests[i].selectors, host, host, host); } if (tests[i].expected_value) { tests[i].expected_value = u_strdup_printf(tests[i].expected_value, host, host, host); } printf ("Test %3d: %s ", i + 1, tests[i].explanation); cl = wsmc_create( sd[0].server, sd[0].port, sd[0].path, sd[0].scheme, sd[0].username, sd[0].password); wsmc_transport_init(cl, NULL); options = wsmc_options_init(); if (tests[i].selectors != NULL) wsmc_add_selectors_from_str (options, tests[i].selectors); wsmc_set_action_option(options, FLAG_DUMP_REQUEST); doc = wsmc_action_create(cl, (char *)tests[i].resource_uri, options, resource); if (!doc) { printf("\t\t\033[22;31mUNRESOLVED\033[m\n"); goto CONTINUE; } wsman_output(doc); if (tests[i].final_status != wsmc_get_response_code(cl)) { printf("Status = %ld \t\t\033[22;31mFAILED\033[m\n", wsmc_get_response_code(cl)); goto CONTINUE; } if ((char *)tests[i].expected_value != NULL) { char *xp = ws_xml_get_xpath_value(doc, (char *)tests[i].xpath_expression); if (xp) { if (strcmp(xp,(char *)tests[i].expected_value ) == 0) printf("\t\t\033[22;32mPASSED\033[m\n"); else printf("%s = %s\t\033[22;31mFAILED\033[m\n",(char *)tests[i].xpath_expression, xp); u_free(xp); } else { printf(" No %s\t\033[22;31mFAILED\033[m\n", (char *)tests[i].xpath_expression); ws_xml_dump_node_tree(stdout, ws_xml_get_doc_root(doc)); } } else { printf("\t\t\033[22;32mPASSED\033[m\n"); } ws_xml_destroy_doc(doc); CONTINUE: u_free(tests[i].selectors); u_free(tests[i].expected_value); wsmc_options_destroy(options); wsmc_release(cl); } return 0; }
int main(int argc, char** argv) { int i; WsManClient *cl; WsXmlDocH docp; client_opt_t *options = NULL; char *enumContext = NULL; //unsigned int id = 0; //wsman_debug_set_level(DEBUG_LEVEL_DEBUG); initialize_logging(); //wsmc_add_handler(wsmc_handler, NULL); for (i = 0; i < ntests; i++) { printf ("Test %d: %s:", i + 1, tests[i].explanation); //printf ("------------------------------------------------\n"); cl = wsmc_create( sd[0].server, sd[0].port, sd[0].path, sd[0].scheme, sd[0].username, sd[0].password); wsmc_transport_init(cl, NULL); options = wsmc_options_init(); options->flags = tests[i].flags; options->max_elements = tests[i].max_elements; if (tests[i].selectors != NULL) wsmc_add_selectors_from_str (options, tests[i].selectors); WsXmlDocH enum_response = wsmc_action_enumerate(cl, (char *)tests[i].resource_uri , options, NULL); if (!enum_response) { printf("\t\t\033[22;31mUNRESOLVED\033[m\n"); goto CONTINUE; } //wsman_output(enum_response); if ((char *)tests[i].expected_value != NULL) { char *xp = ws_xml_get_xpath_value(enum_response, (char *)tests[i].xpath_expression); if (xp) { if (strcmp(xp,(char *)tests[i].expected_value ) == 0) printf("\t\t\033[22;32mPASSED\033[m\n"); else printf("\t\t\033[22;31mFAILED\033[m\n"); u_free(xp); } } wsmc_free_enum_context(enumContext); enumContext = wsmc_get_enum_context(enum_response); ws_xml_destroy_doc(enum_response); while (enumContext != NULL) { docp = wsmc_action_pull(cl, (char *)tests[i].resource_uri, options, NULL, enumContext); if (!docp) { printf("\t\t\033[22;31mUNRESOLVED\033[m\n"); goto CONTINUE; } wsman_output(docp); wsmc_free_enum_context(enumContext); enumContext = wsmc_get_enum_context(docp); ws_xml_destroy_doc(docp); } CONTINUE: wsmc_options_destroy(options); wsmc_release(cl); } return 0; }