[f7040d8] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
[6159c8d] | 4 | use Getopt::Long; |
---|
[f7040d8] | 5 | use Data::Dumper; |
---|
[a26520d] | 6 | use IO::Socket::SSL; |
---|
[f7040d8] | 7 | use XMLRPC; |
---|
| 8 | |
---|
| 9 | use lib '../swig/perl'; |
---|
| 10 | use ABAC; |
---|
| 11 | |
---|
| 12 | use constant { |
---|
| 13 | PORT => 8000, |
---|
| 14 | }; |
---|
| 15 | |
---|
[6159c8d] | 16 | my ($keystore, $cert, $key); |
---|
| 17 | my $port = 8000; |
---|
| 18 | GetOptions( |
---|
| 19 | 'keystore=s' => \$keystore, |
---|
| 20 | 'port=i' => \$port, |
---|
| 21 | 'cert=s' => \$cert, |
---|
| 22 | 'key=s' => \$key, |
---|
| 23 | ) || usage(); |
---|
| 24 | |
---|
| 25 | usage() unless defined $keystore && defined $cert && defined $key; |
---|
| 26 | |
---|
[f7040d8] | 27 | my $ctx = ABAC::Context->new; |
---|
| 28 | $ctx->load_directory($keystore); |
---|
| 29 | |
---|
| 30 | my $server = XMLRPC->new(); |
---|
| 31 | $server->add_method({ |
---|
| 32 | name => 'abac.query', |
---|
| 33 | code => \&abac_query, |
---|
| 34 | signature => [ 'struct struct' ], |
---|
| 35 | }); |
---|
[6159c8d] | 36 | $server->run($port, $cert, $key); |
---|
[f7040d8] | 37 | |
---|
| 38 | sub abac_query { |
---|
| 39 | my ($server, $request) = @_; |
---|
| 40 | |
---|
| 41 | my $peer_cert = $server->{peer_cert}; |
---|
| 42 | my $peer_id = ABAC::SSL_keyid($peer_cert); |
---|
| 43 | |
---|
| 44 | # clone the context so the state remains pure between requests |
---|
| 45 | my $local_ctx = ABAC::Context->new($ctx); |
---|
| 46 | foreach my $cred (@{$request->{credentials}}) { |
---|
| 47 | my $ret = $local_ctx->load_id_chunk($cred->{issuer_cert}); |
---|
| 48 | warn "Invalid issuer certificate" unless $ret == $ABAC::ABAC_CERT_SUCCESS; |
---|
| 49 | |
---|
| 50 | $ret = $local_ctx->load_attribute_chunk($cred->{attribute_cert}); |
---|
| 51 | warn "Invalid attribute certificate" unless $ret == $ABAC::ABAC_CERT_SUCCESS; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | my $role = $request->{role}; |
---|
| 55 | print "$role <- $peer_id\n"; |
---|
| 56 | my ($success, $credentials) = $local_ctx->query($role, $peer_id); |
---|
| 57 | |
---|
[a26520d] | 58 | return { |
---|
| 59 | success => $success, |
---|
| 60 | map {{ |
---|
| 61 | attribute_cert => RPC::XML::base64->new($_->attribute_cert), |
---|
| 62 | issuer_cert => RPC::XML::base64->new($_->issuer_cert), |
---|
| 63 | }} @$credentials, |
---|
| 64 | }; |
---|
[f7040d8] | 65 | } |
---|
[6159c8d] | 66 | |
---|
| 67 | sub usage { |
---|
| 68 | print "Usage: $0 \\\n"; |
---|
| 69 | print " --keystore <keystore> [ --port <port> ] \\\n"; |
---|
| 70 | print " --cert <cert.pem> --key <key.pem>\n"; |
---|
| 71 | print " port defaults to 8000\n"; |
---|
[e9bd4dd] | 72 | print "\n"; |
---|
| 73 | print " cert and key must be an OpenSSL cert and key\n"; |
---|
| 74 | print " ABAC cert and key will not work\n"; |
---|
[6159c8d] | 75 | exit 1; |
---|
| 76 | } |
---|