3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/socket.h>
19 #include <ac/string.h> /* get SAFEMEMCPY */
21 #include <quipu/commonarg.h>
22 #include <quipu/attrvalue.h>
23 #include <quipu/ds_error.h>
24 #include <quipu/bind.h>
25 #include <quipu/compare.h>
32 #define BINDTAG (ldap_compat == 20 ? OLD_LDAP_RES_BIND : LDAP_RES_BIND)
34 #define BINDTAG LDAP_RES_BIND
38 * do_bind - perform an X.500 bind operation. Since we always respond
39 * to the request in here, always return 0 to signify the incoming message
56 struct PSAPaddr *addr;
58 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
61 * Parse the bind request. It looks like this:
62 * BindRequest ::= SEQUENCE {
63 * version INTEGER, -- version
64 * name DistinguishedName, -- dn
65 * authentication CHOICE {
66 * simple [0] OCTET STRING -- passwd
67 * krbv42ldap [1] OCTET STRING
68 * krbv42dsa [1] OCTET STRING
73 if ( ber_scanf( ber, "{ia", &version, &dn ) == LBER_ERROR ) {
74 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
75 send_ldap_msgresult( clientsb, BINDTAG, m,
76 LDAP_PROTOCOL_ERROR, NULL, "Decoding error" );
80 if ( ldap_compat == 30 )
81 method = ber_skip_tag( ber, &len );
84 method = ber_peek_tag( ber, &len );
86 if ( ber_scanf( ber, "la}", &len, &pw ) == LBER_ERROR ) {
87 Debug( LDAP_DEBUG_ANY, "ber_scanf2 failed\n", 0, 0, 0 );
88 send_ldap_msgresult( clientsb, BINDTAG, m,
89 LDAP_PROTOCOL_ERROR, NULL, "Decoding error" );
93 if ( version != LDAP_VERSION1 && version != LDAP_VERSION2 ) {
94 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
95 send_ldap_msgresult( clientsb, BINDTAG, m,
96 LDAP_PROTOCOL_ERROR, NULL, "Version not supported" );
100 Debug( LDAP_DEBUG_ARGS, "do_bind: version %d dn (%s) method %lu\n",
101 version, dn, method );
103 if ( m->m_conn->c_paddr == NULLPA ) {
106 sprintf( buf, "Bad DSA address (%s)", dsa_address ?
107 dsa_address : "NULL" );
108 send_ldap_msgresult( clientsb, BINDTAG, m,
109 LDAP_OPERATIONS_ERROR, NULL, buf );
113 if ( m->m_conn->c_dn )
114 free( m->m_conn->c_dn );
115 if ( m->m_conn->c_cred )
116 free( m->m_conn->c_cred );
117 m->m_conn->c_dn = dn;
118 m->m_conn->c_cred = pw;
119 m->m_conn->c_credlen = len;
120 m->m_conn->c_method = method;
122 err = do_bind_real( m->m_conn, bound, &matched );
124 send_ldap_msgresult( clientsb, BINDTAG, m, err, matched, "" );
126 if ( matched != NULL )
134 struct conn *dsaconn,
139 struct ds_bind_arg ba;
140 struct ds_bind_arg br;
141 struct ds_bind_error be;
143 char *dn = dsaconn->c_dn;
148 Debug( LDAP_DEBUG_TRACE, "do_bind_real\n", 0, 0, 0 );
151 if ( (ba.dba_dn = ldap_str2dn( dn )) == NULLDN && *dn != '\0' ) {
152 Debug( LDAP_DEBUG_ANY, "ldap_str2dn (%s) failed\n", dn, 0, 0 );
153 return( LDAP_INVALID_DN_SYNTAX );
156 switch ( dsaconn->c_method ) {
158 case OLD_LDAP_AUTH_SIMPLE:
161 case LDAP_AUTH_SIMPLE_30:
163 case LDAP_AUTH_SIMPLE: /* x.500 simple authentication */
164 if ( dsaconn->c_credlen > DBA_MAX_PASSWD_LEN ) {
165 Debug( LDAP_DEBUG_ANY, "Password too long\n", 0, 0, 0 );
166 return( LDAP_INAPPROPRIATE_AUTH );
168 if (( ba.dba_passwd_len = dsaconn->c_credlen ) > 0 ) {
169 SAFEMEMCPY( ba.dba_passwd, dsaconn->c_cred,
171 ba.dba_auth_type = DBA_AUTH_SIMPLE;
173 ba.dba_auth_type = DBA_AUTH_NONE;
175 ba.dba_version = DBA_VERSION_V1988;
180 case OLD_LDAP_AUTH_KRBV4:
183 case LDAP_AUTH_KRBV41_30:
185 case LDAP_AUTH_KRBV41: /* kerberos authentication to ldap server */
186 return( kerberosv4_ldap_auth( dsaconn->c_cred,
187 dsaconn->c_credlen ) );
191 case OLD_LDAP_AUTH_KRBV42:
194 case LDAP_AUTH_KRBV42_30:
196 case LDAP_AUTH_KRBV42: /* kerberos authentication to x500 dsa */
197 if ( (err = kerberosv4_bindarg( &ba, ba.dba_dn, dsaconn->c_cred,
198 dsaconn->c_credlen, &nonce )) != 0 )
204 return( LDAP_PROTOCOL_ERROR );
208 if ( dsaconn->c_ad != -1 )
209 dap_unbind( dsaconn->c_ad );
211 Debug( LDAP_DEBUG_TRACE, "dap_bind to dsa (%s)...\n", paddr2str(
212 dsaconn->c_paddr, NULLNA ), 0, 0 );
214 err = dap_bind( &dsaconn->c_ad, &ba, &be, &br, dsaconn->c_paddr );
216 if ( err != DS_OK && ba.dba_dn != NULLDN && ba.dba_auth_type
217 == DBA_AUTH_NONE && be.dbe_type == DBE_TYPE_SECURITY ) {
218 /* if doing a NULL bind, retry with a NULL dn */
219 Debug( LDAP_DEBUG_TRACE, "retring NULL dap_bind\n", 0, 0, 0 );
220 dn_free( ba.dba_dn );
222 err = dap_bind( &dsaconn->c_ad, &ba, &be, &br,
226 if ( err != DS_OK ) {
227 if ( ba.dba_dn != NULLDN )
228 dn_free( ba.dba_dn );
230 if ( be.dbe_type == DBE_TYPE_SERVICE ) {
231 dse.dse_type = DSE_SERVICEERROR;
232 dse.ERR_SERVICE.DSE_sv_problem = be.dbe_value;
233 } else if ( be.dbe_type == DBE_TYPE_SECURITY ) {
234 dse.dse_type = DSE_SECURITYERROR;
235 dse.ERR_SECURITY.DSE_sc_problem = be.dbe_value;
237 dse.dse_type = DSE_REMOTEERROR;
239 err = x500err2ldaperr( &dse, matched );
243 print_error( &dse ); /* prints and then frees */
246 ds_error_free( &dse );
252 bind_arg_free( &br );
254 Debug( LDAP_DEBUG_TRACE, "dap_bind successful\n", 0, 0, 0 );
257 /* XXX why doesn't this work??
258 if ( dsaconn->c_method == LDAP_AUTH_KRBV42 &&
259 kerberos_check_mutual( &br, nonce ) != 0 ) {
260 Debug( LDAP_DEBUG_ANY, "Mutual authentication failed\n", 0, 0,
262 return( LDAP_INVALID_CREDENTIALS );
269 return( LDAP_SUCCESS );