1/*	$NetBSD$	*/
2
3/* modify.c - ldap backend modify function */
4/* OpenLDAP: pkg/ldap/servers/slapd/back-ldap/modify.c,v 1.69.2.7 2010/04/13 20:23:28 kurt Exp */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 1999-2010 The OpenLDAP Foundation.
8 * Portions Copyright 1999-2003 Howard Chu.
9 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted only as authorized by the OpenLDAP
14 * Public License.
15 *
16 * A copy of this license is available in the file LICENSE in the
17 * top-level directory of the distribution or, alternatively, at
18 * <http://www.OpenLDAP.org/license.html>.
19 */
20/* ACKNOWLEDGEMENTS:
21 * This work was initially developed by the Howard Chu for inclusion
22 * in OpenLDAP Software and subsequently enhanced by Pierangelo
23 * Masarati.
24 */
25
26#include "portable.h"
27
28#include <stdio.h>
29
30#include <ac/string.h>
31#include <ac/socket.h>
32
33#include "slap.h"
34#include "back-ldap.h"
35
36int
37ldap_back_modify(
38		Operation	*op,
39		SlapReply	*rs )
40{
41	ldapinfo_t		*li = (ldapinfo_t *)op->o_bd->be_private;
42
43	ldapconn_t		*lc = NULL;
44	LDAPMod			**modv = NULL,
45				*mods = NULL;
46	Modifications		*ml;
47	int			i, j, rc;
48	ber_int_t		msgid;
49	int			isupdate;
50	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
51	LDAPControl		**ctrls = NULL;
52
53	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
54		return rs->sr_err;
55	}
56
57	for ( i = 0, ml = op->orm_modlist; ml; i++, ml = ml->sml_next )
58		/* just count mods */ ;
59
60	modv = (LDAPMod **)ch_malloc( ( i + 1 )*sizeof( LDAPMod * )
61			+ i*sizeof( LDAPMod ) );
62	if ( modv == NULL ) {
63		rc = LDAP_NO_MEMORY;
64		goto cleanup;
65	}
66	mods = (LDAPMod *)&modv[ i + 1 ];
67
68	isupdate = be_shadow_update( op );
69	for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
70		if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
71		{
72			continue;
73		}
74
75		modv[ i ] = &mods[ i ];
76		mods[ i ].mod_op = ( ml->sml_op | LDAP_MOD_BVALUES );
77		mods[ i ].mod_type = ml->sml_desc->ad_cname.bv_val;
78
79		if ( ml->sml_values != NULL ) {
80			if ( ml->sml_values == NULL ) {
81				continue;
82			}
83
84			for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
85				/* just count mods */ ;
86			mods[ i ].mod_bvalues =
87				(struct berval **)ch_malloc( ( j + 1 )*sizeof( struct berval * ) );
88			for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
89			{
90				mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
91			}
92			mods[ i ].mod_bvalues[ j ] = NULL;
93
94		} else {
95			mods[ i ].mod_bvalues = NULL;
96		}
97
98		i++;
99	}
100	modv[ i ] = 0;
101
102retry:;
103	ctrls = op->o_ctrls;
104	rc = ldap_back_controls_add( op, rs, lc, &ctrls );
105	if ( rc != LDAP_SUCCESS ) {
106		send_ldap_result( op, rs );
107		rc = -1;
108		goto cleanup;
109	}
110
111	rs->sr_err = ldap_modify_ext( lc->lc_ld, op->o_req_dn.bv_val, modv,
112			ctrls, NULL, &msgid );
113	rc = ldap_back_op_result( lc, op, rs, msgid,
114		li->li_timeout[ SLAP_OP_MODIFY ],
115		( LDAP_BACK_SENDRESULT | retrying ) );
116	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
117		retrying &= ~LDAP_BACK_RETRYING;
118		if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
119			/* if the identity changed, there might be need to re-authz */
120			(void)ldap_back_controls_free( op, rs, &ctrls );
121			goto retry;
122		}
123	}
124
125cleanup:;
126	(void)ldap_back_controls_free( op, rs, &ctrls );
127
128	for ( i = 0; modv[ i ]; i++ ) {
129		ch_free( modv[ i ]->mod_bvalues );
130	}
131	ch_free( modv );
132
133	if ( lc != NULL ) {
134		ldap_back_release_conn( li, lc );
135	}
136
137	return rc;
138}
139
140