Blame SOURCES/cyrus-sasl-2.1.27-Add-basic-test-infrastructure.patch

138d55
From 82e299e970461c153a036bb1fbc84e808f926e12 Mon Sep 17 00:00:00 2001
138d55
From: Simo Sorce <simo@redhat.com>
138d55
Date: Tue, 5 May 2020 14:06:57 -0400
138d55
Subject: [PATCH] Add basic test infrastructure
138d55
138d55
First test is for SASL/GSSAPI
138d55
138d55
Backport of upstream commit id:
138d55
18ff41d5d18f61c2ded7235dad1d9618aa84784b
138d55
138d55
Signed-off-by: Simo Sorce <simo@redhat.com>
138d55
---
138d55
 Makefile.am          |   2 +-
138d55
 configure.ac         |   3 +-
138d55
 tests/Makefile.am    |  79 +++++++++++++++++++
138d55
 tests/runtests.py    | 179 +++++++++++++++++++++++++++++++++++++++++++
138d55
 tests/t_common.c     |  68 ++++++++++++++++
138d55
 tests/t_common.h     |  15 ++++
138d55
 tests/t_gssapi_cli.c |  95 +++++++++++++++++++++++
138d55
 tests/t_gssapi_srv.c | 111 +++++++++++++++++++++++++++
138d55
 8 files changed, 550 insertions(+), 2 deletions(-)
138d55
 create mode 100644 tests/Makefile.am
138d55
 create mode 100755 tests/runtests.py
138d55
 create mode 100644 tests/t_common.c
138d55
 create mode 100644 tests/t_common.h
138d55
 create mode 100644 tests/t_gssapi_cli.c
138d55
 create mode 100644 tests/t_gssapi_srv.c
138d55
138d55
diff --git a/Makefile.am b/Makefile.am
138d55
index 83dae6f..fc24509 100644
138d55
--- a/Makefile.am
138d55
+++ b/Makefile.am
138d55
@@ -70,7 +70,7 @@ else
138d55
 INSTALLOSX = 
138d55
 endif
138d55
 
138d55
-SUBDIRS=include sasldb common lib plugins utils $(PWC) $(SAM) $(JAV) $(SAD)
138d55
+SUBDIRS=include sasldb common lib plugins utils $(PWC) $(SAM) $(JAV) $(SAD) tests
138d55
 EXTRA_DIST=config doc docsrc win32 mac dlcompat-20010505 NTMakefile \
138d55
     INSTALL.TXT libsasl2.pc.in
138d55
 
138d55
diff --git a/configure.ac b/configure.ac
138d55
index ca5936a..c1d2182 100644
138d55
--- a/configure.ac
138d55
+++ b/configure.ac
138d55
@@ -1575,7 +1575,8 @@ java/javax/Makefile
138d55
 java/javax/security/Makefile
138d55
 java/javax/security/auth/Makefile
138d55
 java/javax/security/auth/callback/Makefile
138d55
-pwcheck/Makefile)
138d55
+pwcheck/Makefile
138d55
+tests/Makefile)
138d55
 
