]> git.sur5r.net Git - openldap/blob - libraries/libldap/bind.c
release
[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 /* Portions Copyright (C) The Internet Society (1997)
20  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
21  */
22
23 /*
24  *      BindRequest ::= SEQUENCE {
25  *              version         INTEGER,
26  *              name            DistinguishedName,       -- who
27  *              authentication  CHOICE {
28  *                      simple          [0] OCTET STRING -- passwd
29 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
30  *                      krbv42ldap      [1] OCTET STRING
31  *                      krbv42dsa       [2] OCTET STRING
32 #endif
33  *                      sasl            [3] SaslCredentials     -- LDAPv3
34  *              }
35  *      }
36  *
37  *      BindResponse ::= SEQUENCE {
38  *              COMPONENTS OF LDAPResult,
39  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
40  *      }
41  *
42  */
43
44 #include "portable.h"
45
46 #include <stdio.h>
47
48 #include <ac/stdlib.h>
49
50 #include <ac/socket.h>
51 #include <ac/string.h>
52 #include <ac/time.h>
53
54 #include "ldap-int.h"
55 #include "ldap_log.h"
56
57 /*
58  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
59  * of the entry to which to bind are supplied, along with the authentication
60  * method to use.  The msgid of the bind request is returned on success,
61  * -1 if there's trouble.  Note, the kerberos support assumes the user already
62  * has a valid tgt for now.  ldap_result() should be called to find out the
63  * outcome of the bind request.
64  *
65  * Example:
66  *      ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
67  *          LDAP_AUTH_SIMPLE )
68  */
69
70 int
71 ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
72 {
73         Debug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
74
75         switch ( authmethod ) {
76         case LDAP_AUTH_SIMPLE:
77                 return( ldap_simple_bind( ld, dn, passwd ) );
78
79 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
80         case LDAP_AUTH_KRBV41:
81                 return( ldap_kerberos_bind1( ld, dn ) );
82
83         case LDAP_AUTH_KRBV42:
84                 return( ldap_kerberos_bind2( ld, dn ) );
85 #endif
86
87         case LDAP_AUTH_SASL:
88                 /* user must use ldap_sasl_bind */
89                 /* FALL-THRU */
90
91         default:
92                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
93                 return( -1 );
94         }
95 }
96
97 /*
98  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
99  * of the entry to which to bind are supplied, along with the authentication
100  * method to use.  This routine just calls whichever bind routine is
101  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
102  * some other error indication).  Note, the kerberos support assumes the
103  * user already has a valid tgt for now.
104  *
105  * Examples:
106  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
107  *          "secret", LDAP_AUTH_SIMPLE )
108  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
109  *          NULL, LDAP_AUTH_KRBV4 )
110  */
111 int
112 ldap_bind_s(
113         LDAP *ld,
114         LDAP_CONST char *dn,
115         LDAP_CONST char *passwd,
116         int authmethod )
117 {
118         Debug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
119
120         switch ( authmethod ) {
121         case LDAP_AUTH_SIMPLE:
122                 return( ldap_simple_bind_s( ld, dn, passwd ) );
123
124 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
125         case LDAP_AUTH_KRBV4:
126                 return( ldap_kerberos_bind_s( ld, dn ) );
127
128         case LDAP_AUTH_KRBV41:
129                 return( ldap_kerberos_bind1_s( ld, dn ) );
130
131         case LDAP_AUTH_KRBV42:
132                 return( ldap_kerberos_bind2_s( ld, dn ) );
133 #endif
134
135         case LDAP_AUTH_SASL:
136                 /* user must use ldap_sasl_bind */
137                 /* FALL-THRU */
138
139         default:
140                 return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
141         }
142 }