1 /* bind.c - ldbm backend bind and unbind routines */
6 #include <sys/socket.h>
9 #include "proto-back-ldbm.h"
12 #include <kerberosIV/krb.h>
15 #endif /* KERBEROS_V */
19 /* change for crypted passwords -- lukeh */
21 extern char *crypt (char *key, char *salt);
25 #endif /* LDAP_CRYPT */
28 #include <lutil_sha1.h>
29 #endif /* LDAP_SHA1 */
31 #include <lutil_md5.h>
36 extern Attribute *attr_find();
39 extern int krbv4_ldap_auth();
43 pthread_mutex_t crypt_mutex;
55 for ( i = 0; vals[i] != NULL; i++ ) {
56 if ( syntax != SYNTAX_BIN && strncasecmp( "{CRYPT}",
57 vals[i]->bv_val, (sizeof("{CRYPT}") - 1 ) ) == 0 ) {
58 char *userpassword = vals[i]->bv_val + sizeof("{CRYPT}") - 1;
59 pthread_mutex_lock( &crypt_mutex );
60 if (strcmp(userpassword, crypt(cred->bv_val,
61 userpassword)) == 0) {
62 pthread_mutex_unlock( &crypt_mutex );
65 pthread_mutex_unlock( &crypt_mutex );
67 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{MD5}",
68 vals[i]->bv_val, (sizeof("{MD5}") - 1 ) ) == 0 ) {
69 ldap_MD5_CTX MD5context;
70 unsigned char MD5digest[20];
71 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
73 char *userpassword = vals[i]->bv_val + sizeof("{MD5}") - 1;
75 ldap_MD5Init(&MD5context);
76 ldap_MD5Update(&MD5context, cred->bv_val, strlen(cred->bv_val));
77 ldap_MD5Final(MD5digest, &MD5context);
79 if (b64_ntop(MD5digest, sizeof(MD5digest),
80 base64digest, sizeof(base64digest)) < 0)
85 if (strcmp(userpassword, base64digest) == 0) {
90 } else if ( syntax != SYNTAX_BIN && strncasecmp( "{SHA}",
91 vals[i]->bv_val, (sizeof("{SHA}") - 1 ) ) == 0 ) {
92 ldap_SHA1_CTX SHA1context;
93 unsigned char SHA1digest[20];
94 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
96 char *userpassword = vals[i]->bv_val + sizeof("{SHA}") - 1;
98 ldap_SHA1Init(&SHA1context);
99 ldap_SHA1Update(&SHA1context, cred->bv_val, strlen(cred->bv_val));
100 ldap_SHA1Final(SHA1digest, &SHA1context);
102 if (b64_ntop(SHA1digest, sizeof(SHA1digest),
103 base64digest, sizeof(base64digest)) < 0)
108 if (strcmp(userpassword, base64digest) == 0) {
111 #endif /* LDAP_SHA1 */
113 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
121 #endif /* LDAP_CRYPT */
133 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
137 char *matched = NULL;
139 char krbname[MAX_K_NAME_SZ + 1];
143 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
145 /* get entry with reader lock */
146 if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
147 /* allow noauth binds */
148 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
150 * bind successful, but return 1 so we don't
151 * authorize based on noauth credentials
153 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
155 } else if ( be_isroot_pw( be, dn, cred ) ) {
156 /* front end will send result */
159 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
162 if ( matched != NULL ) {
168 /* check for deleted */
171 case LDAP_AUTH_SIMPLE:
172 if ( cred->bv_len == 0 ) {
173 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
175 /* stop front end from sending result */
178 } else if ( be_isroot_pw( be, dn, cred ) ) {
179 /* front end will send result */
184 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
185 if ( be_isroot_pw( be, dn, cred ) ) {
186 /* front end will send result */
190 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
197 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
199 if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
202 if ( be_isroot_pw( be, dn, cred ) ) {
203 /* front end will send result */
207 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
216 case LDAP_AUTH_KRBV41:
217 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
218 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
223 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
224 : "", ad.pinst, ad.prealm );
225 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
227 * no krbName values present: check against DN
229 if ( strcasecmp( dn, krbname ) == 0 ) {
230 rc = 0; /* XXX wild ass guess */
233 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
237 } else { /* look for krbName match */
238 struct berval krbval;
240 krbval.bv_val = krbname;
241 krbval.bv_len = strlen( krbname );
243 if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
244 send_ldap_result( conn, op,
245 LDAP_INVALID_CREDENTIALS, NULL, NULL );
252 case LDAP_AUTH_KRBV42:
253 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
254 /* stop front end from sending result */
260 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
261 NULL, "auth method not supported" );
267 /* free entry and reader lock */
268 cache_return_entry_r( &li->li_cache, e );
270 /* front end with send result on success (rc==0) */