2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
18 #include <ac/socket.h>
19 #include <ac/string.h> /* get SAFEMEMCPY */
21 #include <quipu/commonarg.h>
22 #include <quipu/attrvalue.h>
23 #include <quipu/ds_error.h>
24 #include <quipu/modify.h>
25 #include <quipu/dap2.h>
26 #include <quipu/dua.h>
33 static CommonArgs common = default_common_args;
35 static int replace_mod( struct entrymod *, Attr_Sequence, Attr_Sequence );
38 #define MODTAG (ldap_compat == 20 ? OLD_LDAP_RES_MODIFY : LDAP_RES_MODIFY)
40 #define MODTAG LDAP_RES_MODIFY
53 unsigned long tag, len;
54 LDAPModList *mods, *modtail;
55 struct ds_read_arg ra;
57 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
60 * Parse the modify request. It looks like this:
61 * ModifyRequest := [APPLICATION 6] SEQUENCE {
62 * name DistinguishedName,
63 * mods SEQUENCE OF SEQUENCE {
64 * operation ENUMERATED {
69 * modification SEQUENCE {
71 * values SET OF AttributeValue
75 * We then have to initiate a read of the entry to be modified.
76 * The actual modification is done by do_modify2(), after the
80 #if ISODEPACKAGE == IC
82 DAS_ReadArgument_INIT( &ra );
86 if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
87 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
88 send_ldap_msgresult( clientsb, MODTAG, m,
89 LDAP_PROTOCOL_ERROR, NULL, "" );
93 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
95 ra.rda_object = ldap_str2dn( dn );
97 if ( ra.rda_object == NULLDN ) {
98 Debug( LDAP_DEBUG_ANY, "ldap_str2dn failed\n", 0, 0, 0 );
99 send_ldap_msgresult( clientsb, MODTAG, m,
100 LDAP_INVALID_DN_SYNTAX, NULL, "" );
103 ra.rda_eis.eis_allattributes = TRUE;
104 ra.rda_eis.eis_infotypes = EIS_ATTRIBUTESANDVALUES;
105 ra.rda_eis.eis_select = NULLATTR;
107 /* collect modifications & save for later */
108 mods = modtail = NULL;
109 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
110 tag = ber_next_element( ber, &len, last ) ) {
113 if ( (tmp = (LDAPModList *) calloc( 1, sizeof(LDAPModList) ))
115 send_ldap_msgresult( clientsb, MODTAG, m,
116 LDAP_OPERATIONS_ERROR, NULL, "Malloc error" );
120 if ( ber_scanf( ber, "{i{a[V]}}", &tmp->m.mod_op,
121 &tmp->m.mod_type, &tmp->m.mod_bvalues ) == LBER_ERROR ) {
122 send_ldap_msgresult( clientsb, MODTAG, m,
123 LDAP_PROTOCOL_ERROR, NULL, "" );
127 if ( mods == NULL ) {
130 modtail->mod_next = tmp;
136 ra.rda_common = common; /* struct copy */
138 rc = initiate_dap_operation( OP_READ, m, &ra );
140 dn_free( ra.rda_object );
143 send_ldap_msgresult( clientsb, MODTAG, m, rc, NULL, "" );
154 struct ds_read_result *rr
157 struct ds_modifyentry_arg ma;
158 struct entrymod *changetail = NULLMOD;
162 Debug( LDAP_DEBUG_TRACE, "do_modify2\n", 0, 0, 0 );
164 #if ISODEPACKAGE == IC
166 DAS_ModifyEntryArgument_INIT( &ma );
170 ma.mea_changes = NULLMOD;
171 for ( mods = m->m_mods; mods != NULL; mods = mods->mod_next ) {
173 Attr_Sequence as, new;
175 if ( (em = (struct entrymod *) calloc( 1,
176 sizeof(struct entrymod) )) == NULLMOD ) {
177 send_ldap_msgresult( clientsb, MODTAG, m,
178 LDAP_OPERATIONS_ERROR, NULL, "Malloc error" );
181 em->em_next = NULLMOD;
183 if ( (new = get_as( clientsb, MODTAG, m,
184 mods->m.mod_type, mods->m.mod_bvalues )) == NULLATTR )
188 for ( as = rr->rdr_entry.ent_attr; as != NULLATTR;
189 as = as->attr_link ) {
190 if ( AttrT_cmp( new->attr_type, as->attr_type ) == 0 )
194 if ( new->attr_value == NULLAV &&
195 mods->m.mod_op != LDAP_MOD_DELETE ) {
196 send_ldap_msgresult( clientsb, MODTAG, m,
197 LDAP_INVALID_SYNTAX, NULL, "No values specified" );
201 switch ( mods->m.mod_op ) {
203 Debug( LDAP_DEBUG_ARGS, "ADD:\n", 0, 0, 0 );
205 if ( as == NULLATTR ) {
206 Debug( LDAP_DEBUG_ARGS, "\tattribute\n", 0, 0,
208 em->em_type = EM_ADDATTRIBUTE;
210 Debug( LDAP_DEBUG_ARGS, "\tvalues\n", 0, 0, 0 );
211 em->em_type = EM_ADDVALUES;
215 case LDAP_MOD_DELETE:
216 Debug( LDAP_DEBUG_ARGS, "DELETE:\n", 0, 0, 0 );
218 if ( as == NULLATTR ) {
219 Debug( LDAP_DEBUG_ARGS,
220 "\tno existing attribute\n", 0, 0, 0 );
221 send_ldap_msgresult( clientsb, MODTAG,
222 m, LDAP_NO_SUCH_ATTRIBUTE, NULL, "" );
226 if ( new->attr_value == NULLAV ) {
227 Debug( LDAP_DEBUG_ARGS, "\tattribute\n",
229 em->em_type = EM_REMOVEATTRIBUTE;
231 if ( avs_cmp( new->attr_value,
232 as->attr_value ) == 0 ) {
233 Debug( LDAP_DEBUG_ARGS,
234 "\tattribute\n", 0, 0, 0 );
238 Debug( LDAP_DEBUG_ARGS,
239 "\tvalues\n", 0, 0, 0 );
240 em->em_type = EM_REMOVEVALUES;
246 case LDAP_MOD_REPLACE:
247 Debug( LDAP_DEBUG_ARGS, "REPLACE:\n", 0, 0, 0 );
249 if ( as == NULLATTR ) {
250 Debug( LDAP_DEBUG_ARGS, "\tattribute\n", 0, 0,
252 em->em_type = EM_ADDATTRIBUTE;
254 if ( replace_mod( em, as, new ) < 0 ) {
261 Debug( LDAP_DEBUG_ARGS, "UNKNOWN MOD:\n", 0, 0, 0 );
263 send_ldap_msgresult( clientsb, MODTAG, m,
264 LDAP_PROTOCOL_ERROR, NULL, "" );
269 if ( em->em_what == NULL ) { /* ignore this mod */
272 if ( ma.mea_changes == NULLMOD ) {
275 changetail->em_next = em;
277 changetail = em->em_next == NULLMOD ? em : em->em_next;
282 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
288 ps = ps_alloc( std_open );
289 std_setup( ps, stderr );
291 fprintf( stderr, "Modify changes are:\n");
292 for (e = ma.mea_changes; e; e = e->em_next) {
293 switch (e->em_type) {
294 case EM_ADDATTRIBUTE:
295 fprintf( stderr, "\tADD ATTRIBUTE\n");
297 case EM_REMOVEATTRIBUTE:
298 fprintf( stderr, "\tREMOVE ATTRIBUTE\n");
301 fprintf( stderr, "\tADD VALUES\n");
303 case EM_REMOVEVALUES:
304 fprintf( stderr, "\tREMOVE VALUES\n");
307 fprintf( stderr, "\tUNKNOWN\n");
312 fprintf( stderr, "\t\ttype (" );
313 AttrT_print( ps, as->attr_type, EDBOUT );
314 fprintf( stderr, ")" );
315 if ( e->em_type == EM_REMOVEATTRIBUTE ) {
316 fprintf( stderr, "\n" );
319 fprintf( stderr, " values" );
320 for (val = as->attr_value; val; val = val->avseq_next) {
321 ps_print( ps, " (" );
322 AttrV_print( ps, &val->avseq_av, EDBOUT );
325 fprintf( stderr, "\n" );
331 if ( ma.mea_changes == NULLMOD ) { /* nothing to do */
332 send_ldap_msgresult( clientsb, MODTAG, m,
333 LDAP_SUCCESS, NULL, "" );
337 ma.mea_object = rr->rdr_entry.ent_dn;
338 ma.mea_common = common; /* struct copy */
340 rc = initiate_dap_operation( OP_MODIFYENTRY, m, &ma );
342 ems_free( ma.mea_changes );
345 send_ldap_msgresult( clientsb, MODTAG, m, rc, NULL, "" );
358 struct berval **bvals
365 Debug( LDAP_DEBUG_TRACE, "get_as\n", 0, 0, 0 );
367 if ( (as = as_comp_new( NULLAttrT, NULLAV, NULLACL_INFO ))
369 send_ldap_msgresult( clientsb, op, m,
370 LDAP_OPERATIONS_ERROR, NULL, "Malloc error" );
373 as->attr_link = NULLATTR;
374 as->attr_value = NULLAV;
375 as->attr_acl = NULLACL_INFO;
377 if ( (as->attr_type = str2AttrT( type )) == NULLAttrT ) {
378 send_ldap_msgresult( clientsb, op, m, LDAP_UNDEFINED_TYPE,
386 syntax = as->attr_type->oa_syntax;
387 for ( i = 0; bvals[i] != NULL; i++ ) {
390 char *sval, *s, *news, *n;
392 if ( syntax == ldap_jpeg_syntax ||
393 syntax == ldap_jpeg_nonfile_syntax ||
394 syntax == ldap_octetstring_syntax ||
395 syntax == ldap_audio_syntax ) {
396 if (( av = bv_octet2AttrV( bvals[i] )) == NULLAttrV ) {
397 send_ldap_msgresult( clientsb, op, m,
398 LDAP_INVALID_SYNTAX, NULL, type );
402 } else if ( syntax == ldap_photo_syntax ) {
403 if (( av = bv_asn2AttrV( bvals[i] )) == NULLAttrV ) {
404 send_ldap_msgresult( clientsb, op, m,
405 LDAP_INVALID_SYNTAX, NULL, type );
411 if (( sval = malloc( bvals[i]->bv_len + 1 )) == NULL ) {
412 send_ldap_msgresult( clientsb, op, m,
413 LDAP_OPERATIONS_ERROR, NULL,
417 SAFEMEMCPY( sval, bvals[i]->bv_val, bvals[i]->bv_len );
418 sval[ bvals[i]->bv_len ] = '\0';
420 /* dang quipu - there's no need for this! */
421 if ( syntax == ldap_postaladdress_syntax ) {
424 for ( s = sval; *s; s++ ) {
429 #define ist61(c) (!isascii(c) || !isalnum(c) \
430 && c != 047 && c != '(' && c != ')' \
431 && c != '+' && c != '-' && c != '.' && c != ',' \
432 && c != '/' && c != ':' && c != '=' && c != '?' \
437 #define T61MARK "{T.61}"
440 news = malloc( strlen(sval) +
441 ncomp * T61MARKLEN + 1 );
442 strcpy( news, T61MARK );
443 for ( n = news + T61MARKLEN, s = sval;
447 strcpy( ++n, T61MARK );
456 av = str_at2AttrV( sval, as->attr_type );
457 } else if ( syntax == ldap_dn_syntax ) {
458 av = ldap_strdn2AttrV( sval );
459 } else if ( i != 0 && syntax == ldap_acl_syntax ) {
460 (void) (*merge_acl)( as->attr_value, sval );
464 av = ldap_str_at2AttrV( sval, as->attr_type );
467 if ( av == NULLAttrV ) {
468 send_ldap_msgresult( clientsb, op, m,
469 LDAP_INVALID_SYNTAX, NULL, sval );
477 as->attr_value = avs_merge( as->attr_value,
478 avs_comp_new( av ) );
485 modify_result( Sockbuf *sb, struct msg *m )
487 send_ldap_msgresult( sb, MODTAG, m, LDAP_SUCCESS, NULL, "" );
493 modlist_free( LDAPModList *mods )
497 for ( ; mods != NULL; mods = next ) {
498 free( mods->m.mod_type );
499 if ( mods->m.mod_bvalues != NULL )
500 ber_bvecfree( mods->m.mod_bvalues );
501 next = mods->mod_next;
507 * called when mod is replace to optimize by only deleting old values
508 * that are not in the new set and by only adding what isn't in old set
513 struct entrymod *rem,
518 AV_Sequence oavs, navs, davs, prev_navs, tmp;
522 ps = ps_alloc( std_open );
523 std_setup( ps, stderr );
525 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
526 ps_print( ps, "replace_mod(" );
527 AttrT_print( ps, oas->attr_type, EDBOUT );
528 ps_print( ps, ")\n" );
533 for ( oavs = oas->attr_value; oavs != NULL; oavs = oavs->avseq_next ) {
535 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
536 ps_print( ps, "old value " );
537 AttrV_print( ps, &oavs->avseq_av, EDBOUT );
538 ps_print( ps, "\n" );
543 for ( navs = nas->attr_value; navs != NULL;
544 prev_navs = navs, navs = navs->avseq_next ) {
546 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
547 ps_print( ps, "\tnew value " );
548 AttrV_print( ps, &navs->avseq_av, EDBOUT );
549 ps_print( ps, "\n" );
552 if ( AttrV_cmp( &oavs->avseq_av, &navs->avseq_av)
558 if ( navs == NULL ) { /* value to delete */
560 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
561 ps_print( ps, "value to delete " );
562 AttrV_print( ps, &oavs->avseq_av, EDBOUT );
563 ps_print( ps, "\n" );
566 if ( davs == NULL ) {
567 davs = avs_comp_cpy( oavs );
569 tmp = avs_comp_cpy( oavs );
570 tmp->avseq_next = davs;
573 } else { /* value to keep */
575 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
576 ps_print( ps, "value to leave alone " );
577 AttrV_print( ps, &oavs->avseq_av, EDBOUT );
578 ps_print( ps, "\n" );
581 if ( prev_navs == NULL ) {
582 nas->attr_value = navs->avseq_next;
584 prev_navs->avseq_next = navs->avseq_next;
586 avs_comp_free( navs );
590 if ( davs == NULL && nas->attr_value == NULL ) {
592 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
593 ps_print( ps, " nothing to do" );
598 /* Must add new values before removing old values.
599 * Otherwise, removing all existing values causes the
600 * attribute to be removed such that subsequent add values
603 if ( nas->attr_value != NULL ) { /* add new values */
605 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
606 AttrT_print( ps, nas->attr_type, EDBOUT );
607 ps_print( ps, ": some to add\n" );
610 rem->em_type = EM_ADDVALUES;
612 rem->em_next = NULLMOD;
615 if ( davs != NULL ) { /* delete old values */
617 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
618 AttrT_print( ps, nas->attr_type, EDBOUT );
619 ps_print( ps, ": some to delete\n" );
622 if ( nas->attr_value != NULL ) {
623 rem->em_next = (struct entrymod *) calloc( 1,
624 sizeof(struct entrymod) );
627 rem->em_type = EM_REMOVEVALUES;
628 rem->em_what = as_comp_new( NULLAttrT, NULLAV,
630 rem->em_what->attr_type = AttrT_cpy( nas->attr_type );
631 rem->em_what->attr_value = davs;