]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/modify.c
Fix memory leak.
[openldap] / servers / slapd / modify.c
index 87ae7a13fbd4862e87ea738d4759313b28078dfe..af40315e35c028dbb374e33aaa97d8f58f71e0dd 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /*
@@ -49,7 +49,7 @@ do_modify(
        Modifications *mods = NULL;
        Backend         *be;
        int rc;
-       char    *text;
+       const char      *text;
 
        Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
 
@@ -193,7 +193,7 @@ do_modify(
                Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
                       0, 0, 0 );
                send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
-                                 NULL, "database is read-only", NULL, NULL );
+                                 NULL, "directory is read-only", NULL, NULL );
                goto cleanup;
        }
 
@@ -218,7 +218,7 @@ do_modify(
                {
                        int update = be->be_update_ndn != NULL;
 #ifdef SLAPD_SCHEMA_NOT_COMPAT
-                       char *text;
+                       const char *text;
                        rc = slap_modlist2mods( modlist, update, &mods, &text );
 
                        if( rc != LDAP_SUCCESS ) {
@@ -235,7 +235,14 @@ do_modify(
                                global_lastmod == ON)) && !update )
                        {
 #ifdef SLAPD_SCHEMA_NOT_COMPAT
-                               rc = slap_mods_opattrs( op, &mods, &text );
+                               Modifications **modstail;
+                               for( modstail = &mods;
+                                       *modstail != NULL;
+                                       modstail = &(*modstail)->sml_next )
+                               {
+                                       /* empty */
+                               }
+                               rc = slap_mods_opattrs( op, modstail, &text );
 #else
                                char *text = "no-user-modification attribute type";
                                rc = add_modified_attrs( op, &mods );
@@ -269,7 +276,7 @@ do_modify(
                }
        } else {
                send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
-                   NULL, "Function not implemented", NULL, NULL );
+                   NULL, "operation not supported within namingContext", NULL, NULL );
        }
 
 cleanup:
