]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/bind.c
ITS#3556: 64-bit portability
[openldap] / servers / slapd / back-bdb / bind.c
1 /* bind.c - bdb backend bind routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 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
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/krb.h>
21 #include <ac/string.h>
22 #include <ac/unistd.h>
23
24 #include "back-bdb.h"
25 #include "external.h"
26
27 int
28 bdb_bind( Operation *op, SlapReply *rs )
29 {
30         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
31         Entry           *e;
32         Attribute       *a;
33         EntryInfo       *ei;
34 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
35         char            krbname[MAX_K_NAME_SZ + 1];
36         AttributeDescription *krbattr = slap_schema.si_ad_krbName;
37         struct berval   krbval;
38         AUTH_DAT        ad;
39 #endif
40
41         AttributeDescription *password = slap_schema.si_ad_userPassword;
42
43         u_int32_t       locker;
44         DB_LOCK         lock;
45
46         Debug( LDAP_DEBUG_ARGS,
47                 "==> " LDAP_XSTRING(bdb_bind) ": dn: %s\n",
48                 op->o_req_dn.bv_val, 0, 0);
49
50         /* allow noauth binds */
51         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
52                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
53                 /* front end will send result */
54                 return LDAP_SUCCESS;
55         }
56
57         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
58         switch(rs->sr_err) {
59         case 0:
60                 break;
61         default:
62                 rs->sr_text = "internal error";
63                 send_ldap_result( op, rs );
64                 return rs->sr_err;
65         }
66
67 dn2entry_retry:
68         /* get entry with reader lock */
69         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1,
70                 locker, &lock );
71
72         switch(rs->sr_err) {
73         case DB_NOTFOUND:
74         case 0:
75                 break;
76         case LDAP_BUSY:
77                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
78                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
79                 return LDAP_BUSY;
80         case DB_LOCK_DEADLOCK:
81         case DB_LOCK_NOTGRANTED:
82                 goto dn2entry_retry;
83         default:
84                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
85                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
86                 return rs->sr_err;
87         }
88
89         e = ei->bei_e;
90         if ( rs->sr_err == DB_NOTFOUND ) {
91                 if( e != NULL ) {
92                         bdb_cache_return_entry_r( bdb->bi_dbenv,
93                                 &bdb->bi_cache, e, &lock );
94                         e = NULL;
95                 }
96
97                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
98                 send_ldap_result( op, rs );
99
100                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
101
102                 return rs->sr_err;
103         }
104
105         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
106
107         /* check for deleted */
108 #ifdef BDB_SUBENTRIES
109         if ( is_entry_subentry( e ) ) {
110                 /* entry is an subentry, don't allow bind */
111                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
112                         0, 0 );
113                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
114                 goto done;
115         }
116 #endif
117
118         if ( is_entry_alias( e ) ) {
119                 /* entry is an alias, don't allow bind */
120                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
121                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
122                 goto done;
123         }
124
125         if ( is_entry_referral( e ) ) {
126                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
127                         0, 0 );
128                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
129                 goto done;
130         }
131
132         switch ( op->oq_bind.rb_method ) {
133         case LDAP_AUTH_SIMPLE:
134                 rs->sr_err = access_allowed( op, e,
135                         password, NULL, ACL_AUTH, NULL );
136                 if ( ! rs->sr_err ) {
137                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
138                         goto done;
139                 }
140
141                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
142                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
143                         goto done;
144                 }
145
146                 if ( slap_passwd_check( op->o_conn,
147                         a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 )
148                 {
149                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
150                         goto done;
151                 }
152
153                 rs->sr_err = 0;
154                 break;
155
156 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
157         case LDAP_AUTH_KRBV41:
158                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad )
159                         != LDAP_SUCCESS )
160                 {
161                         rs->sr_err = LDAP_INVALID_CREDENTIALS,
162                         goto done;
163                 }
164
165                 rs->sr_err = access_allowed( op, e,
166                         krbattr, NULL, ACL_AUTH, NULL );
167                 if ( ! rs->sr_err ) {
168                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS,
169                         goto done;
170                 }
171
172                 krbval.bv_len = sprintf( krbname, "%s%s%s@%s", ad.pname,
173                         *ad.pinst ? "." : "", ad.pinst, ad.prealm );
174
175                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
176                         /*
177                          * no krbname values present: check against DN
178                          */
179                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
180                                 rs->sr_err = 0;
181                                 break;
182                         }
183                         rs->sr_err = LDAP_INAPPROPRIATE_AUTH,
184                         goto done;
185
186                 } else {        /* look for krbname match */
187                         krbval.bv_val = krbname;
188
189                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
190                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
191                                 goto done;
192                         }
193                 }
194                 rs->sr_err = 0;
195                 break;
196 #endif
197
198         default:
199                 assert( 0 ); /* should not be unreachable */
200                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
201                 rs->sr_text = "authentication method not supported";
202         }
203
204 done:
205         /* free entry and reader lock */
206         if( e != NULL ) {
207                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
208         }
209
210         LOCK_ID_FREE(bdb->bi_dbenv, locker);
211
212         if ( rs->sr_err ) {
213                 send_ldap_result( op, rs );
214                 if ( rs->sr_ref ) {
215                         ber_bvarray_free( rs->sr_ref );
216                         rs->sr_ref = NULL;
217                 }
218         }
219         /* front end will send result on success (rs->sr_err==0) */
220         return rs->sr_err;
221 }