Back to index

openldap  2.4.31
sbind.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 
00019 /*
00020  *     BindRequest ::= SEQUENCE {
00021  *            version              INTEGER,
00022  *            name          DistinguishedName,    -- who
00023  *            authentication       CHOICE {
00024  *                   simple        [0] OCTET STRING -- passwd
00025  *                   krbv42ldap    [1] OCTET STRING  -- OBSOLETE
00026  *                   krbv42dsa     [2] OCTET STRING  -- OBSOLETE
00027  *                   sasl          [3] SaslCredentials  -- LDAPv3
00028  *            }
00029  *     }
00030  *
00031  *     BindResponse ::= SEQUENCE {
00032  *            COMPONENTS OF LDAPResult,
00033  *            serverSaslCreds             OCTET STRING OPTIONAL -- LDAPv3
00034  *     }
00035  *
00036  */
00037 
00038 #include "portable.h"
00039 
00040 #include <stdio.h>
00041 
00042 #include <ac/socket.h>
00043 #include <ac/string.h>
00044 #include <ac/time.h>
00045 
00046 #include "ldap-int.h"
00047 
00048 /*
00049  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
00050  * password of the entry to which to bind are supplied.  The message id
00051  * of the request initiated is returned.
00052  *
00053  * Example:
00054  *     ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
00055  *         "secret" )
00056  */
00057 
00058 int
00059 ldap_simple_bind(
00060        LDAP *ld,
00061        LDAP_CONST char *dn,
00062        LDAP_CONST char *passwd )
00063 {
00064        int rc;
00065        int msgid;
00066        struct berval cred;
00067 
00068        Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
00069 
00070        assert( ld != NULL );
00071        assert( LDAP_VALID( ld ) );
00072 
00073        if ( passwd != NULL ) {
00074               cred.bv_val = (char *) passwd;
00075               cred.bv_len = strlen( passwd );
00076        } else {
00077               cred.bv_val = "";
00078               cred.bv_len = 0;
00079        }
00080 
00081        rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
00082               NULL, NULL, &msgid );
00083 
00084        return rc == LDAP_SUCCESS ? msgid : -1;
00085 }
00086 
00087 /*
00088  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
00089  * authentication.  The dn and password of the entry to which to bind are
00090  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
00091  * otherwise.
00092  *
00093  * Example:
00094  *     ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
00095  *         "secret" )
00096  */
00097 
00098 int
00099 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
00100 {
00101        struct berval cred;
00102 
00103        Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
00104 
00105        if ( passwd != NULL ) {
00106               cred.bv_val = (char *) passwd;
00107               cred.bv_len = strlen( passwd );
00108        } else {
00109               cred.bv_val = "";
00110               cred.bv_len = 0;
00111        }
00112 
00113        return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
00114               NULL, NULL, NULL );
00115 }