xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-sql/modify.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/modify.c,v 1.53.2.5 2008/02/11 23:26:48 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2008 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
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, 0, 0 );
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 			0, 0, 0 );
56 		/*
57 		 * FIXME: we don't want to send back
58 		 * excessively detailed messages
59 		 */
60 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
61 			? "SQL-backend error" : NULL;
62 		goto done;
63 	}
64 
65 	bsi.bsi_e = &m;
66 	rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
67 			LDAP_SCOPE_BASE,
68 			(time_t)(-1), NULL, dbh, op, rs,
69 			slap_anlist_all_attributes,
70 			( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY | BACKSQL_ISF_GET_OC ) );
71 	switch ( rs->sr_err ) {
72 	case LDAP_SUCCESS:
73 		break;
74 
75 	case LDAP_REFERRAL:
76 		if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
77 				dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
78 		{
79 			rs->sr_err = LDAP_SUCCESS;
80 			rs->sr_text = NULL;
81 			rs->sr_matched = NULL;
82 			if ( rs->sr_ref ) {
83 				ber_bvarray_free( rs->sr_ref );
84 				rs->sr_ref = NULL;
85 			}
86 			break;
87 		}
88 		e = &m;
89 		/* fallthru */
90 
91 	default:
92 		Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
93 			"could not retrieve modifyDN ID - no such entry\n",
94 			0, 0, 0 );
95 		if ( !BER_BVISNULL( &m.e_nname ) ) {
96 			/* FIXME: should always be true! */
97 			e = &m;
98 
99 		} else {
100 			e = NULL;
101 		}
102 		goto done;
103 	}
104 
105 #ifdef BACKSQL_ARBITRARY_KEY
106 	Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
107 		"modifying entry \"%s\" (id=%s)\n",
108 		bsi.bsi_base_id.eid_dn.bv_val,
109 		bsi.bsi_base_id.eid_id.bv_val, 0 );
110 #else /* ! BACKSQL_ARBITRARY_KEY */
111 	Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
112 		"modifying entry \"%s\" (id=%ld)\n",
113 		bsi.bsi_base_id.eid_dn.bv_val, bsi.bsi_base_id.eid_id, 0 );
114 #endif /* ! BACKSQL_ARBITRARY_KEY */
115 
116 	if ( get_assert( op ) &&
117 			( test_filter( op, &m, get_assertion( op ) )
118 			  != LDAP_COMPARE_TRUE ))
119 	{
120 		rs->sr_err = LDAP_ASSERTION_FAILED;
121 		e = &m;
122 		goto done;
123 	}
124 
125 	slap_mods_opattrs( op, &op->orm_modlist, 1 );
126 
127 	assert( bsi.bsi_base_id.eid_oc != NULL );
128 	oc = bsi.bsi_base_id.eid_oc;
129 
130 	if ( !acl_check_modlist( op, &m, op->orm_modlist ) ) {
131 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
132 		e = &m;
133 		goto done;
134 	}
135 
136 	rs->sr_err = backsql_modify_internal( op, rs, dbh, oc,
137 			&bsi.bsi_base_id, op->orm_modlist );
138 	if ( rs->sr_err != LDAP_SUCCESS ) {
139 		e = &m;
140 		goto do_transact;
141 	}
142 
143 	if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
144 		char		textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
145 
146 		backsql_entry_clean( op, &m );
147 
148 		bsi.bsi_e = &m;
149 		rs->sr_err = backsql_id2entry( &bsi, &bsi.bsi_base_id );
150 		if ( rs->sr_err != LDAP_SUCCESS ) {
151 			e = &m;
152 			goto do_transact;
153 		}
154 
155 		rs->sr_err = entry_schema_check( op, &m, NULL, 0, 0,
156 			&rs->sr_text, textbuf, sizeof( textbuf ) );
157 		if ( rs->sr_err != LDAP_SUCCESS ) {
158 			Debug( LDAP_DEBUG_TRACE, "   backsql_modify(\"%s\"): "
159 				"entry failed schema check -- aborting\n",
160 				m.e_name.bv_val, 0, 0 );
161 			e = NULL;
162 			goto do_transact;
163 		}
164 	}
165 
166 do_transact:;
167 	/*
168 	 * Commit only if all operations succeed
169 	 */
170 	if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
171 		assert( e == NULL );
172 		CompletionType = SQL_COMMIT;
173 	}
174 
175 	SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
176 
177 done:;
178 	if ( e != NULL ) {
179 		if ( !access_allowed( op, e, slap_schema.si_ad_entry, NULL,
180 					ACL_DISCLOSE, NULL ) )
181 		{
182 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
183 			rs->sr_text = NULL;
184 			rs->sr_matched = NULL;
185 			if ( rs->sr_ref ) {
186 				ber_bvarray_free( rs->sr_ref );
187 				rs->sr_ref = NULL;
188 			}
189 		}
190 	}
191 
192 	if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
193 		rs->sr_err = LDAP_X_NO_OPERATION;
194 	}
195 
196 	send_ldap_result( op, rs );
197 	slap_graduate_commit_csn( op );
198 
199 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
200 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
201 	}
202 
203 	if ( !BER_BVISNULL( &m.e_nname ) ) {
204 		backsql_entry_clean( op, &m );
205 	}
206 
207 	if ( bsi.bsi_attrs != NULL ) {
208 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
209 	}
210 
211 	if ( rs->sr_ref ) {
212 		ber_bvarray_free( rs->sr_ref );
213 		rs->sr_ref = NULL;
214 	}
215 
216 	Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
217 
218 	return rs->sr_err;
219 }
220 
221