]> git.sur5r.net Git - openldap/blob - libraries/libldap/bind.c
Changes from HEAD for beta
[openldap] / libraries / libldap / bind.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) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  bind.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/stdlib.h>
43
44 #include <ac/socket.h>
45 #include <ac/string.h>
46 #include <ac/time.h>
47
48 #include "ldap-int.h"
49 #include "ldap_log.h"
50
51 /*
52  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
53  * of the entry to which to bind are supplied, along with the authentication
54  * method to use.  The msgid of the bind request is returned on success,
55  * -1 if there's trouble.  Note, the kerberos support assumes the user already
56  * has a valid tgt for now.  ldap_result() should be called to find out the
57  * outcome of the bind request.
58  *
59  * Example:
60  *      ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
61  *          LDAP_AUTH_SIMPLE )
62  */
63
64 int
65 ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
66 {
67 #ifdef NEW_LOGGING
68         LDAP_LOG ( OPERATION, ENTRY, "ldap_bind\n", 0, 0, 0 );
69 #else
70         Debug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
71 #endif
72
73         switch ( authmethod ) {
74         case LDAP_AUTH_SIMPLE:
75                 return( ldap_simple_bind( ld, dn, passwd ) );
76
77 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
78         case LDAP_AUTH_KRBV41:
79                 return( ldap_kerberos_bind1( ld, dn ) );
80
81         case LDAP_AUTH_KRBV42:
82                 return( ldap_kerberos_bind2( ld, dn ) );
83 #endif
84
85         case LDAP_AUTH_SASL:
86                 /* user must use ldap_sasl_bind */
87                 /* FALL-THRU */
88
89         default:
90                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
91                 return( -1 );
92         }
93 }
94
95 /*
96  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
97  * of the entry to which to bind are supplied, along with the authentication
98  * method to use.  This routine just calls whichever bind routine is
99  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
100  * some other error indication).  Note, the kerberos support assumes the
101  * user already has a valid tgt for now.
102  *
103  * Examples:
104  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
105  *          "secret", LDAP_AUTH_SIMPLE )
106  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
107  *          NULL, LDAP_AUTH_KRBV4 )
108  */
109 int
110 ldap_bind_s(
111         LDAP *ld,
112         LDAP_CONST char *dn,
113         LDAP_CONST char *passwd,
114         int authmethod )
115 {
116 #ifdef NEW_LOGGING
117         LDAP_LOG ( OPERATION, ENTRY, "ldap_bind_s\n", 0, 0, 0 );
118 #else
119         Debug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
120 #endif
121
122         switch ( authmethod ) {
123         case LDAP_AUTH_SIMPLE:
124                 return( ldap_simple_bind_s( ld, dn, passwd ) );
125
126 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
127         case LDAP_AUTH_KRBV4:
128                 return( ldap_kerberos_bind_s( ld, dn ) );
129
130         case LDAP_AUTH_KRBV41:
131                 return( ldap_kerberos_bind1_s( ld, dn ) );
132
133         case LDAP_AUTH_KRBV42:
134                 return( ldap_kerberos_bind2_s( ld, dn ) );
135 #endif
136
137         case LDAP_AUTH_SASL:
138                 /* user must use ldap_sasl_bind */
139                 /* FALL-THRU */
140
141         default:
142                 return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
143         }
144 }