138d55
 AC_MSG_NOTICE([
138d55
 
138d55
diff --git a/tests/Makefile.am b/tests/Makefile.am
138d55
new file mode 100644
138d55
index 0000000..1edf010
138d55
--- /dev/null
138d55
+++ b/tests/Makefile.am
138d55
@@ -0,0 +1,79 @@
138d55
+# Makefile.am -- automake input for cyrus-sasl tests
138d55
+# Simo Sorce
138d55
+#
138d55
+################################################################
138d55
+# Copyright (c) 2000 Carnegie Mellon University.  All rights reserved.
138d55
+#
138d55
+# Redistribution and use in source and binary forms, with or without
138d55
+# modification, are permitted provided that the following conditions
138d55
+# are met:
138d55
+#
138d55
+# 1. Redistributions of source code must retain the above copyright
138d55
+#    notice, this list of conditions and the following disclaimer.
138d55
+#
138d55
+# 2. Redistributions in binary form must reproduce the above copyright
138d55
+#    notice, this list of conditions and the following disclaimer in
138d55
+#    the documentation and/or other materials provided with the
138d55
+#    distribution.
138d55
+#
138d55
+# 3. The name "Carnegie Mellon University" must not be used to
138d55
+#    endorse or promote products derived from this software without
138d55
+#    prior written permission. For permission or any other legal
138d55
+#    details, please contact
138d55
+#      Office of Technology Transfer
138d55
+#      Carnegie Mellon University
138d55
+#      5000 Forbes Avenue
138d55
+#      Pittsburgh, PA  15213-3890
138d55
+#      (412) 268-4387, fax: (412) 268-7395
138d55
+#      tech-transfer@andrew.cmu.edu
138d55
+#
138d55
+# 4. Redistributions of any form whatsoever must retain the following
138d55
+#    acknowledgment:
138d55
+#    "This product includes software developed by Computing Services
138d55
+#     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
138d55
+#
138d55
+# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
138d55
+# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
138d55
+# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
138d55
+# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138d55
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
138d55
+# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
138d55
+# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
138d55
+#
138d55
+################################################################
138d55
+
138d55
+AM_CPPFLAGS=-I$(top_srcdir)/include -DPLUGINDIR='"${top_srcdir}/plugins/.libs"'
138d55
+
138d55
+COMMON_LDADD = ../lib/libsasl2.la $(GSSAPIBASE_LIBS) $(GSSAPI_LIBS) $(LIB_SOCKET)
138d55
+
138d55
+t_gssapi_cli_SOURCES = \
138d55
+	t_common.c \
138d55
+	t_gssapi_cli.c
138d55
+
138d55
+t_gssapi_cli_LDADD = $(COMMON_LDADD)
138d55
+
138d55
+t_gssapi_srv_SOURCES = \
138d55
+	t_common.c \
138d55
+	t_gssapi_srv.c
138d55
+
138d55
+t_gssapi_srv_LDADD = $(COMMON_LDADD)
138d55
+
138d55
+check_PROGRAMS = \
138d55
+	t_gssapi_cli \
138d55
+	t_gssapi_srv \
138d55
+	$(NULL)
138d55
+
138d55
+noinst_PROGRAMS = $(check_PROGRAMS)
138d55
+
138d55
+EXTRA_DIST = \
138d55
+	runtests.py \
138d55
+	$(NULL)
138d55
+
138d55
+all: $(check_PROGRAMS)
138d55
+
138d55
+check:
138d55
+if MACOSX
138d55
+# skip Mac OSX for now
138d55
+else
138d55
+	$(srcdir)/runtests.py $(CHECKARGS)
138d55
+endif
138d55
diff --git a/tests/runtests.py b/tests/runtests.py
138d55
new file mode 100755
138d55
index 0000000..f645adf
138d55
--- /dev/null
138d55
+++ b/tests/runtests.py
138d55
@@ -0,0 +1,179 @@
138d55
+#!/usr/bin/python3
138d55
+
138d55
+import argparse
138d55
+import os
138d55
+import shutil
138d55
+import signal
138d55
+import subprocess
138d55
+import time
138d55
+from string import Template
138d55
+
138d55
+
138d55
+def setup_socket_wrappers(testdir):
138d55
+    """ Try to set up socket wrappers """
138d55
+    wrapdir = os.path.join(testdir, 'w')
138d55
+    os.makedirs(wrapdir)
138d55
+
138d55
+    wrappers = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
138d55
+    wrappers.wait()
138d55
+    if wrappers.returncode != 0:
138d55
+        raise Exception('Socket Wrappers not available')
138d55
+
138d55
+    wrappers = subprocess.Popen(['pkg-config', '--exists', 'nss_wrapper'])
138d55
+    wrappers.wait()
138d55
+    if wrappers.returncode != 0:
138d55
+        raise Exception('NSS Wrappers not available')
138d55
+
138d55
+    hosts = os.path.join(wrapdir, 'hosts')
138d55
+    with open(hosts, 'w+') as conffile:
138d55
+        conffile.write('127.0.0.9 host.realm.test')
138d55
+
138d55
+    return {'LD_PRELOAD': 'libsocket_wrapper.so libnss_wrapper.so',
138d55
+            'SOCKET_WRAPPER_DIR': wrapdir,
138d55
+            'SOCKET_WRAPPER_DEFAULT_IFACE': '9',
138d55
+            'NSS_WRAPPER_HOSTNAME': 'host.realm.test',
138d55
+            'NSS_WRAPPER_HOSTS': hosts}
138d55
+
138d55
+
138d55
+KERBEROS_CONF = '''
138d55
+[libdefaults]
138d55
+  default_realm = REALM.TEST
138d55
+  dns_lookup_realm = false
138d55
+  dns_lookup_kdc = false
138d55
+  rdns = false
138d55
+  ticket_lifetime = 24h
138d55
+  forwardable = yes
138d55
+  default_ccache_name = FILE://${TESTDIR}/ccache
138d55
+  udp_preference_limit = 1
138d55
+
138d55
+[domain_realm]
138d55
+  .realm.test = REALM.TEST
138d55
+  realm.test = REALM.TEST
138d55
+
138d55
+[realms]
138d55
+ REALM.TEST = {
138d55
+  kdc = 127.0.0.9
138d55
+  admin_server = 127.0.0.9
138d55
+  acl_file = ${TESTDIR}/kadm.acl
138d55
+  dict_file = /usr/share/dict/words
138d55
+  admin_keytab = ${TESTDIR}/kadm.keytab
138d55
+  database_name = ${TESTDIR}/kdc.db
138d55
+  key_stash_file = ${TESTDIR}/kdc.stash
138d55
+ }
138d55
+
138d55
+[kdcdefaults]
138d55
+ kdc_ports = 88
138d55
+ kdc_tcp_ports = 88
138d55
+
138d55
+[logging]
138d55
+  kdc = FILE:${TESTDIR}/kdc.log
138d55
+  admin_server = FILE:${TESTDIR}/kadm.log
138d55
+  default = FILE:${TESTDIR}/krb5.log
138d55
+'''
138d55
+
138d55
+
138d55
+def setup_kdc(testdir, env):
138d55
+    """ Setup KDC and start process """
138d55
+    krbconf = os.path.join(testdir, 'krb.conf')
138d55
+    env['KRB5_CONFIG'] = krbconf
138d55
+
138d55
+    kenv = {'KRB5_KDC_PROFILE': krbconf,
138d55
+            'PATH': '/sbin:/bin:/usr/sbin:/usr/bin'}
138d55
+    kenv.update(env)
138d55
+
138d55
+    # KDC/KRB5 CONFIG
138d55
+    templ = Template(KERBEROS_CONF)
138d55
+    text = templ.substitute({'TESTDIR': testdir})
138d55
+    with open(krbconf, 'w+') as conffile:
138d55
+        conffile.write(text)
138d55
+
138d55
+    testlog = os.path.join(testdir, 'kdc.log')
138d55
+    log = open(testlog, 'a')
138d55
+
138d55
+    subprocess.check_call([
138d55
+        "kdb5_util", "create",
138d55
+        "-r", "REALM.TEST", "-s", "-P", "password"
138d55
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
138d55
+
138d55
+    kdc = subprocess.Popen(['krb5kdc', '-n'], env=kenv, preexec_fn=os.setsid)
138d55
+    time.sleep(5)
138d55
+
138d55
+    # Add a user and genrate a keytab
138d55
+    keytab = os.path.join(testdir, "user.keytab")
138d55
+    subprocess.check_call([
138d55
+        "kadmin.local", "-q",
138d55
+        "addprinc -randkey user"
138d55
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
138d55
+
138d55
+    subprocess.check_call([
138d55
+        "kadmin.local", "-q",
138d55
+        "ktadd -k {} user".format(keytab)
138d55
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
138d55
+    env['KRB5_CLIENT_KTNAME'] = keytab
138d55
+
138d55
+    # Add a service and genrate a keytab
138d55
+    keytab = os.path.join(testdir, "test.keytab")
138d55
+    subprocess.check_call([
138d55
+        "kadmin.local", "-q",
138d55
+        "addprinc -randkey test/host.realm.test"
138d55
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
138d55
+
138d55
+    subprocess.check_call([
138d55
+        "kadmin.local", "-q",
138d55
+        "ktadd -k {} test/host.realm.test".format(keytab)
138d55
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
138d55
+    env['KRB5_KTNAME'] = keytab
138d55
+
138d55
+    return kdc, env
138d55
+
138d55
+
138d55
+def gssapi_tests(testdir):
138d55
+    """ SASL/GSSAPI Tests """
138d55
+    env = setup_socket_wrappers(testdir)
138d55
+    kdc, kenv = setup_kdc(testdir, env)
138d55
+    #print("KDC: {}, ENV: {}".format(kdc, kenv))
138d55
+    kenv['KRB5_TRACE'] = os.path.join(testdir, 'trace.log')
138d55
+
138d55
+    try:
138d55
+        srv = subprocess.Popen(["../tests/t_gssapi_srv"],
138d55
+                               stdout=subprocess.PIPE,
138d55
+                               stderr=subprocess.PIPE, env=kenv)
138d55
+        srv.stdout.readline() # Wait for srv to say it is ready
138d55
+        cli = subprocess.Popen(["../tests/t_gssapi_cli"],
138d55
+                               stdout=subprocess.PIPE,
138d55
+                               stderr=subprocess.PIPE, env=kenv)
138d55
+        try:
138d55
+            cli.wait(timeout=5)
138d55
+            srv.wait(timeout=5)
138d55
+        except Exception as e:
138d55
+            print("Failed on {}".format(e));
138d55
+            cli.kill()
138d55
+            srv.kill()
138d55
+        if cli.returncode != 0 or srv.returncode != 0:
138d55
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
138d55
+                cli.returncode, cli.stderr.read().decode('utf-8'),
138d55
+                srv.returncode, srv.stderr.read().decode('utf-8')))
138d55
+    except Exception as e:
138d55
+        print("FAIL: {}".format(e))
138d55
+
138d55
+    print("PASS: CLI({}) SRV({})".format(
138d55
+        cli.stdout.read().decode('utf-8').strip(),
138d55
+        srv.stdout.read().decode('utf-8').strip()))
138d55
+
138d55
+    os.killpg(kdc.pid, signal.SIGTERM)
138d55
+
138d55
+
138d55
+if __name__ == "__main__":
138d55
+
138d55
+    P = argparse.ArgumentParser(description='Cyrus SASL Tests')
138d55
+    P.add_argument('--testdir', default=os.path.join(os.getcwd(), '.tests'),
138d55
+                   help="Directory for running tests")
138d55
+    A = vars(P.parse_args())
138d55
+
138d55
+    T = A['testdir']
138d55
+
138d55
+    if os.path.exists(T):
138d55
+        shutil.rmtree(T)
138d55
+    os.makedirs(T)
138d55
+
138d55
+    gssapi_tests(T)
138d55
diff --git a/tests/t_common.c b/tests/t_common.c
138d55
new file mode 100644
138d55
index 0000000..7168b2f
138d55
--- /dev/null
138d55
+++ b/tests/t_common.c
138d55
@@ -0,0 +1,68 @@
138d55
+/* TBD, add (C) */
138d55
+
138d55
+#include <t_common.h>
138d55
+
138d55
+void s_error(const char *hdr, ssize_t ret, ssize_t len, int err)
138d55
+{
138d55
+    fprintf(stderr, "%s l:%ld/%ld [%d] %s",
138d55
+            hdr, ret, len, err, strerror(err));
138d55
+    exit(-1);
138d55
+}
138d55
+
138d55
+void send_string(int sd, const char *s, unsigned int l)
138d55
+{
138d55
+    ssize_t ret;
138d55
+
138d55
+fprintf(stderr, "s:%u ", l);
138d55
+fflush(stderr);
138d55
+
138d55
+    ret = send(sd, &l, sizeof(l), 0);
138d55
+    if (ret != sizeof(l)) s_error("send size", ret, sizeof(l), errno);
138d55
+
138d55
+    if (l == 0) return;
138d55
+
138d55
+    ret = send(sd, s, l, 0);
138d55
+    if (ret != l) s_error("send data", ret, l, errno);
138d55
+}
138d55
+
138d55
+void recv_string(int sd, char *buf, unsigned int *buflen)
138d55
+{
138d55
+    unsigned int l;
138d55
+    ssize_t ret;
138d55
+
138d55
+    ret = recv(sd, &l, sizeof(l), MSG_WAITALL);
138d55
+    if (ret != sizeof(l)) s_error("recv size", ret, sizeof(l), errno);
138d55
+
138d55
+    if (l == 0) {
138d55
+fprintf(stderr, "r:0 ");
138d55
+fflush(stderr);
138d55
+        *buflen = 0;
138d55
+        return;
138d55
+    }
138d55
+
138d55
+    if (*buflen < l) s_error("recv len", l, *buflen, E2BIG);
138d55
+
138d55
+    ret = recv(sd, buf, l, 0);
138d55
+    if (ret != l) s_error("recv data", ret, l, errno);
138d55
+
138d55
+fprintf(stderr, "r:%ld ", ret);
138d55
+fflush(stderr);
138d55
+    *buflen = ret;
138d55
+}
138d55
+
138d55
+void saslerr(int why, const char *what)
138d55
+{
138d55
+    fprintf(stderr, "%s: %s", what, sasl_errstring(why, NULL, NULL));
138d55
+}
138d55
+
138d55
+int getpath(void *context __attribute__((unused)), const char **path)
138d55
+{
138d55
+    if (! path) {
138d55
+        return SASL_BADPARAM;
138d55
+    }
138d55
+
138d55
+    *path = PLUGINDIR;
138d55
+    return SASL_OK;
138d55
+}
138d55
+
138d55
+
138d55
diff --git a/tests/t_common.h b/tests/t_common.h
138d55
new file mode 100644
138d55
index 0000000..4ee1976
138d55
--- /dev/null
138d55
+++ b/tests/t_common.h
138d55
@@ -0,0 +1,15 @@
138d55
+/* TBD, add (C) */
138d55
+
138d55
+#include "config.h"
138d55
+
138d55
+#include <errno.h>
138d55
+#include <stdio.h>
138d55
+#include <sys/socket.h>
138d55
+
138d55
+#include <sasl.h>
138d55
+
138d55
+void s_error(const char *hdr, ssize_t ret, ssize_t len, int err);
138d55
+void send_string(int sd, const char *s, unsigned int l);
138d55
+void recv_string(int sd, char *buf, unsigned int *buflen);
138d55
+void saslerr(int why, const char *what);
138d55
+int getpath(void *context __attribute__((unused)), const char **path);
138d55
diff --git a/tests/t_gssapi_cli.c b/tests/t_gssapi_cli.c
138d55
new file mode 100644
138d55
index 0000000..c833c05
138d55
--- /dev/null
138d55
+++ b/tests/t_gssapi_cli.c
138d55
@@ -0,0 +1,95 @@
138d55
+/* TBD, add (C) */
138d55
+
138d55
+#include "t_common.h"
138d55
+
138d55
+#include <stdlib.h>
138d55
+#include <stdarg.h>
138d55
+#include <ctype.h>
138d55
+#include <string.h>
138d55
+
138d55
+#ifdef HAVE_UNISTD_H
138d55
+#include <unistd.h>
138d55
+#endif
138d55
+
138d55
+#include <arpa/inet.h>
138d55
+#include <saslplug.h>
138d55
+
138d55
+static int setup_socket(void)
138d55
+{
138d55
+    struct sockaddr_in addr;
138d55
+    int sock, ret;
138d55
+
138d55
+    sock = socket(AF_INET, SOCK_STREAM, 0);
138d55
+    if (sock < 0) s_error("socket", 0, 0, errno);
138d55
+
138d55
+    addr.sin_family = AF_INET;
138d55
+    addr.sin_addr.s_addr = inet_addr("127.0.0.9");
138d55
+    addr.sin_port = htons(9000);
138d55
+
138d55
+    ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
138d55
+    if (ret != 0) s_error("connect", 0, 0, errno);
138d55
+
138d55
+    return sock;
138d55
+}
138d55
+
138d55
+int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
138d55
+{
138d55
+    sasl_callback_t callbacks[2] = {};
138d55
+    char buf[8192];
138d55
+    const char *chosenmech;
138d55
+    sasl_conn_t *conn;
138d55
+    const char *data;
138d55
+    unsigned int len;
138d55
+    int sd;
138d55
+    int r;
138d55
+
138d55
+    /* initialize the sasl library */
138d55
+    callbacks[0].id = SASL_CB_GETPATH;
138d55
+    callbacks[0].proc = (sasl_callback_ft)&getpath;
138d55
+    callbacks[0].context = NULL;
138d55
+    callbacks[1].id = SASL_CB_LIST_END;
138d55
+    callbacks[1].proc = NULL;
138d55
+    callbacks[1].context = NULL;
138d55
+
138d55
+    r = sasl_client_init(callbacks);
138d55
+    if (r != SASL_OK) exit(-1);
138d55
+
138d55
+    r = sasl_client_new("test", "host.realm.test", NULL, NULL, NULL, 0, &conn;;
138d55
+    if (r != SASL_OK) {
138d55
+        saslerr(r, "allocating connection state");
138d55
+        exit(-1);
138d55
+    }
138d55
+
138d55
+    r = sasl_client_start(conn, "GSSAPI", NULL, &data, &len, &chosenmech);
138d55
+    if (r != SASL_OK && r != SASL_CONTINUE) {
138d55
+	saslerr(r, "starting SASL negotiation");
138d55
+	printf("\n%s\n", sasl_errdetail(conn));
138d55
+	exit(-1);
138d55
+    }
138d55
+
138d55
+    sd = setup_socket();
138d55
+
138d55
+    while (r == SASL_CONTINUE) {
138d55
+        send_string(sd, data, len);
138d55
+        len = 8192;
138d55
+        recv_string(sd, buf, &len;;
138d55
+
138d55
+	r = sasl_client_step(conn, buf, len, NULL, &data, &len;;
138d55
+	if (r != SASL_OK && r != SASL_CONTINUE) {
138d55
+	    saslerr(r, "performing SASL negotiation");
138d55
+	    printf("\n%s\n", sasl_errdetail(conn));
138d55
+	    exit(-1);
138d55
+        }
138d55
+    }
138d55
+
138d55
+    if (r != SASL_OK) exit(-1);
138d55
+
138d55
+    if (len > 0) {
138d55
+        send_string(sd, data, len);
138d55
+    }
138d55
+
138d55
+    fprintf(stdout, "DONE\n");
138d55
+    fflush(stdout);
138d55
+    return 0;
138d55
+}
138d55
+
138d55
diff --git a/tests/t_gssapi_srv.c b/tests/t_gssapi_srv.c
138d55
new file mode 100644
138d55
index 0000000..29f538d
138d55
--- /dev/null
138d55
+++ b/tests/t_gssapi_srv.c
138d55
@@ -0,0 +1,111 @@
138d55
+/* TBD, add (C) */
138d55
+
138d55
+#include "t_common.h"
138d55
+
138d55
+#include <stdlib.h>
138d55
+#include <stdarg.h>
138d55
+#include <ctype.h>
138d55
+#include <string.h>
138d55
+
138d55
+#ifdef HAVE_UNISTD_H
138d55
+#include <unistd.h>
138d55
+#endif
138d55
+
138d55
+#include <arpa/inet.h>
138d55
+#include <saslplug.h>
138d55
+
138d55
+static int setup_socket(void)
138d55
+{
138d55
+    struct sockaddr_in addr;
138d55
+    int sock, ret, sd;
138d55
+
138d55
+    sock = socket(AF_INET, SOCK_STREAM, 0);
138d55
+    if (sock < 0) s_error("socket", 0, 0, errno);
138d55
+
138d55
+    addr.sin_family = AF_INET;
138d55
+    addr.sin_addr.s_addr = inet_addr("127.0.0.9");
138d55
+    addr.sin_port = htons(9000);
138d55
+
138d55
+    ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
138d55
+    if (ret != 0) s_error("bind", 0, 0, errno);
138d55
+
138d55
+    ret = listen(sock, 1);
138d55
+    if (ret != 0) s_error("listen", 0, 0, errno);
138d55
+
138d55
+    /* signal we are ready */
138d55
+    fprintf(stdout, "READY\n");
138d55
+    fflush(stdout);
138d55
+
138d55
+    /* block until the client connects */
138d55
+    sd = accept(sock, NULL, NULL);
138d55
+    if (sd < 0) s_error("accept", 0, 0, errno);
138d55
+
138d55
+    close(sock);
138d55
+    return sd;
138d55
+}
138d55
+
138d55
+int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
138d55
+{
138d55
+    sasl_callback_t callbacks[2] = {};
138d55
+    char buf[8192];
138d55
+    sasl_conn_t *conn;
138d55
+    const char *data;
138d55
+    unsigned int len;
138d55
+    int sd;
138d55
+    int r;
138d55
+
138d55
+    /* initialize the sasl library */
138d55
+    callbacks[0].id = SASL_CB_GETPATH;
138d55
+    callbacks[0].proc = (sasl_callback_ft)&getpath;
138d55
+    callbacks[0].context = NULL;
138d55
+    callbacks[1].id = SASL_CB_LIST_END;
138d55
+    callbacks[1].proc = NULL;
138d55
+    callbacks[1].context = NULL;
138d55
+
138d55
+    r = sasl_server_init(callbacks, "t_gssapi_srv");
138d55
+    if (r != SASL_OK) exit(-1);
138d55
+
138d55
+    r = sasl_server_new("test", "host.realm.test", NULL, NULL, NULL,
138d55
+                        callbacks, 0, &conn;;
138d55
+    if (r != SASL_OK) {
138d55
+        saslerr(r, "allocating connection state");
138d55
+        exit(-1);
138d55
+    }
138d55
+
138d55
+    sd = setup_socket();
138d55
+
138d55
+    len = 8192;
138d55
+    recv_string(sd, buf, &len;;
138d55
+
138d55
+    r = sasl_server_start(conn, "GSSAPI", buf, len, &data, &len;;
138d55
+    if (r != SASL_OK && r != SASL_CONTINUE) {
138d55
+	saslerr(r, "starting SASL negotiation");
138d55
+	printf("\n%s\n", sasl_errdetail(conn));
138d55
+	exit(-1);
138d55
+    }
138d55
+
138d55
+    while (r == SASL_CONTINUE) {
138d55
+        send_string(sd, data, len);
138d55
+        len = 8192;
138d55
+        recv_string(sd, buf, &len;;
138d55
+
138d55
+	r = sasl_server_step(conn, buf, len, &data, &len;;
138d55
+	if (r != SASL_OK && r != SASL_CONTINUE) {
138d55
+	    saslerr(r, "performing SASL negotiation");
138d55
+	    printf("\n%s\n", sasl_errdetail(conn));
138d55
+	    exit(-1);
138d55
+	}
138d55
+
138d55
+    }
138d55
+
138d55
+    if (r != SASL_OK) exit(-1);
138d55
+
138d55
+    if (len > 0) {
138d55
+        send_string(sd, data, len);
138d55
+    }
138d55
+
138d55
+    fprintf(stdout, "DONE\n");
138d55
+    fflush(stdout);
138d55
+    return 0;
138d55
+}
138d55
+
138d55
-- 
138d55
2.18.2
138d55