]> git.sur5r.net Git - openldap/blob - libraries/libldap/kbind.c
Use struct berval * instead of ** for newpasswd
[openldap] / libraries / libldap / kbind.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) 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  * ldap_kerberos_bind1 - initiate a bind to the ldap server using
50  * kerberos authentication.  The dn is supplied.  It is assumed the user
51  * already has a valid ticket granting ticket.  The msgid of the
52  * request is returned on success (suitable for passing to ldap_result()),
53  * -1 is returned if there's trouble.
54  *
55  * Example:
56  *      ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
57  */
58 int
59 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
60 {
61         BerElement      *ber;
62         char            *cred;
63         int             rc;
64         ber_len_t credlen;
65         ber_int_t       id;
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind1\n", 0, 0, 0 );
69 #else
70         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
71 #endif
72
73         if( ld->ld_version > LDAP_VERSION2 ) {
74                 ld->ld_errno = LDAP_NOT_SUPPORTED;
75                 return -1;
76         }
77
78         if ( dn == NULL )
79                 dn = "";
80
81         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
82             &credlen )) == NULL ) {
83                 return( -1 );   /* ld_errno should already be set */
84         }
85
86         /* create a message to send */
87         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
88                 LDAP_FREE( cred );
89                 return( -1 );
90         }
91
92         LDAP_NEXT_MSGID( ld, id );
93         /* fill it in */
94         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
95             ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
96
97         if ( rc == -1 ) {
98                 LDAP_FREE( cred );
99                 ber_free( ber, 1 );
100                 ld->ld_errno = LDAP_ENCODING_ERROR;
101                 return( -1 );
102         }
103
104         LDAP_FREE( cred );
105
106
107         /* send the message */
108         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
109 }
110
111 int
112 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
113 {
114         int             msgid;
115         LDAPMessage     *res;
116
117 #ifdef NEW_LOGGING
118         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
119 #else
120         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
121 #endif
122
123         /* initiate the bind */
124         if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
125                 return( ld->ld_errno );
126
127         /* wait for a result */
128         if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
129             == -1 ) {
130                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
131         }
132
133         return( ldap_result2error( ld, res, 1 ) );
134 }
135
136 /*
137  * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
138  * kerberos authentication.  The dn is supplied.  It is assumed the user
139  * already has a valid ticket granting ticket.  The msgid of the
140  * request is returned on success (suitable for passing to ldap_result()),
141  * -1 is returned if there's trouble.
142  *
143  * Example:
144  *      ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
145  */
146 int
147 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
148 {
149         BerElement      *ber;
150         char            *cred;
151         int             rc;
152         ber_len_t credlen;
153         ber_int_t id;
154
155 #ifdef NEW_LOGGING
156         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind2\n", 0, 0, 0 );
157 #else
158         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
159 #endif
160
161         if( ld->ld_version > LDAP_VERSION2 ) {
162                 ld->ld_errno = LDAP_NOT_SUPPORTED;
163                 return -1;
164         }
165
166         if ( dn == NULL )
167                 dn = "";
168
169         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
170             == NULL ) {
171                 return( -1 );   /* ld_errno should already be set */
172         }
173
174         /* create a message to send */
175         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
176                 LDAP_FREE( cred );
177                 return( -1 );
178         }
179
180         LDAP_NEXT_MSGID( ld, id );
181         /* fill it in */
182         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
183             ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
184
185         LDAP_FREE( cred );
186
187         if ( rc == -1 ) {
188                 ber_free( ber, 1 );
189                 ld->ld_errno = LDAP_ENCODING_ERROR;
190                 return( -1 );
191         }
192
193         /* send the message */
194         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
195 }
196
197 /* synchronous bind to DSA using kerberos */
198 int
199 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
200 {
201         int             msgid;
202         LDAPMessage     *res;
203
204 #ifdef NEW_LOGGING
205         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind2_s\n" , 0, 0, 0);
206 #else
207         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
208 #endif
209
210         /* initiate the bind */
211         if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
212                 return( ld->ld_errno );
213
214         /* wait for a result */
215         if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
216             == -1 ) {
217                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
218         }
219
220         return( ldap_result2error( ld, res, 1 ) );
221 }
222
223 /* synchronous bind to ldap and DSA using kerberos */
224 int
225 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
226 {
227         int     err;
228
229 #ifdef NEW_LOGGING
230         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind_s\n", 0, 0, 0 );
231 #else
232         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
233 #endif
234
235         if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
236                 return( err );
237
238         return( ldap_kerberos_bind2_s( ld, dn ) );
239 }
240
241
242 #ifndef AUTHMAN
243 /*
244  * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
245  * The dn of the entry to which to bind is supplied.  It's assumed the
246  * user already has a tgt.
247  */
248
249 char *
250 ldap_get_kerberosv4_credentials(
251         LDAP *ld,
252         LDAP_CONST char *who,
253         LDAP_CONST char *service,
254         ber_len_t *len )
255 {
256         KTEXT_ST        ktxt;
257         int             err;
258         char            realm[REALM_SZ], *cred, *krbinstance;
259
260 #ifdef NEW_LOGGING
261         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
262 #else
263         Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
264 #endif
265
266         if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
267 #ifdef NEW_LOGGING
268                 LDAP_LOG ( OPERATION, ERR, 
269                         "ldap_get_kerberosv4_credentials: krb_get_tf_realm failed: %s\n",
270                         krb_err_txt[err], 0, 0 );
271 #else
272                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
273                         "krb_get_tf_realm failed: %s\n", krb_err_txt[err], 0, 0 );
274 #endif
275                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
276                 return( NULL );
277         }
278
279         if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
280                 /* not connected yet */
281                 int rc = ldap_open_defconn( ld );
282
283                 if( rc < 0 ) return NULL;
284         }
285
286         krbinstance = ld->ld_defconn->lconn_krbinstance;
287
288         if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
289             != KSUCCESS )
290         {
291 #ifdef NEW_LOGGING
292                 LDAP_LOG ( OPERATION, ERR, 
293                         "ldap_get_kerberosv4_credentials: krb_mk_req failed: %s\n",
294                         krb_err_txt[err], 0, 0 );
295 #else
296                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
297                         "krb_mk_req failed (%s)\n", krb_err_txt[err], 0, 0 );
298 #endif
299                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
300                 return( NULL );
301         }
302
303         if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
304                 ld->ld_errno = LDAP_NO_MEMORY;
305                 return( NULL );
306         }
307
308         *len = ktxt.length;
309         AC_MEMCPY( cred, ktxt.dat, ktxt.length );
310
311         return( cred );
312 }
313
314 #endif /* !AUTHMAN */
315 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */