Beispiel #1
0
/*
 * Takes an array of mappings, similar to modify above, with the
 * exception that the 'op' field is ignored and not used at all.
 */
static void
f_ldap_add(INT32 args)
{
    struct pike_string     *dn;
    struct array           *arr;
    struct mapping         *m;
    struct svalue          *val;
    LDAPMod               **mods;
    int                     i, ret;

    if (!THIS->bound)
        Pike_error("OpenLDAP.Client: attempting operation on an unbound connection\n");
    
    get_all_args("OpenLDAP.Client->add()", args, "%S%a", &dn, &arr);
    mods = (LDAPMod**)calloc((arr->size + 1), sizeof(LDAPMod*));
    if (!mods)
        Pike_error("OpenLDAP.Client: OUT OF MEMORY!\n");

    for (i = 0; i < arr->size; i++) {
        mods[i] = (LDAPMod*)calloc(1, sizeof(LDAPMod));
        if (!mods[i])
            Pike_error("OpenLDAP.Client: OUT OF MEMORY!\n");
        mods[i]->mod_op = LDAP_MOD_BVALUES;

        if (arr->item[i].type != T_MAPPING)
            Pike_error("OpenLDAP.Client->add(): array member is not a mapping.\n");
        m = arr->item[i].u.mapping;
        
        val = low_mapping_string_lookup(m, modify_type);
        if (!val)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "Missing the '%s' field\n", "type");
        if (val->type != T_STRING || val->u.string->size_shift > 0)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "The '%s' field is not an 8-bit string\n", "type");
        mods[i]->mod_type = val->u.string->str;

        val = low_mapping_string_lookup(m, modify_values);
        if (!val)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "Missing the '%s' field\n", "values");
        if (val->type != T_ARRAY)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "The '%s' field is not an array\n", "values");
        
        mods[i]->mod_bvalues = make_berval_array(val);
    }
    
    ret = ldap_add_s(THIS->conn, dn->str, mods);
    if (ret != LDAP_SUCCESS)
        Pike_error("OpenLDAP.Client->add(): %s\n",
                   ldap_err2string(ret));
    
    free_mods(mods);
    
    pop_n_elems(args);
}
Beispiel #2
0
/* php_caudium_set_header() sets a header in the header mapping. Called in a
 * thread safe manner from php_caudium_sapi_header_handler.
 */
INLINE static void
php_caudium_set_header(char *header_name, char *value, char *p)
{
  struct svalue hsval;
  struct pike_string *hval, *ind, *hind;
  struct mapping *headermap;
  struct svalue *s_headermap, *soldval;
  int vallen;
  GET_THIS();
  /*  hval = make_shared_string(value); */
  ind = make_shared_string(" _headers");
  hind = make_shared_binary_string(header_name,
				   (int)(p - header_name));

  s_headermap = low_mapping_string_lookup(REQUEST_DATA, ind);
  if(!s_headermap || s_headermap->type != PIKE_T_MAPPING)
  {
    struct svalue mappie;                                           
    mappie.type = PIKE_T_MAPPING;
    headermap = allocate_mapping(1);
    mappie.u.mapping = headermap;
    mapping_string_insert(REQUEST_DATA, ind, &mappie);
    free_mapping(headermap);
    hval = make_shared_string(value);
  } else {
    headermap = s_headermap->u.mapping;
    soldval = low_mapping_string_lookup(headermap, hind);
    vallen = strlen(value);
    if(soldval != NULL && 
       soldval->type == PIKE_T_STRING &&
       soldval->u.string->size_shift == 0) {
      /* Existing, valid header. Prepend.*/
      hval = begin_shared_string(soldval->u.string->len + 1 + vallen);
      MEMCPY(hval->str, soldval->u.string->str, soldval->u.string->len);
      STR0(hval)[soldval->u.string->len] = '\0';
      MEMCPY(hval->str+soldval->u.string->len+1, value, vallen);
      hval = end_shared_string(hval);
    } else { 
      hval = make_shared_string(value);
    }
  }
  hsval.type = PIKE_T_STRING;
  hsval.u.string = hval;

  mapping_string_insert(headermap, hind, &hsval);

  free_string(hval);
  free_string(ind);
  free_string(hind);
}
Beispiel #3
0
INLINE static struct svalue *lookup_header(char *headername)
{
  struct svalue *headers, *value;
  struct pike_string *sind;
  GET_THIS();
  sind = make_shared_string("env");
  headers = low_mapping_string_lookup(REQUEST_DATA, sind);
  free_string(sind);
  if(!headers || headers->type != PIKE_T_MAPPING) return NULL;
  sind = make_shared_string(headername);
  value = low_mapping_string_lookup(headers->u.mapping, sind);
  free_string(sind);
  if(!value) return NULL;
  return value;
}
Beispiel #4
0
/* php_roxen_set_header() sets a header in the header mapping. Called in a
 * thread safe manner from php_roxen_sapi_header_handler.
 */
