Пример #1
0
int main(int argc,char **argv) {
    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);


    apr_status_t st;
    apr_dbm_t *dbm;
    st=apr_dbm_open(&dbm,"friends",APR_DBM_RWCREATE,APR_OS_DEFAULT ,pool);

    apr_datum_t key,value;
    key.dptr="laomeng";
    key.dsize=strlen(key.dptr);
    
    value.dptr="*****@*****.**";
    value.dsize=strlen(value.dptr);
   
    st=apr_dbm_store(dbm,key,value);

    apr_datum_t pvalue;
    st=apr_dbm_fetch(dbm,key,&pvalue);

    char *pstr=apr_pstrndup(pool,pvalue.dptr,pvalue.dsize);
    printf("value => %s\n",pstr);
    
    apr_dbm_close(dbm);

    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}
Пример #2
0
static apr_status_t setUserInDb(apr_dbm_t *userDbm,
                                const char *user,
                                const char *password,
                                yubiauth_dir_cfg *cfg,
                                request_rec *r)
{
    char *timeAuthenticated = NULL;
    char *dbToken = NULL; //This is used to store pw:date
    char *userKey = NULL;

    apr_datum_t key, value;
    apr_status_t rv;

    /* Built up some combination of token:time */
    timeAuthenticated = apr_psprintf(r->pool, "%" APR_TIME_T_FMT, (apr_time_t) (apr_time_sec(apr_time_now())));
    dbToken = apr_pstrcat(r->pool, password, ":", timeAuthenticated, NULL);

    /* store OTP:time combo with username as key in DB */
    userKey = getDbKey(user, cfg, r);
    key = string2datum(userKey, r);
    value = string2datum(dbToken, r);

    /* Pump user into db, store un, cookie value, creation date,
     * let this expire sometime
     */
    ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_DEBUG, 0, r, LOG_PREFIX "Writing key: %s and value: %s to db",
                  key.dptr, value.dptr);
    rv = apr_dbm_store(userDbm, key, value);
    if (rv != APR_SUCCESS) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOG_PREFIX "Error writing to db ... with key: %s and value: %s",
                      key.dptr, value.dptr);
    }
    /* Spit back, so we can decide wheather s.th. went wrong or not */
    return rv;
}
Пример #3
0
    mbox_cache_touch(mbox_cache_info *mli)
{
    apr_status_t rv = APR_SUCCESS;
    char v[sizeof(apr_time_t)];
    apr_datum_t key;
    apr_datum_t nv;

    key.dptr = str_cache_mtime;
    key.dsize = strlen(str_cache_mtime) + 1;

    memcpy(v, &mli->mtime, sizeof(mli->mtime));
    nv.dptr = v;
    nv.dsize = sizeof(mli->mtime);

    rv = apr_dbm_store(mli->db, key, nv);
    /* dump_dbm(mli->db); */
    return rv;
}
Пример #4
0
    mbox_cache_set_count(mbox_cache_info *mli, int count, char *path)
{
    apr_status_t rv = APR_SUCCESS;
    char v[sizeof(int)];
    apr_datum_t key;
    apr_datum_t nv;

    key.dptr = path;
    key.dsize = strlen(path) + 1;

    memcpy(v, &count, sizeof(count));
    nv.dptr = v;
    nv.dsize = sizeof(v);

    rv = apr_dbm_store(mli->db, key, nv);

    return rv;
}
static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
{
    apr_datum_t key, val;

    if (!htdbm->username)
        return APR_SUCCESS;

    key.dptr = htdbm->username;
    key.dsize = strlen(htdbm->username);
    if (apr_dbm_exists(htdbm->dbm, key))
        *changed = 1;

    val.dsize = strlen(htdbm->ctx.passwd);
    if (!htdbm->comment)
        val.dptr  = htdbm->ctx.passwd;
    else {
        val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
                               htdbm->comment, NULL);
        val.dsize += (strlen(htdbm->comment) + 1);
    }
    return apr_dbm_store(htdbm->dbm, key, val);
}
Пример #6
0
    mbox_cache_update(mbox_cache_info ** mlix,
                  const char *path, apr_pool_t *pool,
                  char *list, char *domain)
{
    apr_status_t rv;
    char *temp;
    apr_datum_t key;
    apr_datum_t nv;
    mbox_cache_info *mli;
    int update_only = 0;
    int tver;

    OPEN_DBM(pool, mli, APR_DBM_READWRITE, path, temp, rv);

    if (rv != APR_SUCCESS) {
        OPEN_DBM(pool, mli, APR_DBM_RWCREATE, path, temp, rv);

        mli->mtime = 0;
        if (rv != APR_SUCCESS) {
            return rv;
        }
    }
    else {
        update_only = 1;
    }

    mli->pool = pool;
    apr_pool_cleanup_register(pool, (void *) mli, mli_cleanup,
                              apr_pool_cleanup_null);

    key.dptr = str_cache_version;
    key.dsize = strlen(str_cache_version) + 1;
    tver = MBOX_CACHE_VERSION;

    temp = apr_palloc(pool, sizeof(tver));
    memcpy(temp, &tver, sizeof(tver));

    nv.dptr = temp;
    nv.dsize = sizeof(tver);
    rv = apr_dbm_store(mli->db, key, nv);

    if (rv != APR_SUCCESS) {
        return rv;
    }

    if (update_only == 1) {
        key.dptr = str_cache_mtime;
        key.dsize = strlen(str_cache_mtime) + 1;
        rv = apr_dbm_fetch(mli->db, key, &nv);

        if (rv != APR_SUCCESS) {
            apr_dbm_close(mli->db);
            return rv;
        }
        if (nv.dptr && nv.dsize == sizeof(mli->mtime)) {
            memcpy(&mli->mtime, nv.dptr, sizeof(mli->mtime));
        }
        else {
            mli->mtime = 0;
        }
    }
    else {
        mli->mtime = 0;
    }

    key.dptr = str_cache_list;
    key.dsize = strlen(str_cache_list) + 1;
    nv.dptr = list;
    nv.dsize = strlen(list) + 1;
    rv = apr_dbm_store(mli->db, key, nv);
    if (rv != APR_SUCCESS) {
        return rv;
    }
    mli->domain = apr_pstrdup(pool, list);

    key.dptr = str_cache_domain;
    key.dsize = strlen(str_cache_domain) + 1;
    nv.dptr = domain;
    nv.dsize = strlen(domain) + 1;
    rv = apr_dbm_store(mli->db, key, nv);

    if (rv != APR_SUCCESS) {
        return rv;
    }
    mli->domain = apr_pstrdup(pool, domain);

    *mlix = mli;

    return rv;
}
Пример #7
0
static apr_status_t to_dbm(apr_dbm_t *dbm, apr_file_t *fp, apr_pool_t *pool)
{
    apr_status_t rv = APR_SUCCESS;
    char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */
    apr_datum_t dbmkey;
    apr_datum_t dbmval;
    apr_pool_t* p;

    apr_pool_create(&p, pool);

    while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
        char *c, *value;

        if (*line == '#' || apr_isspace(*line)) {
            continue;
        }

        c = line;

        while (*c && !apr_isspace(*c)) {
            ++c;
        }

        if (!*c) {
            /* no value. solid line of data. */
            continue;
        }

        dbmkey.dptr = apr_pstrmemdup(p, line,  c - line);
        dbmkey.dsize = (c - line);

        while (*c && apr_isspace(*c)) {
            ++c;
        }

        if (!*c) {
            apr_pool_clear(p);
            continue;
        }

        value = c;

        while (*c && !apr_isspace(*c)) {
            ++c;
        }

        dbmval.dptr = apr_pstrmemdup(p, value,  c - value);
        dbmval.dsize = (c - line);

        if (verbose) {
            apr_file_printf(errfile, "    '%s' -> '%s'"NL,
                            dbmkey.dptr, dbmval.dptr);
        }

        rv = apr_dbm_store(dbm, dbmkey, dbmval);

        apr_pool_clear(p);

        if (rv != APR_SUCCESS) {
            break;
        }
    }

    return rv;
}
Пример #8
0
dav_error * dav_dbm_store(dav_db *db, apr_datum_t key, apr_datum_t value)
{
    apr_status_t status = apr_dbm_store(db->file, key, value);

    return dav_fs_dbm_error(db, NULL, status);
}
Пример #9
0
/*
 * dav_generic_save_lock_record:  Saves the lock information specified in the
 *    direct and indirect lock lists about path into the lock database.
 *    If direct and indirect == NULL, the key is removed.
 */
