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