jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone
017ff1
diff --git a/HOWTO.ldap-keys b/HOWTO.ldap-keys
017ff1
new file mode 100644
017ff1
index 0000000..dd5f5cc
017ff1
--- /dev/null
017ff1
+++ b/HOWTO.ldap-keys
f09e2e
@@ -0,0 +1,108 @@
f09e2e
+
f09e2e
+HOW TO START
f09e2e
+
f09e2e
+1) configure LDAP server
f09e2e
+  * Use LDAP server documentation
f09e2e
+2) add appropriate LDAP schema
f09e2e
+  * For OpenLDAP or SunONE Use attached schema, otherwise you have to create it. 
f09e2e
+  * LDAP user entry
f09e2e
+        User entry:
f09e2e
+	- attached to the 'ldapPublicKey' objectclass
f09e2e
+	- attached to the 'posixAccount' objectclass
f09e2e
+	- with a filled 'sshPublicKey' attribute 
f09e2e
+3) insert users into LDAP
f09e2e
+  * Use LDAP Tree management tool as useful
f09e2e
+  * Entry in the LDAP server must respect 'posixAccount' and 'ldapPublicKey' which are defined in core.schema and the additionnal lpk.schema.
f09e2e
+  * Example:
f09e2e
+	dn: uid=captain,ou=commanders,dc=enterprise,dc=universe
f09e2e
+	objectclass: top
f09e2e
+	objectclass: person
f09e2e
+	objectclass: organizationalPerson
f09e2e
+	objectclass: posixAccount
f09e2e
+	objectclass: ldapPublicKey
f09e2e
+	description: Jonathan Archer
f09e2e
+	userPassword: Porthos
f09e2e
+	cn: onathan Archer
f09e2e
+	sn: onathan Archer
f09e2e
+	uid: captain
f09e2e
+	uidNumber: 1001
f09e2e
+	gidNumber: 1001
f09e2e
+	homeDirectory: /home/captain
f09e2e
+	sshPublicKey: ssh-rss AAAAB3.... =captain@universe
f09e2e
+	sshPublicKey: command="kill -9 1" ssh-rss AAAAM5...
f09e2e
+4) on the ssh side set in sshd_config
f09e2e
+  * Set up the backend
f09e2e
+	AuthorizedKeysCommand /usr/libexec/openssh/ssh-ldap-wrapper
f09e2e
+	AuthorizedKeysCommandUser <appropriate user to run LDAP>
f09e2e
+  * Do not forget to set
f09e2e
+	PubkeyAuthentication yes
f09e2e
+  * Swith off unnecessary auth methods
f09e2e
+5) confugure ldap.conf
f09e2e
+  * Default ldap.conf is placed in /etc/ssh
f09e2e
+  * The configuration style is the same as other ldap based aplications
f09e2e
+6) if necessary edit ssh-ldap-wrapper
f09e2e
+  * There is a possibility to change ldap.conf location
f09e2e
+  * There are some debug options
f09e2e
+  * Example
f09e2e
+	/usr/libexec/openssh -s -f /etc/ldap.conf -w -d >> /tmp/ldapdebuglog.txt
f09e2e
+
f09e2e
+HOW TO MIGRATE FROM LPK
f09e2e
+
f09e2e
+1) goto HOW TO START 4) .... the ldap schema is the same
f09e2e
+
f09e2e
+2) convert the group requests to the appropriate LDAP requests
f09e2e
+
f09e2e
+HOW TO SOLVE PROBLEMS
f09e2e
+
f09e2e
+1) use debug in sshd
f09e2e
+  * /usr/sbin/sshd -d -d -d -d
f09e2e
+2) use debug in ssh-ldap-helper
f09e2e
+  * ssh-ldap-helper -d -d -d -d -s <username>
f09e2e
+3) use tcpdump ... other ldap client etc.
f09e2e
+
f09e2e
+ADVANTAGES
f09e2e
+
f09e2e
+1) Blocking an user account can be done directly from LDAP (if sshd is using PubkeyAuthentication + AuthorizedKeysCommand with ldap only).
f09e2e
+
f09e2e
+DISADVANTAGES
f09e2e
+
f09e2e
+1)  LDAP must be well configured, getting the public key of some user is not a problem, but if anonymous LDAP 
f09e2e
+  allows write to users dn, somebody could replace some user's public key by his own and impersonate some 
f09e2e
+  of your users in all your server farm -- be VERY CAREFUL.
f09e2e
+2) With incomplete PKI the MITM attack when sshd is requesting the public key, could lead to a compromise of your servers allowing login 
f09e2e
+  as the impersonated user.
f09e2e
+3) If LDAP server is down there may be no fallback on passwd auth.
f09e2e
+  
f09e2e
+MISC.
f09e2e
+  
f09e2e
+1) todo
f09e2e
+  * Possibility to reuse the ssh-ldap-helper.
f09e2e
+  * Tune the LDAP part to accept  all possible LDAP configurations.
f09e2e
+
f09e2e
+2) differences from original lpk
f09e2e
+  * No LDAP code in sshd.
f09e2e
+  * Support for various LDAP platforms and configurations.
f09e2e
+  * LDAP is configured in separate ldap.conf file.
f09e2e
+
f09e2e
+3) docs/link 
f09e2e
+  * http://pacsec.jp/core05/psj05-barisani-en.pdf
f09e2e
+  * http://fritz.potsdam.edu/projects/openssh-lpk/
f09e2e
+  * http://fritz.potsdam.edu/projects/sshgate/
f09e2e
+  * http://dev.inversepath.com/trac/openssh-lpk
f09e2e
+  * http://lam.sf.net/ ( http://lam.sourceforge.net/documentation/supportedSchemas.htm )
f09e2e
+
f09e2e
+4) contributors/ideas/greets
f09e2e
+  - Eric AUGE <eau@phear.org>
f09e2e
+  - Andrea Barisani <andrea@inversepath.com>
f09e2e
+  - Falk Siemonsmeier.
f09e2e
+  - Jacob Rief.
f09e2e
+  - Michael Durchgraf.
f09e2e
+  - frederic peters.
f09e2e
+  - Finlay dobbie.
f09e2e
+  - Stefan Fisher.
f09e2e
+  - Robin H. Johnson.
f09e2e
+  - Adrian Bridgett.
f09e2e
+
f09e2e
+5) Author
f09e2e
+    Jan F. Chadima <jchadima@redhat.com>
f09e2e
+
017ff1
diff --git a/Makefile.in b/Makefile.in
017ff1
index 28a8ec4..411eadb 100644
017ff1
--- a/Makefile.in
017ff1
+++ b/Makefile.in
017ff1
@@ -25,6 +25,8 @@ SSH_PROGRAM=@bindir@/ssh
017ff1
 ASKPASS_PROGRAM=$(libexecdir)/ssh-askpass
017ff1
 SFTP_SERVER=$(libexecdir)/sftp-server
017ff1
 SSH_KEYSIGN=$(libexecdir)/ssh-keysign
017ff1
+SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
017ff1
+SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
017ff1
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
017ff1
 PRIVSEP_PATH=@PRIVSEP_PATH@
017ff1
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
017ff1
@@ -60,8 +62,9 @@ XAUTH_PATH=@XAUTH_PATH@
017ff1
 LDFLAGS=-L. -Lopenbsd-compat/ @LDFLAGS@
017ff1
 EXEEXT=@EXEEXT@
017ff1
 MANFMT=@MANFMT@
017ff1
+INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
017ff1
 
017ff1
-TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT)
017ff1
+TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT) ssh-ldap-helper$(EXEEXT)
017ff1
 
017ff1
 LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
017ff1
 	canohost.o channels.o cipher.o cipher-aes.o \
017ff1
@@ -98,8 +101,8 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o auth-rsa.o auth-rh-rsa.o \
017ff1
 	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
017ff1
 	sandbox-seccomp-filter.o sandbox-capsicum.o
017ff1
 
017ff1
-MANPAGES	= moduli.5.out scp.1.out ssh-add.1.out ssh-agent.1.out ssh-keygen.1.out ssh-keyscan.1.out ssh.1.out sshd.8.out sftp-server.8.out sftp.1.out ssh-keysign.8.out ssh-pkcs11-helper.8.out sshd_config.5.out ssh_config.5.out
017ff1
-MANPAGES_IN	= moduli.5 scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh-keyscan.1 ssh.1 sshd.8 sftp-server.8 sftp.1 ssh-keysign.8 ssh-pkcs11-helper.8 sshd_config.5 ssh_config.5
017ff1
+MANPAGES	= moduli.5.out scp.1.out ssh-add.1.out ssh-agent.1.out ssh-keygen.1.out ssh-keyscan.1.out ssh.1.out sshd.8.out sftp-server.8.out sftp.1.out ssh-keysign.8.out ssh-pkcs11-helper.8.out sshd_config.5.out ssh_config.5.out ssh-ldap-helper.8.out ssh-ldap.conf.5.out
017ff1
+MANPAGES_IN	= moduli.5 scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh-keyscan.1 ssh.1 sshd.8 sftp-server.8 sftp.1 ssh-keysign.8 ssh-pkcs11-helper.8 sshd_config.5 ssh_config.5 ssh-ldap-helper.8 ssh-ldap.conf.5
017ff1
 MANTYPE		= @MANTYPE@
017ff1
 
017ff1
 CONFIGFILES=sshd_config.out ssh_config.out moduli.out
017ff1
@@ -170,6 +173,9 @@ ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o readco
017ff1
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
017ff1
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
017ff1
 
017ff1
+ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o
017ff1
+	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f09e2e
+
017ff1
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
017ff1
 	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
017ff1
 
017ff1
@@ -273,6 +279,10 @@ install-files:
017ff1
 	$(INSTALL) -m 0755 $(STRIP_OPT) sshd$(EXEEXT) $(DESTDIR)$(sbindir)/sshd$(EXEEXT)
017ff1
 	$(INSTALL) -m 4711 $(STRIP_OPT) ssh-keysign$(EXEEXT) $(DESTDIR)$(SSH_KEYSIGN)$(EXEEXT)
017ff1
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-pkcs11-helper$(EXEEXT) $(DESTDIR)$(SSH_PKCS11_HELPER)$(EXEEXT)
017ff1
+	if test ! -z "$(INSTALL_SSH_LDAP_HELPER)" ; then \
017ff1
+		$(INSTALL) -m 0700 $(STRIP_OPT) ssh-ldap-helper $(DESTDIR)$(SSH_LDAP_HELPER) ; \
017ff1
+		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
017ff1
+	fi
017ff1
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
017ff1
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
017ff1
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
017ff1
@@ -289,6 +299,10 @@ install-files:
017ff1
 	$(INSTALL) -m 644 sftp-server.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/sftp-server.8
017ff1
 	$(INSTALL) -m 644 ssh-keysign.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-keysign.8
017ff1
 	$(INSTALL) -m 644 ssh-pkcs11-helper.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-pkcs11-helper.8
017ff1
+	if test ! -z "$(INSTALL_SSH_LDAP_HELPER)" ; then \
017ff1
+		$(INSTALL) -m 644 ssh-ldap-helper.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-ldap-helper.8 ; \
017ff1
+		$(INSTALL) -m 644 ssh-ldap.conf.5.out $(DESTDIR)$(mandir)/$(mansubdir)5/ssh-ldap.conf.5 ; \
017ff1
+	fi
017ff1
 	-rm -f $(DESTDIR)$(bindir)/slogin
017ff1
 	ln -s ./ssh$(EXEEXT) $(DESTDIR)$(bindir)/slogin
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)1/slogin.1
017ff1
@@ -318,6 +332,13 @@ install-sysconf:
017ff1
 	else \
017ff1
 		echo "$(DESTDIR)$(sysconfdir)/moduli already exists, install will not overwrite"; \
017ff1
 	fi
017ff1
+	if test ! -z "$(INSTALL_SSH_LDAP_HELPER)" ; then \
017ff1
+		if [ ! -f $(DESTDIR)$(sysconfdir)/ldap.conf ]; then \
017ff1
+			$(INSTALL) -m 644 ldap.conf $(DESTDIR)$(sysconfdir)/ldap.conf; \
017ff1
+		else \
017ff1
+			echo "$(DESTDIR)$(sysconfdir)/ldap.conf already exists, install will not overwrite"; \
017ff1
+		fi ; \
017ff1
+	fi
017ff1
 
017ff1
 host-key: ssh-keygen$(EXEEXT)
017ff1
 	@if [ -z "$(DESTDIR)" ] ; then \
017ff1
@@ -381,6 +402,8 @@ uninstall:
017ff1
 	-rm -r $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
017ff1
 	-rm -f $(DESTDIR)$(SSH_KEYSIGN)$(EXEEXT)
017ff1
 	-rm -f $(DESTDIR)$(SSH_PKCS11_HELPER)$(EXEEXT)
017ff1
+	-rm -f $(DESTDIR)$(SSH_LDAP_HELPER)$(EXEEXT)
017ff1
+	-rm -f $(DESTDIR)$(SSH_LDAP_WRAPPER)$(EXEEXT)
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)1/scp.1
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)1/ssh-add.1
017ff1
@@ -392,6 +415,7 @@ uninstall:
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)8/sftp-server.8
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-keysign.8
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-pkcs11-helper.8
017ff1
+	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-ldap-helper.8
017ff1
 	-rm -f $(DESTDIR)$(mandir)/$(mansubdir)1/slogin.1
017ff1
 
017ff1
 regress/modpipe$(EXEEXT): $(srcdir)/regress/modpipe.c
017ff1
diff --git a/configure.ac b/configure.ac
017ff1
index 7c6ce08..722a19e 100644
017ff1
--- a/configure.ac
017ff1
+++ b/configure.ac
017ff1
@@ -1625,6 +1625,106 @@ if test "x$use_pie" != "xno"; then
017ff1
 	fi
017ff1
 fi
017ff1
 
