blob: 3ade4a49185f58574d53bd7e2a493c1d230c4b88 [file] [log] [blame]
aliguori2f9606b2009-03-06 20:27:28 +00001/*
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 Maydelle16f4c82016-01-29 17:49:51 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
aliguori2f9606b2009-03-06 20:27:28 +000027#include "vnc.h"
28
29/* Max amount of data we send/recv for SASL steps to prevent DOS */
30#define SASL_DATA_MAX_LEN (1024 * 1024)
31
32
33void vnc_sasl_client_cleanup(VncState *vs)
34{
35 if (vs->sasl.conn) {
Stefan Weilee032ca2012-03-08 22:58:06 +010036 vs->sasl.runSSF = false;
37 vs->sasl.wantSSF = false;
38 vs->sasl.waitWriteSSF = 0;
aliguori28a76be2009-03-06 20:27:40 +000039 vs->sasl.encodedLength = vs->sasl.encodedOffset = 0;
40 vs->sasl.encoded = NULL;
Stefan Weilae878b12011-10-07 21:15:20 +020041 g_free(vs->sasl.username);
Markus Armbruster302d9d62011-11-08 13:45:21 +010042 g_free(vs->sasl.mechlist);
aliguori28a76be2009-03-06 20:27:40 +000043 vs->sasl.username = vs->sasl.mechlist = NULL;
44 sasl_dispose(&vs->sasl.conn);
45 vs->sasl.conn = NULL;
aliguori2f9606b2009-03-06 20:27:28 +000046 }
47}
48
49
50long vnc_client_write_sasl(VncState *vs)
51{
52 long ret;
53
Corentin Charybc47d202010-05-03 14:31:18 +020054 VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd "
55 "Encoded: %p size %d offset %d\n",
aliguori28a76be2009-03-06 20:27:40 +000056 vs->output.buffer, vs->output.capacity, vs->output.offset,
57 vs->sasl.encoded, vs->sasl.encodedLength, vs->sasl.encodedOffset);
aliguori2f9606b2009-03-06 20:27:28 +000058
59 if (!vs->sasl.encoded) {
aliguori28a76be2009-03-06 20:27:40 +000060 int err;
61 err = sasl_encode(vs->sasl.conn,
62 (char *)vs->output.buffer,
63 vs->output.offset,
64 (const char **)&vs->sasl.encoded,
65 &vs->sasl.encodedLength);
66 if (err != SASL_OK)
Daniel P. Berrange04d25292015-02-27 16:20:57 +000067 return vnc_client_io_error(vs, -1, NULL);
aliguori2f9606b2009-03-06 20:27:28 +000068
aliguori28a76be2009-03-06 20:27:40 +000069 vs->sasl.encodedOffset = 0;
aliguori2f9606b2009-03-06 20:27:28 +000070 }
71
72 ret = vnc_client_write_buf(vs,
aliguori28a76be2009-03-06 20:27:40 +000073 vs->sasl.encoded + vs->sasl.encodedOffset,
74 vs->sasl.encodedLength - vs->sasl.encodedOffset);
aliguori2f9606b2009-03-06 20:27:28 +000075 if (!ret)
aliguori28a76be2009-03-06 20:27:40 +000076 return 0;
aliguori2f9606b2009-03-06 20:27:28 +000077
78 vs->sasl.encodedOffset += ret;
79 if (vs->sasl.encodedOffset == vs->sasl.encodedLength) {
aliguori28a76be2009-03-06 20:27:40 +000080 vs->output.offset = 0;
81 vs->sasl.encoded = NULL;
82 vs->sasl.encodedOffset = vs->sasl.encodedLength = 0;
aliguori2f9606b2009-03-06 20:27:28 +000083 }
84
85 /* Can't merge this block with one above, because
86 * someone might have written more unencrypted
87 * data in vs->output while we were processing
88 * SASL encoded output
89 */
90 if (vs->output.offset == 0) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +000091 if (vs->ioc_tag) {
92 g_source_remove(vs->ioc_tag);
93 }
94 vs->ioc_tag = qio_channel_add_watch(
95 vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
aliguori2f9606b2009-03-06 20:27:28 +000096 }
97
98 return ret;
99}
100
101
102long vnc_client_read_sasl(VncState *vs)
103{
104 long ret;
105 uint8_t encoded[4096];
106 const char *decoded;
107 unsigned int decodedLen;
108 int err;
109
110 ret = vnc_client_read_buf(vs, encoded, sizeof(encoded));
111 if (!ret)
aliguori28a76be2009-03-06 20:27:40 +0000112 return 0;
aliguori2f9606b2009-03-06 20:27:28 +0000113
114 err = sasl_decode(vs->sasl.conn,
aliguori28a76be2009-03-06 20:27:40 +0000115 (char *)encoded, ret,
116 &decoded, &decodedLen);
aliguori2f9606b2009-03-06 20:27:28 +0000117
118 if (err != SASL_OK)
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000119 return vnc_client_io_error(vs, -1, NULL);
aliguori2f9606b2009-03-06 20:27:28 +0000120 VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
aliguori28a76be2009-03-06 20:27:40 +0000121 encoded, ret, decoded, decodedLen);
aliguori2f9606b2009-03-06 20:27:28 +0000122 buffer_reserve(&vs->input, decodedLen);
123 buffer_append(&vs->input, decoded, decodedLen);
124 return decodedLen;
125}
126
127
128static int vnc_auth_sasl_check_access(VncState *vs)
129{
130 const void *val;
131 int err;
aliguori76655d62009-03-06 20:27:37 +0000132 int allow;
aliguori2f9606b2009-03-06 20:27:28 +0000133
134 err = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val);
135 if (err != SASL_OK) {
aliguori28a76be2009-03-06 20:27:40 +0000136 VNC_DEBUG("cannot query SASL username on connection %d (%s), denying access\n",
137 err, sasl_errstring(err, NULL, NULL));
138 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000139 }
140 if (val == NULL) {
aliguori28a76be2009-03-06 20:27:40 +0000141 VNC_DEBUG("no client username was found, denying access\n");
142 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000143 }
144 VNC_DEBUG("SASL client username %s\n", (const char *)val);
145
Anthony Liguori7267c092011-08-20 22:09:37 -0500146 vs->sasl.username = g_strdup((const char*)val);
aliguori2f9606b2009-03-06 20:27:28 +0000147
aliguori76655d62009-03-06 20:27:37 +0000148 if (vs->vd->sasl.acl == NULL) {
aliguori28a76be2009-03-06 20:27:40 +0000149 VNC_DEBUG("no ACL activated, allowing access\n");
150 return 0;
aliguori76655d62009-03-06 20:27:37 +0000151 }
152
153 allow = qemu_acl_party_is_allowed(vs->vd->sasl.acl, vs->sasl.username);
154
155 VNC_DEBUG("SASL client %s %s by ACL\n", vs->sasl.username,
aliguori28a76be2009-03-06 20:27:40 +0000156 allow ? "allowed" : "denied");
aliguori76655d62009-03-06 20:27:37 +0000157 return allow ? 0 : -1;
aliguori2f9606b2009-03-06 20:27:28 +0000158}
159
160static int vnc_auth_sasl_check_ssf(VncState *vs)
161{
162 const void *val;
163 int err, ssf;
164
165 if (!vs->sasl.wantSSF)
aliguori28a76be2009-03-06 20:27:40 +0000166 return 1;
aliguori2f9606b2009-03-06 20:27:28 +0000167
168 err = sasl_getprop(vs->sasl.conn, SASL_SSF, &val);
169 if (err != SASL_OK)
aliguori28a76be2009-03-06 20:27:40 +0000170 return 0;
aliguori2f9606b2009-03-06 20:27:28 +0000171
172 ssf = *(const int *)val;
173 VNC_DEBUG("negotiated an SSF of %d\n", ssf);
174 if (ssf < 56)
aliguori28a76be2009-03-06 20:27:40 +0000175 return 0; /* 56 is good for Kerberos */
aliguori2f9606b2009-03-06 20:27:28 +0000176
177 /* Only setup for read initially, because we're about to send an RPC
178 * reply which must be in plain text. When the next incoming RPC
179 * arrives, we'll switch on writes too
180 *
181 * cf qemudClientReadSASL in qemud.c
182 */
183 vs->sasl.runSSF = 1;
184
185 /* We have a SSF that's good enough */
186 return 1;
187}
188
189/*
190 * Step Msg
191 *
192 * Input from client:
193 *
194 * u32 clientin-length
195 * u8-array clientin-string
196 *
197 * Output to client:
198 *
199 * u32 serverout-length
200 * u8-array serverout-strin
201 * u8 continue
202 */
203
204static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len);
205
206static int protocol_client_auth_sasl_step(VncState *vs, uint8_t *data, size_t len)
207{
208 uint32_t datalen = len;
209 const char *serverout;
210 unsigned int serveroutlen;
211 int err;
212 char *clientdata = NULL;
213
214 /* NB, distinction of NULL vs "" is *critical* in SASL */
215 if (datalen) {
aliguori28a76be2009-03-06 20:27:40 +0000216 clientdata = (char*)data;
217 clientdata[datalen-1] = '\0'; /* Wire includes '\0', but make sure */
218 datalen--; /* Don't count NULL byte when passing to _start() */
aliguori2f9606b2009-03-06 20:27:28 +0000219 }
220
221 VNC_DEBUG("Step using SASL Data %p (%d bytes)\n",
aliguori28a76be2009-03-06 20:27:40 +0000222 clientdata, datalen);
aliguori2f9606b2009-03-06 20:27:28 +0000223 err = sasl_server_step(vs->sasl.conn,
aliguori28a76be2009-03-06 20:27:40 +0000224 clientdata,
225 datalen,
226 &serverout,
227 &serveroutlen);
aliguori2f9606b2009-03-06 20:27:28 +0000228 if (err != SASL_OK &&
aliguori28a76be2009-03-06 20:27:40 +0000229 err != SASL_CONTINUE) {
230 VNC_DEBUG("sasl step failed %d (%s)\n",
231 err, sasl_errdetail(vs->sasl.conn));
232 sasl_dispose(&vs->sasl.conn);
233 vs->sasl.conn = NULL;
234 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000235 }
236
237 if (serveroutlen > SASL_DATA_MAX_LEN) {
aliguori28a76be2009-03-06 20:27:40 +0000238 VNC_DEBUG("sasl step reply data too long %d\n",
239 serveroutlen);
240 sasl_dispose(&vs->sasl.conn);
241 vs->sasl.conn = NULL;
242 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000243 }
244
245 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
aliguori28a76be2009-03-06 20:27:40 +0000246 serveroutlen, serverout ? 0 : 1);
aliguori2f9606b2009-03-06 20:27:28 +0000247
248 if (serveroutlen) {
aliguori28a76be2009-03-06 20:27:40 +0000249 vnc_write_u32(vs, serveroutlen + 1);
250 vnc_write(vs, serverout, serveroutlen + 1);
aliguori2f9606b2009-03-06 20:27:28 +0000251 } else {
aliguori28a76be2009-03-06 20:27:40 +0000252 vnc_write_u32(vs, 0);
aliguori2f9606b2009-03-06 20:27:28 +0000253 }
254
255 /* Whether auth is complete */
256 vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
257
258 if (err == SASL_CONTINUE) {
aliguori28a76be2009-03-06 20:27:40 +0000259 VNC_DEBUG("%s", "Authentication must continue\n");
260 /* Wait for step length */
261 vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
aliguori2f9606b2009-03-06 20:27:28 +0000262 } else {
aliguori28a76be2009-03-06 20:27:40 +0000263 if (!vnc_auth_sasl_check_ssf(vs)) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000264 VNC_DEBUG("Authentication rejected for weak SSF %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000265 goto authreject;
266 }
aliguori2f9606b2009-03-06 20:27:28 +0000267
aliguori28a76be2009-03-06 20:27:40 +0000268 /* Check username whitelist ACL */
269 if (vnc_auth_sasl_check_access(vs) < 0) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000270 VNC_DEBUG("Authentication rejected for ACL %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000271 goto authreject;
272 }
aliguori2f9606b2009-03-06 20:27:28 +0000273
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000274 VNC_DEBUG("Authentication successful %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000275 vnc_write_u32(vs, 0); /* Accept auth */
276 /*
277 * Delay writing in SSF encoded mode until pending output
278 * buffer is written
279 */
280 if (vs->sasl.runSSF)
281 vs->sasl.waitWriteSSF = vs->output.offset;
282 start_client_init(vs);
aliguori2f9606b2009-03-06 20:27:28 +0000283 }
284
285 return 0;
286
287 authreject:
288 vnc_write_u32(vs, 1); /* Reject auth */
289 vnc_write_u32(vs, sizeof("Authentication failed"));
290 vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
291 vnc_flush(vs);
292 vnc_client_error(vs);
293 return -1;
294
295 authabort:
296 vnc_client_error(vs);
297 return -1;
298}
299
300static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len)
301{
302 uint32_t steplen = read_u32(data, 0);
303 VNC_DEBUG("Got client step len %d\n", steplen);
304 if (steplen > SASL_DATA_MAX_LEN) {
aliguori28a76be2009-03-06 20:27:40 +0000305 VNC_DEBUG("Too much SASL data %d\n", steplen);
306 vnc_client_error(vs);
307 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000308 }
309
310 if (steplen == 0)
aliguori28a76be2009-03-06 20:27:40 +0000311 return protocol_client_auth_sasl_step(vs, NULL, 0);
aliguori2f9606b2009-03-06 20:27:28 +0000312 else
aliguori28a76be2009-03-06 20:27:40 +0000313 vnc_read_when(vs, protocol_client_auth_sasl_step, steplen);
aliguori2f9606b2009-03-06 20:27:28 +0000314 return 0;
315}
316
317/*
318 * Start Msg
319 *
320 * Input from client:
321 *
322 * u32 clientin-length
323 * u8-array clientin-string
324 *
325 * Output to client:
326 *
327 * u32 serverout-length
328 * u8-array serverout-strin
329 * u8 continue
330 */
331
332#define SASL_DATA_MAX_LEN (1024 * 1024)
333
334static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len)
335{
336 uint32_t datalen = len;
337 const char *serverout;
338 unsigned int serveroutlen;
339 int err;
340 char *clientdata = NULL;
341
342 /* NB, distinction of NULL vs "" is *critical* in SASL */
343 if (datalen) {
aliguori28a76be2009-03-06 20:27:40 +0000344 clientdata = (char*)data;
345 clientdata[datalen-1] = '\0'; /* Should be on wire, but make sure */
346 datalen--; /* Don't count NULL byte when passing to _start() */
aliguori2f9606b2009-03-06 20:27:28 +0000347 }
348
349 VNC_DEBUG("Start SASL auth with mechanism %s. Data %p (%d bytes)\n",
aliguori28a76be2009-03-06 20:27:40 +0000350 vs->sasl.mechlist, clientdata, datalen);
aliguori2f9606b2009-03-06 20:27:28 +0000351 err = sasl_server_start(vs->sasl.conn,
aliguori28a76be2009-03-06 20:27:40 +0000352 vs->sasl.mechlist,
353 clientdata,
354 datalen,
355 &serverout,
356 &serveroutlen);
aliguori2f9606b2009-03-06 20:27:28 +0000357 if (err != SASL_OK &&
aliguori28a76be2009-03-06 20:27:40 +0000358 err != SASL_CONTINUE) {
359 VNC_DEBUG("sasl start failed %d (%s)\n",
360 err, sasl_errdetail(vs->sasl.conn));
361 sasl_dispose(&vs->sasl.conn);
362 vs->sasl.conn = NULL;
363 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000364 }
365 if (serveroutlen > SASL_DATA_MAX_LEN) {
aliguori28a76be2009-03-06 20:27:40 +0000366 VNC_DEBUG("sasl start reply data too long %d\n",
367 serveroutlen);
368 sasl_dispose(&vs->sasl.conn);
369 vs->sasl.conn = NULL;
370 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000371 }
372
373 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
aliguori28a76be2009-03-06 20:27:40 +0000374 serveroutlen, serverout ? 0 : 1);
aliguori2f9606b2009-03-06 20:27:28 +0000375
376 if (serveroutlen) {
aliguori28a76be2009-03-06 20:27:40 +0000377 vnc_write_u32(vs, serveroutlen + 1);
378 vnc_write(vs, serverout, serveroutlen + 1);
aliguori2f9606b2009-03-06 20:27:28 +0000379 } else {
aliguori28a76be2009-03-06 20:27:40 +0000380 vnc_write_u32(vs, 0);
aliguori2f9606b2009-03-06 20:27:28 +0000381 }
382
383 /* Whether auth is complete */
384 vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
385
386 if (err == SASL_CONTINUE) {
aliguori28a76be2009-03-06 20:27:40 +0000387 VNC_DEBUG("%s", "Authentication must continue\n");
388 /* Wait for step length */
389 vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
aliguori2f9606b2009-03-06 20:27:28 +0000390 } else {
aliguori28a76be2009-03-06 20:27:40 +0000391 if (!vnc_auth_sasl_check_ssf(vs)) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000392 VNC_DEBUG("Authentication rejected for weak SSF %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000393 goto authreject;
394 }
aliguori2f9606b2009-03-06 20:27:28 +0000395
aliguori28a76be2009-03-06 20:27:40 +0000396 /* Check username whitelist ACL */
397 if (vnc_auth_sasl_check_access(vs) < 0) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000398 VNC_DEBUG("Authentication rejected for ACL %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000399 goto authreject;
400 }
aliguori2f9606b2009-03-06 20:27:28 +0000401
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000402 VNC_DEBUG("Authentication successful %p\n", vs->ioc);
aliguori28a76be2009-03-06 20:27:40 +0000403 vnc_write_u32(vs, 0); /* Accept auth */
404 start_client_init(vs);
aliguori2f9606b2009-03-06 20:27:28 +0000405 }
406
407 return 0;
408
409 authreject:
410 vnc_write_u32(vs, 1); /* Reject auth */
411 vnc_write_u32(vs, sizeof("Authentication failed"));
412 vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
413 vnc_flush(vs);
414 vnc_client_error(vs);
415 return -1;
416
417 authabort:
418 vnc_client_error(vs);
419 return -1;
420}
421
422static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len)
423{
424 uint32_t startlen = read_u32(data, 0);
425 VNC_DEBUG("Got client start len %d\n", startlen);
426 if (startlen > SASL_DATA_MAX_LEN) {
aliguori28a76be2009-03-06 20:27:40 +0000427 VNC_DEBUG("Too much SASL data %d\n", startlen);
428 vnc_client_error(vs);
429 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000430 }
431
432 if (startlen == 0)
aliguori28a76be2009-03-06 20:27:40 +0000433 return protocol_client_auth_sasl_start(vs, NULL, 0);
aliguori2f9606b2009-03-06 20:27:28 +0000434
435 vnc_read_when(vs, protocol_client_auth_sasl_start, startlen);
436 return 0;
437}
438
439static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len)
440{
Jim Meyering5847d9e2012-10-04 13:09:54 +0200441 char *mechname = g_strndup((const char *) data, len);
aliguori2f9606b2009-03-06 20:27:28 +0000442 VNC_DEBUG("Got client mechname '%s' check against '%s'\n",
aliguori28a76be2009-03-06 20:27:40 +0000443 mechname, vs->sasl.mechlist);
aliguori2f9606b2009-03-06 20:27:28 +0000444
445 if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000446 if (vs->sasl.mechlist[len] != '\0' &&
447 vs->sasl.mechlist[len] != ',') {
448 VNC_DEBUG("One %d", vs->sasl.mechlist[len]);
Blue Swirl8ce7d352011-01-12 19:48:56 +0000449 goto fail;
aliguori28a76be2009-03-06 20:27:40 +0000450 }
aliguori2f9606b2009-03-06 20:27:28 +0000451 } else {
aliguori28a76be2009-03-06 20:27:40 +0000452 char *offset = strstr(vs->sasl.mechlist, mechname);
453 VNC_DEBUG("Two %p\n", offset);
454 if (!offset) {
Blue Swirl8ce7d352011-01-12 19:48:56 +0000455 goto fail;
aliguori28a76be2009-03-06 20:27:40 +0000456 }
457 VNC_DEBUG("Two '%s'\n", offset);
458 if (offset[-1] != ',' ||
459 (offset[len] != '\0'&&
460 offset[len] != ',')) {
Blue Swirl8ce7d352011-01-12 19:48:56 +0000461 goto fail;
aliguori28a76be2009-03-06 20:27:40 +0000462 }
aliguori2f9606b2009-03-06 20:27:28 +0000463 }
464
Markus Armbruster302d9d62011-11-08 13:45:21 +0100465 g_free(vs->sasl.mechlist);
aliguori2f9606b2009-03-06 20:27:28 +0000466 vs->sasl.mechlist = mechname;
467
468 VNC_DEBUG("Validated mechname '%s'\n", mechname);
469 vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4);
470 return 0;
Blue Swirl8ce7d352011-01-12 19:48:56 +0000471
472 fail:
473 vnc_client_error(vs);
Markus Armbruster302d9d62011-11-08 13:45:21 +0100474 g_free(mechname);
Blue Swirl8ce7d352011-01-12 19:48:56 +0000475 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000476}
477
478static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len)
479{
480 uint32_t mechlen = read_u32(data, 0);
481 VNC_DEBUG("Got client mechname len %d\n", mechlen);
482 if (mechlen > 100) {
aliguori28a76be2009-03-06 20:27:40 +0000483 VNC_DEBUG("Too long SASL mechname data %d\n", mechlen);
484 vnc_client_error(vs);
485 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000486 }
487 if (mechlen < 1) {
aliguori28a76be2009-03-06 20:27:40 +0000488 VNC_DEBUG("Too short SASL mechname %d\n", mechlen);
489 vnc_client_error(vs);
490 return -1;
aliguori2f9606b2009-03-06 20:27:28 +0000491 }
492 vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen);
493 return 0;
494}
495
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000496static char *
497vnc_socket_ip_addr_string(QIOChannelSocket *ioc,
498 bool local,
499 Error **errp)
500{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200501 SocketAddress *addr;
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000502 char *ret;
503
504 if (local) {
505 addr = qio_channel_socket_get_local_address(ioc, errp);
506 } else {
507 addr = qio_channel_socket_get_remote_address(ioc, errp);
508 }
509 if (!addr) {
510 return NULL;
511 }
512
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200513 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000514 error_setg(errp, "Not an inet socket type");
515 return NULL;
516 }
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200517 ret = g_strdup_printf("%s;%s", addr->u.inet.host, addr->u.inet.port);
518 qapi_free_SocketAddress(addr);
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000519 return ret;
520}
521
aliguori2f9606b2009-03-06 20:27:28 +0000522void start_auth_sasl(VncState *vs)
523{
524 const char *mechlist = NULL;
525 sasl_security_properties_t secprops;
526 int err;
527 char *localAddr, *remoteAddr;
528 int mechlistlen;
529
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000530 VNC_DEBUG("Initialize SASL auth %p\n", vs->ioc);
aliguori2f9606b2009-03-06 20:27:28 +0000531
532 /* Get local & remote client addresses in form IPADDR;PORT */
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000533 localAddr = vnc_socket_ip_addr_string(vs->sioc, true, NULL);
534 if (!localAddr) {
aliguori28a76be2009-03-06 20:27:40 +0000535 goto authabort;
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000536 }
aliguori2f9606b2009-03-06 20:27:28 +0000537
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000538 remoteAddr = vnc_socket_ip_addr_string(vs->sioc, false, NULL);
539 if (!remoteAddr) {
Stefan Weilae878b12011-10-07 21:15:20 +0200540 g_free(localAddr);
aliguori28a76be2009-03-06 20:27:40 +0000541 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000542 }
543
544 err = sasl_server_new("vnc",
aliguori28a76be2009-03-06 20:27:40 +0000545 NULL, /* FQDN - just delegates to gethostname */
546 NULL, /* User realm */
547 localAddr,
548 remoteAddr,
549 NULL, /* Callbacks, not needed */
550 SASL_SUCCESS_DATA,
551 &vs->sasl.conn);
Stefan Weilae878b12011-10-07 21:15:20 +0200552 g_free(localAddr);
553 g_free(remoteAddr);
aliguori2f9606b2009-03-06 20:27:28 +0000554 localAddr = remoteAddr = NULL;
555
556 if (err != SASL_OK) {
aliguori28a76be2009-03-06 20:27:40 +0000557 VNC_DEBUG("sasl context setup failed %d (%s)",
558 err, sasl_errstring(err, NULL, NULL));
559 vs->sasl.conn = NULL;
560 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000561 }
562
aliguori2f9606b2009-03-06 20:27:28 +0000563 /* Inform SASL that we've got an external SSF layer from TLS/x509 */
Daniel P. Berrange7e7e2eb2011-06-23 13:31:41 +0100564 if (vs->auth == VNC_AUTH_VENCRYPT &&
565 vs->subauth == VNC_AUTH_VENCRYPT_X509SASL) {
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100566 Error *local_err = NULL;
567 int keysize;
aliguori28a76be2009-03-06 20:27:40 +0000568 sasl_ssf_t ssf;
aliguori2f9606b2009-03-06 20:27:28 +0000569
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100570 keysize = qcrypto_tls_session_get_key_size(vs->tls,
571 &local_err);
572 if (keysize < 0) {
573 VNC_DEBUG("cannot TLS get cipher size: %s\n",
574 error_get_pretty(local_err));
575 error_free(local_err);
aliguori28a76be2009-03-06 20:27:40 +0000576 sasl_dispose(&vs->sasl.conn);
577 vs->sasl.conn = NULL;
578 goto authabort;
579 }
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100580 ssf = keysize * CHAR_BIT; /* tls key size is bytes, sasl wants bits */
aliguori2f9606b2009-03-06 20:27:28 +0000581
aliguori28a76be2009-03-06 20:27:40 +0000582 err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf);
583 if (err != SASL_OK) {
584 VNC_DEBUG("cannot set SASL external SSF %d (%s)\n",
585 err, sasl_errstring(err, NULL, NULL));
586 sasl_dispose(&vs->sasl.conn);
587 vs->sasl.conn = NULL;
588 goto authabort;
589 }
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100590 } else {
aliguori28a76be2009-03-06 20:27:40 +0000591 vs->sasl.wantSSF = 1;
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100592 }
aliguori2f9606b2009-03-06 20:27:28 +0000593
594 memset (&secprops, 0, sizeof secprops);
Daniel P. Berrange3e305e42015-08-06 14:39:32 +0100595 /* Inform SASL that we've got an external SSF layer from TLS.
596 *
597 * Disable SSF, if using TLS+x509+SASL only. TLS without x509
598 * is not sufficiently strong
599 */
600 if (vs->vd->is_unix ||
601 (vs->auth == VNC_AUTH_VENCRYPT &&
602 vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)) {
aliguori28a76be2009-03-06 20:27:40 +0000603 /* If we've got TLS or UNIX domain sock, we don't care about SSF */
604 secprops.min_ssf = 0;
605 secprops.max_ssf = 0;
606 secprops.maxbufsize = 8192;
607 secprops.security_flags = 0;
aliguori2f9606b2009-03-06 20:27:28 +0000608 } else {
aliguori28a76be2009-03-06 20:27:40 +0000609 /* Plain TCP, better get an SSF layer */
610 secprops.min_ssf = 56; /* Good enough to require kerberos */
611 secprops.max_ssf = 100000; /* Arbitrary big number */
612 secprops.maxbufsize = 8192;
613 /* Forbid any anonymous or trivially crackable auth */
614 secprops.security_flags =
615 SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
aliguori2f9606b2009-03-06 20:27:28 +0000616 }
617
618 err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops);
619 if (err != SASL_OK) {
aliguori28a76be2009-03-06 20:27:40 +0000620 VNC_DEBUG("cannot set SASL security props %d (%s)\n",
621 err, sasl_errstring(err, NULL, NULL));
622 sasl_dispose(&vs->sasl.conn);
623 vs->sasl.conn = NULL;
624 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000625 }
626
627 err = sasl_listmech(vs->sasl.conn,
aliguori28a76be2009-03-06 20:27:40 +0000628 NULL, /* Don't need to set user */
629 "", /* Prefix */
630 ",", /* Separator */
631 "", /* Suffix */
632 &mechlist,
633 NULL,
634 NULL);
aliguori2f9606b2009-03-06 20:27:28 +0000635 if (err != SASL_OK) {
aliguori28a76be2009-03-06 20:27:40 +0000636 VNC_DEBUG("cannot list SASL mechanisms %d (%s)\n",
637 err, sasl_errdetail(vs->sasl.conn));
638 sasl_dispose(&vs->sasl.conn);
639 vs->sasl.conn = NULL;
640 goto authabort;
aliguori2f9606b2009-03-06 20:27:28 +0000641 }
642 VNC_DEBUG("Available mechanisms for client: '%s'\n", mechlist);
643
Markus Armbruster302d9d62011-11-08 13:45:21 +0100644 vs->sasl.mechlist = g_strdup(mechlist);
aliguori2f9606b2009-03-06 20:27:28 +0000645 mechlistlen = strlen(mechlist);
646 vnc_write_u32(vs, mechlistlen);
647 vnc_write(vs, mechlist, mechlistlen);
648 vnc_flush(vs);
649
650 VNC_DEBUG("Wait for client mechname length\n");
651 vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4);
652
653 return;
654
655 authabort:
656 vnc_client_error(vs);
aliguori2f9606b2009-03-06 20:27:28 +0000657}
658
659