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