#!/usr/bin/perl
#
# tokens-kafs -- Display and manager rxrpc keys
#
# Written by Bill MacAllister <bill@ca-zephyr.org>
# Copyright (c) 2023 Bill MacAllister <bill@ca-zephyr.org>

use strict;

my $action = 'show';
if (scalar(@ARGV) > 0) {
    $action = $ARGV[0];
}

if ($action eq 'help') {
    print("Usage: tokens-kafs [show|unlink|help]\n");
    exit;
}
if ($action !~ /show|unlink/xms) {
    print("ERROR: unknown action $action\n");
    exit 1;
}

my $out = `keyctl show`;
my @out_lines = split(/\n/, $out);

my $cnt = 0;
for my $a_line (@out_lines) {
    if ($a_line =~ /(\d+)\s+
                   \-\-als\-rv\s+
                   \d+\s+ \d+\s+
                   \\_\s+ rxrpc:\s+ (.*)/xms) {
	my $sid = $1;
	my $txt = $2;
	if ($action eq 'show') {
	    print("$sid $txt\n");
	} elsif ($action eq 'unlink') {
	    my $unlink_out = `keyctl unlink $sid`;
	} else {
	    print("ERROR: Unknown action $action\n");
	    exit 1;
	}
	$cnt++
    }
}

if ($cnt == 0) {
    print("No rxrpc keys found\n");
}

exit;


__END__

=head1 NAME

tokens-kafs - manage rxrpc keys

=head1 SYNOPSIS

tokens-kafs [show|unlink|help]

=head1 DESCRIPTION

This script is a simple wrapper around keyctl that displays any rxrpc
entries in the keyring or unlinks, i.e. removes, the entries from the
keyring.

=head1 ARGUMENTS

=over 4

=item show

Show any rxrpc entries in the keyring.

=item unlink

Remove all rxrpc entries from the keyring.

=item help

Display a short help message.

=back

=head1 AUTHOR

Bill MacAllister <bill@ca-zephyr.org>

=head1 COPYRIGHT

Copyright (C) 2022 Bill MacAllister <bill@ca-zephyr.org>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=cut
