]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
Minor adjustments to better handle NT vs UNIX.
[openldap] / servers / slapd / back-ldbm / bind.c
1 /* bind.c - ldbm backend bind and unbind routines */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/krb.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include "slap.h"
13 #include "back-ldbm.h"
14 #include "proto-back-ldbm.h"
15
16 #include <lutil.h>
17
18 #ifdef HAVE_KERBEROS
19 extern int      krbv4_ldap_auth();
20 #endif
21
22 static int
23 crypted_value_find(
24         struct berval       **vals,
25         struct berval       *v,
26         int                 syntax,
27         int                 normalize,
28         struct berval           *cred
29 )
30 {
31         int     i;
32         for ( i = 0; vals[i] != NULL; i++ ) {
33                 if ( syntax != SYNTAX_BIN ) {
34                         int result;
35
36 #ifdef SLAPD_CRYPT
37                         ldap_pvt_thread_mutex_lock( &crypt_mutex );
38 #endif
39
40                         result = lutil_passwd(
41                                 (char*) cred->bv_val,
42                                 (char*) vals[i]->bv_val);
43
44 #ifdef SLAPD_CRYPT
45                         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
46 #endif
47
48                         return result;
49
50                 } else {
51                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
52                         return( 0 );
53                 }
54         }
55         }
56
57         return( 1 );
58 }
59
60 int
61 ldbm_back_bind(
62     Backend             *be,
63     Connection          *conn,
64     Operation           *op,
65     char                *dn,
66     int                 method,
67     struct berval       *cred,
68         char**  edn
69 )
70 {
71         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
72         Entry           *e;
73         Attribute       *a;
74         int             rc;
75         char            *matched;
76 #ifdef HAVE_KERBEROS
77         char            krbname[MAX_K_NAME_SZ + 1];
78         AUTH_DAT        ad;
79 #endif
80
81         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", dn, 0, 0);
82
83         *edn = NULL;
84
85         /* get entry with reader lock */
86         if ( (e = dn2entry_r( be, dn, &matched )) == NULL ) {
87                 /* allow noauth binds */
88                 if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
89                         /*
90                          * bind successful, but return 1 so we don't
91                          * authorize based on noauth credentials
92                          */
93                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
94                         rc = 1;
95                 } else if ( be_isroot_pw( be, dn, cred ) ) {
96                         /* front end will send result */
97                         *edn = ch_strdup( be_root_dn( be ) );
98                         rc = 0;
99                 } else {
100                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, NULL );
101                         rc = 1;
102                 }
103                 if ( matched != NULL ) {
104                         free( matched );
105                 }
106                 return( rc );
107         }
108
109         *edn = ch_strdup( e->e_dn );
110
111         /* check for deleted */
112
113         switch ( method ) {
114         case LDAP_AUTH_SIMPLE:
115                 if ( cred->bv_len == 0 ) {
116                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
117
118                         /* stop front end from sending result */
119                         rc = 1;
120                         goto return_results;
121                 } 
122
123                 /* check for root dn/passwd */
124                 if ( be_isroot_pw( be, dn, cred ) ) {
125                         /* front end will send result */
126                         if(*edn != NULL) free( *edn );
127                         *edn = ch_strdup( be_root_dn( be ) );
128                         rc = 0;
129                         goto return_results;
130                 }
131
132                 if ( (a = attr_find( e->e_attrs, "userpassword" )) == NULL ) {
133                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
134                             NULL, NULL );
135
136                         /* stop front end from sending result */
137                         rc = 1;
138                         goto return_results;
139                 }
140
141                 if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
142                 {
143                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
144                                 NULL, NULL );
145                         /* stop front end from sending result */
146                         rc = 1;
147                         goto return_results;
148                 }
149
150                 rc = 0;
151                 break;
152
153 #ifdef HAVE_KERBEROS
154         case LDAP_AUTH_KRBV41:
155                 if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
156                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
157                             NULL, NULL );
158                         rc = 0;
159                         goto return_results;
160                 }
161                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
162                     : "", ad.pinst, ad.prealm );
163                 if ( (a = attr_find( e->e_attrs, "krbname" )) == NULL ) {
164                         /*
165                          * no krbName values present:  check against DN
166                          */
167                         if ( strcasecmp( dn, krbname ) == 0 ) {
168                                 rc = 0; /* XXX wild ass guess */
169                                 break;
170                         }
171                         send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH,
172                             NULL, NULL );
173                         rc = 1;
174                         goto return_results;
175
176                 } else {        /* look for krbName match */
177                         struct berval   krbval;
178
179                         krbval.bv_val = krbname;
180                         krbval.bv_len = strlen( krbname );
181
182                         if ( value_find( a->a_vals, &krbval, a->a_syntax, 3 ) != 0 ) {
183                                 send_ldap_result( conn, op,
184                                     LDAP_INVALID_CREDENTIALS, NULL, NULL );
185                                 rc = 1;
186                                 goto return_results;
187                         }
188                 }
189                 rc = 0;
190                 break;
191
192         case LDAP_AUTH_KRBV42:
193                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
194                 /* stop front end from sending result */
195                 rc = 1;
196                 goto return_results;
197 #endif
198
199         default:
200                 send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
201                     NULL, "auth method not supported" );
202                 rc = 1;
203                 goto return_results;
204         }
205
206 return_results:;
207         /* free entry and reader lock */
208         cache_return_entry_r( &li->li_cache, e );
209
210         /* front end with send result on success (rc==0) */
211         return( rc );
212 }
213