017ff1
+# Check whether user wants LDAP support
017ff1
+LDAP_MSG="no"
017ff1
+INSTALL_SSH_LDAP_HELPER=""
017ff1
+AC_ARG_WITH(ldap,
017ff1
+	[  --with-ldap[[=PATH]]      Enable LDAP pubkey support (optionally in PATH)],
017ff1
+	[
017ff1
+		if test "x$withval" != "xno" ; then
f09e2e
+
017ff1
+			INSTALL_SSH_LDAP_HELPER="yes"
017ff1
+			CPPFLAGS="$CPPFLAGS -DLDAP_DEPRECATED"
f09e2e
+
017ff1
+			if test "x$withval" != "xyes" ; then
017ff1
+				CPPFLAGS="$CPPFLAGS -I${withval}/include"
017ff1
+				LDFLAGS="$LDFLAGS -L${withval}/lib"
017ff1
+			fi
f09e2e
+
017ff1
+			AC_DEFINE([WITH_LDAP_PUBKEY], 1, [Enable LDAP pubkey support])
017ff1
+			LDAP_MSG="yes"
f09e2e
+
017ff1
+			AC_CHECK_HEADERS(lber.h)
017ff1
+			AC_CHECK_HEADERS(ldap.h, , AC_MSG_ERROR(could not locate <ldap.h>))
017ff1
+			AC_CHECK_HEADERS(ldap_ssl.h)
f09e2e
+
017ff1
+			AC_ARG_WITH(ldap-lib,
017ff1
+				[  --with-ldap-lib=type    select ldap library [auto|netscape5|netscape4|netscape3|umich|openldap]])
f09e2e
+
017ff1
+			if test -z "$with_ldap_lib"; then
017ff1
+				with_ldap_lib=auto
017ff1
+			fi
f09e2e
+
017ff1
+			if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = umich -o $with_ldap_lib = openldap \); then
017ff1
+				AC_CHECK_LIB(lber, main, LIBS="-llber $LIBS" found_ldap_lib=yes)
017ff1
+				AC_CHECK_LIB(ldap, main, LIBS="-lldap $LIBS" found_ldap_lib=yes)
017ff1
+			fi
f09e2e
+
017ff1
+			if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape5 \); then
017ff1
+				AC_CHECK_LIB(ldap50, main, LIBS="-lldap50 -lssldap50 -lssl3 -lnss3 -lnspr4 -lprldap50 -lplc4 -lplds4 $LIBS" found_ldap_lib=yes)
017ff1
+			fi
f09e2e
+
017ff1
+			if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape4 \); then
017ff1
+				AC_CHECK_LIB(ldapssl41, main, LIBS="-lldapssl41 -lplc3 -lplds3 -lnspr3 $LIBS" found_ldap_lib=yes)
017ff1
+				if test -z "$found_ldap_lib"; then
017ff1
+					AC_CHECK_LIB(ldapssl40, main, LIBS="-lldapssl40 $LIBS" found_ldap_lib=yes)
017ff1
+				fi
017ff1
+				if test -z "$found_ldap_lib"; then
017ff1
+					AC_CHECK_LIB(ldap41, main, LIBS="-lldap41 $LIBS" found_ldap_lib=yes)
017ff1
+				fi
017ff1
+				if test -z "$found_ldap_lib"; then
017ff1
+					AC_CHECK_LIB(ldap40, main, LIBS="-lldap40 $LIBS" found_ldap_lib=yes)
017ff1
+				fi
017ff1
+			fi
f09e2e
+
017ff1
+			if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape3 \); then
017ff1
+				AC_CHECK_LIB(ldapssl30, main, LIBS="-lldapssl30 $LIBS" found_ldap_lib=yes)
017ff1
+			fi
f09e2e
+
017ff1
+			if test -z "$found_ldap_lib"; then
017ff1
+				AC_MSG_ERROR(could not locate a valid LDAP library)
017ff1
+			fi
f09e2e
+
017ff1
+			AC_MSG_CHECKING([for working LDAP support])
017ff1
+			AC_TRY_COMPILE(
017ff1
+				[#include <sys/types.h>
017ff1
+				 #include <ldap.h>],
017ff1
+				[(void)ldap_init(0, 0);],
017ff1
+				[AC_MSG_RESULT(yes)],
017ff1
+				[
017ff1
+				    AC_MSG_RESULT(no) 
017ff1
+					AC_MSG_ERROR([** Incomplete or missing ldap libraries **])
017ff1
+				])
017ff1
+			AC_CHECK_FUNCS( \
017ff1
+				ldap_init \
017ff1
+				ldap_get_lderrno \
017ff1
+				ldap_set_lderrno \
017ff1
+				ldap_parse_result \
017ff1
+				ldap_memfree \
017ff1
+				ldap_controls_free \
017ff1
+				ldap_set_option \
017ff1
+				ldap_get_option \
017ff1
+				ldapssl_init \
017ff1
+				ldap_start_tls_s \
017ff1
+				ldap_pvt_tls_set_option \
017ff1
+				ldap_initialize \
017ff1
+			)
017ff1
+			AC_CHECK_FUNCS(ldap_set_rebind_proc,
017ff1
+				AC_MSG_CHECKING([number arguments of ldap_set_rebind_proc])
017ff1
+				AC_TRY_COMPILE(
017ff1
+					[#include <lber.h>
017ff1
+					#include <ldap.h>],
017ff1
+					[ldap_set_rebind_proc(0, 0, 0);],
017ff1
+					[ac_cv_ldap_set_rebind_proc=3],
017ff1
+					[ac_cv_ldap_set_rebind_proc=2])
017ff1
+				AC_MSG_RESULT($ac_cv_ldap_set_rebind_proc)
017ff1
+				AC_DEFINE(LDAP_SET_REBIND_PROC_ARGS, $ac_cv_ldap_set_rebind_proc, [number arguments of ldap_set_rebind_proc])
017ff1
+			)
017ff1
+		fi
017ff1
+	]
017ff1
+)
017ff1
+AC_SUBST(INSTALL_SSH_LDAP_HELPER)
f09e2e
+
017ff1
 dnl    Checks for library functions. Please keep in alphabetical order
