summaryrefslogtreecommitdiffstats
path: root/pki/base/ra/lib/perl/PKI/Base/PinStore.pm
blob: 437d259ffcec7398cf5fac0df90861d90e774617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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;