static dav_error * dav_generic_save_lock_record(dav_lockdb *lockdb,
                                                apr_datum_t key,
                                                dav_lock_discovery *direct,
                                                dav_lock_indirect *indirect)
{
    dav_error *err;
    apr_status_t status;
    apr_datum_t val = { 0 };
    char *ptr;
    dav_lock_discovery *dp = direct;
    dav_lock_indirect *ip = indirect;

#if DAV_DEBUG
    if (lockdb->ro) {
        return dav_new_error(lockdb->info->pool,
                             HTTP_INTERNAL_SERVER_ERROR, 0, 0,
                             "INTERNAL DESIGN ERROR: the lockdb was opened "
                             "readonly, but an attempt to save locks was "
                             "performed.");
    }
#endif

    if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
        /* ### add a higher-level error? */
        return err;
    }

    /* If nothing to save, delete key */
    if (dp == NULL && ip == NULL) {
        /* don't fail if the key is not present */
        /* ### but what about other errors? */
        apr_dbm_delete(lockdb->info->db, key);
        return NULL;
    }

    while(dp) {
        val.dsize += dav_size_direct(dp);
        dp = dp->next;
    }
    while(ip) {
        val.dsize += dav_size_indirect(ip);
        ip = ip->next;
    }

    /* ### can this be apr_palloc() ? */
    /* ### hmmm.... investigate the use of a buffer here */
    ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);
    dp  = direct;
    ip  = indirect;

    while(dp) {
        /* Direct lock - lock_discovery struct follows */
        *ptr++ = DAV_LOCK_DIRECT;
        memcpy(ptr, dp, sizeof(dp->f));        /* Fixed portion of struct */
        ptr += sizeof(dp->f);
        memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));
        ptr += sizeof(*dp->locktoken);
        if (dp->owner == NULL) {
            *ptr++ = '\0';
        }
        else {
            memcpy(ptr, dp->owner, strlen(dp->owner) + 1);
            ptr += strlen(dp->owner) + 1;
        }
        if (dp->auth_user == NULL) {
            *ptr++ = '\0';
        }
        else {
            memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
            ptr += strlen(dp->auth_user) + 1;
        }

        dp = dp->next;
    }

    while(ip) {
        /* Indirect lock prefix */
        *ptr++ = DAV_LOCK_INDIRECT;

        memcpy(ptr, ip->locktoken, sizeof(*ip->locktoken));
        ptr += sizeof(*ip->locktoken);

        memcpy(ptr, &ip->timeout, sizeof(ip->timeout));
        ptr += sizeof(ip->timeout);

        memcpy(ptr, &ip->key.dsize, sizeof(ip->key.dsize));
        ptr += sizeof(ip->key.dsize);

        memcpy(ptr, ip->key.dptr, ip->key.dsize);
        ptr += ip->key.dsize;

        ip = ip->next;
    }

    if ((status = apr_dbm_store(lockdb->info->db, key, val)) != APR_SUCCESS) {
        /* ### more details? add an error_id? */
        err = dav_generic_dbm_new_error(lockdb->info->db, lockdb->info->pool,
                                        status);
        return dav_push_error(lockdb->info->pool,
                              HTTP_INTERNAL_SERVER_ERROR,
                              DAV_ERR_LOCK_SAVE_LOCK,
                              "Could not save lock information.",
                              err);
    }

    return NULL;
}