static void php_roxen_set_header(char *header_name, char *value, char *p)
{
  struct svalue hsval;
  struct pike_string *hval, *ind, *hind;
  struct mapping *headermap;
  struct svalue *s_headermap;
#ifdef ROXEN_USE_ZTS
  GET_THIS();
#endif
  hval = make_shared_string(value);
  ind = make_shared_string(" _headers");
  hind = make_shared_binary_string(header_name,
				   (int)(p - header_name));

  s_headermap = low_mapping_string_lookup(REQUEST_DATA, ind);
  if(!s_headermap)
  {
    struct svalue mappie;                                           
    mappie.type = PIKE_T_MAPPING;
    headermap = allocate_mapping(1);
    mappie.u.mapping = headermap;
    mapping_string_insert(REQUEST_DATA, ind, &mappie);
    free_mapping(headermap);
  } else
    headermap = s_headermap->u.mapping;

  hsval.type = PIKE_T_STRING;
  hsval.u.string = hval;
  mapping_string_insert(headermap, hind, &hsval);

  free_string(hval);
  free_string(ind);
  free_string(hind);
}
Beispiel #5
0
/*
 **| method: object search ( mapping params );
 **|   This is the preferred way of calling this method.
 **
 **| alt: object search ( string filter );
 **| alt: object search ( string filter, array attrs );
 **| alt: object search ( string filter, array attrs, int attrsonly );
 **| alt: object search ( string filter, array attrs, int attrsonly, int timeout );
 **
 **| general: search the LDAP directory for the specified entry
 **|   (entries). The first form is the preferred one as it
 **|   encompasses all the current and future parameters as expected
 **|   by the underlying API. The following fields are read from the
 **|   mapping:@nl
 **|
 **|   @list
 **|    * base - the base DN to use in this search. Overrides the
 **|      default base DN (as set with set_base_dn)
 **|    * scope - the search scope for this search. Overrides the
 **|      default scope (as set with set_scope)
 **|    * filter - the search filter. The BNF for the filter is as
 **|      follows:
 **|      @pre
 **|        <filter> ::= `(' <filtercomp> `)'
 **|        <filtercomp> ::= <and> | <or> | <not> | <simple>
 **|        <and> ::= `&' <filterlist>
 **|        <or> ::= `|' <filterlist>
 **|        <not> ::= `!' <filter>
 **|        <filterlist> ::= <filter> | <filter> <filterlist>
 **|        <simple> ::= <attributetype> <filtertype> <attributevalue>
 **|        <filtertype> ::= `=' | `~=' | `<=' | `>='
 **|      @pre_end
 **|    * attrs - search attributes. An array of attribute types to
 **|      return in the result. If absent, all types will be returned.
 **|    * attrsonly - if != then the result will contain attribute
 **|      types only - no values shall be returned.
 **|    * timeout - the timeout, in seconds, after which the call
 **|      should return if the search isn't finished.
 **|   @list_end
 */
