1 /* bind.c - ldbm backend bind and unbind routines */
10 #include <ac/unistd.h>
13 #include "back-ldbm.h"
14 #include "proto-back-ldbm.h"
17 #include <lutil_sha1.h>
18 #endif /* SLAPD_SHA1 */
21 #include <lutil_md5.h>
22 #endif /* SLAPD_MD5 */
27 extern int krbv4_ldap_auth();
31 pthread_mutex_t crypt_mutex;
43 for ( i = 0; vals[i] != NULL; i++ ) {
44 if ( syntax != SYNTAX_BIN && strncasecmp( "{CRYPT}",
45 vals[i]->bv_val, (sizeof("{CRYPT}") - 1 ) ) == 0 ) {
46 char *userpassword = vals[i]->bv_val + sizeof("{CRYPT}") - 1;
47 pthread_mutex_lock( &crypt_mutex );
48 if (strcmp(userpassword, crypt(cred->bv_val,
49 userpassword)) == 0) {
50 pthread_mutex_unlock( &crypt_mutex );
53 pthread_mutex_unlock( &crypt_mutex );
55 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{MD5}",
56 vals[i]->bv_val, (sizeof("{MD5}") - 1 ) ) == 0 ) {
57 ldap_MD5_CTX MD5context;
58 unsigned char MD5digest[20];
59 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
61 char *userpassword = vals[i]->bv_val + sizeof("{MD5}") - 1;
63 ldap_MD5Init(&MD5context);
64 ldap_MD5Update(&MD5context,
65 (unsigned char *) cred->bv_val,
66 strlen(cred->bv_val));
67 ldap_MD5Final(MD5digest, &MD5context);
69 if (b64_ntop(MD5digest, sizeof(MD5digest),
70 base64digest, sizeof(base64digest)) < 0)
75 if (strcmp(userpassword, base64digest) == 0) {
78 #endif /* SLAPD_MD5 */
80 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{SHA}",
81 vals[i]->bv_val, (sizeof("{SHA}") - 1 ) ) == 0 ) {
82 ldap_SHA1_CTX SHA1context;
83 unsigned char SHA1digest[20];
84 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
86 char *userpassword = vals[i]->bv_val + sizeof("{SHA}") - 1;
88 ldap_SHA1Init(&SHA1context);
89 ldap_SHA1Update(&SHA1context,
90 (unsigned char *) cred->bv_val,
91 strlen(cred->bv_val));
92 ldap_SHA1Final(SHA1digest, &SHA1context);
94 if (b64_ntop(SHA1digest, sizeof(SHA1digest),
95 base64digest, sizeof(base64digest)) < 0)
100 if (strcmp(userpassword, base64digest) == 0) {
103 #endif /* SLAPD_SHA1 */
105 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
113 #endif /* SLAPD_CRYPT */
125 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131 char krbname[MAX_K_NAME_SZ + 1];
135 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
137 /* get entry with reader lock */
138 if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
139 /* allow noauth binds */
140 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
142 * bind successful, but return 1 so we don't
143 * authorize based on noauth credentials
145 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
147 } else if ( be_isroot_pw( be, dn, cred ) ) {
148 /* front end will send result */
151 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
154 if ( matched != NULL ) {
160 /* check for deleted */
163 case LDAP_AUTH_SIMPLE:
164 if ( cred->bv_len == 0 ) {
165 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
167 /* stop front end from sending result */
170 } else if ( be_isroot_pw( be, dn, cred ) ) {
171 /* front end will send result */
176 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
177 if ( be_isroot_pw( be, dn, cred ) ) {
178 /* front end will send result */
182 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
189 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
191 if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
194 if ( be_isroot_pw( be, dn, cred ) ) {
195 /* front end will send result */
199 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
208 case LDAP_AUTH_KRBV41:
209 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
210 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
215 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
216 : "", ad.pinst, ad.prealm );
217 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
219 * no krbName values present: check against DN
221 if ( strcasecmp( dn, krbname ) == 0 ) {
222 rc = 0; /* XXX wild ass guess */
225 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
229 } else { /* look for krbName match */
230 struct berval krbval;
232 krbval.bv_val = krbname;
233 krbval.bv_len = strlen( krbname );
235 if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
236 send_ldap_result( conn, op,
237 LDAP_INVALID_CREDENTIALS, NULL, NULL );
244 case LDAP_AUTH_KRBV42:
245 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
246 /* stop front end from sending result */
252 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
253 NULL, "auth method not supported" );
259 /* free entry and reader lock */
260 cache_return_entry_r( &li->li_cache, e );
262 /* front end with send result on success (rc==0) */