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