Beispiel #1
0
duo_code_t
_duo_prompt(struct duo_ctx *ctx, bson *obj, int flags, char *buf,
    size_t sz, const char **p)
{
    bson_iterator it;
    char *pos, *passcode;

    passcode = getenv(DUO_ENV_VAR_NAME);

    if ((flags & DUO_FLAG_ENV) && (passcode != NULL)) {
        *p = passcode;
        if (ctx->conv_status != NULL) {
            ctx->conv_status(ctx->conv_arg, ENV_VAR_MSG);
        }
    } else if ((flags & DUO_FLAG_AUTO) != 0) {
        /* Find default OOB factor for automatic login */
        _BSON_FIND(ctx, &it, obj, "factors", bson_object);
        bson_iterator_subobject(&it, obj);
        
        if (bson_find(&it, obj, "default") != bson_string) {
            _duo_seterr(ctx, "No default factor found for automatic login");
            return (DUO_ABORT);
        }
        *p = bson_iterator_string(&it);
        if (ctx->conv_status) {
            if ((pos = strstr(*p, "push"))) {
                ctx->conv_status(ctx->conv_arg, AUTOPUSH_MSG);
            } else if ((pos = strstr(*p, "phone"))) {
                ctx->conv_status(ctx->conv_arg, AUTOPHONE_MSG);
            } else {
                ctx->conv_status(ctx->conv_arg, AUTODEFAULT_MSG);
            }
        }
    } else {
        /* Prompt user for factor choice / token */
        if (ctx->conv_prompt == NULL) {
            _duo_seterr(ctx, "No prompt function set");
            return (DUO_CLIENT_ERROR);
        }
        _BSON_FIND(ctx, &it, obj, "prompt", bson_string);
        *p = bson_iterator_string(&it);
        
        if (ctx->conv_prompt(ctx->conv_arg, *p, buf, sz) == NULL) {
            _duo_seterr(ctx, "Error gathering user response");
            return (DUO_ABORT);
        }
        strtok(buf, "\r\n");

        _BSON_FIND(ctx, &it, obj, "factors", bson_object);
        bson_iterator_subobject(&it, obj);
        
        if (bson_find(&it, obj, buf) == bson_string) {
            *p = bson_iterator_string(&it);
        } else {
            *p = buf;
        }
    }
    return (DUO_CONTINUE);
}
Beispiel #2
0
static duo_code_t
_duo_bson_response(struct duo_ctx *ctx, bson *resp)
{
    bson obj;
    bson_iterator it;
    duo_code_t ret;
    const char *p;
    int code;
    
    bson_init(&obj, (char *)ctx->body, 0);
    
    ret = DUO_SERVER_ERROR;
    
    if (ctx->body_len <= 0 || bson_size(&obj) > ctx->body_len) {
        _duo_seterr(ctx, "invalid BSON response");
        return (ret);
    }
    _BSON_FIND(ctx, &it, &obj, "stat", bson_string);
    p = bson_iterator_string(&it);
    
    if (strcasecmp(p, "OK") == 0) {
        _BSON_FIND(ctx, &it, &obj, "response", bson_object);
        if (resp)
            bson_iterator_subobject(&it, resp);
        ret = DUO_OK;
    } else if (strcasecmp(p, "FAIL") == 0) {
        _BSON_FIND(ctx, &it, &obj, "code", bson_int);
        code = bson_iterator_int(&it);
        _BSON_FIND(ctx, &it, &obj, "message", bson_string);
        _duo_seterr(ctx, "%d: %s", code, bson_iterator_string(&it));
        ret = DUO_FAIL;
    }
    return (ret);
}
bool MongodbObject::GetSubObject(MongodbObject *subObject, const String &fieldname, bool needFinished)
{
    bool found = false;
    
    if(0 != objectData->object->finished)
    {
        bson_iterator it;
        bson_iterator_init(&it, objectData->object);
        
        bson_type foundType = bson_find(&it, objectData->object, fieldname.c_str());
        if(BSON_OBJECT == foundType)
        {
            found = true;
            
            bson sub;
            bson_iterator_subobject(&it, &sub);

            if(needFinished)
            {
                subObject->objectData->InitFinished(&sub);
            }
            else 
            {
                
                subObject->objectData->InitWith(&sub);
            }
        }
    }
    
    return found;
}
Beispiel #4
0
int
QueryElemMatch(char* key,BSONElem* objElem,BSONElem* query) {
   bson_iterator oItr;
   char* k = key;
   bson obj;
   bson so; 
   BSONElem tmpVal;
   BSONElem valElem;
   BSONElemItr it1;
   bson_iterator_init(&oItr,&obj);

   if (!objElem) {
      BSONElemInit(&tmpVal,BSON_EOO,NULL,0,NULL);
      return CompareValue(&tmpVal,query);
   }
   BSONElemItrInit(&it1,objElem);
   if (*k) {
      BSONElem soElem;
      BSONElem* elemPtr = NULL;
      bson_iterator sItr;
      bson b;

      char* keyEnd = strchr(key,'.');
      //printf("Key in new elem '%s','%s'\n",key,keyEnd);
      if (keyEnd) {
         *keyEnd = '\0';
         keyEnd ++;
      } else {
         //printf("No dot \n");
         keyEnd = key+strlen(key);
      }
      while (BSONElemItrNext(&it1,&tmpVal)) {
         if (tmpVal.type != BSON_OBJECT) {
            continue;
         }
         bson_iterator_subobject(tmpVal.itr,&b);
         bson_iterator_init(&sItr,&b);
         if (bson_find(&sItr,&b,key)) {
            BSONElemInitFromItr(&soElem,&sItr);
            elemPtr = &soElem;
         }
         if (QueryElemMatch(keyEnd,elemPtr,query)) {
            return True;
         }
      }
   } else {
      // end of the string
      if (query->type == BSON_ARRAY || query->type == BSON_OBJECT) {
         return CompareValue(objElem,query);
      }
      while (BSONElemItrNext(&it1,&tmpVal)) {
         if (CompareValue(&tmpVal,query)) {
            return True;
         }
      }
      return False;
   }

   return False;
}
Beispiel #5
0
MONGO_EXPORT void gridfile_get_metadata( gridfile *gfile, bson* out ) {
    bson_iterator it;

    if ( bson_find( &it, gfile->meta, "metadata" ) )
        bson_iterator_subobject( &it, out );
    else
        bson_empty( out );
}
void MongodbClient::ReadData(KeyedArchive* archive, void* bsonObj)
{
    if((!archive) || (!bsonObj)) return;

    bson_iterator it;
    bson_iterator_init(&it, (bson*)bsonObj);
    
    while (bson_iterator_next(&it))
    {
        String key = String(bson_iterator_key(&it));
        bson_type type = bson_iterator_type(&it);
        
        if(key == "_id") continue; // ignore _id

        switch (type)
        {
            case BSON_STRING:
                archive->SetString(key, String(bson_iterator_string(&it)));
                break;
                
            case BSON_INT:
                archive->SetInt32(key, bson_iterator_int(&it));
                break;
                
            case BSON_LONG:
                archive->SetInt32(key, (int32)bson_iterator_long(&it));
                break;
                
            case BSON_DOUBLE:
                archive->SetFloat(key, (float32)bson_iterator_double(&it));
                break;
                
            case BSON_OBJECT:
            {
                bson sub;
                
                bson_iterator_subobject(&it, &sub);
                
                KeyedArchive* subArchive = new KeyedArchive();
                ReadData(subArchive, &sub);
                archive->SetArchive(key, subArchive);
                SafeRelease(subArchive);
                break;
            }
                
            case BSON_OID:
                //TODO: add 12-bytes array
                //bson_append_oid(object, key, bson_iterator_oid(&it));
                break;
                
                
            default:
                DVASSERT(false);
                Logger::Error("[MongodbUpdateObject::ReadData] Not implemented type: %d", type);
                break;
        }
    }
}
Beispiel #7
0
bson gridfile_get_metadata( gridfile *gfile ) {
    bson sub;
    bson_iterator it;

    if ( bson_find( &it, gfile->meta, "metadata" ) ) {
        bson_iterator_subobject( &it, &sub );
        return sub;
    } else {
        bson_empty( &sub );
        return sub;
    }
}
void MongodbObject::EnableForEdit()
{
    bson *newObject = new bson();
    bson_init(newObject);
    
    bson_iterator it;
    bson_iterator_init(&it, objectData->object);

    while (bson_iterator_next(&it))
    {
        const char * key = bson_iterator_key(&it);
        bson_type type = bson_iterator_type(&it);
        
        switch (type)
        {
            case BSON_STRING:
                bson_append_string(newObject, key, bson_iterator_string(&it));
                break;

            case BSON_INT:
                bson_append_int(newObject, key, bson_iterator_int(&it));
                break;

            case BSON_LONG:
                bson_append_long(newObject, key, bson_iterator_long(&it));
                break;

            case BSON_DOUBLE:
                bson_append_double(newObject, key, bson_iterator_double(&it));
                break;

            case BSON_OBJECT:
                
                bson sub;
                bson_iterator_subobject(&it, &sub);
                bson_append_bson(newObject, key, &sub);
                break;
                
            case BSON_OID:
                bson_append_oid(newObject, key, bson_iterator_oid(&it));
                break;

                
            default:
                break;
        }
    }

    bson_destroy(objectData->object);
    SafeDelete(objectData->object);
    objectData->object = newObject;
}
 void InitWith(bson *obj)
 {
     bson_iterator it;
     bson_iterator_init(&it, obj);
     
     while (bson_iterator_next(&it))
     {
         const char * key = bson_iterator_key(&it);
         bson_type type = bson_iterator_type(&it);
         
         switch (type)
         {
             case BSON_STRING:
                 bson_append_string(object, key, bson_iterator_string(&it));
                 break;
                 
             case BSON_INT:
                 bson_append_int(object, key, bson_iterator_int(&it));
                 break;
                 
             case BSON_LONG:
                 bson_append_long(object, key, bson_iterator_long(&it));
                 break;
                 
             case BSON_DOUBLE:
                 bson_append_double(object, key, bson_iterator_double(&it));
                 break;
                 
             case BSON_OBJECT:
             {
                 bson sub;
                 
                 bson_iterator_subobject(&it, &sub);
                 bson_append_bson(object, key, &sub);
                 break;
             }
                 
             case BSON_OID:
                 bson_append_oid(object, key, bson_iterator_oid(&it));
                 break;
                 
                 
             default:
                 DVASSERT(false);
                 Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type);
                 break;
         }
     }
 }
