예제 #1
0
static krb5_error_code
LDAP_get_string_value(HDB * db, LDAPMessage * entry,
		      const char *attribute, char **ptr)
{
    struct berval **vals;

    vals = ldap_get_values_len(HDB2LDAP(db), entry, attribute);
    if (vals == NULL || vals[0] == NULL) {
	*ptr = NULL;
	return HDB_ERR_NOENTRY;
    }

    *ptr = malloc(vals[0]->bv_len + 1);
    if (*ptr == NULL) {
	ldap_value_free_len(vals);
	return ENOMEM;
    }

    memcpy(*ptr, vals[0]->bv_val, vals[0]->bv_len);
    (*ptr)[vals[0]->bv_len] = 0;

    ldap_value_free_len(vals);

    return 0;
}
예제 #2
0
파일: ldap.c 프로젝트: LuaDist/lua-apr
static void set_attributes(lua_State *L, LDAP *ld, LDAPMessage *entry, int tab)
{
  BerElement *ber = NULL;
  BerValue **values;
  char *attr;
  int i, n;

  attr = ldap_first_attribute(ld, entry, &ber);
  while (attr != NULL) {
    values = ldap_get_values_len(ld, entry, attr);
    n = ldap_count_values_len(values);
    if (n == 0) /* no values */
      lua_pushboolean(L, 1);
    else if (n == 1) /* just one value */
      lua_pushlstring(L, values[0]->bv_val, values[0]->bv_len);
    else { /* multiple values */
      lua_newtable(L);
      for (i = 0; i < n; i++) {
        lua_pushlstring(L, values[i]->bv_val, values[i]->bv_len);
        lua_rawseti(L, -2, i+1);
      }
    }
    lua_setfield(L, tab, attr);
    ldap_value_free_len(values);
    ldap_memfree(attr);
    attr = ldap_next_attribute(ld, entry, ber);
  }
  ber_free(ber, 0);
}
예제 #3
0
파일: x3ldap.c 프로젝트: evilnet/x3
/* queries the ldap server for account..
 * if a single account match is found, 
 * email is allocated and set to the email address
 * and returns LDAP_SUCCESS. returns LDAP_OTHER if
 * 0 or 2+ entries are matched, or the proper ldap error
 * code for other errors.
 */ 
int ldap_get_user_info(const char *account, char **email) 
{
    int rc;
    struct berval **value;
    LDAPMessage *entry, *res;
    if(email)
      *email = NULL;
    if( (rc = ldap_search_user(account, &res)) == LDAP_SUCCESS) {
        entry = ldap_first_entry(ld, res);
        value = ldap_get_values_len(ld, entry, nickserv_conf.ldap_field_email);
        if(!value) {
           return(LDAP_OTHER);
        }
        if(email)
          *email = strdup(value[0]->bv_val);
        log_module(MAIN_LOG, LOG_DEBUG, "%s: %s\n", nickserv_conf.ldap_field_email, value[0]->bv_val);
        ldap_value_free_len(value);
        /*
        value = ldap_get_values(ld, entry, "description");
        log_module(MAIN_LOG, LOG_DEBUG, "Description: %s\n", value[0]);
        value = ldap_get_values(ld, entry, "userPassword");
        log_module(MAIN_LOG, LOG_DEBUG, "pass: %s\n", value ? value[0] : "error");
        */
    }
    return(rc);
}
예제 #4
0
PWCHAR kuhl_m_sid_getRootDomainNamingContext(LDAP *ld)
{
	DWORD dwErr;
	PWCHAR rootAttr[] = {L"rootDomainNamingContext", NULL}, ret = NULL;
	PLDAPMessage pMessage = NULL;
	PBERVAL *pBerVal;

	dwErr = ldap_search_s(ld, NULL, LDAP_SCOPE_BASE, L"(dn=RootDSE)", rootAttr, FALSE, &pMessage);
	if(dwErr == LDAP_SUCCESS)
	{
		if(ldap_count_entries(ld, pMessage) == 1)
		{
			if(pBerVal = ldap_get_values_len(ld, pMessage, rootAttr[0]))
			{
				if(ldap_count_values_len(pBerVal) == 1)
					ret = kull_m_string_qad_ansi_c_to_unicode(pBerVal[0]->bv_val, pBerVal[0]->bv_len);
				else PRINT_ERROR(L"ldap_get_values_len is NOT 1\n");
				ldap_value_free_len(pBerVal);
			}
		}
		else PRINT_ERROR(L"ldap_count_entries is NOT 1\n");
	}
	else PRINT_ERROR(L"ldap_search_s 0x%x (%u)\n", dwErr, dwErr);
	if(pMessage)
		ldap_msgfree(pMessage);
	return ret;
}
예제 #5
0
/// frees list of entries
/// @param[in] entries   list of entries to free
void ldaputils_free_entries(LDAPUtilsEntry ** entries)
{
   int  x;
   int  y;
   
   if (!(entries))
      return;
   
   for(x = 0; entries[x]; x++)
   {
      if (entries[x]->dn)
         ldap_memfree(entries[x]->dn);
      if (entries[x]->sortval)
         free(entries[x]->sortval);
      if (entries[x]->attributes)
      {
         for(y = 0; entries[x]->attributes[y]; y++)
         {
            if (entries[x]->attributes[y]->name)
               ldap_memfree(entries[x]->attributes[y]->name);
            if (entries[x]->attributes[y]->vals)
               ldap_value_free_len(entries[x]->attributes[y]->vals);
            free(entries[x]->attributes[y]);
         };
         free(entries[x]->attributes);
      };
      free(entries[x]);
   };
   free(entries);
   
   return;
}
예제 #6
0
/*
 * call-seq:
 * entry.get_values(attr)  => Array of String
 * entry.vals(attr)        => Array of String
 * entry[attr]             => Array of String
 *
 * Return an array of all the values belonging to the attribute, +attr+, of
 * the entry.
 */
