42af212
This patch adds the new SASL authentication protocol to the VNC server.
42af212
42af212
It is enabled by setting the 'sasl' flag when launching VNC. SASL can
42af212
optionally provide encryption via its SSF layer, if a suitable mechanism
42af212
is configured (eg, GSSAPI/Kerberos, or Digest-MD5).  If an SSF layer is
42af212
not available, then it should be combined with the x509 VNC authentication
42af212
protocol which provides encryption.
42af212
42af212
eg, if using GSSAPI
42af212
42af212
   qemu -vnc localhost:1,sasl
42af212
42af212
eg if using  TLS/x509 for encryption
42af212
42af212
   qemu -vnc localhost:1,sasl,tls,x509
42af212
42af212
42af212
By default the Cyrus SASL library will look for its configuration in
42af212
the file /etc/sasl2/qemu.conf.  For non-root users, this can be overridden
42af212
by setting the SASL_CONF_PATH environment variable, eg to make it look in
42af212
$HOME/.sasl2.  NB unprivileged users may not have access to the full range
42af212
of SASL mechanisms, since some of them require some administrative privileges
42af212
to configure. The patch includes an example SASL configuration file which
42af212
illustrates config for GSSAPI and Digest-MD5, though it should be noted that
42af212
the latter is not really considered secure any more.
42af212
42af212
Most of the SASL authentication code is located in a separate source file,
42af212
vnc-auth-sasl.c.  The main vnc.c file only contains minimal integration
42af212
glue, specifically parsing of command line flags / setup, and calls to
42af212
start the SASL auth process, to do encoding/decoding for data.
42af212
42af212
There are several possible stacks for reading & writing of data, depending
42af212
on the combo of VNC authentication methods in use
42af212
42af212
 - Clear.    read/write straight to socket
42af212
 - TLS.      read/write via GNUTLS helpers
42af212
 - SASL.     encode/decode via SASL SSF layer, then read/write to socket
42af212
 - SASL+TLS. encode/decode via SASL SSF layer, then read/write via GNUTLS
42af212
42af212
Hence, the vnc_client_read & vnc_client_write methods have been refactored
42af212
a little.
42af212
42af212
   vnc_client_read:  main entry point for reading, calls either
42af212
42af212
       - vnc_client_read_plain   reading, with no intermediate decoding
42af212
       - vnc_client_read_sasl    reading, with SASL SSF decoding
42af212
42af212
   These two methods, then call vnc_client_read_buf(). This decides
42af212
   whether to write to the socket directly or write via GNUTLS.
42af212
42af212
The situation is the same for writing data. More extensive comments
42af212
have been added in the code / patch. The vnc_client_read_sasl and
42af212
vnc_client_write_sasl method implementations live in the separate
42af212
vnc-auth-sasl.c file.
42af212
42af212
The state required for the SASL auth mechanism is kept in a separate
42af212
VncStateSASL struct, defined in vnc-auth-sasl.h and included in the
42af212
main VncState.
42af212
42af212
The configure script probes for SASL and automatically enables it
42af212
if found, unless --disable-vnc-sasl was given to override it.
42af212
42af212
42af212
 Makefile            |    7 
42af212
 Makefile.target     |    5 
42af212
 b/qemu.sasl         |   34 ++
42af212
 b/vnc-auth-sasl.c   |  626 ++++++++++++++++++++++++++++++++++++++++++++++++++++
42af212
 b/vnc-auth-sasl.h   |   67 +++++
42af212
 configure           |   34 ++
42af212
 qemu-doc.texi       |   97 ++++++++
42af212
 vnc-auth-vencrypt.c |   12 
42af212
 vnc.c               |  249 ++++++++++++++++++--
42af212
 vnc.h               |   31 ++
42af212
 10 files changed, 1129 insertions(+), 33 deletions(-)
42af212
42af212
   Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
42af212
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/Makefile
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/Makefile
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/Makefile
Glauber Costa 8571d06
@@ -152,6 +152,9 @@ OBJS+=vnc.o d3des.o
42af212
 ifdef CONFIG_VNC_TLS
42af212
 OBJS+=vnc-tls.o vnc-auth-vencrypt.o
42af212
 endif
42af212
+ifdef CONFIG_VNC_SASL
42af212
+OBJS+=vnc-auth-sasl.o
42af212
+endif
42af212
 
42af212
 ifdef CONFIG_COCOA
42af212
 OBJS+=cocoa.o
Glauber Costa 8571d06
@@ -175,7 +178,7 @@ sdl.o: sdl.c keymaps.h sdl_keysym.h
42af212
 
42af212
 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
42af212
 
42af212
-vnc.h: vnc-tls.h vnc-auth-vencrypt.h keymaps.h
42af212
+vnc.h: vnc-tls.h vnc-auth-vencrypt.h vnc-auth-sasl.h keymaps.h
42af212
 
42af212
 vnc.o: vnc.c vnc.h vnc_keysym.h vnchextile.h d3des.c d3des.h
42af212
 
Glauber Costa 8571d06
@@ -185,6 +188,8 @@ vnc-tls.o: vnc-tls.c vnc.h
42af212
 
42af212
 vnc-auth-vencrypt.o: vnc-auth-vencrypt.c vnc.h
42af212
 
42af212
+vnc-auth-sasl.o: vnc-auth-sasl.c vnc.h
42af212
+
42af212
 curses.o: curses.c keymaps.h curses_keys.h
42af212
 
42af212
 bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/Makefile.target
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/Makefile.target
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/Makefile.target
Glauber Costa 8571d06
@@ -613,6 +613,11 @@ CPPFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
42af212
 LIBS += $(CONFIG_VNC_TLS_LIBS)
42af212
 endif
42af212
 
42af212
+ifdef CONFIG_VNC_SASL
42af212
+CPPFLAGS += $(CONFIG_VNC_SASL_CFLAGS)
42af212
+LIBS += $(CONFIG_VNC_SASL_LIBS)
42af212
+endif
42af212
+
42af212
 ifdef CONFIG_BLUEZ
42af212
 LIBS += $(CONFIG_BLUEZ_LIBS)
42af212
 endif
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/configure
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/configure
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/configure
42af212
@@ -164,6 +164,7 @@ fmod_lib=""
42af212
 fmod_inc=""
42af212
 oss_lib=""
42af212
 vnc_tls="yes"
42af212
+vnc_sasl="yes"
42af212
 bsd="no"
42af212
 linux="no"
42af212
 solaris="no"
Glauber Costa 8571d06
@@ -403,6 +404,8 @@ for opt do
42af212
   ;;
42af212
   --disable-vnc-tls) vnc_tls="no"
42af212
   ;;
42af212
+  --disable-vnc-sasl) vnc_sasl="no"
42af212
+  ;;
42af212
   --disable-slirp) slirp="no"
42af212
   ;;
42af212
   --disable-vde) vde="no"
Glauber Costa 8571d06
@@ -562,6 +565,7 @@ echo "                           Availab
42af212
 echo "  --enable-mixemu          enable mixer emulation"
42af212
 echo "  --disable-brlapi         disable BrlAPI"
42af212
 echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
42af212
+echo "  --disable-vnc-sasl       disable SASL encryption for VNC server"
42af212
 echo "  --disable-curses         disable curses output"
42af212
 echo "  --disable-bluez          disable bluez stack connectivity"
42af212
 echo "  --disable-kvm            disable KVM acceleration support"
Glauber Costa 8571d06
@@ -874,6 +878,25 @@ EOF
42af212
 fi
42af212
 
42af212
 ##########################################
42af212
+# VNC SASL detection
42af212
+if test "$vnc_sasl" = "yes" ; then
42af212
+cat > $TMPC <
42af212
+#include <sasl/sasl.h>
42af212
+#include <stdio.h>
42af212
+int main(void) { sasl_server_init(NULL, "qemu"); return 0; }
42af212
+EOF
42af212
+    # Assuming Cyrus-SASL installed in /usr prefix
42af212
+    vnc_sasl_cflags=""
42af212
+    vnc_sasl_libs="-lsasl2"
42af212
+    if $cc $ARCH_CFLAGS -o $TMPE ${OS_CFLAGS} $vnc_sasl_cflags $TMPC \
42af212
+           $vnc_sasl_libs 2> /dev/null ; then
42af212
+	:
42af212
+    else
42af212
+	vnc_sasl="no"
42af212
+    fi
42af212
+fi
42af212
+
42af212
+##########################################
42af212
 # vde libraries probe
42af212
 if test "$vde" = "yes" ; then
42af212
   cat > $TMPC << EOF
Glauber Costa 8571d06
@@ -1208,6 +1231,11 @@ if test "$vnc_tls" = "yes" ; then
42af212
     echo "    TLS CFLAGS    $vnc_tls_cflags"
42af212
     echo "    TLS LIBS      $vnc_tls_libs"
42af212
 fi
42af212
+echo "VNC SASL support  $vnc_sasl"
42af212
+if test "$vnc_sasl" = "yes" ; then
42af212
+    echo "    SASL CFLAGS    $vnc_sasl_cflags"
42af212
+    echo "    SASL LIBS      $vnc_sasl_libs"
42af212
+fi
42af212
 if test -n "$sparc_cpu"; then
42af212
     echo "Target Sparc Arch $sparc_cpu"
42af212
 fi
Glauber Costa 8571d06
@@ -1451,6 +1479,12 @@ if test "$vnc_tls" = "yes" ; then
42af212
   echo "CONFIG_VNC_TLS_LIBS=$vnc_tls_libs" >> $config_mak
42af212
   echo "#define CONFIG_VNC_TLS 1" >> $config_h
42af212
 fi
42af212
+if test "$vnc_sasl" = "yes" ; then
42af212
+  echo "CONFIG_VNC_SASL=yes" >> $config_mak
42af212
+  echo "CONFIG_VNC_SASL_CFLAGS=$vnc_sasl_cflags" >> $config_mak
42af212
+  echo "CONFIG_VNC_SASL_LIBS=$vnc_sasl_libs" >> $config_mak
42af212
+  echo "#define CONFIG_VNC_SASL 1" >> $config_h
42af212
+fi
42af212
 qemu_version=`head $source_path/VERSION`
42af212
 echo "VERSION=$qemu_version" >>$config_mak
42af212
 echo "#define QEMU_VERSION \"$qemu_version\"" >> $config_h
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/qemu-doc.texi
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/qemu-doc.texi
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/qemu-doc.texi
Glauber Costa 8571d06
@@ -624,6 +624,21 @@ path following this option specifies whe
42af212
 be loaded from. See the @ref{vnc_security} section for details on generating
42af212
 certificates.
42af212
 
42af212
+@item sasl
42af212
+
42af212
+Require that the client use SASL to authenticate with the VNC server.
42af212
+The exact choice of authentication method used is controlled from the
42af212
+system / user's SASL configuration file for the 'qemu' service. This
42af212
+is typically found in /etc/sasl2/qemu.conf. If running QEMU as an
42af212
+unprivileged user, an environment variable SASL_CONF_PATH can be used
42af212
+to make it search alternate locations for the service config.
42af212
+While some SASL auth methods can also provide data encryption (eg GSSAPI),
42af212
+it is recommended that SASL always be combined with the 'tls' and
42af212
+'x509' settings to enable use of SSL and server certificates. This
42af212
+ensures a data encryption preventing compromise of authentication
42af212
+credentials. See the @ref{vnc_security} section for details on using
42af212
+SASL authentication.
42af212
+
42af212
 @end table
42af212
 
42af212
 @end table
Glauber Costa 8571d06
@@ -2058,7 +2073,10 @@ considerations depending on the deployme
42af212
 * vnc_sec_certificate::
42af212
 * vnc_sec_certificate_verify::
42af212
 * vnc_sec_certificate_pw::
42af212
+* vnc_sec_sasl::
42af212
+* vnc_sec_certificate_sasl::
42af212
 * vnc_generate_cert::
42af212
+* vnc_setup_sasl::
42af212
 @end menu
42af212
 @node vnc_sec_none
42af212
 @subsection Without passwords
Glauber Costa 8571d06
@@ -2141,6 +2159,41 @@ Password: ********
42af212
 (qemu)
42af212
 @end example
42af212
 
42af212
+
42af212
+@node vnc_sec_sasl
42af212
+@subsection With SASL authentication
42af212
+
42af212
+The SASL authentication method is a VNC extension, that provides an
42af212
+easily extendable, pluggable authentication method. This allows for
42af212
+integration with a wide range of authentication mechanisms, such as
42af212
+PAM, GSSAPI/Kerberos, LDAP, SQL databases, one-time keys and more.
42af212
+The strength of the authentication depends on the exact mechanism
42af212
+configured. If the chosen mechanism also provides a SSF layer, then
42af212
+it will encrypt the datastream as well.
42af212
+
42af212
+Refer to the later docs on how to choose the exact SASL mechanism
42af212
+used for authentication, but assuming use of one supporting SSF,
42af212
+then QEMU can be launched with:
42af212
+
42af212
+@example
42af212
+qemu [...OPTIONS...] -vnc :1,sasl -monitor stdio
42af212
+@end example
42af212
+
42af212
+@node vnc_sec_certificate_sasl
42af212
+@subsection With x509 certificates and SASL authentication
42af212
+
42af212
+If the desired SASL authentication mechanism does not supported
42af212
+SSF layers, then it is strongly advised to run it in combination
42af212
+with TLS and x509 certificates. This provides securely encrypted
42af212
+data stream, avoiding risk of compromising of the security
42af212
+credentials. This can be enabled, by combining the 'sasl' option
42af212
+with the aforementioned TLS + x509 options:
42af212
+
42af212
+@example
42af212
+qemu [...OPTIONS...] -vnc :1,tls,x509,sasl -monitor stdio
42af212
+@end example
42af212
+
42af212
+
42af212
 @node vnc_generate_cert
42af212
 @subsection Generating certificates for VNC
42af212
 
Glauber Costa 8571d06
@@ -2252,6 +2305,50 @@ EOF
42af212
 The @code{client-key.pem} and @code{client-cert.pem} files should now be securely
42af212
 copied to the client for which they were generated.
42af212
 
42af212
+
42af212
+@node vnc_setup_sasl
42af212
+
42af212
+@subsection Configuring SASL mechanisms
42af212
+
42af212
+The following documentation assumes use of the Cyrus SASL implementation on a
42af212
+Linux host, but the principals should apply to any other SASL impl. When SASL
42af212
+is enabled, the mechanism configuration will be loaded from system default
42af212
+SASL service config /etc/sasl2/qemu.conf. If running QEMU as an
42af212
+unprivileged user, an environment variable SASL_CONF_PATH can be used
42af212
+to make it search alternate locations for the service config.
42af212
+
42af212
+The default configuration might contain
42af212
+
42af212
+@example
42af212
+mech_list: digest-md5
42af212
+sasldb_path: /etc/qemu/passwd.db
42af212
+@end example
42af212
+
42af212
+This says to use the 'Digest MD5' mechanism, which is similar to the HTTP
42af212
+Digest-MD5 mechanism. The list of valid usernames & passwords is maintained
42af212
+in the /etc/qemu/passwd.db file, and can be updated using the saslpasswd2
42af212
+command. While this mechanism is easy to configure and use, it is not
42af212
+considered secure by modern standards, so only suitable for developers /
42af212
+ad-hoc testing.
42af212
+
42af212
+A more serious deployment might use Kerberos, which is done with the 'gssapi'
42af212
+mechanism
42af212
+
42af212
+@example
42af212
+mech_list: gssapi
42af212
+keytab: /etc/qemu/krb5.tab
42af212
+@end example
42af212
+
42af212
+For this to work the administrator of your KDC must generate a Kerberos
42af212
+principal for the server, with a name of  'qemu/somehost.example.com@@EXAMPLE.COM'
42af212
+replacing 'somehost.example.com' with the fully qualified host name of the
42af212
+machine running QEMU, and 'EXAMPLE.COM' with the Keberos Realm.
42af212
+
42af212
+Other configurations will be left as an exercise for the reader. It should
42af212
+be noted that only Digest-MD5 and GSSAPI provides a SSF layer for data
42af212
+encryption. For all other mechanisms, VNC should always be configured to
42af212
+use TLS and x509 certificates to protect security credentials from snooping.
42af212
+
42af212
 @node gdb_usage
42af212
 @section GDB usage
