summaryrefslogtreecommitdiffstats
path: root/base/ra/lib/perl/PKI/Base
diff options
context:
space:
mode:
Diffstat (limited to 'base/ra/lib/perl/PKI/Base')
-rw-r--r--base/ra/lib/perl/PKI/Base/CertStore.pm151
-rwxr-xr-xbase/ra/lib/perl/PKI/Base/Conf.pm130
-rw-r--r--base/ra/lib/perl/PKI/Base/PinStore.pm180
-rw-r--r--base/ra/lib/perl/PKI/Base/Registry.pm55
-rwxr-xr-xbase/ra/lib/perl/PKI/Base/TimeTool.pm54
-rw-r--r--base/ra/lib/perl/PKI/Base/UserStore.pm343
-rwxr-xr-xbase/ra/lib/perl/PKI/Base/Util.pm155
7 files changed, 1068 insertions, 0 deletions
diff --git a/base/ra/lib/perl/PKI/Base/CertStore.pm b/base/ra/lib/perl/PKI/Base/CertStore.pm
new file mode 100644
index 000000000..1a31ff971
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/CertStore.pm
@@ -0,0 +1,151 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::CertStore;
+
+use DBI;
+use PKI::Base::TimeTool;
+
+#######################################
+# Constructs a cert store
+#######################################
+sub new {
+ my $self = {};
+ bless ($self);
+ return $self;
+}
+
+#######################################
+# Opens this store
+#######################################
+sub open {
+ my ($self, $cfg) = @_;
+ $self->{cfg} = $cfg;
+ my $dbfile = $cfg->get("database.dbfile");
+ $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
+}
+
+sub read_certificate {
+ my ($self, $serialno) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from certificates " .
+ "where serialno=" . $dbh->quote($serialno);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+sub map_certificate {
+ my ($self, $certificate) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from certificates " .
+ "where " .
+ "certificate=" . $dbh->quote($certificate);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+sub read_certificate_by_approver {
+ my ($self, $uid, $serialno) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from certificates " .
+ "where approved_by=". $dbh->quote($uid).
+ "AND serialno=" . $dbh->quote($serialno);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+sub list_certs_by_approver {
+ my ($self, $uid, $startpos, $maxcount) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select *,approved_by from certificates " .
+ "where " .
+ "approved_by=". $dbh->quote($uid).
+ " limit $startpos, $maxcount";
+
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my @certs;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@certs, $ref);
+ }
+ $sth->finish();
+ return @certs;
+
+
+}
+
+sub add_certificate {
+ my ($self, $serialno, $csr, $subject_dn, $certificate, $reqid, $approved_by) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ # sqlite is not thread safe, do our own lock here
+ my $cmd = "insert into certificates (" .
+ "subject_dn" . "," .
+ "certificate" . "," .
+ "csr" . "," .
+ "serialno" . "," .
+ "rid" . "," .
+ "approved_by" . "," .
+ "created_at" .
+ ") values (" .
+ $dbh->quote($subject_dn) . "," .
+ $dbh->quote($certificate) . "," .
+ $dbh->quote($csr) . "," .
+ $dbh->quote($serialno) . "," .
+ $dbh->quote($reqid) . "," .
+ $dbh->quote($approved_by) . "," .
+ $dbh->quote($now) .
+ ")";
+REDO_ADD_CERT:
+ eval {
+ $dbh->do($cmd);
+ };
+ if ($dbh->err == 5) {
+ sleep(1);
+ goto REDO_ADD_CERT;
+ }
+
+}
+
+#######################################
+# Closes this store
+#######################################
+sub close {
+ my ($self) = @_;
+ my $dbh = $self->{dbh};
+ $dbh->disconnect();
+}
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/Conf.pm b/base/ra/lib/perl/PKI/Base/Conf.pm
new file mode 100755
index 000000000..895ab28a3
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/Conf.pm
@@ -0,0 +1,130 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+
+package PKI::Base::Conf;
+
+use strict;
+use warnings;
+use Exporter;
+
+$PKI::Base::Conf::VERSION = '1.00';
+
+#######################################################
+# Configuration Store
+#######################################################
+sub new {
+ my $class = shift;
+ my $self = {};
+ my %hash = ();
+ $self->{filename} = "";
+ $self->{hash} = \%hash;
+ bless $self,$class;
+ return $self;
+}
+
+sub load_file
+{
+ my ($self, $filename) = @_;
+
+ $self->{filename} = $filename;
+ if (-e $filename) {
+ open(CF, "<$filename");
+ if (defined fileno CF) {
+ while (<CF>) {
+ if (/^#/) {
+ # comments
+ } elsif (/([^=]+)=(.*)$/) {
+ # print "$1 = $2\n";
+ $self->{hash}{$1} = $2;
+ } else {
+ # preserve comments
+ }
+ }
+ }
+ close(CF);
+ }
+}
+
+sub get_filename
+{
+ my ($self) = @_;
+ return $self->{filename};
+}
+
+sub get
+{
+ my ($self, $n) = @_;
+ return $self->{hash}{$n};
+}
+
+sub put
+{
+ my ($self, $n, $v) = @_;
+ $self->{hash}{$n} = $v;
+}
+
+sub commit
+{
+ my ($self) = @_;
+
+ # write stuff back to the file
+# print $self->{filename} . "\n";
+ my $hash = $self->{hash};
+ my $suffix = time();
+
+ if (-e $self->{filename}) {
+ system("mv \"" . $self->{filename} . "\" \"" .
+ $self->{filename} . "." . $suffix . "\"");
+ }
+
+ open(F, ">" . $self->{filename});
+ foreach my $k (sort keys %{$hash}) {
+ print F "$k=$self->{hash}{$k}\n";
+ }
+ close(F);
+
+ if (-e $self->{filename} . "." . $suffix) {
+ system("rm \"" . $self->{filename} . "." . $suffix . "\"");
+ }
+}
+
+sub commit_with_backup
+{
+ my ($self) = @_;
+
+ # write stuff back to the file
+# print $self->{filename} . "\n";
+ my $hash = $self->{hash};
+ my $suffix = time();
+ system("mv \"" . $self->{filename} . "\" \"" .
+ $self->{filename} . "." . $suffix . "\"");
+
+ open(F, ">" . $self->{filename});
+ foreach my $k (sort keys %{$hash}) {
+ print F "$k=$self->{hash}{$k}\n";
+ }
+ close(F);
+}
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/PinStore.pm b/base/ra/lib/perl/PKI/Base/PinStore.pm
new file mode 100644
index 000000000..437d259ff
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/PinStore.pm
@@ -0,0 +1,180 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::PinStore;
+
+use DBI;
+use PKI::Base::TimeTool;
+
+#######################################
+# Constructs a request queue
+#######################################
+sub new {
+ my $self = {};
+ bless ($self);
+ return $self;
+}
+
+#######################################
+# Opens request queue
+#######################################
+sub open {
+ my ($self, $cfg) = @_;
+ $self->{cfg} = $cfg;
+ my $dbfile = $cfg->get("database.dbfile");
+ $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
+}
+
+#######################################
+# Creates a new request
+#######################################
+sub generate_random
+{
+ my $low = $_[0];
+ my $high = $_[1];
+
+ my $number = 0;
+
+ if( $low >= $high || $low < 0 || $high < 0 ) {
+ return -1;
+ }
+
+ $number = int( rand( $high -$low +1 ) ) + $low;
+
+ return $number;
+}
+
+
+# arg0 length of string
+# return random string
+sub generate_random_string()
+{
+ my $length_of_randomstring=shift; # the length of the string
+
+ my @chars=( 'a'..'z','A'..'Z','0'..'9' );
+ my $random_string;
+
+ foreach( 1..$length_of_randomstring ) {
+ $random_string .= $chars[rand @chars];
+ }
+
+ return $random_string;
+}
+
+sub create_pin {
+ my ($self, $key, $rid, $created_by) = @_;
+ my $dbh = $self->{dbh};
+
+ my $pin = &generate_random_string(10);
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ # delete previous pin
+ my $delete = "delete from pins where key=" . $dbh->quote($key);
+ $dbh->do($delete);
+
+ my $insert = "insert into pins (" .
+ "key" . "," .
+ "pin" . "," .
+ "rid" . "," .
+ "created_by" . "," .
+ "created_at" .
+ ") values (" .
+ $dbh->quote($key) . "," .
+ $dbh->quote($pin) . "," .
+ $dbh->quote($rid) . "," .
+ $dbh->quote($created_by) . "," .
+ $dbh->quote($now) .
+ ")";
+REDO_CREATE_PIN:
+ eval {
+ $dbh->do($insert);
+ };
+ if ($dbh->err == 5) {
+ sleep(1);
+ goto REDO_CREATE_PIN;
+ }
+
+ my $rid = $dbh->func('last_insert_rowid');
+
+# my $ref = $self->read_pin($rid);
+
+ return $pin;
+}
+
+#######################################
+# Matches pin
+#######################################
+sub match {
+ my ($self, $key, $pin) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from pins " .
+ "where " .
+ "key=" . $dbh->quote($key) . " AND " .
+ "pin=" . $dbh->quote($pin);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ if (defined($ref)) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+sub read_pin {
+ my ($self, $key) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from pins " .
+ "where " .
+ "key=" . $dbh->quote($key);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+#######################################
+# Deletes pin
+#######################################
+sub delete {
+ my ($self, $key) = @_;
+ my $dbh = $self->{dbh};
+ my $cmd = "delete from pins " .
+ "where " .
+ "key=" . $dbh->quote($key);
+ $dbh->do($cmd);
+}
+
+#######################################
+# Closes request queue
+#######################################
+sub close {
+ my ($self) = @_;
+ my $dbh = $self->{dbh};
+ $dbh->disconnect();
+}
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/Registry.pm b/base/ra/lib/perl/PKI/Base/Registry.pm
new file mode 100644
index 000000000..a4fb83f28
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/Registry.pm
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::Registry;
+
+use PKI::Base::Conf;
+
+my $docroot;
+my $cfg;
+my $parser;
+
+BEGIN {
+ $docroot = $ENV{DOCUMENT_ROOT};
+ $cfg = PKI::Base::Conf->new();
+ $cfg->load_file("$docroot/../conf/CS.cfg");
+ $parser = new Template::Velocity($docroot);
+
+}
+
+sub get_docroot {
+ my ($self) = @_;
+ return $docroot;
+}
+
+sub get_parser {
+ my ($self) = @_;
+ return $parser;
+}
+
+sub get_config {
+ my ($self) = @_;
+ return $cfg;
+}
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/TimeTool.pm b/base/ra/lib/perl/PKI/Base/TimeTool.pm
new file mode 100755
index 000000000..11f4be208
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/TimeTool.pm
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::TimeTool;
+
+use Time::Local;
+
+use DBI;
+use PKI::Base::TimeTool;
+
+#######################################
+# Constructs a request queue
+#######################################
+sub new {
+ my $self = {};
+ bless ($self);
+ return $self;
+}
+
+sub get_time()
+{
+ my ($self) = @_;
+ my ($sec, $min, $hr, $mday, $mnth, $y, $wd, $yd, $ds) = localtime();
+ my $r_year = 1900 + $y;
+ my $r_mnth;
+ my $r_day;
+ $r_day = $mday;
+ $mnth = $mnth + 1;
+ $r_mnth = $mnth;
+ return "$r_year-$r_mnth-$r_day $hr:$min:$sec";
+}
+
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/UserStore.pm b/base/ra/lib/perl/PKI/Base/UserStore.pm
new file mode 100644
index 000000000..c05683792
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/UserStore.pm
@@ -0,0 +1,343 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::UserStore;
+
+use DBI;
+use PKI::Base::TimeTool;
+
+#######################################
+# Constructs a request queue
+#######################################
+sub new {
+ my $self = {};
+ bless ($self);
+ return $self;
+}
+
+#######################################
+# Opens this store
+#######################################
+sub open {
+ my ($self, $cfg) = @_;
+ $self->{cfg} = $cfg;
+ my $dbfile = $cfg->get("database.dbfile");
+ $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
+ my $timeout = $self->{dbh}->func("busy_timeout");
+ $self->{dbh}->func($timeout * 10, "busy_timeout");
+}
+
+#######################################
+# Maps user
+#######################################
+sub map_user {
+ my ($self, $certificate) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from users " .
+ "where " .
+ "certificate=" . $dbh->quote($certificate);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+#######################################
+# Gets roles of the given user
+#######################################
+sub get_roles {
+ my ($self, $uid) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from roles " .
+ "where " .
+ "uid=" . $dbh->quote($uid);
+ my @roles;
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@roles, $ref->{'gid'});
+ }
+ $sth->finish();
+ return @roles;
+}
+
+
+#######################################
+# Reads a user
+#######################################
+sub read_group {
+ my ($self, $gid) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from groups " .
+ "where gid=" . $dbh->quote($gid);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+sub read_user {
+ my ($self, $uid) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from users " .
+ "where uid=" . $dbh->quote($uid);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ return $ref;
+}
+
+sub set_user {
+ my ($self, $uid, $name, $value) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+ my $update = "update users set " .
+ $name . "=" . $dbh->quote($value) . "," .
+ "updated_at=" . $dbh->quote($now) . " " .
+ "where uid=" . $dbh->quote($uid);
+ $dbh->do($update);
+
+ my $select = "select * from users " .
+ "where uid=" . $dbh->quote($uid);
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+
+ return $ref;
+}
+
+#######################################
+# Lists all members in the given group
+#######################################
+sub list_all_members {
+ my ($self, $gid) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from roles where " .
+ "gid=" . $dbh->quote($gid) . " " .
+ "order by uid desc ";
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my @reqs;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@reqs, $ref);
+ }
+ $sth->finish();
+ return @reqs;
+}
+
+#######################################
+# Lists
+#######################################
+sub list_all_non_members {
+ my ($self, $gid) = @_;
+ my $dbh = $self->{dbh};
+ # find members of the given group
+ my $select1 = "select * from roles where " .
+ "gid=" . $dbh->quote($gid);
+ my $sth1 = $dbh->prepare($select1);
+ $sth1->execute();
+ my $filter = "";
+ while (my $ref1 = $sth1->fetchrow_hashref()) {
+ if ($filter eq "") {
+ $filter = "uid<>" . $dbh->quote($ref1->{'uid'});
+ } else {
+ $filter = $filter . " AND " . "uid<>" . $dbh->quote($ref1->{'uid'});
+ }
+ }
+ $sth1->finish();
+
+ my $select;
+ if ($filter eq "") {
+ $select = "select * from users " .
+ "order by uid desc ";
+ } else {
+ $select = "select * from users where (" .
+ $filter . ") " .
+ "order by uid desc ";
+ }
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my @reqs;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@reqs, $ref);
+ }
+ $sth->finish();
+ return @reqs;
+}
+
+sub delete_user {
+ my ($self, $userid) = @_;
+ my $dbh = $self->{dbh};
+
+ my $cmd = "delete from roles where uid=" . $dbh->quote($userid);
+ $dbh->do($cmd);
+ $cmd = "delete from users where uid=" . $dbh->quote($userid);
+ $dbh->do($cmd);
+}
+
+sub add_user_to_group {
+ my ($self, $gid, $userid) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ my $cmd = "insert into roles (" .
+ "gid" . "," .
+ "uid" .
+ ") values (" .
+ $dbh->quote($gid) . "," .
+ $dbh->quote($userid) .
+ ")";
+ $dbh->do($cmd);
+}
+
+sub delete_user_from_group {
+ my ($self, $gid, $userid) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ my $cmd = "delete from roles where " .
+ "gid=" . $dbh->quote($gid) . " AND " .
+ "uid=" . $dbh->quote($userid);
+ $dbh->do($cmd);
+}
+
+sub add_user {
+ my ($self, $userid, $name, $email, $certificate) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ my $cmd = "insert into users (" .
+ "uid" . "," .
+ "name" . "," .
+ "email" . "," .
+ "certificate" . "," .
+ "created_at" .
+ ") values (" .
+ $dbh->quote($userid) . "," .
+ $dbh->quote($name) . "," .
+ $dbh->quote($email) . "," .
+ $dbh->quote($certificate) . "," .
+ $dbh->quote($now) .
+ ")";
+REDO_ADD_USER:
+ eval {
+ $dbh->do($cmd);
+ };
+ if ($dbh->err == 5) {
+ sleep(1);
+ goto REDO_ADD_USER;
+ }
+}
+
+sub add_group {
+ my ($self, $gid, $name) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ my $cmd = "insert into groups (" .
+ "gid" . "," .
+ "name" . "," .
+ "created_at" .
+ ") values (" .
+ $dbh->quote($gid) . "," .
+ $dbh->quote($name) . "," .
+ $dbh->quote($now) .
+ ")";
+REDO_ADD_GROUP:
+ eval {
+ $dbh->do($cmd);
+ };
+ if ($dbh->err == 5) {
+ sleep(1);
+ goto REDO_ADD_GROUP;
+ }
+}
+
+sub delete_group {
+ my ($self, $gid) = @_;
+ my $dbh = $self->{dbh};
+
+ my $timet = PKI::Base::TimeTool->new();
+ my $now = $timet->get_time();
+
+ my $cmd = "delete from roles where gid=" . $dbh->quote($gid);
+ $dbh->do($cmd);
+ $cmd = "delete from groups where gid=" . $dbh->quote($gid);
+ $dbh->do($cmd);
+}
+
+sub list_users {
+ my ($self, $startpos, $maxcount) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from users " .
+ "order by uid desc " .
+ "limit $startpos, $maxcount";
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my @reqs;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@reqs, $ref);
+ }
+ $sth->finish();
+ return @reqs;
+}
+
+sub list_groups {
+ my ($self, $startpos, $maxcount) = @_;
+ my $dbh = $self->{dbh};
+ my $select = "select * from groups " .
+ "order by gid desc " .
+ "limit $startpos, $maxcount";
+ my $sth = $dbh->prepare($select);
+ $sth->execute();
+ my @reqs;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@reqs, $ref);
+ }
+ $sth->finish();
+ return @reqs;
+}
+#######################################
+# Closes this store
+#######################################
+sub close {
+ my ($self) = @_;
+ my $dbh = $self->{dbh};
+ $dbh->disconnect();
+}
+
+1;
diff --git a/base/ra/lib/perl/PKI/Base/Util.pm b/base/ra/lib/perl/PKI/Base/Util.pm
new file mode 100755
index 000000000..f01062e42
--- /dev/null
+++ b/base/ra/lib/perl/PKI/Base/Util.pm
@@ -0,0 +1,155 @@
+#!/usr/bin/perl
+#
+# --- BEGIN COPYRIGHT BLOCK ---
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2007 Red Hat, Inc.
+# All rights reserved.
+# --- END COPYRIGHT BLOCK ---
+#
+#
+#
+#
+package PKI::Base::Util;
+
+use Time::Local;
+
+use DBI;
+use HTML::Entities;
+
+#######################################
+# Constructs a util
+#######################################
+sub new {
+ my $self = {};
+ bless ($self);
+ return $self;
+}
+
+sub get_val()
+{
+ my ($self, $s) = @_;
+ return $s;
+}
+
+sub get_integer_val()
+{
+ my ($self, $s) = @_;
+ return $s;
+}
+
+sub get_string_val()
+{
+ my ($self, $s) = @_;
+ return $s;
+}
+
+sub get_alphanum_val()
+{
+ my ($self, $s) = @_;
+ $s =~ s/[^A-Za-z0-9 ]*//g;
+ return $s;
+}
+
+sub normalize_csr()
+{
+ my ($self, $s) = @_;
+ $s =~ s/-----BEGIN CERTIFICATE REQUEST-----//g;
+ $s =~ s/-----END CERTIFICATE REQUEST-----//g;
+ $s =~ s/-----BEGIN NEW CERTIFICATE REQUEST-----//g;
+ $s =~ s/-----END NEW CERTIFICATE REQUEST-----//g;
+ $s =~ s/\s//g;
+ return $s;
+}
+
+sub breakline()
+{
+ my ($self, $s, $maxlen) = @_;
+
+ my $new_s;
+ my $i = 0;
+ foreach my $c (split(//, $s)) {
+ if ($i == $maxlen) {
+ $i = 0;
+ $new_s = $new_s . "<br/>";
+ }
+ $new_s = $new_s . $c;
+ $i++;
+ }
+ return $new_s;
+}
+
+sub nv_to_hash()
+{
+ my ($self, $s) = @_;
+ my %hash;
+ my @pairs = split(/;/, $s);
+ foreach $pair (@pairs) {
+ my $i = index('=', $pair);
+ my $n = substr($pair, 0, $i-1);
+ my $v = substr($pair, $i);
+ $hash{$n} = $v;
+ }
+ return \%hash;
+}
+
+sub nv_to_str()
+{
+ my ($self, $hash) = @_;
+ my $s = "";
+ foreach $k (keys %$hash) {
+ if ($s eq "") {
+ $s = $k . "=" . $$hash{$k};
+ } else {
+ $s = $s . ";" . $k . "=" . $$hash{$k};
+ }
+ }
+ return $s;
+}
+
+sub test()
+{
+ my %h;
+ $h{'x'} = 'y';
+ $h{'z'} = 'y';
+ my $o = PKI::Base::NameValueUtil->new();
+ print $o->to_str(\%h) . "\n";
+ print $o->to_str($o->to_hash("5=1;c=2")) . "\n";
+}
+
+sub html_encode()
+{
+ my ($self, $s) = @_;
+ return HTML::Entities::encode($s);
+}
+
+sub html_encode_and_break()
+{
+ my ($self, $s, $maxlen) = @_;
+ my $new_s = '';
+ my $i = 0;
+ foreach my $c (split(//, $s)) {
+ if ($i == $maxlen) {
+ $i = 0;
+ $new_s = $new_s . '***';
+ }
+ $new_s = $new_s . $c;
+ $i++;
+ }
+ $s = HTML::Entities::encode($new_s);
+ $s =~ s/\*\*\*/<br\/>/g;
+ return $s;
+}
+
+1;