aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU VNC display driver: SASL auth protocol |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat, Inc |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 27 | #include "authz/base.h" |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 28 | #include "vnc.h" |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 29 | #include "trace.h" |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 30 | |
Paolo Bonzini | b65310a | 2021-06-04 14:09:15 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Apple has deprecated sasl.h functions in OS X 10.11. Therefore, |
| 33 | * files that use SASL API need to disable -Wdeprecated-declarations. |
| 34 | */ |
| 35 | #ifdef CONFIG_DARWIN |
| 36 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 37 | #endif |
| 38 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 39 | /* Max amount of data we send/recv for SASL steps to prevent DOS */ |
| 40 | #define SASL_DATA_MAX_LEN (1024 * 1024) |
| 41 | |
| 42 | |
Paolo Bonzini | b65310a | 2021-06-04 14:09:15 +0200 | [diff] [blame] | 43 | bool vnc_sasl_server_init(Error **errp) |
| 44 | { |
| 45 | int saslErr = sasl_server_init(NULL, "qemu"); |
| 46 | |
| 47 | if (saslErr != SASL_OK) { |
| 48 | error_setg(errp, "Failed to initialize SASL auth: %s", |
| 49 | sasl_errstring(saslErr, NULL, NULL)); |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 55 | void vnc_sasl_client_cleanup(VncState *vs) |
| 56 | { |
| 57 | if (vs->sasl.conn) { |
Stefan Weil | ee032ca | 2012-03-08 22:58:06 +0100 | [diff] [blame] | 58 | vs->sasl.runSSF = false; |
| 59 | vs->sasl.wantSSF = false; |
| 60 | vs->sasl.waitWriteSSF = 0; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 61 | vs->sasl.encodedLength = vs->sasl.encodedOffset = 0; |
| 62 | vs->sasl.encoded = NULL; |
Stefan Weil | ae878b1 | 2011-10-07 21:15:20 +0200 | [diff] [blame] | 63 | g_free(vs->sasl.username); |
Markus Armbruster | 302d9d6 | 2011-11-08 13:45:21 +0100 | [diff] [blame] | 64 | g_free(vs->sasl.mechlist); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 65 | vs->sasl.username = vs->sasl.mechlist = NULL; |
| 66 | sasl_dispose(&vs->sasl.conn); |
| 67 | vs->sasl.conn = NULL; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | |
Daniel P. Berrange | 30b80fd | 2017-12-18 19:12:28 +0000 | [diff] [blame] | 72 | size_t vnc_client_write_sasl(VncState *vs) |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 73 | { |
Daniel P. Berrange | 30b80fd | 2017-12-18 19:12:28 +0000 | [diff] [blame] | 74 | size_t ret; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 75 | |
Corentin Chary | bc47d20 | 2010-05-03 14:31:18 +0200 | [diff] [blame] | 76 | VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd " |
| 77 | "Encoded: %p size %d offset %d\n", |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 78 | vs->output.buffer, vs->output.capacity, vs->output.offset, |
| 79 | vs->sasl.encoded, vs->sasl.encodedLength, vs->sasl.encodedOffset); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 80 | |
| 81 | if (!vs->sasl.encoded) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 82 | int err; |
| 83 | err = sasl_encode(vs->sasl.conn, |
| 84 | (char *)vs->output.buffer, |
| 85 | vs->output.offset, |
| 86 | (const char **)&vs->sasl.encoded, |
| 87 | &vs->sasl.encodedLength); |
| 88 | if (err != SASL_OK) |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 89 | return vnc_client_io_error(vs, -1, NULL); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 90 | |
Daniel P. Berrange | 8f61f1c | 2017-12-18 19:12:20 +0000 | [diff] [blame] | 91 | vs->sasl.encodedRawLength = vs->output.offset; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 92 | vs->sasl.encodedOffset = 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ret = vnc_client_write_buf(vs, |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 96 | vs->sasl.encoded + vs->sasl.encodedOffset, |
| 97 | vs->sasl.encodedLength - vs->sasl.encodedOffset); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 98 | if (!ret) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 99 | return 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 100 | |
| 101 | vs->sasl.encodedOffset += ret; |
| 102 | if (vs->sasl.encodedOffset == vs->sasl.encodedLength) { |
Daniel P. Berrangé | d50f09f | 2018-02-05 11:49:38 +0000 | [diff] [blame] | 103 | bool throttled = vs->force_update_offset != 0; |
| 104 | size_t offset; |
Daniel P. Berrange | ada8d2e | 2017-12-18 19:12:25 +0000 | [diff] [blame] | 105 | if (vs->sasl.encodedRawLength >= vs->force_update_offset) { |
| 106 | vs->force_update_offset = 0; |
| 107 | } else { |
| 108 | vs->force_update_offset -= vs->sasl.encodedRawLength; |
| 109 | } |
Daniel P. Berrangé | d50f09f | 2018-02-05 11:49:38 +0000 | [diff] [blame] | 110 | if (throttled && vs->force_update_offset == 0) { |
| 111 | trace_vnc_client_unthrottle_forced(vs, vs->ioc); |
| 112 | } |
| 113 | offset = vs->output.offset; |
Daniel P. Berrangé | 627ebec | 2018-02-01 15:58:41 +0000 | [diff] [blame] | 114 | buffer_advance(&vs->output, vs->sasl.encodedRawLength); |
Daniel P. Berrangé | d50f09f | 2018-02-05 11:49:38 +0000 | [diff] [blame] | 115 | if (offset >= vs->throttle_output_offset && |
| 116 | vs->output.offset < vs->throttle_output_offset) { |
| 117 | trace_vnc_client_unthrottle_incremental(vs, vs->ioc, |
| 118 | vs->output.offset); |
| 119 | } |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 120 | vs->sasl.encoded = NULL; |
| 121 | vs->sasl.encodedOffset = vs->sasl.encodedLength = 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Can't merge this block with one above, because |
| 125 | * someone might have written more unencrypted |
| 126 | * data in vs->output while we were processing |
| 127 | * SASL encoded output |
| 128 | */ |
| 129 | if (vs->output.offset == 0) { |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 130 | if (vs->ioc_tag) { |
| 131 | g_source_remove(vs->ioc_tag); |
| 132 | } |
| 133 | vs->ioc_tag = qio_channel_add_watch( |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 134 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 135 | vnc_client_io, vs, NULL); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | |
Daniel P. Berrange | 30b80fd | 2017-12-18 19:12:28 +0000 | [diff] [blame] | 142 | size_t vnc_client_read_sasl(VncState *vs) |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 143 | { |
Daniel P. Berrange | 30b80fd | 2017-12-18 19:12:28 +0000 | [diff] [blame] | 144 | size_t ret; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 145 | uint8_t encoded[4096]; |
| 146 | const char *decoded; |
| 147 | unsigned int decodedLen; |
| 148 | int err; |
| 149 | |
| 150 | ret = vnc_client_read_buf(vs, encoded, sizeof(encoded)); |
| 151 | if (!ret) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 152 | return 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 153 | |
| 154 | err = sasl_decode(vs->sasl.conn, |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 155 | (char *)encoded, ret, |
| 156 | &decoded, &decodedLen); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 157 | |
| 158 | if (err != SASL_OK) |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 159 | return vnc_client_io_error(vs, -1, NULL); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 160 | VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n", |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 161 | encoded, ret, decoded, decodedLen); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 162 | buffer_reserve(&vs->input, decodedLen); |
| 163 | buffer_append(&vs->input, decoded, decodedLen); |
| 164 | return decodedLen; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static int vnc_auth_sasl_check_access(VncState *vs) |
| 169 | { |
| 170 | const void *val; |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 171 | int rv; |
| 172 | Error *err = NULL; |
| 173 | bool allow; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 174 | |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 175 | rv = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val); |
| 176 | if (rv != SASL_OK) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 177 | trace_vnc_auth_fail(vs, vs->auth, "Cannot fetch SASL username", |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 178 | sasl_errstring(rv, NULL, NULL)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 179 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 180 | } |
| 181 | if (val == NULL) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 182 | trace_vnc_auth_fail(vs, vs->auth, "No SASL username set", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 183 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 184 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 185 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 186 | vs->sasl.username = g_strdup((const char*)val); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 187 | trace_vnc_auth_sasl_username(vs, vs->sasl.username); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 188 | |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 189 | if (vs->vd->sasl.authzid == NULL) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 190 | trace_vnc_auth_sasl_acl(vs, 1); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 191 | return 0; |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 194 | allow = qauthz_is_allowed_by_id(vs->vd->sasl.authzid, |
| 195 | vs->sasl.username, &err); |
| 196 | if (err) { |
| 197 | trace_vnc_auth_fail(vs, vs->auth, "Error from authz", |
| 198 | error_get_pretty(err)); |
| 199 | error_free(err); |
| 200 | return -1; |
| 201 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 202 | |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 203 | trace_vnc_auth_sasl_acl(vs, allow); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 204 | return allow ? 0 : -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int vnc_auth_sasl_check_ssf(VncState *vs) |
| 208 | { |
| 209 | const void *val; |
| 210 | int err, ssf; |
| 211 | |
| 212 | if (!vs->sasl.wantSSF) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 213 | return 1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 214 | |
| 215 | err = sasl_getprop(vs->sasl.conn, SASL_SSF, &val); |
| 216 | if (err != SASL_OK) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 217 | return 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 218 | |
| 219 | ssf = *(const int *)val; |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 220 | |
| 221 | trace_vnc_auth_sasl_ssf(vs, ssf); |
| 222 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 223 | if (ssf < 56) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 224 | return 0; /* 56 is good for Kerberos */ |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 225 | |
| 226 | /* Only setup for read initially, because we're about to send an RPC |
| 227 | * reply which must be in plain text. When the next incoming RPC |
| 228 | * arrives, we'll switch on writes too |
| 229 | * |
| 230 | * cf qemudClientReadSASL in qemud.c |
| 231 | */ |
| 232 | vs->sasl.runSSF = 1; |
| 233 | |
| 234 | /* We have a SSF that's good enough */ |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Step Msg |
| 240 | * |
| 241 | * Input from client: |
| 242 | * |
| 243 | * u32 clientin-length |
| 244 | * u8-array clientin-string |
| 245 | * |
| 246 | * Output to client: |
| 247 | * |
| 248 | * u32 serverout-length |
| 249 | * u8-array serverout-strin |
| 250 | * u8 continue |
| 251 | */ |
| 252 | |
| 253 | static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len); |
| 254 | |
| 255 | static int protocol_client_auth_sasl_step(VncState *vs, uint8_t *data, size_t len) |
| 256 | { |
| 257 | uint32_t datalen = len; |
| 258 | const char *serverout; |
| 259 | unsigned int serveroutlen; |
| 260 | int err; |
| 261 | char *clientdata = NULL; |
| 262 | |
| 263 | /* NB, distinction of NULL vs "" is *critical* in SASL */ |
| 264 | if (datalen) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 265 | clientdata = (char*)data; |
| 266 | clientdata[datalen-1] = '\0'; /* Wire includes '\0', but make sure */ |
| 267 | datalen--; /* Don't count NULL byte when passing to _start() */ |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 268 | } |
| 269 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 270 | err = sasl_server_step(vs->sasl.conn, |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 271 | clientdata, |
| 272 | datalen, |
| 273 | &serverout, |
| 274 | &serveroutlen); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 275 | trace_vnc_auth_sasl_step(vs, data, len, serverout, serveroutlen, err); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 276 | if (err != SASL_OK && |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 277 | err != SASL_CONTINUE) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 278 | trace_vnc_auth_fail(vs, vs->auth, "Cannot step SASL auth", |
| 279 | sasl_errdetail(vs->sasl.conn)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 280 | sasl_dispose(&vs->sasl.conn); |
| 281 | vs->sasl.conn = NULL; |
| 282 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (serveroutlen > SASL_DATA_MAX_LEN) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 286 | trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 287 | sasl_dispose(&vs->sasl.conn); |
| 288 | vs->sasl.conn = NULL; |
| 289 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 292 | if (serveroutlen) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 293 | vnc_write_u32(vs, serveroutlen + 1); |
| 294 | vnc_write(vs, serverout, serveroutlen + 1); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 295 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 296 | vnc_write_u32(vs, 0); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Whether auth is complete */ |
| 300 | vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1); |
| 301 | |
| 302 | if (err == SASL_CONTINUE) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 303 | /* Wait for step length */ |
| 304 | vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 305 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 306 | if (!vnc_auth_sasl_check_ssf(vs)) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 307 | trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 308 | goto authreject; |
| 309 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 310 | |
Philippe Mathieu-Daudé | 75ae7c4 | 2021-03-03 19:46:40 +0100 | [diff] [blame] | 311 | /* Check the username access control list */ |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 312 | if (vnc_auth_sasl_check_access(vs) < 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 313 | goto authreject; |
| 314 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 315 | |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 316 | trace_vnc_auth_pass(vs, vs->auth); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 317 | vnc_write_u32(vs, 0); /* Accept auth */ |
| 318 | /* |
| 319 | * Delay writing in SSF encoded mode until pending output |
| 320 | * buffer is written |
| 321 | */ |
| 322 | if (vs->sasl.runSSF) |
| 323 | vs->sasl.waitWriteSSF = vs->output.offset; |
| 324 | start_client_init(vs); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | |
| 329 | authreject: |
| 330 | vnc_write_u32(vs, 1); /* Reject auth */ |
| 331 | vnc_write_u32(vs, sizeof("Authentication failed")); |
| 332 | vnc_write(vs, "Authentication failed", sizeof("Authentication failed")); |
| 333 | vnc_flush(vs); |
| 334 | vnc_client_error(vs); |
| 335 | return -1; |
| 336 | |
| 337 | authabort: |
| 338 | vnc_client_error(vs); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len) |
| 343 | { |
| 344 | uint32_t steplen = read_u32(data, 0); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 345 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 346 | if (steplen > SASL_DATA_MAX_LEN) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 347 | trace_vnc_auth_fail(vs, vs->auth, "SASL step len too large", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 348 | vnc_client_error(vs); |
| 349 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (steplen == 0) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 353 | return protocol_client_auth_sasl_step(vs, NULL, 0); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 354 | else |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 355 | vnc_read_when(vs, protocol_client_auth_sasl_step, steplen); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Start Msg |
| 361 | * |
| 362 | * Input from client: |
| 363 | * |
| 364 | * u32 clientin-length |
| 365 | * u8-array clientin-string |
| 366 | * |
| 367 | * Output to client: |
| 368 | * |
| 369 | * u32 serverout-length |
| 370 | * u8-array serverout-strin |
| 371 | * u8 continue |
| 372 | */ |
| 373 | |
| 374 | #define SASL_DATA_MAX_LEN (1024 * 1024) |
| 375 | |
| 376 | static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len) |
| 377 | { |
| 378 | uint32_t datalen = len; |
| 379 | const char *serverout; |
| 380 | unsigned int serveroutlen; |
| 381 | int err; |
| 382 | char *clientdata = NULL; |
| 383 | |
| 384 | /* NB, distinction of NULL vs "" is *critical* in SASL */ |
| 385 | if (datalen) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 386 | clientdata = (char*)data; |
| 387 | clientdata[datalen-1] = '\0'; /* Should be on wire, but make sure */ |
| 388 | datalen--; /* Don't count NULL byte when passing to _start() */ |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 389 | } |
| 390 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 391 | err = sasl_server_start(vs->sasl.conn, |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 392 | vs->sasl.mechlist, |
| 393 | clientdata, |
| 394 | datalen, |
| 395 | &serverout, |
| 396 | &serveroutlen); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 397 | trace_vnc_auth_sasl_start(vs, data, len, serverout, serveroutlen, err); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 398 | if (err != SASL_OK && |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 399 | err != SASL_CONTINUE) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 400 | trace_vnc_auth_fail(vs, vs->auth, "Cannot start SASL auth", |
| 401 | sasl_errdetail(vs->sasl.conn)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 402 | sasl_dispose(&vs->sasl.conn); |
| 403 | vs->sasl.conn = NULL; |
| 404 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 405 | } |
| 406 | if (serveroutlen > SASL_DATA_MAX_LEN) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 407 | trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 408 | sasl_dispose(&vs->sasl.conn); |
| 409 | vs->sasl.conn = NULL; |
| 410 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 411 | } |
| 412 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 413 | if (serveroutlen) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 414 | vnc_write_u32(vs, serveroutlen + 1); |
| 415 | vnc_write(vs, serverout, serveroutlen + 1); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 416 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 417 | vnc_write_u32(vs, 0); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Whether auth is complete */ |
| 421 | vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1); |
| 422 | |
| 423 | if (err == SASL_CONTINUE) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 424 | /* Wait for step length */ |
| 425 | vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 426 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 427 | if (!vnc_auth_sasl_check_ssf(vs)) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 428 | trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 429 | goto authreject; |
| 430 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 431 | |
Philippe Mathieu-Daudé | 75ae7c4 | 2021-03-03 19:46:40 +0100 | [diff] [blame] | 432 | /* Check the username access control list */ |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 433 | if (vnc_auth_sasl_check_access(vs) < 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 434 | goto authreject; |
| 435 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 436 | |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 437 | trace_vnc_auth_pass(vs, vs->auth); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 438 | vnc_write_u32(vs, 0); /* Accept auth */ |
| 439 | start_client_init(vs); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | |
| 444 | authreject: |
| 445 | vnc_write_u32(vs, 1); /* Reject auth */ |
| 446 | vnc_write_u32(vs, sizeof("Authentication failed")); |
| 447 | vnc_write(vs, "Authentication failed", sizeof("Authentication failed")); |
| 448 | vnc_flush(vs); |
| 449 | vnc_client_error(vs); |
| 450 | return -1; |
| 451 | |
| 452 | authabort: |
| 453 | vnc_client_error(vs); |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len) |
| 458 | { |
| 459 | uint32_t startlen = read_u32(data, 0); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 460 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 461 | if (startlen > SASL_DATA_MAX_LEN) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 462 | trace_vnc_auth_fail(vs, vs->auth, "SASL start len too large", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 463 | vnc_client_error(vs); |
| 464 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (startlen == 0) |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 468 | return protocol_client_auth_sasl_start(vs, NULL, 0); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 469 | |
| 470 | vnc_read_when(vs, protocol_client_auth_sasl_start, startlen); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len) |
| 475 | { |
Jim Meyering | 5847d9e | 2012-10-04 13:09:54 +0200 | [diff] [blame] | 476 | char *mechname = g_strndup((const char *) data, len); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 477 | trace_vnc_auth_sasl_mech_choose(vs, mechname); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 478 | |
| 479 | if (strncmp(vs->sasl.mechlist, mechname, len) == 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 480 | if (vs->sasl.mechlist[len] != '\0' && |
| 481 | vs->sasl.mechlist[len] != ',') { |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 482 | goto fail; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 483 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 484 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 485 | char *offset = strstr(vs->sasl.mechlist, mechname); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 486 | if (!offset) { |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 487 | goto fail; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 488 | } |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 489 | if (offset[-1] != ',' || |
| 490 | (offset[len] != '\0'&& |
| 491 | offset[len] != ',')) { |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 492 | goto fail; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 493 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Markus Armbruster | 302d9d6 | 2011-11-08 13:45:21 +0100 | [diff] [blame] | 496 | g_free(vs->sasl.mechlist); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 497 | vs->sasl.mechlist = mechname; |
| 498 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 499 | vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4); |
| 500 | return 0; |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 501 | |
| 502 | fail: |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 503 | trace_vnc_auth_fail(vs, vs->auth, "Unsupported mechname", mechname); |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 504 | vnc_client_error(vs); |
Markus Armbruster | 302d9d6 | 2011-11-08 13:45:21 +0100 | [diff] [blame] | 505 | g_free(mechname); |
Blue Swirl | 8ce7d35 | 2011-01-12 19:48:56 +0000 | [diff] [blame] | 506 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len) |
| 510 | { |
| 511 | uint32_t mechlen = read_u32(data, 0); |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 512 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 513 | if (mechlen > 100) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 514 | trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too long", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 515 | vnc_client_error(vs); |
| 516 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 517 | } |
| 518 | if (mechlen < 1) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 519 | trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too short", ""); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 520 | vnc_client_error(vs); |
| 521 | return -1; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 522 | } |
| 523 | vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen); |
| 524 | return 0; |
| 525 | } |
| 526 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 527 | static char * |
| 528 | vnc_socket_ip_addr_string(QIOChannelSocket *ioc, |
| 529 | bool local, |
| 530 | Error **errp) |
| 531 | { |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 532 | SocketAddress *addr; |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 533 | char *ret; |
| 534 | |
| 535 | if (local) { |
| 536 | addr = qio_channel_socket_get_local_address(ioc, errp); |
| 537 | } else { |
| 538 | addr = qio_channel_socket_get_remote_address(ioc, errp); |
| 539 | } |
| 540 | if (!addr) { |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 544 | if (addr->type != SOCKET_ADDRESS_TYPE_INET) { |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 545 | error_setg(errp, "Not an inet socket type"); |
Pan Nengyuan | 7791aca | 2020-08-31 09:43:15 -0400 | [diff] [blame] | 546 | qapi_free_SocketAddress(addr); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 549 | ret = g_strdup_printf("%s;%s", addr->u.inet.host, addr->u.inet.port); |
| 550 | qapi_free_SocketAddress(addr); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 551 | return ret; |
| 552 | } |
| 553 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 554 | void start_auth_sasl(VncState *vs) |
| 555 | { |
| 556 | const char *mechlist = NULL; |
| 557 | sasl_security_properties_t secprops; |
| 558 | int err; |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 559 | Error *local_err = NULL; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 560 | char *localAddr, *remoteAddr; |
| 561 | int mechlistlen; |
| 562 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 563 | /* Get local & remote client addresses in form IPADDR;PORT */ |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 564 | localAddr = vnc_socket_ip_addr_string(vs->sioc, true, &local_err); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 565 | if (!localAddr) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 566 | trace_vnc_auth_fail(vs, vs->auth, "Cannot format local IP", |
| 567 | error_get_pretty(local_err)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 568 | goto authabort; |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 569 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 570 | |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 571 | remoteAddr = vnc_socket_ip_addr_string(vs->sioc, false, &local_err); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 572 | if (!remoteAddr) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 573 | trace_vnc_auth_fail(vs, vs->auth, "Cannot format remote IP", |
| 574 | error_get_pretty(local_err)); |
Stefan Weil | ae878b1 | 2011-10-07 21:15:20 +0200 | [diff] [blame] | 575 | g_free(localAddr); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 576 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | err = sasl_server_new("vnc", |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 580 | NULL, /* FQDN - just delegates to gethostname */ |
| 581 | NULL, /* User realm */ |
| 582 | localAddr, |
| 583 | remoteAddr, |
| 584 | NULL, /* Callbacks, not needed */ |
| 585 | SASL_SUCCESS_DATA, |
| 586 | &vs->sasl.conn); |
Stefan Weil | ae878b1 | 2011-10-07 21:15:20 +0200 | [diff] [blame] | 587 | g_free(localAddr); |
| 588 | g_free(remoteAddr); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 589 | localAddr = remoteAddr = NULL; |
| 590 | |
| 591 | if (err != SASL_OK) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 592 | trace_vnc_auth_fail(vs, vs->auth, "SASL context setup failed", |
| 593 | sasl_errstring(err, NULL, NULL)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 594 | vs->sasl.conn = NULL; |
| 595 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 596 | } |
| 597 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 598 | /* Inform SASL that we've got an external SSF layer from TLS/x509 */ |
Daniel P. Berrange | 7e7e2eb | 2011-06-23 13:31:41 +0100 | [diff] [blame] | 599 | if (vs->auth == VNC_AUTH_VENCRYPT && |
| 600 | vs->subauth == VNC_AUTH_VENCRYPT_X509SASL) { |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 601 | int keysize; |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 602 | sasl_ssf_t ssf; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 603 | |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 604 | keysize = qcrypto_tls_session_get_key_size(vs->tls, |
| 605 | &local_err); |
| 606 | if (keysize < 0) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 607 | trace_vnc_auth_fail(vs, vs->auth, "cannot TLS get cipher size", |
| 608 | error_get_pretty(local_err)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 609 | sasl_dispose(&vs->sasl.conn); |
| 610 | vs->sasl.conn = NULL; |
| 611 | goto authabort; |
| 612 | } |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 613 | ssf = keysize * CHAR_BIT; /* tls key size is bytes, sasl wants bits */ |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 614 | |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 615 | err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf); |
| 616 | if (err != SASL_OK) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 617 | trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL external SSF", |
| 618 | sasl_errstring(err, NULL, NULL)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 619 | sasl_dispose(&vs->sasl.conn); |
| 620 | vs->sasl.conn = NULL; |
| 621 | goto authabort; |
| 622 | } |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 623 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 624 | vs->sasl.wantSSF = 1; |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 625 | } |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 626 | |
| 627 | memset (&secprops, 0, sizeof secprops); |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 628 | /* Inform SASL that we've got an external SSF layer from TLS. |
| 629 | * |
| 630 | * Disable SSF, if using TLS+x509+SASL only. TLS without x509 |
| 631 | * is not sufficiently strong |
| 632 | */ |
| 633 | if (vs->vd->is_unix || |
| 634 | (vs->auth == VNC_AUTH_VENCRYPT && |
| 635 | vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 636 | /* If we've got TLS or UNIX domain sock, we don't care about SSF */ |
| 637 | secprops.min_ssf = 0; |
| 638 | secprops.max_ssf = 0; |
| 639 | secprops.maxbufsize = 8192; |
| 640 | secprops.security_flags = 0; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 641 | } else { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 642 | /* Plain TCP, better get an SSF layer */ |
| 643 | secprops.min_ssf = 56; /* Good enough to require kerberos */ |
| 644 | secprops.max_ssf = 100000; /* Arbitrary big number */ |
| 645 | secprops.maxbufsize = 8192; |
| 646 | /* Forbid any anonymous or trivially crackable auth */ |
| 647 | secprops.security_flags = |
| 648 | SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops); |
| 652 | if (err != SASL_OK) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 653 | trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL security props", |
| 654 | sasl_errstring(err, NULL, NULL)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 655 | sasl_dispose(&vs->sasl.conn); |
| 656 | vs->sasl.conn = NULL; |
| 657 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | err = sasl_listmech(vs->sasl.conn, |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 661 | NULL, /* Don't need to set user */ |
| 662 | "", /* Prefix */ |
| 663 | ",", /* Separator */ |
| 664 | "", /* Suffix */ |
| 665 | &mechlist, |
| 666 | NULL, |
| 667 | NULL); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 668 | if (err != SASL_OK) { |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 669 | trace_vnc_auth_fail(vs, vs->auth, "cannot list SASL mechanisms", |
| 670 | sasl_errdetail(vs->sasl.conn)); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 671 | sasl_dispose(&vs->sasl.conn); |
| 672 | vs->sasl.conn = NULL; |
| 673 | goto authabort; |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 674 | } |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 675 | trace_vnc_auth_sasl_mech_list(vs, mechlist); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 676 | |
Markus Armbruster | 302d9d6 | 2011-11-08 13:45:21 +0100 | [diff] [blame] | 677 | vs->sasl.mechlist = g_strdup(mechlist); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 678 | mechlistlen = strlen(mechlist); |
| 679 | vnc_write_u32(vs, mechlistlen); |
| 680 | vnc_write(vs, mechlist, mechlistlen); |
| 681 | vnc_flush(vs); |
| 682 | |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 683 | vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4); |
| 684 | |
| 685 | return; |
| 686 | |
| 687 | authabort: |
Daniel P. Berrange | 7364dbd | 2017-09-21 13:15:28 +0100 | [diff] [blame] | 688 | error_free(local_err); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 689 | vnc_client_error(vs); |
aliguori | 2f9606b | 2009-03-06 20:27:28 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |