3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/socket.h>
23 #include <ac/string.h>
29 static void modlist_free(LDAPModList *ml);
31 static int add_modified_attrs( Operation *op, Modifications **modlist );
43 LDAPModList *modlist = NULL;
44 LDAPModList **modtail = &modlist;
45 Modifications *mods = NULL;
52 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
54 if( op->o_bind_in_progress ) {
55 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
57 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
58 NULL, "SASL bind in progress", NULL, NULL );
59 return LDAP_SASL_BIND_IN_PROGRESS;
63 * Parse the modify request. It looks like this:
65 * ModifyRequest := [APPLICATION 6] SEQUENCE {
66 * name DistinguishedName,
67 * mods SEQUENCE OF SEQUENCE {
68 * operation ENUMERATED {
73 * modification SEQUENCE {
75 * values SET OF AttributeValue
81 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
82 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
83 send_ldap_disconnect( conn, op,
84 LDAP_PROTOCOL_ERROR, "decoding error" );
88 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
90 ndn = ch_strdup( dn );
92 if( dn_normalize( ndn ) == NULL ) {
93 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
94 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
95 "invalid DN", NULL, NULL );
99 /* collect modifications & save for later */
101 for ( tag = ber_first_element( op->o_ber, &len, &last );
103 tag = ber_next_element( op->o_ber, &len, last ) )
107 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
109 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
110 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
113 send_ldap_disconnect( conn, op,
114 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
119 (*modtail)->ml_op = mop;
121 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
122 (*modtail)->ml_op != LDAP_MOD_DELETE &&
123 (*modtail)->ml_op != LDAP_MOD_REPLACE )
125 Debug( LDAP_DEBUG_ANY,
126 "do_modify: invalid modify operation (%ld)\n",
127 (long) (*modtail)->ml_op, 0, 0 );
128 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
129 NULL, "unrecognized modify operation", NULL, NULL );
130 rc = LDAP_PROTOCOL_ERROR;
134 if ( (*modtail)->ml_bvalues == NULL && (
135 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
136 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
138 Debug( LDAP_DEBUG_ANY,
139 "do_modify: invalid modify operation (%ld) without values\n",
140 (long) (*modtail)->ml_op, 0, 0 );
141 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
142 NULL, "unrecognized modify operation without values",
144 rc = LDAP_PROTOCOL_ERROR;
148 #ifndef SLAPD_SCHEMA_NOT_COMPAT
149 attr_normalize( (*modtail)->ml_type );
152 modtail = &(*modtail)->ml_next;
156 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
157 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
161 #ifdef SLAPD_SCHEMA_NOT_COMPAT
162 /* not yet implemented */
168 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
169 for ( tmp = mods; tmp != NULL; tmp = tmp->sml_next ) {
170 #ifdef SLAPD_SCHEMA_NOT_COMPAT
171 char *type = tmp->sml_desc.ad_cname->bv_val;
173 char *type = tmp->sml_type;
175 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
176 tmp->sml_op == LDAP_MOD_ADD
177 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
178 ? "delete" : "replace"), type, 0 );
184 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
185 op->o_connid, op->o_opid, dn, 0, 0 );
188 * We could be serving multiple database backends. Select the
189 * appropriate one, or send a referral to our "referral server"
190 * if we don't hold it.
192 if ( (be = select_backend( ndn )) == NULL ) {
193 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
194 NULL, NULL, default_referral, NULL );
198 /* make sure this backend recongizes critical controls */
199 rc = backend_check_controls( be, conn, op ) ;
201 if( rc != LDAP_SUCCESS ) {
202 send_ldap_result( conn, op, rc,
203 NULL, NULL, NULL, NULL );
207 if ( global_readonly || be->be_readonly ) {
208 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
210 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
211 NULL, "database is read-only", NULL, NULL );
215 /* deref suffix alias if appropriate */
216 ndn = suffix_alias( be, ndn );
219 * do the modify if 1 && (2 || 3)
220 * 1) there is a modify function implemented in this backend;
221 * 2) this backend is master for what it holds;
222 * 3) it's a replica and the dn supplied is the update_ndn.
224 if ( be->be_modify ) {
225 /* do the update here */
226 #ifndef SLAPD_MULTIMASTER
227 /* we don't have to check for replicator dn
228 * because we accept each modify request
230 if ( be->be_update_ndn == NULL ||
231 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
234 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
235 global_lastmod == ON)) && be->be_update_ndn == NULL )
237 rc = add_modified_attrs( op, &mods );
239 if( rc != LDAP_SUCCESS ) {
240 send_ldap_result( conn, op, rc,
241 NULL, "no-user-modification attribute type",
247 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
248 #ifdef SLAPD_MULTIMASTER
249 && ( be->be_update_ndn == NULL ||
250 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
253 /* but we log only the ones not from a replicator user */
254 replog( be, op, dn, mods );
257 #ifndef SLAPD_MULTIMASTER
258 /* send a referral */
260 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
261 be->be_update_refs ? be->be_update_refs : default_referral,
266 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
267 NULL, "Function not implemented", NULL, NULL );
273 if ( modlist != NULL )
274 modlist_free( modlist );
279 add_modified_attrs( Operation *op, Modifications **modlist )
283 struct berval *bvals[2];
291 #ifdef SLAPD_SCHEMA_NOT_COMPAT
292 /* not yet implemented */
294 /* remove any attempts by the user to modify these attrs */
295 for ( m = *modlist; m != NULL; m = m->ml_next ) {
296 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
297 return LDAP_CONSTRAINT_VIOLATION;
301 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
302 bv.bv_val = "<anonymous>";
303 bv.bv_len = strlen( bv.bv_val );
305 bv.bv_val = op->o_dn;
306 bv.bv_len = strlen( bv.bv_val );
308 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
309 m->ml_type = ch_strdup( "modifiersname" );
310 m->ml_op = LDAP_MOD_REPLACE;
311 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
312 m->ml_bvalues[0] = ber_bvdup( &bv );
313 m->ml_next = *modlist;
316 currenttime = slap_get_time();
317 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
318 ltm = gmtime( ¤ttime );
319 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
320 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
323 bv.bv_len = strlen( bv.bv_val );
324 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
325 m->ml_type = ch_strdup( "modifytimestamp" );
326 m->ml_op = LDAP_MOD_REPLACE;
327 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
328 m->ml_bvalues[0] = ber_bvdup( &bv );
329 m->ml_next = *modlist;
343 for ( ; ml != NULL; ml = next ) {
349 if ( ml->ml_bvalues != NULL )
350 ber_bvecfree( ml->ml_bvalues );