ui-qmp-cmds.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * QMP commands related to UI
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "monitor/qmp-helpers.h"
  17. #include "qapi/qapi-commands-ui.h"
  18. #include "qapi/qmp/qerror.h"
  19. #include "qemu/cutils.h"
  20. #include "ui/console.h"
  21. #include "ui/dbus-display.h"
  22. #include "ui/qemu-spice.h"
  23. void qmp_set_password(SetPasswordOptions *opts, Error **errp)
  24. {
  25. int rc;
  26. if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
  27. if (!qemu_using_spice(errp)) {
  28. return;
  29. }
  30. rc = qemu_spice.set_passwd(opts->password,
  31. opts->connected == SET_PASSWORD_ACTION_FAIL,
  32. opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
  33. } else {
  34. assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
  35. if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
  36. /* vnc supports "connected=keep" only */
  37. error_setg(errp, QERR_INVALID_PARAMETER, "connected");
  38. return;
  39. }
  40. /*
  41. * Note that setting an empty password will not disable login
  42. * through this interface.
  43. */
  44. rc = vnc_display_password(opts->u.vnc.display, opts->password);
  45. }
  46. if (rc != 0) {
  47. error_setg(errp, "Could not set password");
  48. }
  49. }
  50. void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
  51. {
  52. time_t when;
  53. int rc;
  54. const char *whenstr = opts->time;
  55. const char *numstr = NULL;
  56. uint64_t num;
  57. if (strcmp(whenstr, "now") == 0) {
  58. when = 0;
  59. } else if (strcmp(whenstr, "never") == 0) {
  60. when = TIME_MAX;
  61. } else if (whenstr[0] == '+') {
  62. when = time(NULL);
  63. numstr = whenstr + 1;
  64. } else {
  65. when = 0;
  66. numstr = whenstr;
  67. }
  68. if (numstr) {
  69. if (qemu_strtou64(numstr, NULL, 10, &num) < 0) {
  70. error_setg(errp, "Parameter 'time' doesn't take value '%s'",
  71. whenstr);
  72. return;
  73. }
  74. when += num;
  75. }
  76. if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
  77. if (!qemu_using_spice(errp)) {
  78. return;
  79. }
  80. rc = qemu_spice.set_pw_expire(when);
  81. } else {
  82. assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
  83. rc = vnc_display_pw_expire(opts->u.vnc.display, when);
  84. }
  85. if (rc != 0) {
  86. error_setg(errp, "Could not set password expire time");
  87. }
  88. }
  89. #ifdef CONFIG_VNC
  90. void qmp_change_vnc_password(const char *password, Error **errp)
  91. {
  92. if (vnc_display_password(NULL, password) < 0) {
  93. error_setg(errp, "Could not set password");
  94. }
  95. }
  96. #endif
  97. bool qmp_add_client_spice(int fd, bool has_skipauth, bool skipauth,
  98. bool has_tls, bool tls, Error **errp)
  99. {
  100. if (!qemu_using_spice(errp)) {
  101. return false;
  102. }
  103. skipauth = has_skipauth ? skipauth : false;
  104. tls = has_tls ? tls : false;
  105. if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
  106. error_setg(errp, "spice failed to add client");
  107. return false;
  108. }
  109. return true;
  110. }
  111. #ifdef CONFIG_VNC
  112. bool qmp_add_client_vnc(int fd, bool has_skipauth, bool skipauth,
  113. bool has_tls, bool tls, Error **errp)
  114. {
  115. skipauth = has_skipauth ? skipauth : false;
  116. vnc_display_add_client(NULL, fd, skipauth);
  117. return true;
  118. }
  119. #endif
  120. #ifdef CONFIG_DBUS_DISPLAY
  121. bool qmp_add_client_dbus_display(int fd, bool has_skipauth, bool skipauth,
  122. bool has_tls, bool tls, Error **errp)
  123. {
  124. if (!qemu_using_dbus_display(errp)) {
  125. return false;
  126. }
  127. if (!qemu_dbus_display.add_client(fd, errp)) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. #endif
  133. void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
  134. {
  135. switch (arg->type) {
  136. case DISPLAY_RELOAD_TYPE_VNC:
  137. #ifdef CONFIG_VNC
  138. if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
  139. vnc_display_reload_certs(NULL, errp);
  140. }
  141. #else
  142. error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
  143. #endif
  144. break;
  145. default:
  146. abort();
  147. }
  148. }
  149. void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
  150. {
  151. switch (arg->type) {
  152. case DISPLAY_UPDATE_TYPE_VNC:
  153. #ifdef CONFIG_VNC
  154. vnc_display_update(&arg->u.vnc, errp);
  155. #else
  156. error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
  157. #endif
  158. break;
  159. default:
  160. abort();
  161. }
  162. }