]> git.sur5r.net Git - openldap/blob - libraries/libldap/sbind.c
a64d0ff778209549a0a689c7797ed8a26bf26d78
[openldap] / libraries / libldap / sbind.c
1 /*
2  *  Copyright (c) 1993 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  sbind.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15
16 #include <ac/socket.h>
17 #include <ac/string.h>
18 #include <ac/time.h>
19
20 #include "lber.h"
21 #include "ldap.h"
22 #include "ldap-int.h"
23
24
25 /*
26  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
27  * password of the entry to which to bind are supplied.  The message id
28  * of the request initiated is returned.
29  *
30  * Example:
31  *      ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
32  *          "secret" )
33  */
34
35 int
36 ldap_simple_bind( LDAP *ld, char *dn, char *passwd )
37 {
38         BerElement      *ber;
39
40         /*
41          * The bind request looks like this:
42          *      BindRequest ::= SEQUENCE {
43          *              version         INTEGER,
44          *              name            DistinguishedName,       -- who
45          *              authentication  CHOICE {
46          *                      simple          [0] OCTET STRING -- passwd
47          *              }
48          *      }
49          * all wrapped up in an LDAPMessage sequence.
50          */
51
52         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
53
54         if ( dn == NULL )
55                 dn = "";
56         if ( passwd == NULL )
57                 passwd = "";
58
59         /* create a message to send */
60         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
61                 return( -1 );
62         }
63
64         /* fill it in */
65         if ( ber_printf( ber, "{it{ists}}", ++ld->ld_msgid, LDAP_REQ_BIND,
66             ld->ld_version, dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
67                 ld->ld_errno = LDAP_ENCODING_ERROR;
68                 ber_free( ber, 1 );
69                 return( -1 );
70         }
71
72 #ifndef NO_CACHE
73         if ( ld->ld_cache != NULL ) {
74                 ldap_flush_cache( ld );
75         }
76 #endif /* !NO_CACHE */
77
78         /* send the message */
79         return( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
80 }
81
82 /*
83  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
84  * authentication.  The dn and password of the entry to which to bind are
85  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
86  * otherwise.
87  *
88  * Example:
89  *      ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
90  *          "secret" )
91  */
92
93 int
94 ldap_simple_bind_s( LDAP *ld, char *dn, char *passwd )
95 {
96         int             msgid;
97         LDAPMessage     *result;
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
100
101         if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
102                 return( ld->ld_errno );
103
104         if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
105                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
106
107         return( ldap_result2error( ld, result, 1 ) );
108 }