Back to index

lightning-sunbird  0.9+nobinonly
bind.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  bind.c
00039  */
00040 
00041 #if 0
00042 #ifndef lint 
00043 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00044 #endif
00045 #endif
00046 
00047 #include "ldap-int.h"
00048 
00049 /*
00050  * ldap_bind - bind to the ldap server. The dn and password
00051  * of the entry to which to bind are supplied, along with the authentication
00052  * method to use.  The msgid of the bind request is returned on success,
00053  * -1 if there's trouble.  Note, the kerberos support assumes the user already
00054  * has a valid tgt for now.  ldap_result() should be called to find out the
00055  * outcome of the bind request.
00056  *
00057  * Example:
00058  *     ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
00059  *         LDAP_AUTH_SIMPLE )
00060  */
00061 
00062 int
00063 LDAP_CALL
00064 ldap_bind( LDAP *ld, const char *dn, const char *passwd, int authmethod )
00065 {
00066        /*
00067         * The bind request looks like this:
00068         *     BindRequest ::= SEQUENCE {
00069         *            version              INTEGER,
00070         *            name          DistinguishedName,    -- who
00071         *            authentication       CHOICE {
00072         *                   simple        [0] OCTET STRING -- passwd
00073         *            }
00074         *     }
00075         * all wrapped up in an LDAPMessage sequence.
00076         */
00077 
00078        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
00079 
00080        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00081               return( -1 );
00082        }
00083 
00084        switch ( authmethod ) {
00085        case LDAP_AUTH_SIMPLE:
00086               return( ldap_simple_bind( ld, dn, passwd ) );
00087 
00088        default:
00089               LDAP_SET_LDERRNO( ld, LDAP_AUTH_UNKNOWN, NULL, NULL );
00090               return( -1 );
00091        }
00092 }
00093 
00094 /*
00095  * ldap_bind_s - bind to the ldap server.  The dn and password
00096  * of the entry to which to bind are supplied, along with the authentication
00097  * method to use.  This routine just calls whichever bind routine is
00098  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
00099  * some other error indication).  Note, the kerberos support assumes the
00100  * user already has a valid tgt for now.
00101  *
00102  * Examples:
00103  *     ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
00104  *         "secret", LDAP_AUTH_SIMPLE )
00105  *     ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
00106  *         NULL, LDAP_AUTH_KRBV4 )
00107  */
00108 int
00109 LDAP_CALL
00110 ldap_bind_s( LDAP *ld, const char *dn, const char *passwd, int authmethod )
00111 {
00112        int    err;
00113 
00114        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
00115 
00116        switch ( authmethod ) {
00117        case LDAP_AUTH_SIMPLE:
00118               return( ldap_simple_bind_s( ld, dn, passwd ) );
00119 
00120        default:
00121               err = LDAP_AUTH_UNKNOWN;
00122               LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00123               return( err );
00124        }
00125 }
00126 
00127 
00128 void
00129 LDAP_CALL
00130 ldap_set_rebind_proc( LDAP *ld, LDAP_REBINDPROC_CALLBACK *rebindproc,
00131     void *arg )
00132 {
00133        if ( ld == NULL ) {
00134               if ( !nsldapi_initialized ) {
00135                      nsldapi_initialize_defaults();
00136               }
00137               ld = &nsldapi_ld_defaults;
00138        }
00139 
00140        if ( NSLDAPI_VALID_LDAP_POINTER( ld )) {
00141               LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
00142               ld->ld_rebind_fn = rebindproc;
00143               ld->ld_rebind_arg = arg;
00144               LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00145        }
00146 }
00147 
00148 
00149 /*
00150  * return a pointer to the bind DN for the default connection (a copy is
00151  * not made).  If there is no bind DN available, NULL is returned.
00152  */
00153 char *
00154 nsldapi_get_binddn( LDAP *ld )
00155 {
00156        char   *binddn;
00157 
00158        binddn = NULL;       /* default -- assume they are not bound */
00159 
00160        LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
00161        if ( NULL != ld->ld_defconn && LDAP_CONNST_CONNECTED ==
00162            ld->ld_defconn->lconn_status && ld->ld_defconn->lconn_bound ) {
00163               if (( binddn = ld->ld_defconn->lconn_binddn ) == NULL ) {
00164                      binddn = "";
00165               }
00166        }
00167        LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00168 
00169        return( binddn );
00170 }