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 */
26 extern Attribute *attr_find();
29 extern int krbv4_ldap_auth();
33 pthread_mutex_t crypt_mutex;
45 for ( i = 0; vals[i] != NULL; i++ ) {
46 if ( syntax != SYNTAX_BIN && strncasecmp( "{CRYPT}",
47 vals[i]->bv_val, (sizeof("{CRYPT}") - 1 ) ) == 0 ) {
48 char *userpassword = vals[i]->bv_val + sizeof("{CRYPT}") - 1;
49 pthread_mutex_lock( &crypt_mutex );
50 if (strcmp(userpassword, crypt(cred->bv_val,
51 userpassword)) == 0) {
52 pthread_mutex_unlock( &crypt_mutex );
55 pthread_mutex_unlock( &crypt_mutex );
57 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{MD5}",
58 vals[i]->bv_val, (sizeof("{MD5}") - 1 ) ) == 0 ) {
59 ldap_MD5_CTX MD5context;
60 unsigned char MD5digest[20];
61 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
63 char *userpassword = vals[i]->bv_val + sizeof("{MD5}") - 1;
65 ldap_MD5Init(&MD5context);
66 ldap_MD5Update(&MD5context, cred->bv_val, 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, cred->bv_val, strlen(cred->bv_val));
90 ldap_SHA1Final(SHA1digest, &SHA1context);
92 if (b64_ntop(SHA1digest, sizeof(SHA1digest),
93 base64digest, sizeof(base64digest)) < 0)
98 if (strcmp(userpassword, base64digest) == 0) {
101 #endif /* SLAPD_SHA1 */
103 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
111 #endif /* SLAPD_CRYPT */
123 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
127 char *matched = NULL;
129 char krbname[MAX_K_NAME_SZ + 1];
133 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
135 /* get entry with reader lock */
136 if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
137 /* allow noauth binds */
138 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
140 * bind successful, but return 1 so we don't
141 * authorize based on noauth credentials
143 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
145 } else if ( be_isroot_pw( be, dn, cred ) ) {
146 /* front end will send result */
149 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
152 if ( matched != NULL ) {
158 /* check for deleted */
161 case LDAP_AUTH_SIMPLE:
162 if ( cred->bv_len == 0 ) {
163 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
165 /* stop front end from sending result */
168 } else if ( be_isroot_pw( be, dn, cred ) ) {
169 /* front end will send result */
174 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
175 if ( be_isroot_pw( be, dn, cred ) ) {
176 /* front end will send result */
180 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
187 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
189 if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
192 if ( be_isroot_pw( be, dn, cred ) ) {
193 /* front end will send result */
197 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
206 case LDAP_AUTH_KRBV41:
207 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
208 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
213 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
214 : "", ad.pinst, ad.prealm );
215 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
217 * no krbName values present: check against DN
219 if ( strcasecmp( dn, krbname ) == 0 ) {
220 rc = 0; /* XXX wild ass guess */
223 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
227 } else { /* look for krbName match */
228 struct berval krbval;
230 krbval.bv_val = krbname;
231 krbval.bv_len = strlen( krbname );
233 if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
234 send_ldap_result( conn, op,
235 LDAP_INVALID_CREDENTIALS, NULL, NULL );
242 case LDAP_AUTH_KRBV42:
243 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
244 /* stop front end from sending result */
250 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
251 NULL, "auth method not supported" );
257 /* free entry and reader lock */
258 cache_return_entry_r( &li->li_cache, e );
260 /* front end with send result on success (rc==0) */