#!/usr/bin/perl our $ID = q$Id$; # # wallet-backend -- Wallet server for storing and retrieving secure data. # # Written by Russ Allbery # Copyright 2007 Board of Trustees, Leland Stanford Jr. University # # See README for licensing terms. ############################################################################## # Declarations and site configuration ############################################################################## use strict; use DBI; use Sys::Syslog qw(openlog syslog); use Wallet::Server; ############################################################################## # Parameter checking ############################################################################## # Check all arguments against a very restricted set of allowed characters and # to ensure the right number of arguments are taken. The arguments are the # number of arguments expected (minimum and maximum), a reference to an array # of which argument numbers shouldn't be checked, and then the arguments. # # This function is probably temporary and will be replaced with something that # knows more about the syntax of each command and can check more things. sub check_args { my ($min, $max, $exclude, @args) = @_; if (@args < $min) { die "insufficient arguments\n"; } elsif (@args > $max and $max != -1) { die "too many arguments\n"; } my %exclude = map { $_ => 1 } @$exclude; for (my $i = 1; $i <= @args; $i++) { next if $exclude{$i}; unless ($args[$i - 1] =~ m,^[\w_/.-]+\z,) { die "invalid characters in argument: $args[$i - 1]\n"; } } } ############################################################################## # Implementation ############################################################################## # Parse and execute a command. We wrap this in a subroutine call for easier # testing. sub command { my $user = $ENV{REMOTE_USER} or die "REMOTE_USER not set\n"; my $host = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR} or die "neither REMOTE_HOST nor REMOTE_ADDR set\n"; # Instantiate the server object. my $server = Wallet::Server->new ($user, $host); # Parse command-line options and dispatch to the appropriate calls. my ($command, @args) = @_; if ($command eq 'acl') { my $action = shift @args; if ($action eq 'add') { check_args (3, 3, [], @args); $server->acl_add (@args) or die $server->error; } elsif ($action eq 'create') { check_args (1, 1, [], @args); $server->acl_create (@args) or die $server->error; } elsif ($action eq 'destroy') { check_args (1, 1, [], @args); $server->acl_destroy (@args) or die $server->error; } elsif ($action eq 'remove') { check_args (3, 3, [], @args); $server->acl_remove (@args) or die $server->error; } elsif ($action eq 'rename') { check_args (2, 2, [], @args); $server->acl_rename (@args) or die $server->error; } elsif ($action eq 'show') { check_args (1, 1, [], @args); my $output = $server->acl_show (@args); if (defined $output) { print $output; } else { die $server->error; } } else { die "unknown command acl $action\n"; } } elsif ($command eq 'create') { check_args (2, 2, [], @args); $server->create (@args) or die $server->error; } elsif ($command eq 'destroy') { check_args (2, 2, [], @args); $server->destroy (@args) or die $server->error; } elsif ($command eq 'expires') { check_args (2, 3, [], @args); if (@args > 2) { $server->expires (@args) or die $server->error; } else { my $output = $server->expires (@args); if (defined $output) { print $output, "\n"; } elsif (not $server->error) { print "No expiration set\n"; } else { die $server->error; } } } elsif ($command eq 'flag') { my $action = shift @args; check_args (3, 3, [], @args); if ($action eq 'clear') { $server->flag_clear (@args) or die $server->error; } elsif ($action eq 'set') { $server->flag_set (@args) or die $server->error; } else { die "unknown command flag $action\n"; } } elsif ($command eq 'get') { check_args (2, 2, [], @args); my $output = $server->get (@args); if (defined $output) { print $output; } else { die $server->error; } } elsif ($command eq 'getacl') { check_args (3, 3, [], @args); my $output = $server->acl (@args); if (defined $output) { print $output, "\n"; } elsif (not $server->error) { print "No ACL set\n"; } else { die $server->error; } } elsif ($command eq 'getattr') { check_args (3, 3, [], @args); my @result = $server->attr (@args); if (not @result and $server->error) { die $server->error; } elsif (@result) { print join ("\n", @result, ''); } } elsif ($command eq 'owner') { check_args (2, 3, [], @args); if (@args > 2) { $server->owner (@args) or die $server->error; } else { my $output = $server->owner (@args); if (defined $output) { print $output, "\n"; } elsif (not $server->error) { print "No owner set\n"; } else { die $server->error; } } } elsif ($command eq 'setacl') { check_args (4, 4, [], @args); $server->acl (@args) or die $server->error; } elsif ($command eq 'setattr') { check_args (4, -1, [], @args); $server->attr (@args) or die $server->error; } elsif ($command eq 'show') { check_args (2, 2, [], @args); my $output = $server->show (@args); if (defined $output) { print $output; } else { die $server->error; } } elsif ($command eq 'store') { check_args (3, 3, [3], @args); $server->store (@args) or die $server->error; } else { die "unknown command $command\n"; } } command (@ARGV); __END__ ############################################################################## # Documentation ############################################################################## # The commands section of this document is duplicated from the documentation # for wallet and should be kept in sync. =head1 NAME wallet-backend - Wallet server for storing and retrieving secure data =head1 SYNOPSIS B I [I ...] =head1 DESCRIPTION B implements the interface between B and the wallet system. It is written to run under B and expects the authenticated identity of the remote user in the REMOTE_USER environment variable. It uses REMOTE_HOST or REMOTE_ADDR if REMOTE_HOST isn't set for additional trace information. It accepts the command from B on the command line, creates a Wallet::Server object, and calls the appropriate methods. This program is a fairly thin wrapper around Wallet::Server that translates command strings into method calls and returns the results. It does check all arguments except for the argument to the store command and rejects any argument not matching C<^[\w_/.-]+\z>; in other words, only alphanumerics, underscore (C<_>), slash (C), period (C<.>), and hyphen (C<->) are permitted in arguments. This provides some additional security over and above the checking already done by the rest of the wallet code. =head1 OPTIONS B takes no traditional options. =head1 COMMANDS Most commands are only available to wallet administrators (users on the C ACL). The exceptions are C, C, C, C, C, C, C, and C. All of those commands have their own ACLs except C, which uses the C ACL, and C, which uses the C ACL. If the appropriate ACL is set, it alone is checked to see if the user has access. Otherwise, C, C, C, C, and C access is permitted if the user is authorized by the owner ACL of the object. Administrators can run any command on any object or ACL except for C and C. For C and C, they must still be authorized by either the appropriate specific ACL or the owner ACL. If the locked flag is set on an object, no commands can be run on that object that change data except the C commands, nor can the C command be used on that object. C, C, and C or C without an argument can still be used on that object. For more information on attributes, see L. =over 4 =item acl add Adds an entry with and to the ACL . may be either the name of an ACL or its numeric identifier. =item acl create Create a new, empty ACL with name . When setting an ACL on an object with a set of entries that don't match an existing ACL, first create a new ACL with C, add the appropriate entries to it with C, and then set the ACL on an object with the C or C commands. =item acl destroy Destroy the ACL . This ACL must no longer be referenced by any object or the ACL destruction will fail. The special ACL named C cannot be destroyed. =item acl remove Remove the entry with and from the ACL . may be either the name of an ACL or its numeric identifier. The last entry in the special ACL C cannot be removed to protect against accidental lockout, but administrators can remove themselves from the C ACL and can leave only a non-functioning entry on the ACL. Use caution when removing entries from the C ACL. =item acl show Display the name, numeric ID, and entries of the ACL . =item create Create a new object of type with name . With some backends, this will trigger creation of an entry in an external system as well. The new object will have no ACLs and no owner set, so usually the administrator will want to then set an owner with C so that the object will be usable. =item destroy Destroy the object identified by and . With some backends, this will trigger destruction of an object in an external system as well. =item expires [] If is not given, displays the current expiration of the object identified by and , or C if none is set. The expiration will be displayed in seconds since epoch. If is given, sets the expiration on the object identified by and to . should be given in seconds since epoch. If is the empty string, clears the expiration of the object. Currently, the expiration of an object is not used. =item flag clear Clears the flag on the object identified by and . =item flag set Sets the flag on the object identified by and . Recognized flags are C, which prevents all further actions on that object until the flag is cleared, and C, which tells the object backend to not generate new data on get but instead return the same data as previously returned. The C flag is not meaningful for objects that do not generate new data on the fly. =item get Prints to standard output the data associated with the object identified by and . This may trigger generation of new data and invalidate old data for that object depending on the object type. =item getacl Prints the ACL , which must be one of C, C, C, C, or C, for the object identified by and . Prints C if that ACL isn't set on that object. Remember that if the C, C, or C ACLs aren't set, authorization falls back to checking the owner ACL. See the C command for displaying or setting it. =item getattr Prints the object attribute for the object identified by and . Attributes are used to store backend-specific information for a particular object type, and must be an attribute type known to the underlying object implementation. The attribute values, if any, are printed one per line. If the attribute is not set on this object, nothing is printed. =item owner [] If is not given, displays the current owner ACL of the object identified by and , or C if none is set. The result will be the name of an ACL. If is given, sets the owner of the object identified by and to . If is the empty string, clears the owner of the object. =item setacl Sets the ACL , which must be one of C, C, C, C, or C, to on the object identified by and . If is the empty string, clears that ACL on the object. =item setattr [ ...] Sets the object attribute for the object identified by and . Attributes are used to store backend-specific information for a particular object type, and must be an attribute type known to the underlying object implementation. To clear the attribute for this object, pass in a of the empty string (C<''>). =item show Displays the current object metadata for the object identified by and . This human-readable output will show the object type and name, the owner, any specific ACLs set on the object, the expiration if any, and the user, remote host, and time when the object was created, last stored, and last downloaded. =item store Stores for the object identified by and for later retrieval with C. Not all object types support this. Currently, is limited to not containing nul characters and may therefore not be binary data, and is limited by the maximum command line length of the operating system of the wallet server. These restrictions will be lifted in the future. =back =head1 ATTRIBUTES Object attributes store additional properties and configuration information for objects stored in the wallet. They are displayed as part of the object data with C, retrieved with C, and set with C. =head1 Keytab Attributes Keytab objects support the following attributes: =over 4 =item sync Sets the external systems to which the key of a given principal is synchronized. The only supported value for this attribute is C, which says to synchronize the key with an AFS Kerberos v4 kaserver. If this attribute is set on a keytab, whenever the C command is run for that keytab, the DES key will be extracted from that keytab and set in the configured AFS kaserver. The Kerberos v4 principal name will be the same as the Kerberos v5 principal name except that the components are separated by C<.> instead of C; the second component is truncated after the first C<.> if the first component is one of C, C, C, C, or C; and the first component is C if the Kerberos v5 principal component is C. The principal name must not contain more than two components. If this attribute is set, calling C will also destroy the principal from the AFS kaserver, with a principal mapping determined as above. =back =head1 SEE ALSO Wallet::Server(3), remctld(8) This program is part of the wallet system. The current version is available from L. =head1 AUTHOR Russ Allbery =cut