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,
67 (unsigned char *) cred->bv_val,
68 strlen(cred->bv_val));
69 ldap_MD5Final(MD5digest, &MD5context);
71 if (b64_ntop(MD5digest, sizeof(MD5digest),
72 base64digest, sizeof(base64digest)) < 0)
77 if (strcmp(userpassword, base64digest) == 0) {
80 #endif /* SLAPD_MD5 */
82 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{SHA}",
83 vals[i]->bv_val, (sizeof("{SHA}") - 1 ) ) == 0 ) {
84 ldap_SHA1_CTX SHA1context;
85 unsigned char SHA1digest[20];
86 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
88 char *userpassword = vals[i]->bv_val + sizeof("{SHA}") - 1;
90 ldap_SHA1Init(&SHA1context);
91 ldap_SHA1Update(&SHA1context,
92 (unsigned char *) cred->bv_val,
93 strlen(cred->bv_val));
94 ldap_SHA1Final(SHA1digest, &SHA1context);
96 if (b64_ntop(SHA1digest, sizeof(SHA1digest),
97 base64digest, sizeof(base64digest)) < 0)
102 if (strcmp(userpassword, base64digest) == 0) {
105 #endif /* SLAPD_SHA1 */
107 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
115 #endif /* SLAPD_CRYPT */
127 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131 char *matched = NULL;
133 char krbname[MAX_K_NAME_SZ + 1];
137 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
139 /* get entry with reader lock */
140 if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
141 /* allow noauth binds */
142 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
144 * bind successful, but return 1 so we don't
145 * authorize based on noauth credentials
147 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
149 } else if ( be_isroot_pw( be, dn, cred ) ) {
150 /* front end will send result */
153 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
156 if ( matched != NULL ) {
162 /* check for deleted */
165 case LDAP_AUTH_SIMPLE:
166 if ( cred->bv_len == 0 ) {
167 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
169 /* stop front end from sending result */
172 } else if ( be_isroot_pw( be, dn, cred ) ) {
173 /* front end will send result */
178 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
179 if ( be_isroot_pw( be, dn, cred ) ) {
180 /* front end will send result */
184 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
191 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
193 if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
196 if ( be_isroot_pw( be, dn, cred ) ) {
197 /* front end will send result */
201 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
210 case LDAP_AUTH_KRBV41:
211 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
212 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
217 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
218 : "", ad.pinst, ad.prealm );
219 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
221 * no krbName values present: check against DN
223 if ( strcasecmp( dn, krbname ) == 0 ) {
224 rc = 0; /* XXX wild ass guess */
227 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
231 } else { /* look for krbName match */
232 struct berval krbval;
234 krbval.bv_val = krbname;
235 krbval.bv_len = strlen( krbname );
237 if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
238 send_ldap_result( conn, op,
239 LDAP_INVALID_CREDENTIALS, NULL, NULL );
246 case LDAP_AUTH_KRBV42:
247 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
248 /* stop front end from sending result */
254 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
255 NULL, "auth method not supported" );
261 /* free entry and reader lock */
262 cache_return_entry_r( &li->li_cache, e );
264 /* front end with send result on success (rc==0) */