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