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