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