actions: deleting keys

This commit is contained in:
Mirek Kratochvil 2013-04-21 12:47:20 +02:00
parent a6d0a590fc
commit 02c2ba8f0f
2 changed files with 80 additions and 1 deletions

View file

@ -22,6 +22,8 @@
#include "generator.h"
#include "str_match.h"
#include <list>
int action_gen_key (const std::string& algspec, const std::string&name,
keyring&KR, algorithm_suite&AS)
{
@ -204,12 +206,49 @@ int action_export (bool armor,
const std::string&filter, const std::string&name,
keyring&KR)
{
//TODO
return 0;
}
int action_delete (bool yes, const std::string&filter, keyring&KR)
{
int kc = 0;
for (keyring::pubkey_storage::iterator
i = KR.pubs.begin(), e = KR.pubs.end();
i != e; ++i) {
if (keyspec_matches (filter, i->second.name, i->first) )
++kc;
}
if (!kc) {
err ("no such key");
return 0;
}
if (kc > 1 && !yes) {
bool okay = false;
ask_for_yes (okay, "This will delete " << kc
<< " pubkeys from your keyring. Continue?");
if (!okay) return 0;
}
//all clear, delete them
std::list<keyring::pubkey_storage::iterator> todel;
for (keyring::pubkey_storage::iterator
i = KR.pubs.begin(), e = KR.pubs.end();
i != e; ++i) {
if (keyspec_matches (filter, i->second.name, i->first) )
todel.push_back (i);
}
for (std::list<keyring::pubkey_storage::iterator>::iterator
i = todel.begin(), e = todel.end(); i != e; ++i)
KR.pubs.erase (*i);
if (!KR.save() ) {
err ("error: couldn't save keyring");
return 1;
}
return 0;
}
@ -252,12 +291,49 @@ int action_export_sec (bool armor,
const std::string&filter, const std::string&name,
keyring&KR)
{
//TODO
return 0;
}
int action_delete_sec (bool yes, const std::string&filter, keyring&KR)
{
int kc = 0;
for (keyring::keypair_storage::iterator
i = KR.pairs.begin(), e = KR.pairs.end();
i != e; ++i) {
if (keyspec_matches (filter, i->second.pub.name, i->first) )
++kc;
}
if (!kc) {
err ("no such key");
return 0;
}
if (!yes) {
bool okay = false;
ask_for_yes (okay, "This will delete " << kc
<< " secrets from your keyring. Continue?");
if (!okay) return 0;
}
//all clear, delete them
std::list<keyring::keypair_storage::iterator> todel;
for (keyring::keypair_storage::iterator
i = KR.pairs.begin(), e = KR.pairs.end();
i != e; ++i) {
if (keyspec_matches (filter, i->second.pub.name, i->first) )
todel.push_back (i);
}
for (std::list<keyring::keypair_storage::iterator>::iterator
i = todel.begin(), e = todel.end(); i != e; ++i)
KR.pairs.erase (*i);
if (!KR.save() ) {
err ("error: couldn't save keyring");
return 1;
}
return 0;
}

View file

@ -32,7 +32,10 @@
#define err(x) std::cerr << x << std::endl
#define erreol std::cerr << std::endl
#define progerr(x) std::cerr << argv[0] << ": " << x << std::endl
#define in(x) std::cin >> x;
#define ask_for_yes(ok,x) do {std::cout << x << " (y/n): "; \
std::string answer; std::cin >> answer; \
ok=(answer=="y");} while(0)
bool redirect_cin (const std::string& fn);
bool redirect_cout (const std::string& fn);