]> git.sur5r.net Git - openldap/blob - libraries/libldap/sbind.c
ITS#2657: authzid (and assertion) should apply to most everything
[openldap] / libraries / libldap / sbind.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1993 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  sbind.c
11  */
12 /*
13  * Portions Copyright (C) The Internet Society (1997)
14  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
15  */
16
17 /*
18  *      BindRequest ::= SEQUENCE {
19  *              version         INTEGER,
20  *              name            DistinguishedName,       -- who
21  *              authentication  CHOICE {
22  *                      simple          [0] OCTET STRING -- passwd
23 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
24  *                      krbv42ldap      [1] OCTET STRING
25  *                      krbv42dsa       [2] OCTET STRING
26 #endif
27  *                      sasl            [3] SaslCredentials     -- LDAPv3
28  *              }
29  *      }
30  *
31  *      BindResponse ::= SEQUENCE {
32  *              COMPONENTS OF LDAPResult,
33  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
34  *      }
35  *
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "ldap-int.h"
47
48 /*
49  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
50  * password of the entry to which to bind are supplied.  The message id
51  * of the request initiated is returned.
52  *
53  * Example:
54  *      ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
55  *          "secret" )
56  */
57
58 int
59 ldap_simple_bind(
60         LDAP *ld,
61         LDAP_CONST char *dn,
62         LDAP_CONST char *passwd )
63 {
64         int rc;
65         int msgid;
66         struct berval cred;
67
68 #ifdef NEW_LOGGING
69         LDAP_LOG ( OPERATION, ENTRY, "ldap_simple_bind\n", 0, 0, 0 );
70 #else
71         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
72 #endif
73
74         assert( ld != NULL );
75         assert( LDAP_VALID( ld ) );
76
77         if ( passwd != NULL ) {
78                 cred.bv_val = (char *) passwd;
79                 cred.bv_len = strlen( passwd );
80         } else {
81                 cred.bv_val = "";
82                 cred.bv_len = 0;
83         }
84
85         rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
86                 NULL, NULL, &msgid );
87
88         return rc == LDAP_SUCCESS ? msgid : -1;
89 }
90
91 /*
92  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
93  * authentication.  The dn and password of the entry to which to bind are
94  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
95  * otherwise.
96  *
97  * Example:
98  *      ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
99  *          "secret" )
100  */
101
102 int
103 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
104 {
105         struct berval cred;
106
107 #ifdef NEW_LOGGING
108         LDAP_LOG ( OPERATION, ENTRY, "ldap_simple_bind_s\n", 0, 0, 0 );
109 #else
110         Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
111 #endif
112
113         if ( passwd != NULL ) {
114                 cred.bv_val = (char *) passwd;
115                 cred.bv_len = strlen( passwd );
116         } else {
117                 cred.bv_val = "";
118                 cred.bv_len = 0;
119         }
120
121         return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
122                 NULL, NULL, NULL );
123 }