]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Use gmtime_r() if HAVE_GMTIME_R is defined (need to add autoconf check)
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 #include <ac/time.h>
33
34 #include "ldap_pvt.h"
35 #include "slap.h"
36 #ifdef LDAP_SLAPI
37 #include "slapi.h"
38 #endif
39 #include "lutil.h"
40
41
42 int
43 do_modify(
44     Operation   *op,
45     SlapReply   *rs )
46 {
47         struct berval dn = { 0, NULL };
48         char            *last;
49         ber_tag_t       tag;
50         ber_len_t       len;
51         Modifications   *modlist = NULL;
52         Modifications   **modtail = &modlist;
53 #ifdef LDAP_DEBUG
54         Modifications *tmp;
55 #endif
56 #ifdef LDAP_SLAPI
57         LDAPMod         **modv = NULL;
58 #endif
59         int manageDSAit;
60         int increment = 0;
61
62 #ifdef NEW_LOGGING
63         LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
64 #else
65         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
66 #endif
67
68         /*
69          * Parse the modify request.  It looks like this:
70          *
71          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
72          *              name    DistinguishedName,
73          *              mods    SEQUENCE OF SEQUENCE {
74          *                      operation       ENUMERATED {
75          *                              add     (0),
76          *                              delete  (1),
77          *                              replace (2)
78          *                      },
79          *                      modification    SEQUENCE {
80          *                              type    AttributeType,
81          *                              values  SET OF AttributeValue
82          *                      }
83          *              }
84          *      }
85          */
86
87         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
88 #ifdef NEW_LOGGING
89                 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
90 #else
91                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
92 #endif
93
94                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
95                 return SLAPD_DISCONNECT;
96         }
97
98 #ifdef NEW_LOGGING
99         LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
100 #else
101         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
102 #endif
103
104
105         /* collect modifications & save for later */
106
107         for ( tag = ber_first_element( op->o_ber, &len, &last );
108             tag != LBER_DEFAULT;
109             tag = ber_next_element( op->o_ber, &len, last ) )
110         {
111                 ber_int_t mop;
112                 Modifications tmp, *mod;
113
114                 tmp.sml_nvalues = NULL;
115
116                 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
117                     &tmp.sml_type, &tmp.sml_values )
118                     == LBER_ERROR )
119                 {
120                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding modlist error" );
121                         rs->sr_err = SLAPD_DISCONNECT;
122                         goto cleanup;
123                 }
124
125                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
126                 mod->sml_op = mop;
127                 mod->sml_type = tmp.sml_type;
128                 mod->sml_values = tmp.sml_values;
129                 mod->sml_nvalues = NULL;
130                 mod->sml_desc = NULL;
131                 mod->sml_next = NULL;
132                 *modtail = mod;
133
134                 switch( mop ) {
135                 case LDAP_MOD_ADD:
136                         if ( mod->sml_values == NULL ) {
137 #ifdef NEW_LOGGING
138                                 LDAP_LOG( OPERATION, ERR, 
139                                         "do_modify: modify/add operation (%ld) requires values\n",
140                                         (long)mop, 0, 0 );
141 #else
142                                 Debug( LDAP_DEBUG_ANY,
143                                         "do_modify: modify/add operation (%ld) requires values\n",
144                                         (long) mop, 0, 0 );
145 #endif
146
147                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
148                                         "modify/add operation requires values" );
149                                 goto cleanup;
150                         }
151
152                         /* fall through */
153
154                 case LDAP_MOD_DELETE:
155                 case LDAP_MOD_REPLACE:
156                         break;
157
158                 case LDAP_MOD_INCREMENT:
159                         if( op->o_protocol >= LDAP_VERSION3 ) {
160                                 increment++;
161                                 if ( mod->sml_values == NULL ) {
162 #ifdef NEW_LOGGING
163                                         LDAP_LOG( OPERATION, ERR, "do_modify: "
164                                                 "modify/increment operation (%ld) requires value\n",
165                                                 (long)mop, 0, 0 );
166 #else
167                                         Debug( LDAP_DEBUG_ANY, "do_modify: "
168                                                 "modify/increment operation (%ld) requires value\n",
169                                                 (long) mop, 0, 0 );
170 #endif
171
172                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
173                                                 "modify/increment operation requires value" );
174                                         goto cleanup;
175                                 }
176
177                                 if( mod->sml_values[1].bv_val ) {
178 #ifdef NEW_LOGGING
179                                         LDAP_LOG( OPERATION, ERR, "do_modify: modify/increment "
180                                                 "operation (%ld) requires single value\n",
181                                                 (long)mop, 0, 0 );
182 #else
183                                         Debug( LDAP_DEBUG_ANY, "do_modify: modify/increment "
184                                                 "operation (%ld) requires single value\n",
185                                                 (long) mop, 0, 0 );
186 #endif
187
188                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
189                                                 "modify/increment operation requires single value" );
190                                         goto cleanup;
191                                 }
192
193                                 break;
194                         }
195                         /* fall thru */
196
197                 default: {
198 #ifdef NEW_LOGGING
199                                 LDAP_LOG( OPERATION, ERR, 
200                                         "do_modify: unrecognized modify operation (%ld)\n",
201                                         (long)mop, 0, 0 );
202 #else
203                                 Debug( LDAP_DEBUG_ANY,
204                                         "do_modify: unrecognized modify operation (%ld)\n",
205                                         (long) mop, 0, 0 );
206 #endif
207
208                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
209                                         "unrecognized modify operation" );
210                                 goto cleanup;
211                         }
212                 }
213
214                 modtail = &mod->sml_next;
215         }
216         *modtail = NULL;
217
218         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
219 #ifdef NEW_LOGGING
220                 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
221 #else
222                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
223 #endif
224
225                 goto cleanup;
226         }
227
228         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
229                 op->o_tmpmemctx );
230         if( rs->sr_err != LDAP_SUCCESS ) {
231 #ifdef NEW_LOGGING
232                 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d  invalid dn (%s)\n",
233                         op->o_connid, dn.bv_val, 0 );
234 #else
235                 Debug( LDAP_DEBUG_ANY,
236                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
237 #endif
238                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
239                 goto cleanup;
240         }
241
242         if( op->o_req_ndn.bv_len == 0 ) {
243 #ifdef NEW_LOGGING
244                 LDAP_LOG( OPERATION, ERR, 
245                         "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
246 #else
247                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
248 #endif
249
250                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
251                         "modify upon the root DSE not supported" );
252                 goto cleanup;
253
254         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
255 #ifdef NEW_LOGGING
256                 LDAP_LOG( OPERATION, ERR,
257                         "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0  );
258 #else
259                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
260 #endif
261
262                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
263                         "modification of subschema subentry not supported" );
264                 goto cleanup;
265         }
266
267 #ifdef LDAP_DEBUG
268 #ifdef NEW_LOGGING
269         LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0  );
270 #else
271         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
272 #endif
273
274         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
275 #ifdef NEW_LOGGING
276                 LDAP_LOG( OPERATION, DETAIL1, "\t%s:  %s\n", 
277                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
278                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
279                                         (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
280                                                 "replace")), tmp->sml_type.bv_val, 0 );
281
282                 if ( tmp->sml_values == NULL ) {
283                         LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
284                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
285                         LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
286                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
287                         LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
288                 } else {
289                         LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
290                 }
291
292 #else
293                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
294                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
295                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
296                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
297                                         "replace")), tmp->sml_type.bv_val, 0 );
298
299                 if ( tmp->sml_values == NULL ) {
300                         Debug( LDAP_DEBUG_ARGS, "%s\n",
301                            "\t\tno values", NULL, NULL );
302                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
303                         Debug( LDAP_DEBUG_ARGS, "%s\n",
304                            "\t\tzero values", NULL, NULL );
305                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
306                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
307                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
308                 } else {
309                         Debug( LDAP_DEBUG_ARGS, "%s\n",
310                            "\t\tmultiple values", NULL, NULL );
311                 }
312 #endif
313         }
314
315         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
316                 char abuf[BUFSIZ/2], *ptr = abuf;
317                 int len = 0;
318
319                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
320                         op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
321
322                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
323                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
324                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
325                                     op->o_connid, op->o_opid, abuf, 0, 0 );
326
327                         len = 0;
328                                 ptr = abuf;
329
330                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
331                                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
332                                                 op->o_connid, op->o_opid, tmp->sml_type.bv_val, 0, 0 );
333                                         continue;
334                                 }
335                         }
336                         if (len) {
337                                 *ptr++ = ' ';
338                                 len++;
339                         }
340                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
341                         len += tmp->sml_type.bv_len;
342                 }
343                 if (len) {
344                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
345                                 op->o_connid, op->o_opid, abuf, 0, 0 );
346                 }
347         }
348 #endif  /* LDAP_DEBUG */
349
350         manageDSAit = get_manageDSAit( op );
351
352         /*
353          * We could be serving multiple database backends.  Select the
354          * appropriate one, or send a referral to our "referral server"
355          * if we don't hold it.
356          */
357         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
358         if ( op->o_bd == NULL ) {
359                 rs->sr_ref = referral_rewrite( default_referral,
360                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
361                 if (!rs->sr_ref) rs->sr_ref = default_referral;
362
363                 if (rs->sr_ref != NULL ) {
364                         rs->sr_err = LDAP_REFERRAL;
365                         send_ldap_result( op, rs );
366
367                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
368                 } else {
369                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
370                                 "referral missing" );
371                 }
372                 goto cleanup;
373         }
374
375         /* check restrictions */
376         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
377                 send_ldap_result( op, rs );
378                 goto cleanup;
379         }
380
381         /* check for referrals */
382         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
383                 goto cleanup;
384         }
385
386         /* check for modify/increment support */
387         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
388                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
389                         "modify/increment not supported in context" );
390         }
391
392 #if defined( LDAP_SLAPI )
393 #define pb      op->o_pb
394         if ( pb ) {
395                 slapi_x_pblock_set_operation( pb, op );
396                 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
397                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
398                 modv = slapi_x_modifications2ldapmods( &modlist );
399                 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
400
401                 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
402                 if ( rs->sr_err < 0 ) {
403                         /*
404                          * A preoperation plugin failure will abort the
405                          * entire operation.
406                          */
407 #ifdef NEW_LOGGING
408                         LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
409                                         "failed\n", 0, 0, 0 );
410 #else
411                         Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
412                                         0, 0, 0);
413 #endif
414                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
415                                  rs->sr_err == LDAP_SUCCESS ) {
416                                 rs->sr_err = LDAP_OTHER;
417                         }
418                         slapi_x_free_ldapmods( modv );
419                         modv = NULL;
420                         goto cleanup;
421                 }
422
423                 /*
424                  * It's possible that the preoperation plugin changed the
425                  * modification array, so we need to convert it back to
426                  * a Modification list.
427                  *
428                  * Calling slapi_x_modifications2ldapmods() destroyed modlist so
429                  * we don't need to free it.
430                  */
431                 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
432                 modlist = slapi_x_ldapmods2modifications( modv );
433         }
434
435         /*
436          * NB: it is valid for the plugin to return no modifications
437          * (for example, a plugin might store some attributes elsewhere
438          * and remove them from the modification list; if only those
439          * attribute types were included in the modification request,
440          * then slapi_x_ldapmods2modifications() above will return
441          * NULL).
442          *
443          * However, the post-operation plugin should still be 
444          * called.
445          */
446         if ( modlist == NULL ) {
447                 rs->sr_err = LDAP_SUCCESS;
448                 send_ldap_result( op, rs );
449         } else {
450 #endif /* defined( LDAP_SLAPI ) */
451
452         /*
453          * do the modify if 1 && (2 || 3)
454          * 1) there is a modify function implemented in this backend;
455          * 2) this backend is master for what it holds;
456          * 3) it's a replica and the dn supplied is the update_ndn.
457          */
458         if ( op->o_bd->be_modify ) {
459                 /* do the update here */
460                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
461
462                 /* Multimaster slapd does not have to check for replicator dn
463                  * because it accepts each modify request
464                  */
465 #ifndef SLAPD_MULTIMASTER
466                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ) &&
467                         ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
468 #else
469                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ))
470 #endif
471                 {
472                         int update = op->o_bd->be_update_ndn.bv_len;
473                         char textbuf[SLAP_TEXT_BUFLEN];
474                         size_t textlen = sizeof textbuf;
475
476                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
477                                 textbuf, textlen, NULL );
478
479                         if( rs->sr_err != LDAP_SUCCESS ) {
480                                 send_ldap_result( op, rs );
481                                 goto cleanup;
482                         }
483
484                         if ( !repl_user ) {
485                                 for( modtail = &modlist;
486                                         *modtail != NULL;
487                                         modtail = &(*modtail)->sml_next )
488                                 {
489                                         /* empty */
490                                 }
491
492                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
493                                         &rs->sr_text, textbuf, textlen );
494                                 if( rs->sr_err != LDAP_SUCCESS ) {
495                                         send_ldap_result( op, rs );
496                                         goto cleanup;
497                                 }
498                         }
499
500                         op->orm_modlist = modlist;
501                         if ( (op->o_bd->be_modify)( op, rs ) == 0
502 #ifdef SLAPD_MULTIMASTER
503                                 && !repl_user
504 #endif
505                         ) {
506                                 /* but we log only the ones not from a replicator user */
507                                 replog( op );
508                         }
509
510 #ifndef SLAPD_MULTIMASTER
511                 /* send a referral */
512                 } else {
513                         BerVarray defref = NULL;
514                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
515                                 syncinfo_t *si;
516                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
517                                         struct berval tmpbv;
518                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
519                                         ber_bvarray_add( &defref, &tmpbv );
520                                 }
521                         } else {
522                                 defref = op->o_bd->be_update_refs
523                                                 ? op->o_bd->be_update_refs : default_referral;
524                         }
525                         if ( defref != NULL ) {
526                                 rs->sr_ref = referral_rewrite( defref,
527                                         NULL, &op->o_req_dn,
528                                         LDAP_SCOPE_DEFAULT );
529                                 if (!rs->sr_ref) rs->sr_ref = defref;
530                                 rs->sr_err = LDAP_REFERRAL;
531                                 send_ldap_result( op, rs );
532                                 if (rs->sr_ref != defref) {
533                                         ber_bvarray_free( rs->sr_ref );
534                                 }
535                         } else {
536                                 send_ldap_error( op, rs,
537                                                 LDAP_UNWILLING_TO_PERFORM,
538                                                 "referral missing" );
539                         }
540 #endif
541                 }
542         } else {
543                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
544                     "operation not supported within namingContext" );
545         }
546
547 #if defined( LDAP_SLAPI )
548         } /* modlist != NULL */
549
550         if ( pb && doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 ) {
551 #ifdef NEW_LOGGING
552                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
553                                 "failed\n", 0, 0, 0 );
554 #else
555                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
556                                 "failed.\n", 0, 0, 0);
557 #endif
558         }
559 #endif /* defined( LDAP_SLAPI ) */
560
561 cleanup:
562
563         slap_graduate_commit_csn( op );
564
565         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
566         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
567         if ( modlist != NULL ) slap_mods_free( modlist );
568 #if defined( LDAP_SLAPI )
569         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
570 #endif
571         return rs->sr_err;
572 }
573
574 /*
575  * Do basic attribute type checking and syntax validation.
576  */
577 int slap_mods_check(
578         Modifications *ml,
579         int update,
580         const char **text,
581         char *textbuf,
582         size_t textlen,
583         void *ctx )
584 {
585         int rc;
586
587         for( ; ml != NULL; ml = ml->sml_next ) {
588                 AttributeDescription *ad = NULL;
589
590                 /* convert to attribute description */
591                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
592
593                 if( rc != LDAP_SUCCESS ) {
594                         snprintf( textbuf, textlen, "%s: %s",
595                                 ml->sml_type.bv_val, *text );
596                         *text = textbuf;
597                         return rc;
598                 }
599
600                 ad = ml->sml_desc;
601
602                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
603                         && !slap_ad_is_binary( ad ))
604                 {
605                         /* attribute requires binary transfer */
606                         snprintf( textbuf, textlen,
607                                 "%s: requires ;binary transfer",
608                                 ml->sml_type.bv_val );
609                         *text = textbuf;
610                         return LDAP_UNDEFINED_TYPE;
611                 }
612
613                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
614                         && slap_ad_is_binary( ad ))
615                 {
616                         /* attribute requires binary transfer */
617                         snprintf( textbuf, textlen,
618                                 "%s: disallows ;binary transfer",
619                                 ml->sml_type.bv_val );
620                         *text = textbuf;
621                         return LDAP_UNDEFINED_TYPE;
622                 }
623
624                 if( slap_ad_is_tag_range( ad )) {
625                         /* attribute requires binary transfer */
626                         snprintf( textbuf, textlen,
627                                 "%s: inappropriate use of tag range option",
628                                 ml->sml_type.bv_val );
629                         *text = textbuf;
630                         return LDAP_UNDEFINED_TYPE;
631                 }
632
633                 if (!update && is_at_no_user_mod( ad->ad_type )) {
634                         /* user modification disallowed */
635                         snprintf( textbuf, textlen,
636                                 "%s: no user modification allowed",
637                                 ml->sml_type.bv_val );
638                         *text = textbuf;
639                         return LDAP_CONSTRAINT_VIOLATION;
640                 }
641
642                 if ( is_at_obsolete( ad->ad_type ) &&
643                         (( ml->sml_op != LDAP_MOD_REPLACE &&
644                                 ml->sml_op != LDAP_MOD_DELETE ) ||
645                                         ml->sml_values != NULL ))
646                 {
647                         /*
648                          * attribute is obsolete,
649                          * only allow replace/delete with no values
650                          */
651                         snprintf( textbuf, textlen,
652                                 "%s: attribute is obsolete",
653                                 ml->sml_type.bv_val );
654                         *text = textbuf;
655                         return LDAP_CONSTRAINT_VIOLATION;
656                 }
657
658                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
659 #ifdef SLAPD_REAL_SYNTAX
660                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
661 #endif
662                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
663                 {
664                         /*
665                          * attribute values must be INTEGER or REAL
666                          */
667                         snprintf( textbuf, textlen,
668                                 "%s: attribute syntax inappropriate for increment",
669                                 ml->sml_type.bv_val );
670                         *text = textbuf;
671                         return LDAP_CONSTRAINT_VIOLATION;
672                 }
673
674                 /*
675                  * check values
676                  */
677                 if( ml->sml_values != NULL ) {
678                         ber_len_t nvals;
679                         slap_syntax_validate_func *validate =
680                                 ad->ad_type->sat_syntax->ssyn_validate;
681                         slap_syntax_transform_func *pretty =
682                                 ad->ad_type->sat_syntax->ssyn_pretty;
683  
684                         if( !pretty && !validate ) {
685                                 *text = "no validator for syntax";
686                                 snprintf( textbuf, textlen,
687                                         "%s: no validator for syntax %s",
688                                         ml->sml_type.bv_val,
689                                         ad->ad_type->sat_syntax->ssyn_oid );
690                                 *text = textbuf;
691                                 return LDAP_INVALID_SYNTAX;
692                         }
693
694                         /*
695                          * check that each value is valid per syntax
696                          *      and pretty if appropriate
697                          */
698                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
699                                 struct berval pval;
700                                 if( pretty ) {
701                                         rc = pretty( ad->ad_type->sat_syntax,
702                                                 &ml->sml_values[nvals], &pval, ctx );
703                                 } else {
704                                         rc = validate( ad->ad_type->sat_syntax,
705                                                 &ml->sml_values[nvals] );
706                                 }
707
708                                 if( rc != 0 ) {
709                                         snprintf( textbuf, textlen,
710                                                 "%s: value #%ld invalid per syntax",
711                                                 ml->sml_type.bv_val, (long) nvals );
712                                         *text = textbuf;
713                                         return LDAP_INVALID_SYNTAX;
714                                 }
715
716                                 if( pretty ) {
717                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
718                                         ml->sml_values[nvals] = pval;
719                                 }
720                         }
721
722                         /*
723                          * a rough single value check... an additional check is needed
724                          * to catch add of single value to existing single valued attribute
725                          */
726                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
727                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
728                         {
729                                 snprintf( textbuf, textlen,
730                                         "%s: multiple values provided",
731                                         ml->sml_type.bv_val );
732                                 *text = textbuf;
733                                 return LDAP_CONSTRAINT_VIOLATION;
734                         }
735
736                         /* if the type has a normalizer, generate the
737                          * normalized values. otherwise leave them NULL.
738                          *
739                          * this is different from the rule for attributes
740                          * in an entry - in an attribute list, the normalized
741                          * value is set equal to the non-normalized value
742                          * when there is no normalizer.
743                          */
744                         if( nvals && ad->ad_type->sat_equality &&
745                                 ad->ad_type->sat_equality->smr_normalize )
746                         {
747                                 ml->sml_nvalues = ber_memalloc_x(
748                                         (nvals+1)*sizeof(struct berval), ctx );
749
750                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
751                                         rc = ad->ad_type->sat_equality->smr_normalize(
752                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
753                                                 ad->ad_type->sat_syntax,
754                                                 ad->ad_type->sat_equality,
755                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
756                                         if( rc ) {
757 #ifdef NEW_LOGGING
758                                                 LDAP_LOG( OPERATION, DETAIL1,
759                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
760                                                         rc, 0, 0 );
761 #else
762                                                 Debug( LDAP_DEBUG_ANY,
763                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
764                                                         rc, 0, 0 );
765 #endif
766                                                 snprintf( textbuf, textlen,
767                                                         "%s: value #%ld normalization failed",
768                                                         ml->sml_type.bv_val, (long) nvals );
769                                                 *text = textbuf;
770                                                 return rc;
771                                         }
772                                 }
773
774                                 ml->sml_nvalues[nvals].bv_val = NULL;
775                                 ml->sml_nvalues[nvals].bv_len = 0;
776                         }
777                 }
778         }
779
780         return LDAP_SUCCESS;
781 }
782
783 int slap_mods_opattrs(
784         Operation *op,
785         Modifications *mods,
786         Modifications **modtail,
787         const char **text,
788         char *textbuf, size_t textlen )
789 {
790         struct berval name, timestamp, csn;
791         struct berval nname;
792         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
793         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
794         Modifications *mod;
795
796         int mop = op->o_tag == LDAP_REQ_ADD
797                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
798
799         assert( modtail != NULL );
800         assert( *modtail == NULL );
801
802         if ( SLAP_LASTMOD( op->o_bd )) {
803                 struct tm *ltm;
804 #ifdef HAVE_GMTIME_R
805                 struct tm ltm_buf;
806 #endif
807                 time_t now = slap_get_time();
808
809 #ifdef HAVE_GMTIME_R
810                 ltm = gmtime_r( &now, &ltm_buf );
811 #else
812                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
813                 ltm = gmtime( &now );
814 #endif /* HAVE_GMTIME_R */
815                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
816
817                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
818
819 #ifndef HAVE_GMTIME_R
820                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
821 #endif
822
823                 timestamp.bv_val = timebuf;
824                 timestamp.bv_len = strlen(timebuf);
825
826                 if( op->o_dn.bv_len == 0 ) {
827                         name.bv_val = SLAPD_ANONYMOUS;
828                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
829                         nname = name;
830                 } else {
831                         name = op->o_dn;
832                         nname = op->o_ndn;
833                 }
834         }
835
836         if( op->o_tag == LDAP_REQ_ADD ) {
837                 struct berval tmpval;
838
839                 if( global_schemacheck ) {
840                         int rc = mods_structural_class( mods, &tmpval,
841                                 text, textbuf, textlen );
842                         if( rc != LDAP_SUCCESS ) return rc;
843
844                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
845                         mod->sml_op = mop;
846                         mod->sml_type.bv_val = NULL;
847                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
848                         mod->sml_values =
849                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
850                         ber_dupbv( &mod->sml_values[0], &tmpval );
851                         mod->sml_values[1].bv_len = 0;
852                         mod->sml_values[1].bv_val = NULL;
853                         assert( mod->sml_values[0].bv_val );
854                         mod->sml_nvalues =
855                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
856                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
857                         mod->sml_nvalues[1].bv_len = 0;
858                         mod->sml_nvalues[1].bv_val = NULL;
859                         assert( mod->sml_nvalues[0].bv_val );
860                         *modtail = mod;
861                         modtail = &mod->sml_next;
862                 }
863
864                 if ( SLAP_LASTMOD( op->o_bd )) {
865                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
866
867                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
868                         tmpval.bv_val = uuidbuf;
869                 
870                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
871                         mod->sml_op = mop;
872                         mod->sml_type.bv_val = NULL;
873                         mod->sml_desc = slap_schema.si_ad_entryUUID;
874                         mod->sml_values =
875                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
876                         ber_dupbv( &mod->sml_values[0], &tmpval );
877                         mod->sml_values[1].bv_len = 0;
878                         mod->sml_values[1].bv_val = NULL;
879                         assert( mod->sml_values[0].bv_val );
880                         mod->sml_nvalues =
881                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
882                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
883                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
884                                         mod->sml_desc->ad_type->sat_syntax,
885                                         mod->sml_desc->ad_type->sat_equality,
886                                         mod->sml_values, mod->sml_nvalues, NULL );
887                         mod->sml_nvalues[1].bv_len = 0;
888                         mod->sml_nvalues[1].bv_val = NULL;
889                         *modtail = mod;
890                         modtail = &mod->sml_next;
891
892                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
893                         mod->sml_op = mop;
894                         mod->sml_type.bv_val = NULL;
895                         mod->sml_desc = slap_schema.si_ad_creatorsName;
896                         mod->sml_values =
897                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
898                         ber_dupbv( &mod->sml_values[0], &name );
899                         mod->sml_values[1].bv_len = 0;
900                         mod->sml_values[1].bv_val = NULL;
901                         assert( mod->sml_values[0].bv_val );
902                         mod->sml_nvalues =
903                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
904                         ber_dupbv( &mod->sml_nvalues[0], &nname );
905                         mod->sml_nvalues[1].bv_len = 0;
906                         mod->sml_nvalues[1].bv_val = NULL;
907                         assert( mod->sml_nvalues[0].bv_val );
908                         *modtail = mod;
909                         modtail = &mod->sml_next;
910
911                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
912                         mod->sml_op = mop;
913                         mod->sml_type.bv_val = NULL;
914                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
915                         mod->sml_values =
916                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
917                         ber_dupbv( &mod->sml_values[0], &timestamp );
918                         mod->sml_values[1].bv_len = 0;
919                         mod->sml_values[1].bv_val = NULL;
920                         assert( mod->sml_values[0].bv_val );
921                         mod->sml_nvalues = NULL;
922                         *modtail = mod;
923                         modtail = &mod->sml_next;
924                 }
925         }
926
927         if ( SLAP_LASTMOD( op->o_bd )) {
928                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
929                 mod->sml_op = mop;
930                 mod->sml_type.bv_val = NULL;
931                 mod->sml_desc = slap_schema.si_ad_entryCSN;
932                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
933                 ber_dupbv( &mod->sml_values[0], &csn );
934                 mod->sml_values[1].bv_len = 0;
935                 mod->sml_values[1].bv_val = NULL;
936                 assert( mod->sml_values[0].bv_val );
937                 mod->sml_nvalues = NULL;
938                 *modtail = mod;
939                 modtail = &mod->sml_next;
940
941                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
942                 mod->sml_op = mop;
943                 mod->sml_type.bv_val = NULL;
944                 mod->sml_desc = slap_schema.si_ad_modifiersName;
945                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
946                 ber_dupbv( &mod->sml_values[0], &name );
947                 mod->sml_values[1].bv_len = 0;
948                 mod->sml_values[1].bv_val = NULL;
949                 assert( mod->sml_values[0].bv_val );
950                 mod->sml_nvalues =
951                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
952                 ber_dupbv( &mod->sml_nvalues[0], &nname );
953                 mod->sml_nvalues[1].bv_len = 0;
954                 mod->sml_nvalues[1].bv_val = NULL;
955                 assert( mod->sml_nvalues[0].bv_val );
956                 *modtail = mod;
957                 modtail = &mod->sml_next;
958
959                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
960                 mod->sml_op = mop;
961                 mod->sml_type.bv_val = NULL;
962                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
963                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
964                 ber_dupbv( &mod->sml_values[0], &timestamp );
965                 mod->sml_values[1].bv_len = 0;
966                 mod->sml_values[1].bv_val = NULL;
967                 assert( mod->sml_values[0].bv_val );
968                 mod->sml_nvalues = NULL;
969                 *modtail = mod;
970                 modtail = &mod->sml_next;
971         }
972
973         *modtail = NULL;
974         return LDAP_SUCCESS;
975 }
976