]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_utils.c
improve usability of global ovrlays for write operations; may need to anticipate...
[openldap] / servers / slapd / slapi / slapi_utils.c
index d9ceeb43db86a6b71f12748b22f4e515d6bef7c1..f1e2b4e73a6dc506c671ef35c326e5f18850d6c2 100644 (file)
@@ -26,7 +26,6 @@
 #include <ac/stdarg.h>
 #include <ac/ctype.h>
 #include <ac/unistd.h>
-#include <ldap_pvt.h>
 
 #include <slap.h>
 #include <slapi.h>
@@ -109,7 +108,7 @@ bvptr2obj(
 Slapi_Entry *
 slapi_str2entry(
        char            *s, 
-       int             check_dup )
+       int             flags )
 {
 #ifdef LDAP_SLAPI
        Slapi_Entry     *e = NULL;
@@ -348,7 +347,7 @@ slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+long
 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -373,7 +372,7 @@ slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+unsigned int
 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -398,7 +397,7 @@ slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+unsigned long
 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -529,6 +528,64 @@ slapi_is_rootdse( const char *dn )
 #endif
 }
 
+int
+slapi_entry_has_children(const Slapi_Entry *e)
+{
+#ifdef LDAP_SLAPI
+       Connection *pConn;
+       Operation *op;
+       int hasSubordinates = 0;
+
+       pConn = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
+       if ( pConn == NULL ) {
+               return 0;
+       }
+
+       op = (Operation *)pConn->c_pending_ops.stqh_first;
+       op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
+       if ( op->o_bd == NULL ) {
+               return 0;
+       }
+
+       op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
+
+       slapi_int_connection_destroy( &pConn );
+
+       return ( hasSubordinates == LDAP_COMPARE_TRUE );
+#else
+       return 0;
+#endif
+}
+
+/*
+ * Return approximate size of the entry rounded to the nearest
+ * 1K. Only the size of the attribute values are counted in the
+ * Sun implementation.
+ *
+ * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
+ */
+size_t slapi_entry_size(Slapi_Entry *e)
+{
+#ifdef LDAP_SLAPI
+       size_t size;
+       Attribute *a;
+       int i;
+
+       for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
+               for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
+                       size += a->a_vals[i].bv_len + 1;
+               }
+       }
+
+       size += 1023;
+       size -= (size % 1024);
+
+       return size;
+#else
+       return 0;
+#endif /* LDAP_SLAPI */
+}
+
 /*
  * Add values to entry.
  *
@@ -1365,7 +1422,7 @@ slapi_register_supported_control(
 
        slapiControlOp2SlapControlMask( controlops, &controlmask );
 
-       register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control );
+       register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
 #endif /* LDAP_SLAPI */
 }
 
