]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/bind.c
neutralize previous add.c change until bdb2_back_entry_release_rw() is provided
[openldap] / servers / slapd / back-bdb2 / bind.c
1 /* bind.c - bdb2 backend bind and unbind routines */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/krb.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include "slap.h"
13 #include "back-bdb2.h"
14 #include "proto-back-bdb2.h"
15
16 #include <lutil.h>
17
18 #ifdef HAVE_KERBEROS
19 extern int      bdb2i_krbv4_ldap_auth();
20 #endif
21
22 static int
23 crypted_value_find(
24         struct berval       **vals,
25         struct berval       *v,
26         int                 syntax,
27         int                 normalize,
28         struct berval           *cred
29 )
30 {
31         int     i;
32         for ( i = 0; vals[i] != NULL; i++ ) {
33                 if ( syntax != SYNTAX_BIN ) {
34                         int result;
35
36 #ifdef SLAPD_CRYPT
37                         ldap_pvt_thread_mutex_lock( &crypt_mutex );
38 #endif
39
40                         result = lutil_passwd(
41                                 (char*) cred->bv_val,
42                                 (char*) vals[i]->bv_val);
43
44 #ifdef SLAPD_CRYPT
45                         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
46 #endif
47
48                         return result;
49
50                 } else {
51                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
52                         return( 0 );
53                 }
54         }
55         }
56
57         return( 1 );
58 }
59
60 static int
61 bdb2i_back_bind_internal(
62     BackendDB           *be,
63     Connection          *conn,
64     Operation           *op,
65     char                *dn,
66     int                 method,
67     struct berval       *cred,
68         char**  edn
69 )
70 {
71         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
72         Entry           *e;
73         Attribute       *a;
74         int             rc;
75         char            *matched;
76 #ifdef HAVE_KERBEROS
77         char            krbname[MAX_K_NAME_SZ + 1];
78         AUTH_DAT        ad;
79 #endif
80
81         Debug(LDAP_DEBUG_ARGS, "==> bdb2_back_bind: dn: %s\n", dn, 0, 0);
82
83         *edn = NULL;
84
85         /* get entry with reader lock */
86         if ( (e = bdb2i_dn2entry_r( be, dn, &matched )) == NULL ) {
87                 /* allow noauth binds */
88                 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
89                         /*
90                          * bind successful, but return 1 so we don't
91                          * authorize based on noauth credentials
92                          */
93                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
94                         rc = 1;
95                 } else if ( be_isroot_pw( be, dn, cred ) ) {
96                         /* front end will send result */
97                         *edn = ch_strdup( be_root_dn( be ) );
98                         rc = 0;
99                 } else {
100                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
101                         rc = 1;
102                 }
103                 if ( matched != NULL ) {
104                         free( matched );
105                 }
106                 return( rc );
107         }
108
109         *edn = ch_strdup( e->e_dn );
110
111         /* check for deleted */
112
113         switch ( method ) {
114         case LDAP_AUTH_SIMPLE:
115                 if ( cred->bv_len == 0 ) {
116                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
117
118                         /* stop front end from sending result */
119                         rc = 1;
120                         goto return_results;
121                 }
122
123                 /* check for root dn/passwd */
124                 if ( be_isroot_pw( be, dn, cred ) ) {
125                         /* front end will send result */
126                         if( *edn != NULL ) free( *edn );
127                         *edn = ch_strdup( be_root_dn( be ) );
128                         rc = 0;
129                         goto return_results;
130                 }
131
132                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
133                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
134                             NULL, NULL );
135
136                         /* stop front end from sending result */
137                         rc = 1;
138                         goto return_results;
139                 }
140
141                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
142                 {
143                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
144                                 NULL, NULL );
145                         /* stop front end from sending result */
146                         rc = 1;
147                         goto return_results;
148                 }
149                 rc = 0;
150                 break;
151
152 #ifdef HAVE_KERBEROS
153         case LDAP_AUTH_KRBV41:
154                 if ( bdb2i_krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
155                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
156                             NULL, NULL );
157                         rc = 0;
158                         goto return_results;
159                 }
160                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
161                     : "", ad.pinst, ad.prealm );
162                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
163                         /*
164                          * no krbName values present:  check against DN
165                          */
166                         if ( strcasecmp( dn, krbname ) == 0 ) {
167                                 rc = 0; /* XXX wild ass guess */
168                                 break;
169                         }
170                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
171                             NULL, NULL );
172                         rc = 1;
173                         goto return_results;
174                 } else {        /* look for krbName match */
175                         struct berval   krbval;
176
177                         krbval.bv_val = krbname;
178                         krbval.bv_len = strlen( krbname );
179
180                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
181                                 send_ldap_result( conn, op,
182                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
183                                 rc = 1;
184                                 goto return_results;
185                         }
186                 }
187                 rc = 0;
188                 break;
189
190         case LDAP_AUTH_KRBV42:
191                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
192                 /* stop front end from sending result */
193                 rc = 1;
194                 goto return_results;
195 #endif
196
197         default:
198                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
199                     NULL, "auth method not supported" );
200                 rc = 1;
201                 goto return_results;
202         }
203
204 return_results:;
205         /* free entry and reader lock */
206         bdb2i_cache_return_entry_r( &li->li_cache, e );
207
208         /* front end with send result on success (rc==0) */
209         return( rc );
210 }
211
212
213 int
214 bdb2_back_bind(
215     BackendDB           *be,
216     Connection          *conn,
217     Operation           *op,
218     char                *dn,
219     int                 method,
220     struct berval       *cred,
221         char**  edn
222 )
223 {
224         DB_LOCK         lock;
225         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
226         struct timeval  time1;
227         int             ret;
228
229         bdb2i_start_timing( be->bd_info, &time1 );
230
231         if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
232
233                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
234                 return( 1 );
235
236         }
237
238         ret = bdb2i_back_bind_internal( be, conn, op, dn, method, cred, edn );
239
240         (void) bdb2i_leave_backend_r( lock );
241
242         bdb2i_stop_timing( be->bd_info, time1, "BIND", conn, op );
243
244         return( ret );
245 }
246
247