xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-sql/bind.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: bind.c,v 1.2 2020/08/11 13:15:42 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2020 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.2 2020/08/11 13:15:42 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
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", 0, 0, 0 );
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, 0, 0 );
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 			0, 0, 0 );
65 
66 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
67 			? "SQL-backend error" : NULL;
68 		goto error_return;
69 	}
70 
71 	anlist[0].an_name = slap_schema.si_ad_userPassword->ad_cname;
72 	anlist[0].an_desc = slap_schema.si_ad_userPassword;
73 	anlist[1].an_name.bv_val = NULL;
74 
75 	bsi.bsi_e = &e;
76 	rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
77 			(time_t)(-1), NULL, dbh, op, rs, anlist,
78 			BACKSQL_ISF_GET_ENTRY );
79 	if ( rc != LDAP_SUCCESS ) {
80 		Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
81 			"could not retrieve bindDN ID - no such entry\n",
82 			0, 0, 0 );
83 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
84 		goto error_return;
85 	}
86 
87 	a = attr_find( e.e_attrs, slap_schema.si_ad_userPassword );
88 	if ( a == NULL ) {
89 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
90 		goto error_return;
91 	}
92 
93 	if ( slap_passwd_check( op, &e, a, &op->oq_bind.rb_cred,
94 				&rs->sr_text ) != 0 )
95 	{
96 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
97 		goto error_return;
98 	}
99 
100 error_return:;
101 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
102 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
103 	}
104 
105 	if ( !BER_BVISNULL( &e.e_nname ) ) {
106 		backsql_entry_clean( op, &e );
107 	}
108 
109 	if ( bsi.bsi_attrs != NULL ) {
110 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
111 	}
112 
113 	if ( rs->sr_err != LDAP_SUCCESS ) {
114 		send_ldap_result( op, rs );
115 	}
116 
117 	Debug( LDAP_DEBUG_TRACE,"<==backsql_bind()\n", 0, 0, 0 );
118 
119 	return rs->sr_err;
120 }
121 
122