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