VALUE
rb_ldap_entry_get_values (VALUE self, VALUE attr)
{
  RB_LDAPENTRY_DATA *edata;
  char *c_attr;
  struct berval **c_vals;
  int i;
  int count;
  VALUE vals;

  GET_LDAPENTRY_DATA (self, edata);
  c_attr = StringValueCStr (attr);

  c_vals = ldap_get_values_len (edata->ldap, edata->msg, c_attr);
  if (c_vals)
    {
      vals = rb_ary_new ();
      count = ldap_count_values_len (c_vals);
      for (i = 0; i < count; i++)
	{
	  VALUE str;
	  str = rb_tainted_str_new (c_vals[i]->bv_val, c_vals[i]->bv_len);
	  rb_ary_push (vals, str);
	}
      ldap_value_free_len (c_vals);
    }
  else
    {
      vals = Qnil;
    }

  return vals;
}
예제 #7
0
char *ReadLDAPValue(const char *Filter, char *Value)
{
	LDAPMessage	*res, *res2;
	struct berval **attrValues;
	char	*attrNames[] = {Value,NULL};
	char	*ret;
	struct timeval	timeout;
	 int	rv;
	
	timeout.tv_sec = 5;
	timeout.tv_usec = 0;
	
	rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
		attrNames, 0, NULL, NULL, &timeout, 1, &res
		);
	printf("ReadLDAPValue: rv = %i\n", rv);
	if(rv) {
		fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
			Value, Filter,
			ldap_err2string(rv)
			);
		return NULL;
	}
	
	res2 = ldap_first_entry(gpLDAP, res);
	attrValues = ldap_get_values_len(gpLDAP, res2, Value);
	
	ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
	
	ldap_value_free_len(attrValues);
	
	
	return ret;
}
예제 #8
0
파일: value.c 프로젝트: GYGit/reactos
/***********************************************************************
 *      ldap_get_valuesW     (WLDAP32.@)
 *
 * Retrieve string values for a given attribute.
 *
 * PARAMS
 *  ld     [I] Pointer to an LDAP context.
 *  entry  [I] Entry to retrieve values from.
 *  attr   [I] Attribute to retrieve values for.
 *
 * RETURNS
 *  Success: Pointer to a character array holding the values.
 *  Failure: NULL
 *
 * NOTES
 *  Call ldap_get_valuesW with the result of a call to
 *  ldap_first_entry or ldap_next_entry. Free the returned
 *  array with a call to ldap_value_freeW.
 */
PWCHAR * CDECL ldap_get_valuesW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PWCHAR attr )
{
    PWCHAR *ret = NULL;
#ifdef HAVE_LDAP
    char *attrU = NULL, **retU;
    struct berval **bv;

    TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_w(attr) );

    if (!ld || !entry || !attr) return NULL;

    attrU = strWtoU( attr );
    if (!attrU) return NULL;

    bv = ldap_get_values_len( ld, entry, attrU );

    retU = bv2str_array( bv );
    ret = strarrayUtoW( retU );

    ldap_value_free_len( bv );
    strarrayfreeU( retU );
    strfreeU( attrU );

