1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Mon, 11 Jan 2016 10:40:31 +0100
4 Subject: [PATCH] vnc: PVE VNC authentication
7 crypto/tlscreds.c | 47 ++++++++++++
8 crypto/tlscredspriv.h | 2 +
9 crypto/tlscredsx509.c | 13 ++--
10 crypto/tlssession.c | 1 +
11 include/crypto/tlscreds.h | 1 +
12 include/ui/console.h | 1 +
13 qapi-schema.json | 1 +
15 ui/vnc-auth-vencrypt.c | 182 ++++++++++++++++++++++++++++++++++++++--------
16 ui/vnc.c | 140 ++++++++++++++++++++++++++++++++++-
19 12 files changed, 364 insertions(+), 40 deletions(-)
21 diff --git a/crypto/tlscreds.c b/crypto/tlscreds.c
22 index 3cd41035bb..e982da3451 100644
23 --- a/crypto/tlscreds.c
24 +++ b/crypto/tlscreds.c
25 @@ -158,6 +158,33 @@ qcrypto_tls_creds_prop_get_verify(Object *obj,
29 +qcrypto_tls_creds_prop_set_pve(Object *obj,
31 + Error **errp G_GNUC_UNUSED)
33 + QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
40 +qcrypto_tls_creds_prop_get_pve(Object *obj,
41 + Error **errp G_GNUC_UNUSED)
43 + QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
48 +bool qcrypto_tls_creds_is_pve(QCryptoTLSCreds *creds)
51 + return qcrypto_tls_creds_prop_get_pve((Object*)creds, &errp);
56 qcrypto_tls_creds_prop_set_dir(Object *obj,
58 Error **errp G_GNUC_UNUSED)
59 @@ -250,6 +277,26 @@ qcrypto_tls_creds_init(Object *obj)
60 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
62 creds->verifyPeer = true;
65 + object_property_add_bool(obj, "verify-peer",
66 + qcrypto_tls_creds_prop_get_verify,
67 + qcrypto_tls_creds_prop_set_verify,
69 + object_property_add_bool(obj, "pve",
70 + qcrypto_tls_creds_prop_get_pve,
71 + qcrypto_tls_creds_prop_set_pve,
73 + object_property_add_str(obj, "dir",
74 + qcrypto_tls_creds_prop_get_dir,
75 + qcrypto_tls_creds_prop_set_dir,
77 + object_property_add_enum(obj, "endpoint",
78 + "QCryptoTLSCredsEndpoint",
79 + &QCryptoTLSCredsEndpoint_lookup,
80 + qcrypto_tls_creds_prop_get_endpoint,
81 + qcrypto_tls_creds_prop_set_endpoint,
86 diff --git a/crypto/tlscredspriv.h b/crypto/tlscredspriv.h
87 index 13e9b6c0b2..0356acc2c9 100644
88 --- a/crypto/tlscredspriv.h
89 +++ b/crypto/tlscredspriv.h
90 @@ -36,6 +36,8 @@ int qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds *creds,
91 gnutls_dh_params_t *dh_params,
94 +bool qcrypto_tls_creds_is_pve(QCryptoTLSCreds *creds);
98 #endif /* QCRYPTO_TLSCREDSPRIV_H */
99 diff --git a/crypto/tlscredsx509.c b/crypto/tlscredsx509.c
100 index 50eb54f6bb..09f7364001 100644
101 --- a/crypto/tlscredsx509.c
102 +++ b/crypto/tlscredsx509.c
103 @@ -555,22 +555,23 @@ qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509 *creds,
104 *key = NULL, *dhparams = NULL;
107 + bool pve = qcrypto_tls_creds_is_pve(&creds->parent_obj);
109 trace_qcrypto_tls_creds_x509_load(creds,
110 creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
112 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
113 if (qcrypto_tls_creds_get_path(&creds->parent_obj,
114 - QCRYPTO_TLS_CREDS_X509_CA_CERT,
115 + pve ? "pve-root-ca.pem" : QCRYPTO_TLS_CREDS_X509_CA_CERT,
116 true, &cacert, errp) < 0 ||
117 qcrypto_tls_creds_get_path(&creds->parent_obj,
118 QCRYPTO_TLS_CREDS_X509_CA_CRL,
119 false, &cacrl, errp) < 0 ||
120 qcrypto_tls_creds_get_path(&creds->parent_obj,
121 - QCRYPTO_TLS_CREDS_X509_SERVER_CERT,
122 + pve ? "local/pve-ssl.pem" : QCRYPTO_TLS_CREDS_X509_SERVER_CERT,
123 true, &cert, errp) < 0 ||
124 qcrypto_tls_creds_get_path(&creds->parent_obj,
125 - QCRYPTO_TLS_CREDS_X509_SERVER_KEY,
126 + pve ? "local/pve-ssl.key" : QCRYPTO_TLS_CREDS_X509_SERVER_KEY,
127 true, &key, errp) < 0 ||
128 qcrypto_tls_creds_get_path(&creds->parent_obj,
129 QCRYPTO_TLS_CREDS_DH_PARAMS,
130 @@ -579,13 +580,13 @@ qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509 *creds,
133 if (qcrypto_tls_creds_get_path(&creds->parent_obj,
134 - QCRYPTO_TLS_CREDS_X509_CA_CERT,
135 + pve ? "pve-root-ca.pem" : QCRYPTO_TLS_CREDS_X509_CA_CERT,
136 true, &cacert, errp) < 0 ||
137 qcrypto_tls_creds_get_path(&creds->parent_obj,
138 - QCRYPTO_TLS_CREDS_X509_CLIENT_CERT,
139 + pve ? "local/pve-ssl.pem" : QCRYPTO_TLS_CREDS_X509_CLIENT_CERT,
140 false, &cert, errp) < 0 ||
141 qcrypto_tls_creds_get_path(&creds->parent_obj,
142 - QCRYPTO_TLS_CREDS_X509_CLIENT_KEY,
143 + pve ? "local/pve-ssl.key" : QCRYPTO_TLS_CREDS_X509_CLIENT_KEY,
144 false, &key, errp) < 0) {
147 diff --git a/crypto/tlssession.c b/crypto/tlssession.c
148 index 96a02deb69..c453e29cad 100644
149 --- a/crypto/tlssession.c
150 +++ b/crypto/tlssession.c
152 #include "crypto/tlscredsanon.h"
153 #include "crypto/tlscredsx509.h"
154 #include "qapi/error.h"
155 +#include "crypto/tlscredspriv.h"
156 #include "qemu/acl.h"
159 diff --git a/include/crypto/tlscreds.h b/include/crypto/tlscreds.h
160 index ad47d88be7..f86d379f26 100644
161 --- a/include/crypto/tlscreds.h
162 +++ b/include/crypto/tlscreds.h
163 @@ -55,6 +55,7 @@ struct QCryptoTLSCreds {
171 diff --git a/include/ui/console.h b/include/ui/console.h
172 index 580dfc57ee..383e5c88bd 100644
173 --- a/include/ui/console.h
174 +++ b/include/ui/console.h
175 @@ -466,6 +466,7 @@ static inline void cocoa_display_init(DisplayState *ds, int full_screen)
179 +void pve_auth_setup(int vmid);
180 void vnc_display_init(const char *id);
181 void vnc_display_open(const char *id, Error **errp);
182 void vnc_display_add_client(const char *id, int csock, bool skipauth);
183 diff --git a/qapi-schema.json b/qapi-schema.json
184 index 348b527681..d2155cb00f 100644
185 --- a/qapi-schema.json
186 +++ b/qapi-schema.json
189 # Commands allowed to return a non-dictionary:
190 'returns-whitelist': [
192 'human-monitor-command',
194 'query-migrate-cache-size',
195 diff --git a/qemu-options.hx b/qemu-options.hx
196 index 7c054af8f9..07129d55bc 100644
197 --- a/qemu-options.hx
198 +++ b/qemu-options.hx
199 @@ -583,6 +583,9 @@ STEXI
203 +DEF("id", HAS_ARG, QEMU_OPTION_id,
204 + "-id n set the VMID\n", QEMU_ARCH_ALL)
206 DEF("fda", HAS_ARG, QEMU_OPTION_fda,
207 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n", QEMU_ARCH_ALL)
208 DEF("fdb", HAS_ARG, QEMU_OPTION_fdb, "", QEMU_ARCH_ALL)
209 diff --git a/ui/vnc-auth-vencrypt.c b/ui/vnc-auth-vencrypt.c
210 index 7833631275..c42acd3714 100644
211 --- a/ui/vnc-auth-vencrypt.c
212 +++ b/ui/vnc-auth-vencrypt.c
214 #include "qapi/error.h"
215 #include "qemu/main-loop.h"
217 +#include "io/channel-socket.h"
219 +static int protocol_client_auth_plain(VncState *vs, uint8_t *data, size_t len)
222 + char username[256];
225 + SocketAddress *clientip = qio_channel_socket_get_remote_address(vs->sioc, &err);
230 + if ((len != (vs->username_len + vs->password_len)) ||
231 + (vs->username_len >= (sizeof(username)-1)) ||
232 + (vs->password_len >= (sizeof(passwd)-1)) ) {
233 + error_setg(&err, "Got unexpected data length");
237 + strncpy(username, (char *)data, vs->username_len);
238 + username[vs->username_len] = 0;
239 + strncpy(passwd, (char *)data + vs->username_len, vs->password_len);
240 + passwd[vs->password_len] = 0;
242 + VNC_DEBUG("AUTH PLAIN username: %s pw: %s\n", username, passwd);
244 + if (pve_auth_verify(clientip->u.inet.host, username, passwd) == 0) {
245 + vnc_write_u32(vs, 0); /* Accept auth completion */
246 + start_client_init(vs);
247 + qapi_free_SocketAddress(clientip);
251 + error_setg(&err, "Authentication failed");
254 + const char *err_msg = error_get_pretty(err);
255 + VNC_DEBUG("AUTH PLAIN ERROR: %s\n", err_msg);
256 + vnc_write_u32(vs, 1); /* Reject auth */
257 + if (vs->minor >= 8) {
258 + int elen = strlen(err_msg);
259 + vnc_write_u32(vs, elen);
260 + vnc_write(vs, err_msg, elen);
265 + vnc_client_error(vs);
267 + qapi_free_SocketAddress(clientip);
273 +static int protocol_client_auth_plain_start(VncState *vs, uint8_t *data, size_t len)
275 + uint32_t ulen = read_u32(data, 0);
276 + uint32_t pwlen = read_u32(data, 4);
277 + const char *err = NULL;
279 + VNC_DEBUG("AUTH PLAIN START %u %u\n", ulen, pwlen);
282 + err = "No User name.";
286 + err = "User name too long.";
290 + err = "Password too short";
293 + if (pwlen >= 511) {
294 + err = "Password too long.";
298 + vs->username_len = ulen;
299 + vs->password_len = pwlen;
301 + vnc_read_when(vs, protocol_client_auth_plain, ulen + pwlen);
306 + VNC_DEBUG("AUTH PLAIN ERROR: %s\n", err);
307 + vnc_write_u32(vs, 1); /* Reject auth */
308 + if (vs->minor >= 8) {
309 + int elen = strlen(err);
310 + vnc_write_u32(vs, elen);
311 + vnc_write(vs, err, elen);
315 + vnc_client_error(vs);
320 static void start_auth_vencrypt_subauth(VncState *vs)
322 @@ -39,6 +141,17 @@ static void start_auth_vencrypt_subauth(VncState *vs)
323 start_client_init(vs);
326 + case VNC_AUTH_VENCRYPT_TLSPLAIN:
327 + case VNC_AUTH_VENCRYPT_X509PLAIN:
328 + VNC_DEBUG("Start TLS auth PLAIN\n");
329 + vnc_read_when(vs, protocol_client_auth_plain_start, 8);
332 + case VNC_AUTH_VENCRYPT_PLAIN:
333 + VNC_DEBUG("Start auth PLAIN\n");
334 + vnc_read_when(vs, protocol_client_auth_plain_start, 8);
337 case VNC_AUTH_VENCRYPT_TLSVNC:
338 case VNC_AUTH_VENCRYPT_X509VNC:
340 @@ -90,45 +203,51 @@ static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len
341 int auth = read_u32(data, 0);
343 trace_vnc_auth_vencrypt_subauth(vs, auth);
344 - if (auth != vs->subauth) {
345 + if (auth != vs->subauth && auth != VNC_AUTH_VENCRYPT_PLAIN) {
346 trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
347 vnc_write_u8(vs, 0); /* Reject auth */
349 vnc_client_error(vs);
352 - QIOChannelTLS *tls;
353 - vnc_write_u8(vs, 1); /* Accept auth */
357 - g_source_remove(vs->ioc_tag);
359 + if (auth == VNC_AUTH_VENCRYPT_PLAIN) {
360 + vs->subauth = auth;
361 + start_auth_vencrypt_subauth(vs);
366 + QIOChannelTLS *tls;
367 + vnc_write_u8(vs, 1); /* Accept auth */
370 - tls = qio_channel_tls_new_server(
373 - vs->vd->tlsaclname,
376 - trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
377 - error_get_pretty(err));
379 - vnc_client_error(vs);
383 + g_source_remove(vs->ioc_tag);
386 + tls = qio_channel_tls_new_server(
389 + vs->vd->tlsaclname,
392 + trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
393 + error_get_pretty(err));
395 + vnc_client_error(vs);
399 - qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
400 - object_unref(OBJECT(vs->ioc));
401 - vs->ioc = QIO_CHANNEL(tls);
402 - trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
403 - vs->tls = qio_channel_tls_get_session(tls);
404 + qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
405 + object_unref(OBJECT(vs->ioc));
406 + vs->ioc = QIO_CHANNEL(tls);
407 + trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
408 + vs->tls = qio_channel_tls_get_session(tls);
410 - qio_channel_tls_handshake(tls,
411 - vnc_tls_handshake_done,
414 + qio_channel_tls_handshake(tls,
415 + vnc_tls_handshake_done,
422 @@ -144,8 +263,9 @@ static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len
423 vnc_client_error(vs);
425 vnc_write_u8(vs, 0); /* Accept version */
426 - vnc_write_u8(vs, 1); /* Number of sub-auths */
427 + vnc_write_u8(vs, 2); /* Number of sub-auths */
428 vnc_write_u32(vs, vs->subauth); /* The supported auth */
429 + vnc_write_u32(vs, VNC_AUTH_VENCRYPT_PLAIN); /* Alternative supported auth */
431 vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
433 diff --git a/ui/vnc.c b/ui/vnc.c
434 index 4494cb1dd4..1589cbe1b3 100644
437 @@ -55,6 +55,125 @@ static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
438 #include "vnc_keysym.h"
439 #include "crypto/cipher.h"
441 +static int pve_vmid = 0;
443 +void pve_auth_setup(int vmid) {
448 +urlencode(char *buf, const char *value)
450 + static const char *hexchar = "0123456789abcdef";
453 + int l = strlen(value);
454 + for (i = 0; i < l; i++) {
456 + if (('a' <= c && c <= 'z') ||
457 + ('A' <= c && c <= 'Z') ||
458 + ('0' <= c && c <= '9')) {
460 + } else if (c == 32) {
464 + *p++ = hexchar[c >> 4];
465 + *p++ = hexchar[c & 15];
474 +pve_auth_verify(const char *clientip, const char *username, const char *passwd)
476 + struct sockaddr_in server;
478 + int sfd = socket(AF_INET, SOCK_STREAM, 0);
480 + perror("pve_auth_verify: socket failed");
484 + struct hostent *he;
485 + if ((he = gethostbyname("localhost")) == NULL) {
486 + fprintf(stderr, "pve_auth_verify: error resolving hostname\n");
490 + memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
491 + server.sin_family = AF_INET;
492 + server.sin_port = htons(85);
494 + if (connect(sfd, (struct sockaddr *)&server, sizeof(server))) {
495 + perror("pve_auth_verify: error connecting to server");
503 + p = urlencode(p, "username");
505 + p = urlencode(p, username);
508 + p = urlencode(p, "password");
510 + p = urlencode(p, passwd);
513 + p = urlencode(p, "path");
515 + char authpath[256];
516 + sprintf(authpath, "/vms/%d", pve_vmid);
517 + p = urlencode(p, authpath);
520 + p = urlencode(p, "privs");
522 + p = urlencode(p, "VM.Console");
524 + sprintf(buf, "POST /api2/json/access/ticket HTTP/1.1\n"
525 + "Host: localhost:85\n"
526 + "Connection: close\n"
527 + "PVEClientIP: %s\n"
528 + "Content-Type: application/x-www-form-urlencoded\n"
529 + "Content-Length: %zd\n\n%s\n", clientip, strlen(form), form);
530 + ssize_t len = strlen(buf);
531 + ssize_t sb = send(sfd, buf, len, 0);
533 + perror("pve_auth_verify: send failed");
537 + fprintf(stderr, "pve_auth_verify: partial send error\n");
541 + len = recv(sfd, buf, sizeof(buf) - 1, 0);
543 + perror("pve_auth_verify: recv failed");
549 + //printf("DATA:%s\n", buf);
551 + shutdown(sfd, SHUT_RDWR);
553 + return strncmp(buf, "HTTP/1.1 200 OK", 15);
556 + shutdown(sfd, SHUT_RDWR);
560 static QTAILQ_HEAD(, VncDisplay) vnc_displays =
561 QTAILQ_HEAD_INITIALIZER(vnc_displays);
563 @@ -3507,10 +3626,16 @@ vnc_display_setup_auth(int *auth,
566 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
567 - *subauth = VNC_AUTH_VENCRYPT_X509VNC;
569 + *subauth = VNC_AUTH_VENCRYPT_X509PLAIN;
571 + *subauth = VNC_AUTH_VENCRYPT_X509VNC;
573 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
574 - *subauth = VNC_AUTH_VENCRYPT_TLSVNC;
576 + *subauth = VNC_AUTH_VENCRYPT_TLSPLAIN;
578 + *subauth = VNC_AUTH_VENCRYPT_TLSVNC;
582 @@ -3544,6 +3669,7 @@ vnc_display_create_creds(bool x509,
589 gchar *credsid = g_strdup_printf("tlsvnc%s", id);
590 @@ -3559,6 +3685,7 @@ vnc_display_create_creds(bool x509,
591 "endpoint", "server",
593 "verify-peer", x509verify ? "yes" : "no",
594 + "pve", pve ? "yes" : "no",
597 creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON,
598 @@ -3566,6 +3693,7 @@ vnc_display_create_creds(bool x509,
601 "endpoint", "server",
602 + "pve", pve ? "yes" : "no",
606 @@ -4032,12 +4160,17 @@ void vnc_display_open(const char *id, Error **errp)
610 - bool tls = false, x509 = false, x509verify = false;
611 + bool tls = false, x509 = false, x509verify = false, pve = false;
612 tls = qemu_opt_get_bool(opts, "tls", false);
613 path = qemu_opt_get(opts, "x509");
617 + if (!strcmp(path, "on")) {
618 + /* magic to default to /etc/pve */
623 path = qemu_opt_get(opts, "x509verify");
625 @@ -4049,6 +4182,7 @@ void vnc_display_open(const char *id, Error **errp)
633 diff --git a/ui/vnc.h b/ui/vnc.h
634 index bbda0540a7..8cc6367ed3 100644
637 @@ -290,6 +290,8 @@ struct VncState
639 int subauth; /* Used by VeNCrypt */
640 char challenge[VNC_AUTH_CHALLENGE_SIZE];
643 QCryptoTLSSession *tls; /* Borrowed pointer from channel, don't free */
644 #ifdef CONFIG_VNC_SASL
646 @@ -595,4 +597,6 @@ int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
647 int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
648 void vnc_zrle_clear(VncState *vs);
650 +int pve_auth_verify(const char *clientip, const char *username, const char *passwd);
652 #endif /* QEMU_VNC_H */
653 diff --git a/vl.c b/vl.c
654 index 75fde82180..255d989009 100644
657 @@ -3096,6 +3096,7 @@ static void register_global_properties(MachineState *ms)
658 int main(int argc, char **argv, char **envp)
661 + long int vm_id_long = 0;
662 int snapshot, linux_boot;
663 const char *initrd_filename;
664 const char *kernel_filename, *kernel_cmdline;
665 @@ -3922,6 +3923,14 @@ int main(int argc, char **argv, char **envp)
669 + case QEMU_OPTION_id:
670 + vm_id_long = strtol(optarg, (char **) &optarg, 10);
671 + if (*optarg != 0 || vm_id_long < 100 || vm_id_long > INT_MAX) {
672 + fprintf(stderr, "Invalid ID\n");
675 + pve_auth_setup(vm_id_long);
677 case QEMU_OPTION_vnc:
678 vnc_parse(optarg, &error_fatal);