]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
f176b4de7e15815aa562f14e9397d9364b74e969
[openldap] / servers / slapd / back-ldbm / bind.c
1 /* bind.c - ldbm 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-ldbm.h"
14 #include "proto-back-ldbm.h"
15
16 #include <lutil.h>
17
18 #ifdef HAVE_KERBEROS
19 extern int      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                                 NULL );
44
45 #ifdef SLAPD_CRYPT
46                         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
47 #endif
48
49                         return result;
50
51                 } else {
52                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
53                         return( 0 );
54                 }
55         }
56         }
57
58         return( 1 );
59 }
60
61 int
62 ldbm_back_bind(
63     Backend             *be,
64     Connection          *conn,
65     Operation           *op,
66     char                *dn,
67     int                 method,
68         char            *mech,
69     struct berval       *cred,
70         char**  edn
71 )
72 {
73         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
74         Entry           *e;
75         Attribute       *a;
76         int             rc;
77         char            *matched;
78 #ifdef HAVE_KERBEROS
79         char            krbname[MAX_K_NAME_SZ + 1];
80         AUTH_DAT        ad;
81 #endif
82
83         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
84
85         *edn = NULL;
86
87         /* get entry with reader lock */
88         if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
89                 /* allow noauth binds */
90                 rc = 1;
91                 if ( method == LDAP_AUTH_SIMPLE ) {
92                         if( cred->bv_len == 0 ) {
93                                 /* SUCCESS */
94                                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
95
96                         } else if ( be_isroot_pw( be, dn, cred ) ) {
97                                 *edn = ch_strdup( be_root_dn( be ) );
98                                 rc = 0; /* front end will send result */
99
100                         } else {
101                                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
102                         }
103
104                 } else if ( method == LDAP_AUTH_SASL ) {
105                         if( mech != NULL && strcasecmp(mech,"DIGEST-MD5") ) {
106                                 /* insert DIGEST calls here */
107                                 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH, NULL, NULL );
108                                 
109                         } else {
110                                 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH, NULL, NULL );
111                         }
112
113                 } else {
114                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
115                 }
116
117                 if ( matched != NULL ) {
118                         free( matched );
119                 }
120                 return( rc );
121         }
122
123         *edn = ch_strdup( e->e_dn );
124
125         /* check for deleted */
126
127 #ifdef SLAPD_ACLAUTH
128         if ( ! access_allowed( be, conn, op, e,
129                 "entry", NULL, ACL_AUTH ) )
130         {
131                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
132                 rc = 1;
133                 goto return_results;
134         }
135 #endif
136
137         switch ( method ) {
138         case LDAP_AUTH_SIMPLE:
139                 if ( cred->bv_len == 0 ) {
140                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
141
142                         /* stop front end from sending result */
143                         rc = 1;
144                         goto return_results;
145                 } 
146
147                 /* check for root dn/passwd */
148                 if ( be_isroot_pw( be, dn, cred ) ) {
149                         /* front end will send result */
150                         if(*edn != NULL) free( *edn );
151                         *edn = ch_strdup( be_root_dn( be ) );
152                         rc = 0;
153                         goto return_results;
154                 }
155
156 #ifdef SLAPD_ACLAUTH
157                 if ( ! access_allowed( be, conn, op, e,
158                         "userpassword", NULL, ACL_AUTH ) )
159                 {
160                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
161                         rc = 1;
162                         goto return_results;
163                 }
164 #endif
165
166                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
167                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
168                             NULL, NULL );
169
170                         /* stop front end from sending result */
171                         rc = 1;
172                         goto return_results;
173                 }
174
175                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
176                 {
177                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
178                                 NULL, NULL );
179                         /* stop front end from sending result */
180                         rc = 1;
181                         goto return_results;
182                 }
183
184                 rc = 0;
185                 break;
186
187 #ifdef HAVE_KERBEROS
188         case LDAP_AUTH_KRBV41:
189 #ifdef SLAPD_ACLAUTH
190                 if ( ! access_allowed( be, conn, op, e,
191                         "krbname", NULL, ACL_AUTH ) )
192                 {
193                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
194                         rc = 1;
195                         goto return_results;
196                 }
197 #endif
198                 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
199                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
200                             NULL, NULL );
201                         rc = 0;
202                         goto return_results;
203                 }
204                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
205                     : "", ad.pinst, ad.prealm );
206                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
207                         /*
208                          * no krbName values present:  check against DN
209                          */
210                         if ( strcasecmp( dn, krbname ) == 0 ) {
211                                 rc = 0;
212                                 break;
213                         }
214                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
215                             NULL, NULL );
216                         rc = 1;
217                         goto return_results;
218
219                 } else {        /* look for krbName match */
220                         struct berval   krbval;
221
222                         krbval.bv_val = krbname;
223                         krbval.bv_len = strlen( krbname );
224
225                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
226                                 send_ldap_result( conn, op,
227                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
228                                 rc = 1;
229                                 goto return_results;
230                         }
231                 }
232                 rc = 0;
233                 break;
234
235         case LDAP_AUTH_KRBV42:
236                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
237                 /* stop front end from sending result */
238                 rc = 1;
239                 goto return_results;
240 #endif
241
242         default:
243                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
244                     NULL, "auth method not supported" );
245                 rc = 1;
246                 goto return_results;
247         }
248
249 return_results:;
250         /* free entry and reader lock */
251         cache_return_entry_r( &li->li_cache, e );
252
253         /* front end with send result on success (rc==0) */
254         return( rc );
255 }
256