]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
Apply devel IDL fixes...
[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
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 int
61 ldbm_back_bind(
62     Backend             *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, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
82
83         *edn = NULL;
84
85         /* get entry with reader lock */
86         if ( (e = 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                 } else if ( be_isroot_pw( be, dn, cred ) ) {
122                         /* front end will send result */
123                         if( *edn != NULL ) free( *edn );
124                         *edn = ch_strdup( be_root_dn( be ) );
125                         rc = 0;
126                         goto return_results;
127                 }
128
129                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
130                         if ( be_isroot_pw( be, dn, cred ) ) {
131                                 /* front end will send result */
132                                 if( *edn != NULL ) free( *edn );
133                                 *edn = ch_strdup( be_root_dn( be ) );
134                                 rc = 0;
135                                 goto return_results;
136                         }
137                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
138                             NULL, NULL );
139                         rc = 1;
140                         goto return_results;
141                 }
142
143                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
144                 {
145                         if ( be_isroot_pw( be, dn, cred ) ) {
146                                 /* front end will send result */
147                                 if( *edn != NULL ) free( *edn );
148                                 *edn = ch_strdup( be_root_dn( be ) );
149                                 rc = 0;
150                                 goto return_results;
151                         }
152                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
153                                 NULL, NULL );
154                         rc = 1;
155                         goto return_results;
156                 }
157                 rc = 0;
158                 break;
159
160 #ifdef HAVE_KERBEROS
161         case LDAP_AUTH_KRBV41:
162                 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
163                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
164                             NULL, NULL );
165                         rc = 0;
166                         goto return_results;
167                 }
168                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
169                     : "", ad.pinst, ad.prealm );
170                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
171                         /*
172                          * no krbName values present:  check against DN
173                          */
174                         if ( strcasecmp( dn, krbname ) == 0 ) {
175                                 rc = 0; /* XXX wild ass guess */
176                                 break;
177                         }
178                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
179                             NULL, NULL );
180                         rc = 1;
181                         goto return_results;
182                 } else {        /* look for krbName match */
183                         struct berval   krbval;
184
185                         krbval.bv_val = krbname;
186                         krbval.bv_len = strlen( krbname );
187
188                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
189                                 send_ldap_result( conn, op,
190                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
191                                 rc = 1;
192                                 goto return_results;
193                         }
194                 }
195                 rc = 0;
196                 break;
197
198         case LDAP_AUTH_KRBV42:
199                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
200                 /* stop front end from sending result */
201                 rc = 1;
202                 goto return_results;
203 #endif
204
205         default:
206                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
207                     NULL, "auth method not supported" );
208                 rc = 1;
209                 goto return_results;
210         }
211
212 return_results:;
213         /* free entry and reader lock */
214         cache_return_entry_r( &li->li_cache, e );
215
216         /* front end with send result on success (rc==0) */
217         return( rc );
218 }
219