static void
f_ldap_search(INT32 args)
{
    char                *filter;
    int                  scope;
    struct pike_string  *base;
    char               **attrs;
    int                  attrsonly;
    struct timeval      *timeout;
    LDAPMessage         *res;
    int                  ret;
    struct object       *obj;

    if (!THIS->bound)
        Pike_error("OpenLDAP.Client: attempting operation on an unbound connection\n");
    
    if (!args)
        Pike_error("OpenLDAP.Client->search() requires at least one argument\n");

    if (args == 1) {
        switch(ARG(1).type) {
            case T_MAPPING:
            {
                /* the new style case */
                struct svalue  *val;
                struct mapping *m = ARG(1).u.mapping;

                /*
                 * m->base
                 */
                val = low_mapping_string_lookup(m,
                                                base_str);
                if (!val || val->type != T_STRING)
                    base = THIS->basedn;
                else
                    base = val->u.string;

                /*
                 * m->scope
                 */
                val = low_mapping_string_lookup(m,
                                                scope_str);
                if (!val || val->type != T_INT)
                    scope = THIS->scope;
                else
                    scope = val->u.integer;

                /*
                 * m->filter
                 */
                val = low_mapping_string_lookup(m,
                                                filter_str);
                if (!val || val->type != T_STRING)
                    filter = OL_DEF_FILTER;
                else
                    filter = val->u.string->str;

                /*
                 * m->attrs
                 */
                val = low_mapping_string_lookup(m,
                                                attrs_str);
                if (!val || val->type != T_ARRAY)
                    attrs = NULL;
                else
                    attrs = make_c_array(val);

                /*
                 * m->attrsonly
                 */
                val = low_mapping_string_lookup(m,
                                                attrsonly_str);
                if (!val || val->type != T_INT)
                    attrsonly = 0;
                else
                    attrsonly = val->u.integer != 0;

                /*
                 * m->timeout
                 */
                val = low_mapping_string_lookup(m,
                                                timeout_str);
                if (!val || val->type != T_INT)
                    timeout = NULL;
                else
                    timeout = make_timeout(val->u.integer); /*TEMPORARY*/
                
                break;
            }

            case T_STRING:
                /* the old style case */                
                base = THIS->basedn;
                scope = THIS->scope;
                filter = ARG(1).u.string->str;
                attrs = NULL;
                attrsonly = 0;
                timeout = NULL;
                break;

            default:
                Pike_error("OpenLDAP.Client->search() with single argument requires either a mapping or a string\n");
                break;
        }
    } else switch(args) {
        case 4: /* timeout */
            if (ARG(4).type != T_INT)
                Pike_error("OpenLDAP.Client->search(): argument 4 must be an integer\n");
            else
                timeout = make_timeout(ARG(4).u.integer);
            /* fall through */

        case 3: /* attrsonly */
            if (ARG(3).type != T_INT)
                Pike_error("OpenLDAP.Client->search(): argument 3 must be an integer\n");
            else
                attrsonly = ARG(3).u.integer != 0;
            /* fall through */

        case 2: /* attrs */
            if (ARG(2).type != T_ARRAY)
                Pike_error("OpenLDAP.Client->search(): argument 2 must be an array\n");
            else
                attrs = make_c_array(&ARG(2));

            base = THIS->basedn;
            scope = THIS->scope;
            filter = ARG(1).u.string->str;
            attrsonly = 0;
            timeout = NULL;
            break;

        default:
            Pike_error("OpenLDAP.Client->search(): incorrect number of arguments\n");
            break;
    }

    ret = timeout ?
        ldap_search_st(THIS->conn,
                       base->str,
                       scope,
                       filter,
                       attrs,
                       attrsonly,
                       timeout,
                       &res) :
        ldap_search_s(THIS->conn,
                      base->str,
                      scope,
                      filter,
                      attrs,
                      attrsonly,
                      &res);    
    
    if (ret)
        Pike_error("OpenLDAP.Client->search(): %s\n",
                   ldap_err2string(ret));

    pop_n_elems(args);
    
    THIS->data = res;
    obj = clone_object(result_program, 0);
    push_object(obj);
    
    if (attrs)
        free(attrs);
}