Back to index

openldap  2.4.31
bind.c
Go to the documentation of this file.
00001 /* bind.c */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00017  * All rights reserved.
00018  */
00019 
00020 #include "portable.h"
00021 
00022 #include <stdio.h>
00023 
00024 #include <ac/stdlib.h>
00025 
00026 #include <ac/socket.h>
00027 #include <ac/string.h>
00028 #include <ac/time.h>
00029 
00030 #include "ldap-int.h"
00031 #include "ldap_log.h"
00032 
00033 /*
00034  *     BindRequest ::= SEQUENCE {
00035  *            version              INTEGER,
00036  *            name          DistinguishedName,    -- who
00037  *            authentication       CHOICE {
00038  *                   simple        [0] OCTET STRING -- passwd
00039  *                   krbv42ldap    [1] OCTET STRING -- OBSOLETE
00040  *                   krbv42dsa     [2] OCTET STRING -- OBSOLETE
00041  *                   sasl          [3] SaslCredentials  -- LDAPv3
00042  *            }
00043  *     }
00044  *
00045  *     BindResponse ::= SEQUENCE {
00046  *            COMPONENTS OF LDAPResult,
00047  *            serverSaslCreds             OCTET STRING OPTIONAL -- LDAPv3
00048  *     }
00049  *
00050  * (Source: RFC 2251)
00051  */
00052 
00053 /*
00054  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
00055  * of the entry to which to bind are supplied, along with the authentication
00056  * method to use.  The msgid of the bind request is returned on success,
00057  * -1 if there's trouble.  ldap_result() should be called to find out the
00058  * outcome of the bind request.
00059  *
00060  * Example:
00061  *     ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
00062  *         LDAP_AUTH_SIMPLE )
00063  */
00064 
00065 int
00066 ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
00067 {
00068        Debug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
00069 
00070        switch ( authmethod ) {
00071        case LDAP_AUTH_SIMPLE:
00072               return( ldap_simple_bind( ld, dn, passwd ) );
00073 
00074 #ifdef HAVE_GSSAPI
00075        case LDAP_AUTH_NEGOTIATE:
00076               return( ldap_gssapi_bind_s( ld, dn, passwd) );
00077 #endif
00078 
00079        case LDAP_AUTH_SASL:
00080               /* user must use ldap_sasl_bind */
00081               /* FALL-THRU */
00082 
00083        default:
00084               ld->ld_errno = LDAP_AUTH_UNKNOWN;
00085               return( -1 );
00086        }
00087 }
00088 
00089 /*
00090  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
00091  * of the entry to which to bind are supplied, along with the authentication
00092  * method to use.  This routine just calls whichever bind routine is
00093  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
00094  * some other error indication).
00095  *
00096  * Examples:
00097  *     ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
00098  *         "secret", LDAP_AUTH_SIMPLE )
00099  *     ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
00100  *         NULL, LDAP_AUTH_KRBV4 )
00101  */
00102 int
00103 ldap_bind_s(
00104        LDAP *ld,
00105        LDAP_CONST char *dn,
00106        LDAP_CONST char *passwd,
00107        int authmethod )
00108 {
00109        Debug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
00110 
00111        switch ( authmethod ) {
00112        case LDAP_AUTH_SIMPLE:
00113               return( ldap_simple_bind_s( ld, dn, passwd ) );
00114 
00115 #ifdef HAVE_GSSAPI
00116        case LDAP_AUTH_NEGOTIATE:
00117               return( ldap_gssapi_bind_s( ld, dn, passwd) );
00118 #endif
00119 
00120        case LDAP_AUTH_SASL:
00121               /* user must use ldap_sasl_bind */
00122               /* FALL-THRU */
00123 
00124        default:
00125               return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
00126        }
00127 }