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