Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # Tune the configuration file |
| 4 | |
| 5 | use warnings; |
| 6 | use strict; |
| 7 | |
| 8 | my $usage = <<EOU; |
| 9 | $0 [-f <file>] full |
| 10 | $0 [-f <file>] unset <name> |
| 11 | $0 [-f <file>] set <name> [<value>] |
| 12 | EOU |
| 13 | |
| 14 | # Things that shouldn't be enabled with "full". |
| 15 | # Notes: |
| 16 | # - POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 and |
| 17 | # POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the |
| 18 | # respective tests were adapted |
| 19 | my @excluded = qw( |
| 20 | POLARSSL_HAVE_INT8 |
| 21 | POLARSSL_HAVE_INT16 |
| 22 | POLARSSL_HAVE_SSE2 |
| 23 | POLARSSL_PLATFORM_NO_STD_FUNCTIONS |
| 24 | POLARSSL_ECP_DP_M221_ENABLED |
| 25 | POLARSSL_ECP_DP_M383_ENABLED |
| 26 | POLARSSL_ECP_DP_M511_ENABLED |
| 27 | POLARSSL_NO_DEFAULT_ENTROPY_SOURCES |
| 28 | POLARSSL_NO_PLATFORM_ENTROPY |
| 29 | POLARSSL_SSL_HW_RECORD_ACCEL |
Manuel Pégourié-Gonnard | 86b2908 | 2014-11-06 02:28:34 +0100 | [diff] [blame^] | 30 | POLARSSL_SSL_DISABLE_RENEGOTIATION |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 31 | POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 |
| 32 | POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 33 | POLARSSL_ZLIB_SUPPORT |
| 34 | POLARSSL_PKCS11_C |
| 35 | _ALT\s*$ |
| 36 | ); |
| 37 | |
| 38 | my $config_file = "include/polarssl/config.h"; |
| 39 | |
| 40 | # get -f option |
| 41 | if (@ARGV >= 2 && $ARGV[0] eq "-f") { |
| 42 | shift; # -f |
| 43 | $config_file = shift; |
| 44 | |
| 45 | -f $config_file or die "No such file: $config_file\n"; |
| 46 | } else { |
| 47 | if (! -f $config_file) { |
| 48 | chdir '..' or die; |
| 49 | -d $config_file |
| 50 | or die "Without -f, must be run from root or scripts\n" |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | # get action |
| 55 | die $usage unless @ARGV; |
| 56 | my $action = shift; |
| 57 | |
| 58 | my ($name, $value); |
| 59 | if ($action eq "full") { |
| 60 | # nothing to do |
| 61 | } elsif ($action eq "unset") { |
| 62 | die $usage unless @ARGV; |
| 63 | $name = shift; |
| 64 | } elsif ($action eq "set") { |
| 65 | die $usage unless @ARGV; |
| 66 | $name = shift; |
| 67 | $value = shift if @ARGV; |
| 68 | } else { |
| 69 | die $usage; |
| 70 | } |
| 71 | die $usage if @ARGV; |
| 72 | |
| 73 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 74 | my @config_lines = <$config_read>; |
| 75 | close $config_read; |
| 76 | |
| 77 | my $exclude_re = join '|', @excluded; |
| 78 | |
| 79 | open my $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 80 | |
| 81 | my $done; |
| 82 | for my $line (@config_lines) { |
| 83 | if ($action eq "full") { |
| 84 | if ($line =~ /name SECTION: Module configuration options/) { |
| 85 | $done = 1; |
| 86 | } |
| 87 | |
| 88 | if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) { |
| 89 | $line =~ s!^//!!; |
| 90 | } |
| 91 | } elsif ($action eq "unset") { |
| 92 | if (!$done && $line =~ /^\s*#define\s*$name/) { |
| 93 | $line = '//' . $line; |
| 94 | $done = 1; |
| 95 | } |
| 96 | } elsif (!$done && $action eq "set") { |
| 97 | if ($line =~ m!^(?://)?\s*#define\s*$name!) { |
| 98 | $line = "#define $name"; |
| 99 | $line .= " $value" if defined $value && $value ne ""; |
| 100 | $line .= "\n"; |
| 101 | $done = 1; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | print $config_write $line; |
| 106 | } |
| 107 | |
| 108 | close $config_write; |
| 109 | |
| 110 | warn "configuration section not found" if ($action eq "full" && !$done); |
| 111 | warn "$name not found" if ($action ne "full" && !$done); |
| 112 | |
| 113 | __END__ |