]> git.sur5r.net Git - openldap/blob - libraries/libldap/kbind.c
unifdef -UNEW_LOGGING
[openldap] / libraries / libldap / kbind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18 /* Portions Copyright (C) The Internet Society (1997)
19  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
20  */
21
22 /*
23  *      BindRequest ::= SEQUENCE {
24  *              version         INTEGER,
25  *              name            DistinguishedName,       -- who
26  *              authentication  CHOICE {
27  *                      simple          [0] OCTET STRING -- passwd
28 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
29  *                      krbv42ldap      [1] OCTET STRING
30  *                      krbv42dsa       [2] OCTET STRING
31 #endif
32  *                      sasl            [3] SaslCredentials     -- LDAPv3
33  *              }
34  *      }
35  *
36  *      BindResponse ::= SEQUENCE {
37  *              COMPONENTS OF LDAPResult,
38  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
39  *      }
40  *
41  */
42
43 #include "portable.h"
44
45 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
46
47 #include <stdio.h>
48 #include <ac/stdlib.h>
49
50 #include <ac/krb.h>
51 #include <ac/socket.h>
52 #include <ac/string.h>
53 #include <ac/time.h>
54
55 #include "ldap-int.h"
56
57 /*
58  * ldap_kerberos_bind1 - initiate a bind to the ldap server using
59  * kerberos authentication.  The dn is supplied.  It is assumed the user
60  * already has a valid ticket granting ticket.  The msgid of the
61  * request is returned on success (suitable for passing to ldap_result()),
62  * -1 is returned if there's trouble.
63  *
64  * Example:
65  *      ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
66  */
67 int
68 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
69 {
70         BerElement      *ber;
71         char            *cred;
72         int             rc;
73         ber_len_t credlen;
74         ber_int_t       id;
75
76         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
77
78         if( ld->ld_version > LDAP_VERSION2 ) {
79                 ld->ld_errno = LDAP_NOT_SUPPORTED;
80                 return -1;
81         }
82
83         if ( dn == NULL )
84                 dn = "";
85
86         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
87             &credlen )) == NULL ) {
88                 return( -1 );   /* ld_errno should already be set */
89         }
90
91         /* create a message to send */
92         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
93                 LDAP_FREE( cred );
94                 return( -1 );
95         }
96
97         LDAP_NEXT_MSGID( ld, id );
98         /* fill it in */
99         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
100             ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
101
102         if ( rc == -1 ) {
103                 LDAP_FREE( cred );
104                 ber_free( ber, 1 );
105                 ld->ld_errno = LDAP_ENCODING_ERROR;
106                 return( -1 );
107         }
108
109         LDAP_FREE( cred );
110
111
112         /* send the message */
113         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
114 }
115
116 int
117 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
118 {
119         int             msgid;
120         LDAPMessage     *res;
121
122         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
123
124         /* initiate the bind */
125         if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
126                 return( ld->ld_errno );
127
128         /* wait for a result */
129         if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &res )
130             == -1 ) {
131                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
132         }
133
134         return( ldap_result2error( ld, res, 1 ) );
135 }
136
137 /*
138  * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
139  * kerberos authentication.  The dn is supplied.  It is assumed the user
140  * already has a valid ticket granting ticket.  The msgid of the
141  * request is returned on success (suitable for passing to ldap_result()),
142  * -1 is returned if there's trouble.
143  *
144  * Example:
145  *      ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
146  */
147 int
148 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
149 {
150         BerElement      *ber;
151         char            *cred;
152         int             rc;
153         ber_len_t credlen;
154         ber_int_t id;
155
156         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
157
158         if( ld->ld_version > LDAP_VERSION2 ) {
159                 ld->ld_errno = LDAP_NOT_SUPPORTED;
160                 return -1;
161         }
162
163         if ( dn == NULL )
164                 dn = "";
165
166         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
167             == NULL ) {
168                 return( -1 );   /* ld_errno should already be set */
169         }
170
171         /* create a message to send */
172         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
173                 LDAP_FREE( cred );
174                 return( -1 );
175         }
176
177         LDAP_NEXT_MSGID( ld, id );
178         /* fill it in */
179         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
180             ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
181
182         LDAP_FREE( cred );
183
184         if ( rc == -1 ) {
185                 ber_free( ber, 1 );
186                 ld->ld_errno = LDAP_ENCODING_ERROR;
187                 return( -1 );
188         }
189
190         /* send the message */
191         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
192 }
193
194 /* synchronous bind to DSA using kerberos */
195 int
196 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
197 {
198         int             msgid;
199         LDAPMessage     *res;
200
201         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
202
203         /* initiate the bind */
204         if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
205                 return( ld->ld_errno );
206
207         /* wait for a result */
208         if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &res )
209             == -1 ) {
210                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
211         }
212
213         return( ldap_result2error( ld, res, 1 ) );
214 }
215
216 /* synchronous bind to ldap and DSA using kerberos */
217 int
218 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
219 {
220         int     err;
221
222         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
223
224         if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
225                 return( err );
226
227         return( ldap_kerberos_bind2_s( ld, dn ) );
228 }
229
230
231 #ifndef AUTHMAN
232 /*
233  * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
234  * The dn of the entry to which to bind is supplied.  It's assumed the
235  * user already has a tgt.
236  */
237
238 char *
239 ldap_get_kerberosv4_credentials(
240         LDAP *ld,
241         LDAP_CONST char *who,
242         LDAP_CONST char *service,
243         ber_len_t *len )
244 {
245         KTEXT_ST        ktxt;
246         int             err;
247         char            realm[REALM_SZ], *cred, *krbinstance;
248
249         Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
250
251         if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
252                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
253                         "krb_get_tf_realm failed: %s\n", krb_err_txt[err], 0, 0 );
254                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
255                 return( NULL );
256         }
257
258         if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
259                 /* not connected yet */
260                 int rc = ldap_open_defconn( ld );
261
262                 if( rc < 0 ) return NULL;
263         }
264
265         krbinstance = ld->ld_defconn->lconn_krbinstance;
266
267         if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
268             != KSUCCESS )
269         {
270                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
271                         "krb_mk_req failed (%s)\n", krb_err_txt[err], 0, 0 );
272                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
273                 return( NULL );
274         }
275
276         if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
277                 ld->ld_errno = LDAP_NO_MEMORY;
278                 return( NULL );
279         }
280
281         *len = ktxt.length;
282         AC_MEMCPY( cred, ktxt.dat, ktxt.length );
283
284         return( cred );
285 }
286
287 #endif /* !AUTHMAN */
288 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */