#!/usr/bin/perl
# Stefan Tomanek <stefan@pico.ruhr.de>
##
# pcf2vpnc.pl <pcf file> <vpnc config>
##
# 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; either version 2 of the License, or
# (at your option) any later version.
#             
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use IO::File;
use LWP::Simple;
use CGI;
use strict;
use warnings;

sub decodePW($$) {
    my ($encPW, $type) = @_;
    
    my $confirm;
    
    print STDERR "To decrypt the $type password, it will be sent to the website\n";
    print STDERR "'http://www.unix-ag.uni-kl.de/~massar/bin/cisco-decode'. If\n";
    print STDERR "you are absolutely sure that you want to do this, enter 'yes',\n";
    print STDERR "otherwise just enter anything else.\n";
    print STDERR " > ";
    read STDIN, $confirm, 3;
    
    unless ($confirm eq "yes") {
	print STDERR "Password decryption has been omitted!";
	return "";
    }
    
    print STDERR "Decrypting password...";
    
    my $q = new CGI;
    my $result = get("http://www.unix-ag.uni-kl.de/~massar/bin/cisco-decode?enc=".$encPW);
    
    if ($result =~ /clear: (.*?)\n/) {
	print STDERR "done.\n";
	return $q->unescapeHTML($1);
    } else {
	print STDERR "Error!\n";
	return ""
    }
}

sub readPCF($) {
    my ($file) = @_;
    my %config;
    while (<$file>) {
	if (/^(.*?)=(.*?)$/) {
	    # We don't need emtpy config strings
	    next if ($2 eq "");
	    if ($1 eq "Host") {
		$config{IPSec}{gateway} = $2;
	    } elsif ($1 eq "GroupName") {
		$config{IPSec}{ID} = $2;
	    } elsif ($1 eq "enc_GroupPwd") {
		$config{IPSec}{secret} = decodePW($2, "group");
	    } elsif ($1 eq "Username") {
		$config{Xauth}{username} = $2;
	    } elsif ($1 eq "UserPassword") {
		$config{Xauth}{password} = $2;
	    } elsif ($1 eq "enc_UserPassword") {
		$config{Xauth}{password} = decodePW($2, "user");
	    } elsif ($1 eq "NTDomain") {
		$config{Domain}{""} = $2;
	    }
	}
    }
    return \%config;
}

sub writeVPNC($) {
    my ($config) = @_;
    my $text = "## generated by pcf2vpnc.pl\n## Stefan Tomanek <stefan\@pico.ruhr.de>\n";
    foreach my $section (keys %$config) {
	foreach my $item (keys %{ $config->{$section} }) {
	    $text .= "$section $item ".$config->{$section}{$item}."\n";
	}
    }
    unless (defined $config->{Xauth}) {
	$text .= "\n## To add your username and password,\n";
	$text .= "## use the following lines:\n";
	$text .= "# Xauth username <your username>\n";
	$text .= "# Xauth password <your password>\n";
    }
    return $text;
}

if (defined $ARGV[0]) {
    my $src = new IO::File($ARGV[0]) || die "Unable to open file ".$ARGV[0]."\n";
    if (defined $ARGV[1]) {
	my $dst = new IO::File($ARGV[1], "w") || die "Unable to open file ".$ARGV[1]."\n";
	$dst->write( writeVPNC(readPCF($src)) );
	$dst->close();
	print STDERR "Vpnc config written to ".$ARGV[1]."\n";
    }
    print writeVPNC(readPCF($src));
    $src->close();
} else {
    print STDERR "Usage: pcf2vpnc.pl <pcf file>\n";
}

