]> git.sur5r.net Git - openldap/blob - libraries/libldap/bind.c
2e7dd74ada172c8dd583844435bbbb3b9a512bc3
[openldap] / libraries / libldap / bind.c
1 /* bind.c */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31 #include "ldap_log.h"
32
33 /*
34  *      BindRequest ::= SEQUENCE {
35  *              version         INTEGER,
36  *              name            DistinguishedName,       -- who
37  *              authentication  CHOICE {
38  *                      simple          [0] OCTET STRING -- passwd
39 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
40  *                      krbv42ldap      [1] OCTET STRING
41  *                      krbv42dsa       [2] OCTET STRING
42 #endif
43  *                      sasl            [3] SaslCredentials     -- LDAPv3
44  *              }
45  *      }
46  *
47  *      BindResponse ::= SEQUENCE {
48  *              COMPONENTS OF LDAPResult,
49  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
50  *      }
51  *
52  * (Source: RFC 2251)
53  */
54
55 /*
56  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
57  * of the entry to which to bind are supplied, along with the authentication
58  * method to use.  The msgid of the bind request is returned on success,
59  * -1 if there's trouble.  Note, the kerberos support assumes the user already
60  * has a valid tgt for now.  ldap_result() should be called to find out the
61  * outcome of the bind request.
62  *
63  * Example:
64  *      ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
65  *          LDAP_AUTH_SIMPLE )
66  */
67
68 int
69 ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
70 {
71         Debug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
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         Debug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
117
118         switch ( authmethod ) {
119         case LDAP_AUTH_SIMPLE:
120                 return( ldap_simple_bind_s( ld, dn, passwd ) );
121
122 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
123         case LDAP_AUTH_KRBV4:
124                 return( ldap_kerberos_bind_s( ld, dn ) );
125
126         case LDAP_AUTH_KRBV41:
127                 return( ldap_kerberos_bind1_s( ld, dn ) );
128
129         case LDAP_AUTH_KRBV42:
130                 return( ldap_kerberos_bind2_s( ld, dn ) );
131 #endif
132
133         case LDAP_AUTH_SASL:
134                 /* user must use ldap_sasl_bind */
135                 /* FALL-THRU */
136
137         default:
138                 return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
139         }
140 }