1 /* bind.c - ldbm backend bind and unbind routines */
6 #include <sys/socket.h>
11 #include <kerberosIV/krb.h>
14 #endif /* KERBEROS_V */
18 /* change for crypted passwords -- lukeh */
20 extern char *crypt (char *key, char *salt);
24 #endif /* LDAP_CRYPT */
27 #include <lutil_sha1.h>
28 #endif /* LDAP_SHA1 */
30 #include <lutil_md5.h>
35 extern Entry *dn2entry();
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 if ( (e = dn2entry( be, dn, &matched )) == NULL ) {
144 /* allow noauth binds */
145 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
147 * bind successful, but return 1 so we don't
148 * authorize based on noauth credentials
150 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
152 } else if ( be_isroot_pw( be, dn, cred ) ) {
153 /* front end will send result */
156 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
160 if ( matched != NULL ) {
167 case LDAP_AUTH_SIMPLE:
168 if ( cred->bv_len == 0 ) {
169 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
171 } else if ( be_isroot_pw( be, dn, cred ) ) {
172 /* 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 */
181 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
183 cache_return_entry( &li->li_cache, e );
188 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
190 if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
193 if ( be_isroot_pw( be, dn, cred ) ) {
194 /* front end will send result */
197 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
199 cache_return_entry( &li->li_cache, e );
205 case LDAP_AUTH_KRBV41:
206 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
207 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
209 cache_return_entry( &li->li_cache, e );
212 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
213 : "", ad.pinst, ad.prealm );
214 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
216 * no krbName values present: check against DN
218 if ( strcasecmp( dn, krbname ) == 0 ) {
221 send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
223 cache_return_entry( &li->li_cache, e );
225 } else { /* look for krbName match */
226 struct berval krbval;
228 krbval.bv_val = krbname;
229 krbval.bv_len = strlen( krbname );
231 if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 )
233 send_ldap_result( conn, op,
234 LDAP_INVALID_CREDENTIALS, NULL, NULL );
235 cache_return_entry( &li->li_cache, e );
241 case LDAP_AUTH_KRBV42:
242 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
243 cache_return_entry( &li->li_cache, e );
248 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
249 NULL, "auth method not supported" );
250 cache_return_entry( &li->li_cache, e );
254 cache_return_entry( &li->li_cache, e );
256 /* success: front end will send result */