Stefan Seyfried <seife+obs@b1-systems.com>
[vpnc.git] / pcf2vpnc
blob40da8a4daf80c336e1a809b378792acd72b14771
1 #!/usr/bin/env perl
2 # Stefan Tomanek <stefan@pico.ruhr.de>
3 # updated by Wolfram Sang <ninja@the-dreams.de> on 21.10.06 and on 26.06.07
4 ##
5 # pcf2vpnc <pcf file> [vpnc file]
6 ##
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # $Id$
23 use IO::File;
24 use strict;
25 use warnings;
27 my %authmode = ( 1 => 'psk', 3 => 'cert', 5 => 'hybrid' );
28 my $needs_cert = 0;
29 my $no_decrypt = 0;
30 if (system("cisco-decrypt", "q") == -1) {
31 $no_decrypt = 1;
32 print STDERR "cisco-decrypt not in search path,\n";
33 print STDERR " adding passwords in obfuscated form\n";
36 sub readPCF($) {
37 my ($file) = @_;
38 my %config;
39 while (<$file>) {
40 # Filter unnecessary chars at beginning & end of line
41 s/^!*//; s/[\r ]*$//;
42 if (/^(.*?)=(.*?)$/) {
43 # We don't need emtpy config strings
44 next if ($2 eq "");
45 if ($1 eq "Host") {
46 $config{IPSec}{gateway} = $2;
47 } elsif ($1 eq "GroupName") {
48 $config{IPSec}{ID} = $2;
49 } elsif ($1 eq "GroupPwd") {
50 $config{IPSec}{secret} = $2;
51 } elsif ($1 eq "enc_GroupPwd") {
52 if ($no_decrypt) {
53 $config{IPSec}{obfuscated} = "secret $2";
54 } else {
55 $config{IPSec}{secret} = `cisco-decrypt $2`;
57 } elsif ($1 eq "AuthType") {
58 $config{IKE}{Authmode} = $authmode{$2};
59 if ($2 == 3 || $2 == 5) {
60 $needs_cert = 1;
62 } elsif ($1 eq "DHGroup") {
63 $config{IKE}{DH} = "Group dh$2";
64 } elsif ($1 eq "Username") {
65 $config{Xauth}{username} = $2;
66 } elsif ($1 eq "UserPassword") {
67 $config{Xauth}{password} = $2;
68 } elsif ($1 eq "enc_UserPassword") {
69 if ($no_decrypt) {
70 $config{Xauth}{obfuscated} = "password $2";
71 } else {
72 $config{Xauth}{password} = `cisco-decrypt $2`;
74 } elsif ($1 eq "NTDomain") {
75 $config{Domain}{""} = $2;
79 return \%config;
82 sub writeVPNC($) {
83 my ($config) = @_;
84 my $text = "## generated by pcf2vpnc\n";
85 foreach my $section (keys %$config) {
86 foreach my $item (keys %{ $config->{$section} }) {
87 $text .= "$section ".($item ? "$item " : '').$config->{$section}{$item}."\n";
90 unless (defined $config->{Xauth}) {
91 $text .= "\n## To add your username and password,\n";
92 $text .= "## use the following lines:\n";
93 $text .= "# Xauth username <your username>\n";
94 $text .= "# Xauth password <your password>\n";
96 return $text;
99 if (defined $ARGV[0]) {
100 my $src = new IO::File($ARGV[0]) || die "Unable to open file ".$ARGV[0]."\n";
101 if (defined $ARGV[1]) {
102 my $dst = new IO::File($ARGV[1], "w") || die "Unable to open file ".$ARGV[1]."\n";
103 $dst->write( writeVPNC(readPCF($src)) ) || die "Unable to write to file ".$ARGV[1]."\n";
104 $dst->close() || die "Unable to close file ".$ARGV[1]."\n";
105 printf STDERR "vpnc config written to '%s' with permissions '%04o'.\n", $ARGV[1], (stat($ARGV[1]))[2];
106 print STDERR "Please take care of permissions.\n";
107 } else {
108 print writeVPNC(readPCF($src));
110 $src->close() || die "Unable to close file ".$ARGV[0]."\n";
111 if ($needs_cert) {
112 print STDERR "\nDon't forget to copy the needed certificate(s).\n\n";
114 } else {
115 print STDERR "$0 converts VPN-config files from pcf to vpnc-format.\n";
116 print STDERR "Usage: $0 <pcf file> [vpnc file]\n";