1#!/usr/bin/perl
2
3# Copyright (c) 2015 Yubico AB
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10#     * Redistributions of source code must retain the above copyright
11#       notice, this list of conditions and the following disclaimer.
12#
13#     * Redistributions in binary form must reproduce the above
14#       copyright notice, this list of conditions and the following
15#       disclaimer in the documentation and/or other materials provided
16#       with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30use IO::Socket::INET;
31
32use strict;
33use warnings;
34
35my %otps = (
36  'vvincredibletrerdegkkrkkneieultcjdghrejjbckh' => 'OK',
37  'vvincrediblltrerdegkkrkkneieultcjdghrejjbckh' => 'OK',
38  'ccccccbchvthlivuitriujjifivbvtrjkjfirllluurj' => 'OK',
39);
40
41my $port = shift;
42die "no port specified" unless $port;
43
44my $socket = new IO::Socket::INET (
45  LocalHost => '127.0.0.1',
46  LocalPort => $port,
47  Proto => 'tcp',
48  Listen => 10,
49  Reuse => 1
50) or die "Oops: $! \n";
51
52warn "YKVAL mockup started on $port";
53
54while(1) {
55  my $clientsocket = $socket->accept();
56  my $clientdata = <$clientsocket>;
57  my $ret = "h=ZrU7UfjwazJVf5ay1P/oC3XCQlI=\n";
58
59  if($clientdata =~ m/nonce=([a-zA-Z0-9]+).*otp=([cbdefghijklnrtuv]+)/) {
60    my $nonce = $1;
61    my $otp = $2;
62    warn "validation for $otp (on port $port)";
63    if($otps{$otp}) {
64      my $status = $otps{$otp};
65      $ret .= "nonce=$nonce\n";
66      $ret .= "otp=$otp\n";
67      $ret .= "status=$status";
68    } else {
69      $ret .= "status=BAD_OTP";
70    }
71  } else {
72    $ret .= "status=MISSING_PARAMETER";
73  }
74  print $clientsocket "\n$ret\n";
75  close $clientsocket;
76}
77