Entry * wt_tool_entry_get( BackendDB *be, ID id ) { Entry *e = NULL; static EntryHeader eh; int rc, eoff; assert( be != NULL ); assert( slapMode & SLAP_TOOL_MODE ); rc = wt_entry_header( &item, &eh ); assert( rc == 0 ); eoff = eh.data - (char *)item.data; eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size; eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len ); memset(eh.bv.bv_val, 0xff, eh.bv.bv_len); eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval ); memcpy(eh.data, item.data, item.size); eh.data += eoff; rc = entry_decode( &eh, &e ); assert( rc == 0 ); if( rc == LDAP_SUCCESS ) { e->e_id = id; } return e; }
int slap_sasl_regexp_config( const char *match, const char *replace ) { int rc; SaslRegexp_t *reg; SaslRegexp = (SaslRegexp_t *) ch_realloc( (char *) SaslRegexp, (nSaslRegexp + 1) * sizeof(SaslRegexp_t) ); reg = &SaslRegexp[nSaslRegexp]; reg->sr_match = ch_strdup( match ); reg->sr_replace = ch_strdup( replace ); /* Precompile matching pattern */ rc = regcomp( ®->sr_workspace, reg->sr_match, REG_EXTENDED|REG_ICASE ); if ( rc ) { #ifdef NEW_LOGGING LDAP_LOG( TRANSPORT, ERR, "slap_sasl_regexp_config: \"%s\" could not be compiled.\n", reg->sr_match, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "SASL match pattern %s could not be compiled by regexp engine\n", reg->sr_match, 0, 0 ); #endif return( LDAP_OTHER ); } rc = slap_sasl_rx_off( reg->sr_replace, reg->sr_offset ); if ( rc != LDAP_SUCCESS ) return rc; nSaslRegexp++; return( LDAP_SUCCESS ); }
AttributeName* file2anlist( AttributeName *an, const char *fname, const char *brkstr ) { FILE *fp; char *line = NULL; char *lcur = NULL; char *c; size_t lmax = LBUFSIZ; fp = fopen( fname, "r" ); if ( fp == NULL ) { Debug( LDAP_DEBUG_ANY, "get_attrs_from_file: failed to open attribute list file " "\"%s\": %s\n", fname, strerror(errno), 0 ); return NULL; } lcur = line = (char *) ch_malloc( lmax ); if ( !line ) { Debug( LDAP_DEBUG_ANY, "get_attrs_from_file: could not allocate memory\n", 0, 0, 0 ); fclose(fp); return NULL; } while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) { if ( ( c = strchr( lcur, '\n' ) ) ) { if ( c == line ) { *c = '\0'; } else if ( *(c-1) == '\r' ) { *(c-1) = '\0'; } else { *c = '\0'; } } else { lmax += LBUFSIZ; line = (char *) ch_realloc( line, lmax ); if ( !line ) { Debug( LDAP_DEBUG_ANY, "get_attrs_from_file: could not allocate memory\n", 0, 0, 0 ); fclose(fp); return NULL; } lcur = line + strlen( line ); continue; } an = str2anlist( an, line, brkstr ); if ( an == NULL ) break; lcur = line; } ch_free( line ); fclose(fp); return an; }
void slapd_slp_init( const char* urls ) { int i; slapd_srvurls = ldap_str2charray( urls, " " ); if( slapd_srvurls == NULL ) return; /* find and expand INADDR_ANY URLs */ for( i=0; slapd_srvurls[i] != NULL; i++ ) { if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) { char *host = ldap_pvt_get_fqdn( NULL ); if ( host != NULL ) { slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i], strlen( host ) + sizeof( LDAP_SRVTYPE_PREFIX ) ); strcpy( lutil_strcopy(slapd_srvurls[i], LDAP_SRVTYPE_PREFIX ), host ); ch_free( host ); } } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) { char *host = ldap_pvt_get_fqdn( NULL ); if ( host != NULL ) { slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i], strlen( host ) + sizeof( LDAPS_SRVTYPE_PREFIX ) ); strcpy( lutil_strcopy(slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX ), host ); ch_free( host ); } } } /* open the SLP handle */ SLPOpen( "en", 0, &slapd_hslp ); }
static int ndb_oc_create( struct ndb_info *ni, NdbOcInfo *oci, int create ) { char buf[4096], *ptr; int i, rc = 0, col; if ( create ) { ptr = buf + sprintf( buf, "CREATE TABLE `%s` (eid bigint unsigned NOT NULL, vid int unsigned NOT NULL", oci->no_table.bv_val ); } col = 0; if ( oci->no_oc->soc_required ) { for ( i=0; oci->no_oc->soc_required[i]; i++ ); col += i; } if ( oci->no_oc->soc_allowed ) { for ( i=0; oci->no_oc->soc_allowed[i]; i++ ); col += i; } /* assume all are present */ oci->no_attrs = (struct ndb_attrinfo **)ch_malloc( col * sizeof(struct ndb_attrinfo *)); col = 2; ldap_pvt_thread_rdwr_wlock( &ni->ni_ai_rwlock ); if ( oci->no_oc->soc_required ) { rc = ndb_ai_check( ni, oci, oci->no_oc->soc_required, &ptr, &col, create ); } if ( !rc && oci->no_oc->soc_allowed ) { rc = ndb_ai_check( ni, oci, oci->no_oc->soc_allowed, &ptr, &col, create ); } ldap_pvt_thread_rdwr_wunlock( &ni->ni_ai_rwlock ); /* shrink down to just the needed size */ oci->no_attrs = (struct ndb_attrinfo **)ch_realloc( oci->no_attrs, oci->no_nattrs * sizeof(struct ndb_attrinfo *)); if ( create ) { ptr = lutil_strcopy( ptr, ", PRIMARY KEY(eid, vid) ) ENGINE=ndb PARTITION BY KEY(eid)" ); rc = mysql_real_query( &ni->ni_sql, buf, ptr - buf ); if ( rc ) { Debug( LDAP_DEBUG_ANY, "ndb_oc_create: CREATE TABLE %s failed, %s (%d)\n", oci->no_table.bv_val, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) ); } } return rc; }
static char *v2ref( BerVarray ref, const char *text ) { size_t len = 0, i = 0; char *v2; if(ref == NULL) { if (text) { return ch_strdup(text); } else { return NULL; } } if ( text != NULL ) { len = strlen( text ); if (text[len-1] != '\n') { i = 1; } } v2 = ch_malloc( len+i+sizeof("Referral:") ); if( text != NULL ) { strcpy(v2, text); if( i ) { v2[len++] = '\n'; } } strcpy( v2+len, "Referral:" ); len += sizeof("Referral:"); for( i=0; ref[i].bv_val != NULL; i++ ) { v2 = ch_realloc( v2, len + ref[i].bv_len + 1 ); v2[len-1] = '\n'; memcpy(&v2[len], ref[i].bv_val, ref[i].bv_len ); len += ref[i].bv_len; if (ref[i].bv_val[ref[i].bv_len-1] != '/') { ++len; } } v2[len-1] = '\0'; return v2; }
static int ainfo_insert( struct bdb_info *bdb, AttrInfo *a ) { int x; int i = bdb_attr_slot( bdb, a->ai_desc, &x ); /* Is it a dup? */ if ( i >= 0 ) return -1; bdb->bi_attrs = ch_realloc( bdb->bi_attrs, ( bdb->bi_nattrs+1 ) * sizeof( AttrInfo * )); if ( x < bdb->bi_nattrs ) AC_MEMCPY( &bdb->bi_attrs[x+1], &bdb->bi_attrs[x], ( bdb->bi_nattrs - x ) * sizeof( AttrInfo *)); bdb->bi_attrs[x] = a; bdb->bi_nattrs++; return 0; }
static int ainfo_insert( struct mdb_info *mdb, AttrInfo *a ) { int x; int i = mdb_attr_slot( mdb, a->ai_desc, &x ); /* Is it a dup? */ if ( i >= 0 ) return -1; mdb->mi_attrs = ch_realloc( mdb->mi_attrs, ( mdb->mi_nattrs+1 ) * sizeof( AttrInfo * )); if ( x < mdb->mi_nattrs ) AC_MEMCPY( &mdb->mi_attrs[x+1], &mdb->mi_attrs[x], ( mdb->mi_nattrs - x ) * sizeof( AttrInfo *)); mdb->mi_attrs[x] = a; mdb->mi_nattrs++; return 0; }
static int aa_add_oc( ObjectClass *oc, ObjectClass ***ocpp, AttributeType ***atpp ) { int i = 0; if ( *ocpp ) { for ( ; (*ocpp)[ i ] != NULL; i++ ) { if ( (*ocpp)[ i ] == oc ) { break; } } if ( (*ocpp)[ i ] != NULL ) { return 0; } } *ocpp = ch_realloc( *ocpp, sizeof( ObjectClass * ) * ( i + 2 ) ); (*ocpp)[ i ] = oc; (*ocpp)[ i + 1 ] = NULL; if ( oc->soc_required ) { int i; for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) { aa_add_at( oc->soc_required[ i ], atpp ); } } if ( oc->soc_allowed ) { int i; for ( i = 0; oc->soc_allowed[ i ] != NULL; i++ ) { aa_add_at( oc->soc_allowed[ i ], atpp ); } } return 0; }
void quorum_add_rid(BackendDB *bd, int rid) { int n; struct present* p; assert(rid > -1 || rid <= SLAP_SYNC_RID_MAX); assert(quorum_list != QR_POISON); lock(); if (! bd->bd_quorum) quorum_be_init(bd); Debug( LDAP_DEBUG_SYNC, "syncrep_quorum: add link-rid %d to %s\n", rid, bd->bd_quorum->qr_cluster ); n = 0; if (bd->bd_quorum->qr_present) { for(n = 0, p = bd->bd_quorum->qr_present; p->ready > -1; ++p, ++n ) { if (p->rid == rid) { assert(0); goto bailout; } } } bd->bd_quorum->qr_present = ch_realloc(bd->bd_quorum->qr_present, (n + 2) * sizeof(struct present)); p = bd->bd_quorum->qr_present + n; p->rid = rid; p->sid = -1; p->ready = 0; p[1].ready = -1; if ((bd->bd_quorum->flags & QR_AUTO_RIDS) && require_append(bd->bd_quorum, QR_VOTED_RID | QR_FLAG_AUTO, rid)) quorum_invalidate(bd); bailout: unlock(); }
void bdb_idl_cache_add_id( struct bdb_info *bdb, DB *db, DBT *key, ID id ) { bdb_idl_cache_entry_t *cache_entry, idl_tmp; DBT2bv( key, &idl_tmp.kstr ); idl_tmp.db = db; ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock ); cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp, bdb_idl_entry_cmp ); if ( cache_entry != NULL ) { if ( !BDB_IDL_IS_RANGE( cache_entry->idl ) && cache_entry->idl[0] < BDB_IDL_DB_MAX ) { size_t s = BDB_IDL_SIZEOF( cache_entry->idl ) + sizeof(ID); cache_entry->idl = ch_realloc( cache_entry->idl, s ); } bdb_idl_insert( cache_entry->idl, id ); } ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock ); }
static int aa_add_at( AttributeType *at, AttributeType ***atpp ) { int i = 0; if ( *atpp ) { for ( i = 0; (*atpp)[ i ] != NULL; i++ ) { if ( (*atpp)[ i ] == at ) { break; } } if ( (*atpp)[ i ] != NULL ) { return 0; } } *atpp = ch_realloc( *atpp, sizeof( AttributeType * ) * ( i + 2 ) ); (*atpp)[ i ] = at; (*atpp)[ i + 1 ] = NULL; return 0; }
/* * Add information about replica host specified by Ri to list * of hosts. */ static Stel * St_add( St *st, Ri *ri ) { int ind; if ( st == NULL || ri == NULL ) { return NULL; } /* Serialize access to the St struct */ ldap_pvt_thread_mutex_lock( &(st->st_mutex )); st->st_nreplicas++; ind = st->st_nreplicas - 1; st->st_data = ( Stel ** ) ch_realloc( st->st_data, ( st->st_nreplicas * sizeof( Stel * ))); if ( st->st_data == NULL ) { ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return NULL; } st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) ); if ( st->st_data[ ind ] == NULL ) { ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return NULL; } st->st_data[ ind ]->hostname = strdup( ri->ri_hostname ); st->st_data[ ind ]->port = ri->ri_port; st->st_data[ ind ]->last = 0; st->st_data[ ind ]->seq = 0; ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return st->st_data[ ind ]; }
/* * Get the next "record" from the file pointed to by fp. A "record" * is delimited by two consecutive newlines. Returns NULL on EOF. */ static char * get_record( FILE *fp ) { int len; static char line[BUFSIZ]; char *buf = NULL; static int lcur, lmax; lcur = lmax = 0; while (( fgets( line, sizeof(line), fp ) != NULL ) && (( len = strlen( line )) > 1 )) { while ( lcur + len + 1 > lmax ) { lmax += BUFSIZ; buf = (char *) ch_realloc( buf, lmax ); } strcpy( buf + lcur, line ); lcur += len; } return( buf ); }
static int require_append(slap_quorum_t *q, int type, int id) { int n; struct requirment* r; assert(id > -1); if (QR_IS_SID(type)) assert(id <= SLAP_SYNC_SID_MAX); else if (QR_IS_RID(type)) assert(id <= SLAP_SYNC_RID_MAX); else assert(0); for(n = 0, r = q->qr_requirements; r && r->type > -1; ++r, ++n ) { if (r->id == id && QR_IS_SID(r->type) == QR_IS_SID(type)) { if (QR_IS_AUTO(r->type) && ! QR_IS_AUTO(type)) { r->type = type; goto ok; } return 0; } } q->qr_requirements = ch_realloc(q->qr_requirements, (n + 2) * sizeof(struct requirment)); r = q->qr_requirements + n; r->id = id; r->type = type; r[1].type = -1; ok: Debug( LDAP_DEBUG_SYNC, "syncrep_quorum: added %s%s-%s %d to %s requirements as #%d\n", QR_IS_AUTO(type) ? "auto-" : "", QR_IS_DEMAND(type) ? "demand" : "vote", QR_IS_SID(type) ? "sid" : "rid", id, q->qr_cluster, n ); return 1; }
static int ndb_tool_next_id( Operation *op, NdbArgs *NA, struct berval *text, int hole ) { struct berval ndn = NA->e->e_nname; int rc; if (ndn.bv_len == 0) { NA->e->e_id = 0; return 0; } rc = ndb_entry_get_info( op, NA, 0, NULL ); if ( rc ) { Attribute *a, tmp = {0}; if ( !be_issuffix( op->o_bd, &ndn ) ) { struct dn_id *dptr; struct berval npdn; dnParent( &ndn, &npdn ); NA->e->e_nname = npdn; NA->rdns->nr_num--; rc = ndb_tool_next_id( op, NA, text, 1 ); NA->e->e_nname = ndn; NA->rdns->nr_num++; if ( rc ) { return rc; } /* If parent didn't exist, it was created just now * and its ID is now in e->e_id. */ dptr = (struct dn_id *)ch_malloc( sizeof( struct dn_id ) + npdn.bv_len + 1); dptr->id = NA->e->e_id; dptr->dn.bv_val = (char *)(dptr+1); strcpy(dptr->dn.bv_val, npdn.bv_val ); dptr->dn.bv_len = npdn.bv_len; if ( avl_insert( &myParents, dptr, ndb_dnid_cmp, avl_dup_error )) { ch_free( dptr ); } } rc = ndb_next_id( op->o_bd, myNdb, &NA->e->e_id ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "next_id failed: %s (%d)", myNdb->getNdbError().message, myNdb->getNdbError().code ); Debug( LDAP_DEBUG_ANY, "=> ndb_tool_next_id: %s\n", text->bv_val, 0, 0 ); return rc; } if ( hole ) { a = NA->e->e_attrs; NA->e->e_attrs = &tmp; tmp.a_desc = slap_schema.si_ad_objectClass; tmp.a_vals = glueval; tmp.a_nvals = tmp.a_vals; tmp.a_numvals = 1; } rc = ndb_entry_put_info( op->o_bd, NA, 0 ); if ( hole ) { NA->e->e_attrs = a; } if ( rc ) { snprintf( text->bv_val, text->bv_len, "ndb_entry_put_info failed: %s (%d)", myNdb->getNdbError().message, myNdb->getNdbError().code ); Debug( LDAP_DEBUG_ANY, "=> ndb_tool_next_id: %s\n", text->bv_val, 0, 0 ); } else if ( hole ) { if ( nholes == nhmax - 1 ) { if ( holes == hbuf ) { holes = (dn_id *)ch_malloc( nhmax * sizeof(dn_id) * 2 ); AC_MEMCPY( holes, hbuf, sizeof(hbuf) ); } else { holes = (dn_id *)ch_realloc( holes, nhmax * sizeof(dn_id) * 2 ); } nhmax *= 2; } ber_dupbv( &holes[nholes].dn, &ndn ); holes[nholes++].id = NA->e->e_id; } } else if ( !hole ) { unsigned i; for ( i=0; i<nholes; i++) { if ( holes[i].id == NA->e->e_id ) { int j; free(holes[i].dn.bv_val); for (j=i;j<nholes;j++) holes[j] = holes[j+1]; holes[j].id = 0; nholes--; rc = ndb_entry_put_info( op->o_bd, NA, 1 ); break; } else if ( holes[i].id > NA->e->e_id ) { break; } } } return rc; }
int slap_mods2entry( Modifications *mods, Entry **e, int repl_user, const char **text, char *textbuf, size_t textlen ) { Attribute **tail = &(*e)->e_attrs; assert( *tail == NULL ); *text = textbuf; for( ; mods != NULL; mods = mods->sml_next ) { Attribute *attr; assert( mods->sml_op == LDAP_MOD_ADD ); assert( mods->sml_desc != NULL ); attr = attr_find( (*e)->e_attrs, mods->sml_desc ); if( attr != NULL ) { #define SLURPD_FRIENDLY #ifdef SLURPD_FRIENDLY ber_len_t i,j; if( !repl_user ) { snprintf( textbuf, textlen, "attribute '%s' provided more than once", mods->sml_desc->ad_cname.bv_val ); return LDAP_TYPE_OR_VALUE_EXISTS; } for( i=0; attr->a_vals[i].bv_val; i++ ) { /* count them */ } for( j=0; mods->sml_bvalues[j].bv_val; j++ ) { /* count them */ } j++; /* NULL */ attr->a_vals = ch_realloc( attr->a_vals, sizeof( struct berval ) * (i+j) ); /* should check for duplicates */ AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues, sizeof( struct berval ) * j ); /* trim the mods array */ ch_free( mods->sml_bvalues ); mods->sml_bvalues = NULL; continue; #else snprintf( textbuf, textlen, "attribute '%s' provided more than once", mods->sml_desc->ad_cname.bv_val ); return LDAP_TYPE_OR_VALUE_EXISTS; #endif } if( mods->sml_bvalues[1].bv_val != NULL ) { /* check for duplicates */ int i, j; MatchingRule *mr = mods->sml_desc->ad_type->sat_equality; /* check if the values we're adding already exist */ if( mr == NULL || !mr->smr_match ) { for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) { /* test asserted values against themselves */ for( j = 0; j < i; j++ ) { if ( bvmatch( &mods->sml_bvalues[i], &mods->sml_bvalues[j] ) ) { /* value exists already */ snprintf( textbuf, textlen, "%s: value #%d provided more than once", mods->sml_desc->ad_cname.bv_val, j ); return LDAP_TYPE_OR_VALUE_EXISTS; } } } } else { int rc; rc = modify_check_duplicates( mods->sml_desc, mr, NULL, mods->sml_bvalues, 0, text, textbuf, textlen ); if ( rc != LDAP_SUCCESS ) { return rc; } } } attr = ch_calloc( 1, sizeof(Attribute) ); /* move ad to attr structure */ attr->a_desc = mods->sml_desc; mods->sml_desc = NULL; /* move values to attr structure */ /* should check for duplicates */ attr->a_vals = mods->sml_bvalues; mods->sml_bvalues = NULL; *tail = attr; tail = &attr->a_next; } return LDAP_SUCCESS; }
static int limits_add( Backend *be, unsigned flags, const char *pattern, ObjectClass *group_oc, AttributeDescription *group_ad, struct slap_limits_set *limit ) { int i; struct slap_limits *lm; unsigned type, style; assert( be != NULL ); assert( limit != NULL ); type = flags & SLAP_LIMITS_TYPE_MASK; style = flags & SLAP_LIMITS_MASK; switch ( style ) { case SLAP_LIMITS_ANONYMOUS: case SLAP_LIMITS_USERS: case SLAP_LIMITS_ANY: /* For these styles, type == 0 (SLAP_LIMITS_TYPE_SELF). */ for ( i = 0; be->be_limits && be->be_limits[ i ]; i++ ) { if ( be->be_limits[ i ]->lm_flags == style ) { return( -1 ); } } break; } lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 ); switch ( style ) { case SLAP_LIMITS_UNDEFINED: style = SLAP_LIMITS_EXACT; /* continue to next cases */ case SLAP_LIMITS_EXACT: case SLAP_LIMITS_ONE: case SLAP_LIMITS_SUBTREE: case SLAP_LIMITS_CHILDREN: { int rc; struct berval bv; ber_str2bv( pattern, 0, 0, &bv ); rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL ); if ( rc != LDAP_SUCCESS ) { ch_free( lm ); return( -1 ); } } break; case SLAP_LIMITS_REGEX: ber_str2bv( pattern, 0, 1, &lm->lm_pat ); if ( regcomp( &lm->lm_regex, lm->lm_pat.bv_val, REG_EXTENDED | REG_ICASE ) ) { free( lm->lm_pat.bv_val ); ch_free( lm ); return( -1 ); } break; case SLAP_LIMITS_ANONYMOUS: case SLAP_LIMITS_USERS: case SLAP_LIMITS_ANY: BER_BVZERO( &lm->lm_pat ); break; } switch ( type ) { case SLAP_LIMITS_TYPE_GROUP: assert( group_oc != NULL ); assert( group_ad != NULL ); lm->lm_group_oc = group_oc; lm->lm_group_ad = group_ad; break; } lm->lm_flags = style | type; lm->lm_limits = *limit; i = 0; if ( be->be_limits != NULL ) { for ( ; be->be_limits[i]; i++ ); } be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits, sizeof( struct slap_limits * ) * ( i + 2 ) ); be->be_limits[i] = lm; be->be_limits[i+1] = NULL; return( 0 ); }
static int bdb_tool_next_id( Operation *op, DB_TXN *tid, Entry *e, struct berval *text, int hole ) { struct berval dn = e->e_name; struct berval ndn = e->e_nname; struct berval pdn, npdn; EntryInfo *ei = NULL, eidummy; int rc; if (ndn.bv_len == 0) { e->e_id = 0; return 0; } rc = bdb_cache_find_ndn( op, tid, &ndn, &ei ); if ( ei ) bdb_cache_entryinfo_unlock( ei ); if ( rc == DB_NOTFOUND ) { if ( !be_issuffix( op->o_bd, &ndn ) ) { ID eid = e->e_id; dnParent( &dn, &pdn ); dnParent( &ndn, &npdn ); e->e_name = pdn; e->e_nname = npdn; rc = bdb_tool_next_id( op, tid, e, text, 1 ); e->e_name = dn; e->e_nname = ndn; if ( rc ) { return rc; } /* If parent didn't exist, it was created just now * and its ID is now in e->e_id. Make sure the current * entry gets added under the new parent ID. */ if ( eid != e->e_id ) { eidummy.bei_id = e->e_id; ei = &eidummy; } } rc = bdb_next_id( op->o_bd, &e->e_id ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "next_id failed: %s (%d)", db_strerror(rc), rc ); Debug( LDAP_DEBUG_ANY, "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); return rc; } rc = bdb_dn2id_add( op, tid, ei, e ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "dn2id_add failed: %s (%d)", db_strerror(rc), rc ); Debug( LDAP_DEBUG_ANY, "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); } else if ( hole ) { if ( nholes == nhmax - 1 ) { if ( holes == hbuf ) { holes = ch_malloc( nhmax * sizeof(dn_id) * 2 ); AC_MEMCPY( holes, hbuf, sizeof(hbuf) ); } else { holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 ); } nhmax *= 2; } ber_dupbv( &holes[nholes].dn, &ndn ); holes[nholes++].id = e->e_id; } } else if ( !hole ) { unsigned i, j; e->e_id = ei->bei_id; for ( i=0; i<nholes; i++) { if ( holes[i].id == e->e_id ) { free(holes[i].dn.bv_val); for (j=i;j<nholes;j++) holes[j] = holes[j+1]; holes[j].id = 0; nholes--; break; } else if ( holes[i].id > e->e_id ) { break; } } } return rc; }
/* Define an attribute option. */ int ad_define_option( const char *name, const char *fname, int lineno ) { int i; unsigned int optlen; if ( options == &lang_option ) { options = NULL; option_count = 0; } if ( name == NULL ) return 0; optlen = 0; do { if ( !DESC_CHAR( name[optlen] ) ) { /* allow trailing '=', same as '-' */ if ( name[optlen] == '=' && !name[optlen+1] ) { msad_range_hack = 1; continue; } Debug( LDAP_DEBUG_ANY, "%s: line %d: illegal option name \"%s\"\n", fname, lineno, name ); return 1; } } while ( name[++optlen] ); options = ch_realloc( options, (option_count+1) * sizeof(Attr_option) ); if ( strcasecmp( name, "binary" ) == 0 || ad_find_option_definition( name, optlen ) ) { Debug( LDAP_DEBUG_ANY, "%s: line %d: option \"%s\" is already defined\n", fname, lineno, name ); return 1; } for ( i = option_count; i; --i ) { if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 ) break; options[i] = options[i-1]; } options[i].name.bv_val = ch_strdup( name ); options[i].name.bv_len = optlen; options[i].prefix = (name[optlen-1] == '-') || (name[optlen-1] == '='); if ( i != option_count && options[i].prefix && optlen < options[i+1].name.bv_len && strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) { Debug( LDAP_DEBUG_ANY, "%s: line %d: option \"%s\" overrides previous option\n", fname, lineno, name ); return 1; } option_count++; return 0; }
static int backsql_get_attr_vals( void *v_at, void *v_bsi ) { backsql_at_map_rec *at = v_at; backsql_srch_info *bsi = v_bsi; backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; RETCODE rc; SQLHSTMT sth = SQL_NULL_HSTMT; BACKSQL_ROW_NTS row; unsigned long i, k = 0, oldcount = 0, res = 0; #ifdef BACKSQL_COUNTQUERY unsigned count, j, append = 0; SQLLEN countsize = sizeof( count ); Attribute *attr = NULL; slap_mr_normalize_func *normfunc = NULL; #endif /* BACKSQL_COUNTQUERY */ #ifdef BACKSQL_PRETTY_VALIDATE slap_syntax_validate_func *validate = NULL; slap_syntax_transform_func *pretty = NULL; #endif /* BACKSQL_PRETTY_VALIDATE */ assert( at != NULL ); assert( bsi != NULL ); #ifdef BACKSQL_ARBITRARY_KEY Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): " "oc=\"%s\" attr=\"%s\" keyval=%s\n", BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val, bsi->bsi_c_eid->eid_keyval.bv_val ); #else /* ! BACKSQL_ARBITRARY_KEY */ Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): " "oc=\"%s\" attr=\"%s\" keyval=%ld\n", BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val, bsi->bsi_c_eid->eid_keyval ); #endif /* ! BACKSQL_ARBITRARY_KEY */ #ifdef BACKSQL_PRETTY_VALIDATE validate = at->bam_true_ad->ad_type->sat_syntax->ssyn_validate; pretty = at->bam_true_ad->ad_type->sat_syntax->ssyn_pretty; if ( validate == NULL && pretty == NULL ) { return 1; } #endif /* BACKSQL_PRETTY_VALIDATE */ #ifdef BACKSQL_COUNTQUERY if ( at->bam_true_ad->ad_type->sat_equality ) { normfunc = at->bam_true_ad->ad_type->sat_equality->smr_normalize; } /* Count how many rows will be returned. This avoids memory * fragmentation that can result from loading the values in * one by one and using realloc() */ rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_countquery, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error preparing count query: %s\n", at->bam_countquery, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); return 1; } rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &bsi->bsi_c_eid->eid_keyval ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error binding key value parameter\n", 0, 0, 0 ); SQLFreeStmt( sth, SQL_DROP ); return 1; } rc = SQLExecute( sth ); if ( ! BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error executing attribute count query '%s'\n", at->bam_countquery, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); return 1; } SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_LONG, (SQLPOINTER)&count, (SQLINTEGER)sizeof( count ), &countsize ); rc = SQLFetch( sth ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error fetch results of count query: %s\n", at->bam_countquery, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); return 1; } Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "number of values in query: %u\n", count, 0, 0 ); SQLFreeStmt( sth, SQL_DROP ); if ( count == 0 ) { return 1; } attr = attr_find( bsi->bsi_e->e_attrs, at->bam_true_ad ); if ( attr != NULL ) { BerVarray tmp; if ( attr->a_vals != NULL ) { oldcount = attr->a_numvals; } tmp = ch_realloc( attr->a_vals, ( oldcount + count + 1 ) * sizeof( struct berval ) ); if ( tmp == NULL ) { return 1; } attr->a_vals = tmp; memset( &attr->a_vals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) ); if ( normfunc ) { tmp = ch_realloc( attr->a_nvals, ( oldcount + count + 1 ) * sizeof( struct berval ) ); if ( tmp == NULL ) { return 1; } attr->a_nvals = tmp; memset( &attr->a_nvals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) ); } else { attr->a_nvals = attr->a_vals; } attr->a_numvals += count; } else { append = 1; /* Make space for the array of values */ attr = attr_alloc( at->bam_true_ad ); attr->a_numvals = count; attr->a_vals = ch_calloc( count + 1, sizeof( struct berval ) ); if ( attr->a_vals == NULL ) { Debug( LDAP_DEBUG_TRACE, "Out of memory!\n", 0,0,0 ); ch_free( attr ); return 1; } if ( normfunc ) { attr->a_nvals = ch_calloc( count + 1, sizeof( struct berval ) ); if ( attr->a_nvals == NULL ) { ch_free( attr->a_vals ); ch_free( attr ); return 1; } } else { attr->a_nvals = attr->a_vals; } } #endif /* BACKSQL_COUNTQUERY */ rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error preparing query: %s\n", at->bam_query, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); #ifdef BACKSQL_COUNTQUERY if ( append ) { attr_free( attr ); } #endif /* BACKSQL_COUNTQUERY */ return 1; } rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &bsi->bsi_c_eid->eid_keyval ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error binding key value parameter\n", 0, 0, 0 ); #ifdef BACKSQL_COUNTQUERY if ( append ) { attr_free( attr ); } #endif /* BACKSQL_COUNTQUERY */ return 1; } #ifdef BACKSQL_TRACE #ifdef BACKSQL_ARBITRARY_KEY Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "query=\"%s\" keyval=%s\n", at->bam_query, bsi->bsi_c_eid->eid_keyval.bv_val, 0 ); #else /* !BACKSQL_ARBITRARY_KEY */ Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "query=\"%s\" keyval=%d\n", at->bam_query, bsi->bsi_c_eid->eid_keyval, 0 ); #endif /* ! BACKSQL_ARBITRARY_KEY */ #endif /* BACKSQL_TRACE */ rc = SQLExecute( sth ); if ( ! BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): " "error executing attribute query \"%s\"\n", at->bam_query, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); #ifdef BACKSQL_COUNTQUERY if ( append ) { attr_free( attr ); } #endif /* BACKSQL_COUNTQUERY */ return 1; } backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx ); #ifdef BACKSQL_COUNTQUERY j = oldcount; #endif /* BACKSQL_COUNTQUERY */ for ( rc = SQLFetch( sth ), k = 0; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ), k++ ) { for ( i = 0; i < (unsigned long)row.ncols; i++ ) { if ( row.value_len[ i ] > 0 ) { struct berval bv; int retval; #ifdef BACKSQL_TRACE AttributeDescription *ad = NULL; const char *text; retval = slap_bv2ad( &row.col_names[ i ], &ad, &text ); if ( retval != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_ANY, "==>backsql_get_attr_vals(\"%s\"): " "unable to find AttributeDescription %s " "in schema (%d)\n", bsi->bsi_e->e_name.bv_val, row.col_names[ i ].bv_val, retval ); res = 1; goto done; } if ( ad != at->bam_ad ) { Debug( LDAP_DEBUG_ANY, "==>backsql_get_attr_vals(\"%s\"): " "column name %s differs from " "AttributeDescription %s\n", bsi->bsi_e->e_name.bv_val, ad->ad_cname.bv_val, at->bam_ad->ad_cname.bv_val ); res = 1; goto done; } #endif /* BACKSQL_TRACE */ /* ITS#3386, ITS#3113 - 20070308 * If a binary is fetched? * must use the actual size read * from the database. */ if ( BACKSQL_IS_BINARY( row.col_type[ i ] ) ) { #ifdef BACKSQL_TRACE Debug( LDAP_DEBUG_ANY, "==>backsql_get_attr_vals(\"%s\"): " "column name %s: data is binary; " "using database size %ld\n", bsi->bsi_e->e_name.bv_val, ad->ad_cname.bv_val, row.value_len[ i ] ); #endif /* BACKSQL_TRACE */ bv.bv_val = row.cols[ i ]; bv.bv_len = row.value_len[ i ]; } else { ber_str2bv( row.cols[ i ], 0, 0, &bv ); } #ifdef BACKSQL_PRETTY_VALIDATE if ( pretty ) { struct berval pbv; retval = pretty( at->bam_true_ad->ad_type->sat_syntax, &bv, &pbv, bsi->bsi_op->o_tmpmemctx ); bv = pbv; } else { retval = validate( at->bam_true_ad->ad_type->sat_syntax, &bv ); } if ( retval != LDAP_SUCCESS ) { char buf[ SLAP_TEXT_BUFLEN ]; /* FIXME: we're ignoring invalid values, * but we're accepting the attributes; * should we fail at all? */ snprintf( buf, sizeof( buf ), "unable to %s value #%lu " "of AttributeDescription %s", pretty ? "prettify" : "validate", k - oldcount, at->bam_ad->ad_cname.bv_val ); Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(\"%s\"): " "%s (%d)\n", bsi->bsi_e->e_name.bv_val, buf, retval ); continue; } #endif /* BACKSQL_PRETTY_VALIDATE */ #ifndef BACKSQL_COUNTQUERY (void)backsql_entry_addattr( bsi->bsi_e, at->bam_true_ad, &bv, bsi->bsi_op->o_tmpmemctx ); #else /* BACKSQL_COUNTQUERY */ if ( normfunc ) { struct berval nbv; retval = (*normfunc)( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, at->bam_true_ad->ad_type->sat_syntax, at->bam_true_ad->ad_type->sat_equality, &bv, &nbv, bsi->bsi_op->o_tmpmemctx ); if ( retval != LDAP_SUCCESS ) { char buf[ SLAP_TEXT_BUFLEN ]; /* FIXME: we're ignoring invalid values, * but we're accepting the attributes; * should we fail at all? */ snprintf( buf, sizeof( buf ), "unable to normalize value #%lu " "of AttributeDescription %s", k - oldcount, at->bam_ad->ad_cname.bv_val ); Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(\"%s\"): " "%s (%d)\n", bsi->bsi_e->e_name.bv_val, buf, retval ); #ifdef BACKSQL_PRETTY_VALIDATE if ( pretty ) { bsi->bsi_op->o_tmpfree( bv.bv_val, bsi->bsi_op->o_tmpmemctx ); } #endif /* BACKSQL_PRETTY_VALIDATE */ continue; } ber_dupbv( &attr->a_nvals[ j ], &nbv ); bsi->bsi_op->o_tmpfree( nbv.bv_val, bsi->bsi_op->o_tmpmemctx ); } ber_dupbv( &attr->a_vals[ j ], &bv ); assert( j < oldcount + count ); j++; #endif /* BACKSQL_COUNTQUERY */ #ifdef BACKSQL_PRETTY_VALIDATE if ( pretty ) { bsi->bsi_op->o_tmpfree( bv.bv_val, bsi->bsi_op->o_tmpmemctx ); } #endif /* BACKSQL_PRETTY_VALIDATE */ #ifdef BACKSQL_TRACE Debug( LDAP_DEBUG_TRACE, "prec=%d\n", (int)row.col_prec[ i ], 0, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "NULL value " "in this row for attribute \"%s\"\n", row.col_names[ i ].bv_val, 0, 0 ); #endif /* BACKSQL_TRACE */ } } } #ifdef BACKSQL_COUNTQUERY if ( BER_BVISNULL( &attr->a_vals[ 0 ] ) ) { /* don't leave around attributes with no values */ attr_free( attr ); } else if ( append ) { Attribute **ap; for ( ap = &bsi->bsi_e->e_attrs; (*ap) != NULL; ap = &(*ap)->a_next ) /* goto last */ ; *ap = attr; } #endif /* BACKSQL_COUNTQUERY */ SQLFreeStmt( sth, SQL_DROP ); Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 ); if ( at->bam_next ) { res = backsql_get_attr_vals( at->bam_next, v_bsi ); } else { res = 1; } #ifdef BACKSQL_TRACE done:; #endif /* BACKSQL_TRACE */ backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx ); return res; }
int read_and_send_results( Operation *op, SlapReply *rs, FILE *fp ) { int bsize, len; char *buf, *bp; char line[BUFSIZ]; char ebuf[128]; /* read in the result and send it along */ buf = (char *) ch_malloc( BUFSIZ ); buf[0] = '\0'; bsize = BUFSIZ; bp = buf; while ( !feof(fp) ) { errno = 0; if ( fgets( line, sizeof(line), fp ) == NULL ) { if ( errno == EINTR ) continue; Debug( LDAP_DEBUG_ANY, "shell: fgets failed: %s (%d)\n", AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 ); break; } Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n", line, 0, 0 ); /* ignore lines beginning with # (LDIFv1 comments) */ if ( *line == '#' ) { continue; } /* ignore lines beginning with DEBUG: */ if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) { continue; } len = strlen( line ); while ( bp + len + 1 - buf > bsize ) { size_t offset = bp - buf; bsize += BUFSIZ; buf = (char *) ch_realloc( buf, bsize ); bp = &buf[offset]; } strcpy( bp, line ); bp += len; /* line marked the end of an entry or result */ if ( *line == '\n' ) { if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) { break; } if ( (rs->sr_entry = str2entry( buf )) == NULL ) { Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf, 0, 0 ); } else { rs->sr_attrs = op->oq_search.rs_attrs; rs->sr_flags = REP_ENTRY_MODIFIABLE; send_search_entry( op, rs ); entry_free( rs->sr_entry ); } bp = buf; } } (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text ); /* otherwise, front end will send this result */ if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) { send_ldap_result( op, rs ); } free( buf ); return( rs->sr_err ); }
static int ldap_back_int_filter_map_rewrite( dncookie *dc, Filter *f, struct berval *fstr, int remap ) { int i; Filter *p; struct berval atmp, vtmp, *tmp; static struct berval /* better than nothing... */ ber_bvfalse = BER_BVC( "(!(objectClass=*))" ), ber_bvtf_false = BER_BVC( "(|)" ), /* better than nothing... */ ber_bvtrue = BER_BVC( "(objectClass=*)" ), ber_bvtf_true = BER_BVC( "(&)" ), #if 0 /* no longer needed; preserved for completeness */ ber_bvundefined = BER_BVC( "(?=undefined)" ), #endif ber_bverror = BER_BVC( "(?=error)" ), ber_bvunknown = BER_BVC( "(?=unknown)" ), ber_bvnone = BER_BVC( "(?=none)" ); ber_len_t len; assert( fstr != NULL ); BER_BVZERO( fstr ); if ( f == NULL ) { ber_dupbv( fstr, &ber_bvnone ); return LDAP_OTHER; } switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) { case LDAP_FILTER_EQUALITY: if ( map_attr_value( dc, f->f_av_desc, &atmp, &f->f_av_value, &vtmp, remap ) ) { goto computed; } fstr->bv_len = atmp.bv_len + vtmp.bv_len + ( sizeof("(=)") - 1 ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)", atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); break; case LDAP_FILTER_GE: if ( map_attr_value( dc, f->f_av_desc, &atmp, &f->f_av_value, &vtmp, remap ) ) { goto computed; } fstr->bv_len = atmp.bv_len + vtmp.bv_len + ( sizeof("(>=)") - 1 ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)", atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); break; case LDAP_FILTER_LE: if ( map_attr_value( dc, f->f_av_desc, &atmp, &f->f_av_value, &vtmp, remap ) ) { goto computed; } fstr->bv_len = atmp.bv_len + vtmp.bv_len + ( sizeof("(<=)") - 1 ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)", atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); break; case LDAP_FILTER_APPROX: if ( map_attr_value( dc, f->f_av_desc, &atmp, &f->f_av_value, &vtmp, remap ) ) { goto computed; } fstr->bv_len = atmp.bv_len + vtmp.bv_len + ( sizeof("(~=)") - 1 ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)", atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); break; case LDAP_FILTER_SUBSTRINGS: if ( map_attr_value( dc, f->f_sub_desc, &atmp, NULL, NULL, remap ) ) { goto computed; } /* cannot be a DN ... */ fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) ); fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */ snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", atmp.bv_val ); if ( !BER_BVISNULL( &f->f_sub_initial ) ) { len = fstr->bv_len; filter_escape_value( &f->f_sub_initial, &vtmp ); fstr->bv_len += vtmp.bv_len; fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3, /* "(attr=" */ "%s*)", vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); } if ( f->f_sub_any != NULL ) { for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) { len = fstr->bv_len; filter_escape_value( &f->f_sub_any[i], &vtmp ); fstr->bv_len += vtmp.bv_len + 1; fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3, /* "(attr=[init]*[any*]" */ "%s*)", vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); } } if ( !BER_BVISNULL( &f->f_sub_final ) ) { len = fstr->bv_len; filter_escape_value( &f->f_sub_final, &vtmp ); fstr->bv_len += vtmp.bv_len; fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3, /* "(attr=[init*][any*]" */ "%s)", vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); } break; case LDAP_FILTER_PRESENT: if ( map_attr_value( dc, f->f_desc, &atmp, NULL, NULL, remap ) ) { goto computed; } fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", atmp.bv_val ); break; case LDAP_FILTER_AND: case LDAP_FILTER_OR: case LDAP_FILTER_NOT: fstr->bv_len = STRLENOF( "(%)" ); fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */ snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)", f->f_choice == LDAP_FILTER_AND ? '&' : f->f_choice == LDAP_FILTER_OR ? '|' : '!' ); for ( p = f->f_list; p != NULL; p = p->f_next ) { int rc; len = fstr->bv_len; rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap ); if ( rc != LDAP_SUCCESS ) { return rc; } fstr->bv_len += vtmp.bv_len; fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" ); ch_free( vtmp.bv_val ); } break; case LDAP_FILTER_EXT: if ( f->f_mr_desc ) { if ( map_attr_value( dc, f->f_mr_desc, &atmp, &f->f_mr_value, &vtmp, remap ) ) { goto computed; } } else { BER_BVSTR( &atmp, "" ); filter_escape_value( &f->f_mr_value, &vtmp ); } /* FIXME: cleanup (less ?: operators...) */ fstr->bv_len = atmp.bv_len + ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) + ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) + vtmp.bv_len + ( STRLENOF( "(:=)" ) ); fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)", atmp.bv_val, f->f_mr_dnattrs ? ":dn" : "", !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "", !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "", vtmp.bv_len ? vtmp.bv_val : "" ); ber_memfree( vtmp.bv_val ); break; case SLAPD_FILTER_COMPUTED: switch ( f->f_result ) { /* FIXME: treat UNDEFINED as FALSE */ case SLAPD_COMPARE_UNDEFINED: computed:; if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) { return LDAP_COMPARE_FALSE; } /* fallthru */ case LDAP_COMPARE_FALSE: if ( META_BACK_TGT_T_F( dc->target ) ) { tmp = &ber_bvtf_false; break; } tmp = &ber_bvfalse; break; case LDAP_COMPARE_TRUE: if ( META_BACK_TGT_T_F( dc->target ) ) { tmp = &ber_bvtf_true; break; } tmp = &ber_bvtrue; break; default: tmp = &ber_bverror; break; } ber_dupbv( fstr, tmp ); break; default: ber_dupbv( fstr, &ber_bvunknown ); break; } return 0; }
static int slap_open_listener( const char* url, int *listeners, int *cur ) { int num, tmp, rc; Listener l; Listener *li; LDAPURLDesc *lud; unsigned short port; int err, addrlen = 0; struct sockaddr **sal, **psal; int socktype = SOCK_STREAM; /* default to COTS */ #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD) /* * use safe defaults */ int crit = 1; #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */ rc = ldap_url_parse( url, &lud ); if( rc != LDAP_URL_SUCCESS ) { #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, ERR, "slap_open_listener: listen URL \"%s\" parse error %d\n", url, rc , 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: listen URL \"%s\" parse error=%d\n", url, rc, 0 ); #endif return rc; } l.sl_url.bv_val = NULL; l.sl_is_mute = 0; #ifndef HAVE_TLS if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) { #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: TLS is not supported (%s)\n", url, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n", url, 0, 0 ); #endif ldap_free_urldesc( lud ); return -1; } if(! lud->lud_port ) { lud->lud_port = LDAP_PORT; } #else l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme ); if(! lud->lud_port ) { lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT; } #endif port = (unsigned short) lud->lud_port; tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme); if ( tmp == LDAP_PROTO_IPC ) { #ifdef LDAP_PF_LOCAL if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) { err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal); } else { err = slap_get_listener_addresses(lud->lud_host, 0, &sal); } #else #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: URL scheme is not supported: %s\n", url, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s", url, 0, 0); #endif ldap_free_urldesc( lud ); return -1; #endif } else { if( lud->lud_host == NULL || lud->lud_host[0] == '\0' || strcmp(lud->lud_host, "*") == 0 ) { err = slap_get_listener_addresses(NULL, port, &sal); } else { err = slap_get_listener_addresses(lud->lud_host, port, &sal); } } #ifdef LDAP_CONNECTIONLESS l.sl_is_udp = ( tmp == LDAP_PROTO_UDP ); #endif #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD) if ( lud->lud_exts ) { err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit ); } else { l.sl_perms = S_IRWXU; } #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */ ldap_free_urldesc( lud ); if ( err ) { return -1; } /* If we got more than one address returned, we need to make space * for it in the slap_listeners array. */ for ( num=0; sal[num]; num++ ); if ( num > 1 ) { *listeners += num-1; slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) ); } psal = sal; while ( *sal != NULL ) { switch( (*sal)->sa_family ) { case AF_INET: #ifdef LDAP_PF_INET6 case AF_INET6: #endif #ifdef LDAP_PF_LOCAL case AF_LOCAL: #endif break; default: sal++; continue; } #ifdef LDAP_CONNECTIONLESS if( l.sl_is_udp ) socktype = SOCK_DGRAM; #endif l.sl_sd = socket( (*sal)->sa_family, socktype, 0); if ( l.sl_sd == AC_SOCKET_INVALID ) { int err = sock_errno(); #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, ERR, "slap_open_listener: socket() failed errno=%d (%s)\n", err, sock_errstr(err), 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: socket() failed errno=%d (%s)\n", err, sock_errstr(err), 0 ); #endif sal++; continue; } #ifndef HAVE_WINSOCK if ( l.sl_sd >= dtblsize ) { #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, ERR, "slap_open_listener: listener descriptor %ld is too " "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: listener descriptor %ld is too great %ld\n", (long) l.sl_sd, (long) dtblsize, 0 ); #endif tcp_close( l.sl_sd ); sal++; continue; } #endif #ifdef LDAP_PF_LOCAL if ( (*sal)->sa_family == AF_LOCAL ) { unlink ( ((struct sockaddr_un *)*sal)->sun_path ); } else #endif { #ifdef SO_REUSEADDR /* enable address reuse */ tmp = 1; rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof(tmp) ); if ( rc == AC_SOCKET_ERROR ) { int err = sock_errno(); #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) " "failed errno %d (%s)\n", (long)l.sl_sd, err, sock_errstr(err) ); #else Debug( LDAP_DEBUG_ANY, "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n", (long) l.sl_sd, err, sock_errstr(err) ); #endif } #endif } switch( (*sal)->sa_family ) { case AF_INET: addrlen = sizeof(struct sockaddr_in); break; #ifdef LDAP_PF_INET6 case AF_INET6: #ifdef IPV6_V6ONLY /* Try to use IPv6 sockets for IPv6 only */ tmp = 1; rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tmp, sizeof(tmp) ); if ( rc == AC_SOCKET_ERROR ) { int err = sock_errno(); #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n", (long)l.sl_sd, err, sock_errstr(err) ); #else Debug( LDAP_DEBUG_ANY, "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n", (long) l.sl_sd, err, sock_errstr(err) ); #endif } #endif addrlen = sizeof(struct sockaddr_in6); break; #endif #ifdef LDAP_PF_LOCAL case AF_LOCAL: addrlen = sizeof(struct sockaddr_un); break; #endif } if (bind(l.sl_sd, *sal, addrlen)) { err = sock_errno(); #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: bind(%ld) failed errno=%d (%s)\n", (long)l.sl_sd, err, sock_errstr(err) ); #else Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n", (long) l.sl_sd, err, sock_errstr(err) ); #endif tcp_close( l.sl_sd ); sal++; continue; } switch ( (*sal)->sa_family ) { #ifdef LDAP_PF_LOCAL case AF_LOCAL: { char *addr = ((struct sockaddr_un *)*sal)->sun_path; if ( chmod( addr, l.sl_perms ) < 0 && crit ) { int err = sock_errno(); #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n", (long)l.sl_sd, err, sock_errstr(err) ); #else Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)", (long) l.sl_sd, err, sock_errstr(err) ); #endif tcp_close( l.sl_sd ); slap_free_listener_addresses(psal); return -1; } l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1; l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 ); snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1, "PATH=%s", addr ); } break; #endif /* LDAP_PF_LOCAL */ case AF_INET: { char *s; #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) char addr[INET_ADDRSTRLEN]; inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr, addr, sizeof(addr) ); s = addr; #else s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr ); #endif port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port ); l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") ); snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"), "IP=%s:%d", s != NULL ? s : SLAP_STRING_UNKNOWN, port ); l.sl_name.bv_len = strlen( l.sl_name.bv_val ); } break; #ifdef LDAP_PF_INET6 case AF_INET6: { char addr[INET6_ADDRSTRLEN]; inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr, addr, sizeof addr); port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port ); l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535"); l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len ); snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d", addr, port ); l.sl_name.bv_len = strlen( l.sl_name.bv_val ); } break; #endif /* LDAP_PF_INET6 */ default: #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, INFO, "slap_open_listener: unsupported address family (%d)\n", (int)(*sal)->sa_family, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n", (int) (*sal)->sa_family, 0, 0 ); #endif break; } AC_MEMCPY(&l.sl_sa, *sal, addrlen); ber_str2bv( url, 0, 1, &l.sl_url); li = ch_malloc( sizeof( Listener ) ); *li = l; slap_listeners[*cur] = li; (*cur)++; sal++; } /* while ( *sal != NULL ) */ slap_free_listener_addresses(psal); if ( l.sl_url.bv_val == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, RESULTS, "slap_open_listener: failed on %s\n", url, 0, 0 ); #else Debug( LDAP_DEBUG_TRACE, "slap_open_listener: failed on %s\n", url, 0, 0 ); #endif return -1; } #ifdef NEW_LOGGING LDAP_LOG( CONNECTION, RESULTS, "slap_open_listener: daemon initialized %s\n", l.sl_url.bv_val, 0, 0 ); #else Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n", l.sl_url.bv_val, 0, 0 ); #endif return 0; }
static int mdb_tool_next_id( Operation *op, MDB_txn *tid, Entry *e, struct berval *text, int hole ) { struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; struct berval dn = e->e_name; struct berval ndn = e->e_nname; struct berval pdn, npdn, nmatched; ID id, pid = 0; int rc; if (ndn.bv_len == 0) { e->e_id = 0; return 0; } rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched ); if ( rc == MDB_NOTFOUND ) { if ( !be_issuffix( op->o_bd, &ndn ) ) { ID eid = e->e_id; dnParent( &ndn, &npdn ); if ( nmatched.bv_len != npdn.bv_len ) { dnParent( &dn, &pdn ); e->e_name = pdn; e->e_nname = npdn; rc = mdb_tool_next_id( op, tid, e, text, 1 ); e->e_name = dn; e->e_nname = ndn; if ( rc ) { return rc; } /* If parent didn't exist, it was created just now * and its ID is now in e->e_id. Make sure the current * entry gets added under the new parent ID. */ if ( eid != e->e_id ) { pid = e->e_id; } } else { pid = id; } } rc = mdb_next_id( op->o_bd, idcursor, &e->e_id ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "next_id failed: %s (%d)", mdb_strerror(rc), rc ); Debug( LDAP_DEBUG_ANY, "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); return rc; } rc = mdb_dn2id_add( op, mcp, mcd, pid, e ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "dn2id_add failed: %s (%d)", mdb_strerror(rc), rc ); Debug( LDAP_DEBUG_ANY, "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); } else if ( hole ) { MDB_val key, data; if ( nholes == nhmax - 1 ) { if ( holes == hbuf ) { holes = ch_malloc( nhmax * sizeof(dn_id) * 2 ); AC_MEMCPY( holes, hbuf, sizeof(hbuf) ); } else { holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 ); } nhmax *= 2; } ber_dupbv( &holes[nholes].dn, &ndn ); holes[nholes++].id = e->e_id; key.mv_size = sizeof(ID); key.mv_data = &e->e_id; data.mv_size = 0; data.mv_data = NULL; rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE ); if ( rc == MDB_KEYEXIST ) rc = 0; if ( rc ) { snprintf( text->bv_val, text->bv_len, "dummy id2entry add failed: %s (%d)", mdb_strerror(rc), rc ); Debug( LDAP_DEBUG_ANY, "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); } } } else if ( !hole ) { unsigned i, j; e->e_id = id; for ( i=0; i<nholes; i++) { if ( holes[i].id == e->e_id ) { free(holes[i].dn.bv_val); for (j=i;j<nholes;j++) holes[j] = holes[j+1]; holes[j].id = 0; nholes--; break; } else if ( holes[i].id > e->e_id ) { break; } } } return rc; }
/* * Convert a delimited string into a list of AttributeNames; add * on to an existing list if it was given. If the string is not * a valid attribute name, if a '-' is prepended it is skipped * and the remaining name is tried again; if a '@' (or '+') is * prepended, an objectclass name is searched instead; if a '!' * is prepended, the objectclass name is negated. * * NOTE: currently, if a valid attribute name is not found, the * same string is also checked as valid objectclass name; however, * this behavior is deprecated. */ AttributeName * str2anlist( AttributeName *an, char *in, const char *brkstr ) { char *str; char *s; char *lasts; int i, j; const char *text; AttributeName *anew; /* find last element in list */ i = 0; if ( an != NULL ) { for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++) ; } /* protect the input string from strtok */ str = ch_strdup( in ); /* Count words in string */ j = 1; for ( s = str; *s; s++ ) { if ( strchr( brkstr, *s ) != NULL ) { j++; } } an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) ); anew = an + i; for ( s = ldap_pvt_strtok( str, brkstr, &lasts ); s != NULL; s = ldap_pvt_strtok( NULL, brkstr, &lasts ) ) { /* put a stop mark */ BER_BVZERO( &anew[1].an_name ); anew->an_desc = NULL; anew->an_oc = NULL; anew->an_flags = 0; ber_str2bv(s, 0, 1, &anew->an_name); slap_bv2ad(&anew->an_name, &anew->an_desc, &text); if ( !anew->an_desc ) { switch( anew->an_name.bv_val[0] ) { case '-': { struct berval adname; adname.bv_len = anew->an_name.bv_len - 1; adname.bv_val = &anew->an_name.bv_val[1]; slap_bv2ad(&adname, &anew->an_desc, &text); if ( !anew->an_desc ) { goto reterr; } } break; case '@': case '+': /* (deprecated) */ case '!': { struct berval ocname; ocname.bv_len = anew->an_name.bv_len - 1; ocname.bv_val = &anew->an_name.bv_val[1]; anew->an_oc = oc_bvfind( &ocname ); if ( !anew->an_oc ) { goto reterr; } if ( anew->an_name.bv_val[0] == '!' ) { anew->an_flags |= SLAP_AN_OCEXCLUDE; } } break; default: /* old (deprecated) way */ anew->an_oc = oc_bvfind( &anew->an_name ); if ( !anew->an_oc ) { goto reterr; } } } anew->an_flags |= SLAP_AN_OCINITED; anew++; } BER_BVZERO( &anew->an_name ); free( str ); return( an ); reterr: anlist_free( an, 1, NULL ); /* * overwrites input string * on error! */ strcpy( in, s ); free( str ); return NULL; }
/* Read table definitions from the DB and populate ObjectClassInfo */ extern "C" int ndb_oc_read( struct ndb_info *ni, const NdbDictionary::Dictionary *myDict ) { const NdbDictionary::Table *myTable; const NdbDictionary::Column *myCol; NdbOcInfo *oci, octmp; NdbAttrInfo *ai; ObjectClass *oc; NdbDictionary::Dictionary::List myList; struct berval bv; int i, j, rc, col; rc = myDict->listObjects( myList, NdbDictionary::Object::UserTable ); /* Populate our objectClass structures */ for ( i=0; i<myList.count; i++ ) { /* Ignore other DBs */ if ( strcmp( myList.elements[i].database, ni->ni_dbname )) continue; /* Ignore internal tables */ if ( !strncmp( myList.elements[i].name, "OL_", 3 )) continue; ber_str2bv( myList.elements[i].name, 0, 0, &octmp.no_name ); oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp ); if ( oci ) continue; oc = oc_bvfind( &octmp.no_name ); if ( !oc ) { /* undefined - shouldn't happen */ continue; } myTable = myDict->getTable( myList.elements[i].name ); oci = (NdbOcInfo *)ch_malloc( sizeof( NdbOcInfo )+oc->soc_cname.bv_len+1 ); oci->no_table.bv_val = (char *)(oci+1); strcpy( oci->no_table.bv_val, oc->soc_cname.bv_val ); oci->no_table.bv_len = oc->soc_cname.bv_len; oci->no_name = oci->no_table; oci->no_oc = oc; oci->no_flag = 0; oci->no_nsets = 0; oci->no_nattrs = 0; col = 0; /* Make space for all attrs, even tho sups will be dropped */ if ( oci->no_oc->soc_required ) { for ( j=0; oci->no_oc->soc_required[j]; j++ ); col = j; } if ( oci->no_oc->soc_allowed ) { for ( j=0; oci->no_oc->soc_allowed[j]; j++ ); col += j; } oci->no_attrs = (struct ndb_attrinfo **)ch_malloc( col * sizeof(struct ndb_attrinfo *)); avl_insert( &ni->ni_oc_tree, oci, ndb_name_cmp, avl_dup_error ); col = myTable->getNoOfColumns(); /* Skip 0 and 1, eid and vid */ for ( j = 2; j<col; j++ ) { myCol = myTable->getColumn( j ); ber_str2bv( myCol->getName(), 0, 0, &bv ); ai = ndb_ai_get( ni, &bv ); /* shouldn't happen */ if ( !ai ) continue; ai->na_oi = oci; ai->na_column = j; ai->na_len = myCol->getLength(); if ( myCol->getType() == NdbDictionary::Column::Blob ) ai->na_flag |= NDB_INFO_ATBLOB; } } /* Link to any attrsets */ for ( i=0; i<myList.count; i++ ) { /* Ignore other DBs */ if ( strcmp( myList.elements[i].database, ni->ni_dbname )) continue; /* Ignore internal tables */ if ( !strncmp( myList.elements[i].name, "OL_", 3 )) continue; ber_str2bv( myList.elements[i].name, 0, 0, &octmp.no_name ); oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp ); /* shouldn't happen */ if ( !oci ) continue; col = 2; if ( oci->no_oc->soc_required ) { rc = ndb_ai_check( ni, oci, oci->no_oc->soc_required, NULL, &col, 0 ); } if ( oci->no_oc->soc_allowed ) { rc = ndb_ai_check( ni, oci, oci->no_oc->soc_allowed, NULL, &col, 0 ); } /* shrink down to just the needed size */ oci->no_attrs = (struct ndb_attrinfo **)ch_realloc( oci->no_attrs, oci->no_nattrs * sizeof(struct ndb_attrinfo *)); } return 0; }
Entry* bdb_tool_entry_get( BackendDB *be, ID id ) { Entry *e = NULL; char *dptr; int rc, eoff; assert( be != NULL ); assert( slapMode & SLAP_TOOL_MODE ); if ( id != previd ) { data.ulen = data.dlen = sizeof( ehbuf ); data.data = ehbuf; data.flags |= DB_DBT_PARTIAL; BDB_ID2DISK( id, &nid ); rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc ) goto done; } /* Get the header */ dptr = eh.bv.bv_val; eh.bv.bv_val = ehbuf; eh.bv.bv_len = data.size; rc = entry_header( &eh ); eoff = eh.data - eh.bv.bv_val; eh.bv.bv_val = dptr; if ( rc ) goto done; /* Get the size */ data.flags &= ~DB_DBT_PARTIAL; data.ulen = 0; rc = cursor->c_get( cursor, &key, &data, DB_CURRENT ); if ( rc != DB_BUFFER_SMALL ) goto done; /* Allocate a block and retrieve the data */ eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size; eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len ); eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval ); data.data = eh.data; data.ulen = data.size; /* Skip past already parsed nattr/nvals */ eh.data += eoff; rc = cursor->c_get( cursor, &key, &data, DB_CURRENT ); if ( rc ) goto done; #ifdef SLAP_ZONE_ALLOC /* FIXME: will add ctx later */ rc = entry_decode( &eh, &e, NULL ); #else rc = entry_decode( &eh, &e ); #endif if( rc == LDAP_SUCCESS ) { e->e_id = id; #ifdef BDB_HIER if ( slapMode & SLAP_TOOL_READONLY ) { EntryInfo *ei = NULL; Operation op = {0}; Opheader ohdr = {0}; op.o_hdr = &ohdr; op.o_bd = be; op.o_tmpmemctx = NULL; op.o_tmpmfuncs = &ch_mfuncs; rc = bdb_cache_find_parent( &op, CURSOR_GETLOCKER(cursor), id, &ei ); if ( rc == LDAP_SUCCESS ) { bdb_cache_entryinfo_unlock( ei ); e->e_private = ei; ei->bei_e = e; bdb_fix_dn( e, 0 ); ei->bei_e = NULL; e->e_private = NULL; } } #endif } done: return e; }
static int bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep ) { Entry *e = NULL; char *dptr; int rc, eoff; assert( be != NULL ); assert( slapMode & SLAP_TOOL_MODE ); if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) { *ep = tool_next_entry; tool_next_entry = NULL; return LDAP_SUCCESS; } if ( id != previd ) { data.ulen = data.dlen = sizeof( ehbuf ); data.data = ehbuf; data.flags |= DB_DBT_PARTIAL; BDB_ID2DISK( id, &nid ); rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc ) { rc = LDAP_OTHER; goto done; } } /* Get the header */ dptr = eh.bv.bv_val; eh.bv.bv_val = ehbuf; eh.bv.bv_len = data.size; rc = entry_header( &eh ); eoff = eh.data - eh.bv.bv_val; eh.bv.bv_val = dptr; if ( rc ) { rc = LDAP_OTHER; goto done; } /* Get the size */ data.flags &= ~DB_DBT_PARTIAL; data.ulen = 0; rc = cursor->c_get( cursor, &key, &data, DB_CURRENT ); if ( rc != DB_BUFFER_SMALL ) { rc = LDAP_OTHER; goto done; } /* Allocate a block and retrieve the data */ eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size; eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len ); eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval ); data.data = eh.data; data.ulen = data.size; /* Skip past already parsed nattr/nvals */ eh.data += eoff; rc = cursor->c_get( cursor, &key, &data, DB_CURRENT ); if ( rc ) { rc = LDAP_OTHER; goto done; } #ifndef BDB_HIER /* TODO: handle BDB_HIER accordingly */ if ( tool_base != NULL ) { struct berval ndn; entry_decode_dn( &eh, NULL, &ndn ); if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) { return LDAP_NO_SUCH_OBJECT; } } #endif #ifdef SLAP_ZONE_ALLOC /* FIXME: will add ctx later */ rc = entry_decode( &eh, &e, NULL ); #else rc = entry_decode( &eh, &e ); #endif if( rc == LDAP_SUCCESS ) { e->e_id = id; #ifdef BDB_HIER if ( slapMode & SLAP_TOOL_READONLY ) { struct bdb_info *bdb = (struct bdb_info *) be->be_private; EntryInfo *ei = NULL; Operation op = {0}; Opheader ohdr = {0}; op.o_hdr = &ohdr; op.o_bd = be; op.o_tmpmemctx = NULL; op.o_tmpmfuncs = &ch_mfuncs; rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, id, &ei ); if ( rc == LDAP_SUCCESS ) { bdb_cache_entryinfo_unlock( ei ); e->e_private = ei; ei->bei_e = e; bdb_fix_dn( e, 0 ); ei->bei_e = NULL; e->e_private = NULL; } } #endif } done: if ( e != NULL ) { *ep = e; } return rc; }