From: Jonathan McDowell Date: Mon, 31 May 2004 23:47:26 +0000 (+0000) Subject: cscvs to tla changeset 56 X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/76c5dd1081bc9db3bf446480457b52d4ae7b3891 cscvs to tla changeset 56 Author: noodles Date: 2003/02/12 22:48:14 Add code to do a dump of the key database to a flat OpenPGP data file. --- diff --git a/keydb.h b/keydb.h index 647916c..7deb95a 100644 --- a/keydb.h +++ b/keydb.h @@ -133,4 +133,16 @@ struct ll *cached_getkeysigs(uint64_t keyid); */ uint64_t getfullkeyid(uint64_t keyid); +/** + * dumpdb - dump the key database + * @filenamebase: The base filename to use for the dump. + * + * Dumps the database into one or more files, which contain pure OpenPGP + * that can be reimported into onak or gpg. filenamebase provides a base + * file name for the dump; several files may be created, all of which will + * begin with this string and then have a unique number and a .pgp + * extension. + */ +int dumpdb(char *filenamebase); + #endif /* __KEYDB_H__ */ diff --git a/keydb_db3.c b/keydb_db3.c index 4d1bcca..de24f75 100644 --- a/keydb_db3.c +++ b/keydb_db3.c @@ -107,7 +107,6 @@ struct ll *makewordlist(struct ll *wordlist, char *word) */ void initdb(void) { - char buf[1024]; int ret = 0; ret = db_env_create(&dbenv, 0); @@ -198,6 +197,7 @@ bool starttrans(void) { int ret; + assert(dbenv != NULL); assert(txn == NULL); ret = txn_begin(dbenv, @@ -221,6 +221,7 @@ void endtrans(void) { int ret; + assert(dbenv != NULL); assert(txn != NULL); ret = txn_commit(txn, @@ -699,6 +700,52 @@ int delete_key(uint64_t keyid, bool intrans) return deadlock ? (-1) : (ret == DB_NOTFOUND); } +/** + * dumpdb - dump the key database + * @filenamebase: The base filename to use for the dump. + * + * Dumps the database into one or more files, which contain pure OpenPGP + * that can be reimported into onak or gpg. filenamebase provides a base + * file name for the dump; several files may be created, all of which will + * begin with this string and then have a unique number and a .pgp + * extension. + */ +int dumpdb(char *filenamebase) +{ + DBT key, data; + DBC *cursor = NULL; + int ret = 0; + int fd = -1; + + starttrans(); + + ret = dbconn->cursor(dbconn, + txn, + &cursor, + 0); /* flags */ + + fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640); + memset(&key, 0, sizeof(key)); + memset(&data, 0, sizeof(data)); + ret = cursor->c_get(cursor, &key, &data, DB_NEXT); + while (ret == 0) { + write(fd, data.data, data.size); + memset(&key, 0, sizeof(key)); + memset(&data, 0, sizeof(data)); + ret = cursor->c_get(cursor, &key, &data, DB_NEXT); + } + dbconn->err(dbconn, ret, "Problem reading key"); + + close(fd); + + ret = cursor->c_close(cursor); + cursor = NULL; + + endtrans(); + + return 0; +} + /* * Include the basic keydb routines. */ diff --git a/onak.c b/onak.c index 07e01a5..f673bd3 100644 --- a/onak.c +++ b/onak.c @@ -120,6 +120,10 @@ int main(int argc, char *argv[]) if ((argc - optind) < 1) { usage(); + } else if (!strcmp("dump", argv[optind])) { + initdb(); + dumpdb("keydump"); + cleanupdb(); } else if (!strcmp("add", argv[optind])) { if (binary) { result = read_openpgp_stream(stdin_getchar, NULL,