@@ -1571,6 +1628,7 @@ slapi_send_ldap_search_entry(
        rs.sr_ref = NULL;
        rs.sr_ctrls = ectrls;
        rs.sr_attrs = an;
+       rs.sr_operational_attrs = NULL;
        rs.sr_entry = e;
        rs.sr_v2ref = NULL;
        rs.sr_flags = 0;
@@ -1610,6 +1668,7 @@ slapi_send_ldap_search_reference(
 
        rs.sr_ctrls = ectrls;
        rs.sr_attrs = NULL;
+       rs.sr_operational_attrs = NULL;
        rs.sr_entry = e;
 
        if ( v2refs != NULL ) {
@@ -2638,10 +2697,38 @@ Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
 {
 #ifdef LDAP_SLAPI
-       /*
-        * FIXME: here we may lose alignment between a_vals/a_nvals
-        */
-       return value_add_one( &a->a_vals, (Slapi_Value *)v );
+       struct berval nval;
+       struct berval *nvalp;
+       int rc;
+       AttributeDescription *desc = a->a_desc;
+
+       if ( desc->ad_type->sat_equality &&
+            desc->ad_type->sat_equality->smr_normalize ) {
+               rc = (*desc->ad_type->sat_equality->smr_normalize)(
+                       SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                       desc->ad_type->sat_syntax,
+                       desc->ad_type->sat_equality,
+                       (Slapi_Value *)v, &nval, NULL );
+               if ( rc != LDAP_SUCCESS ) {
+                       return rc;
+               }
+               nvalp = &nval;
+       } else {
+               nvalp = NULL;
+       }
+
+       rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
+       if ( rc == 0 && nvalp != NULL ) {
+               rc = value_add_one( &a->a_nvals, nvalp );
+       } else {
+               a->a_nvals = a->a_vals;
+       }
+
+       if ( nvalp != NULL ) {
+               slapi_ch_free_string( &nval.bv_val );
+       }
+
+       return rc;
 #else
        return -1;
 #endif
@@ -3422,6 +3509,10 @@ Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
        Modifications *modlist = NULL, **modtail;
        LDAPMod **modp;
 
+       if ( mods == NULL ) {
+               return NULL;
+       }
+
        modtail = &modlist;
 
        for( modp = mods; *modp != NULL; modp++ ) {
@@ -3584,7 +3675,7 @@ int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_
                return 1;
        }
 
-       if ( !c->cac_attrsonly ) {
+       if ( !c->cac_attrsonly && a->a_vals != NULL ) {
                for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
                        if ( !access_allowed( op, e,
                                desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
@@ -3954,3 +4045,225 @@ int slapi_int_access_allowed( Operation *op,
 #endif /* LDAP_SLAPI */
 }
 
+/*
+ * There is no documentation for this.
+ */
+int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
+{
+#ifdef LDAP_SLAPI
+       LDAPRDN lrdn;
+       LDAPAVA *ava;
+       int rc;
+       char *p;
+
+       *type = NULL;
+
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
+
+       rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return -1;
+       }
+
+       if ( lrdn[1] != NULL ) {
+               return -1; /* not single valued */
+       }
+
+       ava = lrdn[0];
+
+       *type = slapi_ch_strdup( ava->la_attr.bv_val );
+       ber_dupbv( bv, &ava->la_value );
+
+       ldap_rdnfree(lrdn);
+
+       return 0;
+#else
+       return -1;
+#endif /* LDAP_SLAPI */
+}
+
+char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
+{
+#ifdef LDAP_SLAPI
+       struct berval new_dn, parent_dn, newrdn;
+
+       new_dn.bv_val = NULL;
+
+       parent_dn.bv_val = (char *)dn;
+       parent_dn.bv_len = strlen( dn );
+
+       newrdn.bv_val = (char *)rdn;
+       newrdn.bv_len = strlen( rdn );
+
+       build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
+
+       return new_dn.bv_val;
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       Backend *be;
+       const char *text;
+       char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
+       size_t textlen = sizeof textbuf;
+       int rc;
+
+       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
+               return -1;
+
+       rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
+
+       return ( rc == LDAP_SUCCESS ) ? 0 : 1;
+#else
+       return -1;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_rdn_values_present( const Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       LDAPDN dn;
+       int rc;
+       int i = 0, match = 0;
+
+       rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return 0;
+       }
+
+       if ( dn[0] != NULL ) {
+               LDAPRDN rdn = dn[0];
+
+               for ( i = 0; rdn[i] != NULL; i++ ) {
+                       LDAPAVA *ava = &rdn[0][i];
+                       Slapi_Attr *a = NULL;
+
+                       if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
+                            slapi_attr_value_find( a, &ava->la_value ) == 0 )
+                               match++;
+               }
+       }
+
+       ldap_dnfree( dn );
+
+       return ( i == match );
+#else
+       return 0;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_add_rdn_values( Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       LDAPDN dn;
+       int i, rc;
+
+       rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       if ( dn[0] != NULL ) {
+               LDAPRDN rdn = dn[0];
+               struct berval *vals[2];
+
+               for ( i = 0; rdn[i] != NULL; i++ ) {
+                       LDAPAVA *ava = &rdn[0][i];
+                       Slapi_Attr *a = NULL;
+
+                       if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
+                            slapi_attr_value_find( a, &ava->la_value ) == 0 )
+                               continue;
+
+                       vals[0] = &ava->la_value;
+                       vals[1] = NULL;
+
+                       slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
+               }
+       }
+
+       ldap_dnfree( dn );
+
+       return LDAP_SUCCESS;
+#else
+       return LDAP_OTHER;
+#endif /* LDAP_SLAPI */
+}
+
+const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       Attribute *attr;
+       const char *uniqueid;
+
+       attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
+       if ( attr == NULL ) {
+               return NULL;
+       }
+
+       if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
+               return slapi_value_get_string( &attr->a_vals[0] );
+       }
+#endif /* LDAP_SLAPI */
+
+       return NULL;
+}
+
+void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
+{
+#ifdef LDAP_SLAPI
+       struct berval bv;
+
+       attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
+
+       bv.bv_val = uniqueid;
+       bv.bv_len = strlen( uniqueid );
+       attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
+#endif /* LDAP_SLAPI */
+}
+
+LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
+{
+#ifdef LDAP_SLAPI
+       LDAP *ld;
+       char *url;
+       size_t size;
+       int rc;
+
+       size = sizeof("ldap:///");
+       if ( secure )
+               size++;
+       size += strlen( ldaphost );
+       if ( ldapport != 0 )
+               size += 32;
+
+       url = slapi_ch_malloc( size );
+
+       if ( ldapport != 0 ) {
+               sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
+       } else {
+               sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
+       }
+
+       rc = ldap_initialize( &ld, url );
+
+       slapi_ch_free_string( &url );
+
+       return ( rc == LDAP_SUCCESS ) ? ld : NULL;
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
+}
+
+void slapi_ldap_unbind( LDAP *ld )
+{
+#ifdef LDAP_SLAPI
+       ldap_unbind( ld );
+#endif /* LDAP_SLAPI */
+}
+