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