@@ -291,30 +298,33 @@ int slap_modlist2mods(
        LDAPModList *ml,
        int update,
        Modifications **mods,
-       char **text )
+       const char **text )
 {
        int rc;
        Modifications **modtail = mods;
 
        for( ; ml != NULL; ml = ml->ml_next ) {
                Modifications *mod;
-               AttributeDescription *ad;
+               AttributeDescription *ad = NULL;
 
                mod = (Modifications *)
                        ch_calloc( 1, sizeof(Modifications) );
 
-               ad = mod->sml_desc;
+               /* copy the op */
+               mod->sml_op = ml->ml_op;
 
                /* convert to attribute description */
-               rc = slap_str2ad( ml->ml_type, &ad, text );
+               rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
 
                if( rc != LDAP_SUCCESS ) {
                        slap_mods_free( mod );
                        return rc;
                }
 
-               if((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
-                       && !( ad->ad_flags & SLAP_DESC_BINARY ))
+               ad = mod->sml_desc;
+
+               if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
+                       && !slap_ad_is_binary( ad ))
                {
                        /* attribute requires binary transfer */
                        slap_mods_free( mod );
@@ -322,6 +332,15 @@ int slap_modlist2mods(
                        return LDAP_UNDEFINED_TYPE;
                }
 
+               if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
+                       && slap_ad_is_binary( ad ))
+               {
+                       /* attribute requires binary transfer */
+                       slap_mods_free( mod );
+                       *text = "attribute disallows ;binary transfer";
+                       return LDAP_UNDEFINED_TYPE;
+               }
+
                if (!update && is_at_no_user_mod( ad->ad_type )) {
                        /* user modification disallowed */
                        slap_mods_free( mod );
@@ -337,6 +356,15 @@ int slap_modlist2mods(
                        slap_syntax_validate_func *validate =
                                ad->ad_type->sat_syntax->ssyn_validate;
 
+                       if( !validate ) {
+                               Debug( LDAP_DEBUG_TRACE,
+                                       "modlist2mods: no validator for syntax %s\n",
+                                       ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
+                               slap_mods_free( mod );
+                               *text = "no validator for syntax";
+                               return LDAP_INVALID_SYNTAX;
+                       }
+
                        /*
                         * check that each value is valid per syntax
                         */
@@ -354,7 +382,7 @@ int slap_modlist2mods(
                         * a rough single value check... an additional check is needed
                         * to catch add of single value to existing single valued attribute
                         */
-                       if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
+                       if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
                                && nvals > 1 && is_at_single_value( ad->ad_type ))
                        {
                                slap_mods_free( mod );
@@ -376,19 +404,20 @@ int slap_modlist2mods(
 int slap_mods_opattrs(
        Operation *op,
        Modifications **modtail,
-       char **text )
+       const char **text )
 {
-       int rc;
        struct berval name, timestamp;
        time_t now = slap_get_time();
        char timebuf[22];
        struct tm *ltm;
        Modifications *mod;
-       AttributeDescription *ad;
 
        int mop = op->o_tag == LDAP_REQ_ADD
                ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
 
+       assert( modtail != NULL );
+       assert( *modtail == NULL );
+
        ldap_pvt_thread_mutex_lock( &gmtime_mutex );
        ltm = gmtime( &now );
        strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
@@ -397,57 +426,27 @@ int slap_mods_opattrs(
        timestamp.bv_len = strlen(timebuf);
 
        if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
-               name.bv_val = "<anonymous>";
-               name.bv_len = sizeof("<anonymous>")-1;
+               name.bv_val = SLAPD_ANONYMOUS;
+               name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
        } else {
                name.bv_val = op->o_dn;
                name.bv_len = strlen( op->o_dn );
        }
 
        if( op->o_tag == LDAP_REQ_ADD ) {
-               rc = slap_str2ad( "creatorsName", &ad, text );
-               if( rc == LDAP_SUCCESS ) {
-                       mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
-                       mod->sml_op = mop;
-                       mod->sml_desc = ad;
-                       mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
-                       mod->sml_bvalues[0] = ber_bvdup( &name );
-                       mod->sml_bvalues[1] = NULL;
-
-                       *modtail = mod;
-                       modtail = &mod->sml_next;
-               }
-
-               rc = slap_str2ad( "createTimeStamp", &ad, text );
-               if( rc == LDAP_SUCCESS ) {
-                       mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
-                       mod->sml_op = mop;
-                       mod->sml_desc = ad;
-                       mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
-                       mod->sml_bvalues[0] = ber_bvdup( &timestamp );
-                       mod->sml_bvalues[1] = NULL;
-                       *modtail = mod;
-                       modtail = &mod->sml_next;
-               }
-       }
-
-       rc = slap_str2ad( "modifiersName", &ad, text );
-       if( rc == LDAP_SUCCESS ) {
                mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
                mod->sml_op = mop;
-               mod->sml_desc = ad;
+               mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
                mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
                mod->sml_bvalues[0] = ber_bvdup( &name );
                mod->sml_bvalues[1] = NULL;
+
                *modtail = mod;
                modtail = &mod->sml_next;
-       }
 
-       rc = slap_str2ad( "modifyTimeStamp", &ad, text );
-       if( rc == LDAP_SUCCESS ) {
                mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
                mod->sml_op = mop;
-               mod->sml_desc = ad;
+               mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
                mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
                mod->sml_bvalues[0] = ber_bvdup( &timestamp );
                mod->sml_bvalues[1] = NULL;
@@ -455,6 +454,24 @@ int slap_mods_opattrs(
                modtail = &mod->sml_next;
        }
 
+       mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
+       mod->sml_op = mop;
+       mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
+       mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
+       mod->sml_bvalues[0] = ber_bvdup( &name );
+       mod->sml_bvalues[1] = NULL;
+       *modtail = mod;
+       modtail = &mod->sml_next;
+
+       mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
+       mod->sml_op = mop;
+       mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
+       mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
+       mod->sml_bvalues[0] = ber_bvdup( &timestamp );
+       mod->sml_bvalues[1] = NULL;
+       *modtail = mod;
+       modtail = &mod->sml_next;
+
        return LDAP_SUCCESS;
 }
 
@@ -480,8 +497,8 @@ add_modified_attrs( Operation *op, Modifications **modlist )
        }
 
        if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
-               bv.bv_val = "<anonymous>";
-               bv.bv_len = sizeof("<anonymous>")-1;
+               bv.bv_val = SLAPD_ANONYMOUS;
+               bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
        } else {
                bv.bv_val = op->o_dn;
                bv.bv_len = strlen( bv.bv_val );