]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/bind.c
Rename
[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                 if ( be_isroot_pw( be, dn, cred ) ) {
124                         /* front end will send result */
125                         if( *edn != NULL ) free( *edn );
126                         *edn = ch_strdup( be_root_dn( be ) );
127                         rc = 0;
128                         goto return_results;
129                 }
130
131                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
132                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
133                             NULL, NULL );
134                         rc = 1;
135                         goto return_results;
136                 }
137
138                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
139                 {
140                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
141                                 NULL, NULL );
142                         rc = 1;
143                         goto return_results;
144                 }
145                 rc = 0;
146                 break;
147
148 #ifdef HAVE_KERBEROS
149         case LDAP_AUTH_KRBV41:
150                 if ( bdb2i_krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
151                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
152                             NULL, NULL );
153                         rc = 0;
154                         goto return_results;
155                 }
156                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
157                     : "", ad.pinst, ad.prealm );
158                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
159                         /*
160                          * no krbName values present:  check against DN
161                          */
162                         if ( strcasecmp( dn, krbname ) == 0 ) {
163                                 rc = 0; /* XXX wild ass guess */
164                                 break;
165                         }
166                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
167                             NULL, NULL );
168                         rc = 1;
169                         goto return_results;
170                 } else {        /* look for krbName match */
171                         struct berval   krbval;
172
173                         krbval.bv_val = krbname;
174                         krbval.bv_len = strlen( krbname );
175
176                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
177                                 send_ldap_result( conn, op,
178                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
179                                 rc = 1;
180                                 goto return_results;
181                         }
182                 }
183                 rc = 0;
184                 break;
185
186         case LDAP_AUTH_KRBV42:
187                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
188                 /* stop front end from sending result */
189                 rc = 1;
190                 goto return_results;
191 #endif
192
193         default:
194                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
195                     NULL, "auth method not supported" );
196                 rc = 1;
197                 goto return_results;
198         }
199
200 return_results:;
201         /* free entry and reader lock */
202         bdb2i_cache_return_entry_r( &li->li_cache, e );
203
204         /* front end with send result on success (rc==0) */
205         return( rc );
206 }
207
208
209 int
210 bdb2_back_bind(
211     BackendDB           *be,
212     Connection          *conn,
213     Operation           *op,
214     char                *dn,
215     int                 method,
216     struct berval       *cred,
217         char**  edn
218 )
219 {
220         DB_LOCK         lock;
221         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
222         struct timeval  time1;
223         int             ret;
224
225         bdb2i_start_timing( be->bd_info, &time1 );
226
227         if ( bdb2i_enter_backend_r( get_dbenv( be ), &lock ) != 0 ) {
228
229                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
230                 return( 1 );
231
232         }
233
234         ret = bdb2i_back_bind_internal( be, conn, op, dn, method, cred, edn );
235
236         (void) bdb2i_leave_backend_r( get_dbenv( be ), lock );
237
238         bdb2i_stop_timing( be->bd_info, time1, "BIND", conn, op );
239
240         return( ret );
241 }
242
243