xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-ldap/modify.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: modify.c,v 1.3 2021/08/14 16:14:59 christos Exp $	*/
2 
3 /* modify.c - ldap backend modify function */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1999-2021 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 <sys/cdefs.h>
27 __RCSID("$NetBSD: modify.c,v 1.3 2021/08/14 16:14:59 christos Exp $");
28 
29 #include "portable.h"
30 
31 #include <stdio.h>
32 
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 
36 #include "slap.h"
37 #include "back-ldap.h"
38 
39 int
ldap_back_modify(Operation * op,SlapReply * rs)40 ldap_back_modify(
41 		Operation	*op,
42 		SlapReply	*rs )
43 {
44 	ldapinfo_t		*li = (ldapinfo_t *)op->o_bd->be_private;
45 
46 	ldapconn_t		*lc = NULL;
47 	LDAPMod			**modv = NULL,
48 				*mods = NULL;
49 	Modifications		*ml;
50 	int			i, j, rc;
51 	ber_int_t		msgid;
52 	int			isupdate;
53 	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
54 	LDAPControl		**ctrls = NULL;
55 
56 	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
57 		return rs->sr_err;
58 	}
59 
60 	for ( i = 0, ml = op->orm_modlist; ml; i++, ml = ml->sml_next )
61 		/* just count mods */ ;
62 
63 	modv = (LDAPMod **)ch_malloc( ( i + 1 )*sizeof( LDAPMod * )
64 			+ i*sizeof( LDAPMod ) );
65 	if ( modv == NULL ) {
66 		rc = LDAP_NO_MEMORY;
67 		goto cleanup;
68 	}
69 	mods = (LDAPMod *)&modv[ i + 1 ];
70 
71 	isupdate = be_shadow_update( op );
72 	for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
73 		if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
74 		{
75 			continue;
76 		}
77 
78 		modv[ i ] = &mods[ i ];
79 		mods[ i ].mod_op = ( ml->sml_op | LDAP_MOD_BVALUES );
80 		mods[ i ].mod_type = ml->sml_desc->ad_cname.bv_val;
81 
82 		if ( ml->sml_values != NULL ) {
83 			for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
84 				/* just count mods */ ;
85 			mods[ i ].mod_bvalues =
86 				(struct berval **)ch_malloc( ( j + 1 )*sizeof( struct berval * ) );
87 			for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
88 			{
89 				mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
90 			}
91 			mods[ i ].mod_bvalues[ j ] = NULL;
92 
93 		} else {
94 			mods[ i ].mod_bvalues = NULL;
95 		}
96 
97 		i++;
98 	}
99 	modv[ i ] = 0;
100 
101 retry:;
102 	ctrls = op->o_ctrls;
103 	rc = ldap_back_controls_add( op, rs, lc, &ctrls );
104 	if ( rc != LDAP_SUCCESS ) {
105 		send_ldap_result( op, rs );
106 		goto cleanup;
107 	}
108 
109 	rs->sr_err = ldap_modify_ext( lc->lc_ld, op->o_req_dn.bv_val, modv,
110 			ctrls, NULL, &msgid );
111 	rc = ldap_back_op_result( lc, op, rs, msgid,
112 		li->li_timeout[ SLAP_OP_MODIFY ],
113 		( LDAP_BACK_SENDRESULT | retrying ) );
114 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
115 		retrying &= ~LDAP_BACK_RETRYING;
116 		if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
117 			/* if the identity changed, there might be need to re-authz */
118 			(void)ldap_back_controls_free( op, rs, &ctrls );
119 			goto retry;
120 		}
121 	}
122 
123 	ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
124 	ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_MODIFY ], 1 );
125 	ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
126 
127 cleanup:;
128 	(void)ldap_back_controls_free( op, rs, &ctrls );
129 
130 	for ( i = 0; modv[ i ]; i++ ) {
131 		ch_free( modv[ i ]->mod_bvalues );
132 	}
133 	ch_free( modv );
134 
135 	if ( lc != NULL ) {
136 		ldap_back_release_conn( li, lc );
137 	}
138 
139 	return rs->sr_err;
140 }
141 
142