1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2021 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22 
23 #include "portable.h"
24 
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28 
29 #include "slap.h"
30 #include "proto-sql.h"
31 
32 int
backsql_modify(Operation * op,SlapReply * rs)33 backsql_modify( Operation *op, SlapReply *rs )
34 {
35 	backsql_info		*bi = (backsql_info*)op->o_bd->be_private;
36 	SQLHDBC 		dbh = SQL_NULL_HDBC;
37 	backsql_oc_map_rec	*oc = NULL;
38 	backsql_srch_info	bsi = { 0 };
39 	Entry			m = { 0 }, *e = NULL;
40 	int			manageDSAit = get_manageDSAit( op );
41 	SQLUSMALLINT		CompletionType = SQL_ROLLBACK;
42 
43 	/*
44 	 * FIXME: in case part of the operation cannot be performed
45 	 * (missing mapping, SQL write fails or so) the entire operation
46 	 * should be rolled-back
47 	 */
48 	Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): modifying entry \"%s\"\n",
49 		op->o_req_ndn.bv_val );
50 
51 	rs->sr_err = backsql_get_db_conn( op, &dbh );
52 	if ( rs->sr_err != LDAP_SUCCESS ) {
53 		Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
54 			"could not get connection handle - exiting\n" );
55 		/*
56 		 * FIXME: we don't want to send back
57 		 * excessively detailed messages
58 		 */
59 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
60 			? "SQL-backend error" : NULL;
61 		goto done;
62 	}
63 
64 	bsi.bsi_e = &m;
65 	rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
66 			LDAP_SCOPE_BASE,
67 			(time_t)(-1), NULL, dbh, op, rs,
68 			slap_anlist_all_attributes,
69 			( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY | BACKSQL_ISF_GET_OC ) );
70 	switch ( rs->sr_err ) {
71 	case LDAP_SUCCESS:
72 		break;
73 
74 	case LDAP_REFERRAL:
75 		if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
76 				dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
77 		{
78 			rs->sr_err = LDAP_SUCCESS;
79 			rs->sr_text = NULL;
80 			rs->sr_matched = NULL;
81 			if ( rs->sr_ref ) {
82 				ber_bvarray_free( rs->sr_ref );
83 				rs->sr_ref = NULL;
84 			}
85 			break;
86 		}
87 		e = &m;
88 		/* fallthru */
89 
90 	default:
91 		Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
92 			"could not retrieve modifyDN ID - no such entry\n" );
93 		if ( !BER_BVISNULL( &m.e_nname ) ) {
94 			/* FIXME: should always be true! */
95 			e = &m;
96 
97 		} else {
98 			e = NULL;
99 		}
100 		goto done;
101 	}
102 
103 	Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
104 		"modifying entry \"%s\" (id=" BACKSQL_IDFMT ")\n",
105 		bsi.bsi_base_id.eid_dn.bv_val,
106 		BACKSQL_IDARG(bsi.bsi_base_id.eid_id) );
107 
108 	if ( get_assert( op ) &&
109 			( test_filter( op, &m, get_assertion( op ) )
110 			  != LDAP_COMPARE_TRUE ))
111 	{
112 		rs->sr_err = LDAP_ASSERTION_FAILED;
113 		e = &m;
114 		goto done;
115 	}
116 
117 	slap_mods_opattrs( op, &op->orm_modlist, 1 );
118 
119 	assert( bsi.bsi_base_id.eid_oc != NULL );
120 	oc = bsi.bsi_base_id.eid_oc;
121 
122 	if ( !acl_check_modlist( op, &m, op->orm_modlist ) ) {
123 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
124 		e = &m;
125 		goto done;
126 	}
127 
128 	rs->sr_err = backsql_modify_internal( op, rs, dbh, oc,
129 			&bsi.bsi_base_id, op->orm_modlist );
130 	if ( rs->sr_err != LDAP_SUCCESS ) {
131 		e = &m;
132 		goto do_transact;
133 	}
134 
135 	if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
136 		char		textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
137 
138 		backsql_entry_clean( op, &m );
139 
140 		bsi.bsi_e = &m;
141 		rs->sr_err = backsql_id2entry( &bsi, &bsi.bsi_base_id );
142 		if ( rs->sr_err != LDAP_SUCCESS ) {
143 			e = &m;
144 			goto do_transact;
145 		}
146 
147 		rs->sr_err = entry_schema_check( op, &m, NULL, 0, 0, NULL,
148 			&rs->sr_text, textbuf, sizeof( textbuf ) );
149 		if ( rs->sr_err != LDAP_SUCCESS ) {
150 			Debug( LDAP_DEBUG_TRACE, "   backsql_modify(\"%s\"): "
151 				"entry failed schema check -- aborting\n",
152 				m.e_name.bv_val );
153 			e = NULL;
154 			goto do_transact;
155 		}
156 	}
157 
158 do_transact:;
159 	/*
160 	 * Commit only if all operations succeed
161 	 */
162 	if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
163 		assert( e == NULL );
164 		CompletionType = SQL_COMMIT;
165 	}
166 
167 	SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
168 
169 done:;
170 	if ( e != NULL ) {
171 		if ( !access_allowed( op, e, slap_schema.si_ad_entry, NULL,
172 					ACL_DISCLOSE, NULL ) )
173 		{
174 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
175 			rs->sr_text = NULL;
176 			rs->sr_matched = NULL;
177 			if ( rs->sr_ref ) {
178 				ber_bvarray_free( rs->sr_ref );
179 				rs->sr_ref = NULL;
180 			}
181 		}
182 	}
183 
184 	if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
185 		rs->sr_err = LDAP_X_NO_OPERATION;
186 	}
187 
188 	send_ldap_result( op, rs );
189 	slap_graduate_commit_csn( op );
190 
191 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
192 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
193 	}
194 
195 	if ( !BER_BVISNULL( &m.e_nname ) ) {
196 		backsql_entry_clean( op, &m );
197 	}
198 
199 	if ( bsi.bsi_attrs != NULL ) {
200 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
201 	}
202 
203 	if ( rs->sr_ref ) {
204 		ber_bvarray_free( rs->sr_ref );
205 		rs->sr_ref = NULL;
206 	}
207 
208 	Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n" );
209 
210 	return rs->sr_err;
211 }
212 
213