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