Esempio n. 1
0
int qvd_connection(const char *host, int port, const char *user, const char *pass, const char *geometry, int fullscreen, int only_list_of_vm, int one_vm, int no_cert_check, int restart_session, const char *nx_options, const char *client_cert, const char *client_key, int preselectedvm) {
  int vm_id;
  qvdclient *qvd;

  qvd = qvd_init(host, port, user, pass);

  if (no_cert_check)
    qvd_set_no_cert_check(qvd);

  if (geometry)
    qvd_set_geometry(qvd, geometry);
  if (fullscreen)
    qvd_set_fullscreen(qvd);
  if (nx_options)
    qvd_set_nx_options(qvd, nx_options);

  qvd_set_cert_files(qvd, client_cert, client_key);

  qvd_set_unknown_cert_callback(qvd, accept_unknown_cert_callback);
  qvd_set_progress_callback(qvd, progress_callback);

  if (qvd_list_of_vm(qvd) == NULL)
    {
      printf("Error fetching vm for user %s in host %s: %s\n", user, host, qvd->error_buffer);
      qvd_free(qvd);
      return 5;
    }
  if (qvd->numvms <= 0)
    {
      printf("No vms found for user %s in host %s\n", user, host);
      qvd_free(qvd);
      return 2;
    }

  if (only_list_of_vm)
    {
      print_vmids(qvd->vmlist);
      printf("No more acctions, -l has been specified\n");
      return 0;
    }


  if (one_vm || qvd->numvms == 1 || preselectedvm)
    {
      if (preselectedvm)
	{
	  vm_id = preselectedvm;
	  printf("Connecting to preselected vm: vm_id %d\n", vm_id);
	}
      else
	{
	  vm_id = qvd->vmlist->data->id;
	  printf("Connecting to the first vm: vm_id %d\n", vm_id);
	}
    }
  else
    {
      vm_id = choose_vmid(qvd->vmlist);
    }
  if (vm_id < 0)
    {
      printf("Error choosing vm_id: %d\n", vm_id);
      return 6;
    }

  if ( restart_session )
     qvd_stop_vm(qvd, vm_id);

  /* Set display if not set */
  _set_display_if_not_set(qvd);

  qvd_connect_to_vm(qvd, vm_id);
  printf("after qvd_connect_to_vm\n");
  qvd_free(qvd);
  return 0;


}
Esempio n. 2
0
/* Init and free functions */
qvdclient *qvd_init(const char *hostname, const int port, const char *username, const char *password) {
  qvdclient *qvd;
  qvd_printf("Starting qvd_init. %s", qvd_get_version_text);
  if (strlen(username) + strlen(password) + 2 > MAX_USERPWD) {
    qvd_error(qvd, "Length of username and password + 2 is longer than %d\n", MAX_USERPWD);
    return NULL;
  }

  if (strlen(hostname) + 6 + strlen("https:///") + 2 > MAX_BASEURL) {
    qvd_error(qvd, "Length of hostname and port + scheme  + 2 is longer than %d\n", MAX_BASEURL);
    return NULL;
  }

  if (! (qvd = (qvdclient *) malloc(sizeof(qvdclient)))) {
    qvd_error(qvd, "Error allocating memory: %s", strerror(errno));
    return NULL;
  }
  
  if (snprintf(qvd->userpwd, MAX_USERPWD, "%s:%s", username, password) >= MAX_USERPWD) {
    qvd_error(qvd, "Error initializing userpwd (string too long)\n");
    free(qvd);
    return NULL;
  }
  if (_qvd_set_base64_auth(qvd)) {
    qvd_error(qvd, "Error initializing authdigest\n");
    free(qvd);
    return NULL;
    }

  if (snprintf(qvd->baseurl, MAX_BASEURL, "https://%s:%d", hostname, port) >= MAX_BASEURL) {
    qvd_error(qvd, "Error initializing baseurl(string too long)\n");
    free(qvd);
    return NULL;
  }

  if (snprintf(qvd->useragent, MAX_USERAGENT, "%s %s", DEFAULT_USERAGENT_PRODUCT, curl_version()) >= MAX_USERAGENT) {
    qvd_error(qvd, "Error initializing useragent (string too long)\n");
    free(qvd);
    return NULL;
  }

  qvd->curl = curl_easy_init();
  if (!qvd->curl) {
    qvd_error(qvd, "Error initializing curl\n");
    free(qvd);
    return NULL;
  }
  qvd_printf("Curl pointer is %p", qvd->curl);
  if (get_debug_level()) {
    curl_easy_setopt(qvd->curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(qvd->curl, CURLOPT_DEBUGFUNCTION, qvd_curl_debug_callback);
  }

  curl_easy_setopt(qvd->curl, CURLOPT_ERRORBUFFER, qvd->error_buffer);  /* curl_easy_setopt(qvd->curl, CURLOPT_SSL_VERIFYPEER, 1L); */
  /* curl_easy_setopt(qvd->curl, CURLOPT_SSL_VERIFYHOST, 2L); */
  curl_easy_setopt(qvd->curl, CURLOPT_CERTINFO, 1L);
  curl_easy_setopt(qvd->curl, CURLOPT_CAPATH, qvd->certpath);
  curl_easy_setopt(qvd->curl, CURLOPT_SSL_CTX_FUNCTION, _qvd_sslctxfun);
  curl_easy_setopt(qvd->curl, CURLOPT_SSL_CTX_DATA, (void *)qvd);
  /*  curl_easy_setopt(qvd->curl, CURLOPT_CAINFO, NULL);*/
  _qvd_ssl_index = SSL_CTX_get_ex_new_index(0, (void *)qvd, NULL, NULL, NULL);
  curl_easy_setopt(qvd->curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(qvd->curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(qvd->curl, CURLOPT_TCP_NODELAY, 1L);
  /*  curl_easy_setopt(qvd->curl, CURLOPT_FAILONERROR, 1L);*/
  curl_easy_setopt(qvd->curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
  curl_easy_setopt(qvd->curl, CURLOPT_USERPWD, qvd->userpwd);
  curl_easy_setopt(qvd->curl, CURLOPT_WRITEFUNCTION, _qvd_write_buffer_callback);
  curl_easy_setopt(qvd->curl, CURLOPT_WRITEDATA, &(qvd->buffer));
  curl_easy_setopt(qvd->curl, CURLOPT_USERAGENT, qvd->useragent);
  /* If client certificate CURLOPT_SSLCERT , CURLOPT_SSLKEY, CURLOPT_SSLCERTTYPE "PEM" */
  /* Copy parameters */
  strncpy(qvd->hostname, hostname, MAX_BASEURL);
  qvd->hostname[MAX_BASEURL - 1] = '\0';
  qvd->port = port;
  strncpy(qvd->username, username, MAX_USERPWD);
  qvd->username[MAX_USERPWD - 1] = '\0';
  strncpy(qvd->password, password, MAX_USERPWD);
  qvd->password[MAX_USERPWD - 1] = '\0';
  strncpy(qvd->client_cert, "", MAX_PATH_STRING);
  strncpy(qvd->client_key, "", MAX_PATH_STRING);
  qvd->use_client_cert = 0;
  qvd->numvms = 0;
  qvd_set_link(qvd, DEFAULT_LINK);
  qvd_set_geometry(qvd, DEFAULT_GEOMETRY);
  qvd_set_os(qvd, DEFAULT_OS);
  qvd->keyboard = "pc%2F105";
  qvd->fullscreen = 0;
  qvd->print_enabled = 0;
  qvd->ssl_no_cert_check = 0;
  qvd->ssl_verify_callback = NULL;
  qvd->progress_callback = NULL;
  qvd->userdata = NULL;
  qvd->nx_options = NULL;

  *(qvd->display) = '\0';
  *(qvd->home) = '\0';
  strcpy(qvd->error_buffer, "");
  QvdBufferInit(&(qvd->buffer));

  if (!(qvd->vmlist = malloc(sizeof(vmlist)))) {
    free(qvd);
    return NULL;
  }
  QvdVmListInit(qvd->vmlist);
  
  return qvd;
}