From: Michael Tremer Date: Fri, 24 Nov 2017 11:58:28 +0000 (+0100) Subject: libpakfire: Make key info dumpable and add access to key properties X-Git-Tag: 0.9.28~1285^2~1289 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=526e4cecc913520a5609834ad4c855e4be26c0ba;p=pakfire.git libpakfire: Make key info dumpable and add access to key properties Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/key.c b/src/_pakfire/key.c index a950ffd3e..165716f87 100644 --- a/src/_pakfire/key.c +++ b/src/_pakfire/key.c @@ -78,7 +78,12 @@ static PyObject* Key_repr(KeyObject* self) { } static PyObject* Key_str(KeyObject* self) { - return NULL; + char* string = pakfire_key_dump(self->key); + + PyObject* object = PyUnicode_FromString(string); + pakfire_free(string); + + return object; } static PyObject* Key_get_fingerprint(KeyObject* self) { diff --git a/src/libpakfire/include/pakfire/key.h b/src/libpakfire/include/pakfire/key.h index 27073cee9..2eae40bfe 100644 --- a/src/libpakfire/include/pakfire/key.h +++ b/src/libpakfire/include/pakfire/key.h @@ -22,6 +22,7 @@ #define PAKFIRE_KEY_H #include +#include #include @@ -37,11 +38,24 @@ PakfireKey pakfire_key_ref(PakfireKey key); void pakfire_key_unref(PakfireKey key); PakfireKey pakfire_key_get(Pakfire pakfire, const char* fingerprint); + +// Access key properties const char* pakfire_key_get_fingerprint(PakfireKey key); +const char* pakfire_key_get_uid(PakfireKey key); +const char* pakfire_key_get_name(PakfireKey key); +const char* pakfire_key_get_email(PakfireKey key); +const char* pakfire_key_get_pubkey_algo(PakfireKey key); +size_t pakfire_key_get_pubkey_length(PakfireKey key); +time_t pakfire_key_get_created(PakfireKey key); +time_t pakfire_key_get_expires(PakfireKey key); +int pakfire_key_is_revoked(PakfireKey key); + PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid); char* pakfire_key_export(PakfireKey key, pakfire_key_export_mode_t mode); PakfireKey* pakfire_key_import(Pakfire pakfire, const char* data); +char* pakfire_key_dump(PakfireKey key); + #ifdef PAKFIRE_PRIVATE struct _PakfireKey { diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index d4ba90bdc..ba21e11a0 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -22,6 +22,7 @@ #define PAKFIRE_UTIL_H #include +#include void pakfire_oom(size_t num, size_t len); @@ -34,6 +35,7 @@ void* pakfire_free(void* mem); char* pakfire_strdup(const char* s); char* pakfire_format_size(double size); +char* pakfire_format_date(time_t t); char* pakfire_path_join(const char* first, const char* second); diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index 7c42a360c..d9b3152c3 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -22,7 +22,9 @@ #include #include +#include #include +#include #include #include #include @@ -164,6 +166,64 @@ const char* pakfire_key_get_fingerprint(PakfireKey key) { return key->gpgkey->fpr; } +const char* pakfire_key_get_uid(PakfireKey key) { + return key->gpgkey->uids->uid; +} + +const char* pakfire_key_get_name(PakfireKey key) { + return key->gpgkey->uids->name; +} + +const char* pakfire_key_get_email(PakfireKey key) { + return key->gpgkey->uids->email; +} + +const char* pakfire_key_get_pubkey_algo(PakfireKey key) { + switch (key->gpgkey->subkeys->pubkey_algo) { + case GPGME_PK_RSA: + case GPGME_PK_RSA_E: + case GPGME_PK_RSA_S: + return "RSA"; + + case GPGME_PK_DSA: + return "DSA"; + + case GPGME_PK_ECDSA: + return "ECDSA"; + + case GPGME_PK_ECDH: + return "ECDH"; + + case GPGME_PK_ECC: + return "ECC"; + + case GPGME_PK_EDDSA: + return "EDDSA"; + + case GPGME_PK_ELG: + case GPGME_PK_ELG_E: + return "ELG"; + } + + return NULL; +} + +size_t pakfire_key_get_pubkey_length(PakfireKey key) { + return key->gpgkey->subkeys->length; +} + +time_t pakfire_key_get_created(PakfireKey key) { + return key->gpgkey->subkeys->timestamp; +} + +time_t pakfire_key_get_expires(PakfireKey key) { + return key->gpgkey->subkeys->expires; +} + +int pakfire_key_is_revoked(PakfireKey key) { + return key->gpgkey->subkeys->revoked; +} + PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid) { gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); assert(gpgctx); @@ -308,3 +368,32 @@ FAIL: return NULL; } + +char* pakfire_key_dump(PakfireKey key) { + char* s = ""; + + time_t created = pakfire_key_get_created(key); + char* date_created = pakfire_format_date(created); + + asprintf(&s, "pub %s%zu/%s %s", + pakfire_key_get_pubkey_algo(key), + pakfire_key_get_pubkey_length(key), + pakfire_key_get_fingerprint(key), + date_created + ); + pakfire_free(date_created); + + const char* uid = pakfire_key_get_uid(key); + if (uid) { + asprintf(&s, "%s\n %s", s, uid); + } + + time_t expires = pakfire_key_get_expires(key); + if (expires) { + char* date_expires = pakfire_format_date(expires); + asprintf(&s, "%s\n %s: %s", s, _("Expires"), date_expires); + pakfire_free(date_expires); + } + + return s; +} diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index bea1d07e7..dbbae8a9d 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -71,11 +71,18 @@ global: # key pakfire_key_create; + pakfire_key_dump; pakfire_key_export; pakfire_key_generate; pakfire_key_get; + pakfire_key_get_email; pakfire_key_get_fingerprint; + pakfire_key_get_name; + pakfire_key_get_pubkey_algo; + pakfire_key_get_pubkey_length; + pakfire_key_get_uid; pakfire_key_import; + pakfire_key_is_revoked; pakfire_key_list; pakfire_key_ref; pakfire_key_unref; diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 87490fb50..62411512b 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -100,6 +101,22 @@ char* pakfire_format_size(double size) { return pakfire_strdup(string); } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +static char* pakfire_strftime(const char* format, time_t t) { + char string[STRING_SIZE]; + struct tm* tm = gmtime(&t); + + strftime(string, sizeof(string), format, tm); + + return pakfire_strdup(string); +} +#pragma GCC diagnostic pop + +char* pakfire_format_date(time_t t) { + return pakfire_strftime("%Y-%m-%d", t); +} + char* pakfire_path_join(const char* first, const char* second) { char* buffer;