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