#endif
    return ret;
}
예제 #9
0
파일: ldap.c 프로젝트: nks5295/lightwave
DWORD
_VdcGetAttributeFromEntry(
    LDAP *pLd,
    LDAPMessage* pEntry,
    PSTR pszAttributeName,
    PSTR *ppszAttributeValue
    )
{
    DWORD dwError = 0;
    PSTR pszAttributeValue = NULL;
    BerValue** ppBerValues = NULL;

    ppBerValues = ldap_get_values_len(pLd, pEntry, pszAttributeName);
    if (ppBerValues != NULL && ldap_count_values_len(ppBerValues) > 0)
    {
        dwError = VmDirAllocateStringA(ppBerValues[0][0].bv_val, &pszAttributeValue);
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    *ppszAttributeValue = pszAttributeValue;

cleanup:
    if (ppBerValues)
    {
        ldap_value_free_len(ppBerValues);
        ppBerValues = NULL;
    }

    return dwError;

error:
    VMDIR_SAFE_FREE_STRINGA(pszAttributeValue);
    goto cleanup;
}
예제 #10
0
int ipadb_ldap_attr_has_value(LDAP *lcontext, LDAPMessage *le,
                              char *attrname, const char *value)
{
    struct berval **vals;
    int ret = ENOENT;
    int i, result;

    vals = ldap_get_values_len(lcontext, le, attrname);
    if (vals) {
        for (i = 0; vals[i]; i++) {
            if (ulc_casecmp(vals[i]->bv_val, vals[i]->bv_len,
                            value, strlen(value),
                            NULL, NULL, &result) != 0) {
                ret = errno;
                break;
            }

            if (result == 0) {
                ret = 0;
                break;
            }
        }

        ldap_value_free_len(vals);
    }

    return ret;
}
예제 #11
0
파일: imap.c 프로젝트: lntoly/skeeter
static void
search_cb(LDAP *ld, LDAPMessage *msg, void *priv)
{
    struct imap_context *ctx = priv;
    struct bufferevent *server_bev;
    struct evbuffer *out;
    BerValue **servername = NULL;

    out = bufferevent_get_output(ctx->client_bev);
    if (msg) {
        servername = ldap_get_values_len(ld, msg, "mailhost");
    }

    // user not provisioned
    if (!servername || !*servername) {
        /*FIXME: need the full request to have somethng to send */
        evbuffer_add_printf(out, "some_tag " AUTH_FAILED_MSG CRLF);
        bufferevent_enable(ctx->client_bev, EV_READ);
        return;
    }

    server_bev = bufferevent_socket_new(ctx->driver->base, -1, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_enable(server_bev, EV_READ|EV_WRITE);
    bufferevent_socket_connect_hostname(server_bev, ctx->driver->dnsbase, AF_UNSPEC, servername[0]->bv_val, ctx->driver->config->default_port);
    bufferevent_setcb(server_bev, NULL, NULL, server_connect_cb, ctx);

    /*
    // copy over client data, CRLF in request has been skipped, so append that
    bufferevent_write(server_bev, req->line.bv_val, req->line.bv_len);
    bufferevent_write(server_bev, CRLF, 2);
    */

    ctx->server_bev = server_bev;
    ldap_value_free_len(servername);
}
예제 #12
0
파일: ldap.c 프로젝트: Mujj/tsps
int tsps_ldap_get_userid(const char *user)
{
	char user_filt[128];
	int res;
	char *attrs[] = { "uidNumber", NULL };
	LDAPMessage *vals = NULL;
	LDAPMessage *val;
	int uidnum = -1;

	snprintf(user_filt, sizeof(user_filt), "(uid=%s)", user);
	
	res = ldap_search_ext_s(ldp, server.ldap_user_base,
		LDAP_SCOPE_ONELEVEL, user_filt, attrs, 0, NULL, NULL,
		NULL, 0, &vals);
	if (res != LDAP_SUCCESS) {
		fprintf(stderr, "LDAP search error: %s\n",
			ldap_err2string(res));
		return -1;
	}

	for (val = ldap_first_message(ldp, vals); val;
			val = ldap_next_message(ldp, val)) {
		struct berval **uid_val =
			ldap_get_values_len(ldp, val, "uidNumber");

		if (uid_val) {
			uidnum = (int)strtol(uid_val[0]->bv_val, NULL, 0);
			ldap_value_free_len(uid_val);
			break;
		}
	}

	ldap_msgfree(vals);
	return uidnum;
}
예제 #13
0
파일: ldap.c 프로젝트: divyamehta/lightwave
static
int
VmAfdCountResultAttribute(
    LDAP        *pLotus,
    LDAPMessage *pSearchResult,
    PCSTR       pszAttribute
)
{
    int i = 0;
    int count = 0;
    struct berval** ppValues = NULL;

    ppValues = ldap_get_values_len(pLotus,
                                pSearchResult,
                                pszAttribute);
    if (ppValues)
    {
        while(ppValues[i++])
        {
            count ++;
        }

        ldap_value_free_len(ppValues);
        ppValues = NULL;
    }

    return count;

}
예제 #14
0
파일: ldap.c 프로젝트: baohaojun/dico
static int
db_get_pass(void *handle, const char *qpw, const char *key, char **ppass)
{
    struct _dico_ldap_handle *lp = handle;
    LDAPMessage *res, *msg;
    int rc;
    struct berval **values;
    
    res = _dico_ldap_search(lp, lp->user_filter, qpw, key);
    if (!res)
	return 1;

    rc = ldap_count_entries(lp->ldap, res);
    if (rc == 0) {
	dico_log(L_ERR, 0, "not enough entires");
	ldap_msgfree(res);
	return 1;
    }

    msg = ldap_first_entry(lp->ldap, res);
    
    values = ldap_get_values_len(lp->ldap, msg, qpw);
    if (ldap_count_values_len(values) == 0) {
	dico_log(L_ERR, 0, "not enough entires");
	ldap_msgfree(res);
	return 1;
    }
    *ppass = strdup(values[0]->bv_val);
    rc = *ppass == NULL;
    if (rc)
	dico_log(L_ERR, 0, "not enough memory");
    ldap_value_free_len(values);
    ldap_msgfree(res);
    return rc;
}
예제 #15
0
/** Frees memory used by a ld_fld structure.
 * This function frees all memory used by a ld_fld structure
 * @param fld Generic db_fld_t* structure being freed.
 * @param payload The ldap extension structure to be freed
 */
static void ld_fld_free(db_fld_t* fld, struct ld_fld* payload)
{
	db_drv_free(&payload->gen);
	if (payload->values) ldap_value_free_len(payload->values);	
	payload->values = NULL;
	if (payload->filter) pkg_free(payload->filter);
	payload->filter = NULL;
	pkg_free(payload);
}
예제 #16
0
krb5_error_code
krb5_decode_krbsecretkey(krb5_context context, krb5_db_entry *entries,
                         struct berval **bvalues,
                         krb5_tl_data *userinfo_tl_data, krb5_kvno *mkvno)
{
    char                        *user=NULL;
    int                         i=0, j=0, noofkeys=0;
    krb5_key_data               *key_data=NULL, *tmp;
    krb5_error_code             st=0;

    if ((st=krb5_unparse_name(context, entries->princ, &user)) != 0)
        goto cleanup;

    for (i=0; bvalues[i] != NULL; ++i) {
        krb5_int16 n_kd;
        krb5_key_data *kd;
        krb5_data in;

        if (bvalues[i]->bv_len == 0)
            continue;
        in.length = bvalues[i]->bv_len;
        in.data = bvalues[i]->bv_val;

        st = asn1_decode_sequence_of_keys (&in,
                                           &kd,
                                           &n_kd,
                                           mkvno);

        if (st != 0) {
            const char *msg = error_message(st);
            st = -1; /* Something more appropriate ? */
            krb5_set_error_message(context, st, _("unable to decode stored "
                                                  "principal key data (%s)"),
                                   msg);
            goto cleanup;
        }
        noofkeys += n_kd;
        tmp = key_data;
        key_data = realloc (key_data, noofkeys * sizeof (krb5_key_data));
        if (key_data == NULL) {
            key_data = tmp;
            st = ENOMEM;
            goto cleanup;
        }
        for (j = 0; j < n_kd; j++)
            key_data[noofkeys - n_kd + j] = kd[j];
        free (kd);
    }

    entries->n_key_data = noofkeys;
    entries->key_data = key_data;

cleanup:
    ldap_value_free_len(bvalues);
    free (user);
    return st;
}
예제 #17
0
파일: ldap.c 프로젝트: henrikhodne/rldap
static VALUE ldapmessage2obj(LDAP *ld, LDAPMessage *msg)
{
	VALUE obj;
	
	char *dn, *attr;
	BerElement *ber;
	BerValue **values;
	BerValue *value;
	VALUE rdn, attrs, ary, str;
	int length, i;

	obj = rb_class_new_instance(0, NULL, cLDAP_Message);
	
	// Set the DN
	dn = ldap_get_dn(ld, msg);
	rdn = rb_str_new2(dn);
	ldap_memfree(dn);
	rb_iv_set(obj, "@dn", rdn);
	
	// Set the attributes
	attrs = rb_hash_new();
	attr = ldap_first_attribute(ld, msg, &ber);
	do {
		values = ldap_get_values_len(ld, msg, attr);

		if (values == NULL) {
			rldap_raise(rldap_errno_c(obj));
		}

		ary = rb_ary_new();
		length = ldap_count_values_len(values);

		for (i=0; i<length; i++) {
			value = values[i];
			if (value->bv_len == 4 && strncmp(value->bv_val, "TRUE", value->bv_len) == 0)
				rb_ary_push(ary, Qtrue);
			else if (value->bv_len == 5 && strncmp(value->bv_val, "FALSE", value->bv_len) == 0)
				rb_ary_push(ary, Qfalse);
			else {
				str = rb_str_new(value->bv_val, value->bv_len);
				rb_ary_push(ary, str);
			}
		}
		
		rb_hash_aset(attrs, rb_str_new2(attr), ary);

		ldap_value_free_len(values);
		ldap_memfree(attr);
	} while (attr = ldap_next_attribute(ld, msg, ber));

	ber_free(ber, 0);

	rb_iv_set(obj, "@attrs", attrs);

	return obj;
}
예제 #18
0
파일: value.c 프로젝트: GYGit/reactos
/***********************************************************************
 *      ldap_value_free_len     (WLDAP32.@)
 *
 * Free an array of berval structures.
 *
 * PARAMS
 *  vals  [I] Array of berval structures.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
ULONG CDECL WLDAP32_ldap_value_free_len( struct WLDAP32_berval **vals )
{
#ifdef HAVE_LDAP

    TRACE( "(%p)\n", vals );
    ldap_value_free_len( (struct berval **)vals );

#endif
    return WLDAP32_LDAP_SUCCESS;
}
예제 #19
0
파일: ldap.c 프로젝트: divyamehta/lightwave
static
DWORD
VmAfdCopyQueryResultAttributeString(
    LDAP*        pLotus,
    LDAPMessage* pCAResult,
    PCSTR        pszAttribute,
    BOOL         bOptional,
    PSTR*        ppszOut
)
{
    DWORD   dwError = 0;
    struct berval** ppValues = NULL;
    PSTR   pszOut = NULL;

    ppValues = ldap_get_values_len(
                                pLotus,
                                pCAResult,
                                pszAttribute);
    if (ppValues && ppValues[0])
    {
        dwError = VmAfdAllocateMemory(
                        sizeof(CHAR) * ppValues[0]->bv_len + 1,
                        (PVOID)&pszOut);
        BAIL_ON_VMAFD_ERROR(dwError);
        memcpy(
            (PVOID) pszOut,
            (PVOID) ppValues[0]->bv_val,
            (size_t) ppValues[0]->bv_len);
    }
    else if (!bOptional)
    {
        dwError = ERROR_INVALID_DATA;
        BAIL_ON_VMAFD_ERROR(dwError);
    }

    *ppszOut = pszOut;

cleanup:

    if (ppValues)
    {
        ldap_value_free_len(ppValues);
        ppValues = NULL;
    }
    return dwError;

error:

    VMAFD_SAFE_FREE_MEMORY(pszOut);
    if (ppszOut)
    {
        *ppszOut = NULL;
    }
    goto cleanup;
}
예제 #20
0
파일: ldap.c 프로젝트: vmware/lightwave
static
DWORD
VMCACopyQueryResultAttributeString(
    PVMCA_LDAP_CONTEXT pContext,
    LDAPMessage* pSearchResult,
    PCSTR        pszAttribute,
    BOOL         bOptional,
    PSTR*        ppszOut
)
{
    DWORD   dwError = 0;
    struct berval** ppValues = NULL;
    PSTR   pszOut = NULL;

    ppValues = ldap_get_values_len(
                                pContext->pConnection,
                                pSearchResult,
                                pszAttribute);
    if (ppValues && ppValues[0])
    {
        dwError = VMCAAllocateMemory(
                        (DWORD)(sizeof(CHAR) * ppValues[0]->bv_len + 1),
                        (PVOID*)&pszOut);
        BAIL_ON_ERROR(dwError);
        memcpy(
            (PVOID) pszOut,
            (PVOID) ppValues[0]->bv_val,
            (size_t) ppValues[0]->bv_len);
    }
    else if (!bOptional)
    {
        dwError = ERROR_INVALID_DATA;
        BAIL_ON_ERROR(dwError);
    }

    *ppszOut = pszOut;

cleanup:

    if (ppValues)
    {
        ldap_value_free_len(ppValues);
        ppValues = NULL;
    }
    return dwError;

error:

    if (ppszOut)
    {
        *ppszOut = NULL;
    }
    VMCA_SAFE_FREE_MEMORY(pszOut);
    goto cleanup;
}
/*
 * parses the result returned by an ldap query
 */
static err_t
parse_ldap_result(LDAP * ldap, LDAPMessage *result, chunk_t *blob)
{
    err_t ugh = NULL;

    LDAPMessage * entry = ldap_first_entry(ldap, result);

    if (entry != NULL)
    {
	BerElement *ber = NULL;
	char *attr;
	
	attr = ldap_first_attribute(ldap, entry, &ber);

	if (attr != NULL)
	{
	    struct berval **values = ldap_get_values_len(ldap, entry, attr);

	    if (values != NULL)
	    {
		if (values[0] != NULL)
		{
		    blob->len = values[0]->bv_len;
		    blob->ptr = alloc_bytes(blob->len, "ldap blob");
		    memcpy(blob->ptr, values[0]->bv_val, blob->len);
		    if (values[1] != NULL)
		    {
			plog("warning: more than one value was fetched from LDAP URL");
		    }
		}
		else
		{
		    ugh = "no values in attribute";
		}
		ldap_value_free_len(values);
	    }
	    else
	    {
		ugh = ldap_err2string(ldap_result2error(ldap, entry, 0));
	    }
	    ldap_memfree(attr);
	}
	else
	{
	    ugh = ldap_err2string(ldap_result2error(ldap, entry, 0));
	}
	ber_free(ber, 0);
    }
    else
    {
	ugh = ldap_err2string(ldap_result2error(ldap, result, 0));
    }
    return ugh;
}
예제 #22
0
int ipadb_ldap_attr_to_strlist(LDAP *lcontext, LDAPMessage *le,
                               char *attrname, char ***result)
{
    struct berval **vals = NULL;
    char **strlist = NULL;
    int ret;
    int i;

    vals = ldap_get_values_len(lcontext, le, attrname);
    if (!vals) {
        return ENOENT;
    }

    for (i = 0; vals[i]; i++) /* count */ ;

    strlist = calloc(i + 1, sizeof(char *));
    if (!strlist) {
        ret = ENOMEM;
        goto fail;
    }

    for (i = 0; vals[i]; i++) {
        strlist[i] = strndup(vals[i]->bv_val, vals[i]->bv_len);
        if (!strlist[i]) {
            ret = ENOMEM;
            goto fail;
        }
    }

    ldap_value_free_len(vals);
    *result = strlist;
    return 0;

fail:
    ldap_value_free_len(vals);
    for (i = 0; strlist && strlist[i]; i++) {
        free(strlist[i]);
    }
    free(strlist);
    return ret;
}
예제 #23
0
파일: ldap.cpp 프로젝트: Robby-/anope
	void BuildReply(int res, LDAPRequest *req)
	{
		LDAPResult *ldap_result = req->result = new LDAPResult();
		req->result->type = req->type;

		if (res != LDAP_SUCCESS)
		{
			ldap_result->error = ldap_err2string(res);
			return;
		}

		if (req->message == NULL)
		{
			return;
		}

		/* a search result */

		for (LDAPMessage *cur = ldap_first_message(this->con, req->message); cur; cur = ldap_next_message(this->con, cur))
		{
			LDAPAttributes attributes;

			char *dn = ldap_get_dn(this->con, cur);
			if (dn != NULL)
			{
				attributes["dn"].push_back(dn);
				ldap_memfree(dn);
				dn = NULL;
			}

			BerElement *ber = NULL;

			for (char *attr = ldap_first_attribute(this->con, cur, &ber); attr; attr = ldap_next_attribute(this->con, cur, ber))
			{
				berval **vals = ldap_get_values_len(this->con, cur, attr);
				int count = ldap_count_values_len(vals);

				std::vector<Anope::string> attrs;
				for (int j = 0; j < count; ++j)
					attrs.push_back(vals[j]->bv_val);
				attributes[attr] = attrs;

				ldap_value_free_len(vals);
				ldap_memfree(attr);
			}

			if (ber != NULL)
				ber_free(ber, 0);

			ldap_result->messages.push_back(attributes);
		}
	}
예제 #24
0
DWORD
LwLdapGetBytes(
        HANDLE hDirectory,
        LDAPMessage* pMessage,
        PSTR pszFieldName,
        PBYTE* ppszByteValue,
        PDWORD pszByteLen
        )
{
    DWORD dwError = LW_ERROR_SUCCESS;
    PLW_LDAP_DIRECTORY_CONTEXT pDirectory = NULL;
    struct berval **ppszValues = NULL;
    PBYTE pszByteValue = NULL;
    DWORD szByteLen = 0;

    pDirectory = (PLW_LDAP_DIRECTORY_CONTEXT)hDirectory;

    ppszValues = ldap_get_values_len(pDirectory->ld, pMessage, pszFieldName);

    if (ppszValues && ppszValues[0]){
        if (ppszValues[0]->bv_len != 0){
            dwError = LwAllocateMemory(
                        sizeof(BYTE) * ppszValues[0]->bv_len,
                        OUT_PPVOID(&pszByteValue));
            BAIL_ON_LW_ERROR(dwError);
            memcpy (pszByteValue, ppszValues[0]->bv_val, ppszValues[0]->bv_len * sizeof (BYTE));
            szByteLen = ppszValues[0]->bv_len;
        }
    }

    *ppszByteValue = pszByteValue;
    *pszByteLen = szByteLen;

cleanup:

    if (ppszValues) {
        ldap_value_free_len(ppszValues);
    }

    return dwError;

error:
    *ppszByteValue = NULL;
    *pszByteLen = 0;

    LW_SAFE_FREE_MEMORY(pszByteValue);

    goto cleanup;
}
예제 #25
0
int ipadb_ldap_attr_to_int(LDAP *lcontext, LDAPMessage *le,
                           char *attrname, int *result)
{
    struct berval **vals;
    int ret = ENOENT;

    vals = ldap_get_values_len(lcontext, le, attrname);
    if (vals) {
        *result = atoi(vals[0]->bv_val);
        ret = 0;
        ldap_value_free_len(vals);
    }

    return ret;
}
예제 #26
0
/*
 * Get the certificate subject base from the IPA configuration.
 *
 * Not considered a show-stopper if this fails for some reason.
 *
 * The caller is responsible for binding/unbinding to LDAP.
 */
static int
get_subject(LDAP *ld, char *ldap_base, const char **subject, int quiet)
{
    char *attrs[] = {"ipaCertificateSubjectBase", NULL};
    char *base = NULL;
    LDAPMessage *entry, *res = NULL;
    struct berval **ncvals;
    int ret, rval = 0;

    ret = asprintf(&base, "cn=ipaconfig,cn=etc,%s", ldap_base);
    if (ret == -1)
    {
        if (!quiet)
            fprintf(stderr, _("Out of memory!\n"));
        rval = 3;
        goto done;
    }

    ret = ldap_search_ext_s(ld, base, LDAP_SCOPE_BASE,
                            "objectclass=*", attrs, 0,
                            NULL, NULL, NULL, 0, &res);

    if (ret != LDAP_SUCCESS) {
        fprintf(stderr,
                _("Search for ipaCertificateSubjectBase failed with error %d"),
                ret);
        rval = 14;
        goto done;
    }

    entry = ldap_first_entry(ld, res);
    ncvals = ldap_get_values_len(ld, entry, attrs[0]);
    if (!ncvals) {
        fprintf(stderr, _("No values for %s"), attrs[0]);
        rval = 14;
        goto done;
    }

    *subject = strdup(ncvals[0]->bv_val);

    ldap_value_free_len(ncvals);

done:
    free(base);
    if (res) ldap_msgfree(res);

    return rval;
}
예제 #27
0
static char *
get_variable (const char  *varname,
	      LDAP        *connection,
	      LDAPMessage *entry)
{
  BerElement *berptr;
  const char *attr;
  char       *retval;

  if (strcmp (varname, "USER") == 0)
    return g_strdup (g_get_user_name ());

  if (strcmp (varname, "EVOLUTION_UID") == 0)
    return get_evolution_uid ();

  if (connection == NULL || entry == NULL)
    return g_strdup ("");

  if (strncmp (varname, "LDAP_ATTR_", 10) != 0)
    return g_strdup ("");

  varname += 10;

  retval = NULL;

  berptr = NULL;
  attr = ldap_first_attribute (connection, entry, &berptr);
  while (attr != NULL && retval == NULL)
    {
      struct berval **values;

      if (strcmp (attr, varname) == 0)
	{
	  values = ldap_get_values_len (connection, entry, attr);
	  if (values != NULL && values[0] != NULL)
	    retval = g_strdup (values[0]->bv_val);
	  ldap_value_free_len (values);
	}

      attr = ldap_next_attribute (connection, entry, berptr);
    }

  ber_free (berptr, 0);

  return retval ? retval : g_strdup ("");
}
예제 #28
0
/**
 * Add all LDAP attribute values to a list.
 * \param ld LDAP handle.
 * \param entry LDAP entry to process.
 * \param attr  LDAP attribute.
 * \return List of values.
 */
static GSList *ldapqry_add_list_values(
		LDAP *ld, LDAPMessage *entry, char *attr )
{
	GSList *list = NULL;
	gint i;
	struct berval **vals;

	if( ( vals = ldap_get_values_len( ld, entry, attr ) ) != NULL ) {
		for( i = 0; vals[i] != NULL; i++ ) {
			/*debug_print("lv\t%s: %s\n", attr?attr:"null",
					vals[i]->bv_val?vals[i]->bv_val:"null");*/
			list = g_slist_append( list, g_strndup( vals[i]->bv_val, vals[i]->bv_len) );
		}
	}
	ldap_value_free_len( vals );
	return list;
}
예제 #29
0
/*
** Push an attribute value (or a table of values) on top of the stack.
** @param L lua_State.
** @param ld LDAP Connection.
** @param entry Current entry.
** @param attr Name of entry's attribute to get values from.
** @return 1 in case of success.
*/
static int push_values (lua_State *L, LDAP *ld, LDAPMessage *entry, char *attr) {
	int i, n;
	BerValue **vals = ldap_get_values_len (ld, entry, attr);
	n = ldap_count_values_len (vals);
	if (n == 0) /* no values */
		lua_pushboolean (L, 1);
	else if (n == 1) /* just one value */
		lua_pushlstring (L, vals[0]->bv_val, vals[0]->bv_len);
	else { /* Multiple values */
		lua_newtable (L);
		for (i = 0; i < n; i++) {
			lua_pushlstring (L, vals[i]->bv_val, vals[i]->bv_len);
			lua_rawseti (L, -2, i+1);
		}
	}
	ldap_value_free_len (vals);
	return 1;
}
예제 #30
0
static InternetAddress*
lbabl_get_internet_address(LDAP *dir, LDAPMessage * e)
{
    InternetAddress *ia;
    BerElement *ber = NULL;
    char *attr;
    struct berval **vals;
    int i;
    gchar *email = NULL, *sn = NULL, *cn = NULL, *first = NULL;

    for (attr = ldap_first_attribute(dir, e, &ber);
	 attr != NULL; 
         attr = ldap_next_attribute(dir, e, ber)) {
	/*
	 * For each attribute, get the attribute name and values.
	 */
	if ((vals = ldap_get_values_len(dir, e, attr)) != NULL) {
	    for (i = 0; vals[i] != NULL; i++) {
		if ((g_ascii_strcasecmp(attr, "sn") == 0) && (!sn))
		    sn = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "cn") == 0) && (!cn))
		    cn = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "givenName") == 0) && (!first))
		    first = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "mail") == 0) && (!email))
		    email = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
	    }
	    ldap_value_free_len(vals);
	}
        ldap_memfree(attr);
    }
    /*
     * Record will have e-mail (searched)
     */
    if(email == NULL) email = g_strdup("none");
    g_return_val_if_fail(email != NULL, NULL);

    if(!cn)
        cn = create_name(first, sn);
    ia = internet_address_mailbox_new(cn, email);
    g_free(email); g_free(sn); g_free(cn); g_free(first);

    return ia;
}