017ff1
 AC_CHECK_FUNCS([ \
017ff1
 	Blowfish_initstate \
017ff1
diff --git a/ldap-helper.c b/ldap-helper.c
017ff1
new file mode 100644
017ff1
index 0000000..e95a94a
017ff1
--- /dev/null
017ff1
+++ b/ldap-helper.c
017ff1
@@ -0,0 +1,155 @@
017ff1
+/* $OpenBSD: ssh-pka-ldap.c,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
017ff1
+/*
017ff1
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
017ff1
+ *
017ff1
+ * Redistribution and use in source and binary forms, with or without
017ff1
+ * modification, are permitted provided that the following conditions
017ff1
+ * are met:
017ff1
+ * 1. Redistributions of source code must retain the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer.
017ff1
+ * 2. Redistributions in binary form must reproduce the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer in the
017ff1
+ *    documentation and/or other materials provided with the distribution.
017ff1
+ *
017ff1
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017ff1
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
017ff1
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
017ff1
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
017ff1
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
017ff1
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
017ff1
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
017ff1
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
017ff1
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
017ff1
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
017ff1
+ */
f09e2e
+
017ff1
+#include "ldapincludes.h"
017ff1
+#include "log.h"
017ff1
+#include "misc.h"
017ff1
+#include "xmalloc.h"
017ff1
+#include "ldapconf.h"
017ff1
+#include "ldapbody.h"
017ff1
+#include <string.h>
017ff1
+#include <unistd.h>
f09e2e
+
017ff1
+static int config_debug = 0;
017ff1
+int config_exclusive_config_file = 0;
017ff1
+static char *config_file_name = "/etc/ssh/ldap.conf";
017ff1
+static char *config_single_user = NULL;
017ff1
+static int config_verbose = SYSLOG_LEVEL_VERBOSE;
017ff1
+int config_warning_config_file = 0;
017ff1
+extern char *__progname;
f09e2e
+
017ff1
+static void
017ff1
+usage(void)
017ff1
+{
017ff1
+	fprintf(stderr, "usage: %s [options]\n",
017ff1
+	    __progname);
017ff1
+	fprintf(stderr, "Options:\n");
017ff1
+	fprintf(stderr, "  -d          Output the log messages to stderr.\n");
017ff1
+	fprintf(stderr, "  -e          Check the config file for unknown commands.\n");
017ff1
+	fprintf(stderr, "  -f file     Use alternate config file (default is /etc/ssh/ldap.conf).\n");
017ff1
+	fprintf(stderr, "  -s user     Do not demonize, send the user's key to stdout.\n");
017ff1
+	fprintf(stderr, "  -v          Increase verbosity of the debug output (implies -d).\n");
017ff1
+	fprintf(stderr, "  -w          Warn on unknown commands in the config file.\n");
017ff1
+	exit(1);
017ff1
+}
f09e2e
+
017ff1
+/*
017ff1
+ * Main program for the ssh pka ldap agent.
017ff1
+ */
f09e2e
+
017ff1
+int
017ff1
+main(int ac, char **av)
017ff1
+{
017ff1
+	int opt;
017ff1
+	FILE *outfile = NULL;
f09e2e
+
017ff1
+	__progname = ssh_get_progname(av[0]);
f09e2e
+
017ff1
+	log_init(__progname, SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
f09e2e
+
017ff1
+	/*
017ff1
+	 * Initialize option structure to indicate that no values have been
017ff1
+	 * set.
017ff1
+	 */
017ff1
+	initialize_options();
f09e2e
+
017ff1
+	/* Parse command-line arguments. */
017ff1
+	while ((opt = getopt(ac, av, "def:s:vw")) != -1) {
017ff1
+		switch (opt) {
017ff1
+		case 'd':
017ff1
+			config_debug = 1;
017ff1
+			break;
f09e2e
+
017ff1
+		case 'e':
017ff1
+			config_exclusive_config_file = 1;
017ff1
+			config_warning_config_file = 1;
017ff1
+			break;
f09e2e
+
017ff1
+		case 'f':
017ff1
+			config_file_name = optarg;
017ff1
+			break;
f09e2e
+
017ff1
+		case 's':
017ff1
+			config_single_user = optarg;
017ff1
+			outfile = fdopen (dup (fileno (stdout)), "w");
017ff1
+			break;
f09e2e
+
017ff1
+		case 'v':
017ff1
+			config_debug = 1;
017ff1
+			if (config_verbose < SYSLOG_LEVEL_DEBUG3)
017ff1
+			    config_verbose++;
017ff1
+			break;
f09e2e
+
017ff1
+		case 'w':
017ff1
+			config_warning_config_file = 1;
017ff1
+			break;
f09e2e
+
017ff1
+		case '?':
017ff1
+		default:
017ff1
+			usage();
017ff1
+			break;
017ff1
+		}
f09e2e
+	}
f09e2e
+
017ff1
+	/* Initialize loging */
017ff1
+	log_init(__progname, config_verbose, SYSLOG_FACILITY_AUTH, config_debug);
f09e2e
+
017ff1
+	if (ac != optind)
017ff1
+	    fatal ("illegal extra parameter %s", av[1]);
f09e2e
+
017ff1
+	/* Ensure that fds 0 and 2 are open or directed to /dev/null */
017ff1
+	if (config_debug == 0)
017ff1
+	    sanitise_stdfd();
f09e2e
+
017ff1
+	/* Read config file */
017ff1
+	read_config_file(config_file_name);
017ff1
+	fill_default_options();
017ff1
+	if (config_verbose == SYSLOG_LEVEL_DEBUG3) {
017ff1
+		debug3 ("=== Configuration ===");
017ff1
+		dump_config();
017ff1
+		debug3 ("=== *** ===");
017ff1
+	}
f09e2e
+
017ff1
+	ldap_checkconfig();
017ff1
+	ldap_do_connect();
f09e2e
+
017ff1
+	if (config_single_user) {
017ff1
+		process_user (config_single_user, outfile);
017ff1
+	} else {
017ff1
+		usage();
017ff1
+		fatal ("Not yet implemented");
017ff1
+/* TODO
017ff1
+ * open unix socket a run the loop on it
017ff1
+ */
f09e2e
+	}
f09e2e
+
017ff1
+	ldap_do_close();
017ff1
+	return 0;
f09e2e
+}
f09e2e
+
017ff1
+/* Ugly hack */
017ff1
+void   *buffer_get_string(Buffer *b, u_int *l) { return NULL; }
017ff1
+void    buffer_put_string(Buffer *b, const void *f, u_int l) {}
f09e2e
+
017ff1
diff --git a/ldap-helper.h b/ldap-helper.h
017ff1
new file mode 100644
017ff1
index 0000000..14cb29a
017ff1
--- /dev/null
017ff1
+++ b/ldap-helper.h
017ff1
@@ -0,0 +1,32 @@
017ff1
+/* $OpenBSD: ldap-helper.h,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
f09e2e
+/*
f09e2e
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
017ff1
+#ifndef LDAP_HELPER_H
017ff1
+#define LDAP_HELPER_H
017ff1
+
017ff1
+extern int config_exclusive_config_file;
017ff1
+extern int config_warning_config_file;
017ff1
+
017ff1
+#endif /* LDAP_HELPER_H */
017ff1
diff --git a/ldap.conf b/ldap.conf
017ff1
new file mode 100644
017ff1
index 0000000..42e38d3
017ff1
--- /dev/null
017ff1
+++ b/ldap.conf
017ff1
@@ -0,0 +1,88 @@
017ff1
+# $Id: openssh-5.5p1-ldap.patch,v 1.3 2010/07/07 13:48:36 jfch2222 Exp $
017ff1
+#
017ff1
+# This is the example configuration file for the OpenSSH
017ff1
+# LDAP backend
017ff1
+# 
017ff1
+# see ssh-ldap.conf(5)
017ff1
+#
017ff1
+
017ff1
+# URI with your LDAP server name. This allows to use
017ff1
+# Unix Domain Sockets to connect to a local LDAP Server.
017ff1
+#uri ldap://127.0.0.1/
017ff1
+#uri ldaps://127.0.0.1/   
017ff1
+#uri ldapi://%2fvar%2frun%2fldapi_sock/
017ff1
+# Note: %2f encodes the '/' used as directory separator
017ff1
+
017ff1
+# Another way to specify your LDAP server is to provide an
017ff1
+# host name and the port of our LDAP server. Host name
017ff1
+# must be resolvable without using LDAP.
017ff1
+# Multiple hosts may be specified, each separated by a 
017ff1
+# space. How long nss_ldap takes to failover depends on
017ff1
+# whether your LDAP client library supports configurable
017ff1
+# network or connect timeouts (see bind_timelimit).
017ff1
+#host 127.0.0.1
017ff1
+
017ff1
+# The port.
017ff1
+# Optional: default is 389.
017ff1
+#port 389
017ff1
+
017ff1
+# The distinguished name to bind to the server with.
017ff1
+# Optional: default is to bind anonymously.
017ff1
+#binddn cn=openssh_keys,dc=example,dc=org
017ff1
+
017ff1
+# The credentials to bind with. 
017ff1
+# Optional: default is no credential.
017ff1
+#bindpw TopSecret
017ff1
+
017ff1
+# The distinguished name of the search base.
017ff1
+#base dc=example,dc=org
017ff1
+
017ff1
+# The LDAP version to use (defaults to 3
017ff1
+# if supported by client library)
017ff1
+#ldap_version 3
017ff1
+
017ff1
+# The search scope.
017ff1
+#scope sub
017ff1
+#scope one
017ff1
+#scope base
017ff1
+
017ff1
+# Search timelimit
017ff1
+#timelimit 30
017ff1
+
017ff1
+# Bind/connect timelimit
017ff1
+#bind_timelimit 30
f09e2e
+
017ff1
+# Reconnect policy: hard (default) will retry connecting to
017ff1
+# the software with exponential backoff, soft will fail
017ff1
+# immediately.
017ff1
+#bind_policy hard
f09e2e
+
017ff1
+# SSL setup, may be implied by URI also.
017ff1
+#ssl no
017ff1
+#ssl on
017ff1
+#ssl start_tls
f09e2e
+
017ff1
+# OpenLDAP SSL options
017ff1
+# Require and verify server certificate (yes/no)
017ff1
+# Default is to use libldap's default behavior, which can be configured in
017ff1
+# /etc/openldap/ldap.conf using the TLS_REQCERT setting.  The default for
017ff1
+# OpenLDAP 2.0 and earlier is "no", for 2.1 and later is "yes".
017ff1
+#tls_checkpeer hard
f09e2e
+
017ff1
+# CA certificates for server certificate verification
017ff1
+# At least one of these are required if tls_checkpeer is "yes"
017ff1
+#tls_cacertfile /etc/ssl/ca.cert
017ff1
+#tls_cacertdir /etc/pki/tls/certs
017ff1
+
017ff1
+# Seed the PRNG if /dev/urandom is not provided
017ff1
+#tls_randfile /var/run/egd-pool
017ff1
+
017ff1
+# SSL cipher suite
017ff1
+# See man ciphers for syntax
017ff1
+#tls_ciphers TLSv1
017ff1
+
017ff1
+# Client certificate and key
017ff1
+# Use these, if your server requires client authentication.
017ff1
+#tls_cert
017ff1
+#tls_key
017ff1
+
017ff1
diff --git a/ldapbody.c b/ldapbody.c
017ff1
new file mode 100644
017ff1
index 0000000..3029108
017ff1
--- /dev/null
017ff1
+++ b/ldapbody.c
017ff1
@@ -0,0 +1,494 @@
017ff1
+/* $OpenBSD: ldapbody.c,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
f09e2e
+/*
f09e2e
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+#include "ldapincludes.h"
f09e2e
+#include "log.h"
f09e2e
+#include "xmalloc.h"
f09e2e
+#include "ldapconf.h"
017ff1
+#include "ldapmisc.h"
017ff1
+#include "ldapbody.h"
017ff1
+#include <stdio.h>
f09e2e
+#include <unistd.h>
f09e2e
+
017ff1
+#define LDAPSEARCH_FORMAT "(&(objectclass=%s)(objectclass=ldapPublicKey)(uid=%s)%s)"
017ff1
+#define PUBKEYATTR "sshPublicKey"
017ff1
+#define LDAP_LOGFILE	"%s/ldap.%d"
017ff1
+
017ff1
+static FILE *logfile = NULL;
017ff1
+static LDAP *ld;
017ff1
+
017ff1
+static char *attrs[] = {
017ff1
+    PUBKEYATTR,
017ff1
+    NULL
017ff1
+};
017ff1
+
017ff1
+void
017ff1
+ldap_checkconfig (void)
017ff1
+{
017ff1
+#ifdef HAVE_LDAP_INITIALIZE
017ff1
+		if (options.host == NULL && options.uri == NULL)
017ff1
+#else
017ff1
+		if (options.host == NULL)
017ff1
+#endif
017ff1
+		    fatal ("missing  \"host\" in config file");
017ff1
+}
017ff1
+
017ff1
+#if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
017ff1
+static int
017ff1
+_rebind_proc (LDAP * ld, LDAP_CONST char *url, int request, ber_int_t msgid)
017ff1
+{
017ff1
+	struct timeval timeout;
017ff1
+	int rc;
017ff1
+#if defined(HAVE_LDAP_PARSE_RESULT) && defined(HAVE_LDAP_CONTROLS_FREE)
017ff1
+	LDAPMessage *result;
017ff1
+#endif /* HAVE_LDAP_PARSE_RESULT && HAVE_LDAP_CONTROLS_FREE */
017ff1
+
017ff1
+	debug2 ("Doing LDAP rebind to %s", options.binddn);
017ff1
+	if (options.ssl == SSL_START_TLS) {
017ff1
+		if ((rc = ldap_start_tls_s (ld, NULL, NULL)) != LDAP_SUCCESS) {
017ff1
+			error ("ldap_starttls_s: %s", ldap_err2string (rc));
017ff1
+			return LDAP_OPERATIONS_ERROR;
017ff1
+		}
017ff1
+	}
017ff1
+
017ff1
+#if !defined(HAVE_LDAP_PARSE_RESULT) || !defined(HAVE_LDAP_CONTROLS_FREE)
017ff1
+	return ldap_simple_bind_s (ld, options.binddn, options.bindpw);
017ff1
+#else
017ff1
+	if (ldap_simple_bind(ld, options.binddn, options.bindpw) < 0)
017ff1
+	    fatal ("ldap_simple_bind %s", ldap_err2string (ldap_get_lderrno (ld, 0, 0)));
017ff1
+
017ff1
+	timeout.tv_sec = options.bind_timelimit;
017ff1
+	timeout.tv_usec = 0;
017ff1
+	result = NULL;
017ff1
+	if ((rc = ldap_result (ld, msgid, FALSE, &timeout, &result)) < 1) {
017ff1
+		error ("ldap_result %s", ldap_err2string (ldap_get_lderrno (ld, 0, 0)));
017ff1
+		ldap_msgfree (result);
017ff1
+		return LDAP_OPERATIONS_ERROR;
017ff1
+	}
017ff1
+	debug3 ("LDAP rebind to %s succesfull", options.binddn);
017ff1
+	return rc;
017ff1
+#endif
017ff1
+}
017ff1
+#else
017ff1
+
017ff1
+static int
017ff1
+_rebind_proc (LDAP * ld, char **whop, char **credp, int *methodp, int freeit)
017ff1
+{
017ff1
+	if (freeit)
017ff1
+	    return LDAP_SUCCESS;
017ff1
+
017ff1
+	*whop = strdup (options.binddn);
017ff1
+	*credp = strdup (options.bindpw);
017ff1
+	*methodp = LDAP_AUTH_SIMPLE;
017ff1
+	debug2 ("Doing LDAP rebind for %s", *whop);
017ff1
+	return LDAP_SUCCESS;
017ff1
+}
017ff1
+#endif
017ff1
+
017ff1
+void
017ff1
+ldap_do_connect(void)
017ff1
+{
017ff1
+	int rc, msgid, ld_errno = 0;
017ff1
+	struct timeval timeout;
017ff1
+#if defined(HAVE_LDAP_PARSE_RESULT) && defined(HAVE_LDAP_CONTROLS_FREE)
017ff1
+	int parserc;
017ff1
+	LDAPMessage *result;
017ff1
+	LDAPControl **controls;
017ff1
+	int reconnect = 0;
017ff1
+#endif /* HAVE_LDAP_PARSE_RESULT && HAVE_LDAP_CONTROLS_FREE */
017ff1
+
017ff1
+	debug ("LDAP do connect");
017ff1
+
017ff1
+retry:
017ff1
+	if (reconnect) {
017ff1
+		debug3 ("Reconnecting with ld_errno %d", ld_errno);
017ff1
+		if (options.bind_policy == 0 ||
017ff1
+		    (ld_errno != LDAP_SERVER_DOWN && ld_errno != LDAP_TIMEOUT) ||
017ff1
+			reconnect > 5)
017ff1
+			    fatal ("Cannot connect to LDAP server");
017ff1
+	
017ff1
+		if (reconnect > 1)
017ff1
+			sleep (reconnect - 1);
017ff1
+
017ff1
+		if (ld != NULL) {
017ff1
+			ldap_unbind (ld);
017ff1
+			ld = NULL;
017ff1
+		}
017ff1
+		logit("reconnecting to LDAP server...");
017ff1
+	}
017ff1
+
017ff1
+	if (ld == NULL) {
017ff1
+		int rc;
017ff1
+		struct timeval tv;
017ff1
+
017ff1
+#ifdef HAVE_LDAP_SET_OPTION
017ff1
+		if (options.debug > 0) {
017ff1
+#ifdef LBER_OPT_LOG_PRINT_FILE
017ff1
+			if (options.logdir) {
017ff1
+				char *logfilename;
017ff1
+				int logfilenamelen;
017ff1
+
017ff1
+				logfilenamelen = strlen (LDAP_LOGFILE) + strlen ("000000") + strlen (options.logdir);
017ff1
+				logfilename = xmalloc (logfilenamelen);
017ff1
+				snprintf (logfilename, logfilenamelen, LDAP_LOGFILE, options.logdir, (int) getpid ());
017ff1
+				logfilename[logfilenamelen - 1] = 0;
017ff1
+				if ((logfile = fopen (logfilename, "a")) == NULL)
017ff1
+				    fatal ("cannot append to %s: %s", logfilename, strerror (errno));
017ff1
+				debug3 ("LDAP debug into %s", logfilename);
017ff1
+				free (logfilename);
017ff1
+				ber_set_option (NULL, LBER_OPT_LOG_PRINT_FILE, logfile);
017ff1
+			}
017ff1
+#endif
017ff1
+			if (options.debug) {
017ff1
+#ifdef LBER_OPT_DEBUG_LEVEL
017ff1
+				ber_set_option (NULL, LBER_OPT_DEBUG_LEVEL, &options.debug);
017ff1
+#endif /* LBER_OPT_DEBUG_LEVEL */
017ff1
+#ifdef LDAP_OPT_DEBUG_LEVEL
017ff1
+				(void) ldap_set_option (NULL, LDAP_OPT_DEBUG_LEVEL, &options.debug);
017ff1
+#endif /* LDAP_OPT_DEBUG_LEVEL */
017ff1
+				debug3 ("Set LDAP debug to %d", options.debug);
017ff1
+			}
017ff1
+		}
017ff1
+#endif /* HAVE_LDAP_SET_OPTION */
017ff1
+
017ff1
+		ld = NULL;
017ff1
+#ifdef HAVE_LDAPSSL_INIT
017ff1
+		if (options.host != NULL) {
017ff1
+			if (options.ssl_on == SSL_LDAPS) {
017ff1
+				if ((rc = ldapssl_client_init (options.sslpath, NULL)) != LDAP_SUCCESS)
017ff1
+				    fatal ("ldapssl_client_init %s", ldap_err2string (rc));
017ff1
+				debug3 ("LDAPssl client init");
017ff1
+			}
017ff1
+
017ff1
+			if (options.ssl_on != SSL_OFF) {
017ff1
+				if ((ld = ldapssl_init (options.host, options.port, TRUE)) == NULL)
017ff1
+				    fatal ("ldapssl_init failed");
017ff1
+				debug3 ("LDAPssl init");
017ff1
+			}
017ff1
+		}
017ff1
+#endif /* HAVE_LDAPSSL_INIT */
017ff1
+
017ff1
+		/* continue with opening */
017ff1
+		if (ld == NULL) {
017ff1
+#if defined (HAVE_LDAP_START_TLS_S) || (defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS))
017ff1
+			/* Some global TLS-specific options need to be set before we create our
017ff1
+			 * session context, so we set them here. */
017ff1
+
017ff1
+#ifdef LDAP_OPT_X_TLS_RANDOM_FILE
017ff1
+			/* rand file */
017ff1
+			if (options.tls_randfile != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
017ff1
+				    options.tls_randfile)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_set_option(LDAP_OPT_X_TLS_RANDOM_FILE): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS random file %s", options.tls_randfile);
017ff1
+			}
017ff1
+#endif /* LDAP_OPT_X_TLS_RANDOM_FILE */
017ff1
+
017ff1
+			/* ca cert file */
017ff1
+			if (options.tls_cacertfile != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTFILE,
017ff1
+				    options.tls_cacertfile)) != LDAP_SUCCESS)
017ff1
+					error ("ldap_set_option(LDAP_OPT_X_TLS_CACERTFILE): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS CA cert file %s ", options.tls_cacertfile);
017ff1
+			}
017ff1
+
017ff1
+			/* ca cert directory */
017ff1
+			if (options.tls_cacertdir != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTDIR,
017ff1
+				    options.tls_cacertdir)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_set_option(LDAP_OPT_X_TLS_CACERTDIR): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS CA cert dir %s ", options.tls_cacertdir);
017ff1
+			}
017ff1
+
017ff1
+			/* require cert? */
017ff1
+			if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
017ff1
+			    &options.tls_checkpeer)) != LDAP_SUCCESS)
017ff1
+				fatal ("ldap_set_option(LDAP_OPT_X_TLS_REQUIRE_CERT): %s",
017ff1
+				    ldap_err2string (rc));
017ff1
+			debug3 ("Set TLS check peer to %d ", options.tls_checkpeer);
017ff1
+
017ff1
+			/* set cipher suite, certificate and private key: */
017ff1
+			if (options.tls_ciphers != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_CIPHER_SUITE,
017ff1
+				    options.tls_ciphers)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_set_option(LDAP_OPT_X_TLS_CIPHER_SUITE): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS ciphers to %s ", options.tls_ciphers);
017ff1
+			}
f09e2e
+
017ff1
+			/* cert file */
017ff1
+			if (options.tls_cert != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_CERTFILE,
017ff1
+				    options.tls_cert)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_set_option(LDAP_OPT_X_TLS_CERTFILE): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS cert file %s ", options.tls_cert);
017ff1
+			}
f09e2e
+
017ff1
+			/* key file */
017ff1
+			if (options.tls_key != NULL) {
017ff1
+				if ((rc = ldap_set_option (NULL, LDAP_OPT_X_TLS_KEYFILE,
017ff1
+				    options.tls_key)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_set_option(LDAP_OPT_X_TLS_KEYFILE): %s",
017ff1
+					    ldap_err2string (rc));
017ff1
+				debug3 ("Set TLS key file %s ", options.tls_key);
017ff1
+			}
017ff1
+#endif
017ff1
+#ifdef HAVE_LDAP_INITIALIZE
017ff1
+			if (options.uri != NULL) {
017ff1
+				if ((rc = ldap_initialize (&ld, options.uri)) != LDAP_SUCCESS)
017ff1
+					fatal ("ldap_initialize %s", ldap_err2string (rc));
017ff1
+				debug3 ("LDAP initialize %s", options.uri);
017ff1
+			}
017ff1
+	}
017ff1
+#endif /* HAVE_LDAP_INTITIALIZE */
f09e2e
+
017ff1
+		/* continue with opening */
017ff1
+		if ((ld == NULL) && (options.host != NULL)) {
017ff1
+#ifdef HAVE_LDAP_INIT
017ff1
+			if ((ld = ldap_init (options.host, options.port)) == NULL)
017ff1
+			    fatal ("ldap_init failed");
017ff1
+			debug3 ("LDAP init %s:%d", options.host, options.port);
017ff1
+#else
017ff1
+			if ((ld = ldap_open (options.host, options.port)) == NULL)
017ff1
+			    fatal ("ldap_open failed");
017ff1
+			debug3 ("LDAP open %s:%d", options.host, options.port);
017ff1
+#endif /* HAVE_LDAP_INIT */
017ff1
+		}
f09e2e
+
017ff1
+		if (ld == NULL)
017ff1
+			fatal ("no way to open ldap");
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
017ff1
+		if (options.ssl == SSL_LDAPS) {
017ff1
+			if ((rc = ldap_set_option (ld, LDAP_OPT_X_TLS, &options.tls_checkpeer)) != LDAP_SUCCESS)
017ff1
+				fatal ("ldap_set_option(LDAP_OPT_X_TLS) %s", ldap_err2string (rc));
017ff1
+			debug3 ("LDAP set LDAP_OPT_X_TLS_%d", options.tls_checkpeer);
017ff1
+		}
017ff1
+#endif /* LDAP_OPT_X_TLS */
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_PROTOCOL_VERSION)
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION,
017ff1
+		    &options.ldap_version);
017ff1
+#else
017ff1
+		ld->ld_version = options.ldap_version;
017ff1
+#endif
017ff1
+		debug3 ("LDAP set version to %d", options.ldap_version);
f09e2e
+
017ff1
+#if LDAP_SET_REBIND_PROC_ARGS == 3
017ff1
+		ldap_set_rebind_proc (ld, _rebind_proc, NULL);
017ff1
+#elif LDAP_SET_REBIND_PROC_ARGS == 2
017ff1
+		ldap_set_rebind_proc (ld, _rebind_proc);
017ff1
+#else
017ff1
+#warning unknown LDAP_SET_REBIND_PROC_ARGS
017ff1
+#endif
017ff1
+		debug3 ("LDAP set rebind proc");
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_DEREF)
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_DEREF, &options.deref);
017ff1
+#else
017ff1
+		ld->ld_deref = options.deref;
017ff1
+#endif
017ff1
+		debug3 ("LDAP set deref to %d", options.deref);
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_TIMELIMIT)
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_TIMELIMIT,
017ff1
+		    &options.timelimit);
017ff1
+#else
017ff1
+		ld->ld_timelimit = options.timelimit;
017ff1
+#endif
017ff1
+		debug3 ("LDAP set timelimit to %d", options.timelimit);
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_X_OPT_CONNECT_TIMEOUT)
017ff1
+		/*
017ff1
+		 * This is a new option in the Netscape SDK which sets 
017ff1
+		 * the TCP connect timeout. For want of a better value,
017ff1
+		 * we use the bind_timelimit to control this.
017ff1
+		 */
017ff1
+		timeout = options.bind_timelimit * 1000;
017ff1
+		(void) ldap_set_option (ld, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout);
017ff1
+		debug3 ("LDAP set opt connect timeout to %d", timeout);
017ff1
+#endif
770374
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_NETWORK_TIMEOUT)
017ff1
+		tv.tv_sec = options.bind_timelimit;
017ff1
+		tv.tv_usec = 0;
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_NETWORK_TIMEOUT, &tv;;
017ff1
+		debug3 ("LDAP set opt network timeout to %ld.0", tv.tv_sec);
017ff1
+#endif
770374
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_REFERRALS)
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_REFERRALS,
017ff1
+		    options.referrals ? LDAP_OPT_ON : LDAP_OPT_OFF);
017ff1
+		debug3 ("LDAP set referrals to %d", options.referrals);
017ff1
+#endif
770374
+
017ff1
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_RESTART)
017ff1
+		(void) ldap_set_option (ld, LDAP_OPT_RESTART,
017ff1
+		    options.restart ? LDAP_OPT_ON : LDAP_OPT_OFF);
017ff1
+		debug3 ("LDAP set restart to %d", options.restart);
017ff1
+#endif
770374
+
017ff1
+#ifdef HAVE_LDAP_START_TLS_S
017ff1
+		if (options.ssl == SSL_START_TLS) {
017ff1
+			int version;
770374
+
017ff1
+			if (ldap_get_option (ld, LDAP_OPT_PROTOCOL_VERSION, &version)
017ff1
+			    == LDAP_SUCCESS) {
017ff1
+				if (version < LDAP_VERSION3) {
017ff1
+					version = LDAP_VERSION3;
017ff1
+					(void) ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION,
017ff1
+					    &version);
017ff1
+					debug3 ("LDAP set version to %d", version);
017ff1
+				}
017ff1
+			}
770374
+
017ff1
+			if ((rc = ldap_start_tls_s (ld, NULL, NULL)) != LDAP_SUCCESS)
017ff1
+			    fatal ("ldap_starttls_s: %s", ldap_err2string (rc));
017ff1
+			debug3 ("LDAP start TLS");
017ff1
+		}
017ff1
+#endif /* HAVE_LDAP_START_TLS_S */
017ff1
+	}
770374
+
017ff1
+	if ((msgid = ldap_simple_bind (ld, options.binddn,
017ff1
+	    options.bindpw)) == -1) {
017ff1
+		ld_errno = ldap_get_lderrno (ld, 0, 0);
770374
+
017ff1
+		error ("ldap_simple_bind %s", ldap_err2string (ld_errno));
017ff1
+		reconnect++;
017ff1
+		goto retry;
017ff1
+	}
017ff1
+	debug3 ("LDAP simple bind (%s)", options.binddn);
f09e2e
+
017ff1
+	timeout.tv_sec = options.bind_timelimit;
017ff1
+	timeout.tv_usec = 0;
017ff1
+	if ((rc = ldap_result (ld, msgid, FALSE, &timeout, &result)) < 1) {
017ff1
+		ld_errno = ldap_get_lderrno (ld, 0, 0);
f09e2e
+
017ff1
+		error ("ldap_result %s", ldap_err2string (ld_errno));
017ff1
+		reconnect++;
017ff1
+		goto retry;
f09e2e
+	}
017ff1
+	debug3 ("LDAP result in time");
f09e2e
+
017ff1
+#if defined(HAVE_LDAP_PARSE_RESULT) && defined(HAVE_LDAP_CONTROLS_FREE)
017ff1
+	controls = NULL;
017ff1
+	if ((parserc = ldap_parse_result (ld, result, &rc, 0, 0, 0, &controls, TRUE)) != LDAP_SUCCESS)
017ff1
+	    fatal ("ldap_parse_result %s", ldap_err2string (parserc));
017ff1
+	debug3 ("LDAP parse result OK");
f09e2e
+
017ff1
+	if (controls != NULL) {
017ff1
+		ldap_controls_free (controls);
017ff1
+	}
017ff1
+#else
017ff1
+	rc = ldap_result2error (session->ld, result, TRUE);
017ff1
+#endif
017ff1
+	if (rc != LDAP_SUCCESS)
017ff1
+	    fatal ("error trying to bind as user \"%s\" (%s)",
017ff1
+		options.binddn, ldap_err2string (rc));
f09e2e
+
017ff1
+	debug2 ("LDAP do connect OK");
017ff1
+}
f09e2e
+
017ff1
+void
017ff1
+process_user (const char *user, FILE *output)
017ff1
+{
017ff1
+	LDAPMessage *res, *e;
017ff1
+	char *buffer;
017ff1
+	int bufflen, rc, i;
017ff1
+	struct timeval timeout;
f09e2e
+
017ff1
+	debug ("LDAP process user");
f09e2e
+
017ff1
+	/* quick check for attempts to be evil */
017ff1
+	if ((strchr(user, '(') != NULL) || (strchr(user, ')') != NULL) ||
017ff1
+	    (strchr(user, '*') != NULL) || (strchr(user, '\\') != NULL)) {
017ff1
+		logit ("illegal user name %s not processed", user);
017ff1
+		return;
017ff1
+	}
f09e2e
+
017ff1
+	/* build  filter for LDAP request */
017ff1
+	bufflen = strlen (LDAPSEARCH_FORMAT) + strlen(options.account_class) + strlen (user);
017ff1
+	if (options.ssh_filter != NULL)
017ff1
+	    bufflen += strlen (options.ssh_filter);
017ff1
+	buffer = xmalloc (bufflen);
017ff1
+	snprintf(buffer, bufflen, LDAPSEARCH_FORMAT, options.account_class, user, (options.ssh_filter != NULL) ? options.ssh_filter : NULL);
017ff1
+	buffer[bufflen - 1] = 0;
f09e2e
+
017ff1
+	debug3 ("LDAP search scope = %d %s", options.scope, buffer);
f09e2e
+
017ff1
+	timeout.tv_sec = options.timelimit;
017ff1
+	timeout.tv_usec = 0;
017ff1
+	if ((rc = ldap_search_st(ld, options.base, options.scope, buffer, attrs, 0, &timeout, &res)) != LDAP_SUCCESS) {
017ff1
+		error ("ldap_search_st(): %s", ldap_err2string (rc));
017ff1
+		free (buffer);
017ff1
+		return;
017ff1
+	}
f09e2e
+
017ff1
+	/* free */
017ff1
+	free (buffer);
f09e2e
+
017ff1
+	for (e = ldap_first_entry(ld, res); e != NULL; e = ldap_next_entry(ld, e)) {
017ff1
+		int num;
017ff1
+		struct berval **keys;
f09e2e
+
017ff1
+		keys = ldap_get_values_len(ld, e, PUBKEYATTR);
017ff1
+		num = ldap_count_values_len(keys);
017ff1
+		for (i = 0 ; i < num ; i++) {
017ff1
+			char *cp; //, *options = NULL;
f09e2e
+
017ff1
+			for (cp = keys[i]->bv_val; *cp == ' ' || *cp == '\t'; cp++);
017ff1
+			if (!*cp || *cp == '\n' || *cp == '#')
017ff1
+			    continue;
f09e2e
+
017ff1
+			/* We have found the desired key. */
017ff1
+			fprintf (output, "%s\n", keys[i]->bv_val);
017ff1
+		}
f09e2e
+
017ff1
+		ldap_value_free_len(keys);
017ff1
+	}
f09e2e
+
017ff1
+	ldap_msgfree(res);
017ff1
+	debug2 ("LDAP process user finished");
017ff1
+}
f09e2e
+
017ff1
+void
017ff1
+ldap_do_close(void)
017ff1
+{
017ff1
+	int rc;
f09e2e
+
017ff1
+	debug ("LDAP do close");
017ff1
+	if ((rc = ldap_unbind_ext(ld, NULL, NULL)) != LDAP_SUCCESS)
017ff1
+	    fatal ("ldap_unbind_ext: %s",
017ff1
+                                    ldap_err2string (rc));
f09e2e
+
017ff1
+	ld = NULL;
017ff1
+	debug2 ("LDAP do close OK");
017ff1
+	return;
017ff1
+}
f09e2e
+
017ff1
diff --git a/ldapbody.h b/ldapbody.h
017ff1
new file mode 100644
017ff1
index 0000000..665dca2
017ff1
--- /dev/null
017ff1
+++ b/ldapbody.h
017ff1
@@ -0,0 +1,37 @@
017ff1
+/* $OpenBSD: ldapbody.h,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
017ff1
+/*
017ff1
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
017ff1
+ *
017ff1
+ * Redistribution and use in source and binary forms, with or without
017ff1
+ * modification, are permitted provided that the following conditions
017ff1
+ * are met:
017ff1
+ * 1. Redistributions of source code must retain the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer.
017ff1
+ * 2. Redistributions in binary form must reproduce the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer in the
017ff1
+ *    documentation and/or other materials provided with the distribution.
017ff1
+ *
017ff1
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017ff1
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
017ff1
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
017ff1
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
017ff1
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
017ff1
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
017ff1
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
017ff1
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
017ff1
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
017ff1
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
017ff1
+ */
f09e2e
+
017ff1
+#ifndef LDAPBODY_H
017ff1
+#define LDAPBODY_H
f09e2e
+
017ff1
+#include <stdio.h>
f09e2e
+
017ff1
+void ldap_checkconfig(void);
017ff1
+void ldap_do_connect(void);
017ff1
+void process_user(const char *, FILE *);
017ff1
+void ldap_do_close(void);
f09e2e
+
017ff1
+#endif /* LDAPBODY_H */
f09e2e
+
017ff1
diff --git a/ldapconf.c b/ldapconf.c
017ff1
new file mode 100644
017ff1
index 0000000..525060a
017ff1
--- /dev/null
017ff1
+++ b/ldapconf.c
017ff1
@@ -0,0 +1,720 @@
017ff1
+/* $OpenBSD: ldapconf.c,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
017ff1
+/*
017ff1
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
017ff1
+ *
017ff1
+ * Redistribution and use in source and binary forms, with or without
017ff1
+ * modification, are permitted provided that the following conditions
017ff1
+ * are met:
017ff1
+ * 1. Redistributions of source code must retain the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer.
017ff1
+ * 2. Redistributions in binary form must reproduce the above copyright
017ff1
+ *    notice, this list of conditions and the following disclaimer in the
017ff1
+ *    documentation and/or other materials provided with the distribution.
017ff1
+ *
017ff1
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017ff1
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
017ff1
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
017ff1
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
017ff1
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
017ff1
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
017ff1
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
017ff1
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
017ff1
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
017ff1
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
017ff1
+ */
f09e2e
+
017ff1
+#include "ldapincludes.h"
017ff1
+#include "ldap-helper.h"
017ff1
+#include "log.h"
017ff1
+#include "misc.h"
017ff1
+#include "xmalloc.h"
017ff1
+#include "ldapconf.h"
017ff1
+#include <unistd.h>
017ff1
+#include <string.h>
f09e2e
+
017ff1
+/* Keyword tokens. */
f09e2e
+
017ff1
+typedef enum {
017ff1
+	lBadOption,
017ff1
+	lHost, lURI, lBase, lBindDN, lBindPW, lRootBindDN,
017ff1
+	lScope, lDeref, lPort, lTimeLimit, lBind_TimeLimit,
017ff1
+	lLdap_Version, lBind_Policy, lSSLPath, lSSL, lReferrals,
017ff1
+	lRestart, lTLS_CheckPeer, lTLS_CaCertFile,
017ff1
+	lTLS_CaCertDir, lTLS_Ciphers, lTLS_Cert, lTLS_Key,
017ff1
+	lTLS_RandFile, lLogDir, lDebug, lSSH_Filter,
017ff1
+	lAccountClass, lDeprecated, lUnsupported
017ff1
+} OpCodes;
f09e2e
+
017ff1
+/* Textual representations of the tokens. */
f09e2e
+
017ff1
+static struct {
017ff1
+	const char *name;
017ff1
+	OpCodes opcode;
017ff1
+} keywords[] = {
017ff1
+	{ "URI", lURI },
017ff1
+	{ "Base", lBase },
017ff1
+	{ "BindDN", lBindDN },
017ff1
+	{ "BindPW", lBindPW },
017ff1
+	{ "RootBindDN", lRootBindDN },
017ff1
+	{ "Host", lHost },
017ff1
+	{ "Port", lPort },
017ff1
+	{ "Scope", lScope },
017ff1
+	{ "Deref", lDeref },
017ff1
+	{ "TimeLimit", lTimeLimit },
017ff1
+	{ "TimeOut", lTimeLimit },
017ff1
+	{ "Bind_Timelimit", lBind_TimeLimit },
017ff1
+	{ "Network_TimeOut", lBind_TimeLimit },
017ff1
+/*
017ff1
+ * Todo
017ff1
+ * SIZELIMIT
017ff1
+ */
017ff1
+	{ "Ldap_Version", lLdap_Version },
017ff1
+	{ "Version", lLdap_Version },
017ff1
+	{ "Bind_Policy", lBind_Policy },
017ff1
+	{ "SSLPath", lSSLPath },
017ff1
+	{ "SSL", lSSL },
017ff1
+	{ "Referrals", lReferrals },
017ff1
+	{ "Restart", lRestart },
017ff1
+	{ "TLS_CheckPeer", lTLS_CheckPeer },
017ff1
+	{ "TLS_ReqCert", lTLS_CheckPeer },
017ff1
+	{ "TLS_CaCertFile", lTLS_CaCertFile },
017ff1
+	{ "TLS_CaCert", lTLS_CaCertFile },
017ff1
+	{ "TLS_CaCertDir", lTLS_CaCertDir },
017ff1
+	{ "TLS_Ciphers", lTLS_Ciphers },
017ff1
+	{ "TLS_Cipher_Suite", lTLS_Ciphers },
017ff1
+	{ "TLS_Cert", lTLS_Cert },
017ff1
+	{ "TLS_Certificate", lTLS_Cert },
017ff1
+	{ "TLS_Key", lTLS_Key },
017ff1
+	{ "TLS_RandFile", lTLS_RandFile },
017ff1
+/*
017ff1
+ * Todo
017ff1
+ * TLS_CRLCHECK
017ff1
+ * TLS_CRLFILE
017ff1
+ */
017ff1
+	{ "LogDir", lLogDir },
017ff1
+	{ "Debug", lDebug },
017ff1
+	{ "SSH_Filter", lSSH_Filter },
017ff1
+	{ "AccountClass", lAccountClass },
017ff1
+	{ NULL, lBadOption }
017ff1
+};
f09e2e
+
017ff1
+/* Configuration ptions. */
f09e2e
+
017ff1
+Options options;
f09e2e
+
017ff1
+/*
017ff1
+ * Returns the number of the token pointed to by cp or oBadOption.
017ff1
+ */
f09e2e
+
017ff1
+static OpCodes
017ff1
+parse_token(const char *cp, const char *filename, int linenum)
017ff1
+{
017ff1
+	u_int i;
f09e2e
+
017ff1
+	for (i = 0; keywords[i].name; i++)
017ff1
+		if (strcasecmp(cp, keywords[i].name) == 0)
017ff1
+			return keywords[i].opcode;
f09e2e
+
017ff1
+	if (config_warning_config_file) 
017ff1
+	    logit("%s: line %d: Bad configuration option: %s",
017ff1
+		filename, linenum, cp);
017ff1
+	return lBadOption;
f09e2e
+}
f09e2e
+
017ff1
+/* Characters considered whitespace in strsep calls. */
017ff1
+#define WHITESPACE " \t\r\n"
f09e2e
+
017ff1
+/* return next token in configuration line */
017ff1
+static char *
017ff1
+ldap_strdelim(char **s)
f09e2e
+{
017ff1
+      char *old;
017ff1
+      int wspace = 0;
f09e2e
+
017ff1
+      if (*s == NULL)
017ff1
+              return NULL;
f09e2e
+
017ff1
+      old = *s;
f09e2e
+
017ff1
+      *s = strpbrk(*s, WHITESPACE);
017ff1
+      if (*s == NULL)
017ff1
+              return (old);
f09e2e
+
017ff1
+      *s[0] = '\0';
f09e2e
+
017ff1
+      /* Skip any extra whitespace after first token */
017ff1
+      *s += strspn(*s + 1, WHITESPACE) + 1;
017ff1
+      if (*s[0] == '=' && !wspace)
017ff1
+              *s += strspn(*s + 1, WHITESPACE) + 1;
f09e2e
+
017ff1
+      return (old);
f09e2e
+}
f09e2e
+
f09e2e
+/*
017ff1
+ * Processes a single option line as used in the configuration files. This
017ff1
+ * only sets those values that have not already been set.
f09e2e
+ */
017ff1
+#define WHITESPACE " \t\r\n"
f09e2e
+
017ff1
+static int
017ff1
+process_config_line(char *line, const char *filename, int linenum)
f09e2e
+{
017ff1
+	char *s, **charptr, **xstringptr, *endofnumber, *keyword, *arg;
017ff1
+	char *rootbinddn = NULL;
017ff1
+	int opcode, *intptr, value;
017ff1
+	size_t len;
f09e2e
+
017ff1
+	/* Strip trailing whitespace */
017ff1
+	for (len = strlen(line) - 1; len > 0; len--) {
017ff1
+		if (strchr(WHITESPACE, line[len]) == NULL)
017ff1
+			break;
017ff1
+		line[len] = '\0';
017ff1
+	}
f09e2e
+
017ff1
+	s = line;
017ff1
+	/* Get the keyword. (Each line is supposed to begin with a keyword). */
017ff1
+	if ((keyword = ldap_strdelim(&s)) == NULL)
017ff1
+		return 0;
017ff1
+	/* Ignore leading whitespace. */
017ff1
+	if (*keyword == '\0')
017ff1
+		keyword = ldap_strdelim(&s);
017ff1
+	if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
017ff1
+		return 0;
f09e2e
+
017ff1
+	opcode = parse_token(keyword, filename, linenum);
f09e2e
+
017ff1
+	switch (opcode) {
017ff1
+	case lBadOption:
017ff1
+		/* don't panic, but count bad options */
017ff1
+		return -1;
017ff1
+		/* NOTREACHED */
f09e2e
+
017ff1
+	case lHost:
017ff1
+		xstringptr = &options.host;
017ff1
+parse_xstring:
017ff1
+		if (!s || *s == '\0')
017ff1
+		    fatal("%s line %d: missing dn",filename,linenum);
017ff1
+		if (*xstringptr == NULL)
017ff1
+		    *xstringptr = xstrdup(s);
017ff1
+		return 0;
f09e2e
+
017ff1
+	case lURI:
017ff1
+		xstringptr = &options.uri;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lBase:
017ff1
+		xstringptr = &options.base;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lBindDN:
017ff1
+		xstringptr = &options.binddn;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lBindPW:
017ff1
+		charptr = &options.bindpw;
017ff1
+parse_string:
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing argument.", filename, linenum);
017ff1
+		if (*charptr == NULL)
017ff1
+			*charptr = xstrdup(arg);
017ff1
+		break;
f09e2e
+
017ff1
+	case lRootBindDN:
017ff1
+		xstringptr = &rootbinddn;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lScope:
017ff1
+		intptr = &options.scope;
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing sub/one/base argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (strcasecmp (arg, "sub") == 0 || strcasecmp (arg, "subtree") == 0)
017ff1
+			value = LDAP_SCOPE_SUBTREE;
017ff1
+		else if (strcasecmp (arg, "one") == 0)
017ff1
+			value = LDAP_SCOPE_ONELEVEL;
017ff1
+		else if (strcasecmp (arg, "base") == 0)
017ff1
+			value = LDAP_SCOPE_BASE;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad sub/one/base argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
f09e2e
+
017ff1
+	case lDeref:
017ff1
+		intptr = &options.scope;
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing never/searching/finding/always argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (!strcasecmp (arg, "never"))
017ff1
+			value = LDAP_DEREF_NEVER;
017ff1
+		else if (!strcasecmp (arg, "searching"))
017ff1
+			value = LDAP_DEREF_SEARCHING;
017ff1
+		else if (!strcasecmp (arg, "finding"))
017ff1
+			value = LDAP_DEREF_FINDING;
017ff1
+		else if (!strcasecmp (arg, "always"))
017ff1
+			value = LDAP_DEREF_ALWAYS;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad never/searching/finding/always argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
017ff1
+
017ff1
+	case lPort:
017ff1
+		intptr = &options.port;
017ff1
+parse_int:
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing argument.", filename, linenum);
017ff1
+		if (arg[0] < '0' || arg[0] > '9')
017ff1
+			fatal("%.200s line %d: Bad number.", filename, linenum);
f09e2e
+
017ff1
+		/* Octal, decimal, or hex format? */
017ff1
+		value = strtol(arg, &endofnumber, 0);
017ff1
+		if (arg == endofnumber)
017ff1
+			fatal("%.200s line %d: Bad number.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
f09e2e
+
017ff1
+	case lTimeLimit:
017ff1
+		intptr = &options.timelimit;
017ff1
+parse_time:
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%s line %d: missing time value.",
017ff1
+			    filename, linenum);
017ff1
+		if ((value = convtime(arg)) == -1)
017ff1
+			fatal("%s line %d: invalid time value.",
017ff1
+			    filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
f09e2e
+
017ff1
+	case lBind_TimeLimit:
017ff1
+		intptr = &options.bind_timelimit;
017ff1
+		goto parse_time;
f09e2e
+
017ff1
+	case lLdap_Version:
017ff1
+		intptr = &options.ldap_version;
017ff1
+		goto parse_int;
f09e2e
+
017ff1
+	case lBind_Policy:
017ff1
+		intptr = &options.bind_policy;
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing soft/hard argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (strcasecmp(arg, "hard") == 0 || strcasecmp(arg, "hard_open") == 0 || strcasecmp(arg, "hard_init") == 0)
017ff1
+			value = 1;
017ff1
+		else if (strcasecmp(arg, "soft") == 0)
017ff1
+			value = 0;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad soft/hard argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+		break;
f09e2e
+
017ff1
+	case lSSLPath:
017ff1
+		charptr = &options.sslpath;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lSSL:
017ff1
+		intptr = &options.ssl;
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing yes/no/start_tls argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0 || strcasecmp(arg, "on") == 0)
017ff1
+			value = SSL_LDAPS;
017ff1
+		else if (strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0 || strcasecmp(arg, "off") == 0)
017ff1
+			value = SSL_OFF;
017ff1
+		else if (!strcasecmp (arg, "start_tls"))
017ff1
+			value = SSL_START_TLS;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad yes/no/start_tls argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
f09e2e
+
017ff1
+	case lReferrals:
017ff1
+		intptr = &options.referrals;
017ff1
+parse_flag:
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0 || strcasecmp(arg, "on") == 0)
017ff1
+			value = 1;
017ff1
+		else if (strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0 || strcasecmp(arg, "off") == 0)
017ff1
+			value = 0;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+			*intptr = value;
017ff1
+		break;
f09e2e
+
017ff1
+	case lRestart:
017ff1
+		intptr = &options.restart;
017ff1
+		goto parse_flag;
f09e2e
+
017ff1
+	case lTLS_CheckPeer:
017ff1
+		intptr = &options.tls_checkpeer;
017ff1
+		arg = ldap_strdelim(&s);
017ff1
+		if (!arg || *arg == '\0')
017ff1
+			fatal("%.200s line %d: Missing never/hard/demand/alow/try argument.", filename, linenum);
017ff1
+		value = 0;	/* To avoid compiler warning... */
017ff1
+		if (strcasecmp(arg, "never") == 0 || strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0 || strcasecmp(arg, "off") == 0)
017ff1
+			value = LDAP_OPT_X_TLS_NEVER;
017ff1
+		else if (strcasecmp(arg, "hard") == 0 || strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0 || strcasecmp(arg, "on") == 0)
017ff1
+			value = LDAP_OPT_X_TLS_HARD;
017ff1
+		else if (strcasecmp(arg, "demand") == 0)
017ff1
+			value = LDAP_OPT_X_TLS_DEMAND;
017ff1
+		else if (strcasecmp(arg, "allow") == 0)
017ff1
+			value = LDAP_OPT_X_TLS_ALLOW;
017ff1
+		else if (strcasecmp(arg, "try") == 0)
017ff1
+			value = LDAP_OPT_X_TLS_TRY;
017ff1
+		else
017ff1
+			fatal("%.200s line %d: Bad never/hard/demand/alow/try argument.", filename, linenum);
017ff1
+		if (*intptr == -1)
017ff1
+		break;
f09e2e
+
017ff1
+	case lTLS_CaCertFile:
017ff1
+		charptr = &options.tls_cacertfile;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lTLS_CaCertDir:
017ff1
+		charptr = &options.tls_cacertdir;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lTLS_Ciphers:
017ff1
+		xstringptr = &options.tls_ciphers;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lTLS_Cert:
017ff1
+		charptr = &options.tls_cert;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lTLS_Key:
017ff1
+		charptr = &options.tls_key;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lTLS_RandFile:
017ff1
+		charptr = &options.tls_randfile;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lLogDir:
017ff1
+		charptr = &options.logdir;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lDebug:
017ff1
+		intptr = &options.debug;
017ff1
+		goto parse_int;
f09e2e
+
017ff1
+	case lSSH_Filter:
017ff1
+		xstringptr = &options.ssh_filter;
017ff1
+		goto parse_xstring;
f09e2e
+
017ff1
+	case lAccountClass:
017ff1
+		charptr = &options.account_class;
017ff1
+		goto parse_string;
f09e2e
+
017ff1
+	case lDeprecated:
017ff1
+		debug("%s line %d: Deprecated option \"%s\"",
017ff1
+		    filename, linenum, keyword);
017ff1
+		return 0;
f09e2e
+
017ff1
+	case lUnsupported:
017ff1
+		error("%s line %d: Unsupported option \"%s\"",
017ff1
+		    filename, linenum, keyword);
017ff1
+		return 0;
f09e2e
+
017ff1
+	default:
017ff1
+		fatal("process_config_line: Unimplemented opcode %d", opcode);
017ff1
+	}
f09e2e
+
017ff1
+	/* Check that there is no garbage at end of line. */
017ff1
+	if ((arg = ldap_strdelim(&s)) != NULL && *arg != '\0') {
017ff1
+		fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
017ff1
+		    filename, linenum, arg);
017ff1
+	}
017ff1
+	return 0;
017ff1
+}
f09e2e
+
017ff1
+/*
017ff1
+ * Reads the config file and modifies the options accordingly.  Options
017ff1
+ * should already be initialized before this call.  This never returns if
017ff1
+ * there is an error.  If the file does not exist, this returns 0.
017ff1
+ */
f09e2e
+
017ff1
+void
017ff1
+read_config_file(const char *filename)
017ff1
+{
017ff1
+	FILE *f;
017ff1
+	char line[1024];
017ff1
+	int active, linenum;
017ff1
+	int bad_options = 0;
017ff1
+	struct stat sb;
f09e2e
+
017ff1
+	if ((f = fopen(filename, "r")) == NULL)
017ff1
+		fatal("fopen %s: %s", filename, strerror(errno));
f09e2e
+
017ff1
+	if (fstat(fileno(f), &sb) == -1)
017ff1
+		fatal("fstat %s: %s", filename, strerror(errno));
017ff1
+	if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
017ff1
+	    (sb.st_mode & 022) != 0))
017ff1
+		fatal("Bad owner or permissions on %s", filename);
f09e2e
+
017ff1
+	debug("Reading configuration data %.200s", filename);
f09e2e
+
017ff1
+	/*
017ff1
+	 * Mark that we are now processing the options.  This flag is turned
017ff1
+	 * on/off by Host specifications.
017ff1
+	 */
017ff1
+	active = 1;
017ff1
+	linenum = 0;
017ff1
+	while (fgets(line, sizeof(line), f)) {
017ff1
+		/* Update line number counter. */
017ff1
+		linenum++;
017ff1
+		if (process_config_line(line, filename, linenum) != 0)
017ff1
+			bad_options++;
017ff1
+	}
017ff1
+	fclose(f);
017ff1
+	if ((bad_options > 0) && config_exclusive_config_file) 
017ff1
+		fatal("%s: terminating, %d bad configuration options",
017ff1
+		    filename, bad_options);
017ff1
+}
f09e2e
+
f09e2e
+/*
017ff1
+ * Initializes options to special values that indicate that they have not yet
017ff1
+ * been set.  Read_config_file will only set options with this value. Options
017ff1
+ * are processed in the following order: command line, user config file,
017ff1
+ * system config file.  Last, fill_default_options is called.
f09e2e
+ */
f09e2e
+
017ff1
+void
017ff1
+initialize_options(void)
017ff1
+{
017ff1
+	memset(&options, 'X', sizeof(options));
017ff1
+	options.host = NULL;
017ff1
+	options.uri = NULL;
017ff1
+	options.base = NULL;
017ff1
+	options.binddn = NULL;
017ff1
+	options.bindpw = NULL;
017ff1
+	options.scope = -1;
017ff1
+	options.deref = -1;
017ff1
+	options.port = -1;
017ff1
+	options.timelimit = -1;
017ff1
+	options.bind_timelimit = -1;
017ff1
+	options.ldap_version = -1;
017ff1
+	options.bind_policy = -1;
017ff1
+	options.sslpath = NULL;
017ff1
+	options.ssl = -1;
017ff1
+	options.referrals = -1;
017ff1
+	options.restart = -1;
017ff1
+	options.tls_checkpeer = -1;
017ff1
+	options.tls_cacertfile = NULL;
017ff1
+	options.tls_cacertdir = NULL;
017ff1
+	options.tls_ciphers = NULL;
017ff1
+	options.tls_cert = NULL;
017ff1
+	options.tls_key = NULL;
017ff1
+	options.tls_randfile = NULL;
017ff1
+	options.logdir = NULL;
017ff1
+	options.debug = -1;
017ff1
+	options.ssh_filter = NULL;
017ff1
+	options.account_class = NULL;
017ff1
+}
017ff1
+
017ff1
+/*
017ff1
+ * Called after processing other sources of option data, this fills those
017ff1
+ * options for which no value has been specified with their default values.
017ff1
+ */
017ff1
+
017ff1
+void
017ff1
+fill_default_options(void)
017ff1
+{
017ff1
+	if (options.uri != NULL) {
017ff1
+		LDAPURLDesc *ludp;
017ff1
+
017ff1
+		if (ldap_url_parse(options.uri, &ludp) == LDAP_SUCCESS) {
017ff1
+			if (options.ssl == -1) {
017ff1
+				if (strcmp (ludp->lud_scheme, "ldap") == 0)
017ff1
+				    options.ssl = 2;
017ff1
+				if (strcmp (ludp->lud_scheme, "ldapi") == 0)
017ff1
+				    options.ssl = 0;
017ff1
+				else if (strcmp (ludp->lud_scheme, "ldaps") == 0)
017ff1
+				    options.ssl = 1;
017ff1
+			}
017ff1
+			if (options.host == NULL)
017ff1
+			    options.host = xstrdup (ludp->lud_host);
017ff1
+			if (options.port == -1)
017ff1
+			    options.port = ludp->lud_port;
017ff1
+
017ff1
+			ldap_free_urldesc (ludp);
017ff1
+		}
017ff1
+	} 
017ff1
+	if (options.ssl == -1)
017ff1
+	    options.ssl = SSL_START_TLS;
017ff1
+	if (options.port == -1)
017ff1
+	    options.port = (options.ssl == 0) ? 389 : 636;
017ff1
+	if (options.uri == NULL) {
017ff1
+		int len;
017ff1
+#define MAXURILEN 4096
017ff1
+
017ff1
+		options.uri = xmalloc (MAXURILEN);
017ff1
+		len = snprintf (options.uri, MAXURILEN, "ldap%s://%s:%d",
017ff1
+		    (options.ssl == 0) ? "" : "s", options.host, options.port);
017ff1
+		options.uri[MAXURILEN - 1] = 0;
017ff1
+		options.uri = xrealloc (options.uri, len + 1, 1);
017ff1
+	}
017ff1
+	if (options.binddn == NULL)
017ff1
+	    options.binddn = "";
017ff1
+	if (options.bindpw == NULL)
017ff1
+	    options.bindpw = "";
017ff1
+	if (options.scope == -1)
017ff1
+	    options.scope = LDAP_SCOPE_SUBTREE;
017ff1
+	if (options.deref == -1)
017ff1
+	    options.deref = LDAP_DEREF_NEVER;
017ff1
+	if (options.timelimit == -1)
017ff1
+	    options.timelimit = 10;
017ff1
+	if (options.bind_timelimit == -1)
017ff1
+	    options.bind_timelimit = 10;
017ff1
+	if (options.ldap_version == -1)
017ff1
+	    options.ldap_version = 3;
017ff1
+	if (options.bind_policy == -1)
017ff1
+	    options.bind_policy = 1;
017ff1
+	if (options.referrals == -1)
017ff1
+	    options.referrals = 1;
017ff1
+	if (options.restart == -1)
017ff1
+	    options.restart = 1;
017ff1
+	if (options.tls_checkpeer == -1)
017ff1
+	    options.tls_checkpeer = LDAP_OPT_X_TLS_HARD;
017ff1
+	if (options.debug == -1)
017ff1
+	    options.debug = 0;
017ff1
+	if (options.ssh_filter == NULL)
017ff1
+	    options.ssh_filter = "";
017ff1
+	if (options.account_class == NULL)
017ff1
+	    options.account_class = "posixAccount";
017ff1
+}
f09e2e
+
017ff1
+static const char *
017ff1
+lookup_opcode_name(OpCodes code)
017ff1
+{
017ff1
+	u_int i;
017ff1
+
017ff1
+	for (i = 0; keywords[i].name != NULL; i++)
017ff1
+	    if (keywords[i].opcode == code)
017ff1
+		return(keywords[i].name);
017ff1
+	return "UNKNOWN";
017ff1
+}
f09e2e
+
f09e2e
+static void
017ff1
+dump_cfg_string(OpCodes code, const char *val)
f09e2e
+{
017ff1
+	if (val == NULL)
017ff1
+	    debug3("%s <UNDEFINED>", lookup_opcode_name(code));
017ff1
+	else
017ff1
+	    debug3("%s %s", lookup_opcode_name(code), val);
f09e2e
+}
f09e2e
+
017ff1
+static void
017ff1
+dump_cfg_int(OpCodes code, int val)
f09e2e
+{
017ff1
+	if (val == -1)
017ff1
+	    debug3("%s <UNDEFINED>", lookup_opcode_name(code));
017ff1
+	else
017ff1
+	    debug3("%s %d", lookup_opcode_name(code), val);
017ff1
+}
f09e2e
+
017ff1
+struct names {
017ff1
+	int value;
017ff1
+	char *name;
017ff1
+};
f09e2e
+
017ff1
+static void
017ff1
+dump_cfg_namedint(OpCodes code, int val, struct names *names)
017ff1
+{
017ff1
+	u_int i;
f09e2e
+
017ff1
+	if (val == -1)
017ff1
+	    debug3("%s <UNDEFINED>", lookup_opcode_name(code));
017ff1
+	else {
017ff1
+		for (i = 0; names[i].value != -1; i++)
017ff1
+	 	    if (names[i].value == val) {
017ff1
+	    		debug3("%s %s", lookup_opcode_name(code), names[i].name);
017ff1
+			    return;
f09e2e
+		}
017ff1
+		debug3("%s unknown: %d", lookup_opcode_name(code), val);
f09e2e
+	}
017ff1
+}
f09e2e
+
017ff1
+static struct names _yesnotls[] = {
017ff1
+	{ 0, "No" },
017ff1
+	{ 1, "Yes" },
017ff1
+	{ 2, "Start_TLS" },
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+static struct names _scope[] = {
017ff1
+	{ LDAP_SCOPE_BASE, "Base" },
017ff1
+	{ LDAP_SCOPE_ONELEVEL, "One" },
017ff1
+	{ LDAP_SCOPE_SUBTREE, "Sub"},
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+static struct names _deref[] = {
017ff1
+	{ LDAP_DEREF_NEVER, "Never" },
017ff1
+	{ LDAP_DEREF_SEARCHING, "Searching" },
017ff1
+	{ LDAP_DEREF_FINDING, "Finding" },
017ff1
+	{ LDAP_DEREF_ALWAYS, "Always" },
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+static struct names _yesno[] = {
017ff1
+	{ 0, "No" },
017ff1
+	{ 1, "Yes" },
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+static struct names _bindpolicy[] = {
017ff1
+	{ 0, "Soft" },
017ff1
+	{ 1, "Hard" },
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+static struct names _checkpeer[] = {
017ff1
+	{ LDAP_OPT_X_TLS_NEVER, "Never" },
017ff1
+	{ LDAP_OPT_X_TLS_HARD, "Hard" },
017ff1
+	{ LDAP_OPT_X_TLS_DEMAND, "Demand" },
017ff1
+	{ LDAP_OPT_X_TLS_ALLOW, "Allow" },
017ff1
+	{ LDAP_OPT_X_TLS_TRY, "TRY" },
017ff1
+	{ -1, NULL }};
f09e2e
+
017ff1
+void
017ff1
+dump_config(void)
017ff1
+{
017ff1
+	dump_cfg_string(lURI, options.uri);
017ff1
+	dump_cfg_string(lHost, options.host);
017ff1
+	dump_cfg_int(lPort, options.port);
017ff1
+	dump_cfg_namedint(lSSL, options.ssl, _yesnotls);
017ff1
+	dump_cfg_int(lLdap_Version, options.ldap_version);
017ff1
+	dump_cfg_int(lTimeLimit, options.timelimit);
017ff1
+	dump_cfg_int(lBind_TimeLimit, options.bind_timelimit);
017ff1
+	dump_cfg_string(lBase, options.base);
017ff1
+	dump_cfg_string(lBindDN, options.binddn);
017ff1
+	dump_cfg_string(lBindPW, options.bindpw);
017ff1
+	dump_cfg_namedint(lScope, options.scope, _scope);
017ff1
+	dump_cfg_namedint(lDeref, options.deref, _deref);
017ff1
+	dump_cfg_namedint(lReferrals, options.referrals, _yesno);
017ff1
+	dump_cfg_namedint(lRestart, options.restart, _yesno);
017ff1
+	dump_cfg_namedint(lBind_Policy, options.bind_policy, _bindpolicy);
017ff1
+	dump_cfg_string(lSSLPath, options.sslpath);
017ff1
+	dump_cfg_namedint(lTLS_CheckPeer, options.tls_checkpeer, _checkpeer);
017ff1
+	dump_cfg_string(lTLS_CaCertFile, options.tls_cacertfile);
017ff1
+	dump_cfg_string(lTLS_CaCertDir, options.tls_cacertdir);
017ff1
+	dump_cfg_string(lTLS_Ciphers, options.tls_ciphers);
017ff1
+	dump_cfg_string(lTLS_Cert, options.tls_cert);
017ff1
+	dump_cfg_string(lTLS_Key, options.tls_key);
017ff1
+	dump_cfg_string(lTLS_RandFile, options.tls_randfile);
017ff1
+	dump_cfg_string(lLogDir, options.logdir);
017ff1
+	dump_cfg_int(lDebug, options.debug);
017ff1
+	dump_cfg_string(lSSH_Filter, options.ssh_filter);
017ff1
+	dump_cfg_string(lAccountClass, options.logdir);
f09e2e
+}
f09e2e
+
017ff1
diff --git a/ldapconf.h b/ldapconf.h
017ff1
new file mode 100644
017ff1
index 0000000..2cb550c
017ff1
--- /dev/null
017ff1
+++ b/ldapconf.h
017ff1
@@ -0,0 +1,72 @@
017ff1
+/* $OpenBSD: ldapconf.c,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
f09e2e
+/*
f09e2e
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
017ff1
+#ifndef LDAPCONF_H
017ff1
+#define LDAPCONF_H
f09e2e
+
017ff1
+#define SSL_OFF          0
017ff1
+#define SSL_LDAPS        1
017ff1
+#define SSL_START_TLS    2
017ff1
+
017ff1
+/* Data structure for representing option data. */
017ff1
+
017ff1
+typedef struct {
017ff1
+	char *host;
017ff1
+	char *uri;
017ff1
+	char *base;
017ff1
+	char *binddn;
017ff1
+	char *bindpw;
017ff1
+	int scope;
017ff1
+	int deref;
017ff1
+	int port;
017ff1
+	int timelimit;
017ff1
+	int bind_timelimit;
017ff1
+	int ldap_version;
017ff1
+	int bind_policy;
017ff1
+	char *sslpath;
017ff1
+	int ssl;
017ff1
+	int referrals;
017ff1
+	int restart;
017ff1
+	int tls_checkpeer;
017ff1
+	char *tls_cacertfile;
017ff1
+	char *tls_cacertdir;
017ff1
+	char *tls_ciphers;
017ff1
+	char *tls_cert;
017ff1
+	char *tls_key;
017ff1
+	char *tls_randfile;
017ff1
+	char *logdir;
017ff1
+	int debug;
017ff1
+	char *ssh_filter;
017ff1
+	char *account_class;
017ff1
+}       Options;
017ff1
+
017ff1
+extern Options options;
017ff1
+
017ff1
+void read_config_file(const char *);
017ff1
+void initialize_options(void);
017ff1
+void fill_default_options(void);
017ff1
+void dump_config(void);
f09e2e
+
017ff1
+#endif /* LDAPCONF_H */
017ff1
diff --git a/ldapincludes.h b/ldapincludes.h
017ff1
new file mode 100644
017ff1
index 0000000..8539bdc
017ff1
--- /dev/null
017ff1
+++ b/ldapincludes.h
f09e2e
@@ -0,0 +1,41 @@
f09e2e
+/* $OpenBSD: ldapconf.c,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
f09e2e
+/*
f09e2e
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+#ifndef LDAPINCLUDES_H
f09e2e
+#define LDAPINCLUDES_H
f09e2e
+
f09e2e
+#include "includes.h"
f09e2e
+
f09e2e
+#ifdef HAVE_LBER_H
f09e2e
+#include <lber.h>
f09e2e
+#endif
f09e2e
+#ifdef HAVE_LDAP_H
f09e2e
+#include <ldap.h>
f09e2e
+#endif
f09e2e
+#ifdef HAVE_LDAP_SSL_H
f09e2e
+#include <ldap_ssl.h>
f09e2e
+#endif
f09e2e
+
f09e2e
+#endif /* LDAPINCLUDES_H */
017ff1
diff --git a/ldapmisc.c b/ldapmisc.c
017ff1
new file mode 100644
017ff1
index 0000000..de23c0c
017ff1
--- /dev/null
017ff1
+++ b/ldapmisc.c
f09e2e
@@ -0,0 +1,79 @@
f09e2e
+
f09e2e
+#include "ldapincludes.h"
f09e2e
+#include "ldapmisc.h"
f09e2e
+
f09e2e
+#ifndef HAVE_LDAP_GET_LDERRNO
f09e2e
+int
f09e2e
+ldap_get_lderrno (LDAP * ld, char **m, char **s)
f09e2e
+{
f09e2e
+#ifdef HAVE_LDAP_GET_OPTION
f09e2e
+	int rc;
f09e2e
+#endif
f09e2e
+	int lderrno;
f09e2e
+
f09e2e
+#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
f09e2e
+	if ((rc = ldap_get_option (ld, LDAP_OPT_ERROR_NUMBER, &lderrno)) != LDAP_SUCCESS)
f09e2e
+	    return rc;
f09e2e
+#else
f09e2e
+	lderrno = ld->ld_errno;
f09e2e
+#endif
f09e2e
+
f09e2e
+	if (s != NULL) {
f09e2e
+#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_STRING)
f09e2e
+		if ((rc = ldap_get_option (ld, LDAP_OPT_ERROR_STRING, s)) != LDAP_SUCCESS)
f09e2e
+		    return rc;
f09e2e
+#else
f09e2e
+		*s = ld->ld_error;
f09e2e
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	if (m != NULL) {
f09e2e
+#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_MATCHED_DN)
f09e2e
+		if ((rc = ldap_get_option (ld, LDAP_OPT_MATCHED_DN, m)) != LDAP_SUCCESS)
f09e2e
+		    return rc;
f09e2e
+#else
f09e2e
+		*m = ld->ld_matched;
f09e2e
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	return lderrno;
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
+#ifndef HAVE_LDAP_SET_LDERRNO
f09e2e
+int
f09e2e
+ldap_set_lderrno (LDAP * ld, int lderrno, const char *m, const char *s)
f09e2e
+{
f09e2e
+#ifdef HAVE_LDAP_SET_OPTION
f09e2e
+	int rc;
f09e2e
+#endif
f09e2e
+
f09e2e
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
f09e2e
+	if ((rc = ldap_set_option (ld, LDAP_OPT_ERROR_NUMBER, &lderrno)) != LDAP_SUCCESS)
f09e2e
+	    return rc;
f09e2e
+#else
f09e2e
+	ld->ld_errno = lderrno;
f09e2e
+#endif
f09e2e
+
f09e2e
+	if (s != NULL) {
f09e2e
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_ERROR_STRING)
f09e2e
+		if ((rc = ldap_set_option (ld, LDAP_OPT_ERROR_STRING, s)) != LDAP_SUCCESS)
f09e2e
+		    return rc;
f09e2e
+#else
f09e2e
+		ld->ld_error = s;
f09e2e
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	if (m != NULL) {
f09e2e
+#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_MATCHED_DN)
f09e2e
+		if ((rc = ldap_set_option (ld, LDAP_OPT_MATCHED_DN, m)) != LDAP_SUCCESS)
f09e2e
+		    return rc;
f09e2e
+#else
f09e2e
+		ld->ld_matched = m;
f09e2e
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	return LDAP_SUCCESS;
f09e2e
+}
f09e2e
+#endif
f09e2e
+
017ff1
diff --git a/ldapmisc.h b/ldapmisc.h
017ff1
new file mode 100644
017ff1
index 0000000..4c271df
017ff1
--- /dev/null
017ff1
+++ b/ldapmisc.h
f09e2e
@@ -0,0 +1,35 @@
f09e2e
+/* $OpenBSD: ldapbody.h,v 1.1 2009/12/03 03:34:42 jfch Exp $ */
f09e2e
+/*
f09e2e
+ * Copyright (c) 2009 Jan F. Chadima.  All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+#ifndef LDAPMISC_H
f09e2e
+#define LDAPMISC_H
f09e2e
+
f09e2e
+#include "ldapincludes.h"
f09e2e
+
f09e2e
+int ldap_get_lderrno (LDAP *, char **, char **);
f09e2e
+int ldap_set_lderrno (LDAP *, int, const char *, const char *);
f09e2e
+
f09e2e
+#endif /* LDAPMISC_H */
f09e2e
+
017ff1
diff --git a/openssh-lpk-openldap.schema b/openssh-lpk-openldap.schema
017ff1
new file mode 100644
017ff1
index 0000000..c84f90f
017ff1
--- /dev/null
017ff1
+++ b/openssh-lpk-openldap.schema
f09e2e
@@ -0,0 +1,21 @@
f09e2e
+#
f09e2e
+# LDAP Public Key Patch schema for use with openssh-ldappubkey
f09e2e
+#                              useful with PKA-LDAP also
f09e2e
+#
f09e2e
+# Author: Eric AUGE <eau@phear.org>
f09e2e
+# 
f09e2e
+# Based on the proposal of : Mark Ruijter
f09e2e
+#
f09e2e
+
f09e2e
+
f09e2e
+# octetString SYNTAX
f09e2e
+attributetype ( 1.3.6.1.4.1.24552.500.1.1.1.13 NAME 'sshPublicKey' 
f09e2e
+	DESC 'MANDATORY: OpenSSH Public key' 
f09e2e
+	EQUALITY octetStringMatch
f09e2e
+	SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )
f09e2e
+
f09e2e
+# printableString SYNTAX yes|no
f09e2e
+objectclass ( 1.3.6.1.4.1.24552.500.1.1.2.0 NAME 'ldapPublicKey' SUP top AUXILIARY
f09e2e
+	DESC 'MANDATORY: OpenSSH LPK objectclass'
f09e2e
+	MUST ( sshPublicKey $ uid ) 
f09e2e
+	)
017ff1
diff --git a/openssh-lpk-sun.schema b/openssh-lpk-sun.schema
017ff1
new file mode 100644
017ff1
index 0000000..3136673
017ff1
--- /dev/null
017ff1
+++ b/openssh-lpk-sun.schema
f09e2e
@@ -0,0 +1,23 @@
f09e2e
+#
f09e2e
+# LDAP Public Key Patch schema for use with openssh-ldappubkey
f09e2e
+#                              useful with PKA-LDAP also
f09e2e
+#
f09e2e
+# Author: Eric AUGE <eau@phear.org>
f09e2e
+# 
f09e2e
+# Schema for Sun Directory Server.
f09e2e
+# Based on the original schema, modified by Stefan Fischer.
f09e2e
+#
f09e2e
+
f09e2e
+dn: cn=schema
f09e2e
+
f09e2e
+# octetString SYNTAX
f09e2e
+attributeTypes: ( 1.3.6.1.4.1.24552.500.1.1.1.13 NAME 'sshPublicKey' 
f09e2e
+	DESC 'MANDATORY: OpenSSH Public key' 
f09e2e
+	EQUALITY octetStringMatch
f09e2e
+	SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )
f09e2e
+
f09e2e
+# printableString SYNTAX yes|no
f09e2e
+objectClasses: ( 1.3.6.1.4.1.24552.500.1.1.2.0 NAME 'ldapPublicKey' SUP top AUXILIARY
f09e2e
+	DESC 'MANDATORY: OpenSSH LPK objectclass'
f09e2e
+	MUST ( sshPublicKey $ uid ) 
f09e2e
+	)
017ff1
diff --git a/ssh-ldap-helper.8 b/ssh-ldap-helper.8
017ff1
new file mode 100644
017ff1
index 0000000..5d2d7be
017ff1
--- /dev/null
017ff1
+++ b/ssh-ldap-helper.8
017ff1
@@ -0,0 +1,79 @@
017ff1
+.\" $OpenBSD: ssh-ldap-helper.8,v 1.1 2010/02/10 23:20:38 markus Exp $
017ff1
+.\"
017ff1
+.\" Copyright (c) 2010 Jan F. Chadima.  All rights reserved.
017ff1
+.\"
017ff1
+.\" Permission to use, copy, modify, and distribute this software for any
017ff1
+.\" purpose with or without fee is hereby granted, provided that the above
017ff1
+.\" copyright notice and this permission notice appear in all copies.
017ff1
+.\"
017ff1
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
017ff1
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
017ff1
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
017ff1
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
017ff1
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
017ff1
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
017ff1
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017ff1
+.\"
017ff1
+.Dd $Mdocdate: April 29 2010 $
017ff1
+.Dt SSH-LDAP-HELPER 8
017ff1
+.Os
017ff1
+.Sh NAME
017ff1
+.Nm ssh-ldap-helper
017ff1
+.Nd sshd helper program for ldap support
017ff1
+.Sh SYNOPSIS
017ff1
+.Nm ssh-ldap-helper
017ff1
+.Op Fl devw
017ff1
+.Op Fl f Ar file
017ff1
+.Op Fl s Ar user
017ff1
+.Sh DESCRIPTION
017ff1
+.Nm
017ff1
+is used by
017ff1
+.Xr sshd 1
017ff1
+to access keys provided by an LDAP.
017ff1
+.Nm
017ff1
+is disabled by default and can only be enabled in the
017ff1
+sshd configuration file
017ff1
+.Pa /etc/ssh/sshd_config
017ff1
+by setting
017ff1
+.Cm AuthorizedKeysCommand
017ff1
+to
017ff1
+.Dq /usr/libexec/ssh-ldap-wrapper .
017ff1
+.Pp
017ff1
+.Nm
017ff1
+is not intended to be invoked by the user, but from
017ff1
+.Xr sshd 8 via
017ff1
+.Xr ssh-ldap-wrapper .
017ff1
+.Pp
017ff1
+The options are as follows:
017ff1
+.Bl -tag -width Ds
017ff1
+.It Fl d
017ff1
+Set the debug mode; 
017ff1
+.Nm
017ff1
+prints all logs to stderr instead of syslog.
017ff1
+.It Fl e
017ff1
+Implies \-w;
017ff1
+.Nm
017ff1
+halts if it encounters an unknown item in the ldap.conf file.
017ff1
+.It Fl f
017ff1
+.Nm
017ff1
+uses this file as the ldap configuration file instead of /etc/ssh/ldap.conf (default).
017ff1
+.It Fl s
017ff1
+.Nm
017ff1
+prints out the user's keys to stdout and exits.
017ff1
+.It Fl v
017ff1
+Implies \-d;
017ff1
+increases verbosity.
017ff1
+.It Fl w
017ff1
+.Nm
017ff1
+writes warnings about unknown items in the ldap.conf configuration file.
017ff1
+.El
017ff1
+.Sh SEE ALSO
017ff1
+.Xr sshd 8 ,
017ff1
+.Xr sshd_config 5 ,
017ff1
+.Xr ssh-ldap.conf 5 ,
017ff1
+.Sh HISTORY
017ff1
+.Nm
017ff1
+first appeared in
017ff1
+OpenSSH 5.5 + PKA-LDAP .
017ff1
+.Sh AUTHORS
017ff1
+.An Jan F. Chadima Aq jchadima@redhat.com
017ff1
diff --git a/ssh-ldap-wrapper b/ssh-ldap-wrapper
017ff1
new file mode 100644
017ff1
index 0000000..cb500aa
017ff1
--- /dev/null
017ff1
+++ b/ssh-ldap-wrapper
017ff1
@@ -0,0 +1,4 @@
017ff1
+#!/bin/sh
017ff1
+
017ff1
+exec /usr/libexec/openssh/ssh-ldap-helper -s "$1"
017ff1
+
017ff1
diff --git a/ssh-ldap.conf.5 b/ssh-ldap.conf.5
017ff1
new file mode 100644
017ff1
index 0000000..f7081b8
017ff1
--- /dev/null
017ff1
+++ b/ssh-ldap.conf.5
f09e2e
@@ -0,0 +1,379 @@
f09e2e
+.\" $OpenBSD: ssh-ldap.conf.5,v 1.1 2010/02/10 23:20:38 markus Exp $
f09e2e
+.\"
f09e2e
+.\" Copyright (c) 2010 Jan F. Chadima.  All rights reserved.
f09e2e
+.\"
f09e2e
+.\" Permission to use, copy, modify, and distribute this software for any
f09e2e
+.\" purpose with or without fee is hereby granted, provided that the above
f09e2e
+.\" copyright notice and this permission notice appear in all copies.
f09e2e
+.\"
f09e2e
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f09e2e
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f09e2e
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f09e2e
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f09e2e
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f09e2e
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f09e2e
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f09e2e
+.\"
f09e2e
+.Dd $Mdocdate: may 12 2010 $
f09e2e
+.Dt SSH-LDAP.CONF 5
f09e2e
+.Os
f09e2e
+.Sh NAME
f09e2e
+.Nm ssh-ldap.conf
f09e2e
+.Nd configuration file for ssh-ldap-helper
f09e2e
+.Sh SYNOPSIS
f09e2e
+.Nm /etc/ssh/ldap.conf
f09e2e
+.Sh DESCRIPTION
f09e2e
+.Xr ssh-ldap-helper 8
f09e2e
+reads configuration data from
f09e2e
+.Pa /etc/ssh/ldap.conf
f09e2e
+(or the file specified with
f09e2e
+.Fl f
f09e2e
+on the command line).
f09e2e
+The file contains keyword-argument pairs, one per line.
f09e2e
+Lines starting with
f09e2e
+.Ql #
f09e2e
+and empty lines are interpreted as comments.
f09e2e
+.Pp
f09e2e
+The value starts with the first non-blank character after 
f09e2e
+the keyword's name, and terminates at the end of the line, 
f09e2e
+or at the last sequence of blanks before the end of the line.
f09e2e
+Quoting values that contain blanks 
f09e2e
+may be incorrect, as the quotes would become part of the value.
f09e2e
+The possible keywords and their meanings are as follows (note that
f09e2e
+keywords are case-insensitive, and arguments, on a case by case basis, may be case-sensitive).
f09e2e
+.Bl -tag -width Ds
f09e2e
+.It Cm URI
f09e2e
+The argument(s) are in the form
f09e2e
+.Pa ldap[si]://[name[:port]]
f09e2e
+and specify the URI(s) of an LDAP server(s) to which the
f09e2e
+.Xr ssh-ldap-helper 8 
f09e2e
+should connect. The URI scheme may be any of
f09e2e
+.Dq ldap ,
f09e2e
+.Dq ldaps 
f09e2e
+or
f09e2e
+.Dq ldapi ,
f09e2e
+which refer to LDAP over TCP, LDAP over SSL (TLS) and LDAP
f09e2e
+over IPC (UNIX domain sockets), respectively.
f09e2e
+Each server's name can be specified as a
f09e2e
+domain-style name or an IP address literal.  Optionally, the
f09e2e
+server's name can followed by a ':' and the port number the LDAP
f09e2e
+server is listening on.  If no port number is provided, the default
f09e2e
+port for the scheme is used (389 for ldap://, 636 for ldaps://).
f09e2e
+For LDAP over IPC, name is the name of the socket, and no port
f09e2e
+is required, nor allowed; note that directory separators must be 
f09e2e
+URL-encoded, like any other characters that are special to URLs; 
f09e2e
+A space separated list of URIs may be provided.
f09e2e
+There is no default.
f09e2e
+.It Cm Base
f09e2e
+Specifies the default base Distinguished Name (DN) to use when performing ldap operations.
f09e2e
+The base must be specified as a DN in LDAP format.
f09e2e
+There is no default.
f09e2e
+.It Cm BindDN
f09e2e
+Specifies the default BIND DN to use when connecting to the ldap server.
f09e2e
+The bind DN must be specified as a Distinguished Name in LDAP format.
f09e2e
+There is no default.
f09e2e
+.It Cm BindPW
f09e2e
+Specifies the default password to use when connecting to the ldap server via
f09e2e
+.Cm BindDN .
f09e2e
+There is no default.
f09e2e
+.It Cm RootBindDN
f09e2e
+Intentionaly does nothing. Recognized for compatibility reasons.
f09e2e
+.It Cm Host
f09e2e
+The argument(s) specifies the name(s) of an LDAP server(s) to which the
f09e2e
+.Xr ssh-ldap-helper 8
f09e2e
+should connect.  Each server's name can be specified as a
f09e2e
+domain-style name or an IP address and optionally followed by a ':' and
f09e2e
+the port number the ldap server is listening on.  A space-separated
f09e2e
+list of hosts may be provided.
f09e2e
+There is no default.
f09e2e
+.Cm Host
f09e2e
+is deprecated in favor of
f09e2e
+.Cm URI .
f09e2e
+.It Cm Port
f09e2e
+Specifies the default port used when connecting to LDAP servers(s).
f09e2e
+The port may be specified as a number.
f09e2e
+The default port is 389 for ldap:// or 636 for ldaps:// respectively.
f09e2e
+.Cm Port
f09e2e
+is deprecated in favor of
f09e2e
+.Cm URI .
f09e2e
+.It Cm Scope
f09e2e
+Specifies the starting point of an LDAP search and the depth from the base DN to which the search should descend.
f09e2e
+There are three options (values) that can be assigned to the
f09e2e
+.Cm Scope parameter:
f09e2e
+.Dq base ,
f09e2e
+.Dq one
f09e2e
+and
f09e2e
+.Dq subtree .
f09e2e
+Alias for the subtree is
f09e2e
+.Dq sub .
f09e2e
+The value
f09e2e
+.Dq base
f09e2e
+is used to indicate searching only the entry at the base DN, resulting in only that entry being returned (keeping in mind that it also has to meet the search filter criteria!).
f09e2e
+The value
f09e2e
+.Dq one
f09e2e
+is used to indicate searching all entries one level under the base DN, but not including the base DN and not including any entries under that one level under the base DN.
f09e2e
+The value
f09e2e
+.Dq subtree
f09e2e
+is used to indicate searching of all entries at all levels under and including the specified base DN.
f09e2e
+The default is
f09e2e
+.Dq subtree .
f09e2e
+.It Cm Deref
f09e2e
+Specifies how alias dereferencing is done when performing a search. There are four
f09e2e
+possible values that can be assigned to the
f09e2e
+.Cm Deref
f09e2e
+parameter:
f09e2e
+.Dq never ,
f09e2e
+.Dq searching ,
f09e2e
+.Dq finding ,
f09e2e
+and
f09e2e
+.Dq always .
f09e2e
+The value
f09e2e
+.Dq never
f09e2e
+means that the aliases are never dereferenced.
f09e2e
+The value
f09e2e
+.Dq searching
f09e2e
+means that the aliases are dereferenced in subordinates of the base object, but
f09e2e
+not in locating the base object of the search.
f09e2e
+The value
f09e2e
+.Dq finding
f09e2e
+means that the aliases are only dereferenced when locating the base object of the search.
f09e2e
+The value
f09e2e
+.Dq always
f09e2e
+means that the aliases are dereferenced both in searching and in locating the base object
f09e2e
+of the search.
f09e2e
+The default is
f09e2e
+.Dq never .
f09e2e
+.It Cm TimeLimit
f09e2e
+Specifies a time limit (in seconds) to use when performing searches.
f09e2e
+The number should be a non-negative integer. A
f09e2e
+.Cm TimeLimit
f09e2e
+of zero (0) specifies that the search time is unlimited. Please note that the server
f09e2e
+may still apply any server-side limit on the duration of a search operation.
f09e2e
+The default value is 10.
f09e2e
+.It Cm TimeOut
f09e2e
+Is an aliast to
f09e2e
+.Cm TimeLimit .
f09e2e
+.It Cm Bind_TimeLimit
f09e2e
+Specifies the timeout (in seconds) after which the poll(2)/select(2)
f09e2e
+following a connect(2) returns in case of no activity.
f09e2e
+The default value is 10.
f09e2e
+.It Cm Network_TimeOut
f09e2e
+Is an alias to
f09e2e
+.Cm Bind_TimeLimit .
f09e2e
+.It Cm Ldap_Version
f09e2e
+Specifies what version of the LDAP protocol should be used.
f09e2e
+The allowed values are 2 or 3. The default is 3.
f09e2e
+.It Cm Version
f09e2e
+Is an alias to
f09e2e
+.Cm Ldap_Version .
f09e2e
+.It Cm Bind_Policy
f09e2e
+Specifies the policy to use for reconnecting to an unavailable LDAP server. There are 2 available values:
f09e2e
+.Dq hard
f09e2e
+and
f09e2e
+.Dq soft.
f09e2e
+.Dq hard has 2 aliases
f09e2e
+.Dq hard_open
f09e2e
+and
f09e2e
+.Dq hard_init .
f09e2e
+The value
f09e2e
+.Dq hard
f09e2e
+means that reconects that the
f09e2e
+.Xr ssh-ldap-helper 8
f09e2e
+tries to reconnect to the LDAP server 5 times before failure. There is exponential backoff before retrying.
f09e2e
+The value
f09e2e
+.Dq soft
f09e2e
+means that
f09e2e
+.Xr ssh-ldap-helper 8
f09e2e
+fails immediately when it cannot connect to the LDAP seerver.
f09e2e
+The deault is
f09e2e
+.Dq hard .
f09e2e
+.It Cm SSLPath
f09e2e
+Specifies the path to the X.509 certificate database.
f09e2e
+There is no default.
f09e2e
+.It Cm SSL
f09e2e
+Specifies whether to use SSL/TLS or not.
f09e2e
+There are three allowed values:
f09e2e
+.Dq yes ,
f09e2e
+.Dq no
f09e2e
+and
f09e2e
+.Dq start_tls
f09e2e
+Both
f09e2e
+.Dq true
f09e2e
+and
f09e2e
+.Dq on
f09e2e
+are the aliases for
f09e2e
+.Dq yes .
f09e2e
+.Dq false
f09e2e
+and
f09e2e
+.Dq off
f09e2e
+are the aliases for
f09e2e
+.Dq no .
f09e2e
+If
f09e2e
+.Dq start_tls
f09e2e
+is specified then StartTLS is used rather than raw LDAP over SSL.
f09e2e
+The default for ldap:// is
f09e2e
+.Dq start_tls ,
f09e2e
+for ldaps://
f09e2e
+.Dq yes
f09e2e
+and
f09e2e
+.Dq no
f09e2e
+for the ldapi:// .
f09e2e
+In case of host based configuration the default is
f09e2e
+.Dq start_tls .
f09e2e
+.It Cm Referrals
f09e2e
+Specifies if the client should automatically follow referrals returned
f09e2e
+by LDAP servers.
f09e2e
+The value can be or
f09e2e
+.Dq yes
f09e2e
+or
f09e2e
+.Dq no .
f09e2e
+.Dq true
f09e2e
+and
f09e2e
+.Dq on
f09e2e
+are the aliases for
f09e2e
+.Dq yes .
f09e2e
+.Dq false
f09e2e
+and
f09e2e
+.Dq off
f09e2e
+are the aliases for
f09e2e
+.Dq no .
f09e2e
+The default is yes.
f09e2e
+.It Cm Restart
f09e2e
+Specifies whether the LDAP client library should restart the select(2) system call when interrupted.
f09e2e
+The value can be or
f09e2e
+.Dq yes
f09e2e
+or
f09e2e
+.Dq no .
f09e2e
+.Dq true
f09e2e
+and
f09e2e
+.Dq on
f09e2e
+are the aliases for
f09e2e
+.Dq yes .
f09e2e
+.Dq false
f09e2e
+and
f09e2e
+.Dq off
f09e2e
+are the aliases for
f09e2e
+.Dq no .
f09e2e
+The default is yes.
f09e2e
+.It Cm TLS_CheckPeer
f09e2e
+Specifies what checks to perform on server certificates in a TLS session,
f09e2e
+if any. The value
f09e2e
+can be specified as one of the following keywords:
f09e2e
+.Dq never ,
f09e2e
+.Dq hard ,
f09e2e
+.Dq demand ,
f09e2e
+.Dq allow
f09e2e
+and
f09e2e
+.Dq try .
f09e2e
+.Dq true ,
f09e2e
+.Dq on
f09e2e
+and
f09e2e
+.Dq yes
f09e2e
+are aliases for
f09e2e
+.Dq hard .
f09e2e
+.Dq false ,
f09e2e
+.Dq off
f09e2e
+and
f09e2e
+.Dq no
f09e2e
+are the aliases for
f09e2e
+.Dq never .
f09e2e
+The value
f09e2e
+.Dq never
f09e2e
+means that the client will not request or check any server certificate.
f09e2e
+The value
f09e2e
+.Dq allow
f09e2e
+means that the server certificate is requested. If no certificate is provided,
f09e2e
+the session proceeds normally. If a bad certificate is provided, it will
f09e2e
+be ignored and the session proceeds normally.
f09e2e
+The value
f09e2e
+.Dq try
f09e2e
+means that the server certificate is requested. If no certificate is provided,
f09e2e
+the session proceeds normally. If a bad certificate is provided,
f09e2e
+the session is immediately terminated.
f09e2e
+The value
f09e2e
+.Dq demand
f09e2e
+means that the server certificate is requested. If no
f09e2e
+certificate is provided, or a bad certificate is provided, the session
f09e2e
+is immediately terminated.
f09e2e
+The value
f09e2e
+.Dq hard
f09e2e
+is the same as
f09e2e
+.Dq demand .
f09e2e
+It requires an SSL connection. In the case of the plain conection the
f09e2e
+session is immediately terminated.
f09e2e
+The default is
f09e2e
+.Dq hard .
f09e2e
+.It Cm TLS_ReqCert
f09e2e
+Is an alias for 
f09e2e
+.Cm TLS_CheckPeer .
f09e2e
+.It Cm TLS_CACertFile
f09e2e
+Specifies the file that contains certificates for all of the Certificate
f09e2e
+Authorities the client will recognize.
f09e2e
+There is no default.
f09e2e
+.It Cm TLS_CACert
f09e2e
+Is an alias for
f09e2e
+.Cm TLS_CACertFile .
f09e2e
+.It Cm TLS_CACertDIR
f09e2e
+Specifies the path of a directory that contains Certificate Authority
f09e2e
+certificates in separate individual files. The
f09e2e
+.Cm TLS_CACert
f09e2e
+is always used before
f09e2e
+.Cm TLS_CACertDir .
f09e2e
+The specified directory must be managed with the OpenSSL c_rehash utility.
f09e2e
+There is no default.
f09e2e
+.It Cm TLS_Ciphers
f09e2e
+Specifies acceptable cipher suite and preference order.
f09e2e
+The value should be a cipher specification for OpenSSL,
f09e2e
+e.g.,
f09e2e
+.Dq HIGH:MEDIUM:+SSLv2 .
f09e2e
+The default is
f09e2e
+.Dq ALL .
f09e2e
+.It Cm TLS_Cipher_Suite
f09e2e
+Is an alias for
f09e2e
+.Cm TLS_Ciphers .
f09e2e
+.It Cm TLS_Cert
f09e2e
+Specifies the file that contains the client certificate.
f09e2e
+There is no default.
f09e2e
+.It Cm TLS_Certificate
f09e2e
+Is an alias for
f09e2e
+.Cm TLS_Cert .
f09e2e
+.It Cm TLS_Key
f09e2e
+Specifies the file that contains the private key that matches the certificate
f09e2e
+stored in the
f09e2e
+.Cm TLS_Cert
f09e2e
+file. Currently, the private key must not be protected with a password, so
f09e2e
+it is of critical importance that the key file is protected carefully.
f09e2e
+There is no default.
f09e2e
+.It Cm TLS_RandFile
f09e2e
+Specifies the file to obtain random bits from when /dev/[u]random is
f09e2e
+not available. Generally set to the name of the EGD/PRNGD socket.
f09e2e
+The environment variable RANDFILE can also be used to specify the filename.
f09e2e
+There is no default.
f09e2e
+.It Cm LogDir
f09e2e
+Specifies the directory used for logging by the LDAP client library.
f09e2e
+There is no default.
f09e2e
+.It Cm Debug
f09e2e
+Specifies the debug level used for logging by the LDAP client library.
f09e2e
+There is no default.
f09e2e
+.It Cm SSH_Filter
f09e2e
+Specifies the user filter applied on the LDAP serch.
f09e2e
+The default is no filter.
f09e2e
+.It Cm AccountClass
f09e2e
+Specifies the LDAP class used to find user accounts.
f09e2e
+The default is posixAccount.
f09e2e
+.El
f09e2e
+.Sh FILES
f09e2e
+.Bl -tag -width Ds
f09e2e
+.It Pa  /etc/ssh/ldap.conf
f09e2e
+Ldap configuration file for
f09e2e
+.Xr ssh-ldap-helper 8 .
f09e2e
+.El
f09e2e
+.Sh "SEE ALSO"
f09e2e
+.Xr ldap.conf 5 ,
f09e2e
+.Xr ssh-ldap-helper 8
f09e2e
+.Sh HISTORY
f09e2e
+.Nm
f09e2e
+first appeared in
f09e2e
+OpenSSH 5.5 + PKA-LDAP .
f09e2e
+.Sh AUTHORS
f09e2e
+.An Jan F. Chadima Aq jchadima@redhat.com