xref: /onnv-gate/usr/src/lib/libldap4/common/bind.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
2*3857Sstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3*3857Sstevel  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
90Sstevel@tonic-gate  *  All rights reserved.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  *  bind.c
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #ifndef lint
150Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
160Sstevel@tonic-gate #endif
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include <stdio.h>
190Sstevel@tonic-gate #include <string.h>
200Sstevel@tonic-gate #ifdef MACOS
210Sstevel@tonic-gate #include <stdlib.h>
220Sstevel@tonic-gate #include "macos.h"
230Sstevel@tonic-gate #else /* MACOS */
240Sstevel@tonic-gate #ifdef DOS
250Sstevel@tonic-gate #include "msdos.h"
260Sstevel@tonic-gate #ifdef NCSA
270Sstevel@tonic-gate #include "externs.h"
280Sstevel@tonic-gate #endif /* NCSA */
290Sstevel@tonic-gate #else /* DOS */
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <sys/time.h>
330Sstevel@tonic-gate #endif /* DOS */
340Sstevel@tonic-gate #endif /* MACOS */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include "lber.h"
370Sstevel@tonic-gate #include "ldap.h"
380Sstevel@tonic-gate #include "ldap-private.h"
390Sstevel@tonic-gate #include "ldap-int.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
440Sstevel@tonic-gate  * of the entry to which to bind are supplied, along with the authentication
450Sstevel@tonic-gate  * method to use.  The msgid of the bind request is returned on success,
460Sstevel@tonic-gate  * -1 if there's trouble.  Note, the kerberos support assumes the user already
470Sstevel@tonic-gate  * has a valid tgt for now.  ldap_result() should be called to find out the
480Sstevel@tonic-gate  * outcome of the bind request.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Example:
510Sstevel@tonic-gate  *	ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
520Sstevel@tonic-gate  *	    LDAP_AUTH_SIMPLE )
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate int
ldap_bind(LDAP * ld,char * dn,char * passwd,int authmethod)560Sstevel@tonic-gate ldap_bind( LDAP *ld, char *dn, char *passwd, int authmethod )
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	/*
590Sstevel@tonic-gate 	 * The bind request looks like this:
600Sstevel@tonic-gate 	 *	BindRequest ::= SEQUENCE {
610Sstevel@tonic-gate 	 *		version		INTEGER,
620Sstevel@tonic-gate 	 *		name		DistinguishedName,	 -- who
630Sstevel@tonic-gate 	 *		authentication	CHOICE {
640Sstevel@tonic-gate 	 *			simple		[0] OCTET STRING -- passwd
650Sstevel@tonic-gate #ifdef KERBEROS
660Sstevel@tonic-gate 	 *			krbv42ldap	[1] OCTET STRING
670Sstevel@tonic-gate 	 *			krbv42dsa	[2] OCTET STRING
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate 	 *		}
700Sstevel@tonic-gate 	 *	}
710Sstevel@tonic-gate 	 * all wrapped up in an LDAPMessage sequence.
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 88, "ldap_bind\n"), 0, 0, 0 );
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	switch ( authmethod ) {
770Sstevel@tonic-gate 	case LDAP_AUTH_SIMPLE:
780Sstevel@tonic-gate 		return( ldap_simple_bind( ld, dn, passwd ) );
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #ifdef KERBEROS
810Sstevel@tonic-gate 	case LDAP_AUTH_KRBV41:
820Sstevel@tonic-gate 		return( ldap_kerberos_bind1( ld, dn ) );
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	case LDAP_AUTH_KRBV42:
850Sstevel@tonic-gate 		return( ldap_kerberos_bind2( ld, dn ) );
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	default:
890Sstevel@tonic-gate 		ld->ld_errno = LDAP_AUTH_UNKNOWN;
900Sstevel@tonic-gate 		return( -1 );
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
960Sstevel@tonic-gate  * of the entry to which to bind are supplied, along with the authentication
970Sstevel@tonic-gate  * method to use.  This routine just calls whichever bind routine is
980Sstevel@tonic-gate  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
990Sstevel@tonic-gate  * some other error indication).  Note, the kerberos support assumes the
1000Sstevel@tonic-gate  * user already has a valid tgt for now.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * Examples:
1030Sstevel@tonic-gate  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
1040Sstevel@tonic-gate  *	    "secret", LDAP_AUTH_SIMPLE )
1050Sstevel@tonic-gate  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
1060Sstevel@tonic-gate  *	    NULL, LDAP_AUTH_KRBV4 )
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate int
ldap_bind_s(LDAP * ld,char * dn,char * passwd,int authmethod)1090Sstevel@tonic-gate ldap_bind_s( LDAP *ld, char *dn, char *passwd, int authmethod )
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 89, "ldap_bind_s\n"), 0, 0, 0 );
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	switch ( authmethod ) {
1140Sstevel@tonic-gate 	case LDAP_AUTH_SIMPLE:
1150Sstevel@tonic-gate 		return( ldap_simple_bind_s( ld, dn, passwd ) );
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #ifdef KERBEROS
1180Sstevel@tonic-gate 	case LDAP_AUTH_KRBV4:
1190Sstevel@tonic-gate 		return( ldap_kerberos_bind_s( ld, dn ) );
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	case LDAP_AUTH_KRBV41:
1220Sstevel@tonic-gate 		return( ldap_kerberos_bind1_s( ld, dn ) );
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	case LDAP_AUTH_KRBV42:
1250Sstevel@tonic-gate 		return( ldap_kerberos_bind2_s( ld, dn ) );
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	default:
1290Sstevel@tonic-gate 		return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate void
ldap_set_rebind_proc(LDAP * ld,LDAP_REBIND_FUNCTION * rebindproc,void * extra_arg)1350Sstevel@tonic-gate ldap_set_rebind_proc( LDAP *ld, LDAP_REBIND_FUNCTION *rebindproc, void *extra_arg )
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate #ifdef _REENTRANT
1380Sstevel@tonic-gate         LOCK_LDAP(ld);
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate 	ld->ld_rebindproc = rebindproc;
1410Sstevel@tonic-gate 	ld->ld_rebind_extra_arg = extra_arg;
1420Sstevel@tonic-gate #ifdef _REENTRANT
1430Sstevel@tonic-gate         UNLOCK_LDAP(ld);
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate }
146