int ldif_fetch_url( LDAP_CONST char *urlstr, char **valuep, ber_len_t *vlenp ) { FILE *url; char buffer[1024]; char *p = NULL; size_t total; size_t bytes; *valuep = NULL; *vlenp = 0; url = ldif_open_url( urlstr ); if( url == NULL ) { return -1; } total = 0; while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) { char *newp = ber_memrealloc( p, total + bytes + 1 ); if( newp == NULL ) { ber_memfree( p ); fclose( url ); return -1; } p = newp; AC_MEMCPY( &p[total], buffer, bytes ); total += bytes; } fclose( url ); if( total == 0 ) { char *newp = ber_memrealloc( p, 1 ); if( newp == NULL ) { ber_memfree( p ); return -1; } p = newp; } p[total] = '\0'; *valuep = p; *vlenp = total; return 0; }
/* * ldap_int_bisect_insert * * args: * vp: pointer to array of length *np (in/out) * np: pointer to length of array *vp (in/out) * id: value to insert (in) * idx: location of insert point (as computed by ldap_int_bisect_find()) * * return: * 0: inserted * -1: error */ int ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ) { ber_int_t *v; ber_len_t n; int i; assert( vp != NULL ); assert( np != NULL ); assert( idx >= 0 ); assert( (unsigned) idx <= *np ); n = *np; v = ber_memrealloc( *vp, sizeof( ber_int_t ) * ( n + 1 ) ); if ( v == NULL ) { return -1; } *vp = v; for ( i = n; i > idx; i-- ) { v[ i ] = v[ i - 1 ]; } v[ idx ] = id; ++(*np); return 0; }
static krb5_error_code LDAP_addmod(LDAPMod *** modlist, int modop, const char *attribute, const char *value) { int cMods, i = 0; krb5_error_code ret; ret = LDAP__setmod(modlist, modop, attribute, &cMods); if (ret) return ret; if (value != NULL) { char **bv; bv = (*modlist)[cMods]->mod_values; if (bv != NULL) { for (i = 0; bv[i] != NULL; i++) ; bv = ber_memrealloc(bv, (i + 2) * sizeof(*bv)); } else bv = ber_memalloc(2 * sizeof(*bv)); if (bv == NULL) return ENOMEM; (*modlist)[cMods]->mod_values = bv; bv[i] = ber_strdup(value); if (bv[i] == NULL) return ENOMEM; bv[i + 1] = NULL; } return 0; }
static int monitor_subsys_conn_update( Operation *op, SlapReply *rs, Entry *e ) { monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private; long n = -1; static struct berval total_bv = BER_BVC( "cn=total" ), current_bv = BER_BVC( "cn=current" ); struct berval rdn; assert( mi != NULL ); assert( e != NULL ); dnRdn( &e->e_nname, &rdn ); if ( dn_match( &rdn, &total_bv ) ) { n = connections_nextid(); } else if ( dn_match( &rdn, ¤t_bv ) ) { Connection *c; ber_socket_t connindex; for ( n = 0, c = connection_first( &connindex ); c != NULL; n++, c = connection_next( c, &connindex ) ) { /* No Op */ ; } connection_done( c ); } if ( n != -1 ) { Attribute *a; char buf[LDAP_PVT_INTTYPE_CHARS(long)]; ber_len_t len; a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter ); if ( a == NULL ) { return( -1 ); } snprintf( buf, sizeof( buf ), "%ld", n ); len = strlen( buf ); if ( len > a->a_vals[ 0 ].bv_len ) { a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 ); } a->a_vals[ 0 ].bv_len = len; memcpy( a->a_vals[ 0 ].bv_val, buf, len + 1 ); /* FIXME: touch modifyTimestamp? */ }
static krb5_error_code LDAP__setmod(LDAPMod *** modlist, int modop, const char *attribute, int *pIndex) { int cMods; if (*modlist == NULL) { *modlist = (LDAPMod **)ber_memcalloc(1, sizeof(LDAPMod *)); if (*modlist == NULL) return ENOMEM; } for (cMods = 0; (*modlist)[cMods] != NULL; cMods++) { if ((*modlist)[cMods]->mod_op == modop && strcasecmp((*modlist)[cMods]->mod_type, attribute) == 0) { break; } } *pIndex = cMods; if ((*modlist)[cMods] == NULL) { LDAPMod *mod; *modlist = (LDAPMod **)ber_memrealloc(*modlist, (cMods + 2) * sizeof(LDAPMod *)); if (*modlist == NULL) return ENOMEM; (*modlist)[cMods] = (LDAPMod *)ber_memalloc(sizeof(LDAPMod)); if ((*modlist)[cMods] == NULL) return ENOMEM; mod = (*modlist)[cMods]; mod->mod_op = modop; mod->mod_type = ber_strdup(attribute); if (mod->mod_type == NULL) { ber_memfree(mod); (*modlist)[cMods] = NULL; return ENOMEM; } if (modop & LDAP_MOD_BVALUES) { mod->mod_bvalues = NULL; } else { mod->mod_values = NULL; } (*modlist)[cMods + 1] = NULL; } return 0; }
/* ARGSUSED */ static int avl_buildlist( void* data, void* arg ) { static int slots; if ( avl_list == (void* *) 0 ) { avl_list = (void* *) ber_memalloc(AVL_GRABSIZE * sizeof(void*)); slots = AVL_GRABSIZE; avl_maxlist = 0; } else if ( avl_maxlist == slots ) { slots += AVL_GRABSIZE; avl_list = (void* *) ber_memrealloc( (char *) avl_list, (unsigned) slots * sizeof(void*)); } avl_list[ avl_maxlist++ ] = data; return( 0 ); }
static krb5_error_code LDAP_addmod_len(LDAPMod *** modlist, int modop, const char *attribute, unsigned char *value, size_t len) { krb5_error_code ret; int cMods, i = 0; ret = LDAP__setmod(modlist, modop | LDAP_MOD_BVALUES, attribute, &cMods); if (ret) return ret; if (value != NULL) { struct berval **bv; bv = (*modlist)[cMods]->mod_bvalues; if (bv != NULL) { for (i = 0; bv[i] != NULL; i++) ; bv = ber_memrealloc(bv, (i + 2) * sizeof(*bv)); } else bv = ber_memalloc(2 * sizeof(*bv)); if (bv == NULL) return ENOMEM; (*modlist)[cMods]->mod_bvalues = bv; bv[i] = ber_memalloc(sizeof(**bv));; if (bv[i] == NULL) return ENOMEM; bv[i]->bv_val = (void *)value; bv[i]->bv_len = len; bv[i + 1] = NULL; } return 0; }
/* Parse an LDIF control line of the form control: oid [true/false] [: value] or control: oid [true/false] [:: base64-value] or control: oid [true/false] [:< url] The control is added to the list of controls in *ppctrls. */ static int parse_ldif_control( struct berval *bval, LDAPControl ***ppctrls ) { char *oid = NULL; int criticality = 0; /* Default is false if not present */ int i, rc=0; char *s, *oidStart; LDAPControl *newctrl = NULL; LDAPControl **pctrls = NULL; struct berval type, bv; int freeval; if (ppctrls) pctrls = *ppctrls; /* OID should come first. Validate and extract it. */ s = bval->bv_val; if (*s == 0) return ( LDAP_PARAM_ERROR ); oidStart = s; while (isdigit((unsigned char)*s) || *s == '.') { s++; /* OID should be digits or . */ } if (s == oidStart) { return ( LDAP_PARAM_ERROR ); /* OID was not present */ } if (*s) { /* End of OID should be space or NULL */ if (!isspace((unsigned char)*s)) { return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */ } *s++ = 0; /* Replace space with null to terminate */ } oid = ber_strdup(oidStart); if (oid == NULL) return ( LDAP_NO_MEMORY ); /* Optional Criticality field is next. */ while (*s && isspace((unsigned char)*s)) { s++; /* Skip white space before criticality */ } if (strncasecmp(s, "true", 4) == 0) { criticality = 1; s += 4; } else if (strncasecmp(s, "false", 5) == 0) { criticality = 0; s += 5; } /* Optional value field is next */ while (*s && isspace((unsigned char)*s)) { s++; /* Skip white space before value */ } if (*s) { if (*s != ':') { /* If value is present, must start with : */ rc = LDAP_PARAM_ERROR; goto cleanup; } /* Back up so value is in the form a: value a:: base64-value a:< url Then we can use ldif_parse_line2 to extract and decode the value */ s--; *s = 'a'; rc = ldif_parse_line2(s, &type, &bv, &freeval); if (rc < 0) { rc = LDAP_PARAM_ERROR; goto cleanup; } } /* Create a new LDAPControl structure. */ newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl)); if ( newctrl == NULL ) { rc = LDAP_NO_MEMORY; goto cleanup; } newctrl->ldctl_oid = oid; oid = NULL; newctrl->ldctl_iscritical = criticality; if ( freeval ) newctrl->ldctl_value = bv; else ber_dupbv( &newctrl->ldctl_value, &bv ); /* Add the new control to the passed-in list of controls. */ i = 0; if (pctrls) { while ( pctrls[i] ) { /* Count the # of controls passed in */ i++; } } /* Allocate 1 more slot for the new control and 1 for the NULL. */ pctrls = (LDAPControl **) ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *))); if (pctrls == NULL) { rc = LDAP_NO_MEMORY; goto cleanup; } pctrls[i] = newctrl; newctrl = NULL; pctrls[i+1] = NULL; *ppctrls = pctrls; cleanup: if (newctrl) { if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid); if (newctrl->ldctl_value.bv_val) { ber_memfree(newctrl->ldctl_value.bv_val); } ber_memfree(newctrl); } if (oid) ber_memfree(oid); return( rc ); }
static int process_ldif_rec( char *rbuf, int linenum ) { char *line, *dn, *newrdn, *newsup; int rc, modop; int expect_modop, expect_sep; int deleteoldrdn; int new_entry, delete_entry, got_all; LDAPMod **pmods, *lm = NULL; int version; LDAPControl **pctrls; int i, j, k, lines, idn, nmods; struct berval *btype, *vals, **bvl, bv; char *freeval; unsigned char *mops = NULL; new_entry = ldapadd; rc = got_all = delete_entry = modop = expect_modop = 0; expect_sep = 0; version = 0; deleteoldrdn = 1; pmods = NULL; pctrls = NULL; dn = newrdn = newsup = NULL; lines = ldif_countlines( rbuf ); btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines ); if ( !btype ) return LDAP_NO_MEMORY; vals = btype+lines+1; freeval = (char *)(vals+lines+1); i = -1; while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) { int freev; if ( *line == '\n' || *line == '\0' ) { break; } ++i; if ( line[0] == '-' && !line[1] ) { BER_BVZERO( btype+i ); freeval[i] = 0; continue; } if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) { fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"), prog, linenum+i, dn == NULL ? "" : dn ); rc = LDAP_PARAM_ERROR; break; } freeval[i] = freev; if ( dn == NULL ) { if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) { int v; if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) { fprintf( stderr, _("%s: invalid version %s, line %d (ignored)\n"), prog, vals[i].bv_val, linenum ); } version++; } else if ( BV_CASEMATCH( btype+i, &BV_DN )) { dn = vals[i].bv_val; idn = i; } /* skip all lines until we see "dn:" */ } } /* check to make sure there was a dn: line */ if ( !dn ) { rc = 0; goto leave; } lines = i+1; if( lines == 0 ) { rc = 0; goto leave; } if( version && lines == 1 ) { rc = 0; goto leave; } i = idn+1; /* Check for "control" tag after dn and before changetype. */ if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) { /* Parse and add it to the list of controls */ rc = parse_ldif_control( vals+i, &pctrls ); if (rc != 0) { fprintf( stderr, _("%s: Error processing %s line, line %d: %s\n"), prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) ); } i++; if ( i>= lines ) { short_input: fprintf( stderr, _("%s: Expecting more input after %s line, line %d\n"), prog, btype[i-1].bv_val, linenum+i ); rc = LDAP_PARAM_ERROR; goto leave; } } /* Check for changetype */ if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) { break; } } if ( ++icnt != vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"), prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn ); vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) { new_entry = 0; expect_modop = 1; } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) { new_entry = 1; modop = LDAP_MOD_ADD; } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT ) || BV_CASEMATCH( vals+i, &BV_MODDNCT ) || BV_CASEMATCH( vals+i, &BV_RENAMECT )) { i++; if ( i >= lines ) goto short_input; if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } newrdn = vals[i].bv_val; i++; if ( i >= lines ) goto short_input; if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1; i++; if ( i < lines ) { if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } newsup = vals[i].bv_val; i++; } got_all = 1; } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) { got_all = delete_entry = 1; } else { fprintf( stderr, _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"), prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } i++; } else if ( ldapadd ) { /* missing changetype => add */ new_entry = 1; modop = LDAP_MOD_ADD; } else { expect_modop = 1; /* missing changetype => modify */ } if ( got_all ) { if ( i < lines ) { fprintf( stderr, _("%s: extra lines at end (line %d, entry \"%s\")\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } goto doit; } nmods = lines - i; idn = i; if ( new_entry ) { int fv; /* Make sure all attributes with multiple values are contiguous */ for (; i<lines; i++) { for (j=i+1; j<lines; j++) { if ( BV_CASEMATCH( btype+i, btype+j )) { nmods--; /* out of order, move intervening attributes down */ if ( j != i+1 ) { bv = vals[j]; fv = freeval[j]; for (k=j; k>i; k--) { btype[k] = btype[k-1]; vals[k] = vals[k-1]; freeval[k] = freeval[k-1]; } k++; btype[k] = btype[i]; vals[k] = bv; freeval[k] = fv; } i++; } } } /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lm = ber_memalloc( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lines + nmods - idn) * sizeof(struct berval *)); pmods = (LDAPMod **)(lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); for (i=idn; i<lines; i++) { if ( BV_CASEMATCH( btype+i, &BV_DN )) { fprintf( stderr, _("%s: attributeDescription \"%s\":" " (possible missing newline" " after line %d, entry \"%s\"?)\n"), prog, btype[i].bv_val, linenum+i - 1, dn ); } if ( !BV_CASEMATCH( btype+i, &bv )) { bvl[k++] = NULL; bv = btype[i]; lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES; lm[j].mod_type = bv.bv_val; lm[j].mod_bvalues = bvl+k; pmods[j] = lm+j; j++; } bvl[k++] = vals+i; } bvl[k] = NULL; pmods[j] = NULL; goto doit; } mops = ber_memalloc( lines+1 ); mops[lines] = M_SEP; mops[i-1] = M_SEP; for ( ; i<lines; i++ ) { if ( expect_modop ) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break; } if ( ++icnt != vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"), prog, type, vals[i].bv_val, linenum+i, dn ); vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ expect_modop = 0; expect_sep = 1; if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) { modop = LDAP_MOD_ADD; mops[i] = M_SEP; nmods--; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) { /* defer handling these since they might have no values. * Use the BVALUES flag to signal that these were * deferred. If values are provided later, this * flag will be switched off. */ modop = LDAP_MOD_REPLACE; mops[i] = modop | LDAP_MOD_BVALUES; btype[i] = vals[i]; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) { modop = LDAP_MOD_DELETE; mops[i] = modop | LDAP_MOD_BVALUES; btype[i] = vals[i]; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) { modop = LDAP_MOD_INCREMENT; mops[i] = M_SEP; nmods--; } else { /* no modify op: invalid LDIF */ fprintf( stderr, _("%s: modify operation type is missing at" " line %d, entry \"%s\"\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } bv = vals[i]; } else if ( expect_sep && BER_BVISEMPTY( btype+i )) { mops[i] = M_SEP; expect_sep = 0; expect_modop = 1; nmods--; } else { if ( !BV_CASEMATCH( btype+i, &bv )) { fprintf( stderr, _("%s: wrong attributeType at" " line %d, entry \"%s\"\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } mops[i] = modop; /* If prev op was deferred and matches this type, * clear the flag */ if ( (mops[i-1] & LDAP_MOD_BVALUES) && BV_CASEMATCH( btype+i, btype+i-1 )) { mops[i-1] = M_SEP; nmods--; } } } #if 0 /* we should faithfully encode the LDIF, not combine */ /* Make sure all modops with multiple values are contiguous */ for (i=idn; i<lines; i++) { if ( mops[i] == M_SEP ) continue; for (j=i+1; j<lines; j++) { if ( mops[j] == M_SEP || mops[i] != mops[j] ) continue; if ( BV_CASEMATCH( btype+i, btype+j )) { nmods--; /* out of order, move intervening attributes down */ if ( j != i+1 ) { int c; struct berval bv; char fv; c = mops[j]; bv = vals[j]; fv = freeval[j]; for (k=j; k>i; k--) { btype[k] = btype[k-1]; vals[k] = vals[k-1]; freeval[k] = freeval[k-1]; mops[k] = mops[k-1]; } k++; btype[k] = btype[i]; vals[k] = bv; freeval[k] = fv; mops[k] = c; } i++; } } } #endif /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lm = ber_memalloc( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lines + nmods - idn) * sizeof(struct berval *)); pmods = (LDAPMod **)(lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); mops[idn-1] = M_SEP; for (i=idn; i<lines; i++) { if ( mops[i] == M_SEP ) continue; if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) { bvl[k++] = NULL; bv = btype[i]; lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES; lm[j].mod_type = bv.bv_val; if ( mops[i] & LDAP_MOD_BVALUES ) { lm[j].mod_bvalues = NULL; } else { lm[j].mod_bvalues = bvl+k; } pmods[j] = lm+j; j++; } bvl[k++] = vals+i; } bvl[k] = NULL; pmods[j] = NULL; doit: /* If default controls are set (as with -M option) and controls are specified in the LDIF file, we must add the default controls to the list of controls sent with the ldap operation. */ if ( rc == 0 ) { if (pctrls) { LDAPControl **defctrls = NULL; /* Default server controls */ LDAPControl **newctrls = NULL; ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls); if (defctrls) { int npc=0; /* Num of LDIF controls */ int ndefc=0; /* Num of default controls */ while (pctrls[npc]) npc++; /* Count LDIF controls */ while (defctrls[ndefc]) ndefc++; /* Count default controls */ newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*)); if (newctrls == NULL) { rc = LDAP_NO_MEMORY; } else { int i; pctrls = newctrls; for (i=npc; i<npc+ndefc; i++) { pctrls[i] = ldap_control_dup(defctrls[i-npc]); if (pctrls[i] == NULL) { rc = LDAP_NO_MEMORY; break; } } pctrls[npc+ndefc] = NULL; } ldap_controls_free(defctrls); /* Must be freed by library */ } } } if ( rc == 0 ) { if ( delete_entry ) { rc = dodelete( dn, pctrls ); } else if ( newrdn != NULL ) { rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls ); } else { rc = domodify( dn, pmods, pctrls, new_entry ); } if ( rc == LDAP_SUCCESS ) { rc = 0; } } leave: if (pctrls != NULL) { ldap_controls_free( pctrls ); } if ( lm != NULL ) { ber_memfree( lm ); } if ( mops != NULL ) { ber_memfree( mops ); } for (i=lines-1; i>=0; i--) if ( freeval[i] ) ber_memfree( vals[i].bv_val ); ber_memfree( btype ); return( rc ); }
/* Get a password from a file. */ int lutil_get_filed_password( const char *filename, struct berval *passwd ) { size_t nread, nleft, nr; FILE *f = fopen( filename, "r" ); if( f == NULL ) { perror( filename ); return -1; } passwd->bv_val = NULL; passwd->bv_len = 4096; #ifdef HAVE_FSTAT { struct stat sb; if ( fstat( fileno( f ), &sb ) == 0 ) { if( sb.st_mode & 006 ) { fprintf( stderr, _("Warning: Password file %s" " is publicly readable/writeable\n"), filename ); } if ( sb.st_size ) passwd->bv_len = sb.st_size; } } #endif /* HAVE_FSTAT */ passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 ); if( passwd->bv_val == NULL ) { perror( filename ); return -1; } nread = 0; nleft = passwd->bv_len; do { if( nleft == 0 ) { /* double the buffer size */ char *p = (char *) ber_memrealloc( passwd->bv_val, 2 * passwd->bv_len + 1 ); if( p == NULL ) { ber_memfree( passwd->bv_val ); passwd->bv_val = NULL; passwd->bv_len = 0; return -1; } nleft = passwd->bv_len; passwd->bv_len *= 2; passwd->bv_val = p; } nr = fread( &passwd->bv_val[nread], 1, nleft, f ); if( nr < nleft && ferror( f ) ) { ber_memfree( passwd->bv_val ); passwd->bv_val = NULL; passwd->bv_len = 0; return -1; } nread += nr; nleft -= nr; } while ( !feof(f) ); passwd->bv_len = nread; passwd->bv_val[nread] = '\0'; fclose( f ); return 0; }
static int monitor_subsys_rww_update( Operation *op, SlapReply *rs, Entry *e ) { monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private; Connection *c; int connindex; long nconns, nwritewaiters, nreadwaiters; int i; struct berval nrdn; Attribute *a; char buf[LDAP_PVT_INTTYPE_CHARS(long)]; long num = 0; ber_len_t len; assert( mi != NULL ); assert( e != NULL ); dnRdn( &e->e_nname, &nrdn ); for ( i = 0; !BER_BVISNULL( &monitor_rww[ i ].nrdn ); i++ ) { if ( dn_match( &nrdn, &monitor_rww[ i ].nrdn ) ) { break; } } if ( i == MONITOR_RWW_LAST ) { return SLAP_CB_CONTINUE; } nconns = nwritewaiters = nreadwaiters = 0; for ( c = connection_first( &connindex ); c != NULL; c = connection_next( c, &connindex ), nconns++ ) { if ( c->c_writewaiter ) { nwritewaiters++; } /* FIXME: ?!? */ if ( c->c_currentber != NULL ) { nreadwaiters++; } } connection_done(c); switch ( i ) { case MONITOR_RWW_READ: num = nreadwaiters; break; case MONITOR_RWW_WRITE: num = nwritewaiters; break; default: assert( 0 ); } snprintf( buf, sizeof( buf ), "%ld", num ); a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter ); assert( a != NULL ); len = strlen( buf ); if ( len > a->a_vals[ 0 ].bv_len ) { a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 ); if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) { BER_BVZERO( &a->a_vals[ 0 ] ); return SLAP_CB_CONTINUE; } } AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 ); a->a_vals[ 0 ].bv_len = len; /* FIXME: touch modifyTimestamp? */ return SLAP_CB_CONTINUE; }
static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen ) { LDAPMod **pmods; int i, j; struct berval *bvp; pmods = *pmodsp; modop |= LDAP_MOD_BVALUES; i = 0; if ( pmods != NULL ) { for ( ; pmods[ i ] != NULL; ++i ) { if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 && pmods[ i ]->mod_op == modop ) { break; } } } if ( pmods == NULL || pmods[ i ] == NULL ) { if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) * sizeof( LDAPMod * ))) == NULL ) { tester_perror( "realloc", NULL ); exit( EXIT_FAILURE ); } *pmodsp = pmods; pmods[ i + 1 ] = NULL; if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod ))) == NULL ) { tester_perror( "calloc", NULL ); exit( EXIT_FAILURE ); } pmods[ i ]->mod_op = modop; if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) { tester_perror( "strdup", NULL ); exit( EXIT_FAILURE ); } } if ( value != NULL ) { j = 0; if ( pmods[ i ]->mod_bvalues != NULL ) { for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) { ; } } if (( pmods[ i ]->mod_bvalues = (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ))) == NULL ) { tester_perror( "ber_memrealloc", NULL ); exit( EXIT_FAILURE ); } pmods[ i ]->mod_bvalues[ j + 1 ] = NULL; if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval ))) == NULL ) { tester_perror( "ber_memalloc", NULL ); exit( EXIT_FAILURE ); } pmods[ i ]->mod_bvalues[ j ] = bvp; bvp->bv_len = vlen; if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) { tester_perror( "malloc", NULL ); exit( EXIT_FAILURE ); } AC_MEMCPY( bvp->bv_val, value, vlen ); bvp->bv_val[ vlen ] = '\0'; } }
static int process_ldif_rec( char *rbuf, unsigned long linenum ) { LDIFRecord lr; int lrflags = ldapadd ? LDIF_DEFAULT_ADD : 0; int rc; struct berval rbuf_bv; #ifdef TEST_LDIF_API if ( getenv( "LDIF_ENTRIES_ONLY" ) ) { lrflags |= LDIF_ENTRIES_ONLY; } if ( getenv( "LDIF_NO_CONTROLS" ) ) { lrflags |= LDIF_NO_CONTROLS; } #endif /* TEST_LDIF_API */ rbuf_bv.bv_val = rbuf; rbuf_bv.bv_len = 0; /* not used */ rc = ldap_parse_ldif_record( &rbuf_bv, linenum, &lr, prog, lrflags ); /* If default controls are set (as with -M option) and controls are specified in the LDIF file, we must add the default controls to the list of controls sent with the ldap operation. */ if ( rc == 0 ) { if (lr.lr_ctrls) { LDAPControl **defctrls = NULL; /* Default server controls */ LDAPControl **newctrls = NULL; ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls); if (defctrls) { int npc=0; /* Num of LDIF controls */ int ndefc=0; /* Num of default controls */ while (lr.lr_ctrls[npc]) npc++; /* Count LDIF controls */ while (defctrls[ndefc]) ndefc++; /* Count default controls */ newctrls = ber_memrealloc(lr.lr_ctrls, (npc+ndefc+1)*sizeof(LDAPControl*)); if (newctrls == NULL) { rc = LDAP_NO_MEMORY; } else { int i; lr.lr_ctrls = newctrls; for (i=npc; i<npc+ndefc; i++) { lr.lr_ctrls[i] = ldap_control_dup(defctrls[i-npc]); if (lr.lr_ctrls[i] == NULL) { rc = LDAP_NO_MEMORY; break; } } lr.lr_ctrls[npc+ndefc] = NULL; } ldap_controls_free(defctrls); /* Must be freed by library */ } } } if ( rc == 0 ) { if ( LDAP_REQ_DELETE == lr.lr_op ) { rc = dodelete( &lr.lr_dn, lr.lr_ctrls ); } else if ( LDAP_REQ_RENAME == lr.lr_op ) { rc = dorename( &lr.lr_dn, &lr.lrop_newrdn, &lr.lrop_newsup, lr.lrop_delold, lr.lr_ctrls ); } else if ( ( LDAP_REQ_ADD == lr.lr_op ) || ( LDAP_REQ_MODIFY == lr.lr_op ) ) { rc = domodify( &lr.lr_dn, lr.lrop_mods, lr.lr_ctrls, LDAP_REQ_ADD == lr.lr_op ); } else { /* record skipped e.g. version: or comment or something we don't handle yet */ } if ( rc == LDAP_SUCCESS ) { rc = 0; } } ldap_ldif_record_done( &lr ); return( rc ); }