Class: Keepass::Database

Inherits:
Object
  • Object
show all
Defined in:
ext/keepass.c,
ext/keepass.c

Overview

A class representing a Keepass database.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rb_file, rb_password) ⇒ Object

Opens up a Keepass database with the given filename and password.

call-seq:

Keepass::Database.new(filename, password)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/keepass.c', line 202

VALUE
rb_kp_db_initialize(VALUE self, VALUE rb_file, VALUE rb_password)
{
    ID id_read;
    VALUE bytes;
    const char *c_bytes;
    long c_length;
    kpass_db *kdb = NULL;
    VALUE kdb_object;
    kpass_retval result;
    uint8_t hashed_pass[32];

    if(TYPE(rb_file) == T_STRING) {
        VALUE rb_filename = rb_file;
        ID id_new         = rb_intern("open");

        rb_file = rb_funcall(rb_cFile, id_new, 2, rb_filename,
            rb_str_new_cstr("rb"));
    }

    Check_Type(rb_file, T_FILE); /* XXX looser type check? */
    Check_Type(rb_password, T_STRING);

    id_read  = rb_intern("read");
    bytes    = rb_funcall(rb_file, id_read, 0);
    c_length = RSTRING_LEN(bytes);
    c_bytes  = RSTRING_PTR(bytes);

    kdb_object = Data_Make_Struct(cDatabase, kpass_db, 0, kpass_free_db, kdb); 

    result = kpass_init_db(kdb, c_bytes, c_length);

    if(result != kpass_success) {
        raise_kp_exception(result);
    }

    result = kpass_hash_pw(kdb, RSTRING_PTR(rb_password), hashed_pass);

    if(result != kpass_success) {
        raise_kp_exception(result);
    }

    result = kpass_decrypt_db(kdb, hashed_pass);

    if(result != kpass_success) {
        raise_kp_exception(result);
    }

    rb_ivar_set(self, rb_intern("@kdb"), kdb_object);

    return Qnil;
}

Class Method Details

.open(rb_file, rb_password) ⇒ Object

Opens up a Keepass database with the given filename and password.

call-seq:

Keepass::Database.open(filename, password)


264
265
266
267
268
269
270
271
272
# File 'ext/keepass.c', line 264

VALUE
rb_kp_db_open(VALUE klass, VALUE rb_file, VALUE rb_password)
{
    ID id_new;

    id_new = rb_intern("new");

    return rb_funcall(klass, id_new, 2, rb_file, rb_password);
}

Instance Method Details

#entriesObject

Returns an Array of entries in this database.



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'ext/keepass.c', line 360

VALUE
rb_kp_db_entries(VALUE self)
{
    VALUE kdb_object;
    kpass_db *kdb = NULL;
    VALUE entries;
    uint32_t i;

    kdb_object = rb_ivar_get(self, rb_intern("@kdb"));
    Data_Get_Struct(kdb_object, kpass_db, kdb);

    entries = rb_ary_new();

    for(i = 0; i < kdb->entries_len; i++) {
        VALUE rb_entry;
        kpass_entry *entry = kdb->entries[i];

        if(! strcmp(entry->title, "Meta-Info")) {
            continue;
        }

        rb_entry = _create_ruby_entry(entry);

        rb_ary_push(entries, rb_entry);
    }

    return entries;
}

#groupsObject

Returns an Array of groups in this database.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'ext/keepass.c', line 315

VALUE
rb_kp_db_groups(VALUE self)
{
    VALUE kdb_object;
    kpass_db *kdb = NULL;
    VALUE groups;
    uint32_t i;

    kdb_object = rb_ivar_get(self, rb_intern("@kdb"));
    Data_Get_Struct(kdb_object, kpass_db, kdb);

    groups = rb_ary_new2(kdb->groups_len);

    for(i = 0; i < kdb->groups_len; i++) {
        VALUE rb_group = _create_ruby_group(self, kdb->groups[i]);

        rb_ary_push(groups, rb_group);
    }

    return groups;
}