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