1N/A/*
1N/A *
1N/A * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * Copyright (c) 1993 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A *
1N/A * sbind.c
1N/A */
1N/A
1N/A#ifndef lint
1N/Astatic char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <string.h>
1N/A
1N/A#ifdef MACOS
1N/A#include "macos.h"
1N/A#endif /* MACOS */
1N/A
1N/A#if !defined( MACOS ) && !defined( DOS )
1N/A#include <sys/types.h>
1N/A#include <sys/socket.h>
1N/A#endif
1N/A
1N/A#include "lber.h"
1N/A#include "ldap.h"
1N/A#include "ldap-private.h"
1N/A#include "ldap-int.h"
1N/A
1N/ABerElement * ldap_build_simple_bind_req(LDAP *ld, char *dn, char *passwd, LDAPControl **serverctrls)
1N/A{
1N/A /*
1N/A * The bind request looks like this:
1N/A * BindRequest ::= SEQUENCE {
1N/A * version INTEGER,
1N/A * name DistinguishedName, -- who
1N/A * authentication CHOICE {
1N/A * simple [0] OCTET STRING -- passwd
1N/A * }
1N/A * }
1N/A * all wrapped up in an LDAPMessage sequence.
1N/A */
1N/A
1N/A BerElement *ber = NULL;
1N/A
1N/A if ( dn == NULL )
1N/A dn = "";
1N/A if ( passwd == NULL )
1N/A passwd = "";
1N/A
1N/A if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
1N/A return (NULLBER);
1N/A }
1N/A
1N/A /* fill it in */
1N/A if ( ber_printf( ber, "{it{ists}", ++ld->ld_msgid, LDAP_REQ_BIND, ld->ld_version, dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
1N/A ld->ld_errno = LDAP_ENCODING_ERROR;
1N/A ber_free( ber, 1 );
1N/A return( NULLBER );
1N/A }
1N/A
1N/A /* LDAPv3 */
1N/A /* Code controls if any */
1N/A if (serverctrls && serverctrls[0]) {
1N/A if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
1N/A ld->ld_errno = LDAP_ENCODING_ERROR;
1N/A ber_free( ber, 1 );
1N/A return( NULLBER );
1N/A }
1N/A } else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
1N/A /* Otherwise, is there any global server ctrls ? */
1N/A if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
1N/A ld->ld_errno = LDAP_ENCODING_ERROR;
1N/A ber_free( ber, 1 );
1N/A return( NULLBER );
1N/A }
1N/A }
1N/A
1N/A if ( ber_printf( ber, "}" ) == -1 ) {
1N/A ld->ld_errno = LDAP_ENCODING_ERROR;
1N/A ber_free( ber, 1 );
1N/A return( NULLBER );
1N/A }
1N/A
1N/A return (ber);
1N/A}
1N/A
1N/A/*
1N/A * ldap_simple_bind - bind to the ldap server (and X.500). The dn and
1N/A * password of the entry to which to bind are supplied. The message id
1N/A * of the request initiated is returned.
1N/A *
1N/A * Example:
1N/A * ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
1N/A * "secret" )
1N/A */
1N/A
1N/Aint
1N/Aldap_simple_bind( LDAP *ld, char *dn, char *passwd )
1N/A{
1N/A BerElement *ber;
1N/A int rv;
1N/A
1N/A
1N/A#ifdef _REENTRANT
1N/A LOCK_LDAP(ld);
1N/A#endif
1N/A Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 240, "ldap_simple_bind\n"), 0, 0, 0 );
1N/A
1N/A if ( dn == NULL )
1N/A dn = "";
1N/A if ( passwd == NULL )
1N/A passwd = "";
1N/A
1N/A /* create a message to send */
1N/A if ( (ber = ldap_build_simple_bind_req( ld, dn, passwd, NULL )) == NULLBER ) {
1N/A#ifdef _REENTRANT
1N/A UNLOCK_LDAP(ld);
1N/A#endif
1N/A return( -1 );
1N/A }
1N/A
1N/A#ifndef NO_CACHE
1N/A if ( ld->ld_cache != NULL ) {
1N/A ldap_flush_cache( ld );
1N/A }
1N/A#endif /* !NO_CACHE */
1N/A
1N/A /* send the message */
1N/A rv = send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
1N/A#ifdef _REENTRANT
1N/A UNLOCK_LDAP(ld);
1N/A#endif
1N/A return ( rv );
1N/A}
1N/A
1N/A/*
1N/A * ldap_simple_bind - bind to the ldap server (and X.500) using simple
1N/A * authentication. The dn and password of the entry to which to bind are
1N/A * supplied. LDAP_SUCCESS is returned upon success, the ldap error code
1N/A * otherwise.
1N/A *
1N/A * Example:
1N/A * ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
1N/A * "secret" )
1N/A */
1N/A
1N/Aint
1N/Aldap_simple_bind_s( LDAP *ld, char *dn, char *passwd )
1N/A{
1N/A int msgid;
1N/A LDAPMessage *result;
1N/A
1N/A Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 241, "ldap_simple_bind_s\n"), 0, 0, 0 );
1N/A
1N/A if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
1N/A return( ld->ld_errno );
1N/A
1N/A if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
1N/A return( ld->ld_errno ); /* ldap_result sets ld_errno */
1N/A
1N/A return( ldap_result2error( ld, result, 1 ) );
1N/A}