]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
HEADS UP: connections are forced to "anonymous" status upon receiving
[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                 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
91                         /*
92                          * bind successful, but return 1 so we don't
93                          * authorize based on noauth credentials
94                          */
95                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
96                         rc = 1;
97                 } else if ( be_isroot_pw( be, dn, cred ) ) {
98                         /* front end will send result */
99                         *edn = ch_strdup( be_root_dn( be ) );
100                         rc = 0;
101                 } else {
102                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
103                         rc = 1;
104                 }
105                 if ( matched != NULL ) {
106                         free( matched );
107                 }
108                 return( rc );
109         }
110
111         *edn = ch_strdup( e->e_dn );
112
113         /* check for deleted */
114
115 #ifdef SLAPD_ACLAUTH
116         if ( ! access_allowed( be, conn, op, e,
117                 "entry", NULL, ACL_AUTH ) )
118         {
119                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
120                 rc = 1;
121                 goto return_results;
122         }
123 #endif
124
125         switch ( method ) {
126         case LDAP_AUTH_SIMPLE:
127                 if ( cred->bv_len == 0 ) {
128                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
129
130                         /* stop front end from sending result */
131                         rc = 1;
132                         goto return_results;
133                 } 
134
135                 /* check for root dn/passwd */
136                 if ( be_isroot_pw( be, dn, cred ) ) {
137                         /* front end will send result */
138                         if(*edn != NULL) free( *edn );
139                         *edn = ch_strdup( be_root_dn( be ) );
140                         rc = 0;
141                         goto return_results;
142                 }
143
144 #ifdef SLAPD_ACLAUTH
145                 if ( ! access_allowed( be, conn, op, e,
146                         "userpassword", NULL, ACL_AUTH ) )
147                 {
148                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
149                         rc = 1;
150                         goto return_results;
151                 }
152 #endif
153
154                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
155                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
156                             NULL, NULL );
157
158                         /* stop front end from sending result */
159                         rc = 1;
160                         goto return_results;
161                 }
162
163                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
164                 {
165                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
166                                 NULL, NULL );
167                         /* stop front end from sending result */
168                         rc = 1;
169                         goto return_results;
170                 }
171
172                 rc = 0;
173                 break;
174
175 #ifdef HAVE_KERBEROS
176         case LDAP_AUTH_KRBV41:
177 #ifdef SLAPD_ACLAUTH
178                 if ( ! access_allowed( be, conn, op, e,
179                         "krbname", NULL, ACL_AUTH ) )
180                 {
181                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
182                         rc = 1;
183                         goto return_results;
184                 }
185 #endif
186                 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
187                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
188                             NULL, NULL );
189                         rc = 0;
190                         goto return_results;
191                 }
192                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
193                     : "", ad.pinst, ad.prealm );
194                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
195                         /*
196                          * no krbName values present:  check against DN
197                          */
198                         if ( strcasecmp( dn, krbname ) == 0 ) {
199                                 rc = 0;
200                                 break;
201                         }
202                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
203                             NULL, NULL );
204                         rc = 1;
205                         goto return_results;
206
207                 } else {        /* look for krbName match */
208                         struct berval   krbval;
209
210                         krbval.bv_val = krbname;
211                         krbval.bv_len = strlen( krbname );
212
213                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
214                                 send_ldap_result( conn, op,
215                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
216                                 rc = 1;
217                                 goto return_results;
218                         }
219                 }
220                 rc = 0;
221                 break;
222
223         case LDAP_AUTH_KRBV42:
224                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
225                 /* stop front end from sending result */
226                 rc = 1;
227                 goto return_results;
228 #endif
229
230         default:
231                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
232                     NULL, "auth method not supported" );
233                 rc = 1;
234                 goto return_results;
235         }
236
237 return_results:;
238         /* free entry and reader lock */
239         cache_return_entry_r( &li->li_cache, e );
240
241         /* front end with send result on success (rc==0) */
242         return( rc );
243 }
244