42af212
 
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/qemu.sasl
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- /dev/null
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/qemu.sasl
42af212
@@ -0,0 +1,34 @@
42af212
+# If you want to use the non-TLS socket, then you *must* include
42af212
+# the GSSAPI or DIGEST-MD5 mechanisms, because they are the only
42af212
+# ones that can offer session encryption as well as authentication.
42af212
+#
42af212
+# If you're only using TLS, then you can turn on any mechanisms
42af212
+# you like for authentication, because TLS provides the encryption
42af212
+#
42af212
+# Default to a simple username+password mechanism
42af212
+# NB digest-md5 is no longer considered secure by current standards
42af212
+mech_list: digest-md5
42af212
+
42af212
+# Before you can use GSSAPI, you need a service principle on the
42af212
+# KDC server for libvirt, and that to be exported to the keytab
42af212
+# file listed below
42af212
+#mech_list: gssapi
42af212
+#
42af212
+# You can also list many mechanisms at once, then the user can choose
42af212
+# by adding  '?auth=sasl.gssapi' to their libvirt URI, eg
42af212
+#   qemu+tcp://hostname/system?auth=sasl.gssapi
42af212
+#mech_list: digest-md5 gssapi
42af212
+
42af212
+# Some older builds of MIT kerberos on Linux ignore this option &
42af212
+# instead need KRB5_KTNAME env var.
42af212
+# For modern Linux, and other OS, this should be sufficient
42af212
+keytab: /etc/qemu/krb5.tab
42af212
+
42af212
+# If using digest-md5 for username/passwds, then this is the file
42af212
+# containing the passwds. Use 'saslpasswd2 -a qemu [username]'
42af212
+# to add entries, and 'sasldblistusers2 -a qemu' to browse it
42af212
+sasldb_path: /etc/qemu/passwd.db
42af212
+
42af212
+
42af212
+auxprop_plugin: sasldb
42af212
+
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/vnc-auth-sasl.c
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- /dev/null
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/vnc-auth-sasl.c
42af212
@@ -0,0 +1,626 @@
42af212
+/*
42af212
+ * QEMU VNC display driver: SASL auth protocol
42af212
+ *
42af212
+ * Copyright (C) 2009 Red Hat, Inc
42af212
+ *
42af212
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
42af212
+ * of this software and associated documentation files (the "Software"), to deal
42af212
+ * in the Software without restriction, including without limitation the rights
42af212
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42af212
+ * copies of the Software, and to permit persons to whom the Software is
42af212
+ * furnished to do so, subject to the following conditions:
42af212
+ *
42af212
+ * The above copyright notice and this permission notice shall be included in
42af212
+ * all copies or substantial portions of the Software.
42af212
+ *
42af212
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42af212
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42af212
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
42af212
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42af212
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42af212
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42af212
+ * THE SOFTWARE.
42af212
+ */
42af212
+
42af212
+#include "vnc.h"
42af212
+
42af212
+/* Max amount of data we send/recv for SASL steps to prevent DOS */
42af212
+#define SASL_DATA_MAX_LEN (1024 * 1024)
42af212
+
42af212
+
42af212
+void vnc_sasl_client_cleanup(VncState *vs)
42af212
+{
42af212
+    if (vs->sasl.conn) {
42af212
+	vs->sasl.runSSF = vs->sasl.waitWriteSSF = vs->sasl.wantSSF = 0;
42af212
+	vs->sasl.encodedLength = vs->sasl.encodedOffset = 0;
42af212
+	vs->sasl.encoded = NULL;
42af212
+	free(vs->sasl.username);
42af212
+	free(vs->sasl.mechlist);
42af212
+	vs->sasl.username = vs->sasl.mechlist = NULL;
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+    }
42af212
+}
42af212
+
42af212
+
42af212
+long vnc_client_write_sasl(VncState *vs)
42af212
+{
42af212
+    long ret;
42af212
+
42af212
+    VNC_DEBUG("Write SASL: Pending output %p size %d offset %d Encoded: %p size %d offset %d\n",
42af212
+	      vs->output.buffer, vs->output.capacity, vs->output.offset,
42af212
+	      vs->sasl.encoded, vs->sasl.encodedLength, vs->sasl.encodedOffset);
42af212
+
42af212
+    if (!vs->sasl.encoded) {
42af212
+	int err;
42af212
+	err = sasl_encode(vs->sasl.conn,
42af212
+			  (char *)vs->output.buffer,
42af212
+			  vs->output.offset,
42af212
+			  (const char **)&vs->sasl.encoded,
42af212
+			  &vs->sasl.encodedLength);
42af212
+	if (err != SASL_OK)
42af212
+	    return vnc_client_io_error(vs, -1, EIO);
42af212
+
42af212
+	vs->sasl.encodedOffset = 0;
42af212
+    }
42af212
+
42af212
+    ret = vnc_client_write_buf(vs,
42af212
+			       vs->sasl.encoded + vs->sasl.encodedOffset,
42af212
+			       vs->sasl.encodedLength - vs->sasl.encodedOffset);
42af212
+    if (!ret)
42af212
+	return 0;
42af212
+
42af212
+    vs->sasl.encodedOffset += ret;
42af212
+    if (vs->sasl.encodedOffset == vs->sasl.encodedLength) {
42af212
+	vs->output.offset = 0;
42af212
+	vs->sasl.encoded = NULL;
42af212
+	vs->sasl.encodedOffset = vs->sasl.encodedLength = 0;
42af212
+    }
42af212
+
42af212
+    /* Can't merge this block with one above, because
42af212
+     * someone might have written more unencrypted
42af212
+     * data in vs->output while we were processing
42af212
+     * SASL encoded output
42af212
+     */
42af212
+    if (vs->output.offset == 0) {
42af212
+	qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
42af212
+    }
42af212
+
42af212
+    return ret;
42af212
+}
42af212
+
42af212
+
42af212
+long vnc_client_read_sasl(VncState *vs)
42af212
+{
42af212
+    long ret;
42af212
+    uint8_t encoded[4096];
42af212
+    const char *decoded;
42af212
+    unsigned int decodedLen;
42af212
+    int err;
42af212
+
42af212
+    ret = vnc_client_read_buf(vs, encoded, sizeof(encoded));
42af212
+    if (!ret)
42af212
+	return 0;
42af212
+
42af212
+    err = sasl_decode(vs->sasl.conn,
42af212
+		      (char *)encoded, ret,
42af212
+		      &decoded, &decodedLen);
42af212
+
42af212
+    if (err != SASL_OK)
42af212
+	return vnc_client_io_error(vs, -1, -EIO);
42af212
+    VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
42af212
+	      encoded, ret, decoded, decodedLen);
42af212
+    buffer_reserve(&vs->input, decodedLen);
42af212
+    buffer_append(&vs->input, decoded, decodedLen);
42af212
+    return decodedLen;
42af212
+}
42af212
+
42af212
+
42af212
+static int vnc_auth_sasl_check_access(VncState *vs)
42af212
+{
42af212
+    const void *val;
42af212
+    int err;
42af212
+
42af212
+    err = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val;;
42af212
+    if (err != SASL_OK) {
42af212
+	VNC_DEBUG("cannot query SASL username on connection %d (%s)\n",
42af212
+		  err, sasl_errstring(err, NULL, NULL));
42af212
+	return -1;
42af212
+    }
42af212
+    if (val == NULL) {
42af212
+	VNC_DEBUG("no client username was found\n");
42af212
+	return -1;
42af212
+    }
42af212
+    VNC_DEBUG("SASL client username %s\n", (const char *)val);
42af212
+
42af212
+    vs->sasl.username = qemu_strdup((const char*)val);
42af212
+
42af212
+    return 0;
42af212
+}
42af212
+
42af212
+static int vnc_auth_sasl_check_ssf(VncState *vs)
42af212
+{
42af212
+    const void *val;
42af212
+    int err, ssf;
42af212
+
42af212
+    if (!vs->sasl.wantSSF)
42af212
+	return 1;
42af212
+
42af212
+    err = sasl_getprop(vs->sasl.conn, SASL_SSF, &val;;
42af212
+    if (err != SASL_OK)
42af212
+	return 0;
42af212
+
42af212
+    ssf = *(const int *)val;
42af212
+    VNC_DEBUG("negotiated an SSF of %d\n", ssf);
42af212
+    if (ssf < 56)
42af212
+	return 0; /* 56 is good for Kerberos */
42af212
+
42af212
+    /* Only setup for read initially, because we're about to send an RPC
42af212
+     * reply which must be in plain text. When the next incoming RPC
42af212
+     * arrives, we'll switch on writes too
42af212
+     *
42af212
+     * cf qemudClientReadSASL  in qemud.c
42af212
+     */
42af212
+    vs->sasl.runSSF = 1;
42af212
+
42af212
+    /* We have a SSF that's good enough */
42af212
+    return 1;
42af212
+}
42af212
+
42af212
+/*
42af212
+ * Step Msg
42af212
+ *
42af212
+ * Input from client:
42af212
+ *
42af212
+ * u32 clientin-length
42af212
+ * u8-array clientin-string
42af212
+ *
42af212
+ * Output to client:
42af212
+ *
42af212
+ * u32 serverout-length
42af212
+ * u8-array serverout-strin
42af212
+ * u8 continue
42af212
+ */
42af212
+
42af212
+static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len);
42af212
+
42af212
+static int protocol_client_auth_sasl_step(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    uint32_t datalen = len;
42af212
+    const char *serverout;
42af212
+    unsigned int serveroutlen;
42af212
+    int err;
42af212
+    char *clientdata = NULL;
42af212
+
42af212
+    /* NB, distinction of NULL vs "" is *critical* in SASL */
42af212
+    if (datalen) {
42af212
+	clientdata = (char*)data;
42af212
+	clientdata[datalen-1] = '\0'; /* Wire includes '\0', but make sure */
42af212
+	datalen--; /* Don't count NULL byte when passing to _start() */
42af212
+    }
42af212
+
42af212
+    VNC_DEBUG("Step using SASL Data %p (%d bytes)\n",
42af212
+	      clientdata, datalen);
42af212
+    err = sasl_server_step(vs->sasl.conn,
42af212
+			   clientdata,
42af212
+			   datalen,
42af212
+			   &serverout,
42af212
+			   &serveroutlen);
42af212
+    if (err != SASL_OK &&
42af212
+	err != SASL_CONTINUE) {
42af212
+	VNC_DEBUG("sasl step failed %d (%s)\n",
42af212
+		  err, sasl_errdetail(vs->sasl.conn));
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+    if (serveroutlen > SASL_DATA_MAX_LEN) {
42af212
+	VNC_DEBUG("sasl step reply data too long %d\n",
42af212
+		  serveroutlen);
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+    VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
42af212
+	      serveroutlen, serverout ? 0 : 1);
42af212
+
42af212
+    if (serveroutlen) {
42af212
+	vnc_write_u32(vs, serveroutlen + 1);
42af212
+	vnc_write(vs, serverout, serveroutlen + 1);
42af212
+    } else {
42af212
+	vnc_write_u32(vs, 0);
42af212
+    }
42af212
+
42af212
+    /* Whether auth is complete */
42af212
+    vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
42af212
+
42af212
+    if (err == SASL_CONTINUE) {
42af212
+	VNC_DEBUG("%s", "Authentication must continue\n");
42af212
+	/* Wait for step length */
42af212
+	vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
42af212
+    } else {
42af212
+	if (!vnc_auth_sasl_check_ssf(vs)) {
42af212
+	    VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs->csock);
42af212
+	    goto authreject;
42af212
+	}
42af212
+
42af212
+	/* Check username whitelist ACL */
42af212
+	if (vnc_auth_sasl_check_access(vs) < 0) {
42af212
+	    VNC_DEBUG("Authentication rejected for ACL %d\n", vs->csock);
42af212
+	    goto authreject;
42af212
+	}
42af212
+
42af212
+	VNC_DEBUG("Authentication successful %d\n", vs->csock);
42af212
+	vnc_write_u32(vs, 0); /* Accept auth */
42af212
+	/*
42af212
+	 * Delay writing in SSF encoded mode until pending output
42af212
+	 * buffer is written
42af212
+	 */
42af212
+	if (vs->sasl.runSSF)
42af212
+	    vs->sasl.waitWriteSSF = vs->output.offset;
42af212
+	start_client_init(vs);
42af212
+    }
42af212
+
42af212
+    return 0;
42af212
+
42af212
+ authreject:
42af212
+    vnc_write_u32(vs, 1); /* Reject auth */
42af212
+    vnc_write_u32(vs, sizeof("Authentication failed"));
42af212
+    vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
42af212
+    vnc_flush(vs);
42af212
+    vnc_client_error(vs);
42af212
+    return -1;
42af212
+
42af212
+ authabort:
42af212
+    vnc_client_error(vs);
42af212
+    return -1;
42af212
+}
42af212
+
42af212
+static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    uint32_t steplen = read_u32(data, 0);
42af212
+    VNC_DEBUG("Got client step len %d\n", steplen);
42af212
+    if (steplen > SASL_DATA_MAX_LEN) {
42af212
+	VNC_DEBUG("Too much SASL data %d\n", steplen);
42af212
+	vnc_client_error(vs);
42af212
+	return -1;
42af212
+    }
42af212
+
42af212
+    if (steplen == 0)
42af212
+	return protocol_client_auth_sasl_step(vs, NULL, 0);
42af212
+    else
42af212
+	vnc_read_when(vs, protocol_client_auth_sasl_step, steplen);
42af212
+    return 0;
42af212
+}
42af212
+
42af212
+/*
42af212
+ * Start Msg
42af212
+ *
42af212
+ * Input from client:
42af212
+ *
42af212
+ * u32 clientin-length
42af212
+ * u8-array clientin-string
42af212
+ *
42af212
+ * Output to client:
42af212
+ *
42af212
+ * u32 serverout-length
42af212
+ * u8-array serverout-strin
42af212
+ * u8 continue
42af212
+ */
42af212
+
42af212
+#define SASL_DATA_MAX_LEN (1024 * 1024)
42af212
+
42af212
+static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    uint32_t datalen = len;
42af212
+    const char *serverout;
42af212
+    unsigned int serveroutlen;
42af212
+    int err;
42af212
+    char *clientdata = NULL;
42af212
+
42af212
+    /* NB, distinction of NULL vs "" is *critical* in SASL */
42af212
+    if (datalen) {
42af212
+	clientdata = (char*)data;
42af212
+	clientdata[datalen-1] = '\0'; /* Should be on wire, but make sure */
42af212
+	datalen--; /* Don't count NULL byte when passing to _start() */
42af212
+    }
42af212
+
42af212
+    VNC_DEBUG("Start SASL auth with mechanism %s. Data %p (%d bytes)\n",
42af212
+	      vs->sasl.mechlist, clientdata, datalen);
42af212
+    err = sasl_server_start(vs->sasl.conn,
42af212
+			    vs->sasl.mechlist,
42af212
+			    clientdata,
42af212
+			    datalen,
42af212
+			    &serverout,
42af212
+			    &serveroutlen);
42af212
+    if (err != SASL_OK &&
42af212
+	err != SASL_CONTINUE) {
42af212
+	VNC_DEBUG("sasl start failed %d (%s)\n",
42af212
+		  err, sasl_errdetail(vs->sasl.conn));
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+    if (serveroutlen > SASL_DATA_MAX_LEN) {
42af212
+	VNC_DEBUG("sasl start reply data too long %d\n",
42af212
+		  serveroutlen);
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+    VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
42af212
+	      serveroutlen, serverout ? 0 : 1);
42af212
+
42af212
+    if (serveroutlen) {
42af212
+	vnc_write_u32(vs, serveroutlen + 1);
42af212
+	vnc_write(vs, serverout, serveroutlen + 1);
42af212
+    } else {
42af212
+	vnc_write_u32(vs, 0);
42af212
+    }
42af212
+
42af212
+    /* Whether auth is complete */
42af212
+    vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
42af212
+
42af212
+    if (err == SASL_CONTINUE) {
42af212
+	VNC_DEBUG("%s", "Authentication must continue\n");
42af212
+	/* Wait for step length */
42af212
+	vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
42af212
+    } else {
42af212
+	if (!vnc_auth_sasl_check_ssf(vs)) {
42af212
+	    VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs->csock);
42af212
+	    goto authreject;
42af212
+	}
42af212
+
42af212
+	/* Check username whitelist ACL */
42af212
+	if (vnc_auth_sasl_check_access(vs) < 0) {
42af212
+	    VNC_DEBUG("Authentication rejected for ACL %d\n", vs->csock);
42af212
+	    goto authreject;
42af212
+	}
42af212
+
42af212
+	VNC_DEBUG("Authentication successful %d\n", vs->csock);
42af212
+	vnc_write_u32(vs, 0); /* Accept auth */
42af212
+	start_client_init(vs);
42af212
+    }
42af212
+
42af212
+    return 0;
42af212
+
42af212
+ authreject:
42af212
+    vnc_write_u32(vs, 1); /* Reject auth */
42af212
+    vnc_write_u32(vs, sizeof("Authentication failed"));
42af212
+    vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
42af212
+    vnc_flush(vs);
42af212
+    vnc_client_error(vs);
42af212
+    return -1;
42af212
+
42af212
+ authabort:
42af212
+    vnc_client_error(vs);
42af212
+    return -1;
42af212
+}
42af212
+
42af212
+static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    uint32_t startlen = read_u32(data, 0);
42af212
+    VNC_DEBUG("Got client start len %d\n", startlen);
42af212
+    if (startlen > SASL_DATA_MAX_LEN) {
42af212
+	VNC_DEBUG("Too much SASL data %d\n", startlen);
42af212
+	vnc_client_error(vs);
42af212
+	return -1;
42af212
+    }
42af212
+
42af212
+    if (startlen == 0)
42af212
+	return protocol_client_auth_sasl_start(vs, NULL, 0);
42af212
+
42af212
+    vnc_read_when(vs, protocol_client_auth_sasl_start, startlen);
42af212
+    return 0;
42af212
+}
42af212
+
42af212
+static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    char *mechname = malloc(len + 1);
42af212
+    if (!mechname) {
42af212
+	VNC_DEBUG("Out of memory reading mechname\n");
42af212
+	vnc_client_error(vs);
42af212
+    }
42af212
+    strncpy(mechname, (char*)data, len);
42af212
+    mechname[len] = '\0';
42af212
+    VNC_DEBUG("Got client mechname '%s' check against '%s'\n",
42af212
+	      mechname, vs->sasl.mechlist);
42af212
+
42af212
+    if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
42af212
+	if (vs->sasl.mechlist[len] != '\0' &&
42af212
+	    vs->sasl.mechlist[len] != ',') {
42af212
+	    VNC_DEBUG("One %d", vs->sasl.mechlist[len]);
42af212
+	    vnc_client_error(vs);
42af212
+	    return -1;
42af212
+	}
42af212
+    } else {
42af212
+	char *offset = strstr(vs->sasl.mechlist, mechname);
42af212
+	VNC_DEBUG("Two %p\n", offset);
42af212
+	if (!offset) {
42af212
+	    vnc_client_error(vs);
42af212
+	    return -1;
42af212
+	}
42af212
+	VNC_DEBUG("Two '%s'\n", offset);
42af212
+	if (offset[-1] != ',' ||
42af212
+	    (offset[len] != '\0'&&
42af212
+	     offset[len] != ',')) {
42af212
+	    vnc_client_error(vs);
42af212
+	    return -1;
42af212
+	}
42af212
+    }
42af212
+
42af212
+    free(vs->sasl.mechlist);
42af212
+    vs->sasl.mechlist = mechname;
42af212
+
42af212
+    VNC_DEBUG("Validated mechname '%s'\n", mechname);
42af212
+    vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4);
42af212
+    return 0;
42af212
+}
42af212
+
42af212
+static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len)
42af212
+{
42af212
+    uint32_t mechlen = read_u32(data, 0);
42af212
+    VNC_DEBUG("Got client mechname len %d\n", mechlen);
42af212
+    if (mechlen > 100) {
42af212
+	VNC_DEBUG("Too long SASL mechname data %d\n", mechlen);
42af212
+	vnc_client_error(vs);
42af212
+	return -1;
42af212
+    }
42af212
+    if (mechlen < 1) {
42af212
+	VNC_DEBUG("Too short SASL mechname %d\n", mechlen);
42af212
+	vnc_client_error(vs);
42af212
+	return -1;
42af212
+    }
42af212
+    vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen);
42af212
+    return 0;
42af212
+}
42af212
+
42af212
+#define USES_X509_AUTH(vs)			      \
42af212
+    ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE ||   \
42af212
+     (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC ||    \
42af212
+     (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN ||  \
42af212
+     (vs)->subauth == VNC_AUTH_VENCRYPT_X509SASL)
42af212
+
42af212
+
42af212
+void start_auth_sasl(VncState *vs)
42af212
+{
42af212
+    const char *mechlist = NULL;
42af212
+    sasl_security_properties_t secprops;
42af212
+    int err;
42af212
+    char *localAddr, *remoteAddr;
42af212
+    int mechlistlen;
42af212
+
42af212
+    VNC_DEBUG("Initialize SASL auth %d\n", vs->csock);
42af212
+
42af212
+    /* Get local & remote client addresses in form  IPADDR;PORT */
42af212
+    if (!(localAddr = vnc_socket_local_addr("%s;%s", vs->csock)))
42af212
+	goto authabort;
42af212
+
42af212
+    if (!(remoteAddr = vnc_socket_remote_addr("%s;%s", vs->csock))) {
42af212
+	free(localAddr);
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+    err = sasl_server_new("vnc",
42af212
+			  NULL, /* FQDN - just delegates to gethostname */
42af212
+			  NULL, /* User realm */
42af212
+			  localAddr,
42af212
+			  remoteAddr,
42af212
+			  NULL, /* Callbacks, not needed */
42af212
+			  SASL_SUCCESS_DATA,
42af212
+			  &vs->sasl.conn);
42af212
+    free(localAddr);
42af212
+    free(remoteAddr);
42af212
+    localAddr = remoteAddr = NULL;
42af212
+
42af212
+    if (err != SASL_OK) {
42af212
+	VNC_DEBUG("sasl context setup failed %d (%s)",
42af212
+		  err, sasl_errstring(err, NULL, NULL));
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+#ifdef CONFIG_VNC_TLS
42af212
+    /* Inform SASL that we've got an external SSF layer from TLS/x509 */
42af212
+    if (vs->vd->auth == VNC_AUTH_VENCRYPT &&
42af212
+	vs->vd->subauth == VNC_AUTH_VENCRYPT_X509SASL) {
42af212
+	gnutls_cipher_algorithm_t cipher;
42af212
+	sasl_ssf_t ssf;
42af212
+
42af212
+	cipher = gnutls_cipher_get(vs->tls.session);
42af212
+	if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
42af212
+	    VNC_DEBUG("%s", "cannot TLS get cipher size\n");
42af212
+	    sasl_dispose(&vs->sasl.conn);
42af212
+	    vs->sasl.conn = NULL;
42af212
+	    goto authabort;
42af212
+	}
42af212
+	ssf *= 8; /* tls key size is bytes, sasl wants bits */
42af212
+
42af212
+	err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf;;
42af212
+	if (err != SASL_OK) {
42af212
+	    VNC_DEBUG("cannot set SASL external SSF %d (%s)\n",
42af212
+		      err, sasl_errstring(err, NULL, NULL));
42af212
+	    sasl_dispose(&vs->sasl.conn);
42af212
+	    vs->sasl.conn = NULL;
42af212
+	    goto authabort;
42af212
+	}
42af212
+    } else
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
+	vs->sasl.wantSSF = 1;
42af212
+
42af212
+    memset (&secprops, 0, sizeof secprops);
42af212
+    /* Inform SASL that we've got an external SSF layer from TLS */
42af212
+    if (strncmp(vs->vd->display, "unix:", 5) == 0
42af212
+#ifdef CONFIG_VNC_TLS
42af212
+	/* Disable SSF, if using TLS+x509+SASL only. TLS without x509
42af212
+	   is not sufficiently strong */
42af212
+	|| (vs->vd->auth == VNC_AUTH_VENCRYPT &&
42af212
+	    vs->vd->subauth == VNC_AUTH_VENCRYPT_X509SASL)
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
+	) {
42af212
+	/* If we've got TLS or UNIX domain sock, we don't care about SSF */
42af212
+	secprops.min_ssf = 0;
42af212
+	secprops.max_ssf = 0;
42af212
+	secprops.maxbufsize = 8192;
42af212
+	secprops.security_flags = 0;
42af212
+    } else {
42af212
+	/* Plain TCP, better get an SSF layer */
42af212
+	secprops.min_ssf = 56; /* Good enough to require kerberos */
42af212
+	secprops.max_ssf = 100000; /* Arbitrary big number */
42af212
+	secprops.maxbufsize = 8192;
42af212
+	/* Forbid any anonymous or trivially crackable auth */
42af212
+	secprops.security_flags =
42af212
+	    SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
42af212
+    }
42af212
+
42af212
+    err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops);
42af212
+    if (err != SASL_OK) {
42af212
+	VNC_DEBUG("cannot set SASL security props %d (%s)\n",
42af212
+		  err, sasl_errstring(err, NULL, NULL));
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+
42af212
+    err = sasl_listmech(vs->sasl.conn,
42af212
+			NULL, /* Don't need to set user */
42af212
+			"", /* Prefix */
42af212
+			",", /* Separator */
42af212
+			"", /* Suffix */
42af212
+			&mechlist,
42af212
+			NULL,
42af212
+			NULL);
42af212
+    if (err != SASL_OK) {
42af212
+	VNC_DEBUG("cannot list SASL mechanisms %d (%s)\n",
42af212
+		  err, sasl_errdetail(vs->sasl.conn));
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+    VNC_DEBUG("Available mechanisms for client: '%s'\n", mechlist);
42af212
+
42af212
+    if (!(vs->sasl.mechlist = strdup(mechlist))) {
42af212
+	VNC_DEBUG("Out of memory");
42af212
+	sasl_dispose(&vs->sasl.conn);
42af212
+	vs->sasl.conn = NULL;
42af212
+	goto authabort;
42af212
+    }
42af212
+    mechlistlen = strlen(mechlist);
42af212
+    vnc_write_u32(vs, mechlistlen);
42af212
+    vnc_write(vs, mechlist, mechlistlen);
42af212
+    vnc_flush(vs);
42af212
+
42af212
+    VNC_DEBUG("Wait for client mechname length\n");
42af212
+    vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4);
42af212
+
42af212
+    return;
42af212
+
42af212
+ authabort:
42af212
+    vnc_client_error(vs);
42af212
+    return;
42af212
+}
42af212
+
42af212
+
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/vnc-auth-sasl.h
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- /dev/null
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/vnc-auth-sasl.h
42af212
@@ -0,0 +1,67 @@
42af212
+/*
42af212
+ * QEMU VNC display driver: SASL auth protocol
42af212
+ *
42af212
+ * Copyright (C) 2009 Red Hat, Inc
42af212
+ *
42af212
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
42af212
+ * of this software and associated documentation files (the "Software"), to deal
42af212
+ * in the Software without restriction, including without limitation the rights
42af212
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42af212
+ * copies of the Software, and to permit persons to whom the Software is
42af212
+ * furnished to do so, subject to the following conditions:
42af212
+ *
42af212
+ * The above copyright notice and this permission notice shall be included in
42af212
+ * all copies or substantial portions of the Software.
42af212
+ *
42af212
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42af212
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42af212
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
42af212
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42af212
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42af212
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42af212
+ * THE SOFTWARE.
42af212
+ */
42af212
+
42af212
+
42af212
+#ifndef __QEMU_VNC_AUTH_SASL_H__
42af212
+#define __QEMU_VNC_AUTH_SASL_H__
42af212
+
42af212
+
42af212
+#include <sasl/sasl.h>
42af212
+
42af212
+typedef struct VncStateSASL VncStateSASL;
42af212
+
42af212
+struct VncStateSASL {
42af212
+    sasl_conn_t *conn;
42af212
+    /* If we want to negotiate an SSF layer with client */
42af212
+    int wantSSF :1;
42af212
+    /* If we are now running the SSF layer */
42af212
+    int runSSF :1;
42af212
+    /*
42af212
+     * If this is non-zero, then wait for that many bytes
42af212
+     * to be written plain, before switching to SSF encoding
42af212
+     * This allows the VNC auth result to finish being
42af212
+     * written in plain.
42af212
+     */
42af212
+    unsigned int waitWriteSSF;
42af212
+
42af212
+    /*
42af212
+     * Buffering encoded data to allow more clear data
42af212
+     * to be stuffed onto the output buffer
42af212
+     */
42af212
+    const uint8_t *encoded;
42af212
+    unsigned int encodedLength;
42af212
+    unsigned int encodedOffset;
42af212
+    char *username;
42af212
+    char *mechlist;
42af212
+};
42af212
+
42af212
+void vnc_sasl_client_cleanup(VncState *vs);
42af212
+
42af212
+long vnc_client_read_sasl(VncState *vs);
42af212
+long vnc_client_write_sasl(VncState *vs);
42af212
+
42af212
+void start_auth_sasl(VncState *vs);
42af212
+
42af212
+#endif /* __QEMU_VNC_AUTH_SASL_H__ */
42af212
+
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/vnc-auth-vencrypt.c
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/vnc-auth-vencrypt.c
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/vnc-auth-vencrypt.c
42af212
@@ -43,8 +43,15 @@ static void start_auth_vencrypt_subauth(
42af212
        start_auth_vnc(vs);
42af212
        break;
42af212
 
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    case VNC_AUTH_VENCRYPT_TLSSASL:
42af212
+    case VNC_AUTH_VENCRYPT_X509SASL:
42af212
+      VNC_DEBUG("Start TLS auth SASL\n");
42af212
+      return start_auth_sasl(vs);
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
+
42af212
     default: /* Should not be possible, but just in case */
42af212
-       VNC_DEBUG("Reject auth %d\n", vs->vd->auth);
42af212
+       VNC_DEBUG("Reject subauth %d server bug\n", vs->vd->auth);
42af212
        vnc_write_u8(vs, 1);
42af212
        if (vs->minor >= 8) {
42af212
            static const char err[] = "Unsupported authentication type";
42af212
@@ -105,7 +112,8 @@ static void vnc_tls_handshake_io(void *o
42af212
 #define NEED_X509_AUTH(vs)			      \
42af212
     ((vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509NONE ||   \
42af212
      (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509VNC ||    \
42af212
-     (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
42af212
+     (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509PLAIN ||  \
42af212
+     (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509SASL)
42af212
 
42af212
 
42af212
 static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/vnc.c
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/vnc.c
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/vnc.c
42af212
@@ -68,7 +68,8 @@ static char *addr_to_string(const char *
42af212
     return addr;
42af212
 }
42af212
 
42af212
-static char *vnc_socket_local_addr(const char *format, int fd) {
42af212
+
42af212
+char *vnc_socket_local_addr(const char *format, int fd) {
42af212
     struct sockaddr_storage sa;
42af212
     socklen_t salen;
42af212
 
42af212
@@ -79,7 +80,8 @@ static char *vnc_socket_local_addr(const
42af212
     return addr_to_string(format, &sa, salen);
42af212
 }
42af212
 
42af212
-static char *vnc_socket_remote_addr(const char *format, int fd) {
42af212
+
42af212
+char *vnc_socket_remote_addr(const char *format, int fd) {
42af212
     struct sockaddr_storage sa;
42af212
     socklen_t salen;
42af212
 
42af212
@@ -125,12 +127,18 @@ static const char *vnc_auth_name(VncDisp
42af212
 	    return "vencrypt+x509+vnc";
42af212
 	case VNC_AUTH_VENCRYPT_X509PLAIN:
42af212
 	    return "vencrypt+x509+plain";
42af212
+	case VNC_AUTH_VENCRYPT_TLSSASL:
42af212
+	    return "vencrypt+tls+sasl";
42af212
+	case VNC_AUTH_VENCRYPT_X509SASL:
42af212
+	    return "vencrypt+x509+sasl";
42af212
 	default:
42af212
 	    return "vencrypt";
42af212
 	}
42af212
 #else
42af212
 	return "vencrypt";
42af212
 #endif
42af212
+    case VNC_AUTH_SASL:
42af212
+	return "sasl";
42af212
     }
42af212
     return "unknown";
42af212
 }
42af212
@@ -278,7 +286,7 @@ static void vnc_framebuffer_update(VncSt
42af212
     vnc_write_s32(vs, encoding);
42af212
 }
42af212
 
42af212
-static void buffer_reserve(Buffer *buffer, size_t len)
42af212
+void buffer_reserve(Buffer *buffer, size_t len)
42af212
 {
42af212
     if ((buffer->capacity - buffer->offset) < len) {
42af212
 	buffer->capacity += (len + 1024);
42af212
@@ -290,22 +298,22 @@ static void buffer_reserve(Buffer *buffe
42af212
     }
42af212
 }
42af212
 
42af212
-static int buffer_empty(Buffer *buffer)
42af212
+int buffer_empty(Buffer *buffer)
42af212
 {
42af212
     return buffer->offset == 0;
42af212
 }
42af212
 
42af212
-static uint8_t *buffer_end(Buffer *buffer)
42af212
+uint8_t *buffer_end(Buffer *buffer)
42af212
 {
42af212
     return buffer->buffer + buffer->offset;
42af212
 }
42af212
 
42af212
-static void buffer_reset(Buffer *buffer)
42af212
+void buffer_reset(Buffer *buffer)
42af212
 {
42af212
 	buffer->offset = 0;
42af212
 }
42af212
 
42af212
-static void buffer_append(Buffer *buffer, const void *data, size_t len)
42af212
+void buffer_append(Buffer *buffer, const void *data, size_t len)
42af212
 {
42af212
     memcpy(buffer->buffer + buffer->offset, data, len);
42af212
     buffer->offset += len;
42af212
@@ -821,7 +829,8 @@ static void audio_del(VncState *vs)
42af212
     }
42af212
 }
42af212
 
42af212
-static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
42af212
+
42af212
+int vnc_client_io_error(VncState *vs, int ret, int last_errno)
42af212
 {
42af212
     if (ret == 0 || ret == -1) {
42af212
         if (ret == -1) {
42af212
@@ -847,6 +856,9 @@ static int vnc_client_io_error(VncState 
42af212
 #ifdef CONFIG_VNC_TLS
42af212
 	vnc_tls_client_cleanup(vs);
42af212
 #endif /* CONFIG_VNC_TLS */
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+        vnc_sasl_client_cleanup(vs);
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
         audio_del(vs);
42af212
 
42af212
         VncState *p, *parent = NULL;
42af212
@@ -877,14 +889,28 @@ void vnc_client_error(VncState *vs)
42af212
     vnc_client_io_error(vs, -1, EINVAL);
42af212
 }
42af212
 
42af212
-void vnc_client_write(void *opaque)
42af212
+
42af212
+/*
42af212
+ * Called to write a chunk of data to the client socket. The data may
42af212
+ * be the raw data, or may have already been encoded by SASL.
42af212
+ * The data will be written either straight onto the socket, or
42af212
+ * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
42af212
+ *
42af212
+ * NB, it is theoretically possible to have 2 layers of encryption,
42af212
+ * both SASL, and this TLS layer. It is highly unlikely in practice
42af212
+ * though, since SASL encryption will typically be a no-op if TLS
42af212
+ * is active
42af212
+ *
42af212
+ * Returns the number of bytes written, which may be less than
42af212
+ * the requested 'datalen' if the socket would block. Returns
42af212
+ * -1 on error, and disconnects the client socket.
42af212
+ */
42af212
+long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
42af212
 {
42af212
     long ret;
42af212
-    VncState *vs = opaque;
42af212
-
42af212
 #ifdef CONFIG_VNC_TLS
42af212
     if (vs->tls.session) {
42af212
-	ret = gnutls_write(vs->tls.session, vs->output.buffer, vs->output.offset);
42af212
+	ret = gnutls_write(vs->tls.session, data, datalen);
42af212
 	if (ret < 0) {
42af212
 	    if (ret == GNUTLS_E_AGAIN)
42af212
 		errno = EAGAIN;
42af212
@@ -894,10 +920,42 @@ void vnc_client_write(void *opaque)
42af212
 	}
42af212
     } else
42af212
 #endif /* CONFIG_VNC_TLS */
42af212
-	ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
42af212
-    ret = vnc_client_io_error(vs, ret, socket_error());
42af212
+	ret = send(vs->csock, data, datalen, 0);
42af212
+    VNC_DEBUG("Wrote wire %p %d -> %ld\n", data, datalen, ret);
42af212
+    return vnc_client_io_error(vs, ret, socket_error());
42af212
+}
42af212
+
42af212
+
42af212
+/*
42af212
+ * Called to write buffered data to the client socket, when not
42af212
+ * using any SASL SSF encryption layers. Will write as much data
42af212
+ * as possible without blocking. If all buffered data is written,
42af212
+ * will switch the FD poll() handler back to read monitoring.
42af212
+ *
42af212
+ * Returns the number of bytes written, which may be less than
42af212
+ * the buffered output data if the socket would block. Returns
42af212
+ * -1 on error, and disconnects the client socket.
42af212
+ */
42af212
+static long vnc_client_write_plain(VncState *vs)
42af212
+{
42af212
+    long ret;
42af212
+
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    VNC_DEBUG("Write Plain: Pending output %p size %d offset %d. Wait SSF %d\n",
42af212
+              vs->output.buffer, vs->output.capacity, vs->output.offset,
42af212
+              vs->sasl.waitWriteSSF);
42af212
+
42af212
+    if (vs->sasl.conn &&
42af212
+        vs->sasl.runSSF &&
42af212
+        vs->sasl.waitWriteSSF) {
42af212
+        ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
42af212
+        if (ret)
42af212
+            vs->sasl.waitWriteSSF -= ret;
42af212
+    } else
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
+        ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
42af212
     if (!ret)
42af212
-	return;
42af212
+        return 0;
42af212
 
42af212
     memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
42af212
     vs->output.offset -= ret;
42af212
@@ -905,6 +963,29 @@ void vnc_client_write(void *opaque)
42af212
     if (vs->output.offset == 0) {
42af212
 	qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
42af212
     }
42af212
+
42af212
+    return ret;
42af212
+}
42af212
+
42af212
+
42af212
+/*
42af212
+ * First function called whenever there is data to be written to
42af212
+ * the client socket. Will delegate actual work according to whether
42af212
+ * SASL SSF layers are enabled (thus requiring encryption calls)
42af212
+ */
42af212
+void vnc_client_write(void *opaque)
42af212
+{
42af212
+    long ret;
42af212
+    VncState *vs = opaque;
42af212
+
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    if (vs->sasl.conn &&
42af212
+        vs->sasl.runSSF &&
42af212
+        !vs->sasl.waitWriteSSF)
42af212
+        ret = vnc_client_write_sasl(vs);
42af212
+    else
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
+        ret = vnc_client_write_plain(vs);
42af212
 }
42af212
 
42af212
 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
42af212
@@ -913,16 +994,28 @@ void vnc_read_when(VncState *vs, VncRead
42af212
     vs->read_handler_expect = expecting;
42af212
 }
42af212
 
42af212
-void vnc_client_read(void *opaque)
42af212
+
42af212
+/*
42af212
+ * Called to read a chunk of data from the client socket. The data may
42af212
+ * be the raw data, or may need to be further decoded by SASL.
42af212
+ * The data will be read either straight from to the socket, or
42af212
+ * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
42af212
+ *
42af212
+ * NB, it is theoretically possible to have 2 layers of encryption,
42af212
+ * both SASL, and this TLS layer. It is highly unlikely in practice
42af212
+ * though, since SASL encryption will typically be a no-op if TLS
42af212
+ * is active
42af212
+ *
42af212
+ * Returns the number of bytes read, which may be less than
42af212
+ * the requested 'datalen' if the socket would block. Returns
42af212
+ * -1 on error, and disconnects the client socket.
42af212
+ */
42af212
+long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
42af212
 {
42af212
-    VncState *vs = opaque;
42af212
     long ret;
42af212
-
42af212
-    buffer_reserve(&vs->input, 4096);
42af212
-
42af212
 #ifdef CONFIG_VNC_TLS
42af212
     if (vs->tls.session) {
42af212
-	ret = gnutls_read(vs->tls.session, buffer_end(&vs->input), 4096);
42af212
+	ret = gnutls_read(vs->tls.session, data, datalen);
42af212
 	if (ret < 0) {
42af212
 	    if (ret == GNUTLS_E_AGAIN)
42af212
 		errno = EAGAIN;
Glauber Costa 8571d06
@@ -932,12 +1025,52 @@ void vnc_client_read(void *opaque)
42af212
 	}
42af212
     } else
42af212
 #endif /* CONFIG_VNC_TLS */
42af212
-	ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
42af212
-    ret = vnc_client_io_error(vs, ret, socket_error());
Glauber Costa 8571d06
-    if (!ret)
Glauber Costa 8571d06
-	return;
42af212
+	ret = recv(vs->csock, data, datalen, 0);
42af212
+    VNC_DEBUG("Read wire %p %d -> %ld\n", data, datalen, ret);
42af212
+    return vnc_client_io_error(vs, ret, socket_error());
42af212
+}
Glauber Costa 8571d06
 
42af212
+
42af212
+/*
42af212
+ * Called to read data from the client socket to the input buffer,
42af212
+ * when not using any SASL SSF encryption layers. Will read as much
42af212
+ * data as possible without blocking.
42af212
+ *
42af212
+ * Returns the number of bytes read. Returns -1 on error, and
42af212
+ * disconnects the client socket.
42af212
+ */
42af212
+static long vnc_client_read_plain(VncState *vs)
42af212
+{
42af212
+    int ret;
42af212
+    VNC_DEBUG("Read plain %p size %d offset %d\n",
42af212
+              vs->input.buffer, vs->input.capacity, vs->input.offset);
42af212
+    buffer_reserve(&vs->input, 4096);
42af212
+    ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
42af212
+    if (!ret)
42af212
+        return 0;
Glauber Costa 8571d06
     vs->input.offset += ret;
42af212
+    return ret;
42af212
+}
42af212
+
42af212
+
42af212
+/*
42af212
+ * First function called whenever there is more data to be read from
42af212
+ * the client socket. Will delegate actual work according to whether
42af212
+ * SASL SSF layers are enabled (thus requiring decryption calls)
42af212
+ */
42af212
+void vnc_client_read(void *opaque)
42af212
+{
42af212
+    VncState *vs = opaque;
42af212
+    long ret;
42af212
+
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    if (vs->sasl.conn && vs->sasl.runSSF)
42af212
+        ret = vnc_client_read_sasl(vs);
42af212
+    else
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
+        ret = vnc_client_read_plain(vs);
Glauber Costa 8571d06
+    if (!ret)
Glauber Costa 8571d06
+	return;
42af212
 
42af212
     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
42af212
 	size_t len = vs->read_handler_expect;
42af212
@@ -1722,6 +1855,13 @@ static int protocol_client_auth(VncState
42af212
            break;
42af212
 #endif /* CONFIG_VNC_TLS */
42af212
 
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+       case VNC_AUTH_SASL:
42af212
+           VNC_DEBUG("Accept SASL auth\n");
42af212
+           start_auth_sasl(vs);
42af212
+           break;
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
+
42af212
        default: /* Should not be possible, but just in case */
42af212
            VNC_DEBUG("Reject auth %d\n", vs->vd->auth);
42af212
            vnc_write_u8(vs, 1);
42af212
@@ -1923,6 +2063,10 @@ int vnc_display_open(DisplayState *ds, c
42af212
 #ifdef CONFIG_VNC_TLS
42af212
     int tls = 0, x509 = 0;
42af212
 #endif
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    int sasl = 0;
42af212
+    int saslErr;
42af212
+#endif
42af212
 
42af212
     if (!vnc_display)
42af212
         return -1;
42af212
@@ -1942,6 +2086,10 @@ int vnc_display_open(DisplayState *ds, c
42af212
 	    reverse = 1;
42af212
 	} else if (strncmp(options, "to=", 3) == 0) {
42af212
             to_port = atoi(options+3) + 5900;
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+	} else if (strncmp(options, "sasl", 4) == 0) {
42af212
+	    sasl = 1; /* Require SASL auth */
42af212
+#endif
42af212
 #ifdef CONFIG_VNC_TLS
42af212
 	} else if (strncmp(options, "tls", 3) == 0) {
42af212
 	    tls = 1; /* Require TLS */
42af212
@@ -1978,6 +2126,22 @@ int vnc_display_open(DisplayState *ds, c
42af212
 	}
42af212
     }
42af212
 
42af212
+    /*
42af212
+     * Combinations we support here:
42af212
+     *
42af212
+     *  - no-auth                (clear text, no auth)
42af212
+     *  - password               (clear text, weak auth)
42af212
+     *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
42af212
+     *  - tls                    (encrypt, weak anonymous creds, no auth)
42af212
+     *  - tls + password         (encrypt, weak anonymous creds, weak auth)
42af212
+     *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
42af212
+     *  - tls + x509             (encrypt, good x509 creds, no auth)
42af212
+     *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
42af212
+     *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
42af212
+     *
42af212
+     * NB1. TLS is a stackable auth scheme.
42af212
+     * NB2. the x509 schemes have option to validate a client cert dname
42af212
+     */
42af212
     if (password) {
42af212
 #ifdef CONFIG_VNC_TLS
42af212
 	if (tls) {
42af212
@@ -1990,13 +2154,34 @@ int vnc_display_open(DisplayState *ds, c
42af212
 		vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
42af212
 	    }
42af212
 	} else {
42af212
-#endif
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
 	    VNC_DEBUG("Initializing VNC server with password auth\n");
42af212
 	    vs->auth = VNC_AUTH_VNC;
42af212
 #ifdef CONFIG_VNC_TLS
42af212
 	    vs->subauth = VNC_AUTH_INVALID;
42af212
 	}
42af212
-#endif
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    } else if (sasl) {
42af212
+#ifdef CONFIG_VNC_TLS
42af212
+        if (tls) {
42af212
+            vs->auth = VNC_AUTH_VENCRYPT;
42af212
+            if (x509) {
42af212
+		VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
42af212
+                vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
42af212
+            } else {
42af212
+		VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
42af212
+                vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
42af212
+            }
42af212
+        } else {
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
+	    VNC_DEBUG("Initializing VNC server with SASL auth\n");
42af212
+            vs->auth = VNC_AUTH_SASL;
42af212
+#ifdef CONFIG_VNC_TLS
42af212
+            vs->subauth = VNC_AUTH_INVALID;
42af212
+        }
42af212
+#endif /* CONFIG_VNC_TLS */
42af212
+#endif /* CONFIG_VNC_SASL */
42af212
     } else {
42af212
 #ifdef CONFIG_VNC_TLS
42af212
 	if (tls) {
42af212
@@ -2018,6 +2203,16 @@ int vnc_display_open(DisplayState *ds, c
42af212
 #endif
42af212
     }
42af212
 
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
42af212
+        fprintf(stderr, "Failed to initialize SASL auth %s",
42af212
+                sasl_errstring(saslErr, NULL, NULL));
42af212
+        free(vs->display);
42af212
+        vs->display = NULL;
42af212
+        return -1;
42af212
+    }
42af212
+#endif
42af212
+
42af212
     if (reverse) {
42af212
         /* connect to viewer */
42af212
         if (strncmp(display, "unix:", 5) == 0)
Glauber Costa 8571d06
Index: kvm-84.git-snapshot-20090303/qemu/vnc.h
Glauber Costa 8571d06
===================================================================
Glauber Costa 8571d06
--- kvm-84.git-snapshot-20090303.orig/qemu/vnc.h
Glauber Costa 8571d06
+++ kvm-84.git-snapshot-20090303/qemu/vnc.h
42af212
@@ -79,6 +79,10 @@ typedef struct VncDisplay VncDisplay;
42af212
 #include "vnc-tls.h"
42af212
 #include "vnc-auth-vencrypt.h"
42af212
 #endif
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+#include "vnc-auth-sasl.h"
42af212
+#endif
42af212
+
42af212
 
42af212
 struct VncDisplay
42af212
 {
42af212
@@ -118,10 +122,12 @@ struct VncState
42af212
     int minor;
42af212
 
42af212
     char challenge[VNC_AUTH_CHALLENGE_SIZE];
42af212
-
42af212
 #ifdef CONFIG_VNC_TLS
42af212
     VncStateTLS tls;
42af212
 #endif
42af212
+#ifdef CONFIG_VNC_SASL
42af212
+    VncStateSASL sasl;
42af212
+#endif
42af212
 
42af212
     Buffer output;
42af212
     Buffer input;
42af212
@@ -160,8 +166,9 @@ enum {
42af212
     VNC_AUTH_RA2NE = 6,
42af212
     VNC_AUTH_TIGHT = 16,
42af212
     VNC_AUTH_ULTRA = 17,
42af212
-    VNC_AUTH_TLS = 18,
42af212
-    VNC_AUTH_VENCRYPT = 19
42af212
+    VNC_AUTH_TLS = 18,      /* Supported in GTK-VNC & VINO */
42af212
+    VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */
42af212
+    VNC_AUTH_SASL = 20,     /* Supported in GTK-VNC & VINO */
42af212
 };
42af212
 
42af212
 enum {
42af212
@@ -172,6 +179,8 @@ enum {
42af212
     VNC_AUTH_VENCRYPT_X509NONE = 260,
42af212
     VNC_AUTH_VENCRYPT_X509VNC = 261,
42af212
     VNC_AUTH_VENCRYPT_X509PLAIN = 262,
42af212
+    VNC_AUTH_VENCRYPT_X509SASL = 263,
42af212
+    VNC_AUTH_VENCRYPT_TLSSASL = 264,
42af212
 };
42af212
 
42af212
 
42af212
@@ -255,6 +264,8 @@ enum {
42af212
 void vnc_client_read(void *opaque);
42af212
 void vnc_client_write(void *opaque);
42af212
 
42af212
+long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
42af212
+long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
42af212
 
42af212
 /* Protocol I/O functions */
42af212
 void vnc_write(VncState *vs, const void *data, size_t len);
42af212
@@ -274,8 +285,22 @@ uint32_t read_u32(uint8_t *data, size_t 
42af212
 
42af212
 /* Protocol stage functions */
42af212
 void vnc_client_error(VncState *vs);
42af212
+int vnc_client_io_error(VncState *vs, int ret, int last_errno);
42af212
 
42af212
 void start_client_init(VncState *vs);
42af212
 void start_auth_vnc(VncState *vs);
42af212
 
42af212
+/* Buffer management */
42af212
+void buffer_reserve(Buffer *buffer, size_t len);
42af212
+int buffer_empty(Buffer *buffer);
42af212
+uint8_t *buffer_end(Buffer *buffer);
42af212
+void buffer_reset(Buffer *buffer);
42af212
+void buffer_append(Buffer *buffer, const void *data, size_t len);
42af212
+
42af212
+
42af212
+/* Misc helpers */
42af212
+
42af212
+char *vnc_socket_local_addr(const char *format, int fd);
42af212
+char *vnc_socket_remote_addr(const char *format, int fd);
42af212
+
42af212
 #endif /* __QEMU_VNC_H */