]> git.sur5r.net Git - openldap/blob - libraries/libldap/sbind.c
EXPERIMENTAL: Use sasl bind for simple bind.
[openldap] / libraries / libldap / sbind.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1993 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  sbind.c
10  */
11
12 /*
13  *      BindRequest ::= SEQUENCE {
14  *              version         INTEGER,
15  *              name            DistinguishedName,       -- who
16  *              authentication  CHOICE {
17  *                      simple          [0] OCTET STRING -- passwd
18 #ifdef HAVE_KERBEROS
19  *                      krbv42ldap      [1] OCTET STRING
20  *                      krbv42dsa       [2] OCTET STRING
21 #endif
22  *                      sasl            [3] SaslCredentials     -- LDAPv3
23  *              }
24  *      }
25  *
26  *      BindResponse ::= SEQUENCE {
27  *              COMPONENTS OF LDAPResult,
28  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
29  *      }
30  *
31  */
32
33 #include "portable.h"
34
35 #include <stdio.h>
36
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/time.h>
40
41 #include "ldap-int.h"
42
43
44 /*
45  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
46  * password of the entry to which to bind are supplied.  The message id
47  * of the request initiated is returned.
48  *
49  * Example:
50  *      ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
51  *          "secret" )
52  */
53
54 int
55 ldap_simple_bind(
56         LDAP *ld,
57         LDAP_CONST char *dn,
58         LDAP_CONST char *passwd )
59 {
60         int rc;
61         int msgid;
62         struct berval cred;
63
64         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
65
66         assert( ld != NULL );
67         assert( LDAP_VALID( ld ) );
68
69         if ( passwd != NULL ) {
70                 cred.bv_val = (char *) passwd;
71                 cred.bv_len = strlen( passwd );
72         } else {
73                 cred.bv_val = "";
74                 cred.bv_len = 0;
75         }
76
77         rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
78                 NULL, NULL, &msgid );
79
80         return rc == LDAP_SUCCESS ? msgid : -1;
81 }
82
83 /*
84  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
85  * authentication.  The dn and password of the entry to which to bind are
86  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
87  * otherwise.
88  *
89  * Example:
90  *      ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
91  *          "secret" )
92  */
93
94 int
95 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
96 {
97         struct berval cred;
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
100
101         if ( passwd != NULL ) {
102                 cred.bv_val = (char *) passwd;
103                 cred.bv_len = strlen( passwd );
104         } else {
105                 cred.bv_val = "";
106                 cred.bv_len = 0;
107         }
108
109         return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
110                 NULL, NULL, NULL );
111 }