summaryrefslogtreecommitdiff
path: root/keyctl.c
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2013-09-10 16:20:04 +0100
committerDavid Howells <dhowells@redhat.com>2013-10-02 15:58:10 +0100
commit16a6d435b48b168d3e23673437d488e78deb91fc (patch)
tree235f3f50b3bb31b9eba3022efc776e78a9fcbf54 /keyctl.c
parent949159b53e688f6daa0e10dd50058a2dd929234d (diff)
downloadkeyutils-16a6d435b48b168d3e23673437d488e78deb91fc.tar.gz
keyctl: Make it possible to specify a key by name rather than by number
Permit callers of the keyctl program to specify a key by its type and name rather than by its number. This is done by replacing a key ID like: 1234 or: @s with a type and name: %<type>:<name> e.g.: %user:a As a shorthand, leaving out the type name: %:<name> e.g.: %:_ses refers to a keyring of the given name. Signed-off-by: David Howells <dhowells@redhat.com>
Diffstat (limited to 'keyctl.c')
-rw-r--r--keyctl.c38
1 files changed, 36 insertions, 2 deletions
diff --git a/keyctl.c b/keyctl.c
index a137e08..e403ead 100644
--- a/keyctl.c
+++ b/keyctl.c
@@ -111,7 +111,7 @@ const struct command commands[] = {
static int dump_key_tree(key_serial_t keyring, const char *name, int hex_key_IDs);
static void format(void) __attribute__((noreturn));
static void error(const char *msg) __attribute__((noreturn));
-static key_serial_t get_key_id(const char *arg);
+static key_serial_t get_key_id(char *arg);
static uid_t myuid;
static gid_t mygid, *mygroups;
@@ -1577,7 +1577,7 @@ static int act_keyctl_invalidate(int argc, char *argv[])
/*
* parse a key identifier
*/
-static key_serial_t get_key_id(const char *arg)
+static key_serial_t get_key_id(char *arg)
{
key_serial_t id;
char *end;
@@ -1596,6 +1596,36 @@ static key_serial_t get_key_id(const char *arg)
exit(2);
}
+ /* handle a lookup-by-name request "%<type>:<desc>", eg: "%keyring:_ses" */
+ if (arg[0] == '%') {
+ char *type;
+
+ arg++;
+ if (!*arg)
+ goto incorrect_key_by_name_spec;
+
+ if (*arg == ':') {
+ type = "keyring";
+ arg++;
+ } else {
+ type = arg;
+ arg = strchr(arg, ':');
+ if (!arg)
+ goto incorrect_key_by_name_spec;
+ *(arg++) = '\0';
+ }
+
+ if (!*arg)
+ goto incorrect_key_by_name_spec;
+
+ id = find_key_by_type_and_desc(type, arg, 0);
+ if (id == -1) {
+ fprintf(stderr, "Can't find '%s:%s'\n", type, arg);
+ exit(1);
+ }
+ return id;
+ }
+
/* handle a numeric key ID */
id = strtoul(arg, &end, 0);
if (*end) {
@@ -1605,6 +1635,10 @@ static key_serial_t get_key_id(const char *arg)
return id;
+incorrect_key_by_name_spec:
+ fprintf(stderr, "Incorrect key-by-name spec\n");
+ exit(2);
+
} /* end get_key_id() */
/*****************************************************************************/