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