xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-sql/bind.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: bind.c,v 1.3 2021/08/14 16:15:01 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2021 The OpenLDAP Foundation.
7  * Portions Copyright 1999 Dmitry Kovalev.
8  * Portions Copyright 2002 Pierangelo Masarati.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This work was initially developed by Dmitry Kovalev for inclusion
21  * by OpenLDAP Software.  Additional significant contributors include
22  * Pierangelo Masarati.
23  */
24 
25 #include <sys/cdefs.h>
26 __RCSID("$NetBSD: bind.c,v 1.3 2021/08/14 16:15:01 christos Exp $");
27 
28 #include "portable.h"
29 
30 #include <stdio.h>
31 #include <sys/types.h>
32 
33 #include "slap.h"
34 #include "proto-sql.h"
35 
36 int
backsql_bind(Operation * op,SlapReply * rs)37 backsql_bind( Operation *op, SlapReply *rs )
38 {
39 	SQLHDBC			dbh = SQL_NULL_HDBC;
40 	Entry			e = { 0 };
41 	Attribute		*a;
42 	backsql_srch_info	bsi = { 0 };
43 	AttributeName		anlist[2];
44 	int			rc;
45 
46 	Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n" );
47 
48 	switch ( be_rootdn_bind( op, rs ) ) {
49 	case SLAP_CB_CONTINUE:
50 		break;
51 
52 	default:
53 		/* in case of success, front end will send result;
54 		 * otherwise, be_rootdn_bind() did */
55  		Debug( LDAP_DEBUG_TRACE, "<==backsql_bind(%d)\n",
56 			rs->sr_err );
57 		return rs->sr_err;
58 	}
59 
60 	rs->sr_err = backsql_get_db_conn( op, &dbh );
61 	if ( rs->sr_err != LDAP_SUCCESS ) {
62      		Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
63 			"could not get connection handle - exiting\n" );
64 
65 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
66 			? "SQL-backend error" : NULL;
67 		goto error_return;
68 	}
69 
70 	anlist[0].an_name = slap_schema.si_ad_userPassword->ad_cname;
71 	anlist[0].an_desc = slap_schema.si_ad_userPassword;
72 	anlist[1].an_name.bv_val = NULL;
73 
74 	bsi.bsi_e = &e;
75 	rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
76 			(time_t)(-1), NULL, dbh, op, rs, anlist,
77 			BACKSQL_ISF_GET_ENTRY );
78 	if ( rc != LDAP_SUCCESS ) {
79 		Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
80 			"could not retrieve bindDN ID - no such entry\n" );
81 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
82 		goto error_return;
83 	}
84 
85 	a = attr_find( e.e_attrs, slap_schema.si_ad_userPassword );
86 	if ( a == NULL ) {
87 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
88 		goto error_return;
89 	}
90 
91 	if ( slap_passwd_check( op, &e, a, &op->oq_bind.rb_cred,
92 				&rs->sr_text ) != 0 )
93 	{
94 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
95 		goto error_return;
96 	}
97 
98 error_return:;
99 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
100 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
101 	}
102 
103 	if ( !BER_BVISNULL( &e.e_nname ) ) {
104 		backsql_entry_clean( op, &e );
105 	}
106 
107 	if ( bsi.bsi_attrs != NULL ) {
108 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
109 	}
110 
111 	if ( rs->sr_err != LDAP_SUCCESS ) {
112 		send_ldap_result( op, rs );
113 	}
114 
115 	Debug( LDAP_DEBUG_TRACE,"<==backsql_bind()\n" );
116 
117 	return rs->sr_err;
118 }
119 
120