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