Beispiel #10
0
uint8_t
QueryObjMatch(bson* obj,bson* query) {
   bson_iterator oItr,qItr; 
   bson_type o,q;

   bson_iterator_init(&oItr,obj);
   bson_iterator_init(&qItr,query);

   while ((o=bson_iterator_next(&oItr))) {
      BSONElem oVal,qVal;
      int cVal;
      q = bson_find(&qItr,query,bson_iterator_key(&oItr));
      if (!q) {
         return False;
      }
      if ( (o == BSON_OBJECT && q != BSON_OBJECT ) ||
           (q == BSON_OBJECT && o != BSON_OBJECT ) ) {
         return False;
      }
      if ( o == q && o == BSON_OBJECT) {
         bson o1,q1;
         bson_iterator_subobject(&oItr,&o1);
         bson_iterator_subobject(&qItr,&q1);
         return QueryObjMatch(&o1,&q1);
      }
      BSONElemInitFromItr(&oVal,&oItr);
      BSONElemInitFromItr(&qVal,&qItr);
      if (CompareElem(&oVal,&qVal,&cVal)) {
         return False;
      }
      if (cVal) {
         return False;
      }
   }
   return True;
}
Beispiel #11
0
int CompareValue(BSONElem* val,BSONElem* query) {
   bson_type qType = query->type;
   int r,cVal;
   if (qType == BSON_OBJECT) {
      r =  ComputeOperator(val,query);
      if (!r && val->type == BSON_OBJECT) {
         bson o,q;
         bson_iterator_subobject(val->itr,&o);
         bson_iterator_subobject(query->itr,&q);

         return QueryObjMatch(&o,&q); 
      }
      return r;
         // do individual element comparision with object
   }
   r = CompareElem(val,query,&cVal);
   if (r) {
      return False;
   }
   if (!cVal) {
      return True;
   }
   return False;
}
Beispiel #12
0
void json_from_bson_type(bson_type t, yajl_gen *g, bson_iterator *it)
{
    switch( t )
    {
    case BSON_ARRAY:
    {
        bson_iterator subi[1];
        bson_iterator_subiterator( it, subi );
        json_from_bson_array( g, subi );
        break;
    }
    case BSON_OBJECT:
    {
        bson sub[1];
        bson_iterator_subobject( it, sub );
        bson_iterator subi[1];
        bson_iterator_init( subi, sub );
        json_from_bson_object( g, subi );
        break;
    }
    case BSON_DOUBLE:
        json_from_bson_double( g, it );
        break;
    case BSON_STRING:
        json_from_bson_string( g, it );
        break;
    case BSON_BOOL:
        json_from_bson_bool( g, it );
        break;
    case BSON_NULL:
        json_from_bson_null( g );
        break;
    case BSON_INT:
        json_from_bson_int( g, it );
        break;
    case BSON_LONG:
        json_from_bson_long( g, it );
        break;
    default:
        break;
    }
}
Beispiel #13
0
EXPORT int mongo_bson_find(struct bson_* b, char* name, struct bson_iterator_** i) {
    bson* _b = (bson*)b;
    bson sub;
    bson_iterator iter;
    const char* next = name;
    do {
        int t;
        char *p;
        char prefix[2048];
        int len;
        if (bson_find(&iter, _b, next) != BSON_EOO) {
            bson_iterator* _i = (bson_iterator*)malloc(sizeof(bson_iterator));
            *_i = iter;
            *i = (struct bson_iterator_*)_i;
            return 1;
        }
        p = strchr((char*)next, '.');
        if (!p)
            return 0;
        len = (int)(p - next);
        strncpy(prefix, next, len);
        prefix[len] = '\0';
        if ((t = bson_find(&iter, _b, prefix)) == BSON_EOO)
            return 0;
        if (t == BSON_ARRAY || t == BSON_OBJECT) {
            bson_iterator_subobject(&iter, &sub);
            _b = &sub;
            next = p + 1;
        }
        else
            return 0;
    }
    while (1);
    /* never gets here */
    return 0;
}
Beispiel #14
0
bool bson_values_are_equal( bson_type t, const bson_iterator *it1, const bson_iterator *it2 )
{
    assert( it1 );
    assert( it2 );

    bool match;

    switch( t )
    {
    case BSON_ARRAY:
    {
        bson_iterator *sub_it1 = malloc( sizeof( bson_iterator ) );
        bson_iterator *sub_it2 = malloc( sizeof( bson_iterator ) );
        bson_iterator_subiterator( it1, sub_it1 );
        bson_iterator_subiterator( it2, sub_it2 );
        match = bson_cmp_array( sub_it1, sub_it2 );

        free( sub_it1 );
        free( sub_it2 );
        break;
    }
    case BSON_OBJECT:
    {
        bson first_sub[1], second_sub[1];

        bson_iterator_subobject( it1, first_sub );
        bson_iterator_subobject( it2, second_sub );
        match = bson_cmp( first_sub, second_sub );

        break;
    }
    case BSON_DOUBLE:
    {
        double first = bson_iterator_double( it1 );
        double second = bson_iterator_double( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_STRING:
    {
        const char* first = bson_iterator_value( it1 );
        const char* second = bson_iterator_value( it2 );
        match = (bool)(0 == strcmp( first, second ));
        break;
    }
    case BSON_BOOL:
    {
        bson_bool_t first = bson_iterator_bool( it1 );
        bson_bool_t second = bson_iterator_bool( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_NULL:
    {
        match = true;
        break;
    }
    case BSON_INT:
    {
        int first = bson_iterator_int( it1 );
        int second = bson_iterator_int( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_LONG:
    {
        int64_t first = bson_iterator_long( it1 );
        int64_t second = bson_iterator_long( it2 );
        match = (bool)(first == second);
        break;
    }
    default:
        break;
    }

    return match;
}