]> git.sur5r.net Git - openldap/blob - libraries/libldap/sbind.c
fd7892d5e65fcb1fb380af0167b0bc5fff6f3ca6
[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( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
56 {
57         BerElement      *ber;
58
59         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
60
61         assert( ld != NULL );
62         assert( LDAP_VALID( ld ) );
63
64         if ( dn == NULL )
65                 dn = "";
66         if ( passwd == NULL )
67                 passwd = "";
68
69         /* create a message to send */
70         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
71                 return( -1 );
72         }
73
74         assert( BER_VALID( ber ) );
75
76         /* fill it in */
77         if ( ber_printf( ber, "{it{ists}}", ++ld->ld_msgid, LDAP_REQ_BIND,
78             ld->ld_version, dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
79                 ld->ld_errno = LDAP_ENCODING_ERROR;
80                 ber_free( ber, 1 );
81                 return( -1 );
82         }
83
84 #ifndef LDAP_NOCACHE
85         if ( ld->ld_cache != NULL ) {
86                 ldap_flush_cache( ld );
87         }
88 #endif /* !LDAP_NOCACHE */
89
90         /* send the message */
91         return( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
92 }
93
94 /*
95  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
96  * authentication.  The dn and password of the entry to which to bind are
97  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
98  * otherwise.
99  *
100  * Example:
101  *      ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
102  *          "secret" )
103  */
104
105 int
106 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
107 {
108         int             msgid;
109         LDAPMessage     *result;
110
111         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
112
113         if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
114                 return( ld->ld_errno );
115
116         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 )
117                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
118
119         return( ldap_result2error( ld, result, 1 ) );
120 }