2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
17 #include <ac/socket.h>
18 #include <ac/string.h>
23 static void modlist_free(LDAPModList *ml);
24 static void add_lastmods(Operation *op, LDAPModList **ml);
35 unsigned long tag, len;
36 LDAPModList *modlist, *tmp;
37 LDAPModList **modtail;
40 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
43 * Parse the modify request. It looks like this:
45 * ModifyRequest := [APPLICATION 6] SEQUENCE {
46 * name DistinguishedName,
47 * mods SEQUENCE OF SEQUENCE {
48 * operation ENUMERATED {
53 * modification SEQUENCE {
55 * values SET OF AttributeValue
61 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
62 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
63 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
66 odn = ch_strdup( dn );
69 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
71 /* collect modifications & save for later */
75 for ( tag = ber_first_element( op->o_ber, &len, &last );
77 tag = ber_next_element( op->o_ber, &len, last ) )
79 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
81 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->ml_op,
82 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
85 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
91 modlist_free( modlist );
95 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
96 (*modtail)->ml_op != LDAP_MOD_DELETE &&
97 (*modtail)->ml_op != LDAP_MOD_REPLACE )
99 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
100 "unrecognized modify operation" );
103 modlist_free( modlist );
107 if ( (*modtail)->ml_bvalues == NULL
108 && (*modtail)->ml_op != LDAP_MOD_DELETE )
110 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
114 modlist_free( modlist );
117 attr_normalize( (*modtail)->ml_type );
119 modtail = &(*modtail)->ml_next;
124 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
125 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
126 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
127 tmp->ml_op == LDAP_MOD_ADD
128 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
129 ? "delete" : "replace"), tmp->ml_type, 0 );
133 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
134 conn->c_connid, op->o_opid, dn, 0, 0 );
137 * We could be serving multiple database backends. Select the
138 * appropriate one, or send a referral to our "referral server"
139 * if we don't hold it.
141 if ( (be = select_backend( dn )) == NULL ) {
144 modlist_free( modlist );
145 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
150 /* alias suffix if approp */
151 dn = suffixAlias ( dn, op, be );
154 * do the modify if 1 && (2 || 3)
155 * 1) there is a modify function implemented in this backend;
156 * 2) this backend is master for what it holds;
157 * 3) it's a replica and the dn supplied is the updatedn.
159 if ( be->be_modify != NULL ) {
160 /* do the update here */
161 if ( be->be_updatedn == NULL ||
162 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
164 if ( (be->be_lastmod == ON || ( be->be_lastmod == UNDEFINED &&
165 global_lastmod == ON ) ) && be->be_updatedn == NULL ) {
166 add_lastmods( op, &modlist );
168 if ( (*be->be_modify)( be, conn, op, odn, modlist ) == 0 ) {
169 replog( be, LDAP_REQ_MODIFY, dn, modlist, 0 );
172 /* send a referral */
174 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
178 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
179 "Function not implemented" );
184 modlist_free( modlist );
194 for ( ; ml != NULL; ml = next ) {
198 if ( ml->ml_bvalues != NULL )
199 ber_bvecfree( ml->ml_bvalues );
206 add_lastmods( Operation *op, LDAPModList **modlist )
210 struct berval *bvals[2];
215 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
220 /* remove any attempts by the user to modify these attrs */
221 for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
222 if ( strcasecmp( (*m)->ml_type, "modifytimestamp" ) == 0 ||
223 strcasecmp( (*m)->ml_type, "modifiersname" ) == 0 ||
224 strcasecmp( (*m)->ml_type, "createtimestamp" ) == 0 ||
225 strcasecmp( (*m)->ml_type, "creatorsname" ) == 0 ) {
227 Debug( LDAP_DEBUG_TRACE,
228 "add_lastmods: found lastmod attr: %s\n",
229 (*m)->ml_type, 0, 0 );
232 free( tmp->ml_type );
233 if ( tmp->ml_bvalues != NULL ) {
234 ber_bvecfree( tmp->ml_bvalues );
242 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
243 bv.bv_val = "NULLDN";
244 bv.bv_len = strlen( bv.bv_val );
246 bv.bv_val = op->o_dn;
247 bv.bv_len = strlen( bv.bv_val );
249 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
250 tmp->ml_type = ch_strdup( "modifiersname" );
251 tmp->ml_op = LDAP_MOD_REPLACE;
252 tmp->ml_bvalues = (struct berval **) ch_calloc( 1,
253 2 * sizeof(struct berval *) );
254 tmp->ml_bvalues[0] = ber_bvdup( &bv );
255 tmp->ml_next = *modlist;
258 pthread_mutex_lock( ¤ttime_mutex );
259 #ifndef LDAP_LOCALTIME
260 ltm = gmtime( ¤ttime );
261 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
263 ltm = localtime( ¤ttime );
264 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
266 pthread_mutex_unlock( ¤ttime_mutex );
268 bv.bv_len = strlen( bv.bv_val );
269 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
270 tmp->ml_type = ch_strdup( "modifytimestamp" );
271 tmp->ml_op = LDAP_MOD_REPLACE;
272 tmp->ml_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
273 tmp->ml_bvalues[0] = ber_bvdup( &bv );
274 tmp->ml_next = *modlist;